Files
rune/scripts/gen_build_basis_truth.py
T

324 lines
16 KiB
Python

#!/usr/bin/env python3
"""Freeze build_basis oracle truth for the Rust port battery (dichotomy
wave #2479 tranche, batch: build_basis — RUST side verdict).
Exercises EVERY public function across EVERY branch:
research_depth (all route defaults, unknown-route floor, tier-selecting
vs annotating justifications, blank justification), new_basis,
set_dispositions (legal/illegal/missing keys), validate_basis (schema,
depth, WHY rule D, OUTWARD/FULL prior-art gate, INWARD evidence gate,
illegal disposition, item truncation), is_qualified,
attach_evidence_ref (issue_ref/adhoc/missing created_at), summary
(empty/qualified/None implications/counts), revise_depth (legal
revision, blocked downgrade, no-op, unknown depth, reason truncation),
to_json (Python json.dumps indent=1 sort_keys incl. non-ASCII escaping),
from_json, generate_research_plan (topic kinds, outward keywords,
depth heuristics FULL/OUTWARD/INWARD/NONE, blank filtering, caller
depth, truncations).
Output: src/rust/build_basis/src/frozen_bb_truth.json — replayed
VERBATIM by src/rust/build_basis/src/branch_battery.rs.
Timestamps the kernel generates (decided_at/created_at/revised_at/
plan_generated_at) are masked (removed on both sides) — they are wall
clock, not logic. Fixed created_at values are INJECTED into basis inputs
so kernel pass-through fields compare exactly.
"""
from __future__ import annotations
import json
import os
import sys
from pathlib import Path
os.environ["RUNE_BB_CORE"] = "python" # POISONED-TRUTH LAW: freeze the ORACLE, never the kernel
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from rune import build_basis as bb # noqa: E402
MASKED = ("decided_at", "created_at", "revised_at", "plan_generated_at")
def mask(doc):
"""Remove kernel-generated wall-clock fields wherever they appear."""
if isinstance(doc, dict):
return {k: mask(v) for k, v in doc.items() if k not in MASKED}
if isinstance(doc, list):
return [mask(v) for v in doc]
return doc
def fixed_basis(depth_route="RESEARCHED_CHANGE", question="q",
issue_ref="research/issue-1"):
b = bb.new_basis(question=question,
depth_decision=bb.research_depth(depth_route),
issue_ref=issue_ref)
b["created_at"] = "2026-10-11T00:00:00Z" # fixed: pass-through, exact-compare
return b
cases: dict[str, list] = {}
def run(fn: str, name: str, **input):
out = getattr(bb, fn)(**input)
cases.setdefault(fn, []).append(
{"name": name, "input": input, "expected": mask(out)})
def run_err(fn: str, name: str, **input):
try:
getattr(bb, fn)(**input)
except ValueError as e:
cases.setdefault(fn, []).append(
{"name": name, "input": input, "error": str(e)})
return
raise AssertionError(f"{fn}({name}) did not raise")
# ── research_depth: every branch ──────────────────────────────────────
for route in ("LOCAL_FIX", "STANDARD_CHANGE", "RESEARCHED_CHANGE"):
run("research_depth", f"route-{route}", route=route)
run("research_depth", "unknown-route-floor", route="MYSTERY_ROUTE")
run("research_depth", "annotation-justification", route="RESEARCHED_CHANGE",
justification="tiny config fix; full pipeline disproportionate")
run("research_depth", "tier-select-justification", route="STANDARD_CHANGE",
justification="OUTWARD: new provider integration, precedent needed")
run("research_depth", "tier-select-downgrade-NONE", route="RESEARCHED_CHANGE",
justification="NONE: nothing to research")
run("research_depth", "tier-bare-word", route="STANDARD_CHANGE",
justification=" OUTWARD ")
run("research_depth", "tier-multi-colon", route="STANDARD_CHANGE",
justification="FULL: architectural; check heuristics first")
run("research_depth", "blank-justification-default", route="LOCAL_FIX",
justification=" ")
run("research_depth", "blank-justification-unknown", route="WEIRD",
justification=" ")
run("research_depth", "unknown-route-with-justification", route="WEIRD",
justification="check the spike notes")
run("research_depth", "mode-recorded", route="RESEARCHED_CHANGE",
mode="rune-internal")
# ── new_basis ─────────────────────────────────────────────────────────
b = bb.new_basis(question="structural model for project scaffolds",
depth_decision=bb.research_depth("RESEARCHED_CHANGE"),
issue_ref="research/structural-model")
b["created_at"] = "2026-10-11T00:00:00Z"
cases.setdefault("new_basis", []).append(
{"name": "full", "input": {"question": b["question"],
"depth_decision": mask(b["depth"]),
"issue_ref": b["issue_ref"]},
"expected": mask(b)})
b2 = bb.new_basis(question="q", depth_decision=bb.research_depth("LOCAL_FIX"))
b2["created_at"] = "2026-10-11T00:00:00Z"
cases["new_basis"].append(
{"name": "no-issue-ref", "input": {"question": "q",
"depth_decision": mask(b2["depth"])},
"expected": mask(b2)})
# ── set_dispositions ──────────────────────────────────────────────────
for nm, disp in (
("legal", [{"item": "Backstage catalog entity model", "disposition": "ADAPT",
"why": "nesting fits", "ref": "backstage.io"},
{"item": "own graph database", "disposition": "REJECT",
"why": "structural_store is adequate", "ref": "repo"}]),
("illegal", [{"item": "x", "disposition": "MAYBE"}]),
("missing-keys", [{}]),
("empty-list", []),
):
basis = fixed_basis()
before = json.loads(json.dumps(basis))
errs = bb.set_dispositions(basis, disp)
cases.setdefault("set_dispositions", []).append(
{"name": nm, "input": {"basis": before, "dispositions": disp},
"expected": {"basis": mask(basis), "errors": errs}})
# ── validate_basis: every error branch ────────────────────────────────
def val_case(nm, basis):
cases.setdefault("validate_basis", []).append(
{"name": nm, "input": {"basis": basis},
"expected": bb.validate_basis(basis)})
b = fixed_basis(); b["schema_version"] = 2
val_case("schema-mismatch", b)
b = fixed_basis(); b["schema_version"] = None
val_case("schema-none", b)
b = fixed_basis(); b["depth"] = {}
val_case("depth-missing", b)
b = fixed_basis(); b["depth"] = {"depth": "WEIRD", "why": "w"}
val_case("depth-illegal", b)
b = fixed_basis(); b["depth"] = {"depth": "FULL"}
val_case("why-missing-rule-D", b)
val_case("outward-no-evidence", fixed_basis(depth_route="STANDARD_CHANGE",
issue_ref="",
question="q") | {"depth": {"depth": "OUTWARD", "why": "w"}})
b = fixed_basis(); b["depth"] = {"depth": "FULL", "why": "w"}
val_case("full-no-evidence", b)
val_case("inward-empty", bb.new_basis(question="q",
depth_decision={"depth": "INWARD", "why": "w"}))
b = bb.new_basis(question="q", depth_decision={"depth": "INWARD", "why": "w"})
b["inward"]["findings"] = ["f"]
val_case("inward-with-findings", b)
b = fixed_basis()
b["dispositions"] = [{"item": "x" * 60, "disposition": "MAYBE"}]
val_case("illegal-disposition-item-truncated-40", b)
b = fixed_basis(); b.pop("dispositions")
val_case("dispositions-key-absent", b)
b = fixed_basis(); b["outward"] = None
val_case("outward-null-inward-ok", b)
val_case("qualified-basis-zero-errors", fixed_basis() | {
"outward": {"prior_art_questions_answered": ["q1"], "findings": [], "refs": []},
"dispositions": [{"item": "i", "disposition": "ADOPT", "why": "w", "ref": "r"}]})
# ── is_qualified ──────────────────────────────────────────────────────
q = fixed_basis()
q["outward"] = {"prior_art_questions_answered": ["q1"], "findings": [], "refs": []}
q["dispositions"] = [{"item": "i", "disposition": "ADOPT", "why": "w", "ref": "r"}]
cases.setdefault("is_qualified", []).append(
{"name": "qualified", "input": {"basis": q}, "expected": bb.is_qualified(q)})
cases["is_qualified"].append(
{"name": "empty-unqualified", "input": {"basis": fixed_basis()},
"expected": False})
# ── attach_evidence_ref ───────────────────────────────────────────────
cases.setdefault("attach_evidence_ref", []).append(
{"name": "with-issue-ref", "input": {"basis": fixed_basis()},
"expected": bb.attach_evidence_ref(fixed_basis())})
b = fixed_basis(); b["issue_ref"] = ""
cases["attach_evidence_ref"].append(
{"name": "adhoc", "input": {"basis": b}, "expected": bb.attach_evidence_ref(b)})
b = fixed_basis(); b.pop("created_at")
cases["attach_evidence_ref"].append(
{"name": "missing-created-at", "input": {"basis": b},
"expected": bb.attach_evidence_ref(b)})
# ── summary ───────────────────────────────────────────────────────────
b = fixed_basis()
cases.setdefault("summary", []).append(
{"name": "empty", "input": {"basis": json.loads(json.dumps(b))},
"expected": mask(bb.summary(b))})
q2 = fixed_basis()
q2["outward"] = {"prior_art_questions_answered": [], "findings": ["f"], "refs": ["r"]}
q2["dispositions"] = [{"item": "x" * 100, "disposition": "ADOPT", "why": "w", "ref": "r"},
{"item": "short", "disposition": "ADAPT"}]
q2["uncertainty"] = ["u1", "u2"]
q2["risks"] = ["r1"]
q2["implications"] = {"architecture": ["a1", "a2"], "migration": [], "verification": ["v"]}
q2s = json.loads(json.dumps(q2))
cases["summary"].append(
{"name": "qualified-truncation-counts",
"input": {"basis": q2s}, "expected": mask(bb.summary(q2))})
b = fixed_basis(); b["implications"] = None
cases["summary"].append(
{"name": "implications-null", "input": {"basis": b},
"expected": mask(bb.summary(b))})
# ── revise_depth ──────────────────────────────────────────────────────
def rev_case(nm, basis, new_depth, reason, error_expected=False):
before = json.loads(json.dumps(basis))
if error_expected:
try:
bb.revise_depth(basis, new_depth=new_depth, reason=reason)
except ValueError as e:
cases.setdefault("revise_depth", []).append(
{"name": nm, "input": {"basis": before, "new_depth": new_depth,
"reason": reason}, "error": str(e)})
return
raise AssertionError(f"revise_depth {nm} did not raise")
rev = bb.revise_depth(basis, new_depth=new_depth, reason=reason)
cases.setdefault("revise_depth", []).append(
{"name": nm, "input": {"basis": before, "new_depth": new_depth,
"reason": reason},
"expected": {"basis": mask(basis), "revision": mask(rev)}})
rev_case("full-to-inward-justified", fixed_basis(), "INWARD",
"prior-art question resolved as non-material")
rev_case("full-to-inward-blocked", fixed_basis(), "INWARD",
"skip it, not relevant", error_expected=True)
rev_case("no-op", fixed_basis("INWARD"), "INWARD", "same", error_expected=True)
rev_case("unknown-depth", fixed_basis(), "WEIRD", "why not", error_expected=True)
rev_case("outward-to-none-unnecessary", fixed_basis(), "NONE",
"prior-art question unnecessary: internal solution exists")
rev_case("upgrade-full-to-outward", fixed_basis(), "OUTWARD", "scope narrowed")
rev_case("none-to-inward", fixed_basis("LOCAL_FIX"), "INWARD", "repo code matters")
rev_case("reason-truncated-200", fixed_basis(), "OUTWARD", "r" * 300)
rev_case("inward-to-none-keyword", fixed_basis("INWARD"), "NONE",
"discovery showed nothing to record")
# ── to_json (Python json.dumps indent=1 sort_keys semantics) ──────────
cases.setdefault("to_json", []).append(
{"name": "basis", "input": {"basis": fixed_basis()},
"expected": bb.to_json(fixed_basis())})
uni = fixed_basis()
uni["question"] = "héllo 日本語 🎉 quoted \"x\" back\\slash \n\t ctrl\x01"
cases["to_json"].append(
{"name": "unicode-escapes", "input": {"basis": uni},
"expected": bb.to_json(uni)})
cases["to_json"].append(
{"name": "empty-dict", "input": {"basis": {}}, "expected": bb.to_json({})})
mixed = {"b": 1, "a": [1, 2.5, True, None, "x"], "c": {"z": [], "y": {}},
"é": "café", "Z": -17, "nested": {"deep": {"deeper": [{}]},
"flag": False}}
cases["to_json"].append(
{"name": "mixed-sorted", "input": {"basis": mixed},
"expected": bb.to_json(mixed)})
# ── from_json ─────────────────────────────────────────────────────────
cases.setdefault("from_json", []).append(
{"name": "object", "input": {"text": bb.to_json(fixed_basis())},
"expected": mask(json.loads(bb.to_json(fixed_basis())))})
cases["from_json"].append(
{"name": "array", "input": {"text": "[1, \"two\", null, {\"k\": true}]"},
"expected": [1, "two", None, {"k": True}]})
try:
bb.from_json("{bad json")
raise AssertionError("from_json did not raise")
except ValueError as e:
cases["from_json"].append(
{"name": "invalid", "input": {"text": "{bad json"},
"error_kind": "ValueError"})
# ── generate_research_plan: every branch ──────────────────────────────
for nm, kw in (
("no-args-none", dict(intent="fix a typo in the README")),
("unknowns-inward", dict(intent="i", unknowns=["which code path renders tables?"])),
("outward-prior-art", dict(intent="i",
unknowns=["is there an existing Python PDF library?"])),
("full-consequential", dict(intent="migrate the auth schema",
unknowns=["which user tables hold credential hashes?"],
risks=["breaking change to login flow"])),
("consequential-but-no-topics", dict(intent="deploy and delete the old cluster")),
("blank-filtered", dict(intent="i", unknowns=["", " ", "real unknown u"],
decisions=[" ", "d1"], risks=["", "r1"])),
("decisions-and-risks-kinds", dict(intent="i", unknowns=["u1"],
decisions=["d1"], risks=["r1"])),
("truncation-80", dict(intent="i", unknowns=["u" * 120])),
("depth-supplied", dict(intent="i", unknowns=["big unknown"], depth="INWARD")),
("multiword-prior-art", dict(intent="i",
unknowns=["prior art on vector stores?"])),
("target-and-reality", dict(intent="i", target="wr-bridge",
current_reality="c" * 300,
unknowns=["u"], depth="NONE")),
("tool-framework-keywords", dict(intent="i",
unknowns=["which framework/tool convention applies?"])),
):
plan = bb.generate_research_plan(**kw)
cases.setdefault("generate_research_plan", []).append(
{"name": nm, "input": kw, "expected": mask(plan)})
truth = {
"frozen": "build_basis full oracle truth (dichotomy wave #2479)",
"oracle": "src/rune/build_basis.py",
"note": ("Timestamp fields the kernel GENERATES (decided_at, created_at, "
"revised_at, plan_generated_at) are masked on both sides — wall "
"clock, not logic. created_at values inside basis INPUTS are "
"fixed pass-through and compare exactly. error = the exact "
"Python ValueError message."),
"cases": cases,
}
out = Path(__file__).resolve().parents[1] / "src" / "rust" / "build_basis" / "src" / "frozen_bb_truth.json"
out.write_text(json.dumps(truth, indent=1, sort_keys=True) + "\n")
total = sum(len(v) for v in cases.values())
print(f"frozen {total} cases across {len(cases)} functions -> {out}")