Files
rune/scripts/gen_construction_activation_truth.py
T
Nick a15406bc36 feat(construction_activation): close PARTIAL gaps — dichotomy wave
audit 1/4 PARTIAL -> 15/15 FULL (RUST side; the 15-fn surface = the 4 oracle fns _scaffold_for/activate/_auto_structural_awareness/dispatch_from_decision + the JSON parity shims + seam machinery; nested rig closures under the _py_* exclusion law). Ported name-preserving into src/rust/construction_activation: the full mode-dispatch matrix (NOT_CONSTRUCTION/UNKNOWN/LOCAL_CHANGE/EXISTING_PROJECT_CHANGE/PROJECT_BUILD/unhandled incl. int-mode py_display reason), identity-before-repository slug derivation (unicode+astral lowercasing, run-collapsed dashes, [:48] by chars, unnamed-project fallback), store-v2 Scaffold persistence ported for the battery (segments WIN, idempotent journals, canonical-digest commit verify), the T21.5 awareness branch matrix (registry x spine x {registered,skipped,recorded,error} with save-only error injection and 120-char error-note truncation), and crash-parity error envelopes (AttributeError object-access, TypeError list() non-iterables, char-spread str list() quirk).

Kernel/facade split preserves the monkeypatch contract: the Rust kernel computes rec+plan (ca_activate FFI returns them UNPERSISTED); the Python facade owns all persistence IO (Scaffold.init/commit, registry.save, spine.save — the seams the failure-tolerance tests pin) and always runs awareness on its side. activate_json (battery surface) applies the plan + realizes awareness writes = oracle-equivalent standalone.

Frozen truth: 52-case corpus via the shared freeze_truth.py harness + scripts/coa_freeze_seams rig (cwd-is-the-rig; row-private two-level roots because the oracle derives the spine from the parent scaffold's GRANDPARENT — shared roots bleed spine state). Verified byte-identical twice. Battery gap_battery.rs replays the truth VERBATIM in order in a throwaway cwd (52/52 + slug edges). Live parity scripts/check_construction_activation_parity.py: seam-delegation 52/52, FFI composites 39/39 (13 shim-only: validation-crash/io-errors-injection/files_after rows pinned by the seam pass). cargo build/clippy -D warnings/fmt clean. Python suite both pins: native_activation 6/6 + construction_interface 37/37; full-suite failure set byte-identical across pins (140 pre-existing; one isolated load-timing flake in test_mimo_tty_contract re-verified green 4x, construction-unrelated).

Evidence: verification/ca-*.exit + ca-summary.md (logs local, *.log gitignored).
2026-10-14 17:12:01 +01:00

318 lines
16 KiB
Python

#!/usr/bin/env python3
"""Freeze construction_activation oracle truth for the Rust gap-closing
port battery (dichotomy wave #2479, shared harness scripts/freeze_truth.py).
Exercises EVERY public function and branch of
src/rune/construction_activation.py through the JSON-native row runners
(the module's own activate/dispatch/awareness/_scaffold_for, driven from
JSON so the same rows replay through the Rust battery and the FFI):
scaffold_for_json (missing+no-create, missing+create,
existing store, create writes nothing)
activate_json (NOT_CONSTRUCTION, UNKNOWN ± probes,
LOCAL_CHANGE authority set/None,
EXISTING_PROJECT_CHANGE, unhandled mode,
PROJECT_BUILD: fresh bootstrap / need.project
fallback / absent-project default "rune" on
an existing store / objective-slug derivation
(unicode+astral+long, empty→unnamed-project) /
existing entity (init=false) / segments-era
store / registry pre-registered / pre-existing
spine (relationship appended) / io_errors
registry|spine|both / >120-char message
truncation / probe-question gaps / evidence
echo + char-spread quirk / evidence
non-iterable TypeError)
dispatch_from_decision_json(mode_decision object-present AttributeError
(dict/list), construction_mode full/minimal/
missing → UNKNOWN default, LOCAL_CHANGE,
non-iterable TypeError, non-dict
construction_mode AttributeError, int mode →
unhandled-mode reason, probe_questions
char-spread)
awareness_json (fresh → registered, no spine → skipped;
registry pre-registered → skipped; pre-existing
spine → recorded; io_errors registry|spine,
>120-char truncation)
files_after_json (fruit-file parity: scaffold current.json,
segments entity, project_registry.json,
spine current.json, absent paths)
Layout law: the oracle derives the spine from the PARENT scaffold's
grandparent (`parent.current.parent.parent / "spine"`) and the registry
from the parent store dir — every row family gets its own two-level root
ca_t/<name>/s so the spine (ca_t/<name>/spine) and registry
(ca_t/<name>/s/project_registry.json) are row-private and replay is
order-independent. cwd-is-the-rig (relative paths, machine-independent
truth); the clock is always the explicit `now` kwarg (no-clock law).
Output: src/rust/construction_activation/src/frozen_ca_truth.json
(harness array schema: [{fn, args, kwargs, expected}] — embedded VERBATIM
by src/rust/construction_activation/src/gap_battery.rs).
"""
from __future__ import annotations
import sys
from pathlib import Path
# No PYTHONHASHSEED execve here (cd precedent): this oracle iterates no
# sets — every ordering is list-order or explicitly sorted — and the rig
# chdirs at startup, which would break a relative PYTHONPATH on re-exec.
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO / "scripts"))
from freeze_truth import freeze, write # noqa: E402
OUT = REPO / "src" / "rust" / "construction_activation" / "src" / "frozen_ca_truth.json"
NOW = "2026-10-14T12:34:56Z"
def md(**kw):
d = {"mode": "UNKNOWN", "evidence": [], "target_project": None,
"project_authority": None, "confidence": 0.0,
"clarification_required": False, "probe_questions": [],
"bootstrap_required": False, "override": None,
"proposed_by_model": None}
d.update(kw)
return d
def need(**kw):
d = {"need_id": "n-1", "objective": "stand up the atlas deploy pipeline",
"origin": "user", "project": "", "task_ref": "", "constraints": {},
"affected_surface": [], "evidence_refs": [], "approval_context": "",
"requested_permanence": ""}
d.update(kw)
if d.get("project") is None:
del d["project"] # key ABSENT → _NeedView default "rune"
return d
def root(name: str) -> str:
"""Row-private parent store dir; the spine derives to ca_t/<name>/spine."""
return f"ca_t/{name}/s"
def spine_path(name: str) -> str:
return f"ca_t/{name}/spine/current.json"
def ps_current(name: str, project: str) -> str:
return f"ca_t/{name}/s/projects/{project}/current.json"
def si(name: str, project: str, purpose: str = "seed purpose") -> dict:
return {"op": "scaffold_init", "store_dir": f"ca_t/{name}/s/projects/{project}",
"project": project, "purpose": purpose}
def registry_doc(project: str) -> dict:
return {"schema_version": 1, "updated_at": NOW,
"projects": {project: {
"project_id": project,
"scaffold_location": f"ca_t/seed/projects/{project}",
"repo_identity": "", "forge_repo": "", "default_branch": "master",
"structural_schema_version": 1, "runtime_generation": "",
"last_known_good": "", "registered_at": NOW}}}
def minimal_spine(entities=None, relationships=None) -> dict:
return {"schema_version": 1,
"entities": entities if entities is not None else
[{"id": "project:rune", "kind": "project"}],
"relationships": relationships if relationships is not None else []}
def act_kw(r: str, **kw):
d = {"parent_store_dir": root(r), "builder": "rune", "now": NOW,
"prestate": None, "io_errors": None}
d.update(kw)
return d
cases = []
# ── scaffold_for_json ──────────────────────────────────────────────────────
cases += [
("scaffold_for_json", ["ghost"], {"create": False, "parent_current": None, "now": NOW}),
("scaffold_for_json", ["ghost"], {"create": True, "parent_current": None, "now": NOW}),
("scaffold_for_json", ["alpha"], {
"create": False, "parent_current": "ca_t/sf/scaffold/current.json", "now": NOW,
"prestate": [{"op": "scaffold_init",
"store_dir": "ca_t/sf/scaffold/projects/alpha",
"project": "alpha", "purpose": "seed purpose"}]}),
("scaffold_for_json", ["alpha"], {
"create": True, "parent_current": "ca_t/sf/scaffold/current.json", "now": NOW}),
("scaffold_for_json", ["beta"], {"create": False, "parent_current": None, "now": NOW}),
# create computes the store object but never writes (no init op)
("files_after_json", [["ca_t/sf/scaffold/projects/ghost/current.json"]], {}),
]
# ── activate_json: mode branches ───────────────────────────────────────────
cases += [
("activate_json", [md(mode="NOT_CONSTRUCTION", evidence=["ev:intent"]),
need()], act_kw("a1")),
("activate_json", [md(mode="UNKNOWN",
probe_questions=["where does it deploy?", "which repo?"]),
need()], act_kw("a2")),
("activate_json", [md(mode="UNKNOWN"), need()], act_kw("a3")),
("activate_json", [md(mode="LOCAL_CHANGE", project_authority="spine:rune"),
need()], act_kw("a4")),
("activate_json", [md(mode="LOCAL_CHANGE"), need()], act_kw("a5")),
("activate_json", [md(mode="EXISTING_PROJECT_CHANGE"), need()], act_kw("a6")),
("activate_json", [md(mode="SOMETHING_ELSE"), need()], act_kw("a7")),
]
# ── activate_json: PROJECT_BUILD — fresh bootstrap + fruit ────────────────
cases += [
("activate_json", [md(mode="PROJECT_BUILD", target_project="gamma",
evidence=["ev:build"], probe_questions=["target platform?"]),
need(need_id="n-42")], act_kw("a8")),
("files_after_json", [[
ps_current("a8", "gamma"),
"ca_t/a8/s/projects/gamma/segments/entities/project__gamma.json",
"ca_t/a8/s/project_registry.json",
spine_path("a8"), # no pre-existing spine → skipped → never written
"ca_t/a8/s/projects/gamma/segments/observations.jsonl",
"ca_t/a8/missing.json",
]], {}),
]
# ── activate_json: PROJECT_BUILD — project identity derivation ────────────
cases += [
("activate_json", [md(mode="PROJECT_BUILD"), need(project="delta")], act_kw("a9")),
("activate_json", [md(mode="PROJECT_BUILD"),
need(objective="Build the Über-Fancy deploy pipeline 🚀 for Acme-Corp (2026) "
+ "x" * 60)], act_kw("a10")),
("activate_json", [md(mode="PROJECT_BUILD"), need(objective="!!!")], act_kw("a11")),
# project key ABSENT → _NeedView default "rune" → existing store, init=false
("activate_json", [md(mode="PROJECT_BUILD"), need(project=None)],
act_kw("a12", prestate=[si("a12", "rune")])),
]
# ── activate_json: PROJECT_BUILD — store states ───────────────────────────
cases += [
# existing entity: init=false, ext merged onto the seed
("activate_json", [md(mode="PROJECT_BUILD", target_project="epsilon"),
need()], act_kw("a13", prestate=[si("a13", "epsilon")])),
# segments-era store: segments WIN over the legacy cache file
("activate_json", [md(mode="PROJECT_BUILD", target_project="zeta"),
need()], act_kw("a14", prestate=[
si("a14", "zeta"),
{"op": "commit_ext_seed",
"store_dir": "ca_t/a14/s/projects/zeta",
"project": "zeta",
"ext": {"repository": "https://seed", "keep": 1}},
])),
("files_after_json", [[
"ca_t/a14/s/projects/zeta/segments/entities/project__zeta.json",
"ca_t/a14/s/projects/zeta/current.json",
]], {}),
# registry already knows the project: awareness registry "skipped"
("activate_json", [md(mode="PROJECT_BUILD", target_project="eta"),
need()], act_kw("a15", prestate=[
{"op": "registry_save", "store_dir": root("a15"),
"doc": registry_doc("eta")},
])),
# pre-existing spine: manages relationship APPENDED, spine "recorded"
("activate_json", [md(mode="PROJECT_BUILD", target_project="theta"),
need()], act_kw("a16", prestate=[
{"op": "spine_save", "path": spine_path("a16"),
"doc": minimal_spine()},
])),
("files_after_json", [[spine_path("a16")]], {}),
]
# ── activate_json: io_errors (T21.5 failure-tolerance surface) ────────────
LONG_MSG = "R" * 150
cases += [
("activate_json", [md(mode="PROJECT_BUILD", target_project="iota"), need()],
act_kw("a17", io_errors={"registry": "boom-reg"})),
("files_after_json", [[f"ca_t/a17/s/project_registry.json", spine_path("a17")]], {}),
("activate_json", [md(mode="PROJECT_BUILD", target_project="kappa"), need()],
act_kw("a18", io_errors={"spine": "boom-spine"})),
("files_after_json", [[f"ca_t/a18/s/project_registry.json", spine_path("a18")]], {}),
("activate_json", [md(mode="PROJECT_BUILD", target_project="lambda"), need()],
act_kw("a19", io_errors={"registry": "r-err", "spine": "s-err"})),
("activate_json", [md(mode="PROJECT_BUILD", target_project="mu"), need()],
act_kw("a20", io_errors={"spine": LONG_MSG})),
]
# ── activate_json: quirks ──────────────────────────────────────────────────
cases += [
# str evidence/props spread into CHARACTERS (list("ab") quirk)
("activate_json", [md(mode="NOT_CONSTRUCTION", evidence="ab"), need()], act_kw("a21")),
# non-iterable evidence → TypeError envelope
("activate_json", [md(mode="NOT_CONSTRUCTION", evidence=5), need()], act_kw("a22")),
# empty-string evidence → empty list
("activate_json", [md(mode="NOT_CONSTRUCTION", evidence=""), need()], act_kw("a23")),
]
# ── dispatch_from_decision_json ────────────────────────────────────────────
cases += [
("dispatch_from_decision_json",
[{"mode_decision": {"mode": "PROJECT_BUILD"}}, need()], act_kw("d1")),
("dispatch_from_decision_json",
[{"mode_decision": [1, 2]}, need()], act_kw("d2")),
("dispatch_from_decision_json",
[{"construction_mode": md(mode="PROJECT_BUILD", target_project="nu")}, need()],
act_kw("d3")),
("dispatch_from_decision_json", [{"construction_mode": {}}, need()], act_kw("d4")),
("dispatch_from_decision_json", [{}, need()], act_kw("d5")),
("dispatch_from_decision_json",
[{"construction_mode": md(mode="LOCAL_CHANGE", project_authority="auth:x")}, need()],
act_kw("d6")),
("dispatch_from_decision_json",
[{"construction_mode": md(mode="UNKNOWN", evidence=7)}, need()], act_kw("d7")),
("dispatch_from_decision_json",
[{"construction_mode": "oops"}, need()], act_kw("d8")),
("dispatch_from_decision_json",
[{"construction_mode": md(mode=123)}, need()], act_kw("d9")),
("dispatch_from_decision_json",
[{"construction_mode": md(mode="UNKNOWN", probe_questions="pq")}, need()], act_kw("d10")),
]
# ── awareness_json ─────────────────────────────────────────────────────────
cases += [
# fresh: registry registered, no spine → skipped (never written)
("awareness_json", ["xi"], {"ps_current": ps_current("w1", "xi"),
"parent_store_dir": root("w1"), "now": NOW,
"prestate": [si("w1", "xi")]}),
("files_after_json", [[f"ca_t/w1/s/project_registry.json", spine_path("w1")]], {}),
# pre-existing spine → recorded (relationship appended)
("awareness_json", ["omicron"], {
"ps_current": ps_current("w2", "omicron"),
"parent_store_dir": root("w2"), "now": NOW,
"prestate": [si("w2", "omicron"),
{"op": "spine_save", "path": spine_path("w2"),
"doc": minimal_spine()}]}),
("files_after_json", [[spine_path("w2")]], {}),
# registry pre-registered → skipped; spine still recorded
("awareness_json", ["sigma"], {
"ps_current": ps_current("w3", "sigma"),
"parent_store_dir": root("w3"), "now": NOW,
"prestate": [si("w3", "sigma"),
{"op": "registry_save", "store_dir": root("w3"),
"doc": registry_doc("sigma")},
{"op": "spine_save", "path": spine_path("w3"),
"doc": minimal_spine()}]}),
("awareness_json", ["tau"], {"ps_current": ps_current("w4", "tau"),
"parent_store_dir": root("w4"), "now": NOW,
"prestate": [si("w4", "tau")],
"io_errors": {"registry": "reg-down"}}),
("awareness_json", ["upsilon"], {"ps_current": ps_current("w5", "upsilon"),
"parent_store_dir": root("w5"), "now": NOW,
"prestate": [si("w5", "upsilon")],
"io_errors": {"spine": "spine-down"}}),
("awareness_json", ["phi"], {"ps_current": ps_current("w6", "phi"),
"parent_store_dir": root("w6"), "now": NOW,
"prestate": [si("w6", "phi")],
"io_errors": {"spine": LONG_MSG}}),
]
truth = freeze("rune.construction_activation", cases,
oracle_module="rune.construction_activation")
write(truth, OUT)
print(f"wrote {len(truth)} cases -> {OUT}")