"""Run one owned native test at a time, with memory and paid-ledger guards.""" import json import os from pathlib import Path import subprocess import sys import time root = Path.cwd() image = 'sha256:89c7295e521903c16b8ab72f8049b8166ceeb718ac7d531b815dd5d7613dea7b' records = [] duplex = '--duplex' in sys.argv retention = '--retention' in sys.argv or '--retention-active' in sys.argv active_retention = '--retention-active' in sys.argv if retention: image = 'rag-local-s2s-candidate:phase3-retain-v1' modes = tuple(a for a in sys.argv[1:] if a not in ('--duplex','--retention','--retention-active')) or ('normal', 'sigint', 'sigterm', 'failure') if any(mode not in ('normal', 'sigint', 'sigterm', 'failure', 'real') for mode in modes): raise SystemExit('Unknown test mode') def available(): return int(next(x.split()[1] for x in Path('/proc/meminfo').read_text().splitlines() if x.startswith('MemAvailable:'))) * 1024 def stop_owned(name, process): subprocess.run(['docker', 'stop', '--time', '3', name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=8) if process.poll() is None: subprocess.run(['docker', 'kill', name], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5) for mode in modes: if mode == 'real' and (root/'.local-s2s-build/native-paid/native-paid-ledger.json').exists(): raise SystemExit('Paid ledger exists; refusing repeat') name = 'rag-s2s-evaluation-' + mode + '-' + str(os.getpid()) command = ['docker', 'run', '--rm', '--name', name, '--network', 'bridge' if mode == 'real' else 'none', '--read-only', '--cap-drop', 'ALL', '--security-opt', 'no-new-privileges', '--memory', '3g', '--memory-swap', '3g', '--cpus', '2', '--pids-limit', '128', '--tmpfs', '/tmp:rw,noexec,nosuid,size=64m', '-e', 'PYTHONPATH=/app', '-e', 'OMP_NUM_THREADS=1', '-e', 'OPENBLAS_NUM_THREADS=1'] mounts = [('.local-s2s-build/assets-linux/nltk', '/assets/nltk'), ('.local-s2s-build/assets-linux/silero-vad', '/vad/silero-vad'), ('docs/evaluation/local-s2s/phase-02-linux-assets.json', '/assets/assets.json'), ('.local-s2s-build/session-input-24k.pcm', '/checks/input.pcm'), ('docs/evaluation/local-s2s/image-session-smoke.py', '/checks/session.py')] if mode == 'real': mounts = [(s, t) for s, t in mounts if t != '/checks/input.pcm'] mounts += [('.local-s2s-build/native-paid/input.pcm', '/checks/input.pcm'), ('docs/evaluation/local-s2s/native-budget-transport.py', '/checks/native-budget-transport.py'), ('.local-s2s-build/openrouter-test.key', '/secret/openrouter-test.key')] # Match the owner of the private 0600 credential; do not broaden its mode. command += ['--user', str(os.getuid()) + ':' + str(os.getgid()), '-v', str(root/'.local-s2s-build/native-paid') + ':/evidence:rw'] for source, target in mounts: command += ['-v', str(root/source) + ':' + target + ':ro'] command += ['--entrypoint', 'python', image, '/checks/session.py', mode] if retention: if mode != 'normal':raise SystemExit('Retention requires normal fixture mode') command += ['--retention-active' if active_retention else '--retention'] if duplex: if mode != 'normal':raise SystemExit('Duplex reproduction requires normal fixture mode') command += ['--duplex'] report = {'mode': mode, 'command': command, 'minimum_host_available_bytes': available(), 'aborted': None, 'exit_code': None} began = time.monotonic() process = None try: if report['minimum_host_available_bytes'] <= 5*1024**3: report['aborted'] = 'insufficient_host_headroom_before_start' else: with (root/('.local-s2s-build/guarded-'+mode+'.jsonl')).open('w') as output, \ (root/('.local-s2s-build/guarded-'+mode+'.stderr')).open('w') as error: process = subprocess.Popen(command, stdout=output, stderr=error) while process.poll() is None: free = available() report['minimum_host_available_bytes'] = min(report['minimum_host_available_bytes'], free) if free < 2500*1024**2 or time.monotonic()-began > 180: report['aborted'] = 'host_memory_headroom' if free < 2500*1024**2 else 'wall_deadline' stop_owned(name, process) break time.sleep(1) report['exit_code'] = process.wait(timeout=10) except BaseException as exc: report['aborted'] = type(exc).__name__ raise finally: try: if process is not None and process.poll() is None: stop_owned(name, process) finally: report['elapsed_seconds'] = round(time.monotonic()-began, 3) records.append(report) (root/('.local-s2s-build/guarded-native-retention-runs.json' if retention else '.local-s2s-build/guarded-native-runs.json')).write_text(json.dumps(records, indent=2)+'\n') print(json.dumps({k:v for k,v in report.items() if k != 'command'}), flush=True) if report['aborted'] or report['exit_code'] != 0: raise SystemExit(1)