Port the 21 audit-missing functions name-preserving to the existing Rust crate (gap.rs: _ss_core probe, _sorted, new_doc, _redact_secrets+_is_secret_key, save, canonical_digest, snapshot, alias, spine_uri, resolve_uri, _doc_authorities, authority_health, bind_task, task_entities, impact_frontier, record_observed_impact, predicted_vs_observed, structural_events, diff, dedupe_observations), 19 doc-envelope FFI ss_* entry points, ctypes loader mirroring broker_loader.py, facade delegation with RUNE_STRUCTURAL_CORE=python pin. Fix-first: promote ordering (unknown-id silent), alias-aware entity resolution, resolve_status verbatim-status + pythonic conflicts, supersede add_relationship law, authority_failed observation, SCHEMA_VERSION 1. Frozen truth: 86 cases generated against the live oracle (pinned clock, deep-copied inputs), embedded verbatim in cfg(test) battery. Live parity 100%: 4387 checks, 0 failures (frozen 86 + fuzz 4300 + live 1). Contract BUILDER-2026-10-13T00:32:22Z-bc3aae-factory-gap-ss.
192 lines
6.6 KiB
Python
192 lines
6.6 KiB
Python
"""test_structural_store_delegation.py — dichotomy-wave gap-closing tests.
|
|
|
|
The facade must behave identically with the Rust core LIVE (RUNE_STRUCTURAL_CORE
|
|
unset, libstructural_store.so built) and with the PYTHON ORACLE pinned
|
|
(RUNE_STRUCTURAL_CORE=python). Each battery case runs through both modes and
|
|
the results must be identical (timestamps masked by the pinned clock).
|
|
"""
|
|
import copy
|
|
import json
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
sys.path.insert(0, str(ROOT / "src"))
|
|
|
|
from rune import ss_loader # noqa: E402
|
|
from rune import structural_store as ss # noqa: E402
|
|
|
|
FIXED_NOW = "2026-10-13T01:02:03Z"
|
|
|
|
needs_live = pytest.mark.skipif(
|
|
not ss_loader.available(), reason="Rust structural_store core not built"
|
|
)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def _pin_clock(monkeypatch):
|
|
import calendar
|
|
import time as _time
|
|
import types
|
|
|
|
monkeypatch.setattr(ss, "now", lambda: FIXED_NOW)
|
|
epoch = calendar.timegm(_time.strptime(FIXED_NOW, "%Y-%m-%dT%H:%M:%SZ"))
|
|
# snapshot() stamps straight off time.strftime/gmtime
|
|
monkeypatch.setattr(
|
|
ss, "time",
|
|
types.SimpleNamespace(
|
|
gmtime=lambda: _time.gmtime(epoch),
|
|
strftime=lambda fmt, t=None: _time.strftime(
|
|
fmt, t if t is not None else _time.gmtime()),
|
|
),
|
|
)
|
|
|
|
|
|
def run_both_modes(call):
|
|
"""Run `call(ss_module)` under the pinned oracle and the live core;
|
|
results must be identical."""
|
|
results = []
|
|
for pin in (True, False):
|
|
if pin:
|
|
monkey = pytest.MonkeyPatch()
|
|
monkey.setenv("RUNE_STRUCTURAL_CORE", "python")
|
|
else:
|
|
if not ss_loader.available():
|
|
pytest.skip("Rust core not built")
|
|
monkey = pytest.MonkeyPatch()
|
|
monkey.delenv("RUNE_STRUCTURAL_CORE", raising=False)
|
|
try:
|
|
results.append(call(ss))
|
|
finally:
|
|
monkey.undo()
|
|
assert json.dumps(results[0], sort_keys=True) == json.dumps(
|
|
results[1], sort_keys=True
|
|
), f"modes diverged:\n{json.dumps(results[0], sort_keys=True)}\n{json.dumps(results[1], sort_keys=True)}"
|
|
return results[0]
|
|
|
|
|
|
def make_doc():
|
|
d = ss.new_doc()
|
|
ss.add_entity(d, id="m:a", kind="module")
|
|
ss.add_entity(d, id="m:b", kind="widget_component")
|
|
ss.add_entity(d, id="c:api", kind="contract")
|
|
ss.add_relationship(d, type="implements", from_id="m:a", to_id="c:api", evidence_refs=["ev"])
|
|
ss.add_relationship(d, type="depends_on", from_id="m:b", to_id="m:a", evidence_refs=["ev"])
|
|
ss.promote(d, "m:a", "WIRED", "ev-2")
|
|
return d
|
|
|
|
|
|
class TestDelegationDualMode:
|
|
def test_new_doc_and_uris(self):
|
|
def call(m):
|
|
out = {"doc": m.new_doc(), "uri": m.spine_uri("project:alpha")}
|
|
try:
|
|
out["resolved"] = m.resolve_uri("spine://project/alpha")
|
|
except ValueError:
|
|
out["resolved"] = None
|
|
try:
|
|
m.resolve_uri("bogus://x")
|
|
out["bad_uri"] = None
|
|
except ValueError as exc:
|
|
out["bad_uri"] = str(exc)
|
|
return out
|
|
|
|
run_both_modes(call)
|
|
|
|
def test_save_snapshot_digest_bytes(self, tmp_path):
|
|
def call(m):
|
|
d = make_doc.__wrapped__() if hasattr(make_doc, "__wrapped__") else None
|
|
doc = _make_doc(m)
|
|
p = m.save(doc, tmp_path / "a" / "s.json")
|
|
snap = m.snapshot(doc, tmp_path / "snaps", "l1", tag="deadbeef123")
|
|
return {
|
|
"bytes": p.read_text(),
|
|
"snapshot_name": snap.name,
|
|
"snapshot_bytes": snap.read_text(),
|
|
"digest": m.canonical_digest(doc),
|
|
"doc_schema": doc["schema_version"],
|
|
}
|
|
|
|
run_both_modes(call)
|
|
|
|
def test_alias_error_and_success(self):
|
|
def call(m):
|
|
doc = _make_doc(m)
|
|
e = m.alias(doc, "m:b", "m:b-old")
|
|
with pytest.raises(ValueError):
|
|
m.alias(doc, "m:zz", "m:x")
|
|
with pytest.raises(ValueError):
|
|
m.alias(doc, "m:a", "m:a")
|
|
return {"entity": e, "doc": doc}
|
|
|
|
run_both_modes(call)
|
|
|
|
def test_frontier_events_diff_dedupe(self):
|
|
def call(m):
|
|
doc = _make_doc(m)
|
|
d2 = copy.deepcopy(doc)
|
|
m.dedupe_observations(d2)
|
|
return {
|
|
"frontier": m.impact_frontier(doc, ["c:api"], change_kind=None),
|
|
"frontier_impl": m.impact_frontier(doc, ["m:a"], change_kind=None),
|
|
"frontier_violation": m.impact_frontier(doc, ["c:api"], change_kind="contract_violation"),
|
|
"events": m.structural_events(doc),
|
|
"diff": m.diff(doc, copy.deepcopy(doc)),
|
|
"deduped": d2,
|
|
"task_entities": m.task_entities(doc, "T1"),
|
|
"health": m.authority_health(doc),
|
|
}
|
|
|
|
run_both_modes(call)
|
|
|
|
def test_bind_task_and_observed_impact(self):
|
|
def call(m):
|
|
doc = _make_doc(m)
|
|
m.bind_task(doc, "T1", ["m:a", "m:b"], "ev-t")
|
|
r = m.record_observed_impact(
|
|
doc, "T2", [{"entity": "m:a", "source": "test", "detail": "x" * 400}]
|
|
)
|
|
return {"doc": doc, "observed": r}
|
|
|
|
run_both_modes(call)
|
|
|
|
def test_sorted_redact_and_authorities(self):
|
|
def call(m):
|
|
v = {"b": 2, "a": [{"z": 1, "y": 2}, "é", True, None], "token": "s3cret"}
|
|
doc = _make_doc(m)
|
|
m._doc_authorities(doc)["p1"] = {"generation": 1, "observed": []}
|
|
return {
|
|
"sorted": m._sorted(v),
|
|
"redacted": m._redact_secrets(v),
|
|
"doc": doc,
|
|
}
|
|
|
|
run_both_modes(call)
|
|
|
|
def test_promote_contract_unchanged(self):
|
|
def call(m):
|
|
doc = _make_doc(m)
|
|
e = m.promote(doc, "m:a", "REACHABLE", "ev-3")
|
|
with pytest.raises(ValueError):
|
|
m.promote(doc, "m:a", "WIRED", "")
|
|
with pytest.raises(ValueError):
|
|
m.promote(doc, "m:a", "BOGUS", "ev")
|
|
assert m.promote(doc, "m:zz", "PRESENT", "ev") is None
|
|
return {"entity": e, "doc": doc}
|
|
|
|
run_both_modes(call)
|
|
|
|
|
|
def _make_doc(m):
|
|
d = m.new_doc()
|
|
m.add_entity(d, id="m:a", kind="module")
|
|
m.add_entity(d, id="m:b", kind="widget_component")
|
|
m.add_entity(d, id="c:api", kind="contract")
|
|
m.add_relationship(d, type="implements", from_id="m:a", to_id="c:api", evidence_refs=["ev"])
|
|
m.add_relationship(d, type="depends_on", from_id="m:b", to_id="m:a", evidence_refs=["ev"])
|
|
m.promote(d, "m:a", "WIRED", "ev-2")
|
|
return d
|