#!/usr/bin/env python3 """gen_inference_paths_truth.py — freeze/replay parity truth for inference_paths (#145 census + trace wiring, dichotomy wave #2479). Freeze mode (default): exercises EVERY public function of the LIVE Python oracle (src/rune/inference_paths.py) across every branch and records (kernel_input, expected_kernel_output) pairs into src/go/inference-paths/truth.json (the GO port's battery replays this VERBATIM). Replay mode (--replay): re-runs the SAME scenarios against the live Python oracle and asserts facade-level parity against the frozen file. Kernel-level (Go) parity runs via `go test ./...` + the `replay` CLI in src/go/inference-paths — the battery embeds the frozen file verbatim. No-clock law: the wall clock is frozen by patching time.gmtime, so every kernel payload carries its own `at` and replay is deterministic regardless of run date. Boundary law (ledger verdict: GO — the journal write is I/O-bound file IO and the census is simple counting): the JOURNAL attempt (rune.inference_trace import + record_trace + the journaled flag) and the grep-verified census-completeness obligations stay in the Python facade — the Go kernel owns the record shape (schema_version, kind, module, function, model, task_id, duration_ms, at) and census_report. Journal attempts in the battery write to throwaway temp dirs (never the real journal); the failure branch passes a non-creatable dir so the oracle's except path (journaled=False) is exercised deterministically. Deep-copy law: every input is deep-copied before it reaches the oracle. """ import copy import datetime import json import os import sys import tempfile import time REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TRUTH_PATH = os.path.join(REPO, "src", "go", "inference-paths", "truth.json") REPLAY = "--replay" in sys.argv os.environ["RUNE_IT_CORE"] = "python" # poisoned-truth law: the journal # path's own delegated serializer must not vary under the freeze sys.path.insert(0, os.path.join(REPO, "src")) sys.path.insert(0, os.path.join(REPO, "src", "rune")) import inference_paths as oracle_mod # noqa: E402 FROZEN_AT = "2026-10-12T12:34:56Z" FROZEN_STRUCT = time.strptime(FROZEN_AT, "%Y-%m-%dT%H:%M:%SZ") # no-clock law: freeze the wall clock BEFORE any scenario runs oracle_mod.time.gmtime = lambda secs=None: FROZEN_STRUCT class Case: def __init__(self, name, fn): self.name = name self.fn = fn # ── trace_side_path ────────────────────────────────────────────────────────── def _tsp_case(rec, name, trace_dir, **kwargs): args = {"module": "", "function": "", "model": "", "task_id": "", "duration_ms": 0.0} args.update(kwargs) args_c = copy.deepcopy(args) got = oracle_mod.trace_side_path(trace_dir=trace_dir, **copy.deepcopy(args)) rec(name, {**args_c, "at": FROZEN_AT}, got, {"record": {k: v for k, v in got.items() if k != "journaled"}}) def c_tsp_defaults(rec): _tsp_case(rec, "tsp-defaults-side-path", tempfile.mkdtemp(prefix="ip_t_"), module="goal", function="_goal") def c_tsp_full(rec): _tsp_case(rec, "tsp-full-fields", tempfile.mkdtemp(prefix="ip_t_"), module="rune_operator", function="_runtime_for_model", model="llama3", task_id="t1", duration_ms=12.5) def c_tsp_zero_duration(rec): _tsp_case(rec, "tsp-zero-duration", tempfile.mkdtemp(prefix="ip_t_"), module="witness", function="_witness") def c_tsp_float_precision(rec): _tsp_case(rec, "tsp-float-precision", tempfile.mkdtemp(prefix="ip_t_"), module="tiered_executor", function="_execute", model="qwen2.5", task_id="task-99", duration_ms=123.456) def c_tsp_journal_failure(rec): # non-creatable dir → record_trace raises → journaled=False branch _tsp_case(rec, "tsp-journal-failure-false", "/nonexistent-ip-truth/nope", module="curator", function="_embed") def c_tsp_empty_module(rec): _tsp_case(rec, "tsp-empty-module-function", tempfile.mkdtemp(prefix="ip_t_")) # ── census_report ──────────────────────────────────────────────────────────── def c_cr_full(rec): got = oracle_mod.census_report() rec("cr-full-counts-and-exclusions", {}, got, got) # ── driver ─────────────────────────────────────────────────────────────────── TRACE = [c_tsp_defaults, c_tsp_full, c_tsp_zero_duration, c_tsp_float_precision, c_tsp_journal_failure, c_tsp_empty_module] REPORT = [c_cr_full] GROUPS = [("trace_side_path", TRACE), ("census_report", REPORT)] def canon(x) -> str: return json.dumps(json.loads(json.dumps(x, default=str)), sort_keys=True) def main(): frozen = None if REPLAY: with open(TRUTH_PATH) as f: frozen = json.load(f)["cases"] groups = {} for group, cases in GROUPS: recs = [] def rec(name, inp, oracle_ret, expected, _r=recs): entry = {"name": name, "input": inp, "return": oracle_ret, "expected": expected} if REPLAY: old = next(c for c in frozen[group] if c["name"] == name) for key_ in ("input", "return", "expected"): got, want = entry[key_], old[key_] if canon(got) != canon(want): print(f"FACADE DIVERGE [{group}] {name} {key_}") print(" got: ", json.dumps(got, sort_keys=True, default=str)[:600]) print(" want:", json.dumps(want, sort_keys=True, default=str)[:600]) recs.append({"name": name, "diverged": key_}) return _r.append(entry) for c in cases: c(rec) groups[group] = recs total = sum(len(v) for v in groups.values()) if REPLAY: failures = sum(1 for v in groups.values() for c in v if "diverged" in c) print(f"replay: facade {total - failures}/{total} " f"(kernel/Go parity: `go test ./...` + `replay` CLI, " f"battery embeds truth.json verbatim)") return 1 if failures else 0 doc = { "frozen": datetime.datetime.now(datetime.timezone.utc).isoformat(), "oracle": "src/rune/inference_paths.py", "pin": "time.gmtime frozen at " + FROZEN_AT + " + RUNE_IT_CORE=python", "note": "Frozen via scripts/gen_inference_paths_truth.py against " "the live Python oracle. Ledger verdict: GO (the journal " "write is I/O-bound file IO; the census is simple " "counting). The journal attempt (rune.inference_trace + " "the journaled flag) stays in the Python facade — the Go " "kernel owns the record shape (schema_version, kind, " "module, function, model, task_id, duration_ms, at; `at` " "is a kernel argument — no-clock law) and census_report. " "Battery embeds this VERBATIM; float durations compare " "semantically (Python renders 0.0, Go marshals 0 — same " "value).", "cases": groups, } os.makedirs(os.path.dirname(TRUTH_PATH), exist_ok=True) with open(TRUTH_PATH, "w") as f: json.dump(doc, f, indent=1) f.write("\n") print(f"frozen {total} cases → {os.path.relpath(TRUTH_PATH, REPO)}") return 0 if __name__ == "__main__": sys.exit(main())