Files
rune/scripts/close_contract_response_adapter.py
T

157 lines
7.0 KiB
Python

#!/usr/bin/env python3
"""Close the #2479 dichotomy-wave response_adapter gap contract (RUST
port). Full lifecycle walk in ONE process (shared-store clobber hazard;
segment transitions take time — do NOT split across processes). The final
step is iface.close() with contract_quality observed_evidence (the bare
`transition -> closed` is rejected by the quality gate). Safe to re-run:
pass the existing contract id to resume an interrupted walk.
Carries the already-pushed parity SHA as evidence:
feat b6eb41d4 close PARTIAL gaps — dichotomy wave (5/7 -> 7/7)
Usage: close_contract_response_adapter.py [contract_id]
"""
import json
import os
import sys
from pathlib import Path
os.environ.setdefault(
"CS_RUST_LIB",
"/mnt/workdrive/rune/src/rust/capability_scoring/target/release/libcapability_scoring.so",
)
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
from rune.construction_interface import ConstructionInterface
PARITY_SHA = "b6eb41d4"
EVIDENCE = f"parity {PARITY_SHA}"
NEED = (
"response_adapter dichotomy gap-close (RUST): name-preserving port of "
"the audit-missing functions (RuneResponse.to_dict ordered form, "
"parse_plain_response, parse_response dispatcher with the auto-detect "
"ladder reasoning_content-key→mimo / <think>-substring→qwen / "
"Thinking-Process-substring→local / else plain and the "
"unknown-provider→plain fallback, test_adapter assert-parity) into "
"src/rust/response_adapter/src/lib.rs, plus real parity fixes to the "
"existing parsers (usage reasoning_tokens/completion_tokens/"
"total_tokens + qwen reasoning_words with Python .get(k,0) "
"pass-through semantics — ANY JSON value rides; local reasoning_tokens "
"on the match branch; raw model/provider pass-through). serde_json "
"preserve_order (insertion-order quirk). FFI ra_parse_response + "
"ra_replay (cs_free_string serves the process); facade delegation in "
"src/rune/response_adapter.py + ra_loader.py with "
"RUNE_RESPONSE_ADAPTER_CORE=python pin (to_dict/test_adapter stay "
"Python on the facade — pure projection / stdout contract, W6 "
"FFI-overhead law). Freeze rig scripts/ra_freeze_seams (to_dict-"
"carrying wrappers + the CROSS-CALL law: parse_response resolves its "
"parser dict from module globals at call time and test_adapter reads "
".user_text, so the rig restores originals around the self-test; no "
"clock/HOME seams — the module is pure). 36-case frozen corpus "
"frozen_ra_gap_truth.json via the shared harness verified byte-"
"identical twice, embedded VERBATIM in gap_battery (7 tests, "
"ORDER-SENSITIVE serialized compare). Live parity 100% "
"(scripts/check_response_adapter_parity.py: FROZEN 36/36 ordered "
"compare through ra_replay, FUZZ 2000 ops seed 2479 incl. unicode/"
"floats/nulls/nested tags/whitespace runs, PINS 10 facade calls "
"identical under both pins); clippy -D warnings + fmt + cargo build "
"--release clean; contracts PASS + 7-fn facade smoke + "
"test_contracts_port.py green BOTH pins. Divergences documented in "
"verification/ra-summary.md (non-str content coercion, non-dict "
"usage, trim/strip control-char boundary). Dichotomy audit 5/7 "
"PARTIAL -> 7/7 FULL (RUST). Delivered on gitea/master: feat "
+ PARITY_SHA + "."
)
SCOPE_FILES = [
"src/rust/response_adapter/src/lib.rs",
"src/rust/response_adapter/src/gap_battery.rs",
"src/rust/response_adapter/Cargo.toml",
"src/rust/response_adapter/Cargo.lock",
"src/rust/response_adapter/frozen_ra_gap_truth.json",
"src/rune/response_adapter.py",
"src/rune/ra_loader.py",
"scripts/ra_freeze_seams/sitecustomize.py",
"scripts/gen_response_adapter_truth.py",
"scripts/check_response_adapter_parity.py",
]
iface = ConstructionInterface(builder="factory-gap-ra")
if len(sys.argv) > 1:
cid = sys.argv[1]
print("resuming contract:", cid)
else:
r = iface.submit_need(NEED, origin="builder")
print("submit_need:", json.dumps(r)[:300])
if not r.get("success"):
print("NEED FAILED — aborting")
sys.exit(1)
cid = r["ids"]["contract_id"]
print("contract_id:", cid)
def phase_of(cid):
doc = iface.scaffold.load()
e = iface.scaffold.contract(doc, cid)
return e.get("phase") if e else "MISSING"
phase = phase_of(cid)
print("current phase:", phase)
for want in ("approved", "building", "verifying"):
if phase == want or phase in ("verifying", "closed"):
break
t = iface.transition(cid, want, evidence_ref=EVIDENCE)
ok = t.get("success")
print(f"transition->{want}:", "ok" if ok else json.dumps(t)[:300])
if not ok:
sys.exit(1)
phase = want
c = iface.close(
cid,
observed_evidence=[{
"kind": "port_evidence",
"detail": "response_adapter dichotomy completion — parity "
f"{PARITY_SHA} feat(response_adapter): close PARTIAL "
"gaps — dichotomy wave; pushed gitea/master. Rust port "
"at src/rust/response_adapter closes the audit gaps "
"name-preserving (RuneResponse.to_dict ordered form "
"with serde_json preserve_order, parse_plain_response, "
"parse_response dispatcher with the four-route "
"auto-detect ladder + unknown→plain fallback, "
"test_adapter assert-parity) plus real parity fixes "
"on the existing parsers (usage token fields + qwen "
"reasoning_words with .get(k,0) pass-through, local "
"reasoning_tokens, raw model/provider ride). Freeze "
"rig scripts/ra_freeze_seams + generator "
"scripts/gen_response_adapter_truth.py: 36-case "
"frozen truth regenerates byte-identical; "
"freeze_truth --verify OK twice against the live "
"oracle. Battery gap_battery 7/7 (VERBATIM order-"
"sensitive replay + FFI roundtrip + branch proofs); "
"live parity scripts/check_response_adapter_parity.py "
"100% (FROZEN 36/36 ordered compare, FUZZ 2000 ops "
"seed 2479, PINS 10 facade calls identical under "
"both pins); clippy -D warnings + fmt + cargo build "
"--release clean; contracts PASS + 7-fn facade smoke "
"+ test_contracts_port.py green BOTH pins. Audit 5/7 "
"→ 7/7 FULL (RUST). Divergences documented in "
"verification/ra-summary.md: (1) non-string content "
"coerces to \"\" on the Rust side (struct field is "
"String); (2) non-dict usage treated as absent vs the "
"oracle's AttributeError; (3) Rust trim vs Python "
"strip boundary at \\x1c-\\x1f control chars.",
}],
scope_files=SCOPE_FILES,
)
print("close:", json.dumps(c)[:400])
final = phase_of(cid)
print("FINAL PHASE:", final)
ok = c.get("success") and final == "closed"
print("CLOSED" if ok else "CLOSE_FAILED", cid)
sys.exit(0 if ok else 1)