From 4cb446bdbd1231e84d1a66ca3952245338b230ec Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 17:14:28 +0800 Subject: [PATCH 1/2] fix: adopt cross-build id churn instead of destroying fold state when fold-reconcile fully re-anchors (#4131) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The #1001 unannounced-rewrite detector judged history rewrites on raw-id hash identity alone, outside the order-centric reconcile ladder. A bili upgrade across v0.1.201 re-mints non-text message ids by design (#3642 D1 accepted this one-time churn and promised the reconcile ladder would absorb it), so on the first turn after a cross-version restart the detector saw the collapse, marked a compaction boundary anyway, and applyCompactionArchive pruned the ref map + tombstoned hundreds of still-cited refs — destroying fold coverage for a pure transform (incident: 212 covered refs re-anchored with 0 unmatched, then pruned to 392 live ids). detectUnannouncedHistoryRewrite now takes this pass's fold-reconcile verdict: when kind is reanchored/resend with unmatched === 0, the collapse is explained churn — the destructive boundary is suppressed and an id-churn-adoption conflict event + info log are recorded instead. Off/noop verdicts carry no evidence and keep legacy behavior; partial claims (unmatched > 0) keep the legacy escalation path unchanged. Second facet (issue expectation #4): applyCompactionArchive now names in its log line how many refs the prune drops and which block coverages they sit under, so any remaining destructive prune is auditable instead of silent. Tests: tests/issue4131-cross-version-churn-gate.test.ts pins both directions (explained churn suppressed; genuine rewrite still escalates; off/noop verdicts do not suppress; legacy callers unchanged) plus the log facet. --- src/conflict-watch.ts | 2 +- src/fold-reconcile.ts | 2 +- src/server/prepare-anthropic.ts | 11 +- src/server/prepare-google.ts | 11 +- src/server/prepare-openai.ts | 14 +- src/session.ts | 36 +++- ...issue4131-cross-version-churn-gate.test.ts | 161 ++++++++++++++++++ 7 files changed, 222 insertions(+), 15 deletions(-) create mode 100644 tests/issue4131-cross-version-churn-gate.test.ts diff --git a/src/conflict-watch.ts b/src/conflict-watch.ts index 4384032534..cb25ff9fbc 100644 --- a/src/conflict-watch.ts +++ b/src/conflict-watch.ts @@ -13,7 +13,7 @@ import { markDirty, type Session } from "./session.js"; import { isCodexClient } from "./codex-compact.js"; import { isDisplayOnlyConflictDetail, isSiblingConflictDetail } from "./thirdparty-scan.js"; -type ConflictKind = "third-party-plugin" | "unannounced-rewrite" | "orphan-reap" | "native-compaction" | "native-compaction-inferred" | "identity-orphan"; +type ConflictKind = "third-party-plugin" | "unannounced-rewrite" | "orphan-reap" | "native-compaction" | "native-compaction-inferred" | "identity-orphan" | "id-churn-adoption"; export interface ConflictEvent { at: number; diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index 0bfd631eae..7c6e2de66f 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -389,7 +389,7 @@ interface ReconciliationPlan { claimedChurnTokens?: number; } -interface FoldReconcileResult { +export interface FoldReconcileResult { kind: "off" | "noop" | "resend" | "reanchored" | "unmatched"; missing: number; claims: number; diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index aafb3fb290..b2c76fb000 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -289,7 +289,7 @@ export async function prepareAnthropic( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); + const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); noteSystemPromptFingerprint(session, clientSystem, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. @@ -373,8 +373,13 @@ export async function prepareAnthropic( // 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) { + 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 + // covered id on the wire; adopt instead of destroying. + recordConflict(session, "id-churn-adoption", `${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known; fold-reconcile re-anchored every covered id (0 unmatched) — bili-side id re-mint across builds (#3642), not a client rewrite — no compaction boundary (#4131)`); + log("info", `[${sessionId}] cross-build id churn adopted: hash-identity collapse (${rewrite.knownIncoming}/${rewrite.incomingTotal} pre-turn refs of ${rewrite.knownBefore} known) explained by fold-reconcile full re-anchor (0 unmatched) — skipping compaction boundary (#4131/#3642)`); + } else if (rewrite.detected) { // #2709: attribute a framing-absent rewrite to client-native compaction when bili witnessed the client's own compaction being refused — stops the phantom-second-compressor errand. const dshWitness = dshNativeCompactionWitness(session, { incomingTotal: rewrite.incomingTotal, knownBefore: rewrite.knownBefore }); log("warn", `[${sessionId}] ${dshWitness !== undefined ? "client-native compaction landing (framing absent; attributed from refusal ledger)" : "unannounced client history rewrite"} detected (${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known) — marking compaction boundary (#1001${dshWitness !== undefined ? "/#2709" : ""})`); diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 0cd9ed03c9..01546c47c7 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -174,7 +174,7 @@ export async function prepareGoogle( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); + const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. @@ -247,8 +247,13 @@ export async function prepareGoogle( // as an announced /compact boundary — syncBlocks above has already // deactivated the blocks whose sources left the context. { - const rewrite = detectUnannouncedHistoryRewrite(session, knownRefsBefore, msgs.map((m) => m.id)); - if (rewrite.detected) { + 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 + // covered id on the wire; adopt instead of destroying. + recordConflict(session, "id-churn-adoption", `${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known; fold-reconcile re-anchored every covered id (0 unmatched) — bili-side id re-mint across builds (#3642), not a client rewrite — no compaction boundary (#4131)`); + log("info", `[${sessionId}] cross-build id churn adopted: hash-identity collapse (${rewrite.knownIncoming}/${rewrite.incomingTotal} pre-turn refs of ${rewrite.knownBefore} known) explained by fold-reconcile full re-anchor (0 unmatched) — skipping compaction boundary (#4131/#3642)`); + } else 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)`); recordConflict(session, "unannounced-rewrite", `${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known`); markCompactionBoundary(session); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 6b825e22e9..bdeccd7926 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -181,8 +181,9 @@ export async function prepareOpenai( } // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client - // re-serialization stay covered (src/fold-reconcile.ts). - reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); + // re-serialization stay covered (src/fold-reconcile.ts). The verdict + // feeds the #4131 rewrite gate below (explained churn ≠ destruction). + const reconcileOutcome = reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. @@ -270,8 +271,13 @@ export async function prepareOpenai( // 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) { + 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 + // covered id on the wire; adopt instead of destroying. + recordConflict(session, "id-churn-adoption", `${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known; fold-reconcile re-anchored every covered id (0 unmatched) — bili-side id re-mint across builds (#3642), not a client rewrite — no compaction boundary (#4131)`); + log("info", `[${sessionId}] cross-build id churn adopted: hash-identity collapse (${rewrite.knownIncoming}/${rewrite.incomingTotal} pre-turn refs of ${rewrite.knownBefore} known) explained by fold-reconcile full re-anchor (0 unmatched) — skipping compaction boundary (#4131/#3642)`); + } else if (rewrite.detected) { // #2709: attribute a framing-absent rewrite to client-native compaction when bili witnessed the client's own compaction being refused — stops the phantom-second-compressor errand. const dshWitness = dshNativeCompactionWitness(session, { incomingTotal: rewrite.incomingTotal, knownBefore: rewrite.knownBefore }); log("warn", `[${sessionId}] ${dshWitness !== undefined ? "client-native compaction landing (framing absent; attributed from refusal ledger)" : "unannounced client history rewrite"} detected (${rewrite.knownIncoming}/${rewrite.incomingTotal} incoming message(s) carry pre-turn refs of ${rewrite.knownBefore} known) — marking compaction boundary (#1001${dshWitness !== undefined ? "/#2709" : ""})`); diff --git a/src/session.ts b/src/session.ts index 8a0b5d1e89..b9504a4bd9 100644 --- a/src/session.ts +++ b/src/session.ts @@ -10,6 +10,7 @@ import { durableMessageToolPaths, withDurableProtection } from "./durable-messag import type { AnonymousProvenance } from "./anon-provenance.js"; import type { WireProtocol } from "./util.js"; import type { InboundSlot, SeamSlot } from "./cache-ledger.js"; +import type { FoldReconcileResult } from "./fold-reconcile.js"; export type BlockView = { text: string; count: number }; @@ -1453,6 +1454,14 @@ export function applyCompactionArchive( } const { byRaw, byRef } = session.state.messageRefs; + // #4131: name what the prune costs — ref(s) about to be dropped that still + // sit under some block's coverage, so silent coverage loss is auditable. + const droppedIds = new Set(Object.keys(byRaw).filter((id) => id !== REF_FLOOR_RAW_ID && !liveRawIds.has(id))); + let coverageNote = ""; + if (droppedIds.size > 0) { + const affectedBlocks = session.state.blocks.filter((b) => (b.effectiveMessageIds ?? []).some((id) => droppedIds.has(id))).map((b) => b.blockId); + coverageNote = `; ${droppedIds.size} ref(s) dropped${affectedBlocks.length > 0 ? `, covering block(s) ${affectedBlocks.join(", ")}` : ""}`; + } pruneRefsToLiveIds(session, liveRawIds, byRaw, byRef); session.metadata.compactionBoundary = { @@ -1462,7 +1471,7 @@ export function applyCompactionArchive( archivedBlocks: deactivated, }; markDirty(session); - log("info", `[${session.id}] native compaction boundary: archived ${deactivated.length} pre-compaction block(s)${deactivated.length > 0 ? ` (${deactivated.join(", ")})` : ""}; pruned ref maps to ${liveRawIds.size} live raw id(s)`); + log("info", `[${session.id}] native compaction boundary: archived ${deactivated.length} pre-compaction block(s)${deactivated.length > 0 ? ` (${deactivated.join(", ")})` : ""}; pruned ref maps to ${liveRawIds.size} live raw id(s)${coverageNote}`); } /** Prune the ref maps to `liveRawIds` in place (same maps, not new objects: @@ -1574,12 +1583,19 @@ interface RewriteDetection { knownBefore: number; incomingTotal: number; knownIncoming: number; + /** #4131: the ratio collapse was explained by this pass's fold-reconcile + * finding EVERY covered id on the wire (present or re-anchored, zero + * unmatched) — bili's own sanctioned id churn across builds (#3642 D1), + * not a client rewrite. The destructive boundary is suppressed and + * callers record an adoption event instead of marking one. */ + churnExplained: boolean; } export function detectUnannouncedHistoryRewrite( session: Session, knownRefsBefore: ReadonlySet, liveRawIds: Iterable, + reconcile?: Pick | null, ): RewriteDetection { const knownBefore = knownRefsBefore.size - (knownRefsBefore.has(REF_FLOOR_RAW_ID) ? 1 : 0); let incomingTotal = 0; @@ -1588,14 +1604,28 @@ export function detectUnannouncedHistoryRewrite( incomingTotal++; if (knownRefsBefore.has(id)) knownIncoming++; } - const detected = + const rawDetected = knownBefore >= REWRITE_MIN_KNOWN_REFS && session.state.blocks.length > 0 && incomingTotal >= REWRITE_MIN_INCOMING_TOTAL && knownIncoming > 0 && knownIncoming / incomingTotal < REWRITE_MAX_KNOWN_RATIO && knownIncoming / knownBefore < REWRITE_MAX_KNOWN_RATIO; - return { detected, knownBefore, incomingTotal, knownIncoming }; + // #4131: order-centric doctrine — no single dimension may veto the + // multi-evidence verdict. When this pass's fold-reconcile found EVERY + // covered id on the wire (kind reanchored/resend, unmatched === 0), the + // hash-identity collapse is explained churn: bili's own cross-build id + // re-mint (#3642 D1 accepted exactly this once-per-upgrade mass churn, + // promising the reconcile ladder would absorb it). Marking the destructive + // compaction boundary here instead tombstones hundreds of still-cited + // refs (#4131 incident). Off/noop verdicts (reconcile disabled, side-request + // skip) carry no evidence either way → legacy behavior unchanged; partial + // claims (unmatched > 0) keep the legacy escalation path. + const churnExplained = rawDetected + && reconcile !== undefined && reconcile !== null + && (reconcile.kind === "reanchored" || reconcile.kind === "resend") + && reconcile.unmatched === 0; + return { detected: rawDetected && !churnExplained, knownBefore, incomingTotal, knownIncoming, churnExplained }; } /** #1195: message ids are content hashes — if a client resends history whose diff --git a/tests/issue4131-cross-version-churn-gate.test.ts b/tests/issue4131-cross-version-churn-gate.test.ts new file mode 100644 index 0000000000..f44fcabb54 --- /dev/null +++ b/tests/issue4131-cross-version-churn-gate.test.ts @@ -0,0 +1,161 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; + +process.env.NODE_ENV = "test"; + +// #4131: cross-version restart (v0.1.197 → v0.1.202, kernel 0.0.122 → 0.0.136) +// re-mints non-text message ids by design (#3642 D1 accepted the one-time mass +// churn and promised the reconcile ladder would absorb it). The #1001 rewrite +// detector ran OUTSIDE that ladder on raw-id hash identity alone, marked a +// compaction boundary anyway, and applyCompactionArchive pruned the ref map + +// tombstoned hundreds of still-cited refs — destroying fold coverage for a +// transform. Gate discipline: both directions asserted — explained churn is +// suppressed, genuine destruction still escalates, legacy callers unchanged. + +import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { applyCompactionArchive, detectUnannouncedHistoryRewrite, markCompactionBoundary } from "../src/session.ts"; +import { reconcileFoldCoverage } from "../src/fold-reconcile.ts"; + +const noLog = (_level: string, _msg: string): void => {}; + +function makeSession(): Session { + return { + id: `test-${Math.random().toString(36).slice(2)}`, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +// 24 real messages → processTurn assigns m00001..m00024 → compress the first +// half so the session carries one active block (same shape as +// tests/fix-1001-history-rewrite.test.ts). +function sessionWithBlockAndRefs(): { session: Session; blockId: string; all: CoreMessage[] } { + const session = makeSession(); + const core = createCore(); + const config = defaultConfig(200000); + const msgs: CoreMessage[] = []; + for (let i = 0; i < 24; i++) { + msgs.push({ id: `h_distinct${i}`, role: i % 2 === 0 ? "user" : "assistant", contentType: "text", text: `message ${i} ${"x".repeat(2000)}` }); + } + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const res = core.applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00012", summary: "compressed early history that is long enough to pass the min summary length check" }], + messages: turn.messages, + state: turn.state, + config, + }); + session.state = res.state; + return { session, blockId: session.state.blocks[session.state.blocks.length - 1].blockId, all: msgs }; +} + +const repair = () => ({ mode: "repair" as const, sessionId: "s-4131", log: noLog }); + +// #3642 incident shape: bili's own cross-build id re-mint — bytes unchanged, +// ~2/3 of message ids re-minted (non-text cores), order preserved. +function remint(all: CoreMessage[]): CoreMessage[] { + return all.map((m, i) => (i % 3 === 0 ? m : { ...m, id: `v2_${m.id}` })); +} + +test("#4131 POSITIVE: cross-build id re-mint with full re-anchor does NOT mark a destructive boundary", () => { + const { session, all } = sessionWithBlockAndRefs(); + const knownBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); + assert.equal(knownBefore.size, 24); + + // seed anchors/order metadata with a clean pass over the original history + // (production does this on every pre-restart turn) + const pass1 = reconcileFoldCoverage(session, all, repair()); + assert.equal(pass1.kind, "resend", `clean first pass: ${JSON.stringify(pass1)}`); + + const reminted = remint(all); + assert.equal(reminted.filter((m, i) => i % 3 !== 0).length, 16, "fixture re-mints 16 of 24 ids"); + + // sanity: the hash-identity collapse alone WOULD trip the legacy detector + const legacy = detectUnannouncedHistoryRewrite(session, knownBefore, reminted.map((m) => m.id)); + assert.equal(legacy.detected, true, `legacy gate must fire on this shape (${JSON.stringify(legacy)})`); + assert.equal(legacy.churnExplained, false); + + // the real verdict from THIS pass's fold-reconcile + const pass2 = reconcileFoldCoverage(session, reminted, repair()); + assert.ok(pass2.kind === "reanchored" || pass2.kind === "resend", `expected full recovery verdict: ${JSON.stringify(pass2)}`); + assert.equal(pass2.unmatched, 0, `every covered id must be found on the wire: ${JSON.stringify(pass2)}`); + + const det = detectUnannouncedHistoryRewrite(session, knownBefore, reminted.map((m) => m.id), pass2); + assert.equal(det.churnExplained, true, `explained churn must be flagged (${JSON.stringify(det)})`); + assert.equal(det.detected, false, "destructive boundary must be suppressed"); + assert.equal(session.metadata.compactionBoundary, undefined, "no boundary recorded"); + assert.equal(session.state.deadRefs, undefined, "no refs tombstoned"); +}); + +test("#4131 NEGATIVE 1: genuine client rewrite (covered substrate gone) still escalates despite the verdict", () => { + const { session, all } = sessionWithBlockAndRefs(); + const knownBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); + reconcileFoldCoverage(session, all, repair()); + + // #1001 shape: client kept the last 8 originals, appended 16 brand-new + // messages — the covered head (m00001–m00012) is truly gone + const rewritten: CoreMessage[] = [ + ...all.slice(16), + ...Array.from({ length: 16 }, (_, k): CoreMessage => ({ id: `h_new${k}`, role: k % 2 === 0 ? "user" : "assistant", contentType: "text", text: `new ${k}` })), + ]; + const pass = reconcileFoldCoverage(session, rewritten, repair()); + assert.ok(pass.unmatched > 0, `covered substrate must stay honestly unmatched: ${JSON.stringify(pass)}`); + + const det = detectUnannouncedHistoryRewrite(session, knownBefore, rewritten.map((m) => m.id), pass); + assert.equal(det.churnExplained, false, `partial loss must not be explained away (${JSON.stringify(det)})`); + assert.equal(det.detected, true, "genuine rewrite keeps the legacy escalation path"); +}); + +test("#4131 NEGATIVE 2: append-only growth is neither detected nor explained", () => { + const { session, all } = sessionWithBlockAndRefs(); + const knownBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); + const appended: CoreMessage[] = [ + ...all, + ...Array.from({ length: 4 }, (_, k): CoreMessage => ({ id: `h_new${k}`, role: k % 2 === 0 ? "user" : "assistant", contentType: "text", text: `new ${k}` })), + ]; + const pass = reconcileFoldCoverage(session, appended, repair()); + assert.equal(pass.kind, "resend"); + const det = detectUnannouncedHistoryRewrite(session, knownBefore, appended.map((m) => m.id), pass); + assert.equal(det.detected, false); + assert.equal(det.churnExplained, false); +}); + +test("#4131 NEGATIVE 3: off/noop verdicts carry no evidence and do NOT suppress the legacy gate", () => { + const { session, all } = sessionWithBlockAndRefs(); + const knownBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); + reconcileFoldCoverage(session, all, repair()); + const reminted = remint(all); + for (const verdict of [ + { kind: "noop" as const, unmatched: 0 }, + { kind: "off" as const, unmatched: 0 }, + null, + undefined, + ]) { + const det = detectUnannouncedHistoryRewrite(session, knownBefore, reminted.map((m) => m.id), verdict); + assert.equal(det.detected, true, `verdict ${String(verdict?.kind ?? "absent")} must not suppress`); + assert.equal(det.churnExplained, false); + } +}); + +test("#4131 fix②: prune log names the dropped refs' block coverage instead of staying silent", () => { + const { session, blockId, all } = sessionWithBlockAndRefs(); + markCompactionBoundary(session); + const logs: string[] = []; + // live set = the tail only: every covered id of the active block is dropped + applyCompactionArchive(session, new Set([blockId]), new Set(all.slice(16).map((m) => m.id)), (l, m) => logs.push(`${l}: ${m}`)); + const line = logs.find((l) => l.includes("native compaction boundary")); + assert.ok(line, `archive log line present: ${logs.join(" | ")}`); + assert.match(line!, /; 16 ref\(s\) dropped, covering block\(s\)/, `coverage cost named in log: ${line}`); + assert.ok(line!.includes(blockId), `affected block id named in log: ${line}`); + // tombstoning behavior itself is unchanged by #4131 + assert.ok(Array.isArray(session.state.deadRefs) && session.state.deadRefs.length > 0, "dropped refs still tombstoned for citation honesty (#3302)"); +}); From 283a2ef1a5623d5884cbdf8407ab34e86b9c387b Mon Sep 17 00:00:00 2001 From: awork Date: Wed, 21 Oct 2026 17:30:22 +0800 Subject: [PATCH 2/2] fix(review): frame id-churn-adoption-only conflict ledgers as bili-own events (#4141 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review of #4141 found that a session whose conflict ledger contains ONLY the new id-churn-adoption events (the exact shape this PR produces on a clean cross-build restart) fell through formatConflictSection into the [suspected]-plugin tier: header claimed 'something outside bili touched this conversation' and the advice told readers to 'confirm each named plugin really compresses' — both false, since the event is bili's own fully-handled re-mint adoption (#3642 D1). Same misframing class as #2802 (orphan-only) and #2912 (handled-native-only). Fix: dedicated adoptionOnly tier in the branch ladder (gated to pure ledgers so mixed ledgers keep their existing wording verbatim) with accurate header + active/historical advice; regression test pins the framing and the absence of plugin-hunt/remediation output. No behavior change beyond the panel/advice text. --- src/conflict-watch.ts | 18 +++++++++++++++++- tests/conflict-remediation.test.ts | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/conflict-watch.ts b/src/conflict-watch.ts index cb25ff9fbc..b7d65f4891 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"); + // #4131 review: an adoption-only ledger is bili's OWN fully-handled cross-build + // id re-mint (#3642 D1) — not external interference; the [suspected]-plugin + // tiers below would misframe it (same class as #2802 orphan-only / #2912 + // handled-native-only). + const adoptionOnly = 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:"}`); + : adoptionOnly + ? "Bili's own cross-build id re-mints (#3642/#4131), adopted with full fold re-anchor — nothing outside bili touched this conversation:" + : "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}`); } @@ -222,6 +229,7 @@ export function formatConflictSection(events: ConflictEvent[], now: number = Dat // foreign confirmed -> strong one-compressor command (live) or verify-then-clear (stock) // native landing (no foreign)-> host-side explanation, no plugin hunt (#2432) // identity-orphan only -> host-lane identity loss, no second compressor (#2802 review) + // id-churn-adoption only -> bili's own re-mint adopted, no external cause (#4131 review) // pure [suspected] -> #1736 soft wording // unconfirmed signals -> "cause not identified" framing, never imperative let advice: string; @@ -247,6 +255,14 @@ 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 (adoptionOnly) { + // #4131 review: bili ADOPTED its own cross-build id re-mint with full fold + // re-anchor — no external cause exists, so neither the [suspected]-plugin + // wording nor any remediation command applies (same misframing class as + // #2802/#2912). + advice = active.length > 0 + ? "The events above are bili's OWN cross-build id re-mints (#3642/#4131): after an upgrade restart bili re-minted non-text message ids and fold-reconcile re-anchored every covered id, so it ADOPTED the re-minted history instead of marking a destructive compaction boundary — there is NO second compressor to hunt for and nothing to disable. If you did not expect this upgrade churn, check your auto-update settings; otherwise clear this ledger — Web UI conflict banner / session page, or POST /__bili/conflicts/clear." + : "All events above are bili's own cross-build id re-mints older than 7 days (historical stock): each was adopted with full fold re-anchor at the time, no ongoing risk. Clear this ledger — Web UI conflict banner / session page, or POST /__bili/conflicts/clear."; } else if (orphanOnly) { // #2802 review: identity-loss takeover (#2791 family) — bili KNOWS what // happened (its own resolver detected the headerless replay of this diff --git a/tests/conflict-remediation.test.ts b/tests/conflict-remediation.test.ts index aa9b590fa5..acb93ffd61 100644 --- a/tests/conflict-remediation.test.ts +++ b/tests/conflict-remediation.test.ts @@ -224,3 +224,25 @@ test("formatConflictSection: mixed orphan + suspected ledger keeps the existing assert.ok(text.includes("confirm each named plugin"), text); assert.ok(text.endsWith(CONFLICT_DOCS_POINTER), text); }); + +test("formatConflictSection: id-churn-adoption-only ledger gets bili-own re-mint framing, not the [suspected]-plugin bucket (#4131 review)", () => { + const adoption = (at: number): ConflictEvent[] => [ + { at, kind: "id-churn-adoption", detail: "95/392 incoming message(s) carry pre-turn refs of 387 known; fold-reconcile re-anchored every covered id (0 unmatched) — bili-side id re-mint across builds (#3642), not a client rewrite — no compaction boundary (#4131)" }, + ]; + + const activeText = formatConflictSection(adoption(Date.now() - 60_000), Date.now(), "opencode").join("\n"); + assert.ok(activeText.includes("bili's OWN cross-build id re-mints"), activeText); + assert.ok(activeText.includes("NO second compressor to hunt for"), activeText); + // the misclassification this test pins: no external-interference header, no + // plugin-hunt advice, no per-client remediation footer on a bili-owned event + assert.ok(!activeText.includes("something outside bili touched"), activeText); + assert.ok(!activeText.includes("Two compressors on one conversation"), activeText); + assert.ok(!activeText.includes("confirm each named plugin"), activeText); + assert.ok(!activeText.includes("Fix (opencode):"), activeText); + assert.ok(!activeText.endsWith(CONFLICT_DOCS_POINTER), activeText); + + const stockText = formatConflictSection(adoption(Date.now() - 30 * DAY), Date.now()).join("\n"); + assert.ok(stockText.includes("historical stock"), stockText); + assert.ok(stockText.toLowerCase().includes("clear this ledger"), stockText); + assert.ok(!stockText.endsWith(CONFLICT_DOCS_POINTER), stockText); +});