FULL parity port to Rust (9/9 fns, was 3/9): token estimation (codepoint len law), the pressure ladder (raw-gate vs round3 display), degradation signals (int-0 max law, \w+ runs), JSONL entry assembly (dict-spread override), log tail reader (deque(maxlen) + file-iteration law), stats counter (crash parity on non-dict lines), on-message hook (truthiness, message/text/str(data) fallback, pyrepr), CLI dispatch (argparse shapes, .1% renders, code-2 law). File IO + wall clock stay in the Python facade (no-clock law); cps_loader.py is the ctypes seam (RUNE_CPS_CORE / RUNE_CONTEXT_PRESSURE_CORE pins). Frozen truth 75 cases under the cps_freeze_seams rig (byte-identical x2, shared-harness --verify OK 28 envelope cases); branch battery 10/10; live parity frozen=78 regen=6 fuzz=1400 live=18 failures=0; pytest 34/34 both pins; clippy -D warnings + fmt clean; audit 3/9 PARTIAL -> 9/9 FULL (RUST). Evidence: verification/summary-cp.md + cp-* files.
385 lines
15 KiB
Python
385 lines
15 KiB
Python
#!/usr/bin/env python3
|
|
"""Live parity: context_pressure Rust core vs Python oracle (dichotomy wave,
|
|
close-PARTIAL gap pass). Layers:
|
|
|
|
1. FROZEN — (a) the envelope truth corpus replayed through the ctypes
|
|
FFI; (b) kernel cases (entry BYTE lines, log parses, stats counts,
|
|
hook result+entry, CLI dispatch {output, code, entry}) through the
|
|
FFI; (c) INDEPENDENT oracle-floor regeneration of both sections
|
|
compared against the frozen file (truth-consumed-not-self-generated).
|
|
2. FUZZ — 1250+ seeded comparisons (seed 2479): oracle floor vs kernel
|
|
across estimate/measure/detect/entry-assembly/log-tail/stats-counts/
|
|
hook (crash parity included, exact str(e) contract).
|
|
3. LIVE — dual-mode facade battery: the same calls through
|
|
rune.context_pressure with RUNE_CPS_CORE=python (oracle floor) and
|
|
with the Rust core live; return docs AND the appended JSONL lines
|
|
must be identical (the clock is rig-frozen, so lines are byte-stable).
|
|
|
|
MUST run under the freeze rig: PYTHONPATH=scripts/cps_freeze_seams (pins
|
|
the oracle floor + hermetic scratch + frozen clock).
|
|
Exit 0 only at 100% parity on the gap-closing surface.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import random
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(REPO / "scripts"))
|
|
|
|
import context_pressure_truth as cpt # noqa: E402
|
|
|
|
SCRATCH = cpt.SCRATCH
|
|
FROZEN_NOW = cpt.FROZEN_NOW
|
|
|
|
# Rig guard: refuse to run unpinned (would touch the real ~/.rune).
|
|
if os.environ.get("RUNE_MEMORY_DIR") != os.path.join(SCRATCH, "memory"):
|
|
sys.exit("FATAL: run under the rig (PYTHONPATH=scripts/cps_freeze_seams)")
|
|
if os.environ.get("RUNE_CPS_CORE") != "python":
|
|
sys.exit("FATAL: rig must pin RUNE_CPS_CORE=python")
|
|
|
|
sys.path.insert(0, str(REPO / "src"))
|
|
|
|
import rune.context_pressure as cp # noqa: E402
|
|
from rune import cps_loader # noqa: E402
|
|
|
|
if not cps_loader.available():
|
|
sys.exit("FATAL: Rust core not built — cargo build --release in src/rust/context_pressure")
|
|
|
|
TRUTH = json.loads(
|
|
(REPO / "src" / "rust" / "context_pressure" / "src"
|
|
/ "frozen_cp_truth.json").read_text())
|
|
|
|
fails: list[str] = []
|
|
counts = {"frozen": 0, "regen": 0, "fuzz": 0, "live": 0}
|
|
|
|
|
|
def fail(layer, name, detail):
|
|
fails.append(f"[{layer}] {name}: {detail}")
|
|
print(f"FAIL[{layer}] {name}: {detail}"[:400])
|
|
|
|
|
|
def j(x):
|
|
return json.dumps(x, sort_keys=True, ensure_ascii=True)
|
|
|
|
|
|
def check(layer, name, want, got):
|
|
counts[layer] += 1
|
|
if j(want) != j(got):
|
|
fail(layer, name, f"want={j(want)[:300]} got={j(got)[:300]}")
|
|
|
|
|
|
def check_crash(layer, name, want, exc):
|
|
"""want is the frozen '__crash__ TYPE: message' string."""
|
|
counts[layer] += 1
|
|
got = f"{type(exc).__name__}: {exc}"
|
|
if j(want) != j(got):
|
|
fail(layer, name, f"want={want} got={got}")
|
|
|
|
|
|
def pin(mode):
|
|
if mode == "python":
|
|
os.environ["RUNE_CPS_CORE"] = "python"
|
|
os.environ["RUNE_CONTEXT_PRESSURE_CORE"] = "python"
|
|
else:
|
|
os.environ.pop("RUNE_CPS_CORE", None)
|
|
os.environ.pop("RUNE_CONTEXT_PRESSURE_CORE", None)
|
|
|
|
|
|
# ── layer 1: FROZEN (kernel through the FFI) ──────────────────────────────
|
|
|
|
for row in TRUTH.get("cases", []):
|
|
fn, args, kwargs, want = (row["fn"], row["args"], row.get("kwargs", {}),
|
|
row["expected"])
|
|
try:
|
|
if fn == "estimate_tokens":
|
|
check("frozen", f"ffi_{fn}:{args[0][:12]}", want,
|
|
cps_loader.estimate_tokens(args[0]))
|
|
elif fn == "measure_pressure":
|
|
check("frozen", f"ffi_{fn}:{args[0][:12]}:{kwargs.get('max_tokens')}",
|
|
want, cps_loader.measure_pressure(args[0], kwargs.get("max_tokens", 128000)))
|
|
elif fn == "detect_degradation":
|
|
check("frozen", f"ffi_{fn}:{args[0][0][:12]}", want,
|
|
cps_loader.detect_degradation(args[0]))
|
|
elif fn == "handle_hook":
|
|
got = cps_loader.handle_hook(args[0], args[1], FROZEN_NOW)
|
|
check("frozen", f"ffi_{fn}:{args[0]}", want, got["result"])
|
|
except Exception as e: # noqa: BLE001 — crash parity surfaces here
|
|
fail("frozen", f"ffi_{fn}", f"{type(e).__name__}: {e}")
|
|
|
|
for kind, rows in TRUTH.get("kernel_cases", {}).items():
|
|
for row in rows:
|
|
args, expect, name = row["args"], row["expect"], row["name"]
|
|
if kind == "logMeasurement":
|
|
entry = cps_loader.log_measurement(args["measurement"],
|
|
args["session_id"], args["now"])
|
|
counts["frozen"] += 1
|
|
# BYTE law: the facade writes json.dumps(entry) + "\n" (default
|
|
# ensure_ascii) — the frozen line is the oracle's exact bytes.
|
|
if json.dumps(entry) + "\n" != expect["line"]:
|
|
fail("frozen", f"{kind}/{name}/line",
|
|
f"want={expect['line'][:160]} got={json.dumps(entry)[:160]}")
|
|
elif kind == "readLog":
|
|
check("frozen", f"{kind}/{name}", expect,
|
|
cps_loader.read_log(args["content"], args["limit"]))
|
|
elif kind == "logStats":
|
|
try:
|
|
check("frozen", f"{kind}/{name}", expect,
|
|
cps_loader.log_stats(args["content"]))
|
|
except Exception as e: # noqa: BLE001
|
|
if isinstance(expect, dict) and "__crash__" in expect:
|
|
check_crash("frozen", f"{kind}/{name}", expect["__crash__"], e)
|
|
else:
|
|
fail("frozen", f"{kind}/{name}", f"unexpected crash {e}")
|
|
elif kind == "kernelHook":
|
|
try:
|
|
got = cps_loader.handle_hook(args["event"], args["data"], args["now"])
|
|
if isinstance(expect, dict) and "__crash__" in expect:
|
|
fail("frozen", f"{kind}/{name}", "kernel did not crash where oracle did")
|
|
else:
|
|
check("frozen", f"{kind}/{name}", expect, got)
|
|
except Exception as e: # noqa: BLE001
|
|
if isinstance(expect, dict) and "__crash__" in expect:
|
|
check_crash("frozen", f"{kind}/{name}", expect["__crash__"], e)
|
|
else:
|
|
fail("frozen", f"{kind}/{name}", f"unexpected crash {e}")
|
|
elif kind == "mainDispatch":
|
|
try:
|
|
got = cps_loader.main_dispatch(args["argv"], args["content"], args["now"])
|
|
want = {k: expect[k] for k in ("output", "code") if k in expect}
|
|
gotv = {k: got[k] for k in ("output", "code") if k in got}
|
|
check("frozen", f"{kind}/{name}", want, gotv)
|
|
if "entry" in expect:
|
|
check("frozen", f"{kind}/{name}/entry", expect["entry"], got.get("entry"))
|
|
except Exception as e: # noqa: BLE001
|
|
fail("frozen", f"{kind}/{name}", f"unexpected crash {e}")
|
|
|
|
# ── layer 1c: independent oracle-floor regeneration vs the frozen file ────
|
|
|
|
env_regen = cpt.regen_envelope_cases()
|
|
check("regen", "envelope_cases", TRUTH["cases"], env_regen)
|
|
kc_regen = cpt.regen_kernel_cases()
|
|
for kind, rows in TRUTH["kernel_cases"].items():
|
|
check("regen", f"kernel_cases:{kind}", rows, kc_regen[kind])
|
|
|
|
# ── layer 2: FUZZ (oracle floor vs kernel, seeded) ────────────────────────
|
|
|
|
rng = random.Random(2479)
|
|
VOCAB = ["hello", "world", "éé", "üü", "dup", "word", "👍", "a_b_c",
|
|
"MiXeD", "runner", "label", "law", "123"]
|
|
WORDS = ["dup", "word", "pair", "echo", "alpha", "beta"]
|
|
|
|
|
|
def rand_text():
|
|
n = rng.randrange(0, 12)
|
|
return " ".join(rng.choice(VOCAB) for _ in range(n))
|
|
|
|
|
|
def rand_measurement():
|
|
if rng.random() < 0.5:
|
|
return cp.measure_pressure(rand_text())
|
|
base = rng.choice(["tokens", "user_key", "note"])
|
|
doc = {base: rng.randrange(0, 50)}
|
|
if rng.random() < 0.7:
|
|
doc["pressure"] = rng.choice([0.0, 0.5, 1.0, 0.666, 2.5])
|
|
if rng.random() < 0.5:
|
|
doc["status"] = rng.choice(["ok", "warning", "critical", None])
|
|
return doc
|
|
|
|
|
|
for trial in range(200):
|
|
text = rand_text()
|
|
check("fuzz", f"estimate:{trial}", cp.estimate_tokens(text),
|
|
cps_loader.estimate_tokens(text))
|
|
|
|
for trial in range(400):
|
|
text = rand_text()
|
|
max_tokens = rng.choice([1, 2, 3, 7, 12000, 20000, 128000, 99999])
|
|
w_err = g_err = None
|
|
try:
|
|
w = cp.measure_pressure(text, max_tokens=max_tokens)
|
|
except Exception as e: # noqa: BLE001
|
|
w, w_err = None, f"{type(e).__name__}: {e}"
|
|
try:
|
|
g = cps_loader.measure_pressure(text, max_tokens)
|
|
except Exception as e: # noqa: BLE001
|
|
g, g_err = None, f"{type(e).__name__}: {e}"
|
|
counts["fuzz"] += 1
|
|
if w_err or g_err:
|
|
if w_err != g_err:
|
|
fail("fuzz", f"measure:{trial}", f"crash mismatch {w_err} vs {g_err}")
|
|
elif j(w) != j(g):
|
|
fail("fuzz", f"measure:{trial}", f"want={j(w)[:200]} got={j(g)[:200]}")
|
|
|
|
for trial in range(200):
|
|
n = rng.randrange(2, 9)
|
|
responses = []
|
|
for i in range(n):
|
|
if i == 0 and rng.random() < 0.5:
|
|
responses.append(rand_text())
|
|
else:
|
|
responses.append(" ".join(rng.choice(WORDS)
|
|
for _ in range(rng.randrange(1, 12))))
|
|
check("fuzz", f"detect:{trial}", cp.detect_degradation(responses),
|
|
cps_loader.detect_degradation(responses))
|
|
|
|
for trial in range(200):
|
|
m = rand_measurement()
|
|
sid = rng.choice(["", "session A", "s" * 40])
|
|
want = {"timestamp": FROZEN_NOW, "session_id": sid, **m}
|
|
got = cps_loader.log_measurement(m, sid, FROZEN_NOW)
|
|
counts["fuzz"] += 1
|
|
if j(want) != j(got):
|
|
fail("fuzz", f"log_measurement:{trial}", f"want={j(want)[:200]} got={j(got)[:200]}")
|
|
|
|
LOG_POOL = [
|
|
'{"tokens": 3, "pressure": 0.5, "status": "warning"}',
|
|
'{"tokens": 1, "pressure": 0.0, "status": "ok"}',
|
|
'{"status": "critical"}',
|
|
"not json at all",
|
|
"5",
|
|
"",
|
|
' {"padded": true} ',
|
|
"[]",
|
|
'"str line"',
|
|
]
|
|
|
|
for trial in range(150):
|
|
n = rng.randrange(0, 10)
|
|
content = "\n".join(rng.choice(LOG_POOL) for _ in range(n))
|
|
if content and rng.random() < 0.5:
|
|
content += "\n"
|
|
limit = rng.choice([-2, 0, 1, 2, 3, 5, 20])
|
|
cpt._seed_log(content)
|
|
w = cp.read_log(limit=limit)
|
|
g = cps_loader.read_log(content, limit)
|
|
counts["fuzz"] += 1
|
|
if j(w) != j(g):
|
|
fail("fuzz", f"read_log:{trial}:lim{limit}", f"want={j(w)[:200]} got={j(g)[:200]}")
|
|
|
|
for trial in range(100):
|
|
n = rng.randrange(0, 8)
|
|
content = "\n".join(rng.choice(LOG_POOL) for _ in range(n))
|
|
if content and rng.random() < 0.7:
|
|
content += "\n"
|
|
cpt._seed_log(content)
|
|
try:
|
|
w = cp.log_stats()
|
|
w_err = None
|
|
except Exception as e: # noqa: BLE001
|
|
w, w_err = None, f"{type(e).__name__}: {e}"
|
|
try:
|
|
g = cps_loader.log_stats(content)
|
|
g_err = None
|
|
except Exception as e: # noqa: BLE001
|
|
g, g_err = None, f"{type(e).__name__}: {e}"
|
|
counts["fuzz"] += 1
|
|
if w_err or g_err:
|
|
if w_err != g_err:
|
|
fail("fuzz", f"log_stats:{trial}", f"crash mismatch {w_err} vs {g_err}")
|
|
elif j(w) != j(g):
|
|
fail("fuzz", f"log_stats:{trial}", f"want={j(w)[:200]} got={j(g)[:200]}")
|
|
|
|
HOOK_EVENTS = ["on_message", "on_message", "on_message", "other_event", ""]
|
|
HOOK_DATAS = [
|
|
{"message": "hello"},
|
|
{"message": "x" * 95000},
|
|
{"text": "hello world"},
|
|
{"message": "", "text": "ok text"},
|
|
{"other": 1},
|
|
{"a": True, "n": None},
|
|
{"k": "a'b"},
|
|
{},
|
|
None,
|
|
[1, 2],
|
|
True,
|
|
5,
|
|
"plain str",
|
|
]
|
|
|
|
for trial in range(150):
|
|
event = rng.choice(HOOK_EVENTS)
|
|
data = rng.choice(HOOK_DATAS)
|
|
seed_data = json.loads(json.dumps(data)) if isinstance(data, (dict, list, str)) else data
|
|
try:
|
|
cpt._seed_log("")
|
|
w = cp.handle_hook(event, seed_data)
|
|
w_entry = None
|
|
for line in cpt._read_log_file().splitlines():
|
|
if line.strip():
|
|
w_entry = json.loads(line)
|
|
w_err = None
|
|
except Exception as e: # noqa: BLE001
|
|
w, w_entry, w_err = None, None, f"{type(e).__name__}: {e}"
|
|
try:
|
|
g = cps_loader.handle_hook(event, data, FROZEN_NOW)
|
|
g_err = None
|
|
except Exception as e: # noqa: BLE001
|
|
g, g_err = None, f"{type(e).__name__}: {e}"
|
|
counts["fuzz"] += 1
|
|
if w_err or g_err:
|
|
if w_err != g_err:
|
|
fail("fuzz", f"hook:{trial}:{event}", f"crash mismatch {w_err} vs {g_err}")
|
|
else:
|
|
g_entry = g.get("entry")
|
|
want = {"result": w, "entry": w_entry}
|
|
got = {"result": g.get("result"), "entry": g_entry}
|
|
if j(want) != j(got):
|
|
fail("fuzz", f"hook:{trial}:{event}", f"want={j(want)[:260]} got={j(got)[:260]}")
|
|
|
|
# ── layer 3: LIVE (dual-mode facade battery) ──────────────────────────────
|
|
|
|
BIG = "x" * 95000
|
|
CASES = [
|
|
("estimate_unicode", lambda: cp.estimate_tokens("héllo wörld 👍")),
|
|
("measure_ok", lambda: cp.measure_pressure("hello world")),
|
|
("measure_warning", lambda: cp.measure_pressure("x" * 49000, max_tokens=20000)),
|
|
("measure_critical", lambda: cp.measure_pressure(BIG, max_tokens=12000)),
|
|
("measure_empty", lambda: cp.measure_pressure("")),
|
|
("detect_trio", lambda: cp.detect_degradation(["aaa", "bbb", "ccc"])),
|
|
("detect_decline", lambda: cp.detect_degradation(["x" * 200, "y" * 180, "", "", ""])),
|
|
("detect_insufficient", lambda: cp.detect_degradation(["a", "b"])),
|
|
("read_log", lambda: cpt._seed_log('{"a": 1}\nbroken\n{"b": 2}\n')
|
|
or cp.read_log(limit=2)),
|
|
("log_stats", lambda: cpt._seed_log(
|
|
'{"status": "warning"}\n{"status": "critical"}\nbroken\n') or cp.log_stats()),
|
|
("log_stats_empty", lambda: cpt._seed_log("") or cp.log_stats()),
|
|
("log_measurement", lambda: cpt._seed_log("") or cp.log_measurement(
|
|
{"tokens": 3, "max_tokens": 128000, "pressure": 0.0, "status": "ok",
|
|
"recommendation": "none"}, "sid-1") or cpt._read_log_file()),
|
|
("log_measurement_override", lambda: cpt._seed_log("") or cp.log_measurement(
|
|
{"timestamp": "T-override", "user_key": "u"}, "") or cpt._read_log_file()),
|
|
("hook_ok", lambda: cpt._seed_log("") or cp.handle_hook("on_message", {"text": "hello"})),
|
|
("hook_critical", lambda: cpt._seed_log("") or cp.handle_hook(
|
|
"on_message", {"message": BIG})),
|
|
("hook_str_data", lambda: cpt._seed_log("") or cp.handle_hook(
|
|
"on_message", {"other": 1})),
|
|
("hook_empty", lambda: cpt._seed_log("") or cp.handle_hook("on_message", {})),
|
|
("hook_wrong_event", lambda: cpt._seed_log("") or cp.handle_hook(
|
|
"ping", {"message": BIG})),
|
|
]
|
|
|
|
results = {}
|
|
for mode in ("python", "rust"):
|
|
pin(mode)
|
|
mode_out = {}
|
|
for name, fn in CASES:
|
|
try:
|
|
mode_out[name] = fn()
|
|
except Exception as e: # noqa: BLE001 — crash parity surfaces here
|
|
mode_out[name] = {"__crash__": f"{type(e).__name__}: {e}"}
|
|
results[mode] = mode_out
|
|
pin("python")
|
|
|
|
for name, _ in CASES:
|
|
counts["live"] += 1
|
|
w, g = results["python"][name], results["rust"][name]
|
|
if j(w) != j(g):
|
|
fail("live", f"dual:{name}", f"want={j(w)[:260]} got={j(g)[:260]}")
|
|
|
|
print(f"parity: frozen={counts['frozen']} regen={counts['regen']} "
|
|
f"fuzz={counts['fuzz']} live={counts['live']} failures={len(fails)}")
|
|
sys.exit(1 if fails else 0) |