Files
rag-technical-docs-assistant/docs/evaluation/local-s2s/watch-phase4-next.py
11linear11 a94b8cd3ea feat(voice): implement local S2S retained history and provider selection
Add pinned CPU engine dependencies, retained interrupted-response history, provider admission, offline evaluation and isolated browser tooling.

Record Phase 2 acceptance and Phase 3/4 preparation, bounded paid-test accounting and interruption incident findings. Human interruption acceptance, paired OpenAI evaluation and rollback remain pending.
2026-10-16 17:09:27 -04:00

69 lines
3.1 KiB
Python

"""Own the candidate engine: startup, memory floor and 30-minute handoff expiry."""
import json
from pathlib import Path
import signal
import subprocess
import time
import sys
root=Path.cwd()
compose=['docker','compose','--env-file','.local-s2s-build/browser-test.env','-f','docker-compose.local-s2s-test.yml','-f','docs/evaluation/local-s2s/phase-02-browser-linux.override.yml']
compose+=['-f','docs/evaluation/local-s2s/phase-04-browser.override.yml']
compose+=['-f','docs/evaluation/local-s2s/phase-04-next-browser.override.yml']
repair=False
if repair:compose+=['-f','docs/evaluation/local-s2s/phase-02-browser-repair.override.yml']
evidence='phase4-next-paid'
state={'repair':repair,'stage':'preflight','minimum_host_available_bytes':2**63}
report=root/'.local-s2s-build/phase4-next-watch.json'
stop=False
def available():
return int(next(x.split()[1] for x in Path('/proc/meminfo').read_text().splitlines() if x.startswith('MemAvailable:')))*1024
def save():report.write_text(json.dumps(state,indent=2)+'\n')
def terminate(*args):
global stop
stop=True
for sig in (signal.SIGINT,signal.SIGTERM):signal.signal(sig,terminate)
if (root/'.local-s2s-build'/evidence/'browser-paid-ledger.json').exists():
raise SystemExit('Browser paid ledger exists; no automatic restart')
if available()<=5*1024**3:
state.update(stage='blocked',reason='insufficient_host_headroom_before_start');save()
raise SystemExit(1)
began=time.monotonic()
owned=False
try:
owned=True
subprocess.run(compose+['up','--pull','never','-d','local-s2s-engine'],check=True,timeout=30)
state['stage']='starting';save()
while not stop:
free=available()
state['minimum_host_available_bytes']=min(state['minimum_host_available_bytes'],free)
if free<2500*1024**2:
state['reason']='host_memory_headroom';break
if time.monotonic()-began>1800:
state['reason']='handoff_expired_30_minutes';break
status=subprocess.run(compose+['ps','--all','--format','json','local-s2s-engine'],capture_output=True,text=True,timeout=10)
rows=[json.loads(line) for line in status.stdout.splitlines() if line.startswith('{')]
if rows and rows[0].get('State') not in ('running','created'):
state['reason']='engine_exited';break
if state['stage']=='starting':
logs=subprocess.run(compose+['logs','--no-log-prefix','--tail','10','local-s2s-engine'],capture_output=True,text=True,timeout=10).stdout
if '"stage": "startup"' in logs:
state.update(stage='ready',startup_seconds=round(time.monotonic()-began,3))
print(json.dumps({'stage':'ready','startup_seconds':state['startup_seconds']}),flush=True)
elif time.monotonic()-began>180:
state['reason']='startup_wall_deadline';break
save()
time.sleep(2)
except BaseException as exc:
state['reason']=type(exc).__name__
raise
finally:
if owned:
subprocess.run(compose+['stop','--timeout','5','local-s2s-engine'],timeout=15)
state.update(stage='stopped',elapsed_seconds=round(time.monotonic()-began,3))
state.setdefault('reason','operator_stop');save()