"""Foreground isolated before/after API + browser proof; always remove own state.""" import io import json import os from pathlib import Path import shutil import signal import socket import subprocess import sys import tarfile import tempfile import time import urllib.request root = Path(__file__).resolve().parents[1] os.chdir(root) out = root.parent / 'evidence/hel809' out.mkdir(parents=True, exist_ok=True) for port in (18249, 18250): with socket.socket() as s: assert s.connect_ex(('127.0.0.1', port)) != 0, f'Port in use: {port}' bootstrap = '''from pathlib import Path import os from calliope import config root = Path(os.environ['HEL809_DATA']) config.CONFIG_FILE = root / 'settings.json' config.settings = config.Settings(data_dir=root/'data', assets_dir=root/'data/assets', comfyui_base_url='http://127.0.0.1:1', llm_base_url='http://127.0.0.1:1/v1', dry_run=False) config.settings.ensure_storage_dirs() from calliope.queue.manager import queue_manager queue_manager.paused = True from calliope.main import create_app import uvicorn uvicorn.run(create_app(),host='127.0.0.1',port=18249,log_level='warning') ''' cleanup = [] with tempfile.TemporaryDirectory(prefix='hel809-', dir=out) as tmp: work = Path(tmp) old = work/'before'; old.mkdir() archive = subprocess.check_output(['git','archive','cb410fbdeaa87528aaaab1f0dbffeb0240a9a32e','calliope-backend/src']) with tarfile.open(fileobj=io.BytesIO(archive)) as bundle: bundle.extractall(old, filter='data') server = work/'server.py'; server.write_text(bootstrap) for phase, src in [('before',old/'calliope-backend/src'),('after',root/'calliope-backend/src')]: data = work/phase/'runtime'; data.mkdir(parents=True) env = {**os.environ, 'PYTHONPATH':str(src), 'HEL809_DATA':str(data), 'HEL809_PHASE':phase,'HEL809_EVIDENCE':str(out)} with (out/(phase+'-backend.log')).open('w') as log: child = subprocess.Popen([sys.executable,str(server)],env=env,stdout=log,stderr=subprocess.STDOUT) try: for attempt in range(100): try: urllib.request.urlopen('http://127.0.0.1:18249/api/health',timeout=1);break except Exception: if child.poll() is not None: raise RuntimeError('Backend failed: '+phase) if attempt==99:raise time.sleep(.1) subprocess.run(['node','e2e/hel809.cjs'],env=env,check=True,timeout=100) finally: daemon = json.loads(subprocess.check_output(['multica','daemon','status','--output','json'])) assert child.pid != daemon['pid'] if child.poll() is None:child.send_signal(signal.SIGTERM) try:child.wait(timeout=10) except subprocess.TimeoutExpired:child.kill();child.wait() cleanup.append({'phase':phase,'pid':child.pid,'exit':child.returncode}) # TemporaryDirectory removes only this run's database and before-source copy. (out/'cleanup.json').write_text(json.dumps({'children':cleanup,'temporary_state_removed':not work.exists()},indent=2)) print('CLEANUP',cleanup, 'state removed',not work.exists())