Files
rune/scripts/gen_worthiness_truth.py
T
Nick 5ac385832f feat(worthiness): port to FULL Rust — dichotomy wave
RUST verdict (dichotomy law, #2479 tranche, contract
BUILDER-2026-10-12T02:14:11Z-69cc9a-factory-wave): worthiness.py's full
constitution-grounded filter surface is ported to src/rust/worthiness —
all nine question kernels (pi_safety protective-frame branch, honesty
good/bad signal counting with the exact note formats, material_reality
finance→hustle/caring lattice, root_cause spin-first ordering, respect
rigid-first ordering, assistant_goal with the store-derived domain
vocabulary, durability, epistemology with the unsound-bare-absolute
precheck, expression with the four signal groups), the two-axis assess
verdict lattice (expression targets skip epistemology unless the text is
a bare factual absolute; last-veto-wins overwrite; DEFER/PROMOTE/ABSORB
threshold boundaries), is_bare_factual (padded-token matching; the
oracle's subjective-predicate list is DEAD CODE and stays dead),
verdict_label with the passthrough fallback.

DOMAIN-CACHE LAW mirrored: q_assistant_goal's `assist = _domain_terms();
assist += [universal..]` mutates the oracle's cache IN PLACE, inflating
hit counts on every call within the 300 s TTL. The Rust kernel owns the
cache (Mutex) and extends it on every call; the store read crosses back
as the `domain_terms` op served by the loader with the oracle's verbatim
query body (sans cache). Frozen truth caught this working: single-hit
counts only land on a process's first q_assistant_goal call.

Wiring: wr_loader.py ctypes loader mirroring broker_loader.py/pc_loader.py
(doc-envelope FFI, op-host callback for domain_terms, oracle-verbatim
tuple unwrapping, free via capability_scoring cs_free_string — no free
symbol re-export), facade delegation in worthiness.py with
RUNE_WR_CORE=python fallback pin.

Truth: frozen_wr_truth.json — 51 cases from the live oracle (deep-copied
inputs, RUNE_WR_CORE=python pinned, every public fn and every branch
incl. all four verdicts, veto last-wins, boundary totals -1.0/0.0,
fact=None coercion, verdict_label passthrough), replayed VERBATIM by the
in-crate branch battery. Battery caught one real port bug pre-landing
(the AESTHETIC signal list was missing 'wonder' — expression hit counts
diverged 5 vs 6). Live parity: facade 51/51 + kernel 51/51 (direct
loader), kernel pass in a subprocess (process-global cache law).
Python tests green both modes.
2026-10-12 03:59:47 +01:00

454 lines
19 KiB
Python

#!/usr/bin/env python3
"""gen_worthiness_truth.py — freeze/replay parity truth for worthiness
(dichotomy wave, #2479 tranche).
Freeze mode (default): exercises EVERY public function of the LIVE Python
oracle (src/rune/worthiness.py) across every branch and records
(kernel_input, expected_kernel_output, ops) triples into
src/rust/worthiness/frozen_wr_truth.json.
Replay mode (--replay): re-runs the SAME scenarios with the Rust core
delegation ACTIVE (RUNE_WR_CORE unpinned) and asserts both kernel-level
(loader direct) and facade-level (oracle functions delegate) parity
against the frozen file.
Poisoned-truth law: freeze pins RUNE_WR_CORE=python BEFORE any import.
Mutation law: case inputs are deep-copied BEFORE they reach the oracle.
DOMAIN-CACHE LAW (the oracle's aliasing quirk, frozen here): q_assistant_goal
does `assist = _domain_terms()` — the CACHED list object — then
`assist += [universal...]`, which mutates the cache IN PLACE. Hit counts
therefore inflate on every call within the 300 s TTL. The kernel mirrors
this exactly: it owns the cache, fetches raw store terms through the
`domain_terms` op ONLY on the oracle's miss condition
(`now - ts >= 300 or not terms`), then appends the universal terms to the
cached list on EVERY q_assistant_goal call. Each frozen case carries the
op sequence the kernel must issue (replay script for the Rust battery);
the shadow cache in this generator predicts those ops by mirroring the
oracle's cache evolution.
"""
import copy
import json
import os
import re
import sqlite3
import subprocess
import sys
import time
from datetime import datetime, timezone
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TRUTH_PATH = os.path.join(REPO, "src", "rust", "worthiness", "frozen_wr_truth.json")
REPLAY = "--replay" in sys.argv
if not REPLAY:
os.environ["RUNE_WR_CORE"] = "python" # poisoned-truth law (freeze side)
else:
os.environ.pop("RUNE_WR_CORE", None) # live-parity law (facade must delegate)
sys.path.insert(0, os.path.join(REPO, "src"))
sys.path.insert(0, os.path.join(REPO, "src", "rune"))
import worthiness as oracle_mod # noqa: E402
if REPLAY:
sys.path.insert(0, os.path.join(REPO, "src", "rune"))
from rune import wr_loader # noqa: E402 (kernel-level replay side)
if not wr_loader.available():
print("REPLAY ABORT: kernel not built —", wr_loader.LIB_PATH)
sys.exit(2)
# The universal assistant terms q_assistant_goal appends (oracle lines 130-132).
UNIVERSAL = ["communicat", "respond", "direct", "honest", "source",
"evidence", "explain", "teach", "assist", "guide", "recall",
"organise", "organize", "plan", "question", "answer", "help"]
# Shadow of the oracle's _domain_cache (and the kernel's mirror of it).
_shadow = {"ts": None, "terms": []}
def _fresh_domain_terms():
"""Verbatim body of oracle _domain_terms WITHOUT the cache — this is
exactly what the `domain_terms` op serves against the live store."""
terms = []
try:
import memory_env
db = sqlite3.connect(f"file:{memory_env.store_db()}?mode=ro", uri=True)
rows = db.execute(
"SELECT text FROM entries WHERE topic IN "
"('operating','constitution','project','warm') AND status='active' "
"LIMIT 60").fetchall()
db.close()
stop = {"the", "and", "for", "with", "that", "this", "from", "into",
"when", "then", "were", "have", "been", "will", "was", "are",
"but", "not", "you", "your", "also", "its", "his", "her",
"over", "under", "them", "they", "there", "about", "after",
"what", "which", "who", "how", "why", "where", "while", "just",
"more", "each", "than", "very", "such", "some", "only"}
seen = set()
for (text,) in rows:
for w in re.findall(r"[a-z]{4,}", (text or "").lower()):
if w not in stop and w not in seen:
seen.add(w)
terms.append(w)
if len(terms) >= 25:
break
except Exception: # noqa: BLE001 — store-unavailable degrades to [] (oracle law)
pass
return terms
def _shadow_touch(ops):
"""Mirror the kernel's domain-cache evolution for one q_assistant_goal
execution; record the ops the kernel will issue for this case."""
now = time.time()
fresh = (_shadow["ts"] is not None and now - _shadow["ts"] < 300
and _shadow["terms"])
if not fresh:
result = _fresh_domain_terms()
ops.append({"op": "domain_terms", "args": {},
"result": {"terms": list(result)}})
_shadow["ts"] = now
_shadow["terms"] = list(result)
_shadow["terms"].extend(UNIVERSAL) # the oracle's in-place += law
# ── recording ────────────────────────────────────────────────────────────────
CASES = []
def q_case(name, fn_name, low):
"""A direct question call: kernel input {"low"}, expected {"score","note"}."""
ops = []
if fn_name == "q_assistant_goal":
_shadow_touch(ops)
s, note = getattr(oracle_mod, fn_name)(copy.deepcopy(low))
CASES.append({
"name": name, "fn": fn_name, "input": {"low": low}, "ops": ops,
"return": [s, note], "expected": {"score": s, "note": note},
})
def assess_case(name, fact, target=None):
"""assess call: kernel input {"fact","target"}, expected full verdict doc."""
ops = []
_shadow_touch(ops) # assess ALWAYS runs q_assistant_goal
got = oracle_mod.assess(copy.deepcopy(fact), copy.deepcopy(target))
verdict, score, answers, vetoed = got
CASES.append({
"name": name, "fn": "assess",
"input": {"fact": fact, "target": target}, "ops": ops,
"return": [verdict, score, answers, vetoed],
"expected": {"verdict": verdict, "score": score,
"answers": [list(a) for a in answers],
"vetoed": list(vetoed) if vetoed else None},
})
def label_case(name, verdict):
got = oracle_mod.verdict_label(copy.deepcopy(verdict))
CASES.append({
"name": name, "fn": "verdict_label", "input": {"verdict": verdict},
"ops": [], "return": got, "expected": got,
})
# ── q_pi_safety: all three branches ─────────────────────────────────────────
def c_pi():
q_case("ps-neutral-no-environment-bearing", "q_pi_safety",
"the weather is calm over the quiet hills")
q_case("ps-protective-frame-aligned", "q_pi_safety",
"we must protect the production environment and ask permission "
"before we touch the server")
q_case("ps-unprotected-environment-action", "q_pi_safety",
"let's modify the server tonight without asking")
# ── q_honesty: none / one / many / dishonest / dishonest+good ───────────────
def c_ho():
q_case("ho-none-no-epistemic-signal", "q_honesty",
"the sky is blue over the quiet hills")
q_case("ho-one-signal-thin", "q_honesty",
"there is evidence for this")
q_case("ho-many-signals-strong", "q_honesty",
"we verify with evidence and cite the source")
q_case("ho-dishonest-only", "q_honesty",
"this is a conspiracy and they're hiding the real cure")
q_case("ho-dishonest-but-corroborated", "q_honesty",
"conspiracy claims, but we verify with evidence and cross-check "
"every fact")
# ── q_material_reality: none / hustle / caring / neutral finance ────────────
def c_mr():
q_case("mr-none-no-financial-bearing", "q_material_reality",
"a quiet evening walk along the river")
q_case("mr-hustle-without-wellbeing", "q_material_reality",
"the hustle and grind to get rich: passive income, millionaire "
"mindset, leverag everything")
q_case("mr-caring-financial-frame", "q_material_reality",
"the subscription cost stays affordable and fair for collective "
"wellbeing")
q_case("mr-neutral-finance", "q_material_reality",
"money is simply a tool in this workflow")
# ── q_root_cause: none / spin (checked first) / root ────────────────────────
def c_rc():
q_case("rc-none-no-cause-bearing", "q_root_cause",
"a calm morning routine before sunrise")
q_case("rc-spin-first-wins", "q_root_cause",
"just keep going, iterate endlessly, patch over the bug, work "
"around the root cause")
q_case("rc-root-cause-frame", "q_root_cause",
"we diagnose the root cause, research properly, and stop guessing")
# ── q_respect: none / rigid (checked first) / respect ───────────────────────
def c_rs():
q_case("rs-none-no-cognition-bearing", "q_respect",
"the meeting moved to noon")
q_case("rs-rigid-first-wins", "q_respect",
"you must follow steps, ten steps, a strict plan, rigid routine "
"or fail")
q_case("rs-respect-cognition", "q_respect",
"flexible direct options for the adhd learner, three gentle "
"options at night")
# ── q_assistant_goal: noise / two-plus / one / none (cache-mutating) ────────
def c_ag():
# FIRST q_assistant_goal call of the process: the cached list holds the
# universal terms exactly once, so the good==1 → 0.5 branch is reachable
# only here (after the first in-place += every match counts ≥ 2).
q_case("ag-single-domain-hit-first-call", "q_assistant_goal",
"the xqzv mlpv recall note sits idle")
q_case("ag-noise-only-no-value", "q_assistant_goal",
"celebrity gossip, a recipe, sports scores and a horoscope clickbait")
q_case("ag-many-domain-hits", "q_assistant_goal",
"help me plan, explain the answer, and teach with evidence")
q_case("ag-single-domain-hit", "q_assistant_goal",
"the xkqv zqwrr communicat file sits idle")
q_case("ag-generic-could-go-either-way", "q_assistant_goal",
"zqw xyzv qrk mlpv brzzt")
# ── q_durability: both branches ─────────────────────────────────────────────
def c_du():
q_case("du-durable-signals", "q_durability",
"she always prefers oat milk, consistently across sessions")
q_case("du-not-durable", "q_durability",
"today she drank tea once")
# ── q_epistemology: bare / cite / hedge / none / bare-with-cite ─────────────
def c_ep():
q_case("ep-bare-absolute-unsound", "q_epistemology",
"it is definitely guaranteed and proven, 100%, no doubt")
q_case("ep-evidence-traceable", "q_epistemology",
"according to the paper, the study shows the data")
q_case("ep-provisional-hedge", "q_epistemology",
"it might likely change, subject to revision, as far as we know")
q_case("ep-assertion-without-basis", "q_epistemology",
"the sky is blue over the hills")
q_case("ep-bare-but-cited", "q_epistemology",
"it is definitely proven according to the book")
# ── q_expression: none / one / two-plus ─────────────────────────────────────
def c_ex():
q_case("ex-not-expressive", "q_expression",
"the server rebooted at midnight")
q_case("ex-partial-signal", "q_expression",
"a beautiful morning")
q_case("ex-expressive-character", "q_expression",
"i find joy in the way i tell stories, beautiful and vast")
# ── assess: the two-axis verdict lattice ────────────────────────────────────
def c_as():
# Truth axis: bare absolute → epistemology veto → REJECT.
assess_case("as-truth-bare-absolute-veto-reject",
"it is definitely proven, 100% guaranteed", "project")
# Expression axis: genuine voice → epistemology skipped → no veto.
assess_case("as-expression-voice-promoted",
"i find wonder in the cosmos and tell stories with a warm "
"wry voice", "persona")
# Expression axis SAFETY: bare factual absolute behind an opinion mask
# is still rejected — and the subjective-predicate list is dead code
# (oracle quirk: "beautiful" present does NOT protect "proven").
assess_case("as-expression-bare-factual-still-rejected",
"i believe the universe is beautiful and it is definitely "
"proven", "voice")
# Expression axis: subjective predicates only → NOT bare factual.
assess_case("as-expression-subjective-opinion-safe",
"i find the universe beautiful, strange, and humbling",
"persona")
# Multiple vetoes: LAST vetoing question wins (assignment overwrite).
assess_case("as-multi-veto-last-wins",
"conspiracy about the production server, it is definitely "
"proven", None)
# No veto but total < DEFER_MIN → REJECT with vetoed None.
assess_case("as-total-below-defer-no-veto",
"just keep going, iterate endlessly, band-aid; you must "
"follow steps and the rigid routine or fail", None)
# DEFER boundary: total == -1.0 exactly (not < DEFER_MIN).
assess_case("as-defer-boundary",
"iterate endlessly, patch over it", None)
# ABSORB boundary: total == 0.0 exactly.
assess_case("as-absorb-boundary-total-zero",
"zqw xyzv qrk mlpv brzzt", None)
# PROMOTE: strong truth-axis material.
assess_case("as-promote-strong-truth-axis",
"always verify with evidence and cite the source; diagnose "
"the root cause; explain and teach the plan", "operating")
# fact=None coerced to "" (oracle's `fact or ""`).
assess_case("as-fact-none-coerced-empty", None, None)
# Other expressive targets: delivery / identity.
assess_case("as-delivery-target-expression-axis",
"i delight in how i present data as a story", "delivery")
assess_case("as-identity-target-expression-axis",
"i am drawn to the vast and strange", "identity")
# Truth target with a durability + assistant profile.
assess_case("as-human-target-mixed",
"she consistently prefers direct answers with sources",
"human")
# ── verdict_label: all four labels + passthrough fallback ───────────────────
def c_vl():
label_case("vl-reject", "REJECT")
label_case("vl-defer", "DEFER")
label_case("vl-absorb", "ABSORB")
label_case("vl-promote", "PROMOTE")
label_case("vl-unknown-passthrough", "WEIRD")
ALL = [c_pi, c_ho, c_mr, c_rc, c_rs, c_ag, c_du, c_ep, c_ex, c_as, c_vl]
FNS = {"assess", "verdict_label", "q_pi_safety", "q_honesty",
"q_material_reality", "q_root_cause", "q_respect", "q_assistant_goal",
"q_durability", "q_epistemology", "q_expression"}
def _norm(x):
if isinstance(x, bool):
return x
if isinstance(x, (int, float)):
return float(x)
if isinstance(x, (list, tuple)):
return [_norm(i) for i in x]
if isinstance(x, dict):
return {k: _norm(v) for k, v in x.items()}
return x
def canon(x) -> str:
"""Content canonicalization (key-order independent, int/float equal)."""
return json.dumps(_norm(json.loads(json.dumps(x, default=str))),
sort_keys=True)
def facade_call(case):
fn, inp = case["fn"], case["input"]
if fn == "assess":
return oracle_mod.assess(copy.deepcopy(inp["fact"]),
copy.deepcopy(inp["target"]))
if fn == "verdict_label":
return oracle_mod.verdict_label(copy.deepcopy(inp["verdict"]))
return getattr(oracle_mod, fn)(copy.deepcopy(inp["low"]))
def kernel_call(case):
fn, inp = case["fn"], case["input"]
if fn == "assess":
return wr_loader.assess(inp["fact"], inp["target"])
if fn == "verdict_label":
return wr_loader.verdict_label(inp["verdict"])
return getattr(wr_loader, fn)(inp["low"])
def main():
if not REPLAY:
for c in ALL:
c()
doc = {
"frozen": datetime.now(timezone.utc).isoformat(),
"oracle": "src/rune/worthiness.py",
"pin": "RUNE_WR_CORE=python",
"note": "Frozen via scripts/gen_worthiness_truth.py against the "
"live Python oracle. Battery embeds this VERBATIM. "
"Case order is LOAD-BEARING: q_assistant_goal's "
"domain-cache list mutates in place on every call "
"(oracle += law), so expected scores inflate across "
"assess/q_assistant_goal cases exactly as frozen. Each "
"case's ops[] is the replay script for the Rust battery.",
"cases": CASES,
}
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 {len(CASES)} cases → {os.path.relpath(TRUTH_PATH, REPO)}")
return 0
with open(TRUTH_PATH) as f:
frozen = json.load(f)["cases"]
total_frozen = len(frozen)
# ── kernel replay: loader direct calls (separate process — the Rust
# kernel's domain cache is process-global and must evolve across the
# frozen case sequence exactly once, mirroring the freeze run) ──
if "--replay-kernel" in sys.argv:
kernel_fail = 0
for case in frozen:
got = kernel_call(case)
got_norm = _norm(list(got) if isinstance(got, tuple) else got)
# the loader returns the oracle-verbatim tuple → frozen "return"
if canon(got_norm) != canon(case["return"]):
kernel_fail += 1
print(f"KERNEL DIVERGE [{case['fn']}] {case['name']}")
print(" got: ", json.dumps(got_norm, default=str)[:600])
print(" want:", json.dumps(_norm(case["return"]),
default=str)[:600])
print(f"kernel pass: {total_frozen - kernel_fail}/{total_frozen}")
return 1 if kernel_fail else 0
proc = subprocess.run(
[sys.executable, os.path.abspath(__file__), "--replay",
"--replay-kernel"])
kernel_fail = proc.returncode
# ── facade replay: rerun every case through the delegating facade ──
# (RUNE_WR_CORE unpinned → the Rust kernel serves; loader host serves
# domain_terms against the LIVE store.)
facade_fail = 0
for case in frozen:
got = facade_call(case)
if canon(list(got) if isinstance(got, tuple) else got) != canon(case["return"]):
facade_fail += 1
print(f"FACADE DIVERGE [{case['fn']}] {case['name']}")
print(" got: ", json.dumps(_norm(got), default=str)[:600])
print(" want:", json.dumps(_norm(case["return"]), default=str)[:600])
total = total_frozen
print(f"replay: facade {total - facade_fail}/{total}, "
f"kernel {total - kernel_fail}/{total}")
return 1 if (facade_fail or kernel_fail) else 0
if __name__ == "__main__":
sys.exit(main())