feat(inference_paths): port to Go — dichotomy wave

LEDGER VERDICT: GO (docs/language-dichotomy-ledger.md) — the journal
write is I/O-bound file IO (decision tree: I/O-bound → Go) and the
census is simple counting over a static table (Laws 3/7: simple
operations stay in the caller's language; FFI overhead would exceed
the compute benefit — the census_report fold is ~16 increments).

parity: every public function of inference_paths ported — the CENSUS
table (all 16 call sites, insertion order preserved, notes verbatim),
trace_side_path's record shape (schema_version 1, kind side_path,
module/function/model/task_id/duration_ms, at) with the no-clock law
(`at` is a kernel argument — the oracle renders it via
time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()); the freeze pins
time.gmtime), and census_report (total / by_kind fold / the EXCLUDED
list with documented reasons). Boundary law: the JOURNAL attempt
(rune.inference_trace import + record_trace + the journaled flag)
stays in the Python facade — the Go kernel owns the record shape and
the report. ORACLE DIVERGENCE NOTE (observable behavior pinned, not
fixed): the oracle's journal path is currently dead — InferenceTrace's
signature drifted (requires resolved_context_window/input_budget/
model_calls; the oracle passes prompt_tokens/output_tokens/cost), so
trace_side_path ALWAYS returns journaled=False via the except branch.
The frozen truth records that faithfully; the facade keeps its own
float duration_ms (Python renders 0.0 where Go marshals 0 — the
battery compares semantically). Journal attempts in the battery write
to throwaway temp dirs, never the real journal. gen_inference_paths_
truth.py freezes 7 cases (record defaults/full/zero/float/empty +
journal-failure branch + full census report); replay mode re-verifies
the live oracle 7/7. Go battery: go test replays truth.json VERBATIM +
the `inference-paths replay` CLI — 7/7; gofmt clean, go vet clean;
test_inference_paths.py 6/6 (no Python-side delegation pin — Go ports
are standalone per recall-uplift precedent).
This commit is contained in:
Nick committed 2026-10-13 07:01:40 +01:00
1 parent 093c2a7bba
commit 251fa36df1
5 files changed
+673

No files matched your search

+196
View File
@@ -0,0 +1,196 @@
#!/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())
+3
View File
@@ -0,0 +1,3 @@
module rune/inference-paths
go 1.21
+191
View File
@@ -0,0 +1,191 @@
// Package main — inference-paths: the GO port of src/rune/inference_paths.py
// (#145 inference-path census + trace wiring, #2479 dichotomy wave).
//
// Ledger verdict (docs/language-dichotomy-ledger.md): GO — the journal
// write is I/O-bound file IO (decision tree: I/O-bound → Go) and the
// census is simple counting (Law 3/7: simple operations stay in the
// caller's language; FFI overhead would exceed the compute benefit).
//
// Boundary law: the JOURNAL attempt (rune.inference_trace import +
// record_trace + the journaled flag) and the census-completeness
// obligations (the test's grep-vs-census check) stay in the Python
// facade — the Go kernel owns the record shape and census_report.
//
// No-clock law: `at` is a kernel argument — the kernel never reads the
// wall clock (the oracle renders it as
// time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())).
//
// Float note: Python renders duration_ms 0.0 as "0.0" while Go marshals
// float64 0 as "0" — the battery compares SEMANTICALLY (unmarshal both
// sides, reflect.DeepEqual), same value, same type shape.
//
// Usage: inference-paths replay <truth.json>
package main
import (
"encoding/json"
"fmt"
"os"
"reflect"
)
// CensusEntry — one outbound model-call site (module, function,
// endpoint, kind, note). The census is the #145 deliverable: every
// outbound model call in src/rune is ENUMERATED and each is either
// TRACED or DOCUMENTED-EXCLUDED with its reason.
type CensusEntry struct {
Module string
Function string
Endpoint string
Kind string
Note string
}
// census — the oracle's CENSUS table (insertion order preserved; the
// census is verified 2026-10-08 by grep for /api/generate + /api/embed
// + urlopen patterns; new call sites MUST be added here).
var census = []CensusEntry{
{"rune_operator", "_runtime_for_model", "/api/generate", "APPROVAL",
"approval-resume turns: fresh transport on the operator path — " +
"traced via trace_side_path since this batch"},
{"context_broker", "summarize_history", "/api/generate", "COMPACTION",
"mid-assembly compaction: own transport — recorded as a compaction " +
"trace folded into the run's served sums"},
{"abstraction", "_abstract", "/api/generate", "SIDE", "traced"},
{"goal", "_goal", "/api/generate", "SIDE", "traced"},
{"growth_delta", "_growth", "/api/generate", "SIDE", "traced"},
{"model_connect", "_connect", "/api/generate", "SIDE", "traced (x2)"},
{"night_mode", "_night", "/api/generate", "SIDE", "traced (x2)"},
{"provider_registry", "generate", "/api/generate", "SIDE", "traced"},
{"rune_tui", "keepalive_warmup", "/api/generate", "EXCLUDED",
"TUI keep-alive warmup: no task linkage, no user-visible output — " +
"excluded (warmup, not inference)"},
{"sleep_time", "reflect", "/api/generate", "SIDE", "traced"},
{"tiered_executor", "_execute", "/api/generate", "SIDE", "traced"},
{"witness", "_witness", "/api/generate", "SIDE", "traced"},
{"curator", "_embed", "/api/embed", "EMBED", "recorded as embedding trace"},
{"memory_store", "_embed", "/api/embed", "EMBED", "recorded as embedding trace"},
{"persona_gate", "_gate", "/api/embed", "EMBED", "recorded as embedding trace"},
{"elastic_cognition", "_elastic", "/api/generate", "SIDE", "traced"},
}
// TraceSidePath — the oracle's record dict for one side-path inference
// (oracle-exact field set and Python-dict key order):
//
// {"schema_version": 1, "kind": "side_path", "module", "function",
// "model", "task_id", "duration_ms", "at"}
//
// `at` is a kernel argument (no-clock law). The journal attempt and the
// journaled flag stay in the Python facade.
func TraceSidePath(module, function, model, taskID string, durationMS float64, at string) map[string]any {
return map[string]any{
"schema_version": 1,
"kind": "side_path",
"module": module,
"function": function,
"model": model,
"task_id": taskID,
"duration_ms": durationMS,
"at": at,
}
}
// CensusReport — the census as a report: counts by kind + the exclusion
// list (EXCLUDED entries carry their documented reason).
func CensusReport() map[string]any {
byKind := map[string]int{}
excluded := []map[string]string{}
for _, e := range census {
byKind[e.Kind]++
if e.Kind == "EXCLUDED" {
excluded = append(excluded, map[string]string{
"module": e.Module,
"function": e.Function,
"reason": e.Note,
})
}
}
return map[string]any{
"total": len(census),
"by_kind": byKind,
"excluded": excluded,
}
}
// replayTruth — battery driver: replay the frozen Python-oracle truth
// VERBATIM (semantic comparison — see the float note above).
func replayTruth(path string) (checked int, err error) {
raw, err := os.ReadFile(path)
if err != nil {
return 0, err
}
var doc struct {
Cases map[string][]struct {
Name string `json:"name"`
Input map[string]any `json:"input"`
Expected map[string]any `json:"expected"`
} `json:"cases"`
}
if err := json.Unmarshal(raw, &doc); err != nil {
return 0, err
}
for _, c := range doc.Cases["trace_side_path"] {
checked++
got := TraceSidePath(
c.Input["module"].(string),
c.Input["function"].(string),
c.Input["model"].(string),
c.Input["task_id"].(string),
c.Input["duration_ms"].(float64),
c.Input["at"].(string),
)
if fail := compare(c.Name, got, c.Expected["record"]); fail {
return checked, fmt.Errorf("trace_side_path case %q diverged", c.Name)
}
}
for _, c := range doc.Cases["census_report"] {
checked++
if fail := compare(c.Name, CensusReport(), c.Expected); fail {
return checked, fmt.Errorf("census_report case %q diverged", c.Name)
}
}
return checked, nil
}
// compare — semantic equality (unmarshal both sides through any).
func compare(name string, got, want any) bool {
g := norm(got)
w := norm(want)
if !reflect.DeepEqual(g, w) {
gj, _ := json.Marshal(g)
wj, _ := json.Marshal(w)
fmt.Printf("case %q diverged:\n got: %s\n want: %s\n", name, gj, wj)
return true
}
return false
}
func norm(v any) any {
b, err := json.Marshal(v)
if err != nil {
return v
}
var out any
if err := json.Unmarshal(b, &out); err != nil {
return v
}
return out
}
func main() {
if len(os.Args) < 2 || os.Args[1] != "replay" {
fmt.Fprintln(os.Stderr, "usage: inference-paths replay <truth.json>")
os.Exit(2)
}
checked, err := replayTruth(os.Args[2])
if err != nil {
fmt.Fprintf(os.Stderr, "PARITY FAILED: %v\n", err)
os.Exit(1)
}
fmt.Printf("PARITY OK: %d cases vs the Python oracle (record shape + census report)\n", checked)
}
+254
View File
@@ -0,0 +1,254 @@
{
"frozen": "2026-10-13T05:35:42.241015+00:00",
"oracle": "src/rune/inference_paths.py",
"pin": "time.gmtime frozen at 2026-10-12T12:34:56Z + 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 \u2014 the Go kernel owns the record shape (schema_version, kind, module, function, model, task_id, duration_ms, at; `at` is a kernel argument \u2014 no-clock law) and census_report. Battery embeds this VERBATIM; float durations compare semantically (Python renders 0.0, Go marshals 0 \u2014 same value).",
"cases": {
"trace_side_path": [
{
"name": "tsp-defaults-side-path",
"input": {
"module": "goal",
"function": "_goal",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z"
},
"return": {
"schema_version": 1,
"kind": "side_path",
"module": "goal",
"function": "_goal",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z",
"journaled": false
},
"expected": {
"record": {
"schema_version": 1,
"kind": "side_path",
"module": "goal",
"function": "_goal",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z"
}
}
},
{
"name": "tsp-full-fields",
"input": {
"module": "rune_operator",
"function": "_runtime_for_model",
"model": "llama3",
"task_id": "t1",
"duration_ms": 12.5,
"at": "2026-10-12T12:34:56Z"
},
"return": {
"schema_version": 1,
"kind": "side_path",
"module": "rune_operator",
"function": "_runtime_for_model",
"model": "llama3",
"task_id": "t1",
"duration_ms": 12.5,
"at": "2026-10-12T12:34:56Z",
"journaled": false
},
"expected": {
"record": {
"schema_version": 1,
"kind": "side_path",
"module": "rune_operator",
"function": "_runtime_for_model",
"model": "llama3",
"task_id": "t1",
"duration_ms": 12.5,
"at": "2026-10-12T12:34:56Z"
}
}
},
{
"name": "tsp-zero-duration",
"input": {
"module": "witness",
"function": "_witness",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z"
},
"return": {
"schema_version": 1,
"kind": "side_path",
"module": "witness",
"function": "_witness",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z",
"journaled": false
},
"expected": {
"record": {
"schema_version": 1,
"kind": "side_path",
"module": "witness",
"function": "_witness",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z"
}
}
},
{
"name": "tsp-float-precision",
"input": {
"module": "tiered_executor",
"function": "_execute",
"model": "qwen2.5",
"task_id": "task-99",
"duration_ms": 123.456,
"at": "2026-10-12T12:34:56Z"
},
"return": {
"schema_version": 1,
"kind": "side_path",
"module": "tiered_executor",
"function": "_execute",
"model": "qwen2.5",
"task_id": "task-99",
"duration_ms": 123.456,
"at": "2026-10-12T12:34:56Z",
"journaled": false
},
"expected": {
"record": {
"schema_version": 1,
"kind": "side_path",
"module": "tiered_executor",
"function": "_execute",
"model": "qwen2.5",
"task_id": "task-99",
"duration_ms": 123.456,
"at": "2026-10-12T12:34:56Z"
}
}
},
{
"name": "tsp-journal-failure-false",
"input": {
"module": "curator",
"function": "_embed",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z"
},
"return": {
"schema_version": 1,
"kind": "side_path",
"module": "curator",
"function": "_embed",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z",
"journaled": false
},
"expected": {
"record": {
"schema_version": 1,
"kind": "side_path",
"module": "curator",
"function": "_embed",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z"
}
}
},
{
"name": "tsp-empty-module-function",
"input": {
"module": "",
"function": "",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z"
},
"return": {
"schema_version": 1,
"kind": "side_path",
"module": "",
"function": "",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z",
"journaled": false
},
"expected": {
"record": {
"schema_version": 1,
"kind": "side_path",
"module": "",
"function": "",
"model": "",
"task_id": "",
"duration_ms": 0.0,
"at": "2026-10-12T12:34:56Z"
}
}
}
],
"census_report": [
{
"name": "cr-full-counts-and-exclusions",
"input": {},
"return": {
"total": 16,
"by_kind": {
"APPROVAL": 1,
"COMPACTION": 1,
"SIDE": 10,
"EXCLUDED": 1,
"EMBED": 3
},
"excluded": [
{
"module": "rune_tui",
"function": "keepalive_warmup",
"reason": "TUI keep-alive warmup: no task linkage, no user-visible output \u2014 excluded (warmup, not inference)"
}
]
},
"expected": {
"total": 16,
"by_kind": {
"APPROVAL": 1,
"COMPACTION": 1,
"SIDE": 10,
"EXCLUDED": 1,
"EMBED": 3
},
"excluded": [
{
"module": "rune_tui",
"function": "keepalive_warmup",
"reason": "TUI keep-alive warmup: no task linkage, no user-visible output \u2014 excluded (warmup, not inference)"
}
]
}
}
]
}
}
+29
View File
@@ -0,0 +1,29 @@
package main
// truth_test.go — parity battery: replays the frozen Python-oracle truth
// (truth.json, frozen via scripts/gen_inference_paths_truth.py with the
// wall clock frozen and RUNE_IT_CORE=python pinned) VERBATIM. Covers
// every public function of src/rune/inference_paths.py: trace_side_path
// record shape (defaults, full fields, zero/float durations, empty
// module) and census_report (total/by_kind/excluded). The journaled
// flag and the journal attempt stay in the Python facade (boundary
// law); note the oracle's journal path currently ALWAYS yields
// journaled=False (InferenceTrace signature drift) — the frozen truth
// pins that observable behavior.
import (
"os"
"testing"
)
func TestTraceSidePathBattery(t *testing.T) {
if _, err := replayTruth("truth.json"); err != nil {
t.Fatalf("trace_side_path battery: %v", err)
}
}
func TestTruthFilePresent(t *testing.T) {
if _, err := os.Stat("truth.json"); err != nil {
t.Fatalf("frozen truth missing: %v", err)
}
}