2026-11-27 19:08:59 +00:00
"""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 ())
2026-11-28 20:58:48 +00:00
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'
2026-11-27 19:08:59 +00:00
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' ] } " ))
2026-11-27 19:15:48 +00:00
result = subprocess . run ([ sys . executable , '-m' , 'reviewer' , '--input' , 'demo/demo.py' ,
2026-11-27 19:08:59 +00:00
'--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 ())}
2026-11-27 19:15:48 +00:00
measurement = dict ( scope = 'job cgroup including preparation, reviewer and private tmpfs model' ,
2026-11-28 20:58:48 +00:00
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 ,
2026-11-27 19:08:59 +00:00
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 ))
2026-11-28 20:58:48 +00:00
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'
2026-11-27 19:08:59 +00:00
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 )
2026-11-28 21:24:06 +00:00
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 )