From aba73fc8490e3ebd4dd3fe5c961871eabf6567b1 Mon Sep 17 00:00:00 2001 From: ci-check Date: Fri, 6 Nov 2026 15:06:01 +0800 Subject: [PATCH 1/3] fix(conflict-watch): stop recovered id-churn-adoption from lighting the conflict banner #4131 added the `id-churn-adoption` ConflictKind (fold-reconcile re-mints and re-anchors ids across builds, 0 unmatched, NO compaction boundary) but only wired the recording side. Both display classifiers still treated it as an unexplained signal: - web banner severity (bili_conflictSeverity) dumped it into the residual signalN, so a pure id-churn-adoption ledger read landed=true and lit the alarm banner even though the event is fully recovered by design (#3235 policy: recovered stays quiet). - formatConflictSection framed a pure id-churn-adoption ledger as "something OUTSIDE bili touched this conversation" + "[suspected] confirm each named plugin", pointing the user at a phantom second compressor. Carve id-churn-adoption out of signalN (it joins the neutral/recovered bucket) and give formatConflictSection a dedicated churnAdoptOnly header + advice ("bili itself re-minted and fully re-anchored the ids; no second compressor, nothing to act on"). Mixed ledgers still alarm on genuine unannounced-rewrite/orphan signals. Behavior change (old -> new): a session whose only conflict evidence is a recovered id-churn-adoption event PREVIOUSLY showed the web alarm banner and the /acp panel framed it as an outside rewrite; NOW it stays quiet (banner hidden) and is framed correctly. Still recorded in acp_status/stats. Scope: this PR is deliberately limited to the id-churn-adoption variant of the recurring "Compression conflict banner" symptom. The unannounced-rewrite variant (dsh/claude native-compaction landings) is a different mechanism and still alarms by design pending separate attribution work -- untouched here. Closes #5786 (cherry picked from commit 4a96bad4c72e8f595f3afc15a2d7f744ad935dee) (cherry picked from commit 3c7adee5f31dd95f7298bf41fc5c14870d3ebf75) --- src/conflict-watch.ts | 18 +++++++++++++++++- src/web/client.ts | 8 ++++++-- tests/conflict-lifecycle.test.ts | 27 +++++++++++++++++++++++++++ tests/web-conflict-banner.test.ts | 18 ++++++++++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-) diff --git a/src/conflict-watch.ts b/src/conflict-watch.ts index 43006f9f3f..1b4889e8c9 100644 --- a/src/conflict-watch.ts +++ b/src/conflict-watch.ts @@ -203,10 +203,17 @@ export function formatConflictSection(events: ConflictEvent[], now: number = Dat // landing was fully rebased onto (#2372/#2432) — handled, no ongoing risk. Gated to the // pure case so mixed ledgers keep their existing tier verbatim. const handledNativeOnly = meaningful.length > 0 && meaningful.every((e) => e.kind === "native-compaction"); + // #5786: a ledger whose every meaningful event is a cross-build id re-mint that + // fold-reconcile fully RE-ANCHORED (#4131/#3642) is bili's own recovery — no second + // compressor, no compaction boundary, no data loss. Gated to the pure case so mixed + // ledgers keep their existing tier verbatim (same discipline as #2912/#2802-review). + const churnAdoptOnly = meaningful.length > 0 && meaningful.every((e) => e.kind === "id-churn-adoption"); const activeConfirmed = confirmed.some((e) => now - e.at <= CONFLICT_ACTIVE_WINDOW_MS); lines.push(`COMPRESSION CONFLICTS — ${events.length} event(s) in this session (${active.length} active · ${historical.length} historical; active = within ${CONFLICT_ACTIVE_WINDOW_MS / 86_400_000} days). ${foreignConfirmed || nativePresent ? "Two compressors on one conversation (bili + another compression plugin — third-party or bili's own sibling — or client native compaction) double-compress and corrupt message refs:" - : "Diagnostic evidence that something outside bili touched this conversation — how strong that evidence is is tiered below:"}`); + : churnAdoptOnly + ? "Recorded evidence that bili itself re-minted message ids across builds and fully re-anchored them (no second compressor, no data loss):" + : "Diagnostic evidence that something outside bili touched this conversation — how strong that evidence is is tiered below:"}`); for (const e of events.slice(-10)) { lines.push(` [${fmtTime(e.at)}] ${e.kind} — ${e.detail}`); } @@ -247,6 +254,15 @@ export function formatConflictSection(events: ConflictEvent[], now: number = Dat advice = active.length > 0 ? "The events above are client-native compaction landings bili ALREADY rebased onto (#2372/#2432): each detected checkpoint rebuilt the fold base, so there is no ongoing double-compression risk and nothing to disable. If you did not expect these sessions to auto-compact, review the client's own auto-compaction setting; otherwise clear this ledger — Web UI conflict banner / session page, or POST /__bili/conflicts/clear?session=." : "All events above are client-native compaction landings older than 7 days (historical stock) that bili already rebased onto (#2372/#2432): handled at the time, no ongoing risk. Clear this ledger — Web UI conflict banner / session page, or POST /__bili/conflicts/clear?session=."; + } else if (churnAdoptOnly) { + // #5786: bili's own cross-build id re-mint, fully adopted by fold-reconcile (every + // covered id re-anchored, 0 unmatched) with NO compaction boundary marked — the fold + // state survived intact. Not a second compressor and not auto-compaction: nothing to + // hunt for or disable. Say so plainly instead of routing the user into the + // [suspected]-plugin verify-first errand below. + advice = active.length > 0 + ? "The events above are cross-build id re-mints that bili itself adopted (#4131/#3642): fold-reconcile re-anchored every covered id (0 unmatched) and marked NO compaction boundary, so the fold state survived intact. There is NO second compressor and NO auto-compaction to disable — nothing to act on. Clear this ledger if you want it gone — Web UI conflict banner / session page, or POST /__bili/conflicts/clear?session=." + : "All events above are cross-build id re-mints older than 7 days (historical stock) that bili already adopted (#4131/#3642): the fold state was re-anchored intact at the time, no ongoing risk. Clear this ledger — Web UI conflict banner / session page, or POST /__bili/conflicts/clear?session=."; } else if (orphanOnly) { // A headerless replay does not prove that the original client moved. // Older ledgers may contain tail-only matches; neither proves state loss. diff --git a/src/web/client.ts b/src/web/client.ts index 5ca025d133..af56b2af2c 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -299,7 +299,11 @@ export const WEB_CLIENT = `(function () { // as an unexplained rewrite (warned-on-impact policy, #2912). const confirmedNativeN = kinds["native-compaction-inferred"] || 0; const handledNativeN = kinds["native-compaction"] || 0; - const signalN = Math.max(0, c.events - pluginN - confirmedNativeN - handledNativeN); + // #5786: cross-build id re-mint that fold-reconcile fully RE-ANCHORED (#4131/#3642) — + // bili's own recovery, no compaction boundary marked. Recovered stock like + // handledNativeN: not a landed-unrecovered signal, so it must not light the alarm. + const churnAdoptN = kinds["id-churn-adoption"] || 0; + const signalN = Math.max(0, c.events - pluginN - confirmedNativeN - handledNativeN - churnAdoptN); const whatParts = []; if (confirmedTpN > 0) whatParts.push(t("conflict.what_plugin")); if (suspectedN > 0) whatParts.push(t("conflict.what_suspected")); @@ -333,7 +337,7 @@ export const WEB_CLIENT = `(function () { // #2430/#2545: a ledger naming ONLY bili's own siblings or verified read-only viewers // stands down completely — compatible family / non-compressors, not conflicts. const siblingOnly = !hasConfirmed && suspectedN === 0 && signalN === 0 && siblingN > 0; - const neutralOnly = !hasConfirmed && suspectedN === 0 && signalN === 0 && (siblingN + displayOnlyN + handledNativeN) > 0; // #2912: fully-handled (recovered-native-only) ledger also stands down + const neutralOnly = !hasConfirmed && suspectedN === 0 && signalN === 0 && (siblingN + displayOnlyN + handledNativeN + churnAdoptN) > 0; // #2912/#5786: fully-handled (recovered-native / adopted-id-churn-only) ledger also stands down // #3235 owner rule: the banner is an ALARM for interception FAILURE — evidence a // history rewrite actually LANDED unrecovered (confirmed third-party / inferred // native landing / unexplained rewrite signal). Co-residence alone (suspected diff --git a/tests/conflict-lifecycle.test.ts b/tests/conflict-lifecycle.test.ts index 2952efbfc9..86e796a4d6 100644 --- a/tests/conflict-lifecycle.test.ts +++ b/tests/conflict-lifecycle.test.ts @@ -117,3 +117,30 @@ test("formatConflictSection: header age split + advice switches with liveness (# assert.ok(susText.includes("Every event above is [suspected]")); assert.ok(!susText.includes("Keep exactly ONE compressor")); }); + +test("formatConflictSection: pure id-churn-adoption is bili's own recovery — no plugin hunt, no imperative (#5786/#4131)", () => { + const now = Date.now(); + // The reporter's ledger from #5786: a single active, fully-re-anchored adoption. + const adopt: ConflictEvent[] = [ + { at: now - 60000, kind: "id-churn-adoption", detail: "980/2967 incoming message(s) carry pre-turn refs of 2960 known; fold-reconcile re-anchored every covered id (0 unmatched)" }, + ]; + const text = formatConflictSection(adopt, now).join("\n"); + assert.ok(text.includes("bili itself re-minted message ids across builds"), text); + assert.ok(!text.includes("something outside bili touched this conversation"), "not framed as external interference"); + assert.ok(text.includes("cross-build id re-mints that bili itself adopted"), text); + assert.ok(text.includes("nothing to act on"), "a recovered adoption demands no corrective action"); + assert.ok(!text.includes("Every event above is [suspected]"), "no verify-first plugin errand"); + assert.ok(!text.includes("Keep exactly ONE compressor"), "no imperative without confirmed evidence"); + assert.ok(!text.includes("disable the client's own auto-compaction"), "no per-client auto-compaction command for a self-recovery"); +}); + +test("formatConflictSection: mixed id-churn-adoption + rewrite keeps the unconfirmed-signal tier verbatim (#5786)", () => { + const now = Date.now(); + const mixed: ConflictEvent[] = [ + { at: now - 60000, kind: "id-churn-adoption", detail: "adopted churn" }, + { at: now - 30000, kind: "unannounced-rewrite", detail: "fresh rewrite" }, + ]; + const text = formatConflictSection(mixed, now).join("\n"); + assert.ok(text.includes("UNCONFIRMED signal"), "the genuine rewrite still drives the unconfirmed tier"); + assert.ok(!text.includes("cross-build id re-mints that bili itself adopted"), "pure-case framing must not leak into a mixed ledger"); +}); diff --git a/tests/web-conflict-banner.test.ts b/tests/web-conflict-banner.test.ts index 6f46bad5c7..9222ec305f 100644 --- a/tests/web-conflict-banner.test.ts +++ b/tests/web-conflict-banner.test.ts @@ -340,6 +340,24 @@ test("#3235 landed tier: recovered-only ledger stays quiet; a rewrite signal alo assert.strictEqual(rewrite.landed, true, "an observed history rewrite is a landed event by definition"); }); +test("#5786 recovered cross-build id re-mint (id-churn-adoption) stays quiet — not a landed signal (#4131/#3642)", () => { + const sev = bannerSeverity(); + // The reporter's exact ledger shape from #5786: one active id-churn-adoption event. + const r = sev({ events: 1, kinds: { "id-churn-adoption": 1 }, active: 1 }); + assert.strictEqual(r.hasConfirmed, false); + assert.strictEqual(r.signalN, 0, "a fully-re-anchored adoption is not an unexplained rewrite"); + assert.strictEqual(r.landed, false, "recovered id churn must not light the alarm banner"); + assert.strictEqual(r.neutralOnly, true, "adopted-id-churn-only ledger stands down like recovered native-compaction"); +}); + +test("#5786 mixed ledger: a genuine rewrite still alarms; adopted id-churn adds no false weight", () => { + const sev = bannerSeverity(); + const mixed = sev({ events: 2, kinds: { "id-churn-adoption": 1, "unannounced-rewrite": 1 }, active: 2 }); + assert.strictEqual(mixed.signalN, 1, "only the rewrite counts as a signal, not the adopted churn"); + assert.strictEqual(mixed.onKey, "conflict.on_unconfirmed", "the genuine rewrite drives the unconfirmed tier"); + assert.strictEqual(mixed.landed, true, "an observed history rewrite is a landed event by definition"); +}); + test("#3235 banner gate + dismiss + report buttons wired in the client source", () => { assert.ok(WEB_CLIENT.includes("sev0.landed && !dismissed"), "gate requires landed-unrecovered evidence and no live dismissal"); assert.ok(WEB_CLIENT.includes('id="conflicts-dismiss-btn"'), "dismiss button rendered on the banner"); From dab17c2f3c783b1a1622154ee10f0ef2724d0739 Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 12 Nov 2026 13:34:00 +0800 Subject: [PATCH 2/3] fix(conflict-watch): attribute Claude Code native compaction on the anthropic lane rewrites the client history in place; the next request replays [continuation summary, retained tail...] against the same session id. The anthropic lane's compaction-landing detection knew only the dsh checkpoint framing, so the landing fell through to detectUnannouncedHistoryRewrite and recorded "unannounced-rewrite" (signal tier), lighting the conflict banner for the client's own auto-compact. The anthropic lane now detects the CC continuation framing with the same three-gate discipline as the dsh detector (#2621/#4438): carrier (user/text), novelty (fresh id vs the pre-turn ref map), standalone (sentence opens the message). On a hit it records "native-compaction" (handled tier) and rebases the ACP state onto the compacted history - the same marker+rebase treatment as the dsh lane (#2432/#2658) and the codex lane (#2373). The framing check is lane-scoped (dsh profile or claude identity/client); the sentence is version-pinned like DSH_CHECKPOINT_PREAMBLE_PREFIX (#970) - if Claude Code rewords it, detection degrades to the pre-#5767 archive path, never a false positive. Tests: drift pin on the pinned sentence; carrier/novelty unit gates; e2e positive (claude session rebases, no unannounced-rewrite, replay does not re-trigger) + four e2e negatives (no framing / assistant quote / old id / non-claude lane). Fixes #5767 (cherry picked from commit 0d9231c517908baf4c1d92efe40bcf7c9fadfe0f) --- src/server/claude-compaction-guard.ts | 83 ++++ src/server/prepare-anthropic.ts | 58 ++- ...issue5767-claude-native-compaction.test.ts | 354 ++++++++++++++++++ 3 files changed, 476 insertions(+), 19 deletions(-) create mode 100644 src/server/claude-compaction-guard.ts create mode 100644 tests/issue5767-claude-native-compaction.test.ts diff --git a/src/server/claude-compaction-guard.ts b/src/server/claude-compaction-guard.ts new file mode 100644 index 0000000000..8a0ab3b121 --- /dev/null +++ b/src/server/claude-compaction-guard.ts @@ -0,0 +1,83 @@ +// #5767: Claude Code native-compaction landing detector (anthropic lane). +// +// Claude Code's auto-compact (CLAUDE_CODE_AUTO_COMPACT_WINDOW) rewrites the +// client's history in place: the shadowed region is replaced by ONE +// continuation user message that opens with the verbatim sentence below, and +// the next request replays [continuation summary, retained tail…] against the +// SAME session id. The #5767 ledger shows the landing shape — 19–24 incoming +// messages, 5–9 carrying pre-turn refs, of 415–2312 known — a region +// replacement the generic rewrite detector can see (the gap) but cannot +// attribute (no framing it knows), so it recorded "unannounced-rewrite" and +// lit the alarm banner for the client's own auto-compact. The dsh marker path +// (#2432/#2658) and the codex local-compaction path (#2373) already settle the +// same landing shape as handled "native-compaction"; this detector gives the +// claude lane the same treatment. +// +// Version-pinned like DSH_CHECKPOINT_PREAMBLE_PREFIX (#970): if Claude Code +// rewords the continuation sentence, detection degrades to a MISS — the +// pre-#5767 unannounced-rewrite archive path — never a false positive. +export const CLAUDE_COMPACTION_CONTINUATION_PREFIX = + "This session is being continued from a previous conversation that ran out of context"; + +type Rec = Record; + +/** Concatenate a wire content field (string | parts array) into plain text. */ +function textOfContent(content: unknown): string { + if (typeof content === "string") return content; + if (!Array.isArray(content)) return ""; + let out = ""; + for (const part of content) { + if (typeof part === "string") out += part; + else if (part !== null && typeof part === "object" && typeof (part as Rec).text === "string") { + out += (part as Rec).text; + } + } + return out; +} + +/** True when a freshly-LANDED Claude Code native-compaction continuation + * message is present in the resent history. Same three-gate discipline as + * carriesDshLocalCompactionSummary (#2621/#4438): + * + * ① CARRIER — role="user" AND plain text (contentType "text" when typed). + * A tool_result riding a wire role:"user" message flattens to + * role="tool"/contentType="tool-result" (kernel anthropicToCore), so the + * role gate alone already drops it; the contentType gate is kept as + * belt-and-suspenders for any lane that maps a structured user block to + * a non-text contentType. + * ② NOVELTY — the message's raw id must NOT already be in the pre-turn ref + * map (`knownIds` = Object.keys(session.state.messageRefs.byRaw) taken + * BEFORE processTurn). A freshly-landed continuation is by definition a + * NEW message; a historical quote/replay hits an OLD id. + * ③ STANDALONE — the message must START with the continuation sentence; a + * quote embeds it inside other content. Consequence accepted per the + * version-pin doctrine (header): if Claude Code rewords the sentence, + * detection degrades to a miss — never a false positive wiping live fold + * state. + * + * Reads the core-flattened text first (BiliMessage/CoreMessage carry prose in + * `.text`), falling back to a raw wire content field for direct callers. When + * `knownIds` is omitted the novelty gate is skipped (carrier gate only) — + * in-lane callers pass it. */ +export function carriesClaudeNativeCompactionSummary( + msgs: readonly { id?: unknown; role?: unknown; contentType?: unknown; text?: unknown; content?: unknown }[], + knownIds?: ReadonlySet, +): boolean { + for (const m of msgs) { + // ① CARRIER (#2621): only a user-role plain-text message can be a + // continuation summary. Exclude every other producer of the sentence. + if (m.role !== "user" || (m.contentType !== undefined && m.contentType !== "text")) continue; + // ② NOVELTY (#2621): a freshly-landed continuation is a NEW id; a + // quoted or replayed mention is an old id already in the pre-turn ref + // map. + if (knownIds !== undefined && typeof m.id === "string" && knownIds.has(m.id)) continue; + const text = typeof m.text === "string" && m.text !== "" ? m.text : textOfContent(m.content); + if (text === "") continue; + // ③ STANDALONE (#4438): the sentence must open the message — see the + // gate list above for why mid-text matching is a false-positive + // machine on synthesized aux blobs that quote old summaries. + const head = text.trimStart(); + if (head.startsWith(CLAUDE_COMPACTION_CONTINUATION_PREFIX)) return true; + } + return false; +} diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index e840165aa6..e0b1317d66 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -20,6 +20,7 @@ import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { absorbAbortedTurnGrowth, nudgeStampsOf, restoreNudgeStamps, applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { getStore } from "../persist.js"; +import { carriesClaudeNativeCompactionSummary } from "./claude-compaction-guard.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL, RULE_TOOL, absorbToolsFor, blockOpsTools, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote } from "../compress-tool.js"; @@ -32,7 +33,7 @@ import { filterDeadRanges } from "../dead-ranges.js"; import { detectGenerationBoundary } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; import { estimateCoreMessages, estimateCoreMessagesUpper, extractBillingAttributionBlock } from "../preflight.js"; -import { dshNativeCompactionWitness, recordConflict } from "../conflict-watch.js"; +import { conflictClientOf, dshNativeCompactionWitness, recordConflict } from "../conflict-watch.js"; import { anthropicToolsCarryCacheControl, computeAnthropicMessageMarks, countAnthropicSystemCacheControls, countAnthropicToolsCacheControls, pinClientMessageMarks, stampAnthropicSystemCacheControl } from "../loop/cache-control.js"; import { reconcileSystemAnchor } from "../system-anchor.js"; import { isStrictReasoningEcho, modelIdOf } from "../strict-echo.js"; @@ -332,28 +333,46 @@ export async function prepareAnthropic( // skipped like on the openai lane (same tiny-budget heuristic). // #6061: honour a boundary armed on an earlier request. The detector in // generation-boundary.ts arms it when stale above-frontier citations prove - // the persisted ledger is from a previous generation; the dsh branch below - // only fires on a client-side compaction signature, which a plain + // the persisted ledger is from a previous generation; the dsh/claude branch + // below only fires on a client-side compaction signature, which a plain // model/upstream switch does not produce — that left the two surfaces (nudge // vs compress) permanently disagreeing about "highest allocated ref". if (reconcileNativeCompactionBoundary(session)) { log("info", `[${sessionId}] [acp-gen-boundary] ref ledger belongs to a previous generation — ACP compression state rebased onto the current one`); } - let dshRebased = false; + // #5767: Claude Code's auto-compact (CLAUDE_CODE_AUTO_COMPACT_WINDOW) + // lands the SAME shape — [continuation summary, retained tail…] + // against the same session id — but its framing is the CC continuation + // sentence, which the dsh detector does not know, so the landing fell + // through to detectUnannouncedHistoryRewrite below and lit the alarm + // banner for the client's own auto-compact. Same marker+rebase + // treatment as the dsh lane; the framing check is lane-scoped (a dsh + // session never matches the claude sentence and vice versa). + let nativeRebased = false; const anthropicTitleGen = typeof parsed.max_tokens === "number" && parsed.max_tokens <= 200; - if (!anthropicTitleGen && laneDecisions.profile.compactsClientSide && 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) || carriesBiliSnapshotCheckpoint(msgs))) { - const missing = dshGap.expected - dshGap.matched; - if (missing >= DSH_LOCAL_COMPACTION_MIN_MISSING && missing * 2 >= dshGap.expected) { + const dshLane = laneDecisions.profile.compactsClientSide; + const claudeLane = laneDecisions.profile.identity === "claude-responses" || conflictClientOf(session) === "claude"; + if (!anthropicTitleGen && (dshLane || claudeLane) && session.state.blocks.some((b) => b.active)) { + const coveredBeforeNativeCompact = coveredRealHistoryIds(session.state.blocks); // #2627: same real-history caliber + const nativeGap = foldCoverage(coveredBeforeNativeCompact, msgs.map((m) => m.id)); + // #2621 novelty gate enforced here (knownRefsBefore = pre-turn ref map). + const dshFramed = dshLane && (carriesDshLocalCompactionSummary(msgs) || carriesBiliSnapshotCheckpoint(msgs)); + const claudeFramed = claudeLane && carriesClaudeNativeCompactionSummary(msgs, knownRefsBefore); + if (nativeGap && (dshFramed || claudeFramed)) { + const missing = nativeGap.expected - nativeGap.matched; + if (missing >= DSH_LOCAL_COMPACTION_MIN_MISSING && missing * 2 >= nativeGap.expected) { // #3733: our own tag = authored checkpoint. - const authored = carriesBiliSnapshotCheckpoint(msgs); + const authored = dshFramed && carriesBiliSnapshotCheckpoint(msgs); if (authored) session.metadata.dshAuthoredCheckpointLanded = true; - recordConflict(session, "native-compaction", `dsh native compaction: ${missing}/${dshGap.expected} covered id(s) replaced by the compacted history; ACP state rebased (#2432)`); - log(authored ? "info" : "warn", `[${sessionId}] dsh ${authored ? "AUTHORED checkpoint landed" : "native compaction detected"} (${dshGap.matched}/${dshGap.expected} covered id(s) retained, checkpoint framing in resent history) — ${authored ? "rebasing ACP state onto the authored bili-snapshot checkpoint (#3733)" : "rebasing ACP state onto the compacted history (#2432)"}`); + if (claudeFramed) { + recordConflict(session, "native-compaction", `claude native compaction: ${missing}/${nativeGap.expected} covered id(s) replaced by the compacted history; ACP state rebased (#5767)`); + log("warn", `[${sessionId}] claude native compaction detected (${nativeGap.matched}/${nativeGap.expected} covered id(s) retained, continuation framing in resent history) — rebasing ACP state onto the compacted history (#5767)`); + } else { + recordConflict(session, "native-compaction", `dsh native compaction: ${missing}/${nativeGap.expected} covered id(s) replaced by the compacted history; ACP state rebased (#2432)`); + log(authored ? "info" : "warn", `[${sessionId}] dsh ${authored ? "AUTHORED checkpoint landed" : "native compaction detected"} (${nativeGap.matched}/${nativeGap.expected} covered id(s) retained, checkpoint framing in resent history) — ${authored ? "rebasing ACP state onto the authored bili-snapshot checkpoint (#3733)" : "rebasing ACP state onto the compacted history (#2432)"}`); + } markNativeCompactionBoundary(session); - dshRebased = reconcileNativeCompactionBoundary(session); + nativeRebased = reconcileNativeCompactionBoundary(session); } } } @@ -455,11 +474,12 @@ export async function prepareAnthropic( // #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. 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) { + // the rewrite was just classified as a marker-detected native + // compaction above (#2432/#2658 dsh, #5767 claude): 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 (!nativeRebased) { const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id), reconcileOutcome); if (rewrite.churnExplained) { // #4131: cross-build id re-mint (#3642) — fold-reconcile found every diff --git a/tests/issue5767-claude-native-compaction.test.ts b/tests/issue5767-claude-native-compaction.test.ts new file mode 100644 index 0000000000..2f28c04ce3 --- /dev/null +++ b/tests/issue5767-claude-native-compaction.test.ts @@ -0,0 +1,354 @@ +// #5767: Claude Code's auto-compact (CLAUDE_CODE_AUTO_COMPACT_WINDOW) rewrites +// the client's history in place: the shadowed region is replaced by ONE +// continuation user message opening with the verbatim sentence "This session +// is being continued from a previous conversation that ran out of context", +// and the next request replays [continuation summary, retained tail…] against +// the SAME session id. Before this fix the anthropic lane's compaction-landing +// detection knew only the dsh checkpoint framing, so the landing fell through +// to detectUnannouncedHistoryRewrite → "unannounced-rewrite" (signal tier) and +// the conflict banner alarmed for the client's own auto-compact. The anthropic +// lane now detects the CC continuation framing (three gates: carrier / novelty +// / standalone — the same discipline as the dsh detector, #2621/#4438), +// records "native-compaction" (handled tier) and rebases the ACP state onto +// the compacted history — the same marker+rebase treatment as the dsh lane +// (#2432/#2658) and the codex lane (#2373). + +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST = "0"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { getSession, _resetSessionsForTest } from "../src/session.ts"; +import { conflictEventsOf } from "../src/conflict-watch.ts"; +import { carriesClaudeNativeCompactionSummary, CLAUDE_COMPACTION_CONTINUATION_PREFIX } from "../src/server/claude-compaction-guard.ts"; + +const MODEL = "claude-test"; +const SYSTEM = "You are Claude Code, an interactive CLI tool."; + +type Rig = { + proxyPort: number; + upstreamPort: number; + bodies: string[]; + close: () => Promise; +}; + +function anthropicSse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +async function startRig(compress: { injectTool: boolean; injectNudge?: boolean }): Promise { + const bodies: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + bodies.push(raw); + // Report prompt tokens proportional to the payload so the token + // baseline moves between turns (a constant report pins it). + const promptTokens = Math.max(1, Math.round(raw.length / 4)); + res.writeHead(200, { "content-type": "text/event-stream" }); + res.write(anthropicSse("message_start", { type: "message_start", message: { id: `msg_${bodies.length}`, role: "assistant", usage: { input_tokens: promptTokens } } })); + res.write(anthropicSse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })); + res.write(anthropicSse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })); + res.write(anthropicSse("content_block_stop", { type: "content_block_stop", index: 0 })); + res.write(anthropicSse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 2 } })); + res.write(anthropicSse("message_stop", { type: "message_stop" })); + res.end(); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetSessionsForTest(); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { [MODEL]: { context: 30_000 } } } }, + modelContextLimit: 30_000, + kernelConfig: defaultConfig(30_000), + compress, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return { + proxyPort: (proxy.address() as { port: number }).port, + upstreamPort, + bodies, + close: async () => { + proxy.close(); + proxy.closeIdleConnections?.(); + await once(proxy, "close"); + upstream.close(); + upstream.closeIdleConnections?.(); + await once(upstream, "close"); + }, + }; +} + +const messagesUrl = (rig: Rig) => `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`; + +function claudeHeaders(conv: string): Record { + return { "content-type": "application/json", "x-claude-code-session-id": conv }; +} + +function post(rig: Rig, headers: Record, messages: unknown[]): Promise { + return fetch(messagesUrl(rig), { + method: "POST", + headers, + body: JSON.stringify({ model: MODEL, max_tokens: 1024, stream: true, system: SYSTEM, messages }), + }); +} + +// 14 messages sized like the codex #2372 fixture: the kernel protects the +// last 5 messages, so folding m00001–m00009 covers 9 ids (> the 8-id floor). +function seedInput() { + return Array.from({ length: 14 }, (_, i) => ({ + role: i % 2 === 0 ? "user" : "assistant", + content: `Message ${i} of the folded conversation. ` + `FILLER_${i}_content_`.repeat(230), + })); +} + +function continuationMessage() { + return { + role: "user", + content: `${CLAUDE_COMPACTION_CONTINUATION_PREFIX}. The summary below covers the earlier portion of the conversation.\n\n## Primary Request and Intent\n- drive a fold, then replay the compacted view`, + }; +} + +const FOLD_SUMMARY = "MAIN-SUMMARY-SETUP-CONTEXT-FOLDED-BY-COMPRESSION-LONG-ENOUGH-FOR-KERNEL-MIN-LENGTH-CHECK"; + +async function seedAndFold(rig: Rig, conv: string, headers: Record): Promise { + const r1 = await post(rig, headers, seedInput()); + assert.equal(r1.status, 200, "seed request succeeds"); + await r1.text(); + const s = getSession(conv); + assert.ok(s, `seed session exists under the key "${conv}"`); + assert.ok(Object.keys(s!.state.messageRefs.byRaw).length >= 14, "refs assigned to the seeded history"); + + const r2 = await fetch(`http://127.0.0.1:${rig.proxyPort}/__bili/plugin/tool`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ conversationId: conv, tool: "compress", args: { content: [{ startId: "m00001", endId: "m00009", summary: FOLD_SUMMARY }] } }), + }); + const j = (await r2.json()) as { ok: boolean; result?: string }; + assert.ok(j.ok, `fold accepted (${r2.status})`); + assert.ok(!j.result?.includes("[Compression FAILED"), `fold succeeded: ${j.result?.slice(0, 200)}`); + const covered = new Set((s!.state.blocks ?? []).flatMap((b) => (b.active ? b.effectiveMessageIds : []))); + assert.ok(covered.size >= 8, `fold covers >=8 ids (got ${covered.size})`); +} + +test("drift pin: claude continuation sentence is versioned (#5767)", () => { + // The verbatim opening of Claude Code's auto-compact continuation message + // (quoted in the #4168 report). If Claude Code rewords it, detection + // degrades to the pre-#5767 unannounced-rewrite archive path — never a + // false positive. This pin makes the drift visible in CI. + assert.equal(CLAUDE_COMPACTION_CONTINUATION_PREFIX, "This session is being continued from a previous conversation that ran out of context"); +}); + +test("unit: carriesClaudeNativeCompactionSummary matches framing only on a user/text carrier (#2621)", () => { + const QUOTE = `the transcript literally contains "${CLAUDE_COMPACTION_CONTINUATION_PREFIX}" at line 40`; + // Positive: a freshly-landed continuation is a user-role plain-text + // message whose text opens with the sentence. (No knownIds set here → + // novelty gate skipped, so these exercise the carrier gate alone.) + assert.equal(carriesClaudeNativeCompactionSummary([]), false, "empty history"); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "user", contentType: "text", text: "plain prose" }]), false); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "user", contentType: "text", text: CLAUDE_COMPACTION_CONTINUATION_PREFIX + ". The summary below…" }]), true, "continuation sentence at message start"); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "user", contentType: "text", text: `intro\n\n${CLAUDE_COMPACTION_CONTINUATION_PREFIX}. The summary below…` }]), false, "#4438: sentence embedded MID-message is a quote, not a landing"); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "user", contentType: "text", content: `see ${CLAUDE_COMPACTION_CONTINUATION_PREFIX} below` }]), false, "mid-text quote via raw wire content is not a landing"); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "assistant", contentType: "text", text: `checking: ${QUOTE}` }]), false, "assistant prose quoting the sentence"); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "assistant", contentType: "reasoning", text: QUOTE }]), false, "assistant reasoning quoting the sentence"); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "user", contentType: "tool-result", text: QUOTE }]), false, "tool result flattened to role=user (anthropic lane) quoting the sentence"); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "tool", contentType: "tool-result", text: QUOTE }]), false, "openai role=tool result quoting the sentence"); + assert.equal(carriesClaudeNativeCompactionSummary([{ role: "assistant", contentType: "tool-call", text: QUOTE }]), false, "assistant tool-call args quoting the sentence"); + assert.equal(carriesClaudeNativeCompactionSummary([{ text: QUOTE }]), false, "role-less bare shape reads false (safe miss, #2621 direction)"); +}); + +test("unit: carriesClaudeNativeCompactionSummary novelty gate — old ids never match (#2621)", () => { + const framed = `${CLAUDE_COMPACTION_CONTINUATION_PREFIX}. The summary below covers the earlier portion.\n\n## section`; + // A user/text message carrying the sentence but whose id is ALREADY in the + // pre-turn ref map is a replay/quote, not a fresh landing → no match. + assert.equal( + carriesClaudeNativeCompactionSummary([{ id: "old", role: "user", contentType: "text", text: framed }], new Set(["old"])), + false, "an already-seen user/text replay of the continuation is not a landing"); + // The identical framed message with a FRESH id (absent from the pre-turn + // map) matches. + assert.equal( + carriesClaudeNativeCompactionSummary([{ id: "fresh", role: "user", contentType: "text", text: framed }], new Set(["other"])), + true, "a fresh user/text message carrying the continuation is a landing"); + // Omitting knownIds skips the novelty gate (carrier gate only) — the + // framed user/text message still matches, preserving the direct-caller + // contract. + assert.equal( + carriesClaudeNativeCompactionSummary([{ id: "whatever", role: "user", contentType: "text", text: framed }]), + true, "no knownIds set → novelty gate skipped, carrier gate decides"); +}); + +test("e2e #5767: claude replaying [continuation, retained tail] rebases the ACP state instead of lighting the conflict banner", async () => { + const rig = await startRig({ injectTool: true, injectNudge: false }); + try { + const conv = "cc-5767"; + await seedAndFold(rig, conv, claudeHeaders(conv)); + const s = getSession(conv)!; + assert.ok(s.state.blocks.some((b) => b.active), "fold produced an active block"); + + // CC auto-compact lands: the shadowed region is replaced by the + // continuation summary; the retained tail is re-sent. + const r = await post(rig, claudeHeaders(conv), [continuationMessage(), seedInput()[12]!, seedInput()[13]!]); + assert.equal(r.status, 200, "compaction-landing request succeeds"); + const raw = await r.text(); + assert.ok(raw.includes("message_stop"), "stream completed"); + + const events = conflictEventsOf(s); + const native = events.find((e) => e.kind === "native-compaction"); + assert.ok(native, `native-compaction recorded (events: ${JSON.stringify(events.map((e) => e.kind))})`); + assert.ok(native!.detail.startsWith("claude native compaction"), `claude attribution: ${native!.detail}`); + assert.equal(events.find((e) => e.kind === "unannounced-rewrite"), undefined, "no unannounced-rewrite beside the native-compaction attribution"); + + // Rebased: the pre-compaction fold state is gone; the ref space is + // re-seeded with the high-water pinned (no ref re-issue). + assert.equal(s.state.blocks.filter((b) => b.active).length, 0, "active blocks cleared by the rebase"); + const boundary = s.metadata.nativeCompactionBoundary as { pendingRebase?: boolean } | undefined; + assert.equal(boundary?.pendingRebase, false, "boundary marked AND reconciled"); + assert.ok(typeof s.metadata.nativeCompactionAt === "number", "nativeCompactionAt stamped"); + assert.ok(Object.keys(s.state.messageRefs.byRaw).length >= 3, "ref map re-seeded from the retained tail"); + + // The forwarded body carries the continuation summary (wire untouched). + const fwd = rig.bodies[rig.bodies.length - 1]!; + assert.ok(fwd.includes(CLAUDE_COMPACTION_CONTINUATION_PREFIX), "continuation summary forwarded upstream"); + + // Post-compaction turns keep working (no cannot-be-anchored death + // spiral) and the replayed continuation (now an OLD id) does not + // re-trigger the rebase. + const r2 = await post(rig, claudeHeaders(conv), [continuationMessage(), seedInput()[12]!, seedInput()[13]!, { role: "user", content: "what did we decide about the harness?" }]); + assert.equal(r2.status, 200, "post-compaction turn succeeds"); + await r2.text(); + const events2 = conflictEventsOf(s); + assert.equal(events2.filter((e) => e.kind === "native-compaction").length, 1, "the replayed continuation (old id) does not re-trigger the rebase"); + } finally { + await rig.close(); + } +}); + +test("e2e #5767 negative: decimated history WITHOUT the continuation framing does not rebase (no false positive)", async () => { + const rig = await startRig({ injectTool: true, injectNudge: false }); + try { + const conv = "cc-5767-noframe"; + await seedAndFold(rig, conv, claudeHeaders(conv)); + const s = getSession(conv)!; + + // Same decimation shape, but the replacement message is plain prose + // (no continuation sentence) — an unknown host or a user edit. Must + // NOT rebase: a framing-less gap stays on the existing paths. + const r = await post(rig, claudeHeaders(conv), [{ role: "user", content: "Here is a summary of what we covered so far: we set up the harness and drove a fold." }, seedInput()[12]!, seedInput()[13]!]); + assert.equal(r.status, 200, "framing-less decimation request succeeds"); + await r.text(); + + const events = conflictEventsOf(s); + assert.equal(events.find((e) => e.kind === "native-compaction"), undefined, "no native-compaction without the framing"); + assert.equal(s.metadata.nativeCompactionBoundary, undefined, "no boundary marked — no rebase"); + } finally { + await rig.close(); + } +}); + +test("e2e #5767 negative: continuation sentence QUOTED by an assistant message does not rebase (#2621)", async () => { + const rig = await startRig({ injectTool: true, injectNudge: false }); + try { + const conv = "cc-5767-quote"; + await seedAndFold(rig, conv, claudeHeaders(conv)); + const s = getSession(conv)!; + + // The model quotes the sentence in its own prose while the history is + // decimated. The carrier gate must reject it — rebasing on a quote + // would destroy live fold state with no host compaction behind it. + const r = await post(rig, claudeHeaders(conv), [ + { role: "assistant", content: `FYI the host injected this: "${CLAUDE_COMPACTION_CONTINUATION_PREFIX}. The summary below covers the earlier portion of the conversation."` }, + seedInput()[12]!, seedInput()[13]!, + ]); + assert.equal(r.status, 200, "assistant-quote request succeeds"); + await r.text(); + + const events = conflictEventsOf(s); + assert.equal(events.find((e) => e.kind === "native-compaction"), undefined, "an assistant quote of the sentence is not a landing"); + assert.equal(s.metadata.nativeCompactionBoundary, undefined, "no boundary marked — no rebase"); + } finally { + await rig.close(); + } +}); + +test("e2e #5767 negative: a continuation message already seen (old id) does not rebase (#2621 novelty)", async () => { + const rig = await startRig({ injectTool: true, injectNudge: false }); + try { + const conv = "cc-5767-oldid"; + await seedAndFold(rig, conv, claudeHeaders(conv)); + const s = getSession(conv)!; + + // Turn 1: the continuation lands alongside the INTACT history (no + // gap) — it is just a new user message; it gets a ref. + const r1 = await post(rig, claudeHeaders(conv), [...seedInput(), continuationMessage()]); + assert.equal(r1.status, 200, "continuation-with-intact-history request succeeds"); + await r1.text(); + assert.equal(conflictEventsOf(s).find((e) => e.kind === "native-compaction"), undefined, "no gap → no rebase on turn 1"); + + // Turn 2: the compacted replay now includes the SAME continuation + // (old id) + a decimated tail. The novelty gate must reject it. + const r2 = await post(rig, claudeHeaders(conv), [continuationMessage(), seedInput()[12]!, seedInput()[13]!]); + assert.equal(r2.status, 200, "replayed-continuation request succeeds"); + await r2.text(); + + const events = conflictEventsOf(s); + assert.equal(events.find((e) => e.kind === "native-compaction"), undefined, "a replayed continuation (old id) is not a landing"); + assert.equal(s.metadata.nativeCompactionBoundary, undefined, "no boundary marked — no rebase"); + } finally { + await rig.close(); + } +}); + +test("e2e #5767 negative: the claude framing on a NON-claude session does not rebase (lane gate)", async () => { + const rig = await startRig({ injectTool: true, injectNudge: false }); + try { + const conv = "cc-5767-foreign"; + // A generic session (x-acp-session key, no claude header, no claude + // UA): the lane gate must reject the claude framing for a foreign + // client even when the decimation shape is identical. + const genericHeaders = { "content-type": "application/json", "x-acp-session": conv }; + await seedAndFold(rig, conv, genericHeaders); + const s = getSession(conv)!; + + const r = await post(rig, genericHeaders, [continuationMessage(), seedInput()[12]!, seedInput()[13]!]); + assert.equal(r.status, 200, "foreign-session framing request succeeds"); + await r.text(); + + const events = conflictEventsOf(s); + assert.equal(events.find((e) => e.kind === "native-compaction"), undefined, "the claude framing never matches on a non-claude lane"); + assert.equal(s.metadata.nativeCompactionBoundary, undefined, "no boundary marked — no rebase"); + } finally { + await rig.close(); + } +}); From 6fb8805ef96fb6d24ac06ef4b1062b041fe9d57e Mon Sep 17 00:00:00 2001 From: ci-check Date: Thu, 12 Nov 2026 23:25:50 +0800 Subject: [PATCH 3/3] docs(architecture): list claude-compaction-guard.ts in the src/ map (#5767) The PR added src/server/claude-compaction-guard.ts but the architecture map (reference/architecture.md) did not list it, so the architecture-docs CI lane failed with 'missing: src/server/claude-compaction-guard.ts'. Add the entry next to its sibling dsh-compaction-guard.ts. --- reference/architecture.md | 1 + 1 file changed, 1 insertion(+) diff --git a/reference/architecture.md b/reference/architecture.md index 88f79aa24d..23403d9a1e 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -15,6 +15,7 @@ billion-context/ │ │ ├── admin.ts # /__bili/* + /__acp/* admin route surface (stats/status/overview/sessions/detail/logs/config/plugin/watcher/upstream endpoints), loopback + trusted-origin gated (#1440) │ │ ├── budget.ts # chars/4 overhead measure outside the kernel fold space │ │ ├── chain-artifacts.ts # Allocation-free byte pre-filter for chain artifacts (#1421) +│ │ ├── claude-compaction-guard.ts # Claude Code native-compaction landing detector (anthropic lane) (#5767) │ │ ├── codex-catalog-cache.ts # Same-credential, bounded offline model metadata for Codex discovery │ │ ├── context-window.ts # Context-window resolution (launcher model channel, beta headers) │ │ ├── dsh-compaction-guard.ts # Rejects dsh whole-prefix compaction replays (#1729)