Files
rune/scripts/gen_skill_compiler_truth.py
T
Nick 922ecbb093 feat(skill_compiler): port to FULL Rust — dichotomy wave
Language verdict: RUST per docs/language-dichotomy-ledger.md (Law 4 —
deterministic hashing/assembly law; the replay CALLABLE is host-side by
nature and stays in the facade).

- sk_sequence_id/sk_candidate_skill/sk_default_step_check/sk_sandbox_
  gate/sk_promote doc-envelope FFI (sk_* symbols; free via
  capability_scoring cs_free_string — no free re-export); sk_loader
  mirroring broker_loader.py.
- Facade delegation with RUNE_SKC_CORE=python pin; kernel floor on any
  kernel error.
- Python laws replicated exactly: json.dumps(steps, sort_keys=True)
  BYTE law via pyjson::dumps_sorted + sha256[:12] (unicode, quotes,
  nested structures verified); 'evidence or {}' falsy chain; default
  shape check ('shape-ok'/'missing action'); passed=all with Python
  truthiness and VACUOUS-TRUE empty steps (missing steps key too);
  promote truthiness gate (missing/empty sandbox and passed=False all
  block).
- 33-case frozen oracle truth (RUNE_SKC_CORE=python pinned, deep-copied
  inputs, every public function + branch) embedded in the crate and
  replayed VERBATIM by the branch battery; live parity 1624/1624 (24
  frozen + 1600 fuzz, seed 2479); pytest 21 passed BOTH ways
  (RUNE_SKC_CORE=python and auto); clippy -D warnings clean.
- Documented edge (not in corpus): integral float step values round-
  trip as '3' through pyjson where Python would hash '3.0' — replay
  scripts carry dict/list/str/int by construction.
2026-10-13 05:50:34 +01:00

156 lines
6.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Freeze the skill_compiler oracle truth (dichotomy wave, #2479).
Generator law: RUNE_SKC_CORE=python pinned, every input deep-copied,
every public function exercised across ALL branches:
* sequence_id — unicode/quotes/nested steps (the json.dumps(sort_keys)
byte law + sha256[:12])
* candidate_skill — evidence None / {} / given (the `evidence or {}`
falsy law)
* sandbox_test — default shape check (ok step, missing action,
non-dict step), empty steps (vacuous pass), missing steps key,
replay_fn ok/fail tuples, replay_fn crash (failure verdict, not a
harness crash) — the HOST replay loop's results are frozen and the
gate replayed through the kernel
* promote — no sandbox key, sandbox passed False, sandbox passed True
"""
import copy
import json
import os
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO / "src"))
sys.path.insert(0, str(REPO / "src" / "rune"))
os.environ["RUNE_SKC_CORE"] = "python"
import skill_compiler as sk # noqa: E402
cases_sequence_id = []
cases_candidate = []
cases_sandbox = [] # full facade-level (skill + frozen replay results)
cases_gate = [] # kernel-level (skill + results -> sandboxed skill)
cases_promote = []
def case_seq(name, steps):
steps = copy.deepcopy(steps)
cases_sequence_id.append({
"name": name,
"args": {"steps": steps},
"expect": sk.sequence_id(copy.deepcopy(steps)),
})
def case_candidate(name, steps, family, evidence):
steps = copy.deepcopy(steps)
family = copy.deepcopy(family)
evidence = copy.deepcopy(evidence)
cases_candidate.append({
"name": name,
"args": {"steps": steps, "family": family, "evidence": evidence},
"expect": sk.candidate_skill(copy.deepcopy(steps),
copy.deepcopy(family),
copy.deepcopy(evidence)),
})
def case_sandbox(name, skill, results):
"""Full sandbox_test law with the replay results FROZEN (the replay
callable is host-side by nature and cannot cross FFI)."""
skill = copy.deepcopy(skill)
results = copy.deepcopy(results)
expect = copy.deepcopy(skill)
expect["sandbox"] = {"passed": all(r["ok"] for r in results),
"results": results}
cases_gate.append({"name": name, "args": {"skill": skill,
"results": results},
"expect": expect})
cases_sandbox.append({"name": name, "args": {"skill": skill,
"results": results},
"expect": expect})
def case_promote(name, skill):
skill = copy.deepcopy(skill)
cases_promote.append({
"name": name,
"args": {"skill": skill},
"expect": sk.promote(copy.deepcopy(skill)),
})
# ── sequence_id: byte law (unicode, quotes, nesting) ────────────────────────
case_seq("simple-two-steps", [{"action": "backup"}, {"action": "verify"}])
case_seq("unicode-step", [{"action": "détaché", "note": "naïve – ünïcode"}])
case_seq("quotes-and-escapes", [{"action": 'say "hi"', "path": "a\\b"}])
case_seq("nested-structure", [{"action": "a", "opts": {"z": 1, "a": [1, 2, {"k": None}]}}])
case_seq("empty-steps", [])
case_seq("non-dict-step", ["plain", 3, None, True])
case_seq("key-order-insensitive", [{"b": 1, "a": 2}])
# ── candidate_skill: evidence falsy law ─────────────────────────────────────
case_candidate("evidence-none", [{"action": "x"}], "backup", None)
case_candidate("evidence-empty-falsy", [{"action": "x"}], "backup", {})
case_candidate("evidence-given", [{"action": "a"}, {"action": "b"}], "test",
{"source": "verified-runs", "runs": 3})
case_candidate("family-empty", [{"action": "x"}], "", None)
# ── sandbox_test: every branch (results frozen, gate kernel-replayed) ──────
case_sandbox("default-ok-step",
{"id": "skill-abc", "family": "f", "steps": [{"action": "a"}],
"status": "candidate", "evidence": {}},
[{"step": {"action": "a"}, "ok": True, "detail": "shape-ok"}])
case_sandbox("default-missing-action",
{"id": "skill-def", "steps": [{"nope": 1}]},
[{"step": {"nope": 1}, "ok": False, "detail": "missing action"}])
case_sandbox("default-non-dict-step",
{"id": "skill-ghi", "steps": ["not-a-step"]},
[{"step": "not-a-step", "ok": False, "detail": "missing action"}])
case_sandbox("replay-ok-tuple",
{"id": "skill-jkl", "steps": [{"action": "a"}]},
[{"step": {"action": "a"}, "ok": True, "detail": "replayed"}])
case_sandbox("replay-fail-tuple",
{"id": "skill-mno", "steps": [{"action": "a"}]},
[{"step": {"action": "a"}, "ok": False, "detail": "exit 1"}])
case_sandbox("replay-crash-is-failure",
{"id": "skill-pqr", "steps": [{"action": "boom"}]},
[{"step": {"action": "boom"}, "ok": False, "detail": "boom"}])
case_sandbox("empty-steps-vacuous-pass",
{"id": "skill-stu", "steps": []},
[])
case_sandbox("missing-steps-key",
{"id": "skill-vwx", "family": "f"},
[])
case_sandbox("mixed-ok-and-fail",
{"id": "skill-yz", "steps": [{"action": "a"}, {"nope": 1}]},
[{"step": {"action": "a"}, "ok": True, "detail": "shape-ok"},
{"step": {"nope": 1}, "ok": False, "detail": "missing action"}])
# ── promote: every branch ───────────────────────────────────────────────────
case_promote("no-sandbox-key", {"id": "skill-1", "status": "candidate"})
case_promote("sandbox-failed", {"id": "skill-2",
"sandbox": {"passed": False, "results": []}})
case_promote("sandbox-passed", {"id": "skill-3", "status": "candidate",
"sandbox": {"passed": True,
"results": [{"ok": True}]}})
case_promote("sandbox-empty-falsy", {"id": "skill-4", "sandbox": {}})
truth = {
"module": "skill_compiler",
"core_pin": "RUNE_SKC_CORE=python",
"sequence_id_cases": cases_sequence_id,
"candidate_skill_cases": cases_candidate,
"sandbox_gate_cases": cases_gate,
"sandbox_cases": cases_sandbox,
"promote_cases": cases_promote,
}
out = REPO / "src" / "rust" / "skill_compiler" / "frozen_truth.json"
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps(truth, indent=1, sort_keys=True) + "\n",
encoding="utf-8")
n = sum(len(v) for k, v in truth.items() if k.endswith("_cases"))
print(f"frozen {n} cases -> {out}")