Files
myfileagent-pipeline/ci/run_live_review.py
T

61 lines
3.7 KiB
Python
Raw Normal View History

"""Run the pinned reviewer inside the actual capped Actions job and retain measurements."""
import hashlib
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
root = Path(__file__).resolve().parents[1]
lock_bytes = (root/'reviewer/model-lock.json').read_bytes()
lock = json.loads(lock_bytes)
runtime = root/'out/runtime'
cgroup = Path('/sys/fs/cgroup')
limit = int((cgroup/'memory.max').read_text())
hard_ceiling = 384000000
target = lock['memory_target_bytes']
assert target == 250000000, 'Model lock must preserve the agreed memory target'
assert hard_ceiling - os.sysconf('SC_PAGE_SIZE') < limit <= hard_ceiling, 'Actual Actions container must have the page-rounded hard ceiling'
assert (cgroup/'memory.swap.max').read_text().strip() == '0', 'Swap must be disabled'
assert not os.environ.get('REVIEWER_BACKEND_COMMAND'), 'Fixture override is forbidden'
private = Path('/private-model')/lock['quantized_filename']
assert private.is_file(), 'Model must be downloaded into private tmpfs'
library = runtime/'libgomp-root'/lock['libgomp_library_path']
assert hashlib.sha256(library.read_bytes()).hexdigest() == lock['libgomp_library_sha256']
env = os.environ.copy()
env.update(REVIEWER_MODEL_PATH=str(private),
REVIEWER_LLAMA_COMPLETION=str(runtime/f"llama-{lock['runtime_tag']}"/lock['entrypoint']),
LD_LIBRARY_PATH=str(library.parent)+':'+str(runtime/f"llama-{lock['runtime_tag']}"))
result = subprocess.run([sys.executable,'-m','reviewer','--input','demo/demo.py',
'--source-commit',env['SOURCE_SHA'],'--out-dir','out/review'],cwd=root,env=env)
peak = int((cgroup/'memory.peak').read_text())
events = {k:int(v) for k,v in (line.split() for line in (cgroup/'memory.events').read_text().splitlines())}
measurement = dict(scope='job cgroup including preparation, reviewer and private tmpfs model',
requested_memory_limit_bytes=hard_ceiling,memory_limit_bytes=limit,
memory_target_bytes=target,memory_target_exceeded=peak > target,
memory_peak_bytes=peak,memory_events=events,
swap_limit_bytes=0,reviewer_exit_code=result.returncode)
Path('out').mkdir(exist_ok=True)
Path('out/resource.json').write_text(json.dumps(measurement,indent=2)+'\n')
print(json.dumps(measurement))
if peak > target:
print(f'WARNING: measured Actions job memory peak {peak} bytes exceeds the {target}-byte project target; continuing under the {limit}-byte hard ceiling', file=sys.stderr)
if events.get('max', 0):
print(f'WARNING: Actions job encountered {events["max"]} hard-ceiling pressure events; check resource.json for OOM events', file=sys.stderr)
assert result.returncode == 0 and events['oom_kill'] == 0 and peak <= limit, 'Live model or hard memory ceiling failed'
p=Path('out/review/review.json');data=json.loads(p.read_text())
assert data['mode']=='live' and data['status']=='corrected'
data['resource_measurement']=measurement
data['model_lock_sha256']=hashlib.sha256(lock_bytes).hexdigest()
data['raw_response']=(p.parent/'raw_response.txt').read_text()
data['runtime_response']=json.loads((p.parent/'runtime_response.json').read_text())
p.write_text(json.dumps(data,indent=2)+'\n')
subprocess.run([sys.executable,'tests/smoke_demo.py',str(p.parent/'reviewed.py')],check=True)
print(f'LIVE REVIEW ACCEPTED for source commit {data["source_commit"]}', flush=True)
print(f'Model: {data["model_id"]}; input tokens: {data["input_tokens"]}; output tokens: {data["output_tokens"]}', flush=True)
print(f'Raw model response: {data["raw_response"]!r}', flush=True)
print('Accepted one-character source diff:', flush=True)
print(data['diff'], end='' if data['diff'].endswith('\n') else '\n', flush=True)
print('Reviewed Python execution: PASS (stdout: hello)', flush=True)