From 9f85c2a312fcc5e62fd1a0a60667d735b0d6c080 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 21 Oct 2026 13:31:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?verif:=20tag-echo-filter=20replay=20judge?= =?UTF-8?q?=20=E2=80=94=20finds=20real=20bugs=20#4106=20+=20#4107?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit tests/verif-tag-echo-filter.test.ts: property judge over the REAL filter family (src/loop/tag-echo-filter.ts). No solver — the interesting entry points are outside the symbolic sublanguage today (lookaround + \\p{So} regexes in stripAcpTags' family, stateful re.exec loops in the stream filters), so this judge runs real code over adversarial grammar-generated shapes (deterministic seed 0x7ea4c0de): - P1 chunk-split equivalence (streaming ≡ whole-text) across 5 filter factories, all split points for short texts + sampled + 3-way - P1b absorb-instructed emission hold split-stability - P2 stripAcpTags/normalizeEchoArtifacts idempotence - P3 monotone length, P4 clean-prose passthrough, P5 residue duality (strip-acted ⇒ strict detectors silent), P6 stats accounting, P7 identity/compose sanity - per-issue controls pinning the file's own history: #644 #1720 #1760 #1881 #2023 #2862 #717 #2405 TWO real bugs found immediately by the properties: - #4106 stripAcpTags is NOT idempotent: the gated DEAD_REF_CLOSE pass evaluates `out !== text` BEFORE the marker-line pass (which runs LAST), so a marker line + trailing dead-ref-close text strips once (keeps the dead ref) then strips MORE on the second call (marker removal enabled an end-anchored TRUNC_REF_CLOSE match). Re-normalized messages shrink per pass = unstable sent bytes. Pinned C#4106; P2 domain excludes the shape until fixed. - #4107 stream output depends on chunk boundaries (two shapes): (a) dead-ref residue `m1255 number { + let a = seed >>> 0; + return () => { + 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; + }; +} +const rnd = mulberry32(0x7ea4c0de); +const ri = (lo: number, hi: number): number => lo + Math.floor(rnd() * (hi - lo + 1)); +const pick = (xs: readonly T[]): T => xs[ri(0, xs.length - 1)]!; + +// ------------------------------------------------------- tag grammar +// ACP_NAME_ALT is a case-insensitive alternation of acp-permutations +// (optionally with one inserted letter from [aAcCpPiI]); the generator +// samples names from that language so the regexes see real shapes. +const CORES = ["acp", "apc", "cap", "cpa", "pac", "pca"] as const; +const ci = (ch: string): string => (rnd() < 0.5 ? ch.toLowerCase() : ch.toUpperCase()); +function acpName(): string { + const core = pick(CORES); + const letters = core.split(""); + if (rnd() < 0.25) letters.splice(ri(0, letters.length), 0, pick(["a", "A", "c", "C", "p", "P", "i", "I"])); + return letters.map(ci).join(""); +} +const ATTRS = [` tokens="12" type="text"`, ` = "x"`, ` tokens="0"`] as const; +const attr = (): string => (rnd() < 0.5 ? "" : pick(ATTRS)); +const refRun = (): string => { + const n = ri(1, 3); + const out: string[] = []; + for (let i = 0; i < n; i++) out.push(`m${ri(0, 9999).toString().padStart(4, "0")}`); + return out.join(" "); +}; +const PROSE = [ + "the quick brown fox", "jumps over", "42 lazy dogs", "x", "hello, world", + "multi\nline\ntext", "unicode ✓ café 日本", "trailing spaces ", " leading", + "quotes \" and ' mixed", "angle < not a tag", "half \x3c acp", "digits 123456", +] as const; + +function tagShape(): string { + const name = acpName(); + switch (ri(0, 10)) { + case 0: return `<${name}${attr()}>${refRun()}`; // paired, refs payload + case 1: return `<${name}${attr()}>${pick(PROSE)}`; // paired, prose payload + case 2: return `<${name}${attr()}/>`; // self-close + case 3: return `<${name}${attr()}>`; // lone open + case 4: return ``; // lone close + case 5: return `<${name}> m${ri(1000, 9999)} `; // degenerate pair + case 6: { const t = `<${name}${attr()}>${pick(PROSE)}`; return t.slice(0, ri(1, Math.max(1, t.length - 1))); } // truncated (either end) + case 7: return `${refRun()}${pick(PROSE)}`; // open, no close (prose-wearing?) + default: return pick(PROSE); // plain prose + } +} +function randomText(): string { + const parts: string[] = []; + const n = ri(1, 6); + for (let i = 0; i < n; i++) parts.push(rnd() < 0.45 ? tagShape() : pick(PROSE)); + return parts.join(pick([" ", "\n", "", " ", "\n\n"])); +} +// A tool-call emission field (toolCallEmissionSpan shape) for the +// absorb-instructed stream paths. +function emissionField(): string { + const ref = `m${ri(0, 9999).toString().padStart(4, "0")}`; + return `\n\n${ref}\n\nsum\n`; +} + +// -------------------------------------------------- stream properties +type FilterFactory = () => TagEchoFilter; +const FACTORIES: [string, FilterFactory][] = [ + ["tagEcho", () => createTagEchoFilter()], + ["tagEcho+absorb", () => createTagEchoFilter(undefined, undefined, true)], + ["marker", () => createMarkerLineFilter()], + ["artifact", () => createBiliArtifactFilter()], + ["marker∘tagEcho", () => composeStreamFilters(createMarkerLineFilter(), createTagEchoFilter())], +]; +/** push every chunk, flush, concatenate — the filter's full transcript. */ +const runStream = (f: TagEchoFilter, chunks: string[]): string => + chunks.map((c) => f.push(c)).join("") + f.flush(); + +/** Split points to sample: all positions for short texts, boundaries + + * random interior for long ones. Splitting INSIDE a multi-byte char is + * still valid — JS strings are UTF-16 code units, slice lands on them. */ +const splitPoints = (t: string): number[] => { + if (t.length <= 12) return Array.from({ length: t.length + 1 }, (_, i) => i); + const pts = new Set([1, t.length - 1, t.length]); + for (let i = 0; i < 8; i++) pts.add(ri(1, t.length - 1)); + return [...pts]; +}; + +test("P1 chunk-split equivalence: streaming === whole-text", { skip: false }, () => { + // Domain: split-dependence is a KNOWN bug while #4107 is open — texts + // whose shape triggers the dead-ref-close release path diverge (pinned + // as control C#4107 below). Until it lands, run the property on texts + // WITHOUT echo residue (clean pairing / markers / prose) — the filter + // must at least be split-invariant there. + for (let iter = 0; iter < 120; iter++) { + const base = rnd() < 0.1 ? emissionField() : randomText(); + const text = rnd() < 0.2 ? `${pick(PROSE)}${base}${pick(PROSE)}` : base; + if (containsRenderTagText(text) || containsEchoResidue(text)) continue; // #4107 family: split-dependence on tag-bearing texts (2 shapes pinned) + for (const [name, mk] of FACTORIES) { + const whole = runStream(mk(), [text]); + for (const k of splitPoints(text)) { + const two = runStream(mk(), [text.slice(0, k), text.slice(k)]); + assert.equal(two, whole, `${name}: 2-way split @${k} diverged — text=${JSON.stringify(text)}`); + } + // three-way split at two sampled points + const a = ri(1, Math.max(1, text.length - 2)); + const b = ri(a, text.length); + const three = runStream(mk(), [text.slice(0, a), text.slice(a, b), text.slice(b)]); + assert.equal(three, whole, `${name}: 3-way split (${a},${b}) diverged — text=${JSON.stringify(text)}`); + } + } +}); + +test("P1b absorb-instructed emission hold is split-stable", () => { + for (let iter = 0; iter < 60; iter++) { + const field = `${pick(["", " ", "\n"])}${emissionField()}`; + const request = `model instructions ${ABSORB_INSTRUCTION_MARKER} please comply`; + const mk = (): TagEchoFilter => createTagEchoFilter(undefined, undefined, true, request); + const whole = runStream(mk(), [field]); + for (const k of splitPoints(field)) { + assert.equal( + runStream(mk(), [field.slice(0, k), field.slice(k)]), + whole, + `absorb hold split @${k} diverged — field=${JSON.stringify(field)}`, + ); + } + } +}); + +test("P2 stripAcpTags / normalizeEchoArtifacts are idempotent", () => { + // Domain: idempotence is a KNOWN bug while #4106 is open — the marker- + // line pass enables a later TRUNC_REF_CLOSE strip on the second call + // (pinned as control C#4106 below). Until it lands, assert idempotence + // on texts without the triggering shape (marker line AND trailing + // dead-ref-close together). + for (let iter = 0; iter < 400; iter++) { + const t = randomText(); + if (/\[ACP\]/.test(t) && /m\d{4,}\s*<\//.test(stripAcpTags(t))) continue; // #4106 domain exclusion + const s1 = stripAcpTags(t); + assert.equal(stripAcpTags(s1), s1, `strip not idempotent — input=${JSON.stringify(t)}`); + const n1 = normalizeEchoArtifacts(t); + assert.equal(normalizeEchoArtifacts(n1), n1, `normalize not idempotent — input=${JSON.stringify(t)}`); + } +}); + +test("P3 strip never lengthens the text", () => { + for (let iter = 0; iter < 400; iter++) { + const t = randomText(); + assert.ok( + stripAcpTags(t).length <= t.length, + `strip lengthened ${t.length} -> ${stripAcpTags(t).length} — input=${JSON.stringify(t)}`, + ); + } +}); + +test("P4 clean prose passes through untouched", () => { + for (let iter = 0; iter < 200; iter++) { + const parts: string[] = []; + for (let i = 0; i < ri(1, 4); i++) parts.push(pick(PROSE)); + const t = parts.join(" "); + // no tag-like evidence at all → byte-identical passthrough + assert.equal(stripAcpTags(t), t, `clean text mutated — input=${JSON.stringify(t)}`); + assert.equal(runStream(createTagEchoFilter(), [t]), t, `stream mutated clean text — input=${JSON.stringify(t)}`); + assert.equal(runStream(createMarkerLineFilter(), [t]), t); + } +}); + +test("P5 residue duality: strip removes every detectable artifact", () => { + for (let iter = 0; iter < 300; iter++) { + const t = randomText(); + const out = stripAcpTags(t); + const why: string[] = []; + // containsRenderTagText / containsEchoResidue are the strict shape + // detectors; containsMarkerLineText is deliberately the RAW pre-check + // (includes "[ACP]", per #717) while removal is line-anchored — a + // mid-line marker is detect-but-keep BY DESIGN, so it is excluded + // from the duality (only line-START markers must go). + // Echo residue may legitimately SURVIVE when the text carries no + // other tag attestation (#1760 close-only content preservation) — the + // contains* audits are log-signals, not strip guarantees. Duality is + // asserted only when the input was ATTESTED. + // atestation = the strip ACTED (its own #1760 gate passed — close-only + // shapes with no other evidence are content-preserved by design, and + // then the detectors keep firing on the preserved close: log-signals, + // not strip guarantees). When strip acted, nothing strict may remain. + const acted = stripAcpTags(t) !== t; + if (acted && containsRenderTagText(out)) why.push("render-tag"); + if (acted && containsEchoResidue(out)) why.push("echo-residue"); + if (/(^|\n)\s*[\u{1F300}-\u{1FAFF}\u2600-\u27BF}\u2B00-\u2BFF] ?\[ACP\]/u.test(out)) why.push("marker-line-at-line-start"); + assert.deepEqual(why, [], `strip left artifacts — input=${JSON.stringify(t)} out=${JSON.stringify(out)}`); + } +}); + +test("P6 stats accounting: output never exceeds input, drops imply dropped", () => { + for (let iter = 0; iter < 120; iter++) { + const f = createTagEchoFilter(); + const t = randomText(); + const out = runStream(f, [t]); + const st = f.stats(); + assert.ok(st.outputChars <= st.inputChars, `stats: ${st.outputChars} > ${st.inputChars}`); + assert.equal(st.inputChars, t.length); + assert.equal(out.length, st.outputChars); + // dropped ("anything dropped as an imitation") and dropCount ("logical + // artifacts") are DIFFERENT documented concepts — recordDrop paths can + // set dropped without counting an artifact; no equivalence asserted. + } +}); + +test("P7 identity filter and composition sanity", () => { + const id = createIdentityStreamFilter(); + const t = randomText(); + assert.equal(runStream(id, [t]), t); + const comp = composeStreamFilters(createIdentityStreamFilter(), createMarkerLineFilter()); + const single = runStream(createMarkerLineFilter(), [t]); + assert.equal(runStream(comp, [t]), single); + // composition is order-honest: both orders over random splits agree with + // their own whole-run (P1 covers stability; here just sanity that the + // composed object still honors the interface) + const comp2 = composeStreamFilters(createMarkerLineFilter(), createBiliArtifactFilter()); + const k = ri(1, Math.max(1, t.length - 1)); + assert.equal( + runStream(composeStreamFilters(createMarkerLineFilter(), createBiliArtifactFilter()), [t.slice(0, k), t.slice(k)]), + runStream(comp2, [t]), + ); +}); + +// ------------------------------------------- issue-history controls +test("C#644/#1881: bare open past cap releases as prose; attrs tail does not resurrect", () => { + // A bare opening followed by long prose: the tail is CONTENT when + // the swallow budget releases it (#644). The filter must not eat it all. + const t = "" + "prose words here ".repeat(40); + const f = createTagEchoFilter(); + const out = runStream(f, [t]); + assert.ok(out.includes("prose words"), `#644: content tail eaten — out=${JSON.stringify(out.slice(0, 120))}`); +}); +test("C#1720: a close inside a pair belongs to the pair (payload survives)", () => { + const t = "payload with insideafter"; + const out = stripAcpTags(t); + // the OUTER pair is stripped; the inner close belongs to its content — + // the payload text the client cares about must not vanish entirely, and + // 'after' (outside any tag) always survives. + assert.ok(out.includes("after"), `#1720: post-tag text lost — out=${JSON.stringify(out)}`); +}); +test("C#1760: close-only shape with no other echo is content-preserved", () => { + const t = "just text"; + assert.equal(stripAcpTags(t), t); + // a lone close with NO attestation: strip leaves it (no evidence) + const t2 = "text tail"; + const out2 = stripAcpTags(t2); + assert.ok(out2.length > 0, "#1760: close-only strip must not empty the text"); +}); +test("C#2862: dead ref-close unit goes when a tag echo is attested", () => { + // attested by a paired tag elsewhere in the text → the m1234 { + const line = "\u{1F4E6} [ACP] Compressed m00120\u2013m0300 \u2192 1 block(s)"; + const t = `before\n${line}\nafter`; + assert.ok(containsMarkerLineText(t)); + const f = createMarkerLineFilter(); + const out = runStream(f, [t]); + assert.ok(!out.includes("[ACP]"), `marker survived stream — out=${JSON.stringify(out)}`); + assert.ok(out.includes("before") && out.includes("after"), "marker filter ate surrounding lines"); +}); +test("C#2405: a pair split across pushes counts as ONE drop", () => { + const f = createTagEchoFilter(); + const full = `m0001`; + f.push(full.slice(0, 10)); + f.push(full.slice(10)); + f.flush(); + assert.equal(f.stats().dropCount, 1, `#2405: split pair counted as ${f.stats().dropCount}`); +}); + +// ------------------------------------------------ pinned bugs +// #4106: stripAcpTags not idempotent — the marker-line pass (LAST) enables +// the gated DEAD_REF_CLOSE / end-anchored TRUNC_REF_CLOSE passes on the +// SECOND call. FLIP this control (assert idempotence) when #4106 lands. +test("C#4106 pinned: stripAcpTags NOT idempotent today (marker + trailing dead-ref)", () => { + const t = "angle < not a tag\nm7385 { + const t = 'angle < not a tagm1255trailing spaces '; + const run = (chunks: string[]): string => { + const f = createTagEchoFilter(); + return chunks.map((c) => f.push(c)).join("") + f.flush(); + }; + const whole = run([t]); + const split = run([t.slice(0, 41), t.slice(41)]); + assert.notEqual(whole, split, "#4107 fixed? — flip this pin and re-enable the full P1 domain"); + assert.ok(split.includes("m1255"), "residue no longer leaks? — #4107 may be fixed; flip this pin"); +}); +// #4107 shape 2 (same family, different mechanism): a CLOSE fragment at +// the chunk boundary — whole-text strips the bare { + const t = 'unicode \u2713 caf\u00e9 \u65e5\u672c unicode \u2713 caf\u00e9 \u65e5\u672cquotes " and \' mixed'; + const run = (chunks: string[]): string => { + const f = createTagEchoFilter(); + return chunks.map((c) => f.push(c)).join("") + f.flush(); + }; + const whole = run([t]); + const split = run([t.slice(0, 49), t.slice(49)]); + assert.notEqual(whole, split, "#4107 fixed? — flip this pin and re-enable the full P1 domain"); + assert.ok(split.includes(" Date: Wed, 21 Oct 2026 13:37:44 +0800 Subject: [PATCH 2/2] verif: adapter buildRequest wire-stability judge (openai/anthropic/google) Turn-merging-aware append stability: completed turns' wire bytes must never change when history grows (P1 strict at turn boundaries, drop-last open-turn carve-out); idempotence + input non-mutation; notes tail-only; tool rounds verbatim. Generalizes the #1637/#1876/#2499 byte-prefix property across all three adapters. No solver needed (pure replay). --- tests/verif-adapter-wire-stability.test.ts | 216 +++++++++++++++++++++ verif/README.md | 1 + 2 files changed, 217 insertions(+) create mode 100644 tests/verif-adapter-wire-stability.test.ts diff --git a/tests/verif-adapter-wire-stability.test.ts b/tests/verif-adapter-wire-stability.test.ts new file mode 100644 index 0000000000..4fc433cc75 --- /dev/null +++ b/tests/verif-adapter-wire-stability.test.ts @@ -0,0 +1,216 @@ +/** + * Adapter wire-stability judge (direction 3). + * + * Targets: createOpenaiAdapter / createAnthropicAdapter / + * createGoogleAdapter `buildRequest` — the outbound re-serialization used on + * every compression round. The #3837 family's claim is "inbound is pure + * append; outbound must be too": growing the core history by ONE message may + * only append to the wire payload; every already-sent element must stay + * byte-identical across rounds (#1637 byte-prefix property, generalized off + * the anthropic comments and pinned for all three adapters). + * + * This is a replay judge: it drives the REAL adapters end-to-end, no symbolic + * engine. Properties: + * P1 append-stability — build(msgs[0..k)) payload is an elementwise prefix + * of build(msgs[0..k+1)) payload, for every k, and the non-payload + * fields (model/system/systemInstruction/…) are unchanged. + * P2 idempotence + input non-mutation — two builds deep-equal; the input + * core array and its messages are untouched. + * P3 notes stability — appending notes only appends elements at the tail + * (prefix from the no-notes build survives elementwise). + * P4 round-trip sanity — a follow-up user message after a tool round keeps + * the earlier tool call/result elements verbatim (non-vacuity for + * tool-bearing shapes; guards against future "smart" merges). + */ + +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; + +import { createOpenaiAdapter } from "../src/loop/adapter-openai.js"; +import { createAnthropicAdapter } from "../src/loop/adapter-anthropic.js"; +import { createGoogleAdapter } from "../src/loop/adapter-google.js"; +import type { CoreMessage } from "acp-kernel"; + +// --- helpers ------------------------------------------------------------- + +const j = (v: unknown) => JSON.stringify(v); + +function payloadOf(built: Record): unknown[] { + const arr = built.messages ?? built.contents; + assert.ok(Array.isArray(arr), `payload field missing: ${j(Object.keys(built))}`); + return arr as unknown[]; +} + +function miscOf(built: Record): Record { + const out: Record = {}; + for (const [k, v] of Object.entries(built)) { + if (k === "messages" || k === "contents") continue; + out[k] = v; + } + return out; +} + +/** elementwise prefix: a[i] deep-equals b[i] for all i < a.length */ +function assertPrefix(short: unknown[], long: unknown[], what: string): void { + assert.ok(short.length <= long.length, `${what}: length grew backwards (${short.length} > ${long.length})`); + for (let i = 0; i < short.length; i++) { + assert.deepEqual(short[i], long[i], `${what}: element ${i} changed when history grew`); + } +} + +/** Turn-merging-aware stability: everything EXCEPT the last element is a + * completed turn that was already sent once — those bytes must never + * change. The last element is the open turn and may legitimately grow + * (reasoning+text coalesce, tool blocks append, notes merge in). */ +function assertDropLastPrefix(short: unknown[], long: unknown[], what: string): void { + assert.ok(short.length <= long.length, `${what}: length grew backwards (${short.length} > ${long.length})`); + const stable = short.length - 1; + for (let i = 0; i < stable; i++) { + assert.deepEqual(short[i], long[i], `${what}: COMPLETED element ${i} changed when history grew`); + } +} + +// --- fixtures ------------------------------------------------------------ + +function coreMessages(): CoreMessage[] { + return [ + { id: "u1", role: "user", contentType: "text", text: "What is the weather in Tokyo?" }, + { id: "a1", role: "assistant", contentType: "tool-call", toolName: "get_weather", toolCallId: "call_1", text: "{\"city\":\"Tokyo\"}" }, + { id: "u2", role: "tool", contentType: "tool-result", toolCallId: "call_1", text: "18C, rain" }, + { id: "a2", role: "assistant", contentType: "reasoning", text: "check other cities too", thinkingTokens: 120 }, + { id: "a3", role: "assistant", contentType: "text", text: "It is 18C and rainy in Tokyo." }, + { id: "u3", role: "user", contentType: "text", text: "And in Osaka?" }, + { id: "a4", role: "assistant", contentType: "tool-call", toolName: "get_weather", toolCallId: "call_2", text: "{\"city\":\"Osaka\"}" }, + { id: "u4", role: "tool", contentType: "tool-result", toolCallId: "call_2", text: "21C, clear" }, + { id: "a5", role: "assistant", contentType: "text", text: "Osaka is 21C and clear." }, + ]; +} + +const SYS = "You are the compression operator. Keep answers short."; +const CLIENT_SYS = "Client-side system preamble."; + +// --- the three adapters, built once like the loop builds them ------------ + +const openaiBody = { model: "gpt-4o", temperature: 0.2, stream: true, messages: [{ role: "user", content: "stale host copy" }] }; +const anthropicBody = { model: "claude-3-7", max_tokens: 1024, stream: true, system: "client original system", messages: [{ role: "user", content: "stale host copy" }] }; +const googleBody = { model: "gemini-2", generationConfig: { temperature: 0.2 }, contents: [{ role: "user", parts: [{ text: "stale host copy" }] }] }; + +const adapters = [ + { + name: "openai", + build: (msgs: CoreMessage[]) => { + const a = createOpenaiAdapter(openaiBody, CLIENT_SYS, "absorb_x", ["note-a", "note-b"]); + return a.buildRequest(msgs, SYS, structuredClone(openaiBody)); + }, + bare: (msgs: CoreMessage[]) => { + const a = createOpenaiAdapter(openaiBody, CLIENT_SYS, "absorb_x"); + return a.buildRequest(msgs, SYS, structuredClone(openaiBody)); + }, + }, + { + name: "anthropic", + build: (msgs: CoreMessage[]) => { + const a = createAnthropicAdapter(anthropicBody, "client original system", ["note-a"]); + return a.buildRequest(msgs, SYS, structuredClone(anthropicBody)); + }, + bare: (msgs: CoreMessage[]) => { + const a = createAnthropicAdapter(anthropicBody, "client original system"); + return a.buildRequest(msgs, SYS, structuredClone(anthropicBody)); + }, + }, + { + name: "anthropic+cacheMarks", + build: (msgs: CoreMessage[]) => { + const marks = new Map([["u1", { type: "ephemeral" }], ["a3", { type: "ephemeral" }]]); + const a = createAnthropicAdapter(anthropicBody, "client original system", undefined, "protocol", marks); + return a.buildRequest(msgs, SYS, structuredClone(anthropicBody)); + }, + bare: (msgs: CoreMessage[]) => { + const marks = new Map([["u1", { type: "ephemeral" }], ["a3", { type: "ephemeral" }]]); + const a = createAnthropicAdapter(anthropicBody, "client original system", undefined, "protocol", marks); + return a.buildRequest(msgs, SYS, structuredClone(anthropicBody)); + }, + }, + { + name: "google", + build: (msgs: CoreMessage[]) => { + const a = createGoogleAdapter(googleBody, CLIENT_SYS, undefined, "gemini-2", ["note-a"]); + return a.buildRequest(msgs, SYS, structuredClone(googleBody)); + }, + bare: (msgs: CoreMessage[]) => { + const a = createGoogleAdapter(googleBody, CLIENT_SYS, undefined, "gemini-2"); + return a.buildRequest(msgs, SYS, structuredClone(googleBody)); + }, + }, +]; + +// --- judge ---------------------------------------------------------------- + +describe("verif: adapter wire stability (buildRequest append/idempotence)", () => { + for (const { name, build, bare } of adapters) { + it(`${name}: P1 append-stability — completed turns never change`, () => { + const msgs = coreMessages(); + const full = bare(msgs); + for (let k = 1; k <= msgs.length; k++) { + const shorter = bare(msgs.slice(0, k)); + assertDropLastPrefix(payloadOf(shorter), payloadOf(full), `${name} k=${k}`); + // non-payload fields must not depend on history length at all + assert.deepEqual(miscOf(shorter), miscOf(full), `${name} k=${k}: non-payload fields changed`); + } + // turn-COMPLETE boundaries: nothing merges across them, so the full + // elementwise prefix must hold (P1 strict form — non-vacuity for the + // drop-last carve-out above). + const fullPayload = payloadOf(full); + for (const k of [1, 5, 8]) { + const at = payloadOf(bare(msgs.slice(0, k))); + assertPrefix(at, fullPayload, `${name} strict k=${k}`); + } + }); + + it(`${name}: P2 idempotence + input non-mutation`, () => { + const msgs = coreMessages(); + const before = j(msgs); + const r1 = build(msgs); + const r2 = build(msgs); + assert.equal(j(r1), j(r2), `${name}: rebuild differs`); + assert.equal(j(msgs), before, `${name}: input core messages were mutated`); + }); + + it(`${name}: P3 payload stable across every adjacent growth pair`, () => { + const msgs = coreMessages(); + const built = msgs.map((_, k) => bare(msgs.slice(0, k + 1))); + for (let a = 0; a + 1 < built.length; a++) { + assertDropLastPrefix(payloadOf(built[a]), payloadOf(built[a + 1]), `${name} grow ${a + 1}->${a + 2}`); + } + }); + + it(`${name}: P3b notes only touch the tail`, () => { + const msgs = coreMessages(); + const without = payloadOf(bare(msgs)); + const withNotes = payloadOf(build(msgs)); + // anthropic/google merge a trailing user note into the open user turn + // (block/part append); openai emits separate user elements. Both are + // fine for cache stability as long as COMPLETED bytes never move. + if (withNotes.length >= without.length) { + assertDropLastPrefix(without, withNotes, `${name} notes`); + assert.ok(j(withNotes).includes(j(without.at(-1))), `${name}: last completed turn mangled by notes`); + } else { + assert.ok(false, `${name}: notes shrank the payload`); + } + }); + } + + it("all: P4 non-vacuity — payload is non-empty and tool rounds survive verbatim", () => { + const msgs = coreMessages(); + for (const { name, build, bare } of adapters) { + const full = payloadOf(build(msgs)); + assert.ok(full.length >= msgs.length - 2, `${name}: payload suspiciously short (${full.length})`); + // the openai tool-call args and tool results must round-trip verbatim + const flat = j(full); + assert.ok(flat.includes("call_1"), `${name}: toolCallId lost`); + assert.ok(flat.includes("get_weather"), `${name}: tool name lost`); + assert.ok(flat.includes('city'), `${name}: tool args lost`); + assert.ok(flat.includes("18C, rain"), `${name}: tool result lost`); + } + }); +}); diff --git a/verif/README.md b/verif/README.md index c770db29d3..16484fae15 100644 --- a/verif/README.md +++ b/verif/README.md @@ -39,6 +39,7 @@ VERIF_PYTHON=/path/to/python-with-cvc5 node --import tsx --test tests/verif-bug* | `verif-bug3805-durable-ref-migration` | 定向不变量 | 保护解除时 BLOCKED→数字引用迁移给已发送字节中途加标签;非 durable 追加稳定(UNSAT) | | `verif-generic-append-stability` | **任意输入** | 消息全符号化、唯一不变量"只增不删的字节稳定":Q1 SAT(#3805 形状泛化重现)、Q2 SAT(**求解器自己发现的新 bug #3945**:id 撞车重打标,零保护解除)、Q3 UNSAT(排除两个已知形状后界内无第三种) | | `verif-induction-append-stability` | **归纳判官** | 任意消息数的 append-only 稳定性:前缀抽象成一个符号绑定,归纳步 UNSAT(id 不重复时任何前缀状态都动不了);撤掉约束 SAT = #3945;真代码随机抽查 60 轮 | +| `verif-adapter-wire-stability` | 回放判官(无求解器) | 三个 adapter 的 buildRequest 出站重建:已完成轮次字节跨轮稳定(P1/P3,轮合并感知——开轮可长,完成轮不可动)、幂等+输入不可变、notes 只动尾部、工具轮全量保全。#1637/#1876/#2499 的字节前缀性质推广到三家并钉住 | | `verif-view-latch-discipline` | **锁存纪律判官**(#3989) | 安静轮 latch 字节逐字回放(Q1 UNSAT)+ 新鲜消息直通不受污染(Q2 UNSAT,任意 pre/post/latch 文本)+ 真代码行为矩阵(quiet 回放 / 到达锁存 / 折叠重置 / 稳态零churn) | | `verif-conflict-watch` / `verif-snapshot-render` / `verif-wire-anthropic-roundtrip` / `verif-wire-google-rebuild-stability` | 定向不变量 | conflict-watch 降落门限纪律(#2709/#3806 形状,边界 749/1000 vs 750/1000);snapshot-render 尾保真(发现 **#4052**:新消息与已压缩消息同文本被整条丢弃,钉为翻转断言);anthropic 同类对序+载荷保真;google 重建 append-only 稳定 | | `verif-responses-ingress` | 定向不变量(#3837 家族) | 入站三件套(normalize→sanitize→drop,真实管线顺序)幂等/规范化/局部性全部 UNSAT + 各阶段可达性 SAT + 60 轮随机回放锚 |