diff --git a/src/invariant-judge.ts b/src/invariant-judge.ts index 21d52cfeed..ad2e2a5267 100644 --- a/src/invariant-judge.ts +++ b/src/invariant-judge.ts @@ -37,6 +37,7 @@ import type { CoreMessage } from "acp-kernel"; import { openaiToCore, anthropicToCore, responsesToCore, googleToCore } from "acp-kernel/wire"; import { log as loggerLog } from "./logger.js"; import { invariantJudgeEnabled } from "./knobs.js"; +import { normalizeEchoArtifacts } from "./loop/tag-echo-filter.js"; // `ref`; angle brackets hex-escaped per KDD#2. // An optional leading newline is consumed too — the renderer injects the tag on @@ -89,7 +90,13 @@ interface JudgeOptions { } function normText(m: CoreMessage): string { - return (m.text ?? "").replace(RENDER_TAG_RE, "").trim(); + // #3938 PR-D closure: normalize by the SAME artifact set the outbound + // stream filters strip (case-drift #1731, typo drift #673, bare #1881, + // degen/self-close #2190/#2348, mangled #2066, markers #717, chain carriers + // #1634, dead ref-close #2862 …) so an echo shape one side legitimately + // removed can never read as a mid-history rewrite. RENDER_TAG_RE stays as + // a belt for the kernel's canonical render form. + return normalizeEchoArtifacts((m.text ?? "").replace(RENDER_TAG_RE, "")).trim(); } function isReasoning(m: CoreMessage): boolean { diff --git a/src/loop/tag-echo-filter.ts b/src/loop/tag-echo-filter.ts index 126904a9a2..26591afb8d 100644 --- a/src/loop/tag-echo-filter.ts +++ b/src/loop/tag-echo-filter.ts @@ -483,6 +483,25 @@ export function stripAcpTags(text: string, dropToolCallEmission = false, request return stripBiliArtifacts(out); } +/** Whole-text echo normalization for parity judges (the invariant judge, + * #3938 PR-D): apply the SAME artifact set the outbound stream filters strip, + * so entry-vs-exit comparisons never false-positive on shapes one side + * legitimately removed. Single source of truth by construction — when the + * filter family grows, the judge's normalization grows with it. Content- + * preserving for prose (paired shapes keep their payload; only tag bytes go), + * and the tool-call emission span is deliberately NOT touched (args are user + * intent, #1039). */ +export function normalizeEchoArtifacts(text: string): string { + if ( + !containsRenderTagText(text) && + !containsMarkerLineText(text) && + !containsBiliInternalText(text) + ) { + return text; + } + return stripAcpTags(text); +} + // Raw-wire pre-check for forged confirmation markers (#717): the literal // "[ACP]" survives JSON escaping unscathed (brackets are not escaped), so a // plain includes() on the raw SSE/JSON string is sound and cheap. The diff --git a/tests/invariant-judge.test.ts b/tests/invariant-judge.test.ts index bc88f68335..e6f42eb3a5 100644 --- a/tests/invariant-judge.test.ts +++ b/tests/invariant-judge.test.ts @@ -270,3 +270,50 @@ test("loop continuity judge: absent prev (clipped/oversized forensics) silently _setInvariantJudgeForTest(false); } }); + +// #3938 PR-D closure (拍板①): the judge's normalization must cover the SAME +// echo-artifact shapes the outbound stream filters strip. Each family below +// appears in the ENTRY text only (the shape a client resent that the pipeline +// legitimately removed on the way out) — none of them may read as a rewrite. +test("PR-D closure: echo family shapes in entry vs filtered exit never flag", () => { + const prose = "analysis of the cache seam"; + // exit models the REAL filter output per shape (stripAcpTags semantics); + // e.g. a mangled open drops its tags but leaves the ref behind as residue, + // and an isolated dead ref-close with no other echo evidence is kept + // verbatim (#1760) — the judge must agree with the filter either way. + const echoShapes: Array<[string, string, string]> = [ + ["case-drift (#1731)", `${prose}\n\x3cACP tokens="2" type="text"\x3em00420\x3c/ACP\x3e`, prose], + ["typo-drift (#673)", `${prose}\n\x3cacip tokens="1"\x3em00420\x3c/acip\x3e`, prose], + ["bare no-attrs (#1881)", `${prose}\n\x3cacp\x3em00420\x3c/acp\x3e`, prose], + ["degenerate close (#2190)", `${prose}\n\x3cacp tokens="2"\x3em00420\x3c/ap\x3e`, prose], + ["self-closing (#2348)", `${prose}\n\x3cacp tokens="2"/\x3e`, prose], + ["mangled open (#2066)", `${prose}\n\x3cacp=1\x3em00420\x3c/acp\x3e`, `${prose}\nm00420`], + ["forged marker line (#717)", `\u{1F52E} [ACP] compressed 3 messages (saved 1200 tokens)\n${prose}`, prose], + ["chain carrier artifact (#1634)", `${prose}\n\x3cbili-chain "x"/>\x3c/bili-chain\x3e`, `${prose}\n\x3cbili-chain "x"/>\x3c/bili-chain\x3e`], + ["dead ref-close (#2862)", `${prose} m00420\x3c/acp`, `${prose} m00420\x3c/acp`], + ]; + for (const [family, entryText, exitText] of echoShapes) { + const entry = [mk("user", "text", "hello"), mk("assistant", "text", entryText)]; + const exit = [mk("user", "text", "hello"), mk("assistant", "text", exitText)]; + const v = judgeInboundOutbound(entry, exit); + assert.equal(v.ok, true, `${family} must not read as a rewrite`); + } +}); + +test("PR-D closure: prose payload survives the normalization on BOTH sides (paired prose-wearing tags keep content)", () => { + // A prose-wearing pair keeps its payload when stripped — entry and exit + // carrying the SAME prose (with/without the tag bytes) must still match. + const entry = [mk("user", "text", "hello"), mk("assistant", "text", "\x3cACP tokens='2'\x3ereal content\x3c/ACP\x3e")]; + const exit = [mk("user", "text", "hello"), mk("assistant", "text", "real content")]; + assert.equal(judgeInboundOutbound(entry, exit).ok, true); +}); + +test("PR-D closure: a REAL mid-history rewrite still flags through the echo normalizer", () => { + // The closure must not blind the judge: arbitrary prose change in the + // middle of history is still a mid-history-rewrite violation. + const entry = [mk("user", "text", "hello"), mk("assistant", "text", "original words"), mk("user", "text", "and then?")]; + const exit = [mk("user", "text", "hello"), mk("assistant", "text", "rewritten words \x3cACP tokens='1'\x3em00420\x3c/ACP\x3e"), mk("user", "text", "and then?")]; + const v = judgeInboundOutbound(entry, exit); + assert.equal(v.ok, false); + assert.ok(v.violations.some((x) => x.kind === "mid-history-rewrite")); +});