From 0e3f23a88dc131162a27d2b55444fdba543c91a4 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 15:53:54 +0800 Subject: [PATCH] test: harden theorem lane -- claim-coupling mutation twin + raw-id grounding (#3198) --- proof/README.md | 16 +++++++-- proof/spec/t2-claimcoupling.smt2 | 43 ++++++++++++++++++++++++ tests/cache-theorem.test.ts | 56 ++++++++++++++++++++++++++++++-- 3 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 proof/spec/t2-claimcoupling.smt2 diff --git a/proof/README.md b/proof/README.md index 25b6d6cc90..14ca22fd72 100644 --- a/proof/README.md +++ b/proof/README.md @@ -66,6 +66,15 @@ session HIGH (number 5) dies; archive prunes; two appends allocate. residual), the solver produces the counterexample `n4 = 5` — the dead high number reissued. The residual window is thereby characterized exactly: it opens iff the cursor drops below a dead ever-issued number. +- `t2-claimcoupling.smt2` — **SAT** (#3198 hardening): if live-message ref + presentation were gated on reconcile CLAIM state instead of the persistent + content-keyed map, a byte-identical live message presents under different + refs across consecutive turns (witness: turn A takes the map-lookup path → + stored number 5; turn B the fresh-allocation path → ≥ cursor 6). Shipped code + has no such coupling — `assignRefs` reads only the persistent map + persisted + cursor ([grounding:g1]) and raw ids derive from message content + ([grounding:g4]); this twin proves the theorem WOULD break if the coupling + were introduced. ### Mutation discipline (acceptance criterion: proofs are not vacuous) @@ -88,6 +97,7 @@ so one conjunct's failure IS the pair property's failure. | `esc` E0/E1/E2/E3 (escaping algebra) | `[grounding:g3]` — homomorphism + injectivity of the inner JSON string encoder on 200 randomized BMP strings | | `mktag` determinism/injectivity | `[grounding:g2]` — `indexToRef`/`refToIndex` round-trip + injectivity over 200k numbers | | T2 allocation rules (floor, smallest-free-over-live, existing-untouched, BLOCKED-free, `cursor := n+1`) | `[grounding:g1]` — `assignRefs`/`allocateFreeRef`/`highestUsedIndex` on constructed maps, incl. the gap-below-cursor case | +| raw-id derivation (content-keyed determinism; cluster numbering pass-stable + prefix-invariant) | `[grounding:g4]` — `deriveMessageId`/`ClusterCounter` on constructed inputs | | envelope/framing bytes (`hdr`/`ftr`/`fpre`/`fmid`/`fpost`/`SEP`) | `tests/cache-proof.test.ts` (#2144) — byte-LCP unexplainedDivergences=0 across four wires and six path classes | | carrier anchor placement (merge-last-user / tail-append, optionality) | #2144 scenario matrix + call-site survey recorded in `spec/t1-*.smt2` headers | @@ -98,10 +108,10 @@ so one conjunct's failure IS the pair property's failure. node --import tsx --test tests/cache-theorem.test.ts ``` -Nine solver obligations (two context-sanity SAT guards per scenario family, +Ten solver obligations (two context-sanity SAT guards per scenario family, which catch vacuous proofs if the context ever becomes inconsistent; two -growth-stability UNSAT theorems; two T1 mutations; one T2 theorem; one T2 -mutation) plus three grounding test groups. Without a solver installed, solver +growth-stability UNSAT theorems; two T1 mutations; one T2 theorem; two T2 +mutations) plus four grounding test groups. Without a solver installed, solver subtests skip individually and grounding still runs, so the main suite stays green; CI runs the `.github/workflows/ci-theorem.yml` lane (ubuntu, pinned `z3-solver==4.9.1.0`, paths-filtered to the assembly surface) so every relevant diff --git a/proof/spec/t2-claimcoupling.smt2 b/proof/spec/t2-claimcoupling.smt2 new file mode 100644 index 0000000000..caa18c1823 --- /dev/null +++ b/proof/spec/t2-claimcoupling.smt2 @@ -0,0 +1,43 @@ +; MUTATION (T2): claim-state coupling made executable (#3198 proof hardening). +; If live-message ref PRESENTATION were gated on RECONCILE CLAIM state instead +; of the persistent content-keyed map (assignRefs: an existing byRaw entry +; always wins), a byte-identical live message could present under DIFFERENT +; refs across two consecutive turns -- breaking T1's shared-subterm congruence +; for its tag bytes even though the mid-history bytes never changed. +; Expectation SAT -- the model must exhibit equal content with unequal refs: +; turn A takes the map-lookup path (the stored, ever-issued number 5) while +; turn B takes the fresh-allocation path (a number >= cursor 6). +; +; The shipped rule has no such coupling: kernel/src/refs.ts assignRefs reads +; only the persistent map + persisted cursor, and kernel/src/wire/message-id.ts +; derives raw ids from message CONTENT (same bytes => same raw id => same map +; key => same stored ref). [grounding:g1] pins the allocator half, +; [grounding:g4] the id-derivation half. This twin proves the theorem WOULD +; break if the coupling were ever introduced (falsifiability discipline per +; ../README.md "Mutation discipline"). + +(declare-sort CMsg 0) +(declare-const H0 CMsg) +(declare-const H1 CMsg) +(assert (= H0 H1)) ; antecedent: mid-history bytes identical + +(declare-const storedNum Int) ; persistent byRaw[rawId(H0)] binding (ISSUED0 member) +(assert (= storedNum 5)) +(declare-const curNum Int) ; monotone persisted cursor, above every issued number +(assert (= curNum 6)) + +(declare-const claimA Bool) ; reconcile claim state at each turn +(declare-const claimB Bool) +(declare-const rA Int) ; presented ref at each turn +(declare-const rB Int) +; COUPLED assignment rule (the injected defect): unclaimed -> map lookup, +; claimed -> fresh allocation from the cursor. +(assert (=> (not claimA) (= rA storedNum))) +(assert (=> claimA (>= rA curNum))) +(assert (=> (not claimB) (= rB storedNum))) +(assert (=> claimB (>= rB curNum))) + +; THE DEFECT: same bytes, different presented refs across turns +(assert (and (= rA storedNum) ; turn A took the map-lookup path + (>= rB curNum) ; turn B took the fresh-allocation path + (not (= rA rB)))) ; => tag bytes differ mid-history diff --git a/tests/cache-theorem.test.ts b/tests/cache-theorem.test.ts index 875a8de322..67980203a2 100644 --- a/tests/cache-theorem.test.ts +++ b/tests/cache-theorem.test.ts @@ -7,9 +7,10 @@ * (two scenarios: merge anchor flips / stationary). * T2 ref monotonicity persistent monotone cursor => no allocation reissues * an ever-issued number (kernel id-never-reused contract). - * Mutations a stable-byte rewrite (T1) and a post-archive cursor - * reset (T2, the #421 residual) MUST be found by the - * solver as counterexamples -- the proofs are not vacuous. + * Mutations a stable-byte rewrite (T1), a post-archive cursor + * reset (T2, the #421 residual) and a claim-state-coupled + * ref presentation (T2, #3198 hardening) MUST be found by + * the solver as counterexamples -- the proofs are not vacuous. * * Trust boundary: theorems are proven over proof/spec/*.smt2 (the assembly * CONTRACT). The [grounding:*] subtests below pin each spec axiom to the real @@ -122,6 +123,17 @@ const CHECKS: Check[] = [ expect: "sat", modelVars: ["hui", "creset", "n3", "n4"], }, + { + // #3198 hardening: if live-ref presentation were gated on reconcile CLAIM + // state instead of the persistent content-keyed map, byte-equal mid-history + // messages could present under different refs across turns. Shipped code has + // no such coupling ([grounding:g1] pins the allocator half, [grounding:g4] + // the id-derivation half). + name: "t2-claimcoupling-permits-ref-drift", + parts: ["preamble.smt2", "t2.smt2", "t2-claimcoupling.smt2"], + expect: "sat", + modelVars: ["storedNum", "curNum", "rA", "rB"], + }, ]; function runCheck(kind: "cli" | "py", check: Check): { verdict: string; models: string[]; ms: number } { @@ -307,3 +319,41 @@ test("[grounding:g1] ref cursor mechanics pinned to assignRefs/allocateFreeRef", } } }); + +test("[grounding:g4] raw-id derivation pinned: determinism + prefix-extension invariance", async () => { + const { deriveMessageId, ClusterCounter } = await import("../kernel/src/wire/message-id.js"); + const opts = { toolCallId: "tc-1", toolName: "bash" }; + const id = deriveMessageId("tool", "tool-result", "output text", opts); + assert.equal(deriveMessageId("tool", "tool-result", "output text", opts), id, "determinism"); + assert.notEqual(deriveMessageId("user", "tool-result", "output text", opts), id, "role participates"); + assert.notEqual(deriveMessageId("tool", "text", "output text", opts), id, "contentType participates"); + assert.notEqual( + deriveMessageId("tool", "tool-result", "output text", { toolCallId: "tc-2", toolName: "bash" }), + id, + "toolCallId participates", + ); + assert.notEqual( + deriveMessageId("tool", "tool-result", "output text", { toolCallId: "tc-1", toolName: "cat" }), + id, + "toolName participates", + ); + assert.notEqual(deriveMessageId("tool", "tool-result", "output text!", opts), id, "text participates"); + { + const seq = ["dup", "solo", "dup", "dup"]; + const c1 = new ClusterCounter(); + const c2 = new ClusterCounter(); + const n1 = seq.map((x) => c1.next(x)); + const n2 = seq.map((x) => c2.next(x)); + assert.deepEqual(n1, n2, "two passes over the same content, same numbering"); + assert.deepEqual(n1, ["dup", "solo", "dup_1", "dup_2"]); + } + { + const prefixSeq = ["dup", "solo", "dup"]; + const c1 = new ClusterCounter(); + const first = prefixSeq.map((x) => c1.next(x)); + const c2 = new ClusterCounter(); + const extended = [...prefixSeq, "other", "dup"].map((x) => c2.next(x)); + assert.deepEqual(extended.slice(0, prefixSeq.length), first, "prefix ids stable under extension"); + assert.equal(extended[extended.length - 1], "dup_2", "later duplicate continues the count"); + } +});