"""Run bounded public pilots and a separate evaluator using the existing Fly runner.""" import argparse from contextlib import ExitStack import json from pathlib import Path import subprocess import sys import time from urllib.parse import urlparse from eval_overlap import coverage from eval_observations import qualify p = argparse.ArgumentParser(__doc__) p.add_argument('--zkv-binary', type=Path, required=True) p.add_argument('--rocks-binary', type=Path) p.add_argument('--rocks-source', type=Path) p.add_argument('--eval-binary', type=Path, required=True) p.add_argument('--seconds', type=int, choices=[120, 600], default=120) p.add_argument('--output', type=Path, required=True) a = p.parse_args() if a.rocks_binary and not a.rocks_source: p.error('rocks-source is required with rocks-binary for accurate fingerprints') a.output.mkdir(parents=True, exist_ok=False) runner = Path(__file__).with_name('fly_live.py').resolve() pilots = [('grouped', a.zkv_binary, 'zkv')] if a.rocks_binary: pilots.append(('rocks', a.rocks_binary, 'rocks')) children = [] with ExitStack() as stack: try: for label, binary, engine in pilots: output = a.output / label log = stack.enter_context((a.output / f'{label}-driver.log').open('w')) command = [sys.executable, str(runner), '--binary', str(binary.resolve()), '--output', str(output), '--seconds', str(a.seconds), '--memory', '2048', '--cpu-kind', 'performance', '--cpus', '1', '--engine', engine, '--encoding', 'grouped' if engine == 'zkv' else 'legacy', '--network-mib', '6144', '--public', '--linger', '120'] if engine == 'rocks': command += ['--source', str(a.rocks_source.resolve())] process = subprocess.Popen(command, stdout=log, stderr=subprocess.STDOUT) children.append((process, output)) deadline = time.monotonic() + 180 ready = {} while len(ready) < len(children) and time.monotonic() < deadline: for process, output in children: if process.poll() is not None: raise RuntimeError(f'pilot exited before external evaluation: {output}') if output in ready: continue try: state = json.loads((output / 'fly-resources.json').read_text()) url = state['public_url'] ip = next((row['Address'] for row in state.get('addresses', []) if row['Type'] == 'shared_v4'), None) if ip is None: continue except (OSError, ValueError, KeyError): continue health = subprocess.run(['curl', '--silent', '--show-error', '--max-time', '2', '--output', '/dev/null', '--write-out', '%{http_code}', '--resolve', f'{urlparse(url).hostname}:443:{ip}', url + '/_healthz'], capture_output=True, text=True) with (a.output / 'readiness.jsonl').open('a') as diagnostic: diagnostic.write(json.dumps({'time_ms': int(time.time() * 1000), 'url': url, 'assigned_ipv4': ip, 'returncode': health.returncode, 'status': health.stdout, 'error': health.stderr}) + '\n') if health.returncode == 0 and health.stdout.strip() == '200': ready[output] = urlparse(url).hostname if len(ready) < len(children): time.sleep(2) assert len(ready) == len(children), 'public pilot readiness deadline exceeded' command = [sys.executable, str(runner), '--references-only', '--eval-binary', str(a.eval_binary.resolve()), '--output', str(a.output / 'observer'), '--seconds', str(a.seconds), '--memory', '2048', '--cpu-kind', 'performance', '--cpus', '1'] for host in ready.values(): command += ['--eval-pilot', host] print('External evaluation of ' + ', '.join(ready.values()), flush=True) subprocess.run(command, check=True) finally: for process, output in children: if process.poll() is None: try: state = json.loads((output / 'fly-resources.json').read_text()) subprocess.run(['fly', 'ssh', 'console', '-a', state['app'], '-C', 'touch /tmp/live/evaluation-done'], timeout=20, check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except (OSError, ValueError, KeyError, subprocess.SubprocessError): pass failures = [] for process, output in children: code = process.wait() try: state = json.loads((output / 'fly-resources.json').read_text()) if not state['cleanup_verified'] or code != 0: failures.append(str(output)) except (OSError, ValueError, KeyError): failures.append(str(output)) assert not failures, 'pilot or cleanup checks failed; see driver logs: ' + ', '.join(failures) report = json.loads((a.output / 'observer/output/eval-observations.json').read_text()) references = ['bsky.network', 'relay.waow.tech'] result = {'transport': qualify(report, references + list(ready.values())), 'did_coverage': coverage(report, references, list(ready.values()))} (a.output / 'comparison.json').write_text(json.dumps(result, indent=2) + '\n') assert result['transport']['valid_window'], 'external observation window was incomplete' print(json.dumps(result, indent=2))