From 6cbc93e6800937412eeba1804ec094fdc0778efc Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 5 Oct 2026 15:50:53 +0800 Subject: [PATCH 1/3] feat: machine-checked cache-stability theorems via Z3 (route 4, #2149) Add the solver lane of #2149 as a model-specification (route 4) proof: - proof/spec/*.smt2: byte-level contract of outbound-body assembly (String theory over bytes; impure points modeled at the interface as free functions/free inputs per owner stance) plus nine obligations: T1 growth stability (f(H+[m]) vs f(H) share a prefix of depth D past the merge-anchor's stable content; two scenarios: anchor flip and stationary) proven UNSAT in refutation form, T2 ref non-reuse under the persisted monotone cursor (UNSAT), and mutation twins that MUST be found (stable-region byte rewrite below D; post-archive cursor reset reissuing the dead high number 5 = the documented #421 residual, model hui=3 creset=4 n3=4 n4=5). - tests/cache-theorem.test.ts: z3 driver (PATH CLI or python module; solver subtests skip individually when absent so the main suite stays green) with per-check timing, plus grounding subtests pinning each spec axiom to the real code it abstracts (esc algebra vs JSON string encoder, mktag vs indexToRef, allocation rules vs assignRefs/ allocateFreeRef). - .github/workflows/ci-theorem.yml: dedicated ubuntu-only theorem lane, pinned z3-solver==4.9.1.0, paths-filtered to the assembly surface. Pure test/proof surface: zero production code touched, kernel/** untouched, no config surface added, no behavior changes. --- .github/workflows/ci-theorem.yml | 64 +++++++ proof/README.md | 137 +++++++++++++++ proof/spec/esc-axioms.smt2 | 10 ++ proof/spec/preamble.smt2 | 59 +++++++ proof/spec/t1-s1-mutation.smt2 | 18 ++ proof/spec/t1-s1-theorem.smt2 | 9 + proof/spec/t1-s1.smt2 | 118 +++++++++++++ proof/spec/t1-s2-mutation.smt2 | 23 +++ proof/spec/t1-s2-theorem.smt2 | 5 + proof/spec/t1-s2.smt2 | 103 +++++++++++ proof/spec/t2-host.smt2 | 35 ++++ proof/spec/t2-mutation.smt2 | 32 ++++ proof/spec/t2.smt2 | 52 ++++++ tests/cache-theorem.test.ts | 292 +++++++++++++++++++++++++++++++ 14 files changed, 957 insertions(+) create mode 100644 .github/workflows/ci-theorem.yml create mode 100644 proof/README.md create mode 100644 proof/spec/esc-axioms.smt2 create mode 100644 proof/spec/preamble.smt2 create mode 100644 proof/spec/t1-s1-mutation.smt2 create mode 100644 proof/spec/t1-s1-theorem.smt2 create mode 100644 proof/spec/t1-s1.smt2 create mode 100644 proof/spec/t1-s2-mutation.smt2 create mode 100644 proof/spec/t1-s2-theorem.smt2 create mode 100644 proof/spec/t1-s2.smt2 create mode 100644 proof/spec/t2-host.smt2 create mode 100644 proof/spec/t2-mutation.smt2 create mode 100644 proof/spec/t2.smt2 create mode 100644 tests/cache-theorem.test.ts diff --git a/.github/workflows/ci-theorem.yml b/.github/workflows/ci-theorem.yml new file mode 100644 index 0000000000..c5c933c26d --- /dev/null +++ b/.github/workflows/ci-theorem.yml @@ -0,0 +1,64 @@ +name: Theorem Lane + +# Machine-checked cache-stability theorems (#2149). Runs Z3 over proof/spec +# plus the grounding tests in tests/cache-theorem.test.ts. Ubuntu-only: the +# pinned z3-solver wheel targets linux x86_64; windows legs of ci.yml run the +# same test file with solver subtests skipped by design. +on: + pull_request: + branches: [master] + paths: + - "proof/**" + - "tests/cache-theorem.test.ts" + - "src/loop/**" + - "src/wire-body.ts" + - "src/system-anchor.ts" + - "src/chain-checkpoint.ts" + - "src/image-note.ts" + - "src/server.ts" + - "src/fake-completion.ts" + - "src/degenerate-retry.ts" + - "kernel/src/rebuild.ts" + - "kernel/src/render-refs.ts" + - "kernel/src/refs.ts" + - "kernel/src/segment.ts" + - "kernel/src/block-map.ts" + - "kernel/src/ccr.ts" + - ".github/workflows/ci-theorem.yml" + push: + branches: [master] + paths: + - "proof/**" + - "tests/cache-theorem.test.ts" + - "src/loop/**" + - "src/wire-body.ts" + - "src/system-anchor.ts" + - "src/chain-checkpoint.ts" + - "src/image-note.ts" + - "src/server.ts" + - "src/fake-completion.ts" + - "src/degenerate-retry.ts" + - "kernel/src/rebuild.ts" + - "kernel/src/render-refs.ts" + - "kernel/src/refs.ts" + - "kernel/src/segment.ts" + - "kernel/src/block-map.ts" + - "kernel/src/ccr.ts" + - ".github/workflows/ci-theorem.yml" + +jobs: + theorem: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + # pin the solver so local iteration and CI verdicts match + - name: Install pinned z3 + run: python3 -m pip install --quiet "z3-solver==4.9.1.0" + - name: Install dependencies + run: npm install --ignore-scripts --no-fund --no-audit + - name: Run theorem lane + run: node --import tsx --test tests/cache-theorem.test.ts diff --git a/proof/README.md b/proof/README.md new file mode 100644 index 0000000000..25b6d6cc90 --- /dev/null +++ b/proof/README.md @@ -0,0 +1,137 @@ +# Cache-Stability Theorems (#2149) + +Machine-checked statements about the proxy's outbound-body assembly, proven in +Z3 over the byte-level model in `spec/*.smt2`. This is the **route-4 (model +specification)** lane of #2149: the theorems are proven over the *contract*, +and two mechanisms keep the contract honest against the code: + +1. **Grounding tests** (`tests/cache-theorem.test.ts`, `[grounding:*]`): each + spec axiom is pinned to the real code it abstracts (below). +2. **The #2144 billing suite** (`tests/cache-proof.test.ts`): real proxy + real + kernel vs an ideal prefix-cache judge, byte-LCP assertions across six path + classes — the code↔spec sync evidence at whole-body granularity. + +## Trust boundary (read this first) + +A theorem here says: **any assembly function whose components satisfy the spec +axioms has the stated byte property.** It does NOT by itself say "the shipped +TS code satisfies the axioms" — that direction is covered by (1)+(2), not by +the solver. This is the honest strength of route 4: cheapest cost, guarantee +bounded to the spec surface. Routes 1–3 (compiling the real pipeline into a +symex front-end) would remove the grounding gap entirely and are tracked as a +separate issue after this lane lands. + +## Theorems + +### T1 — growth stability (`t1-s1*`, `t1-s2*`) + +For history `H`, message `m`, pipeline `f`: `f(H+[m])` and `f(H)` share a +common byte prefix of depth at least `D`, where `D` = envelope head + stable +content of the merge-anchor element. The statement is encoded with an explicit +shared-prefix term `P` (`blen(P) = D`) as `substr(body, 0, blen(P)) = P` for +both bodies -- a decidable strengthening of the quantified "exists p >= D" +form, which Z3's string solver returns `unknown` on. Everything after `D` may differ only at +the existentially quantified declared carrier positions: + +- **MERGE family** — nudge/hint text appended to the last-user turn's content + tail (`src/wire-body.ts appendTrailingUserText`; degenerate-retry; + fake-completion). Two scenarios are instantiated: merge anchor *flips* to the + new message (S1) and stays stationary (S2). +- **TAIL family** — trailing user element (imgNote conditional / chain + checkpoint stamp / fresh-turn fallback): optional presence + free content per + run; the checkpoint stamp carries the clock (`stampOutbound(nowMs)`) — the + x-at-interface impurity, modeled as a free function of a free integer + instantiated differently per run. + +All other bytes (envelope, old elements, ref tags, separators, framing) are +shared subterms in both bodies ⇒ equal by congruence. Impure points (network, +session store, clock) are modeled as free functions/free inputs at the +interface per the owner stance — no pure extraction was needed; the surveyed +rebuild core has zero impure calls inside `kernel/src/rebuild.ts`. + +### T2 — ref monotonicity / id-never-reused (`t2*`) + +Bounded 4-step session over the real allocator rules +(`kernel/src/refs.ts assignRefs/allocateFreeRef`; archive op = #421 +`applyCompactionArchive`): initial live map `{a→5, b→2, c→3}`, cursor 6; the +session HIGH (number 5) dies; archive prunes; two appends allocate. + +- `t2-host.smt2` — **UNSAT**: with the persisted monotone cursor (grounded in + `src/session.ts`: `state.nextIndex` carried verbatim; `cursor := n+1`; + retire/archive never move it), no allocation reissues an ever-issued number, + even though the high died. This is the machine-checked form of the host duty + behind the kernel id-never-reused contract. +- `t2-mutation.smt2` — **SAT**: if the cursor is instead RECOMPUTED from the + pruned map after the archive (`highestUsedIndex+1`, the documented #421 + 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. + +### Mutation discipline (acceptance criterion: proofs are not vacuous) + +Each theorem has a mutation twin that injects a stable-region defect (a +same-length content rewrite strictly below `D` in T1 -- which byte changed is +existentially hidden at this abstraction level; the concrete byte surface is +#2144's job -- and the post-archive cursor reset in T2). The solver MUST find +each as a counterexample within the time budget; the T2 model exhibits exactly +`hui=3 creset=4 n3=4 n4=5` (the dead high reissued). A proof that cannot be +falsified by its own known-bad mutation is treated as broken, not merged. + +T1 mutations are stated single-side (`substr(corruptedBody, 0, blen(P)) != P`): +sound because the uncorrupted side satisfies the property by its theorem file, +so one conjunct's failure IS the pair property's failure. + +## Spec → code grounding map + +| Spec axiom | Grounded by | +|---|---| +| `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 | +| 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 | + +## Running + +```bash +# needs z3: CLI on PATH, or python3 with the z3 module (pip install z3-solver==4.9.1.0) +node --import tsx --test tests/cache-theorem.test.ts +``` + +Nine 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 +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 +PR gets the verdicts. All nine resolve in well under a second total. + +Solver compatibility notes (learned the hard way): + +- Bytes are modeled as the SMT-LIB **String** theory (`str.++`/`str.len`/ + `str.substr`), not sequences of bitvectors -- this parser rejects + `(_ Seq (_ BitVec 8))` sorts outright. +- No obligation context carries quantifiers or array terms. A quantified axiom + over a String-typed function (the old `mktag` injectivity) poisons SAT-mode + model building in this build (every mutation check went `unknown` with it + present, instant without); the `esc` algebra lives in + `spec/esc-axioms.smt2`, outside every obligation, since the proofs are + congruence arguments that never instantiate it. +- Verified on both Z3 4.9.1.0 (native, the CI pin) and 4.16.0.0 (WASM, + local iteration): identical verdicts and models. + +## Known limitations (honest scope) + +- Bounded instantiations: fixed role shapes (S1/S2), 4-step T2 session. The + general schema is the same equations at arbitrary N; extending N is mechanical + but not yet parameterized in SMT2. +- `esc` length bounds use worst-case ×6 (JSON `\uXXXX`); the real encoder is + tighter — the bound only constrains, never enables, a wrong conclusion here. +- Frame/envelope functions stay uninterpreted: their *placement* (content right + after `fpre`, separator discipline, carrier slots) is the load-bearing part + and IS asserted; their exact byte content is delegated to the #2144 suite. +- Stronger decompositional form of T1 (explicit `bodyA = P ++ CA ++ M1` split + witnesses with per-carrier offsets) is future work; the current explicit-P + prefix form already matches the issue's statement (prefix equality outside + the existentially quantified carrier positions). diff --git a/proof/spec/esc-axioms.smt2 b/proof/spec/esc-axioms.smt2 new file mode 100644 index 0000000000..acd1d0f83e --- /dev/null +++ b/proof/spec/esc-axioms.smt2 @@ -0,0 +1,10 @@ +; esc axioms E0-E3 (see preamble header for meanings). Kept separate from the +; theorem runs: they are quantified over String and are NOT needed for any +; obligation (proofs are congruence arguments; carrier merges are spelled out +; esc(c) ++ esc(C) BY CONSTRUCTION in the element definitions). This file +; exists so their mutual consistency stays machine-checked [esc-axioms- +; consistency] and so the grounding map in ../README.md has a concrete target. +(assert (= (esc bempty) bempty)) +(assert (forall ((a BSeq) (b BSeq)) (= (esc (bconcat a b)) (bconcat (esc a) (esc b))))) +(assert (forall ((a BSeq) (b BSeq)) (=> (= (esc a) (esc b)) (= a b)))) +(assert (forall ((s BSeq)) (and (<= (blen s) (blen (esc s))) (<= (blen (esc s)) (* 6 (blen s)))))) diff --git a/proof/spec/preamble.smt2 b/proof/spec/preamble.smt2 new file mode 100644 index 0000000000..f12795e98c --- /dev/null +++ b/proof/spec/preamble.smt2 @@ -0,0 +1,59 @@ +; billion-context cache-stability theorems (#2149) -- shared preamble. +; Byte-level model of the outbound-body assembly contract. Conservative SMT2 +; (uninterpreted functions + sequences + arrays + linear integer arithmetic); +; targets Z3 4.8+. Trust boundary and grounding map: see ../README.md. + +(set-option :produce-models true) +(set-option :timeout 60000) + +(define-sort BSeq () String) + +(define-fun bempty () BSeq "") +(define-fun bconcat ((a BSeq) (b BSeq)) BSeq (str.++ a b)) +(define-fun blen ((s BSeq)) Int (str.len s)) +(define-fun bunit ((x Int)) BSeq (str.from_code x)) +(define-fun bextract ((s BSeq) (i Int) (j Int)) BSeq (str.substr s i j)) + +; JSON array-element separator between serialized messages (fixed per wire). +(declare-const SEP BSeq) +(assert (= SEP (bunit 44))) + +; esc: the char-wise string encoder inside the JSON document (V8 string +; escaping as used by JSON.stringify on each content/tools field). +; E0 empty maps to empty +; E1 homomorphism: esc(a ++ b) = esc(a) ++ esc(b) (per-char processing) +; E2 injectivity (escaping is deterministic and reversible) +; E3 length bounds: |s| <= |esc(s)| <= 6|s| (\uXXXX worst case) +; GROUNDING [g3] in tests/cache-theorem.test.ts pins E1/E2 against the real +; serializer on randomized inputs. +(declare-fun esc (BSeq) BSeq) +; NOTE: the esc AXIOMS (E0-E3) live in esc-axioms.smt2, deliberately +; OUTSIDE the theorem runs: the proofs are congruence arguments over shared +; subterms and need no quantified axioms, and keeping them out keeps every +; obligation quantifier-free and fast. See ../README.md. + +; Per-message element frame: elem = fpre(r) ++ contentBytes ++ fmid(r) +; ++ toolsBytes ++ fpost(r, tagBytes). +; Content bytes sit immediately after fpre: this placement is what makes the +; guaranteed-stable depth D land right after the stable content of the +; merge-anchor element. Pure deterministic framing -- no axioms needed. +(declare-fun fpre (Int) BSeq) +(declare-fun fmid (Int) BSeq) +(declare-fun fpost (Int BSeq) BSeq) + +; Envelope head/tail outside the message array (model field, stream flag, +; tool surface, ...): pure functions of (modelBytes, cfgBytes). Same inputs +; in both runs => same bytes by congruence. GROUNDING: #2144 billing suite +; pins these empirically (byte-LCP unexplainedDivergences=0 across wires). +(declare-fun hdr (BSeq BSeq) BSeq) +(declare-fun ftr (BSeq) BSeq) + +; ACP tag bytes: deterministic function of the ref number. +; GROUNDING [g2] pins determinism/injectivity to indexToRef (kernel/src/refs.ts). +(declare-fun mktag (Int) BSeq) +; NOTE: no injectivity axiom here. The theorems never need it (tags appear as +; shared subterms on both sides of every equality), and a quantified axiom over +; a String-typed function poisons SAT-mode model building in this solver build +; (every mutation check went unknown with it present, instant without). +; Determinism/injectivity of the real tag renderer is grounded separately +; ([grounding:g2] in tests/cache-theorem.test.ts vs indexToRef). \ No newline at end of file diff --git a/proof/spec/t1-s1-mutation.smt2 b/proof/spec/t1-s1-mutation.smt2 new file mode 100644 index 0000000000..dff3816f09 --- /dev/null +++ b/proof/spec/t1-s1-mutation.smt2 @@ -0,0 +1,18 @@ +; MUTATION (S1): a regression that rewrites element 0's STABLE content in run +; B -- same length, different bytes (a stable-region corruption at the spec +; level; WHICH byte changed is existentially hidden at this abstraction, the +; concrete byte surface is pinned by the #2144 suite). Expectation SAT: the +; corrupted run fails the guaranteed-prefix property -- the proof is falsifiable. +(declare-const c0bad BSeq) +(assert (and (not (= c0bad (esc c0))) (= (blen c0bad) (blen (esc c0))))) +(define-fun elem0Bm () BSeq + (bconcat (bconcat (bconcat (fpre r0) c0bad) (fmid r0)) + (bconcat (esc t0) (fpost r0 (mktag rho0))))) +(define-fun bodyBm () BSeq + (bconcat (bconcat (bconcat (hdr MB CFG) elem0Bm) SEP) + (bconcat (bconcat (bconcat (bconcat elem1 SEP) elem2) SEP) + (bconcat (bconcat elemM (ite hasTailB (bconcat SEP tailB) bempty)) + (ftr CFG))))) +; single-side form: the uncorrupted side satisfies the property by the theorem +; file, so this conjunct's failure IS the pair property's failure. +(assert (not (= (bextract bodyBm 0 (blen P)) P))) diff --git a/proof/spec/t1-s1-theorem.smt2 b/proof/spec/t1-s1-theorem.smt2 new file mode 100644 index 0000000000..63c17b51e8 --- /dev/null +++ b/proof/spec/t1-s1-theorem.smt2 @@ -0,0 +1,9 @@ +; THEOREM 1 (S1): growth stability -- negated form; expectation UNSAT. +; P (defined in t1-s1.smt2) is the explicit common-prefix term: envelope head + +; stable content of the merge-anchor element; blen(P) = D. The statement says +; the first blen(P) bytes of BOTH bodies equal P -- the decidable form of +; "common byte prefix of depth at least D": Z3's string solver normalizes +; substr at boundaries aligned with the concat structure, whereas quantifying +; over a symbolic p is beyond its decision procedure (returns unknown). +(assert (not (and (= (bextract bodyA 0 (blen P)) P) + (= (bextract bodyB 0 (blen P)) P)))) diff --git a/proof/spec/t1-s1.smt2 b/proof/spec/t1-s1.smt2 new file mode 100644 index 0000000000..6d0154024c --- /dev/null +++ b/proof/spec/t1-s1.smt2 @@ -0,0 +1,118 @@ +; THEOREM 1 -- scenario S1 (growth stability): history H = [user, assistant, +; assistant], new message m = user. The merge anchor FLIPS: lastUser(H) is +; element 0, lastUser(H+[m]) is the new element. +; Run A = f(H): nudge/hint merges into element 0's content tail. +; Run B = f(H+[m]): merge lands in the NEW element; element 0 stays clean. +; Carrier families (existentially quantified: free per run): +; MERGE -- nudge/hint text appended to the last-user turn's content +; (src/wire-body.ts appendTrailingUserText; degenerate-retry; +; fake-completion) +; TAIL -- trailing user element (imgNote conditional / chain-checkpoint +; stamp / fresh-turn fallback): optional, present or absent per run + +(declare-const R_USER Int) +(declare-const R_ASSIST Int) +(assert (distinct R_USER R_ASSIST)) +(declare-const r0 Int) +(declare-const r1 Int) +(declare-const r2 Int) +(declare-const rm Int) +(assert (= r0 R_USER)) +(assert (= r1 R_ASSIST)) +(assert (= r2 R_ASSIST)) +(assert (= rm R_USER)) + +; stable raw message data -- identical in both runs (same session state) +(declare-const c0 BSeq) +(declare-const c1 BSeq) +(declare-const c2 BSeq) +(declare-const t0 BSeq) +(declare-const t1 BSeq) +(declare-const t2 BSeq) +(declare-const cm BSeq) +(declare-const tm BSeq) + +; ref numbers: old ones distinct; fresh number for m exceeds every old one +; (grounded in kernel/src/refs.ts: existing bindings untouched, allocateFreeRef +; scans upward from the persisted cursor) +(declare-const rho0 Int) +(declare-const rho1 Int) +(declare-const rho2 Int) +(declare-const rhom Int) +(assert (and (>= rho0 1) (>= rho1 1) (>= rho2 1) (>= rhom 1))) +(assert (distinct rho0 rho1 rho2 rhom)) +(assert (and (> rhom rho0) (> rhom rho1) (> rhom rho2))) + +; model + config bytes (shared across runs) +(declare-const MB BSeq) +(declare-const CFG BSeq) + +; impure points at the interface (owner stance: x at the interface -- free +; functions / free inputs instantiated per run, no pure extraction): +; wall clock advances between the two runs +(declare-const nowA Int) +(declare-const nowB Int) +; checkpoint-tag render: free function of the clock (stampOutbound nowMs arg, +; src/chain-checkpoint.ts) +(declare-fun ckpt (Int) BSeq) + +; carrier contents (free per run) +(declare-const CMergeA BSeq) +(declare-const CMergeB BSeq) +(declare-const CTailA BSeq) +(declare-const CTailB BSeq) +(declare-const hasTailA Bool) +(declare-const hasTailB Bool) + +; --- elements. Shared subterms are referenced by BOTH bodies: the proof +; hinges on congruence over these identical subterms. --- +(define-fun elem1 () BSeq + (bconcat (bconcat (bconcat (fpre r1) (esc c1)) (fmid r1)) + (bconcat (esc t1) (fpost r1 (mktag rho1))))) +(define-fun elem2 () BSeq + (bconcat (bconcat (bconcat (fpre r2) (esc c2)) (fmid r2)) + (bconcat (esc t2) (fpost r2 (mktag rho2))))) + +; element 0: clean (run B) vs carrier-merged (run A). By E1, +; esc(c0 ++ C) = esc(c0) ++ esc(C): carrier bytes sit AFTER the stable ones. +(define-fun elem0B () BSeq + (bconcat (bconcat (bconcat (fpre r0) (esc c0)) (fmid r0)) + (bconcat (esc t0) (fpost r0 (mktag rho0))))) +(define-fun elem0A () BSeq + (bconcat (bconcat (bconcat (bconcat (fpre r0) (esc c0)) (esc CMergeA)) + (fmid r0)) + (bconcat (esc t0) (fpost r0 (mktag rho0))))) + +; the new element (run B only); the merge lands in its content tail +(define-fun elemM () BSeq + (bconcat (bconcat (bconcat (bconcat (fpre rm) (esc cm)) (esc CMergeB)) + (fmid rm)) + (bconcat (esc tm) (fpost rm (mktag rhom))))) + +; trailing elements (optional per run) +(define-fun tailA () BSeq + (bconcat (bconcat (bconcat (fpre R_USER) (esc CTailA)) (fmid R_USER)) + (fpost R_USER (ckpt nowA)))) +(define-fun tailB () BSeq + (bconcat (bconcat (bconcat (fpre R_USER) (esc CTailB)) (fmid R_USER)) + (fpost R_USER (ckpt nowB)))) + +; --- bodies --- +(define-fun bodyA () BSeq + (bconcat (bconcat (bconcat (hdr MB CFG) elem0A) SEP) + (bconcat (bconcat (bconcat elem1 SEP) elem2) + (bconcat (ite hasTailA (bconcat SEP tailA) bempty) + (ftr CFG))))) +(define-fun bodyB () BSeq + (bconcat (bconcat (bconcat (hdr MB CFG) elem0B) SEP) + (bconcat (bconcat (bconcat (bconcat elem1 SEP) elem2) SEP) + (bconcat (bconcat elemM (ite hasTailB (bconcat SEP tailB) bempty)) + (ftr CFG))))) + +; guaranteed-stable depth: envelope head + stable content of the merge anchor +(define-fun D () Int + (+ (blen (hdr MB CFG)) (blen (fpre r0)) (blen (esc c0)))) + +; explicit common-prefix term whose length is exactly D (used by the theorem / +; mutation files) +(define-fun P () BSeq (bconcat (bconcat (hdr MB CFG) (fpre r0)) (esc c0))) diff --git a/proof/spec/t1-s2-mutation.smt2 b/proof/spec/t1-s2-mutation.smt2 new file mode 100644 index 0000000000..892e737557 --- /dev/null +++ b/proof/spec/t1-s2-mutation.smt2 @@ -0,0 +1,23 @@ +; MUTATION (S2): a regression that rewrites the MIDDLE element's STABLE +; content in BOTH runs -- same length, different bytes, strictly before D. +; Expectation SAT with the corruption visible in the model (c1bad != esc(c1)). +(declare-const c1bad BSeq) +(assert (and (not (= c1bad (esc c1))) (= (blen c1bad) (blen (esc c1))))) +(define-fun elem1bad () BSeq + (bconcat (bconcat (bconcat (fpre r1) c1bad) (fmid r1)) + (bconcat (esc t1) (fpost r1 (mktag rho1))))) +(define-fun bodyAm () BSeq + (bconcat (bconcat (bconcat (bconcat (hdr MB CFG) elem0) SEP) + (bconcat (bconcat elem1bad SEP) elem2A)) + (bconcat (ite hasTailA (bconcat SEP tailA) bempty) + (ftr CFG)))) +(define-fun bodyBm () BSeq + (bconcat (bconcat (bconcat (bconcat (hdr MB CFG) elem0) SEP) + (bconcat (bconcat (bconcat (bconcat elem1bad SEP) elem2B) SEP) + elemM)) + (bconcat (ite hasTailB (bconcat SEP tailB) bempty) + (ftr CFG)))) +; the corrupted run fails the guaranteed-prefix property -- expected SAT. +; (Sound single-side form: the uncorrupted side satisfies the property by the +; theorem file, so this conjunct's failure IS the pair property's failure.) +(assert (not (= (bextract bodyAm 0 (blen P)) P))) diff --git a/proof/spec/t1-s2-theorem.smt2 b/proof/spec/t1-s2-theorem.smt2 new file mode 100644 index 0000000000..f24db73dd1 --- /dev/null +++ b/proof/spec/t1-s2-theorem.smt2 @@ -0,0 +1,5 @@ +; THEOREM 1 (S2): growth stability, stationary merge anchor -- negated form; +; expectation UNSAT. Same explicit-prefix statement as S1: the first blen(P) +; bytes of both bodies equal the shared term P (see t1-s2.smt2). +(assert (not (and (= (bextract bodyA 0 (blen P)) P) + (= (bextract bodyB 0 (blen P)) P)))) diff --git a/proof/spec/t1-s2.smt2 b/proof/spec/t1-s2.smt2 new file mode 100644 index 0000000000..8af3dce779 --- /dev/null +++ b/proof/spec/t1-s2.smt2 @@ -0,0 +1,103 @@ +; THEOREM 1 -- scenario S2 (growth stability): history H = [user, assistant, +; user], new message m = assistant. The merge anchor is STATIONARY at element +; 2 in both runs (m does not become the last user turn): both runs merge their +; carrier into element 2's content tail, so the two bodies differ only in the +; carrier bytes themselves and in everything after the new element. + +(declare-const R_USER Int) +(declare-const R_ASSIST Int) +(assert (distinct R_USER R_ASSIST)) +(declare-const r0 Int) +(declare-const r1 Int) +(declare-const r2 Int) +(declare-const rm Int) +(assert (= r0 R_USER)) +(assert (= r1 R_ASSIST)) +(assert (= r2 R_USER)) +(assert (= rm R_ASSIST)) + +(declare-const c0 BSeq) +(declare-const c1 BSeq) +(declare-const c2 BSeq) +(declare-const t0 BSeq) +(declare-const t1 BSeq) +(declare-const t2 BSeq) +(declare-const cm BSeq) +(declare-const tm BSeq) + +(declare-const rho0 Int) +(declare-const rho1 Int) +(declare-const rho2 Int) +(declare-const rhom Int) +(assert (and (>= rho0 1) (>= rho1 1) (>= rho2 1) (>= rhom 1))) +(assert (distinct rho0 rho1 rho2 rhom)) +(assert (and (> rhom rho0) (> rhom rho1) (> rhom rho2))) + +(declare-const MB BSeq) +(declare-const CFG BSeq) + +(declare-const nowA Int) +(declare-const nowB Int) +(declare-fun ckpt (Int) BSeq) + +(declare-const CMergeA BSeq) +(declare-const CMergeB BSeq) +(declare-const CTailA BSeq) +(declare-const CTailB BSeq) +(declare-const hasTailA Bool) +(declare-const hasTailB Bool) + +; shared elements (identical subterms in both bodies) +(define-fun elem0 () BSeq + (bconcat (bconcat (bconcat (fpre r0) (esc c0)) (fmid r0)) + (bconcat (esc t0) (fpost r0 (mktag rho0))))) +(define-fun elem1 () BSeq + (bconcat (bconcat (bconcat (fpre r1) (esc c1)) (fmid r1)) + (bconcat (esc t1) (fpost r1 (mktag rho1))))) + +; merge-anchor element 2: stable content + per-run carrier tail +(define-fun elem2A () BSeq + (bconcat (bconcat (bconcat (bconcat (fpre r2) (esc c2)) (esc CMergeA)) + (fmid r2)) + (bconcat (esc t2) (fpost r2 (mktag rho2))))) +(define-fun elem2B () BSeq + (bconcat (bconcat (bconcat (bconcat (fpre r2) (esc c2)) (esc CMergeB)) + (fmid r2)) + (bconcat (esc t2) (fpost r2 (mktag rho2))))) + +; the new element (run B only); no merge lands here (m is not a user turn) +(define-fun elemM () BSeq + (bconcat (bconcat (bconcat (fpre rm) (esc cm)) (fmid rm)) + (bconcat (esc tm) (fpost rm (mktag rhom))))) + +(define-fun tailA () BSeq + (bconcat (bconcat (bconcat (fpre R_USER) (esc CTailA)) (fmid R_USER)) + (fpost R_USER (ckpt nowA)))) +(define-fun tailB () BSeq + (bconcat (bconcat (bconcat (fpre R_USER) (esc CTailB)) (fmid R_USER)) + (fpost R_USER (ckpt nowB)))) + +(define-fun bodyA () BSeq + (bconcat (bconcat (bconcat (bconcat (hdr MB CFG) elem0) SEP) + (bconcat (bconcat elem1 SEP) elem2A)) + (bconcat (ite hasTailA (bconcat SEP tailA) bempty) + (ftr CFG)))) +(define-fun bodyB () BSeq + (bconcat (bconcat (bconcat (bconcat (hdr MB CFG) elem0) SEP) + (bconcat (bconcat (bconcat (bconcat elem1 SEP) elem2B) SEP) + elemM)) + (bconcat (ite hasTailB (bconcat SEP tailB) bempty) + (ftr CFG)))) + +; guaranteed-stable depth: envelope head + elements 0,1 + stable content of +; the (stationary) merge anchor +(define-fun D () Int + (+ (blen (hdr MB CFG)) (blen elem0) (blen SEP) (blen elem1) (blen SEP) + (blen (fpre r2)) (blen (esc c2)))) + +; explicit common-prefix term whose length is exactly D (used by the theorem / +; mutation files) +(define-fun P () BSeq + (bconcat (bconcat (bconcat (bconcat (bconcat (bconcat (hdr MB CFG) elem0) SEP) elem1) SEP) + (fpre r2)) + (esc c2))) diff --git a/proof/spec/t2-host.smt2 b/proof/spec/t2-host.smt2 new file mode 100644 index 0000000000..aec3c1e5c5 --- /dev/null +++ b/proof/spec/t2-host.smt2 @@ -0,0 +1,35 @@ +; THEOREM 2 (host discipline): with the persisted MONOTONE cursor -- grounded +; in src/session.ts (state.nextIndex carried verbatim into assignRefs; +; cursor := allocated+1; retire/archive never move it) -- no allocation in the +; session reissues an ever-issued number, even though the session high died. +; Negated conclusion below; expectation UNSAT. +; +; Allocation rule (kernel/src/refs.ts allocateFreeRef), unfolded GROUND form +; over the finite live sets (the scan walks up from the floor; a winner above +; candidate k means every earlier candidate -- k included -- was taken): +; (FLOOR) n >= floor +; (FREE) n not in the current live number set +; (SMALL) n > k ==> k in the current live number set +; Live sets, unfolded from the store chain in t2.smt2: +; M2: idB->2, idC->3 (idA retired; nothing else ever bound) +; M3: idB->2, idC->3, q1->n3 + +; step 3: append q1; floor = c1 (persisted cursor = 6) +(declare-const n3 Int) +(assert (>= n3 c1)) +(assert (and (not (= n3 2)) (not (= n3 3)))) +(assert (=> (> n3 c1) (or (= c1 2) (= c1 3)))) +(assert (=> (> n3 (+ c1 1)) (or (= (+ c1 1) 2) (= (+ c1 1) 3)))) +(declare-const c2 Int) +(assert (= c2 (+ n3 1))) + +; step 4: append q2 +(declare-const n4 Int) +(assert (>= n4 c2)) +(assert (and (not (= n4 2)) (not (= n4 3)) (not (= n4 n3)))) +(assert (=> (> n4 c2) (or (= c2 2) (= c2 3) (= c2 n3)))) +(assert (=> (> n4 (+ c2 1)) (or (= (+ c2 1) 2) (= (+ c2 1) 3) (= (+ c2 1) n3)))) + +; NEGATED conclusion: SOME allocation reissues an ever-issued number +(assert (or (= n3 2) (= n3 3) (= n3 5) + (= n4 2) (= n4 3) (= n4 5) (= n4 n3))) diff --git a/proof/spec/t2-mutation.smt2 b/proof/spec/t2-mutation.smt2 new file mode 100644 index 0000000000..867af3e126 --- /dev/null +++ b/proof/spec/t2-mutation.smt2 @@ -0,0 +1,32 @@ +; MUTATION (T2): the #421 residual made executable. After the archive prunes +; the live map (idA's number 5 gone, nothing above it allocated), the cursor is +; RECOMPUTED as highestUsedIndex(pruned)+1 instead of being carried verbatim. +; Expectation SAT -- the model must exhibit the reissued number (n4 = 5). +; Same ground-form allocation rule as t2-host.smt2. + +; highestUsedIndex(M2) per kernel/src/refs.ts: max over live bindings. +; Ground unfolding: M2 binds exactly idB->2 and idC->3 (idA retired), so 3. +(declare-const hui Int) +(assert (= hui 3)) +(declare-const creset Int) +(assert (= creset (+ hui 1))) + +; step 3: append q1; floor = max(creset, MIN_INDEX=1) +(declare-const n3 Int) +(assert (>= n3 creset)) +(assert (and (not (= n3 2)) (not (= n3 3)))) +(assert (=> (> n3 creset) (or (= creset 2) (= creset 3)))) +(assert (=> (> n3 (+ creset 1)) (or (= (+ creset 1) 2) (= (+ creset 1) 3)))) +(declare-const c3 Int) +(assert (= c3 (+ n3 1))) + +; step 4: append q2 +(declare-const n4 Int) +(assert (>= n4 c3)) +(assert (and (not (= n4 2)) (not (= n4 3)) (not (= n4 n3)))) +(assert (=> (> n4 c3) (or (= c3 2) (= c3 3) (= c3 n3)))) +(assert (=> (> n4 (+ c3 1)) (or (= (+ c3 1) 2) (= (+ c3 1) 3) (= (+ c3 1) n3)))) + +; SOME allocation reissues an ever-issued number -- expected SAT (n4 = 5) +(assert (or (= n3 2) (= n3 3) (= n3 5) + (= n4 2) (= n4 3) (= n4 5) (= n4 n3))) diff --git a/proof/spec/t2.smt2 b/proof/spec/t2.smt2 new file mode 100644 index 0000000000..719807b538 --- /dev/null +++ b/proof/spec/t2.smt2 @@ -0,0 +1,52 @@ +; THEOREM 2 -- ref allocation session model. +; Grounded in kernel/src/refs.ts (assignRefs / allocateFreeRef / +; highestUsedIndex) and src/session.ts applyCompactionArchive (#421). +; +; Session shape (SAME operation sequence in all variants): +; M0: idA->5, idB->2, idC->3, persisted cursor c0=6 +; (numbers 2,3,5 were allocated earlier in the session; the cursor sits +; above every one of them -- the invariant the host maintains) +; step 1: RETIRE idA -- the session HIGH dies (fold/death removes its binding) +; step 2: ARCHIVE (#421) -- prune the live map to surviving raw ids +; (numeric no-op on this instance; the pruning's effect on the +; cursor is exactly what the variants disagree about) +; step 3: APPEND q1 -> allocates n3 +; step 4: APPEND q2 -> allocates n4 +; +; Variants: +; t2-host.smt2 persisted MONOTONE cursor (state.nextIndex carried verbatim, +; cursor := n+1 after each allocation, retire/archive never +; move it) => negation of "some allocation reissues an +; ever-issued number" expected UNSAT. +; t2-mutation.smt2 cursor RECOMPUTED from the pruned map after the archive +; (highestUsedIndex+1 -- the #421 residual) => reuse +; expected SAT; the model exhibits n4 = 5 (idA's dead number). + +(declare-sort RawId 0) +(declare-const UNDEF Int) +(declare-const BLOCKED Int) +(assert (= UNDEF -1)) +(assert (= BLOCKED -2)) + +(declare-const idA RawId) +(declare-const idB RawId) +(declare-const idC RawId) +(assert (distinct idA idB idC)) +(declare-const c0 Int) +(assert (= c0 6)) +; step 1: retire idA -- the persisted cursor does not move +(declare-const c1 Int) +(assert (= c1 c0)) + +(declare-const q1 RawId) +(declare-const q2 RawId) +(assert (and (not (= q1 idA)) (not (= q1 idB)) (not (= q1 idC)) + (not (= q2 idA)) (not (= q2 idB)) (not (= q2 idC)) + (not (= q2 q1)))) + +; NOTE: the live maps themselves (M0 -> M1 -> M2 via retire/archive) are NOT +; formally asserted here -- the variant files state their consequences in GROUND +; form over the finite live sets (only integers, no array quantifiers): the +; ground facts below are mechanical unfoldings of exactly this store chain. + +; yardstick: numbers ever issued before this session -- ISSUED0 = {2, 3, 5} diff --git a/tests/cache-theorem.test.ts b/tests/cache-theorem.test.ts new file mode 100644 index 0000000000..0e52634b73 --- /dev/null +++ b/tests/cache-theorem.test.ts @@ -0,0 +1,292 @@ +/** + * Cache-stability theorems (#2149) -- solver lane. + * + * Machine-checked statements (Z3, pinned z3-solver==4.9.1.0 in CI): + * T1 growth stability f(H) vs f(H+[m]) byte-equal up to depth D outside the + * existentially quantified declared carrier positions + * (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. + * + * Trust boundary: theorems are proven over proof/spec/*.smt2 (the assembly + * CONTRACT). The [grounding:*] subtests below pin each spec axiom to the real + * code (serializer escaping, tag rendering, ref-cursor mechanics); the #2144 + * billing suite pins the whole-body behavior empirically. See proof/README.md. + * + * Solver availability: a `z3` CLI on PATH or python3 with the `z3` module. + * Without either, solver subtests skip individually (grounding always runs) so + * the main suite stays green on boxes without a solver; the CI theorem lane + * guarantees execution. + */ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import os from "node:os"; +import path from "path"; +import { fileURLToPath } from "node:url"; +import type { CoreMessage } from "../kernel/src/types.js"; + +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); +const SPEC_DIR = path.join(repoRoot, "proof", "spec"); +const SOLVER_TIMEOUT_MS = 120_000; + +type SolverKind = "cli" | "py" | null; + +function detectSolver(): SolverKind { + const cli = spawnSync("z3", ["--version"], { encoding: "utf8", timeout: 15_000 }); + if (!cli.error && cli.status === 0) return "cli"; + const py = spawnSync( + "python3", + ["-c", "import z3"], + { encoding: "utf8", timeout: 15_000 }, + ); + if (!py.error && py.status === 0) return "py"; + return null; +} + +const PY_SHIM = ` +import re, sys +import z3 + +src = open(sys.argv[1], "r").read() +want_model = sys.argv[2] == "1" +names = [n for n in sys.argv[3].split(",") if n] +if want_model: + src += "\\n(set-option :model true)\\n(get-model)\\n" +s = z3.Solver() +s.from_string(src) +r = s.check() +print("VERDICT:" + ("sat" if r == z3.sat else ("unsat" if r == z3.unsat else "unknown"))) +if want_model and r == z3.sat: + m = s.model() + # new bindings expose to_smt2(); 4.9.x only has the internal str() form + txt = m.to_smt2() if hasattr(m, "to_smt2") else str(m) + for name in names: + val = None + hit = re.search(r"\\(define-fun " + re.escape(name) + r" \\(\\)[^\\n]*? ([^)\\n]+)\\)", txt) + if hit: + val = hit.group(1) + else: + hit = re.search(r"(?:^|[\\s,\\[])" + re.escape(name) + r" = ([^\\s,\\]\\]]+)", txt) + if hit: + val = hit.group(1) + print("MODEL:%s=%s" % (name, val if val is not None else "?")) +`; + +interface Check { + name: string; + parts: string[]; + expect: "sat" | "unsat"; + modelVars?: string[]; +} + +const CHECKS: Check[] = [ + { name: "t1-s1-context-consistent", parts: ["preamble.smt2", "t1-s1.smt2"], expect: "sat" }, + { + name: "t1-s1-growth-stability", + parts: ["preamble.smt2", "t1-s1.smt2", "t1-s1-theorem.smt2"], + expect: "unsat", + }, + { + // verdict-only: the corrupted content constant is existentially hidden by + // construction (spec-level abstraction); the byte surface is #2144's job + name: "t1-s1-stable-byte-mutation", + parts: ["preamble.smt2", "t1-s1.smt2", "t1-s1-mutation.smt2"], + expect: "sat", + }, + { name: "t1-s2-context-consistent", parts: ["preamble.smt2", "t1-s2.smt2"], expect: "sat" }, + { + name: "t1-s2-growth-stability", + parts: ["preamble.smt2", "t1-s2.smt2", "t1-s2-theorem.smt2"], + expect: "unsat", + }, + { + // verdict-only, same rationale as the S1 mutation above + name: "t1-s2-middle-element-mutation", + parts: ["preamble.smt2", "t1-s2.smt2", "t1-s2-mutation.smt2"], + expect: "sat", + }, + { name: "t2-context-consistent", parts: ["preamble.smt2", "t2.smt2"], expect: "sat" }, + { + name: "t2-no-reuse-under-monotone-cursor", + parts: ["preamble.smt2", "t2.smt2", "t2-host.smt2"], + expect: "unsat", + }, + { + name: "t2-archive-reset-reissues", + parts: ["preamble.smt2", "t2.smt2", "t2-mutation.smt2"], + expect: "sat", + modelVars: ["hui", "creset", "n3", "n4"], + }, +]; + +function runCheck(kind: "cli" | "py", check: Check): { verdict: string; models: string[]; ms: number } { + const src = check.parts.map((p) => readFileSync(path.join(SPEC_DIR, p), "utf8")).join("\n\n"); + const tmp = mkdtempSync(path.join(os.tmpdir(), "bili-theorem-")); + const file = path.join(tmp, check.name + ".smt2"); + const wantModel = check.expect === "sat"; + writeFileSync(file, src + (wantModel ? "\n(set-option :model true)\n(get-model)\n" : "")); + const args = + kind === "cli" + ? ["z3", "-smt2", file] + : ["python3", "-c", PY_SHIM, file, wantModel ? "1" : "0", (check.modelVars ?? []).join(",")]; + const start = Date.now(); + const res = spawnSync(args[0], args.slice(1), { encoding: "utf8", timeout: SOLVER_TIMEOUT_MS }); + const ms = Date.now() - start; + const out = `${res.stdout ?? ""}${res.stderr ?? ""}`; + let verdict = "unknown"; + const vm = out.match(/VERDICT:(\w+)/); + if (vm) verdict = vm[1]; + else { + const lines = out.split("\n").map((l) => l.trim()); + verdict = lines.find((l) => l === "sat" || l === "unsat" || l === "unknown") ?? "error"; + if (res.error) verdict = `error:${String(res.error)}`; + } + const models = [...out.matchAll(/MODEL:(\w+)=([^\s)]+)/g)].map((m) => `${m[1]}=${m[2]}`); + return { verdict, models, ms }; +} + +test("cache-theorem: solver obligations", async (t) => { + const kind = detectSolver(); + if (kind === null) { + await t.test("solver unavailable -> skip", (st) => { + st.skip("no z3 CLI and no python3 z3 module on this box; CI theorem lane covers execution"); + }); + return; + } + const timings: Array<[string, string, string[], number]> = []; + for (const check of CHECKS) { + await t.test(check.name, async () => { + const { verdict, models, ms } = runCheck(kind, check); + timings.push([check.name, verdict, models, ms]); + assert.equal(verdict, check.expect, `expected ${check.expect}, got ${verdict}`); + if (check.expect === "sat" && kind === "py" && check.modelVars?.length) { + assert.ok(models.length > 0, "SAT check should expose a model witness"); + } + }); + } + const report = + timings.map(([name, verdict, models, ms]) => `${name}: ${verdict} ${models.join(" ")} ${ms}ms`) + .join("\n") + `\ntotal: ${timings.reduce((a, [, , , ms]) => a + ms, 0)}ms`; + process.stdout.write(report + "\n"); +}); + +// Grounding: pins each spec axiom to the real code; runs WITHOUT a solver. +function mulberry32(seed: number): () => number { + let a = seed >>> 0; + return () => { + a |= 0; + a = (a + 0x6d2b79f5) | 0; + let t = Math.imul(a ^ (a >>> 15), 1 | a); + t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; + }; +} + +function randBmpString(rng: () => number, maxLen: number): string { + const len = Math.floor(rng() * maxLen); + let out = ""; + for (let i = 0; i < len; i++) { + // valid BMP chars only: exclude surrogates (0xD800-0xDFFF) to stay clear + // of lone-surrogate engine edge cases + let cp: number; + do { + cp = Math.floor(rng() * 0x10000); + } while (cp >= 0xd800 && cp <= 0xdfff); + out += String.fromCodePoint(cp); + } + return out; +} + +test("[grounding:g3] esc homomorphism + injectivity pinned to the real serializer", () => { + const rng = mulberry32(0x2149); + for (let i = 0; i < 200; i++) { + const a = randBmpString(rng, 24); + const b = randBmpString(rng, 24); + const inner = (s: string) => JSON.stringify(s).slice(1, -1); + assert.equal(inner(a + b), inner(a) + inner(b), "E1 homomorphism"); + if (inner(a) === inner(b)) assert.equal(a, b, "E2 injectivity (collision case)"); + } +}); + +test("[grounding:g2] mktag determinism/injectivity pinned to indexToRef", async () => { + const { indexToRef, refToIndex } = await import("../kernel/src/refs.js"); + const seen = new Set(); + for (let i = 1; i <= 200_000; i++) { + const tag = indexToRef(i); + assert.equal(indexToRef(i), tag, "determinism"); + assert.ok(!seen.has(tag), `injectivity at ${i}`); + seen.add(tag); + assert.equal(refToIndex(tag), i, "round-trip"); + } +}); + +test("[grounding:g1] ref cursor mechanics pinned to assignRefs/allocateFreeRef", async () => { + const { assignRefs, emptyRefMap, highestUsedIndex, BLOCKED_REF } = await import("../kernel/src/refs.js"); + const msg = (id: string): CoreMessage => ({ id, role: "user", contentType: "text" }); + + const liveNumbers = (map: ReturnType): number[] => + Object.values(map.byRaw) + .filter((v) => v !== BLOCKED_REF) + .map((v) => Number(v.replace(/^m0*/, ""))); + + // fresh assignment: smallest free number >= max(nextIndex, 1); cursor := n+1 + { + const existing = emptyRefMap(); + existing.byRaw["a"] = "m00005"; + existing.byRef["m00005"] = "a"; + existing.byRaw["b"] = "m00002"; + existing.byRef["m00002"] = "b"; + existing.byRaw["c"] = "m00003"; + existing.byRef["m00003"] = "c"; + const res = assignRefs([msg("q1")], { existing, nextIndex: 6 }); + assert.equal(res.newlyAssigned, 1); + assert.equal(res.nextIndex, 7, "cursor := allocated+1"); + assert.equal(res.map.byRaw["q1"], "m00006", "smallest free >= nextIndex"); + assert.equal(highestUsedIndex(res.map), 6); + } + // pre-existing binding untouched; gap below the cursor is claimable + { + const existing = emptyRefMap(); + existing.byRaw["a"] = "m00005"; + existing.byRef["m00005"] = "a"; + const res = assignRefs([msg("a"), msg("q")], { existing, nextIndex: 4 }); + assert.equal(res.map.byRaw["a"], "m00005", "existing untouched"); + assert.equal(res.newlyAssigned, 1, "existing message consumes no number"); + assert.equal(res.map.byRaw["q"], "m00004", "gap below cursor is the first free slot"); + assert.equal(res.nextIndex, 5); + } + // BLOCKED consumes no number + { + const res = assignRefs( + [msg("p"), msg("q")], + { existing: emptyRefMap(), nextIndex: 1, isProtected: (m) => m.id === "p" }, + ); + assert.equal(res.map.byRaw["p"], BLOCKED_REF); + assert.equal(res.map.byRaw["q"], "m00001", "protected message consumes no number"); + assert.equal(res.nextIndex, 2); + } + // monotone: sequential appends yield strictly increasing numbers + { + let map = emptyRefMap(); + let cursor = 1; + let prev = 0; + const rng = mulberry32(0xbeef); + for (let i = 0; i < 50; i++) { + const id = `x${Math.floor(rng() * 1e9)}`; + const res = assignRefs([msg(id)], { existing: map, nextIndex: cursor }); + map = res.map; + cursor = res.nextIndex; + const nums = liveNumbers(map); + const newest = Number(map.byRaw[id].replace(/^m0*/, "")); + assert.ok(newest > prev, "strictly increasing"); + prev = newest; + assert.equal(new Set(nums).size, nums.length, "live bindings pairwise distinct"); + assert.equal(cursor, prev + 1, "cursor tracks highest allocated + 1"); + } + } +}); From 407f712f0533e47dc341517854565fd8c1b2ebb5 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 5 Oct 2026 16:20:35 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20theorem=20lane=20=E2=80=94=20z3=20CL?= =?UTF-8?q?I=20path=20needs=20(check-sat);=20extract=20model=20witness=20t?= =?UTF-8?q?here=20too=20(#2159)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The spec files carry assertions only. detectSolver() prefers a z3 CLI on PATH, and the pinned z3-solver wheel bundles one, so CI took the CLI branch: 'z3 -smt2 ' executed no check command, printed no verdict, exited 0 — all 9 obligations reported 'error'. Append (check-sat) (and get-model after it for SAT checks) on the CLI path, parse z3's define-fun model output for the mutation-witness assertion on both paths. --- tests/cache-theorem.test.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/cache-theorem.test.ts b/tests/cache-theorem.test.ts index 0e52634b73..875a8de322 100644 --- a/tests/cache-theorem.test.ts +++ b/tests/cache-theorem.test.ts @@ -129,7 +129,18 @@ function runCheck(kind: "cli" | "py", check: Check): { verdict: string; models: const tmp = mkdtempSync(path.join(os.tmpdir(), "bili-theorem-")); const file = path.join(tmp, check.name + ".smt2"); const wantModel = check.expect === "sat"; - writeFileSync(file, src + (wantModel ? "\n(set-option :model true)\n(get-model)\n" : "")); + // The spec files carry assertions only. As a z3 CLI script they execute no + // check command and print no verdict unless (check-sat) is appended; the + // python-API path checks programmatically and needs nothing extra. + const trailer = + kind === "cli" + ? wantModel + ? "\n(set-option :model true)\n(check-sat)\n(get-model)\n" + : "\n(check-sat)\n" + : wantModel + ? "\n(set-option :model true)\n(get-model)\n" + : ""; + writeFileSync(file, src + trailer); const args = kind === "cli" ? ["z3", "-smt2", file] @@ -146,7 +157,13 @@ function runCheck(kind: "cli" | "py", check: Check): { verdict: string; models: verdict = lines.find((l) => l === "sat" || l === "unsat" || l === "unknown") ?? "error"; if (res.error) verdict = `error:${String(res.error)}`; } - const models = [...out.matchAll(/MODEL:(\w+)=([^\s)]+)/g)].map((m) => `${m[1]}=${m[2]}`); + // py path: MODEL:name=value lines from the shim. cli path: z3 prints the + // model as (define-fun name () Int ) pairs, value possibly on a + // following line. + const models = [ + ...out.matchAll(/MODEL:(\w+)=([^\s)]+)/g), + ...(kind === "cli" ? [...out.matchAll(/\(define-fun (\w+) \(\) Int\r?\n?\s*(-?\d+)/g)] : []), + ].map((m) => `${m[1]}=${m[2]}`); return { verdict, models, ms }; } @@ -164,7 +181,7 @@ test("cache-theorem: solver obligations", async (t) => { const { verdict, models, ms } = runCheck(kind, check); timings.push([check.name, verdict, models, ms]); assert.equal(verdict, check.expect, `expected ${check.expect}, got ${verdict}`); - if (check.expect === "sat" && kind === "py" && check.modelVars?.length) { + if (check.expect === "sat" && check.modelVars?.length) { assert.ok(models.length > 0, "SAT check should expose a model witness"); } }); From 582c881885039971efc5872343af451d380e05f1 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 5 Oct 2026 16:20:35 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20theorem=20lane=20paths=20filter=20?= =?UTF-8?q?=E2=80=94=20cover=20src/session.ts=20(#2159)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit t2.smt2 grounds the archive op in applyCompactionArchive and the cursor discipline in state.nextIndex carry-verbatim, both in src/session.ts; the paths filter missed that file, so changes there could bypass the lane. --- .github/workflows/ci-theorem.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci-theorem.yml b/.github/workflows/ci-theorem.yml index c5c933c26d..6223d2b4cb 100644 --- a/.github/workflows/ci-theorem.yml +++ b/.github/workflows/ci-theorem.yml @@ -16,6 +16,7 @@ on: - "src/chain-checkpoint.ts" - "src/image-note.ts" - "src/server.ts" + - "src/session.ts" - "src/fake-completion.ts" - "src/degenerate-retry.ts" - "kernel/src/rebuild.ts" @@ -36,6 +37,7 @@ on: - "src/chain-checkpoint.ts" - "src/image-note.ts" - "src/server.ts" + - "src/session.ts" - "src/fake-completion.ts" - "src/degenerate-retry.ts" - "kernel/src/rebuild.ts"