diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 545db594e2..1bf0482fc0 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -205,7 +205,10 @@ export async function prepareAnthropic( if (!anthropicTitleGen && session.metadata["pluginAgent"] === "dsh" && session.state.blocks.some((b) => b.active)) { const coveredBeforeDshCompact = coveredRealHistoryIds(session.state.blocks); // #2627: same real-history caliber const dshGap = foldCoverage(coveredBeforeDshCompact, msgs.map((m) => m.id)); - if (dshGap && carriesDshLocalCompactionSummary(msgs)) { + // #2621: pass the pre-turn ref map so the framing match also demands + // NOVELTY — a freshly-landed checkpoint is a NEW id, while every + // historical quote/replay of the marker string hits an old id. + if (dshGap && carriesDshLocalCompactionSummary(msgs, knownRefsBefore)) { const missing = dshGap.expected - dshGap.matched; if (missing >= DSH_LOCAL_COMPACTION_MIN_MISSING && missing * 2 >= dshGap.expected) { recordConflict(session, "native-compaction", `dsh native compaction: ${missing}/${dshGap.expected} covered id(s) replaced by the compacted history; ACP state rebased (#2432)`); diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 414b900311..0ae700e838 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -131,16 +131,20 @@ export async function prepareGoogle( // checkpoint framing in resent history AND decimated fold coverage; // detection runs BEFORE reconcileFoldCoverage/processTurn. No known // dsh+google traffic yet — this closes the same hole on the fourth wire. + let dshRebased = false; if (!isTitleGen && session.metadata["pluginAgent"] === "dsh" && session.state.blocks.some((b) => b.active)) { const coveredBeforeDshCompact = coveredRealHistoryIds(session.state.blocks); // #2627: same real-history caliber const dshGap = foldCoverage(coveredBeforeDshCompact, msgs.map((m) => m.id)); - if (dshGap && carriesDshLocalCompactionSummary(msgs)) { + // #2621: pass the pre-turn ref map so the framing match also demands + // NOVELTY — a freshly-landed checkpoint is a NEW id, while every + // historical quote/replay of the marker string hits an old id. + if (dshGap && carriesDshLocalCompactionSummary(msgs, knownRefsBefore)) { const missing = dshGap.expected - dshGap.matched; if (missing >= DSH_LOCAL_COMPACTION_MIN_MISSING && missing * 2 >= dshGap.expected) { recordConflict(session, "native-compaction", `dsh native compaction: ${missing}/${dshGap.expected} covered id(s) replaced by the compacted history; ACP state rebased (#2432)`); log("warn", `[${sessionId}] dsh native compaction detected (${dshGap.matched}/${dshGap.expected} covered id(s) retained, checkpoint framing in resent history) — rebasing ACP state onto the compacted history (#2432)`); markNativeCompactionBoundary(session); - reconcileNativeCompactionBoundary(session); + dshRebased = reconcileNativeCompactionBoundary(session); } } } @@ -185,8 +189,12 @@ log("info", diagTagSummary(turn.messages, sessionId, renderStrategy)); processedMessages = stripKernelSummaries(turn.messages, turn.state); // #1001: a silent client history rewrite takes the same archive+prune path // as an announced /compact boundary — syncBlocks above has already - // deactivated the blocks whose sources left the context. - { + // deactivated the blocks whose sources left the context. Skipped when + // the rewrite was just classified as dsh native compaction above + // (#2432/#2658): it is already recorded as "native-compaction" and + // rebased — a second "unannounced-rewrite" entry beside it would present + // the substrate destruction as "another compressor fighting you". + if (!dshRebased) { const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id)); if (rewrite.detected) { log("warn", `[${sessionId}] unannounced client history rewrite detected (${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known) — marking compaction boundary (#1001)`); diff --git a/tests/issue2658-dsh-anthropic-google-compaction.test.ts b/tests/issue2658-dsh-anthropic-google-compaction.test.ts index da0d3ff61b..67d1325d84 100644 --- a/tests/issue2658-dsh-anthropic-google-compaction.test.ts +++ b/tests/issue2658-dsh-anthropic-google-compaction.test.ts @@ -15,6 +15,10 @@ // (pluginAgent === "dsh" + framing + decimated coverage) is shared across all // four lanes, so those shapes are re-pinned here per wire only where the lane // adds its own twist (the anthropic unannounced-rewrite double-record skip). +// #2775 adds the #2621 novelty-gate negative per wire: a SEEDED user message +// quoting the framing, replayed byte-identical (old id) under real fold drift, +// must not rebase — pre-#2775 neither lane passed knownRefsBefore, so the +// carrier gate alone fired on the quote. import assert from "node:assert/strict"; import http from "node:http"; @@ -320,6 +324,8 @@ test("e2e #2658: dsh replaying [checkpoint, retained tail] on generateContent re await r.text(); assertRebased(conv, rig); + const s = getSession(conv)!; + assert.ok(!conflictEventsOf(s).some((e) => e.kind === "unannounced-rewrite"), "no double-record as unannounced-rewrite beside the native-compaction entry (#2775)"); // The session keeps working afterwards (append + forward). const r2 = await fetch(rig.url, { method: "POST", headers: dshHeaders(conv), body: JSON.stringify({ ...replay(), contents: [...replay().contents as unknown[], { role: "user", parts: [{ text: "Post-compaction turn continues fine." }] }] }) }); @@ -384,3 +390,74 @@ test("negative #2658: google decimated history WITHOUT checkpoint framing stays await rig.close(); } }); + +test("negative #2775: anthropic seeded user quote replayed byte-identical + real fold gap does NOT rebase", async () => { + const rig = await startAnthropicRig(); + const conv = "dshc-ant-quoteold"; + try { + // The quote sits inside the folded span (m00005): the decimated replay + // below retains exactly 1 of the 9 covered ids → missing = 8 (≥ the 8-id + // floor, missing*2 ≥ expected) — signal ② fully armed by plain drift. + const quoteText = `${DSH_CHECKPOINT_PREAMBLE_PREFIX} that I pasted from another session's transcript for reference — not a compaction boundary here.`; + const seedMessages = Array.from({ length: 14 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: i === 4 ? quoteText : seedText(i), + })); + await seedAndFold(rig, conv, { model: MODEL_ANTHROPIC, max_tokens: 8192, stream: true, messages: seedMessages }); + + const r = await fetch(rig.url, { + method: "POST", + headers: dshHeaders(conv), + body: JSON.stringify({ + model: MODEL_ANTHROPIC, max_tokens: 8192, stream: true, + messages: [ + { role: "user", content: quoteText }, + { role: "user", content: seedText(12) }, + { role: "assistant", content: seedText(13) }, + ], + }), + }); + assert.equal(r.status, 200); + await r.text(); + const s = getSession(conv)!; + assert.ok(s.metadata.nativeCompactionBoundary === undefined, "a byte-identical replay of an already-seen user quote must NOT trigger a native-compaction rebase (#2775/#2621)"); + assert.ok(!conflictEventsOf(s).some((e) => e.kind === "native-compaction"), "no misattributed native-compaction conflict recorded (#2775)"); + assert.ok((s.state.blocks ?? []).some((b) => b.active), "the live fold survives the drift — no rebase reset happened"); + } finally { + await rig.close(); + } +}); + +test("negative #2775: google seeded user quote replayed byte-identical + real fold gap does NOT rebase", async () => { + const rig = await startGoogleRig(); + const conv = "dshc-gem-quoteold"; + try { + const quoteText = `${DSH_CHECKPOINT_PREAMBLE_PREFIX} that I pasted from another session's transcript for reference — not a compaction boundary here.`; + const seedContents = Array.from({ length: 14 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "model", + parts: [{ text: i === 4 ? quoteText : seedText(i) }], + })); + await seedAndFold(rig, conv, { model: MODEL_GOOGLE, generationConfig: { maxOutputTokens: 8192 }, contents: seedContents }); + + const r = await fetch(rig.url, { + method: "POST", + headers: dshHeaders(conv), + body: JSON.stringify({ + model: MODEL_GOOGLE, generationConfig: { maxOutputTokens: 8192 }, + contents: [ + { role: "user", parts: [{ text: quoteText }] }, + { role: "user", parts: [{ text: seedText(12) }] }, + { role: "model", parts: [{ text: seedText(13) }] }, + ], + }), + }); + assert.equal(r.status, 200); + await r.text(); + const s = getSession(conv)!; + assert.ok(s.metadata.nativeCompactionBoundary === undefined, "a byte-identical replay of an already-seen user quote must NOT trigger a native-compaction rebase (#2775/#2621)"); + assert.ok(!conflictEventsOf(s).some((e) => e.kind === "native-compaction"), "no misattributed native-compaction conflict recorded (#2775)"); + assert.ok((s.state.blocks ?? []).some((b) => b.active), "the live fold survives the drift — no rebase reset happened"); + } finally { + await rig.close(); + } +});