# /// script # requires-python = '>=3.12' # dependencies = ['pyserial', 'anthropic', 'numpy', 'pillow', 'gua-eval'] # # [tool.uv.sources] # gua-eval = { path = '/home/zhanwenchen/personalgui-accel', editable = true } # /// '''Measure the REAL closed-loop latency on the live rig: Elgato capture -> model decision -> KMBox actuation, end to end, on actual hardware. Unlike bench_latency.py (which times stages in isolation), this runs the whole loop the way an agent would: pull a frame off the capture card, ask a model for one action on that real frame, and send that action to the target over the KMBox — then measure each leg with one clock. Actuation is a move-only round trip (cursor is returned to its start); no clicks, so it is safe to run over any target screen. uv run measure_live_loop.py --model claude-haiku-4-5 --n 5 uv run measure_live_loop.py --model 'local:nvidia/Qwen3.6-35B-A3B-NVFP4@http://127.0.0.1:8000/v1' --n 5 ''' import time from argparse import ArgumentParser from pathlib import Path from statistics import median from hid_kmbox import KMBox from run_wt import load_env, make_client from gua_eval.actions import parse_action from gua_eval.capture import ElgatoSource PROMPT = ('You are looking at a computer screen. Reply with EXACTLY ONE ' 'action and nothing else: click(x,y) in screen pixels, or none.') def main() -> int: ap = ArgumentParser(description='Real live-rig closed-loop latency.') ap.add_argument('--model', default='claude-haiku-4-5') ap.add_argument('--n', type=int, default=5) ap.add_argument('--video', default='/dev/video0') ap.add_argument('--port', default='/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0') ap.add_argument('--out', type=Path, default=Path('experiments/live_loop.json')) args = ap.parse_args() load_env(Path(__file__).parent / '.env') client = make_client(args.model) cap = ElgatoSource(device=args.video) km = KMBox(args.port) km.open() gen = cap.frames() # discard the capture card's first (often malformed) frames for _ in range(15): next(gen) rows: list[dict] = [] try: for i in range(args.n): t0 = time.perf_counter() jpeg, _meta = next(gen) t_cap = time.perf_counter() r = client.decide(jpeg, PROMPT) _ = parse_action(r.text.strip()) t_inf = time.perf_counter() km.move(40, 0) # safe move-only round trip km.move(-40, 0) t_act = time.perf_counter() rows.append({'capture_ms': (t_cap - t0) * 1e3, 'infer_ms': (t_inf - t_cap) * 1e3, 'actuate_ms': (t_act - t_inf) * 1e3, 'total_ms': (t_act - t0) * 1e3, 'text': r.text.strip()[:40]}) print(f'loop {i+1}: cap {rows[-1]["capture_ms"]:.1f} ' f'infer {rows[-1]["infer_ms"]:.0f} ' f'act {rows[-1]["actuate_ms"]:.1f} ' f'total {rows[-1]["total_ms"]:.0f} ms') finally: cap.close() km.close() def med(k: str) -> float: return round(median(r[k] for r in rows), 2) summary = {'model': client.name, 'n': len(rows), 'capture_ms': med('capture_ms'), 'infer_ms': med('infer_ms'), 'actuate_ms': med('actuate_ms'), 'total_ms': med('total_ms'), 'rows': rows} args.out.parent.mkdir(parents=True, exist_ok=True) import json args.out.write_text(json.dumps(summary, indent=1)) print(f'\nMEDIAN capture {summary["capture_ms"]} infer {summary["infer_ms"]}' f' actuate {summary["actuate_ms"]} total {summary["total_ms"]} ms' f' -> {args.out}') return 0 if __name__ == '__main__': raise SystemExit(main())