"""Bounded CPU replay of archived range for geometry-processing diagnostics. Run in an isolated navigation container, with no vehicle or GPU authority. This measures the terrain adapter, not route or semantic acceptance. """ import argparse import cProfile import datetime as dt import hashlib import http.client import json import pstats import time from pathlib import Path import numpy as np from terrain_costs import TerrainCostNormalizer def main(): parser = argparse.ArgumentParser() parser.add_argument("--run", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) parser.add_argument("--first-frame", type=int, required=True) parser.add_argument("--limit", type=int, default=80) args = parser.parse_args() if args.output.exists() or not 1 <= args.limit <= 200: raise ValueError("New evidence directory and bounded frame count required") args.output.mkdir(parents=True) source = args.run / "camera/decisions.jsonl" rows = [json.loads(line) for line in source.read_text().splitlines()] rows = [r for r in rows if r["frame_id"] >= args.first_frame and r.get("goal")][: args.limit] connection = http.client.HTTPConnection("127.0.0.1", 8010, timeout=5) report = dict( utc=dt.datetime.now(dt.UTC).isoformat(), monotonic_ns=time.monotonic_ns(), input_sha256=hashlib.sha256(source.read_bytes()).hexdigest(), authority="offline-geometry-only-no-motion", samples=[], ) for row in rows: frame = args.run / "camera" / f"{row['frame_id']:08d}.range.npz" observation = np.load(frame) payload = dict( points=observation["points"].tolist(), pose=observation["pose"].tolist(), goal=row["goal"], max_speed_mps=0.15, body_contact_height_m=float(observation["body_contact_height_m"]), include_terrain=row is rows[-1], ) started = time.monotonic() connection.request("POST", "/plan", body=json.dumps(payload)) response = connection.getresponse() result = json.loads(response.read()) if response.status != 200: raise RuntimeError(result) terrain = result.pop("observed_terrain", None) report["samples"].append( dict( frame=row["frame_id"], result=result, elapsed_ms=(time.monotonic() - started) * 1000, source_sha256=hashlib.sha256(frame.read_bytes()).hexdigest(), ) ) time.sleep(0.18) connection.close() if terrain is None: raise RuntimeError("No matching terrain diagnostic returned") terrain = np.asarray(terrain, dtype=np.float32) np.save(args.output / "terrain.npy", terrain) normalize = TerrainCostNormalizer() profiler = cProfile.Profile() profiler.enable() result, count = normalize(terrain) profiler.disable() with (args.output / "profile.txt").open("w") as stream: pstats.Stats(profiler, stream=stream).sort_stats("cumulative").print_stats(30) timings = [] for _ in range(8): started = time.monotonic() repeated, repeated_count = normalize(terrain) timings.append((time.monotonic() - started) * 1000) assert np.array_equal(result, repeated) and repeated_count == count report.update( points=len(terrain), corrected=count, repeated_ms=timings, completed_monotonic_ns=time.monotonic_ns(), ) (args.output / "report.json").write_text(json.dumps(report, indent=2)) print(json.dumps(dict(points=len(terrain), corrected=count, repeated_ms=timings))) if __name__ == "__main__": main()