Files
rune/tests/test_skill_compiler_port.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

117 lines
3.7 KiB
Python

"""Tests: skill_compiler.py Rust core port (dichotomy wave, #2479).
Frozen-truth battery: the frozen-oracle cases replayed VERBATIM through
the facade in BOTH core modes (RUNE_SKC_CORE=python floor vs the Rust
kernel), plus loader pin discipline, the replay-crash-is-failure law and
input-copy semantics.
"""
from __future__ import annotations
import copy
import json
import os
import sys
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
sys.path.insert(0, str(ROOT / "src" / "rune"))
import skill_compiler as sk # noqa: E402
from rune import sk_loader # noqa: E402
TRUTH = json.loads(
(ROOT / "src" / "rust" / "skill_compiler" / "frozen_truth.json")
.read_text())
@pytest.fixture()
def core_built():
if not sk_loader.available():
pytest.skip("rust skill_compiler core not built")
return True
def drive(mode, fn, *args):
os.environ["RUNE_SKC_CORE"] = mode
try:
return fn(*copy.deepcopy(args))
finally:
os.environ["RUNE_SKC_CORE"] = "auto"
def test_loader_probe_and_pin(core_built, monkeypatch):
monkeypatch.delenv("RUNE_SKC_CORE", raising=False)
assert sk_loader.available()
assert sk._kernel() is not None
monkeypatch.setenv("RUNE_SKC_CORE", "python")
assert sk._kernel() is None
def test_frozen_sequence_id_both_ways(core_built):
for c in TRUTH["sequence_id_cases"]:
py = drive("python", sk.sequence_id, c["args"]["steps"])
rs = drive("auto", sk.sequence_id, c["args"]["steps"])
assert py == rs == c["expect"], c["name"]
def test_frozen_candidate_both_ways(core_built):
for c in TRUTH["candidate_skill_cases"]:
args = (c["args"]["steps"], c["args"]["family"],
c["args"]["evidence"])
py = drive("python", sk.candidate_skill, *args)
rs = drive("auto", sk.candidate_skill, *args)
assert py == rs == c["expect"], c["name"]
def test_frozen_sandbox_both_ways(core_built):
def replay_fn_for(detail):
if detail == "replayed":
return lambda step: (True, "replayed")
if detail == "exit 1":
return lambda step: (False, "exit 1")
if detail == "boom":
def crash(step):
raise RuntimeError("boom")
return crash
return None
for c in TRUTH["sandbox_gate_cases"]:
details = {r["detail"] for r in c["args"]["results"]}
fn = replay_fn_for(next(iter(details))) if len(details) == 1 else None
py = drive("python", sk.sandbox_test, c["args"]["skill"], fn)
rs = drive("auto", sk.sandbox_test, c["args"]["skill"], fn)
assert py == rs == c["expect"], c["name"]
def test_frozen_promote_both_ways(core_built):
for c in TRUTH["promote_cases"]:
py = drive("python", sk.promote, c["args"]["skill"])
rs = drive("auto", sk.promote, c["args"]["skill"])
assert py == rs == c["expect"], c["name"]
def test_replay_crash_is_failure_verdict(core_built):
skill = {"id": "skill-x", "steps": [{"action": "boom"}]}
def crash(step):
raise ValueError("boom went wrong")
py = drive("python", sk.sandbox_test, skill, crash)
rs = drive("auto", sk.sandbox_test, skill, crash)
assert py == rs
assert py["sandbox"]["passed"] is False
assert py["sandbox"]["results"][0]["detail"] == "boom went wrong"
def test_inputs_not_mutated(core_built):
steps = [{"action": "a"}, {"nope": 1}]
frozen_steps = copy.deepcopy(steps)
drive("auto", sk.sequence_id, steps)
skill = drive("auto", sk.candidate_skill, steps, "f", None)
drive("auto", sk.sandbox_test, skill)
drive("auto", sk.promote, skill)
assert steps == frozen_steps