Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 17 additions & 1 deletion src/conflict-watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down Expand Up @@ -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=<id>."
: "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=<id>.";
} 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=<id>."
: "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=<id>.";
} 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.
Expand Down
83 changes: 83 additions & 0 deletions src/server/claude-compaction-guard.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>;

/** 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<string>,
): 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;
}
58 changes: 39 additions & 19 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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 <bili-snapshot v1 …/> 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);
}
}
}
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading