The audit named three unported fns (11/14 PARTIAL): add_source, _source_store, _mse_core. All three now have Rust counterparts — audit 14/14 FULL, missing []. parity: add_source is the registry decision KERNEL (mse_add_source) — registration order is execution order, re-registering replaces IN PLACE (dict insertion order keeps position); the facade applies the kernel's order as authoritative while carrying the caller's fn. _source_store's persona-lane gate delegates through the already-frozen mse_persona_wanted (lane runs iff wanted==[]) and its merge through mse_store_project; the sqlite IO stays Python per the port law. _mse_core is the facade's delegation probe (machinery — the pin honored inside mse_loader.core); its CONTRACT is frozen (pin -> python floor) and the seam map is documented in the crate header. Freeze rig (scripts/ms_freeze_seams, the memory_consolidation pattern): fixed-path hermetic store seeded via add_entry — raw INSERTs bypass entries_fts and recall abstains on every row (the first freeze attempt's empty truths were exactly that bug); embeddings off, lexical-only. 10-row truth: pin contract, source_names, 4 registry-order rows (append/replace-in-place/solo/replace-keeps-tail), 4 _source_store E2E rows (lane merge, persona-lane dedup by entry id, typed lane skip, trivial abstain). Byte-identical: 2 generation runs + 2 --verify runs. Battery 9/9 (gap truth embedded via include_str! — the ms_add_source rows replay through the kernel; E2E rows replay in the live-parity gate). Live parity 536/536: frozen FFI rows, 522 fuzz registry ops (seed 2479) oracle dict vs kernel, dual-mode facade (RUNE_MSE_CORE=python vs live core) on _source_store/add_source/select_for_task — identical outputs. clippy --all-targets -D warnings 0. Python both pins: test_p1_03 66 passed + 2 pre-existing worktree failures (cached_recall, unrelated, pass in main checkout — verified identical on the stashed base); test_context_broker 18/18 both pins. ruff clean.
201 lines
6.9 KiB
Python
201 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Live parity: memory_selector Rust core vs Python oracle (dichotomy wave,
|
|
close-PARTIAL gap pass). Layers:
|
|
|
|
1. FROZEN — the gap truth corpus (src/rust/memory_selector/frozen_truth.json,
|
|
generated by scripts/gen_memory_selector_truth.py under the
|
|
scripts/ms_freeze_seams rig against the LIVE oracle with
|
|
RUNE_MSE_CORE=python pinned): the kernel-shaped rows (ms_add_source)
|
|
replayed through the ctypes FFI, case by case; the pin-contract row
|
|
asserted in both directions.
|
|
2. FUZZ — 200 seeded random registry ops (seed 2479): the oracle's dict
|
|
semantics vs the FFI kernel, exact compare.
|
|
3. LIVE — dual-mode facade battery: the same facade calls (the frozen
|
|
_source_store E2E rows + registry order via the real add_source + one
|
|
full select_for_task) executed with RUNE_MSE_CORE=python (oracle floor)
|
|
and with the Rust core live; results must be identical.
|
|
|
|
Exit 0 only at 100% parity on the gap-closing functions.
|
|
"""
|
|
import copy
|
|
import functools
|
|
import json
|
|
import os
|
|
import random
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
REPO = Path(__file__).resolve().parents[1]
|
|
os.environ["RUNE_MSE_CORE"] = "python" # oracle floor to start
|
|
PARITY_HOME = "/tmp/ms_parity"
|
|
os.environ["RUNE_MEMORY_DIR"] = PARITY_HOME
|
|
|
|
sys.path.insert(0, str(REPO / "src"))
|
|
|
|
from rune import memory_store as store # noqa: E402
|
|
from rune import memory_selector as ms # noqa: E402
|
|
from rune import mse_loader # noqa: E402
|
|
|
|
if not mse_loader.available():
|
|
sys.exit("FATAL: Rust core not built — cargo build --release in src/rust/memory_selector")
|
|
|
|
TRUTH = json.loads(
|
|
(REPO / "src" / "rust" / "memory_selector" / "frozen_truth.json").read_text())
|
|
|
|
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 (mse_loader.core reads env per call)."""
|
|
if mode == "python":
|
|
os.environ["RUNE_MSE_CORE"] = "python"
|
|
else:
|
|
os.environ.pop("RUNE_MSE_CORE", None)
|
|
|
|
|
|
def seed_store():
|
|
"""The rig's seed (scripts/ms_freeze_seams/sitecustomize.py), verbatim
|
|
rows, at the parity scratch — both pins see byte-identical stores."""
|
|
import shutil
|
|
|
|
if os.path.isdir(PARITY_HOME):
|
|
shutil.rmtree(PARITY_HOME)
|
|
os.makedirs(os.path.join(PARITY_HOME, "store"), exist_ok=True)
|
|
store.DB_PATH = os.path.join(PARITY_HOME, "store", "memory.db")
|
|
store.STORE_DIR = os.path.join(PARITY_HOME, "store")
|
|
store._embed = lambda texts: {} # lexical-only, deterministic
|
|
pinned = os.path.join(PARITY_HOME, "pinned-store.db")
|
|
db = store.connect(pinned)
|
|
try:
|
|
store.add_entry(db, "Kubernetes pods restart automatically on failure",
|
|
importance=0.5, source="truth")
|
|
store.add_entry(db, "Kubernetes pods restart policy for the cluster",
|
|
importance=0.5, source="truth")
|
|
store.add_entry(db, "Voice identity and speaking style stay consistent",
|
|
importance=0.5, source="truth", memory_type="self")
|
|
db.commit()
|
|
finally:
|
|
db.close()
|
|
return pinned
|
|
|
|
|
|
# ── layer 1: FROZEN (kernel rows through the FFI) ──────────────────────────
|
|
|
|
for row in TRUTH:
|
|
fn = row["fn"]
|
|
if fn == "ms_add_source":
|
|
names, name = row["args"]
|
|
check("frozen", f"ffi_add_source:{name}",
|
|
row["expected"], mse_loader.add_source(names, name))
|
|
|
|
pin_row = next(r for r in TRUTH if r["fn"] == "_mse_core")
|
|
pin("python")
|
|
check("frozen", "pin_contract:python_floor", None, ms._mse_core())
|
|
pin("rust")
|
|
check("frozen", "pin_contract:core_reachable", True, ms._mse_core() is not None)
|
|
|
|
# ── layer 2: FUZZ (registry semantics: oracle dict vs FFI kernel) ──────────
|
|
|
|
rng = random.Random(2479)
|
|
DEFAULTS = ["store", "neurons", "graphs", "abstraction", "consolidated", "persona"]
|
|
for trial in range(200):
|
|
regs: dict[str, object] = {}
|
|
names = DEFAULTS[: rng.randrange(0, 4)]
|
|
for n in names:
|
|
regs[n] = object()
|
|
for _ in range(rng.randrange(1, 5)):
|
|
name = rng.choice(DEFAULTS + ["zz", "custom", "x"])
|
|
regs[name] = object()
|
|
names = mse_loader.add_source(names, name)
|
|
check("fuzz", f"trial{trial}:{name}", list(regs), names)
|
|
if fails:
|
|
break
|
|
if fails:
|
|
break
|
|
|
|
# ── layer 3: LIVE (dual-mode facade battery) ───────────────────────────────
|
|
|
|
PINNED = seed_store()
|
|
|
|
|
|
def fresh_selector():
|
|
sel = ms.MemorySelector.__new__(ms.MemorySelector)
|
|
sel.db_path = PINNED
|
|
sel.min_score = 0.42
|
|
sel._sources = {}
|
|
return sel
|
|
|
|
|
|
def dual(label, call):
|
|
pin("python")
|
|
want = call()
|
|
pin("rust")
|
|
got = call()
|
|
check("live", label, want, got)
|
|
|
|
|
|
# The frozen _source_store E2E rows, both pins.
|
|
for row in TRUTH:
|
|
if row["fn"] != "MemorySelector._source_store":
|
|
continue
|
|
query, types, budget, min_score = row["args"]
|
|
|
|
def run(query=query, types=types, budget=budget, min_score=min_score):
|
|
sel = fresh_selector()
|
|
out = ms.MemorySelector._source_store(sel, query, copy.deepcopy(types), budget, min_score)
|
|
return out
|
|
|
|
dual(f"_source_store:{query[:24]}:{types}", run)
|
|
|
|
# Registry order through the REAL add_source, both pins (append + replace).
|
|
for name in ("zz_custom", "store", "aa_first"):
|
|
def reg(name=name):
|
|
sel = fresh_selector()
|
|
for n in DEFAULTS:
|
|
ms.MemorySelector.add_source(sel, n, lambda *a: [])
|
|
ms.MemorySelector.add_source(sel, name, lambda *a: [])
|
|
return ms.MemorySelector.source_names(sel)
|
|
|
|
dual(f"add_source:{name}", reg)
|
|
|
|
# Full select_for_task through the store source only (the other sources'
|
|
# stores are not seeded here), both pins.
|
|
def selection():
|
|
sel = fresh_selector()
|
|
sel._sources = {"store": functools.partial(ms._store_recall, db_path=PINNED)}
|
|
out = ms.MemorySelector.select_for_task(sel, "kubernetes pods restart", budget=4)
|
|
return {
|
|
"blocks": [
|
|
{a: getattr(b, a) for a in
|
|
("name", "priority", "content", "tokens", "source", "evictable")}
|
|
for b in out.blocks
|
|
],
|
|
"candidates_evaluated": out.candidates_evaluated,
|
|
"selected_count": out.selected_count,
|
|
"abstained": out.abstained,
|
|
"types_queried": out.types_queried,
|
|
}
|
|
|
|
dual("select_for_task:store_only", selection)
|
|
|
|
print(f"parity: {sum(counts.values())} checks "
|
|
f"(frozen {counts['frozen']}, fuzz {counts['fuzz']}, live {counts['live']}) "
|
|
f"— {len(fails)} failures")
|
|
sys.exit(1 if fails else 0)
|