216 lines
8.5 KiB
Python
216 lines
8.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Live parity: subsystem_health Rust kernel vs Python oracle (dichotomy
|
|
wave). Layers:
|
|
1. FROZEN — 16 frozen-oracle cases replayed through BOTH the Python
|
|
floor and the Rust kernel.
|
|
2. FUZZ — 500 seeded registry sequences + failure-core calls
|
|
(seed 2479), oracle floor vs kernel, exact compare (sets order-free).
|
|
3. LIVE — full record_failure path (logging + event_bus + filer
|
|
side effects included): fresh process-global registry, oracle floor
|
|
vs facade kernel, comparing surface bools, registry snapshots and
|
|
the observed event payloads captured via a monkeypatched event_bus.
|
|
"""
|
|
import copy
|
|
import json
|
|
import os
|
|
import random
|
|
import string
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(REPO / "src"))
|
|
|
|
import rune.subsystem_health as sh # noqa: E402
|
|
from rune import sh_loader # noqa: E402
|
|
|
|
TRUTH = json.loads(
|
|
(REPO / "src" / "rust" / "subsystem_health" / "src"
|
|
/ "frozen_sh_truth.json").read_text())
|
|
|
|
fails = []
|
|
counts = {"frozen": 0, "fuzz": 0, "live": 0}
|
|
|
|
SUBS = ["cache", "store", "auth", "broker", "memory", "tui"]
|
|
ERRS = ["boom", "disk full", "timeout after 3s", "kaboom 💥", "x" * 350]
|
|
|
|
|
|
def fail(layer, name, detail):
|
|
fails.append((layer, name, detail))
|
|
print(f"FAIL[{layer}] {name}: {detail}")
|
|
|
|
|
|
def norm_state(reg):
|
|
return {"entries": copy.deepcopy(reg._entries),
|
|
"surfaced": sorted(reg._surfaced)}
|
|
|
|
|
|
def jnorm(x):
|
|
return json.dumps(x, sort_keys=True)
|
|
|
|
|
|
def frozen():
|
|
for group, cases in TRUTH.items():
|
|
if group in ("module",):
|
|
continue
|
|
for c in cases:
|
|
counts["frozen"] += 1
|
|
args = copy.deepcopy(c["args"])
|
|
fn = c["fn"]
|
|
want = c["expect"]
|
|
if fn == "registry_record":
|
|
got_k = sh_loader.record(args["entries"], args["surfaced"],
|
|
args["subsystem"], args["error"],
|
|
args["tier"])
|
|
reg = sh.SubsystemHealthRegistry()
|
|
reg._entries = args["entries"]
|
|
reg._surfaced = set(args["surfaced"])
|
|
surface = reg.record(args["subsystem"], args["error"],
|
|
args["tier"])
|
|
got_o = {"entries": reg._entries,
|
|
"surfaced": sorted(reg._surfaced),
|
|
"surface": surface}
|
|
for tag, got in (("oracle", got_o), ("kernel", got_k)):
|
|
if jnorm({"entries": got["entries"],
|
|
"surface": got["surface"]}) != \
|
|
jnorm({"entries": want["state"]["entries"],
|
|
"surface": want["surface"]}) or \
|
|
sorted(got["surfaced"]) != \
|
|
sorted(want["state"]["surfaced"]):
|
|
fail("frozen", f"{fn}/{c['name']}/{tag}",
|
|
f"want={jnorm(want)[:260]} got={jnorm(got)[:260]}")
|
|
elif fn == "registry_snapshot":
|
|
got_o = json.loads(json.dumps(args["entries"]))
|
|
got_k = sh_loader.snapshot(args["entries"])
|
|
for tag, got in (("oracle", got_o), ("kernel", got_k)):
|
|
if jnorm(got) != jnorm(want):
|
|
fail("frozen", f"{fn}/{c['name']}/{tag}",
|
|
f"want={jnorm(want)[:260]} got={jnorm(got)[:260]}")
|
|
elif fn == "failure_core":
|
|
got_k = sh_loader.failure_core(
|
|
args["error_type"], args["error"], args["context"],
|
|
args["tier"])
|
|
message = f"{args['error_type']}: {args['error']}"
|
|
if args["context"]:
|
|
message = f"{message} ({args['context']})"
|
|
got_o = {"message": message,
|
|
"event_type": sh._TIER_EVENTS.get(
|
|
args["tier"], "storage.degraded"),
|
|
"payload_error": message[:300]}
|
|
for tag, got in (("oracle", got_o), ("kernel", got_k)):
|
|
if jnorm(got) != jnorm(want):
|
|
fail("frozen", f"{fn}/{c['name']}/{tag}",
|
|
f"want={jnorm(want)[:260]} got={jnorm(got)[:260]}")
|
|
|
|
|
|
def fuzz():
|
|
rnd = random.Random(2479)
|
|
for i in range(500):
|
|
counts["fuzz"] += 1
|
|
reg_o = sh.SubsystemHealthRegistry()
|
|
entries, surfaced = {}, []
|
|
ok = True
|
|
for _ in range(rnd.randint(1, 8)):
|
|
subsystem = rnd.choice(SUBS)
|
|
error = rnd.choice(ERRS)
|
|
tier = rnd.choice([-1, 0, 1, 2, 3, 99])
|
|
ko = sh_loader.record(entries, surfaced, subsystem, error, tier)
|
|
surface_o = reg_o.record(subsystem, error, tier)
|
|
# the kernel OWNS the evolving state — feed its output forward
|
|
entries, surfaced = ko["entries"], list(ko["surfaced"])
|
|
if ko["surface"] != surface_o or \
|
|
jnorm(ko["entries"]) != jnorm(reg_o._entries) or \
|
|
sorted(ko["surfaced"]) != sorted(reg_o._surfaced):
|
|
fail("fuzz", f"fuzz{i}", f"record diverged at {subsystem}")
|
|
ok = False
|
|
break
|
|
if ok and jnorm(sh_loader.snapshot(entries)) != \
|
|
jnorm(reg_o.snapshot()):
|
|
fail("fuzz", f"fuzz{i}/snapshot", "snapshot diverged")
|
|
counts["fuzz"] += 1
|
|
error_type = rnd.choice(["ValueError", "KeyError", "RuntimeError"])
|
|
error = rnd.choice(ERRS)
|
|
context = rnd.choice(["", "during flush", "é" * 150])
|
|
tier = rnd.choice([-1, 0, 1, 2, 99])
|
|
core = sh_loader.failure_core(error_type, error, context, tier)
|
|
message = f"{error_type}: {error}"
|
|
if context:
|
|
message = f"{message} ({context})"
|
|
if jnorm(core) != jnorm({
|
|
"message": message,
|
|
"event_type": sh._TIER_EVENTS.get(tier, "storage.degraded"),
|
|
"payload_error": message[:300],
|
|
}):
|
|
fail("fuzz", f"fuzz{i}/core", f"core={core}")
|
|
|
|
|
|
def live():
|
|
"""Full record_failure path: surface bools + registry + observed
|
|
event payloads, oracle floor vs facade kernel."""
|
|
captured = []
|
|
|
|
class _CaptureBus:
|
|
@staticmethod
|
|
def emit(event_type, source="", payload=None):
|
|
captured.append({"event_type": event_type, "source": source,
|
|
"payload": payload})
|
|
|
|
import rune.event_bus as eb
|
|
orig_emit = eb.emit
|
|
eb.emit = _CaptureBus.emit
|
|
try:
|
|
for i in range(40):
|
|
rnd = random.Random(1000 + i)
|
|
captured.clear()
|
|
sh._AUTO_FILER = None
|
|
events = [(rnd.choice(SUBS), rnd.choice(ERRS),
|
|
rnd.choice([-1, 0, 1, 2, 3])) for _ in range(5)]
|
|
results = {}
|
|
for pin in ("python", "auto"):
|
|
reg = sh.SubsystemHealthRegistry()
|
|
os.environ["RUNE_SH_CORE"] = pin
|
|
surfaces = []
|
|
seen_events = []
|
|
captured.clear()
|
|
|
|
def fake_emit(event_type, source="", payload=None):
|
|
seen_events.append({"event_type": event_type,
|
|
"payload": payload})
|
|
|
|
eb.emit = fake_emit
|
|
for subsystem, error, tier in events:
|
|
surfaces.append(sh.record_failure(
|
|
subsystem, RuntimeError(error), tier=tier,
|
|
registry=reg))
|
|
results[pin] = {"surfaces": surfaces,
|
|
"state": norm_state(reg),
|
|
"events": seen_events}
|
|
counts["live"] += 1
|
|
if jnorm(results["python"]) != jnorm(results["auto"]):
|
|
fail("live", f"live{i}",
|
|
f"python={jnorm(results['python'])[:260]} "
|
|
f"auto={jnorm(results['auto'])[:260]}")
|
|
finally:
|
|
eb.emit = orig_emit
|
|
os.environ["RUNE_SH_CORE"] = "auto"
|
|
|
|
|
|
def main():
|
|
if not sh_loader.available():
|
|
print("kernel not built — build src/rust/subsystem_health first")
|
|
return 1
|
|
os.environ["RUNE_SH_CORE"] = "python"
|
|
frozen()
|
|
fuzz()
|
|
live()
|
|
os.environ["RUNE_SH_CORE"] = "auto"
|
|
total = counts["frozen"] + counts["fuzz"] + counts["live"]
|
|
print(f"parity {total - len(fails)}/{total} "
|
|
f"(frozen {counts['frozen']}, fuzz {counts['fuzz']}, "
|
|
f"live {counts['live']})")
|
|
return 1 if fails else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|