Port the 8 audit-gap fns to Rust (name-preserving): _ms_core, _now_iso, promote_entry, compute_fama, compute_drift_velocity, compute_drift_direction, pre_commit_gate, pre_commit_gate_entry. The scoring surface delegates to memory_scoring (path dep — the authoritative kernel since its Python oracle was retired); the new decision kernels live in src/rust/drift_core while the sqlite IO stays in the facade (RUNE_DRIFT_CORE pin, dcore_loader seam, doc-envelope FFI, Python-typed JSON output: int/float split preserved). Audit 3/11 PARTIAL -> 12/12 FULL RUST. 30-row frozen truth (dc_freeze_seams rig, shared harness, gen+verify byte-identical x3), Rust battery 3/3 with the truth embedded via include_str!, live parity 30 frozen + 600 fuzz + 11 dual-mode facade rows failures=0, clippy -D warnings 0, fmt clean, pytest both pins 41 passed/4 skipped (drift-files test_211d_g failure verified pre-existing on the stashed base), ruff clean.
389 lines
14 KiB
Python
389 lines
14 KiB
Python
#!/usr/bin/env python3
|
|
"""Live parity: drift_core Rust kernel vs Python oracle (dichotomy wave,
|
|
close-PARTIAL gap pass). Layers:
|
|
|
|
1. FROZEN — the gap truth corpus (src/rust/drift_core/frozen_truth.json,
|
|
generated by scripts/gen_drift_core_truth.py under the
|
|
scripts/dc_freeze_seams rig against the LIVE oracle with
|
|
RUNE_DRIFT_CORE=python pinned): every row replayed through the ctypes
|
|
FFI kernel, exact JSON compare (typing included — ints stay ints,
|
|
round(x, n) floats stay floats), case by case.
|
|
2. FUZZ — seeded random surfaces (seed 2479): eligibility tuples, fama
|
|
row sets, velocity/direction aggregates, promote decisions — the
|
|
oracle's Python floor vs the FFI kernel, exact compare.
|
|
3. LIVE — dual-mode facade battery: the same db-carrying calls
|
|
(promote_entry incl. the post-UPDATE store state, compute_fama,
|
|
compute_drift_velocity, compute_drift_direction, pre_commit_gate)
|
|
executed with RUNE_DRIFT_CORE=python (oracle floor) and with the Rust
|
|
kernel live; results and store effects must be identical.
|
|
|
|
Exit 0 only at 100% parity on the gap-closing functions.
|
|
"""
|
|
import copy
|
|
import json
|
|
import os
|
|
import random
|
|
import shutil
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
os.environ["RUNE_DRIFT_CORE"] = "python" # oracle floor to start
|
|
PARITY_HOME = "/tmp/dc_parity"
|
|
|
|
sys.path.insert(0, str(REPO / "src"))
|
|
|
|
from rune import memory_store as store # noqa: E402
|
|
from rune import drift_core as dc # noqa: E402
|
|
from rune import dcore_loader # noqa: E402
|
|
|
|
if not dcore_loader.available():
|
|
sys.exit("FATAL: Rust kernel not built — cargo build --release in src/rust/drift_core")
|
|
|
|
TRUTH = json.loads(
|
|
(REPO / "src" / "rust" / "drift_core" / "frozen_truth.json").read_text()
|
|
)
|
|
|
|
NOW = "2026-10-13T12:00:00.123456+00:00"
|
|
|
|
# Pin the oracle's module clock to NOW (the harness's frozen-clock trick):
|
|
# the oracle's check_promotion_eligibility resolves `now` internally via
|
|
# _now_iso(None), and the kernel calls below pass NOW explicitly — both
|
|
# sides must see the same instant for the value-reason strings to match.
|
|
from datetime import datetime as _dt # noqa: E402
|
|
|
|
|
|
class _FrozenDatetime(_dt):
|
|
@classmethod
|
|
def now(cls, tz=None):
|
|
base = _dt.fromisoformat(NOW)
|
|
return base.replace(tzinfo=tz) if tz is not None else base
|
|
|
|
|
|
dc.datetime = _FrozenDatetime
|
|
|
|
fails: list[str] = []
|
|
counts = {"frozen": 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 pin(mode):
|
|
"""Flip the facade pin at runtime (dcore_loader.core reads env per call)."""
|
|
if mode == "python":
|
|
os.environ["RUNE_DRIFT_CORE"] = "python"
|
|
else:
|
|
os.environ.pop("RUNE_DRIFT_CORE", None)
|
|
|
|
|
|
def fresh_db(tag):
|
|
"""Hermetic per-call scratch store (both pins see identical state)."""
|
|
root = os.path.join(PARITY_HOME, tag)
|
|
if os.path.isdir(root):
|
|
shutil.rmtree(root)
|
|
os.makedirs(os.path.join(root, "store"), exist_ok=True)
|
|
path = os.path.join(root, "dc-parity.db")
|
|
db = store.connect(path)
|
|
return db, path
|
|
|
|
|
|
def seed_rows(db, rows):
|
|
for i, r in enumerate(rows):
|
|
db.execute(
|
|
"INSERT INTO entries (id, text, importance, created_at, status, "
|
|
"wash_count, rock_status, last_washed) VALUES (?,?,?,?,?,?,?,?)",
|
|
(
|
|
r.get("id", i + 1),
|
|
r.get("text", f"row {i}"),
|
|
r.get("importance", 0.5),
|
|
"2026-10-01T00:00:00.000000+00:00",
|
|
r.get("status", "active"),
|
|
r.get("wash_count", 0),
|
|
r.get("rock_status", "washable"),
|
|
r.get("last_washed"),
|
|
),
|
|
)
|
|
db.commit()
|
|
|
|
|
|
store._embed = lambda texts: {} # lexical-only, deterministic
|
|
|
|
# ── 1. FROZEN: replay the gap truth through the FFI kernel ──────────────────
|
|
|
|
for row in TRUTH["cases"]:
|
|
fn = row["fn"]
|
|
args = row["args"]
|
|
kwargs = row.get("kwargs", {})
|
|
exp = row["expected"]
|
|
if fn == "_now_iso":
|
|
if args[0] is None:
|
|
got = dcore_loader.now_iso(None)
|
|
check("frozen", "now_iso.frozen-clock-shape", True, "T" in got)
|
|
else:
|
|
check("frozen", "now_iso.passthrough", exp, dcore_loader.now_iso(args[0]))
|
|
elif fn == "dc_freeze_ms_core":
|
|
pin("python")
|
|
py_probe_none = dc._dc_core() is None
|
|
pin("live")
|
|
live_probe = dcore_loader.core() is not None
|
|
check("frozen", "ms_core.probe", [True, True, True],
|
|
[exp is True, py_probe_none, live_probe])
|
|
elif fn == "compute_value":
|
|
got = dcore_loader.compute_value(args[0], kwargs.get("current_goals"), NOW)
|
|
check("frozen", "compute_value", exp, got)
|
|
elif fn == "check_promotion_eligibility":
|
|
got = dcore_loader.check_promotion_eligibility(args[0], NOW)
|
|
check("frozen", "check_promotion_eligibility", exp, list(got))
|
|
elif fn == "pre_commit_gate_entry":
|
|
check("frozen", "gate_entry", exp, dcore_loader.pre_commit_gate_entry(args[0]))
|
|
elif fn == "dc_freeze_gate":
|
|
check("frozen", "gate.empty-body", exp, dcore_loader.pre_commit_gate())
|
|
elif fn == "dc_freeze_promote":
|
|
entry = args[0]
|
|
entry_id = args[1]
|
|
got = dcore_loader.promote_entry(entry, entry_id, NOW)
|
|
check("frozen", f"promote[{entry_id}]", exp["result"], got)
|
|
elif fn == "dc_freeze_fama":
|
|
got = dcore_loader.compute_fama(args[0], args[1], args[2])
|
|
check("frozen", "fama", exp, got)
|
|
elif fn == "dc_freeze_velocity":
|
|
changes, promotions = exp["counts"]
|
|
got = dcore_loader.compute_drift_velocity(changes, promotions, args[1])
|
|
check("frozen", f"velocity[d={args[1]}]", exp["out"], got)
|
|
elif fn == "dc_freeze_direction":
|
|
total, established, rock = exp["counts"]
|
|
got = dcore_loader.compute_drift_direction(total, established, rock)
|
|
check("frozen", "direction", exp["out"], got)
|
|
else:
|
|
sys.exit(f"FATAL: unmapped truth row {fn}")
|
|
|
|
# ── 2. FUZZ: seeded random surfaces, oracle floor vs FFI kernel ─────────────
|
|
|
|
rng = random.Random(2479)
|
|
|
|
|
|
def _iso(dt):
|
|
return dt.isoformat()
|
|
|
|
|
|
from datetime import datetime, timedelta, timezone # noqa: E402
|
|
|
|
_now = datetime.now(timezone.utc)
|
|
IN_W = _iso(_now - timedelta(seconds=60)) # inside every window (days >= 1)
|
|
OUT_W = _iso(_now - timedelta(days=3650)) # outside every window
|
|
|
|
for i in range(150):
|
|
entry = {
|
|
"id": i + 1,
|
|
"text": f"fuzz entry {i} proven deployment runner kubernetes",
|
|
"wash_count": rng.choice([0, 1, 5, 19, 20, 25, 40, 60]),
|
|
"importance": rng.choice([0.0, 0.3, 0.5, 0.8, 1.0]),
|
|
"rock_status": rng.choice(["washable", "established", "rock", "odd"]),
|
|
"last_accessed": rng.choice([
|
|
"2026-10-13T06:00:00.000000+00:00",
|
|
"2026-10-10T12:00:00.000000+00:00",
|
|
"2026-09-01T12:00:00.000000+00:00",
|
|
None,
|
|
]),
|
|
}
|
|
pin("python")
|
|
want = list(dc.check_promotion_eligibility(copy.deepcopy(entry)))
|
|
got = dcore_loader.check_promotion_eligibility(copy.deepcopy(entry), NOW)
|
|
check("fuzz", f"eligibility[{i}]", want, list(got))
|
|
|
|
for i in range(150):
|
|
n = rng.randint(0, 6)
|
|
rows = [
|
|
{
|
|
"status": rng.choice(["active", "active", "retired"]),
|
|
"rock_status": rng.choice(["washable", "established", "rock"]),
|
|
"wash_count": rng.choice([0, 1, 7, None]),
|
|
}
|
|
if rng.random() < 0.85
|
|
else None
|
|
for _ in range(n)
|
|
]
|
|
ids = list(range(1, n + 1))
|
|
db, _ = fresh_db(f"fuzz-fama-{i}")
|
|
try:
|
|
present = [{**r, "id": ids[k]} for k, r in enumerate(rows) if r is not None]
|
|
if present:
|
|
seed_rows(db, present)
|
|
pin("python")
|
|
want = dc.compute_fama(db, "fuzz query", ids)
|
|
finally:
|
|
db.close()
|
|
got = dcore_loader.compute_fama(rows, "fuzz query", ids)
|
|
check("fuzz", f"fama[{i}]", want, got)
|
|
|
|
for i in range(100):
|
|
promotions = rng.randint(0, 20)
|
|
changes = promotions + rng.randint(0, 40)
|
|
days = rng.choice([1, 2, 3, 7, 14, 30])
|
|
db, _ = fresh_db(f"fuzz-vel-{i}")
|
|
try:
|
|
# Seed so the oracle's SQL lands exactly on (changes, promotions):
|
|
# every in-window active row counts as a change; the established
|
|
# subset additionally counts as a promotion.
|
|
rows = (
|
|
[{"status": "active", "rock_status": "washable", "last_washed": IN_W}]
|
|
* (changes - promotions)
|
|
+ [{"status": "active", "rock_status": "established", "last_washed": IN_W}]
|
|
* promotions
|
|
+ [{"status": "active", "rock_status": "washable", "last_washed": OUT_W}]
|
|
* 3
|
|
)
|
|
seed_rows(db, rows)
|
|
pin("python")
|
|
want = dc.compute_drift_velocity(db, days)
|
|
finally:
|
|
db.close()
|
|
got = dcore_loader.compute_drift_velocity(changes, promotions, days)
|
|
check("fuzz", f"velocity[{i}]", want, got)
|
|
|
|
for i in range(100):
|
|
total = rng.randint(0, 20)
|
|
established = rng.randint(0, total)
|
|
rock = rng.randint(0, established)
|
|
db, _ = fresh_db(f"fuzz-dir-{i}")
|
|
try:
|
|
# established-count includes rock rows (rock_status IN clause), so
|
|
# seed established-status rows only for the non-rock share.
|
|
rows = (
|
|
[{"status": "active", "rock_status": "established"}]
|
|
* (established - rock)
|
|
+ [{"status": "active", "rock_status": "rock"}] * rock
|
|
+ [{"status": "active", "rock_status": "washable"}]
|
|
* (total - established)
|
|
+ [{"status": "retired", "rock_status": "rock"}]
|
|
)
|
|
seed_rows(db, rows)
|
|
pin("python")
|
|
want = dc.compute_drift_direction(db)
|
|
finally:
|
|
db.close()
|
|
got = dcore_loader.compute_drift_direction(total, established, rock)
|
|
check("fuzz", f"direction[{i}]", want, got)
|
|
|
|
for i in range(100):
|
|
entry = {
|
|
"id": 1,
|
|
"text": "fuzz promote candidate",
|
|
"wash_count": rng.choice([0, 5, 20, 25, 40]),
|
|
"importance": rng.choice([0.2, 0.5, 0.8, 1.0]),
|
|
"rock_status": rng.choice(["washable", "established", "rock"]),
|
|
}
|
|
entry_id = entry["id"]
|
|
db, _ = fresh_db(f"fuzz-promote-{i}")
|
|
try:
|
|
seed_rows(db, [entry])
|
|
pin("python")
|
|
want = dc.promote_entry(db, entry_id)
|
|
finally:
|
|
db.close()
|
|
got = dcore_loader.promote_entry(entry, entry_id, NOW)
|
|
check("fuzz", f"promote[{i}]", want, got)
|
|
|
|
# ── 3. LIVE: dual-mode facade (delegation wiring end to end) ────────────────
|
|
|
|
SCENARIOS = {
|
|
"promote": [
|
|
{"id": 1, "text": "proven deployment runner", "wash_count": 25,
|
|
"importance": 0.8, "rock_status": "washable"},
|
|
{"id": 2, "text": "locked anchor", "wash_count": 40,
|
|
"importance": 0.9, "rock_status": "rock"},
|
|
{"id": 3, "text": "young entry", "wash_count": 4,
|
|
"importance": 0.9, "rock_status": "washable"},
|
|
{"id": 4, "text": "proven established", "wash_count": 25,
|
|
"importance": 0.8, "rock_status": "established"},
|
|
],
|
|
"fama": [
|
|
{"id": 1, "status": "active", "rock_status": "washable", "wash_count": 5},
|
|
{"id": 2, "status": "active", "rock_status": "established", "wash_count": 0},
|
|
{"id": 3, "status": "retired", "rock_status": "rock", "wash_count": 3},
|
|
],
|
|
"velocity": [
|
|
{"status": "active", "rock_status": "washable", "last_washed": IN_W},
|
|
{"status": "active", "rock_status": "established", "last_washed": IN_W},
|
|
{"status": "active", "rock_status": "washable", "last_washed": OUT_W},
|
|
],
|
|
"direction": [
|
|
{"status": "active", "rock_status": "washable"},
|
|
{"status": "active", "rock_status": "established"},
|
|
{"status": "active", "rock_status": "rock"},
|
|
],
|
|
}
|
|
|
|
|
|
def run_facade(mode):
|
|
pin(mode)
|
|
out = {}
|
|
db, _ = fresh_db(f"live-{mode}")
|
|
try:
|
|
seed_rows(db, SCENARIOS["promote"])
|
|
out["promote"] = [dc.promote_entry(db, i) for i in (1, 2, 3, 4)]
|
|
out["promote_after"] = [
|
|
dict(r) if r else None
|
|
for r in (
|
|
db.execute(
|
|
"SELECT rock_status, promotion_eligible FROM entries WHERE id = ?",
|
|
(i,),
|
|
).fetchone()
|
|
for i in (1, 2, 3, 4)
|
|
)
|
|
]
|
|
finally:
|
|
db.close()
|
|
db, _ = fresh_db(f"live-fama-{mode}")
|
|
try:
|
|
seed_rows(db, SCENARIOS["fama"])
|
|
out["fama"] = dc.compute_fama(db, "deploy", [1, 2, 3, 99])
|
|
finally:
|
|
db.close()
|
|
for days in (7, 1, 0, 30):
|
|
db, _ = fresh_db(f"live-vel-{mode}-{days}")
|
|
try:
|
|
seed_rows(db, SCENARIOS["velocity"])
|
|
out[f"velocity-{days}"] = dc.compute_drift_velocity(db, days)
|
|
finally:
|
|
db.close()
|
|
db, _ = fresh_db(f"live-dir-{mode}")
|
|
try:
|
|
seed_rows(db, SCENARIOS["direction"])
|
|
out["direction"] = dc.compute_drift_direction(db)
|
|
out["gate"] = dc.pre_commit_gate(db, 1)
|
|
finally:
|
|
db.close()
|
|
return out
|
|
|
|
|
|
py_mode = run_facade("python")
|
|
rust_mode = run_facade("live")
|
|
for key in py_mode:
|
|
check("live", key, py_mode[key], rust_mode[key])
|
|
|
|
# The pin contract, both directions.
|
|
pin("python")
|
|
check("live", "pin.python-floor", True, dc._dc_core() is None)
|
|
pin("live")
|
|
check("live", "pin.live-kernel", True, dc._dc_core() is not None)
|
|
|
|
print(
|
|
f"parity: frozen={counts['frozen']} fuzz={counts['fuzz']} "
|
|
f"live={counts['live']} failures={len(fails)}"
|
|
)
|
|
sys.exit(1 if fails else 0)
|