Files
myfileagent-pipeline/reviewer/prepare_runtime.py
T
Codex 9275fcdcae
myfileagent-python-ci/pipeline/head This commit looks good
Final project review (live) / review (push) Failing after 1m24s
Integrate real reviewer with measured Actions execution and live Jenkins pipeline
2026-11-27 19:08:59 +00:00

86 lines
3.7 KiB
Python

"""Prepare checksum-pinned model, llama.cpp CPU runtime, and libgomp1."""
import argparse
import hashlib
import json
from pathlib import Path
import shutil
import subprocess
import tarfile
import urllib.request
LOCK = Path(__file__).with_name("model-lock.json")
def digest(path):
value = hashlib.sha256()
with Path(path).open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
value.update(chunk)
return value.hexdigest()
def fetch(url, target, expected):
if target.is_file() and digest(target) == expected:
return
part = target.with_name(target.name + ".part")
part.unlink(missing_ok=True)
try:
with urllib.request.urlopen(urllib.request.Request(url, headers={"User-Agent": "ssw590-member1"}), timeout=60) as source, part.open("wb") as dest:
shutil.copyfileobj(source, dest, 1024 * 1024)
if digest(part) != expected:
raise ValueError(f"checksum mismatch: {target.name}")
part.replace(target)
finally:
part.unlink(missing_ok=True)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--dest", type=Path, required=True, help="directory outside Git for model/runtime files")
parser.add_argument("--model-dir", type=Path, help="optional private tmpfs model directory")
args = parser.parse_args()
lock = json.loads(LOCK.read_text())
args.dest.mkdir(parents=True, exist_ok=True)
model_dir = args.model_dir or args.dest
model_dir.mkdir(parents=True, exist_ok=True)
model = model_dir / lock["quantized_filename"]
archive = args.dest / lock["runtime_archive"]
fetch(lock["quantized_url"], model, lock["quantized_sha256"])
fetch(lock["runtime_url"], archive, lock["runtime_archive_sha256"])
package = args.dest / lock["libgomp_package"]
fetch(lock["libgomp_url"], package, lock["libgomp_package_sha256"])
with tarfile.open(archive) as bundle:
members = bundle.getmembers()
root_name = "llama-" + lock["runtime_tag"]
if any(member.name != root_name and not member.name.startswith(root_name + "/") for member in members):
raise ValueError("unexpected runtime archive layout")
# The pinned compiler has Python 3.11 without tarfile.data_filter.
# Reject traversal, special files and escaping link targets before extraction.
from pathlib import PurePosixPath
for member in members:
for name in (member.name, member.linkname):
path = PurePosixPath(name)
if path.is_absolute() or ".." in path.parts:
raise ValueError("unsafe archive member/link")
if not (member.isfile() or member.isdir() or member.issym() or member.islnk()):
raise ValueError("special archive member")
if hasattr(tarfile, "data_filter"):
bundle.extractall(args.dest, filter="data")
else:
bundle.extractall(args.dest)
server = args.dest / f"llama-{lock['runtime_tag']}" / lock["entrypoint"]
if not server.is_file() or digest(server) != lock["runtime_entrypoint_sha256"]:
raise ValueError("runtime entrypoint checksum mismatch")
libgomp_root = args.dest / "libgomp-root"
subprocess.run(["dpkg-deb", "--extract", str(package), str(libgomp_root)], check=True)
library = libgomp_root / lock["libgomp_library_path"]
if not library.is_file() or digest(library) != lock["libgomp_library_sha256"]:
raise ValueError("libgomp1 library checksum mismatch")
print(f"REVIEWER_MODEL_PATH={model}")
print(f"REVIEWER_LLAMA_COMPLETION={server}")
print(f"LD_LIBRARY_PATH={library.parent}:{server.parent}")
if __name__ == "__main__":
main()