diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 5e3db27c70..cec3a68862 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -111,6 +111,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `promptCache.routing` | "auto" \| "enabled" \| "disabled" | auto | ACP_PROMPT_CACHE_ROUTING | Prompt-cache routing posture for cache-aware lane selection. | | `native.attachExternal` | boolean | false | BILI_NATIVE_ATTACH_EXTERNAL | Let launcher-less native plugins attach to an external (lane'd, unarmed) daemon instead of spawning their own. | | `claude.nativePort` | number | unset (lane sticky zone port) | BILI_CLAUDE_NATIVE_PORT | Exact port pin for the claude native lane's hook-spawned proxy (strict-port: squatters are refused loudly). | +| `claude.allowNativeCompaction` | boolean | false | BILI_ALLOW_CLAUDE_COMPACTION | Lift bili's local 403 refusal of Claude Code native compaction calls (manual /compact, precomputed compression, auto-compact behind a non-managed install — final user message carrying the CC summarize template) on every wire lane bili serves — anthropic natively, openai/responses behind CC-router installs (#2612/#2728): when true the call forwards; on success CC rewrites its transcript to the summary while bili's fold ledger still anchors the raw history (unannounced-rewrite desync family), so this is an explicit opt-out of bili-owned compression. Default OFF keeps the guard active; the managed install block separately sets DISABLE_AUTO_COMPACT=1 so auto-compact does not fire under a bili-managed CC. | | `pi.subagents` | object \| boolean | {} (acp_delegate enabled with package defaults) | PI_ACP_DELEGATE_FORCE_ENABLE, PI_ACP_DELEGATE_MAX_DEPTH, PI_ACP_DELEGATE_SYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_IDLE_TIMEOUT_MINUTES, PI_ACP_DELEGATE_ASYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_MAX_CONCURRENT | Built-in pi-lane sub-agents (acp_delegate) surface, #2230 config-home. The pi.subagents section of billion-context.json owns the delegate config; the four acp.json keys (delegate/delegatePrompt/displayUsage/debug) are a deprecated fallback read only while the section is absent. prompt replaces delegatePrompt; debug is scoped to the sub-agent subsystem. Boolean shorthand subagents: false disables the whole surface. Full field table: the pi section in CONFIGURATION.md. | **MITM lane** @@ -682,10 +683,10 @@ Top-level keys that control how the proxy listens and behaves globally. ### `claude` -- **Type:** `{ nativePort?: number }` -- **Default:** `{}` (lane rides the self-managed port zone) +- **Type:** `{ nativePort?: number, allowNativeCompaction?: boolean }` +- **Default:** `{}` (lane rides the self-managed port zone; compaction guard active) - **Status:** ACTIVE -- **Description:** claude native-lane settings. `nativePort` pins an EXACT port for the hook-spawned proxy (#964/#1660): strict-port semantics — a squatter on the port is refused loudly instead of hopping — and the baked managed `ANTHROPIC_BASE_URL` points at it. Without it the lane rides the self-managed zone (`BILI_ZONE_PORT` base + per-lane sticky) and re-pins the managed URL to the live origin each session, so port drift self-heals. Overridden by `BILI_CLAUDE_NATIVE_PORT`. +- **Description:** claude native-lane settings. `nativePort` pins an EXACT port for the hook-spawned proxy (#964/#1660): strict-port semantics — a squatter on the port is refused loudly instead of hopping — and the baked managed `ANTHROPIC_BASE_URL` points at it. Without it the lane rides the self-managed zone (`BILI_ZONE_PORT` base + per-lane sticky) and re-pins the managed URL to the live origin each session, so port drift self-heals. Overridden by `BILI_CLAUDE_NATIVE_PORT`. `allowNativeCompaction` (env `BILI_ALLOW_CLAUDE_COMPACTION`) lifts the wire guard that refuses Claude Code native compaction with 403 (#2612/#2728) — an explicit opt-out of bili-owned compression; see the key table above. ### `native` @@ -1599,6 +1600,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_ADVISORY_CHECK` | `advisoryCheck` | true | | `BILI_ADVISORY_URL` | `advisoryUrl` | unset (built-in feed) | | `BILI_AFFINITY_SIMHASH` | `affinitySimhash` | true | +| `BILI_ALLOW_CLAUDE_COMPACTION` | `claude.allowNativeCompaction` | false | | `BILI_ALLOW_DSH_COMPACTION` | `dsh.allowDshCompaction` | false | | `BILI_CCR_RETRIEVAL_TTL_MS` | `ccrRetrievalTtlMs` | 600000 (0 disables retrieval) | | `BILI_CHAIN_CONTENT` | `chainContentDetection` | false | @@ -1767,6 +1769,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_ZCODE_SIGNING_FIXED` | `1`/`true` = assume your ZCode build fixed ClientRequestSigningV4 (#1621): the per-entry skips for coding-plan accounts on v3.14+ personal stores flip off and they route again. Temporary escape hatch — there is **no** file equivalent. | | `BILI_CLAUDE_UPSTREAM` | claude direct mode: your relay endpoint, when `ANTHROPIC_BASE_URL` already points at a relay the launcher would otherwise bypass. | | `BILI_CLAUDE_NATIVE_PORT` | Pin an **exact** port for the claude native lane's hook-spawned proxy (#964/#1660): strict-port semantics — a squatter on the port is refused loudly instead of hopping — and the baked managed `ANTHROPIC_BASE_URL` points at it. Without it the hook rides the self-managed zone (`BILI_ZONE_PORT` base + per-lane sticky) and re-pins the managed URL to the live origin each session, so port drift self-heals (#1660). | +| `BILI_ALLOW_CLAUDE_COMPACTION` | Claude Code native-compaction handling (#2612/#2728). Default `0`/unset: bili's wire guard refuses CC compaction calls (manual `/compact`, precomputed compression, auto-compact when the managed `DISABLE_AUTO_COMPACT=1` is not in force) locally with 403 — never forwarded, kernel state untouched, CC's transcript stays verbatim and the refusal surfaces through CC's "Error during compaction" — because on success CC rewrites its transcript to the summary while bili's fold ledger still anchors the raw history (the unannounced-rewrite desync family, #2728), and the envelope re-sends the full raw history upstream exactly when things are already broken. Set `1` to forward anyway (same trade as `claude.allowNativeCompaction` in the file; `0` also forces the guard on when the file opted out). | | `BILI_CODEX_COMPACT` | Codex native-compaction handling. Default `intercept`: bili intercepts codex's compaction requests and forges a local handoff to the ACP state when the safety gate passes (transform ok + steady-state usage < 90% of the window + at least one active compressed block) — trigger form forges a 2-frame SSE, endpoint form forges `{output}` — and never contacts upstream. Forged ACP summaries are re-injected as a history-borne handoff message (developer-message fallback) so compressed content stays visible after codex truncates its history. Set `pass` to opt out and forward codex's compaction requests upstream (native compaction backstops). On any gate failure the request passes through untouched. Codex client detection (which traffic this applies to, also used for window clamping and session-identity fingerprinting): the User-Agent must either start with a registered prefix (`codex_cli_rs/`, `codex_exec/`, `codex desktop/` — matched case-insensitively, since variants like Codex Desktop send an initial capital, #1169) or contain a whitespace-delimited component starting with lowercase `codex` (unknown variants such as `codex_sdk_ts/…`, #645 — narrowed from a bare substring to a token-level prefix in #1641 so non-codex clients whose UAs merely mention "codex" mid-token or in parens are no longer misclassified; the fallback stays case-sensitive on purpose so "Codex"-shaped relays are excluded, #1106). | --- diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index f04cd430f7..12677ef1ac 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -111,6 +111,7 @@ | `promptCache.routing` | "auto" \| "enabled" \| "disabled" | auto | ACP_PROMPT_CACHE_ROUTING | 面向缓存感知路由选择的提示词缓存路由姿态。 | | `native.attachExternal` | boolean | false | BILI_NATIVE_ATTACH_EXTERNAL | 允许无启动器的原生插件挂到外部(车道化、未武装看门狗)守护进程,而不是自行派生。 | | `claude.nativePort` | number | unset (lane sticky zone port) | BILI_CLAUDE_NATIVE_PORT | claude 原生车道钩子派生代理的精确端口钉死(严格端口:被占用时响亮拒绝而非跳端口)。 | +| `claude.allowNativeCompaction` | boolean | false | BILI_ALLOW_CLAUDE_COMPACTION | 解除 bili 对 Claude Code 原生压缩调用的本地 403 拒绝(手动 /compact、预计算压缩、非托管安装下的 auto-compact —— 尾部 user 消息携带 CC 摘要模板),覆盖 bili 服务的全部线路 —— CC 原生走 anthropic,经 CC-router 的安装走 openai/responses(#2612/#2728):为 true 时调用照常转发;一旦成功 CC 会把本地转录改写为摘要,而 bili 的折叠账本仍锚定原始历史(无告知改写 / 静默失同步家族),因此这是对 bili 自管压缩的显式 opt-out。默认关闭(守卫生效);托管安装块另行设置 DISABLE_AUTO_COMPACT=1,bili 托管下的 CC 不会触发 auto-compact。 | | `pi.subagents` | object \| boolean | {} (acp_delegate enabled with package defaults) | PI_ACP_DELEGATE_FORCE_ENABLE, PI_ACP_DELEGATE_MAX_DEPTH, PI_ACP_DELEGATE_SYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_IDLE_TIMEOUT_MINUTES, PI_ACP_DELEGATE_ASYNC_TIMEOUT_MINUTES, PI_ACP_DELEGATE_MAX_CONCURRENT | 内置 pi lane 子代理(acp_delegate)面(#2230 配置搬家)。billion-context.json 的 pi.subagents 段拥有 delegate 配置;acp.json 四键(delegate/delegatePrompt/displayUsage/debug)为已废弃回退源,仅段缺失时读取。prompt 取代 delegatePrompt;debug 限定子代理子系统。布尔简写 subagents: false 整体关闭。完整字段表见 CONFIGURATION.md 的 pi 段。 | **MITM 通道** @@ -1598,6 +1599,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 | `BILI_ADVISORY_CHECK` | `advisoryCheck` | true | | `BILI_ADVISORY_URL` | `advisoryUrl` | unset (built-in feed) | | `BILI_AFFINITY_SIMHASH` | `affinitySimhash` | true | +| `BILI_ALLOW_CLAUDE_COMPACTION` | `claude.allowNativeCompaction` | false | | `BILI_ALLOW_DSH_COMPACTION` | `dsh.allowDshCompaction` | false | | `BILI_CCR_RETRIEVAL_TTL_MS` | `ccrRetrievalTtlMs` | 600000 (0 disables retrieval) | | `BILI_CHAIN_CONTENT` | `chainContentDetection` | false | diff --git a/src/config.ts b/src/config.ts index f5633346dd..a12936d4b8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -892,6 +892,25 @@ export type ProxyOptions = { * `{ "dsh": { "allowDshCompaction": true } }` in the config file, or env * BILI_ALLOW_DSH_COMPACTION=1. */ allowDshCompaction?: boolean; + /** #2612/#2728: opt-in to letting Claude Code's native compaction + * (manual /compact, auto-compact, precomputed compression) run through + * bili instead of being refused by the wire guard + * (src/server/claude-compaction-guard.ts, the claude twin of #1729's + * dsh guard). Default OFF (= guard active): a CC compaction envelope + * (final user message = the CC summarize template) is refused locally + * with 403 — not forwarded, kernel state untouched, CC's transcript + * preserved verbatim. When ON the call is forwarded: on success CC + * rewrites its transcript to the summary while bili's fold ledger still + * anchors the raw history — the unannounced-rewrite desync family + * (#2728) — which is why this stays off by default. The managed install + * block separately sets DISABLE_AUTO_COMPACT=1 (applyClaudeManagedBlock, + * "bili owns compression"), so under a bili-managed CC auto-compact does + * not fire at all; the guard is the wire backstop covering manual + * /compact, users who re-enabled the env, and CC installs routed through + * bili without the managed block. Enable with `{ "claude": { + * "allowNativeCompaction": true } }` in the config file, or env + * BILI_ALLOW_CLAUDE_COMPACTION=1. */ + allowClaudeCompaction?: boolean; }; /** #2028: dsh client-lane settings namespace — home for dsh-specific file @@ -1169,6 +1188,15 @@ export function allowDshCompactionState(env: NodeJS.ProcessEnv): { enabled: bool return { enabled: fileAllow, source: fileAllow ? "file" : null }; } +// #2612: resolved claude-native-compaction opt-in state, the claude twin of +// allowDshCompactionState (same "0-off" env semantics; single source of truth +// for loadOptions and future web-config consumers). +export function allowClaudeCompactionState(env: NodeJS.ProcessEnv): { enabled: boolean; source: "env" | "file" | null } { + const fileAllow = loadConfigFile().claude?.allowNativeCompaction === true; + if (env.BILI_ALLOW_CLAUDE_COMPACTION !== undefined) return { enabled: env.BILI_ALLOW_CLAUDE_COMPACTION !== "0", source: "env" }; + return { enabled: fileAllow, source: fileAllow ? "file" : null }; +} + // #1359: provider/model absorb.* overrides apply only to the proxy lane (plugin // lane follows the base block). Warn once per load so the divergence isn't silent. const ABSORB_DIVERGENCE_FIELDS = ["enabled", "toolName", "minToolTokens", "contextThresholdPct", "excludeTools"] as const; @@ -1439,6 +1467,10 @@ export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions // #2028: default OFF keeps the #1729 wire refusal unconditional unless // explicitly opted out — see the Options.allowDshCompaction docstring. allowDshCompaction: (env.BILI_ALLOW_DSH_COMPACTION ?? (fileConfig.dsh?.allowDshCompaction === true ? "1" : "0")) !== "0", + // #2612: default OFF keeps the claude compaction wire guard + // unconditional unless explicitly opted out — see the + // Options.allowClaudeCompaction docstring. + allowClaudeCompaction: (env.BILI_ALLOW_CLAUDE_COMPACTION ?? (fileConfig.claude?.allowNativeCompaction === true ? "1" : "0")) !== "0", }; } @@ -1656,7 +1688,14 @@ type FileConfig = { * claude lane — strict-port semantics (EADDRINUSE fails loud). Undefined * (the default) means the lane's sticky zone port (ZONE_PORT_BASE base). * Env BILI_CLAUDE_NATIVE_PORT wins over the file. */ - claude?: { nativePort?: number }; + claude?: { nativePort?: number; allowNativeCompaction?: boolean }; + /** Claude-native native-compaction opt-in (#2612/#2728), the claude twin + * of dsh.allowDshCompaction (#2028): `"claude": { + * "allowNativeCompaction": true }` in the config file lifts bili's local + * 403 refusal of Claude Code native compaction requests (/compact, + * auto-compact, precomputed compression — final user message carrying + * the CC summarize template). Default OFF (= guard active). Env + * BILI_ALLOW_CLAUDE_COMPACTION wins over the file. */ /** Built-in pi-lane sub-agents (#2230 config-home): the `pi.subagents` * section owns the acp_delegate surface for the pi lane (embedded wiring * src/agent/pi-subagents.ts; the standalone package diff --git a/src/plugin-install.ts b/src/plugin-install.ts index 59d280da3d..169529feb6 100644 --- a/src/plugin-install.ts +++ b/src/plugin-install.ts @@ -577,7 +577,7 @@ export function applyClaudeManagedBlock(settings: Record, opts: if (dac === undefined || dac === null || dac === "1") { if (dac !== "1") { env.DISABLE_AUTO_COMPACT = "1"; - notes.push("env.DISABLE_AUTO_COMPACT=1 (bili owns compression; manual /compact survives)"); + notes.push("env.DISABLE_AUTO_COMPACT=1 (bili owns compression; manual /compact is refused by the wire guard, #2612)"); } } else { notes.push(`env.DISABLE_AUTO_COMPACT left untouched (foreign value ${JSON.stringify(dac)})`); diff --git a/src/server/claude-compaction-guard.ts b/src/server/claude-compaction-guard.ts new file mode 100644 index 0000000000..69e0fe0dd8 --- /dev/null +++ b/src/server/claude-compaction-guard.ts @@ -0,0 +1,106 @@ +/** + * #2612/#2728: Claude Code native compaction guard. + * + * Claude Code's own compaction — manual /compact, auto-compact (when the + * managed block's DISABLE_AUTO_COMPACT=1 is not in force), and precomputed + * compression — sends a summarize request whose FINAL user message carries the + * fixed instruction template (CC 2.1.x): "Wrap your summary in + * tags" + "REMINDER: Do NOT call any tools. Respond with + * plain text only — an block followed by a block." + * Letting that call execute through bili has no good outcome (#2612/#2728): + * + * - Success → CC rewrites its local transcript to the summary while bili's + * fold ledger still anchors the raw history: an unannounced history + * rewrite. The three read faces split (status shows blocks that no longer + * match what CC sends, search misses, decompress returns text CC can no + * longer place) and the session drifts into the silent-desync family. + * - The moment users actually reach for it is the moment things are already + * broken (folds lost, context re-inflated): the request re-sends the FULL + * raw history (multi-M tokens upstream — 400 or real money) and even its + * compliant reply used to be broken by the #2303 draft-tail retry + * (fixed for the forwarding path by #2619). + * + * So the call is refused locally, mirroring the dsh twin (#1729/#2193, + * src/server/dsh-compaction-guard.ts): never forwarded, kernel state + * untouched, CC keeps its transcript verbatim and shows the refusal through + * its own "Error during compaction" surface. The managed install block + * already sets DISABLE_AUTO_COMPACT=1 (applyClaudeManagedBlock), so + * auto-compact does not fire under a bili-managed CC; this guard is the wire + * backstop covering manual /compact, precomputed compression, users who + * re-enabled the env, and CC installs routed through bili without the + * managed block (explicit ANTHROPIC_BASE_URL). + * + * STRICTNESS vs the #2619 retry-exemption detector (degenerate-turn.ts's + * requestExpectsProseSummary, which matches EITHER phrase): the guard + * requires BOTH instruction phrases in the final user message. The exemption + * is fail-open by design (a missed exemption costs one wasted retry); this + * guard refuses a turn outright, so a user legitimately ASKING for that + * output format ("summarize this, use an block followed by a + * block") must not be 403'd. The full CC template always carries + * both lines; one phrase alone is human paraphrase, not the machine + * template. A reworded CC template degrades to a miss → the request + * forwards and #2619's exemption still protects the retry path (fail-open, + * same doctrine as the dsh marker tracker). + * + * PROTOCOL COVERAGE: openai + anthropic + responses, all marker-decisive on + * the final user text (messages[] / input[]) — CC speaks anthropic natively, + * but CC-behind-a-router installs (claude-code-router and friends) translate + * to the other wires while keeping the template text. Google stays out of + * scope (CC speaks no Google wire). The #1117 passthrough lane is + * deliberately NOT guarded: it exists for unattributed in-process callers + * (dsh's agentless background compaction, #2709), never for external HTTP + * clients like CC. + */ + +import { lastResponsesInputUserText, lastUserMessageText } from "./dsh-compaction-guard.js"; + +/** The two production phrases of CC's compaction instruction, tracked per CC + * release like MAIN_SYSTEM_PREFIXES (#970). Case-insensitive for + * #1731-style drift. Angle brackets hex-escaped per KDD #2. */ +const CC_SUMMARY_TAG_PHRASE = /Wrap your summary in \x3csummary\x3e\x3c\/summary\x3e tags/i; +const CC_ANALYSIS_BLOCK_PHRASE = /an \x3canalysis\x3e block followed by a \x3csummary\x3e block/i; + +/** True when the request's FINAL user message carries the full CC compaction + * instruction template (BOTH phrases — see STRICTNESS above). `parsed` is + * the parsed wire body; `protocol` the lane's wire protocol. */ +export function isClaudeCompactionCall(protocol: string | null, parsed: unknown): boolean { + let finalUser: string | undefined; + if (protocol === "responses") finalUser = lastResponsesInputUserText(parsed); + else if (protocol === "openai" || protocol === "anthropic") finalUser = lastUserMessageText(protocol, parsed); + if (finalUser === undefined) return false; + return CC_SUMMARY_TAG_PHRASE.test(finalUser) && CC_ANALYSIS_BLOCK_PHRASE.test(finalUser); +} + +export type Refusal = { status: number; body: unknown }; + +const REFUSAL_MESSAGE = + `Claude Code native compaction refused by billion-context: bili owns compression on this lane (#2612/#2728). ` + + `A native rewrite desynchronizes Claude Code's transcript from bili's fold ledger and re-sends the full raw history upstream. ` + + `Nothing was executed and your transcript is unchanged. ` + + `bili folds context automatically at its threshold; for a manual fold, ask the model to run the compress tool. ` + + `To allow Claude Code's native compaction anyway, opt in explicitly: "claude": { "allowNativeCompaction": true } in the bili config file, or env BILI_ALLOW_CLAUDE_COMPACTION=1.`; + +/** Protocol-shaped refusal body (same shape contract as the #1729 dsh twin: + * anthropic error envelope for the anthropic wire, an openai-style error + * with a stable code otherwise). 403: a policy refusal, not a malformed + * request — CC's "Error during compaction" surface renders the message and + * the session continues; retry logic must not treat it as transient. */ +export function claudeCompactionRefusal(protocol: string): Refusal { + if (protocol === "anthropic") { + return { + status: 403, + body: { type: "error", error: { type: "invalid_request_error", message: REFUSAL_MESSAGE } }, + }; + } + return { + status: 403, + body: { + error: { + type: "server_error", + code: "claude_compaction_refused", + message: REFUSAL_MESSAGE, + retryable: false, + }, + }, + }; +} diff --git a/src/server/dsh-compaction-guard.ts b/src/server/dsh-compaction-guard.ts index fc071e4aea..7310b6e8c4 100644 --- a/src/server/dsh-compaction-guard.ts +++ b/src/server/dsh-compaction-guard.ts @@ -83,8 +83,10 @@ function textOfContent(content: unknown): string { /** Text of the LAST role=user message for the messages-shaped protocols * (openai chat/completions, anthropic messages). Google's contents/parts - * shape stays out of scope — dsh speaks no Google wire. */ -function lastUserMessageText(protocol: string, parsed: unknown): string | undefined { + * shape stays out of scope — dsh speaks no Google wire. Exported for the + * claude compaction twin (src/server/claude-compaction-guard.ts), which + * reads the same final-user slot for Claude Code's instruction template. */ +export function lastUserMessageText(protocol: string, parsed: unknown): string | undefined { if (parsed === null || typeof parsed !== "object") return undefined; const messages = (parsed as Rec).messages; if (!Array.isArray(messages)) return undefined; @@ -100,8 +102,9 @@ function lastUserMessageText(protocol: string, parsed: unknown): string | undefi /** Text of the LAST role=user item on the Responses wire (parsed.input). * Items are typed objects ({type:"message", role, content: string | parts[]}); * function_call / function_call_output / reasoning items never carry user - * prose, so a role check plus the type guard is enough (#2360). */ -function lastResponsesInputUserText(parsed: unknown): string | undefined { + * prose, so a role check plus the type guard is enough (#2360). Exported for + * the claude compaction twin (src/server/claude-compaction-guard.ts). */ +export function lastResponsesInputUserText(parsed: unknown): string | undefined { if (parsed === null || typeof parsed !== "object") return undefined; const input = (parsed as Rec).input; if (!Array.isArray(input)) return undefined; diff --git a/src/server/handle.ts b/src/server/handle.ts index c6871d7c9f..17b7b6709d 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -50,6 +50,7 @@ import { anthropicBetaContextWindow, BILI_HOP_HEADER, capRegistryWindowByStandar import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; import { demoteGate, hasLeakedBiliToolsOnly, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; import { DSH_COMPACTION_SHAPE_MSGS, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; +import { claudeCompactionRefusal, isClaudeCompactionCall } from "./claude-compaction-guard.js"; import { emergencyNudge } from "./budget.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; import { piSubagentChannelFallback } from "./pi-subagent-channel.js"; @@ -1586,6 +1587,40 @@ async function handleRequest( logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0); return; } + // #2612/#2728: Claude Code native compaction (manual /compact, precomputed + // compression, auto-compact when the managed DISABLE_AUTO_COMPACT=1 block + // is not in force) is refused the same way as the dsh twin above — but the + // refusal rationale is the mirror image. dsh's checkpoint landing would + // durably shadow the raw history; CC's compaction SUCCEEDING rewrites CC's + // local transcript to the summary while bili's fold ledger still anchors + // the raw history: an unannounced history rewrite. The three read faces + // split (status blocks vs what CC actually sends, search misses, + // decompress returns text CC can no longer place) and the session drifts + // into the silent-desync family (#2728). And the moment users reach for + // /compact is exactly when things are already broken (folds lost, context + // re-inflated): the envelope re-sends the FULL raw history — multi-M + // tokens upstream, 400 or real money — for a summary bili would have + // folded for free. Refusal channel = 403 API error, not text: CC's + // "Error during compaction" surface renders the message, the transcript + // stays verbatim, nothing executes. allowClaudeCompaction (config + // claude.allowNativeCompaction / env BILI_ALLOW_CLAUDE_COMPACTION) lifts + // it for users who accept that trade. Runs before the #388 side-request + // lane for the same reason as the dsh guard: full-budget request. + if (protocol !== null && opts.allowClaudeCompaction !== true && isClaudeCompactionCall(protocol, parsed)) { + if (session.metadata.claudeCompactionRefused !== true) { + session.metadata.claudeCompactionRefused = true; + log("warn", `[${session.id}] Claude Code native compaction call identified (final user message = CC summarize template, ${inboundMsgs} msgs) — REFUSED, not forwarded: bili owns compression on this lane; a native rewrite desynchronizes CC's transcript from the fold ledger and re-sends the full raw history (#2612/#2728)`); + } + session.metadata["claudeCompactionRefusals"] = (typeof session.metadata["claudeCompactionRefusals"] === "number" ? session.metadata["claudeCompactionRefusals"] as number : 0) + 1; + markDirty(session); + const refusal = claudeCompactionRefusal(protocol); + if (!res.headersSent && !res.writableEnded && !res.destroyed) { + res.writeHead(refusal.status, { "content-type": "application/json" }); + res.end(JSON.stringify(refusal.body)); + } + logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0); + return; + } // #388: side requests (title-gen etc.) share the main session key but // must not touch kernel state (processTurn/snapshot/usage would pollute // the main view). Forward with a minimal prepared marked sidePassthrough: diff --git a/tests/issue2612-claude-compaction-guard.test.ts b/tests/issue2612-claude-compaction-guard.test.ts new file mode 100644 index 0000000000..ddc24d0dcc --- /dev/null +++ b/tests/issue2612-claude-compaction-guard.test.ts @@ -0,0 +1,378 @@ +// #2612/#2728: Claude Code native-compaction wire guard (claude.allowNativeCompaction). +// Pins: (a) detector strictness — the FULL CC template (BOTH phrases) on the +// final user message, across anthropic/openai/responses wires; single-phrase +// paraphrase, mid-history quotes, and foreign summarize prompts must NOT trip +// it; (b) config resolution matrix (file / env / default OFF); (c) the +// end-to-end flip — the guard refuses CC compaction envelopes locally with 403 +// (never forwarded, nothing reaches upstream) and forwards once the opt-in is +// set in the config file; (d) refusal body shape per protocol; (e) the +// strict-AND contract vs the #2619 retry-exemption detector (either-phrase, +// fail-open) which stays independently green. +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { allowClaudeCompactionState, loadOptions, type ProxyOptions } from "../src/config.ts"; +import { claudeCompactionRefusal, isClaudeCompactionCall } from "../src/server/claude-compaction-guard.ts"; +import { requestExpectsProseSummary } from "../src/degenerate-turn.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { rmrf } from "./tmp-rm.ts"; + +/** The production CC 2.1.x compaction instruction (mirror of the #2619 + * fixture, tests/issue2612-compaction-draft-tail.test.ts). */ +const CC_COMPACTION_PROMPT = [ + "Summarize the conversation so far. Your summary should be about 8-29 sentences. ", + "Wrap your summary in tags.", + "REMINDER: Do NOT call any tools. Respond with plain text only — an block followed by a block.", +].join("\n"); + +/** Just the first production phrase — what a human paraphrasing the format + * ("wrap your summary in tags") would produce. Must NOT trip the guard. */ +const HALF_PHRASE = "Summarize our chat. Wrap your summary in tags."; +/** Just the second phrase. */ +const OTHER_HALF_PHRASE = "Answer in plain text only — an block followed by a block."; + +// --------------------------------------------------------------------------- +// (a) detector + +test("#2612: isClaudeCompactionCall — full CC template on the FINAL user message, anthropic wire", () => { + const body = { + model: "claude-sonnet-4-5", + system: "You are Claude Code", + messages: [ + { role: "user", content: "help me write a login form" }, + { role: "assistant", content: "ok" }, + { role: "user", content: CC_COMPACTION_PROMPT }, + ], + }; + assert.equal(isClaudeCompactionCall("anthropic", body), true, "full template on final user message = compaction call"); +}); + +test("#2612: isClaudeCompactionCall — openai wire (CC behind a router)", () => { + const body = { + model: "gpt-x", + messages: [ + { role: "system", content: "sys" }, + { role: "user", content: "do things" }, + { role: "assistant", content: "done" }, + { role: "user", content: CC_COMPACTION_PROMPT }, + ], + }; + assert.equal(isClaudeCompactionCall("openai", body), true); +}); + +test("#2612: isClaudeCompactionCall — responses wire (input[] final user item)", () => { + const body = { + model: "gpt-x", + instructions: "sys", + input: [ + { type: "message", role: "user", content: "do things" }, + { type: "message", role: "assistant", content: "done" }, + { type: "message", role: "user", content: CC_COMPACTION_PROMPT }, + ], + }; + assert.equal(isClaudeCompactionCall("responses", body), true); +}); + +test("#2612: strict-AND — a single phrase alone is human paraphrase, not the template", () => { + for (const text of [HALF_PHRASE, OTHER_HALF_PHRASE]) { + const body = { messages: [{ role: "user", content: text }] }; + assert.equal(isClaudeCompactionCall("anthropic", body), false, `must not refuse: ${text.slice(0, 40)}…`); + } +}); + +test("#2612: mid-history quote is NOT a compaction call (final-user decisive)", () => { + const body = { + messages: [ + { role: "user", content: CC_COMPACTION_PROMPT }, + { role: "assistant", content: "past turn quoting it" }, + { role: "user", content: "and now the actual question?" }, + ], + }; + assert.equal(isClaudeCompactionCall("anthropic", body), false); +}); + +test("#2612: ordinary traffic never trips the guard", () => { + assert.equal(isClaudeCompactionCall("anthropic", { messages: [{ role: "user", content: "please summarize this file" }] }), false); + assert.equal(isClaudeCompactionCall("anthropic", { messages: [{ role: "assistant", content: CC_COMPACTION_PROMPT }] }), false, "assistant-authored text is not a compaction trigger"); + assert.equal(isClaudeCompactionCall("openai", { messages: [{ role: "user", content: "hello" }] }), false); + assert.equal(isClaudeCompactionCall("responses", { input: [{ type: "message", role: "user", content: "hello" }] }), false); + assert.equal(isClaudeCompactionCall("anthropic", null), false, "unparseable body fails closed-forward (no refusal, normal pipeline)"); + assert.equal(isClaudeCompactionCall(null, { messages: [{ role: "user", content: CC_COMPACTION_PROMPT }] }), false, "unknown protocol is not guarded"); + assert.equal(isClaudeCompactionCall("google", { contents: [{ role: "user", parts: [{ text: CC_COMPACTION_PROMPT }] }] }), false, "google stays out of scope (CC speaks no google wire)"); +}); + +test("#2612: refusal shape per protocol — anthropic error envelope vs openai coded error", () => { + const ant = claudeCompactionRefusal("anthropic"); + assert.equal(ant.status, 403); + const antBody = ant.body as { type: string; error: { type: string; message: string } }; + assert.equal(antBody.type, "error"); + assert.equal(antBody.error.type, "invalid_request_error"); + assert.ok(antBody.error.message.includes("allowNativeCompaction") && antBody.error.message.includes("BILI_ALLOW_CLAUDE_COMPACTION"), "message points at the real opt-in"); + + const oai = claudeCompactionRefusal("openai"); + assert.equal(oai.status, 403); + const oaiBody = oai.body as { error: { code: string; retryable: boolean; message: string } }; + assert.equal(oaiBody.error.code, "claude_compaction_refused"); + assert.equal(oaiBody.error.retryable, false, "policy refusal — retry logic must not treat it as transient"); + + const resp = claudeCompactionRefusal("responses"); + assert.equal((resp.body as { error: { code: string } }).error.code, "claude_compaction_refused"); +}); + +// --------------------------------------------------------------------------- +// (b) config resolution + +function withConfig(body: string | null, fn: () => void): void { + const dir = mkdtempSync(path.join(tmpdir(), "bili-issue2612-")); + const file = path.join(dir, "billion-context.json"); + if (body !== null) writeFileSync(file, body, "utf8"); + const prev = process.env.BILI_CONFIG_FILE; + process.env.BILI_CONFIG_FILE = file; + try { + fn(); + } finally { + if (prev === undefined) delete process.env.BILI_CONFIG_FILE; + else process.env.BILI_CONFIG_FILE = prev; + rmrf(dir); + } +} + +function envWith(value: string | undefined): NodeJS.ProcessEnv { + const env: NodeJS.ProcessEnv = {}; + if (value !== undefined) env.BILI_ALLOW_CLAUDE_COMPACTION = value; + return env; +} + +test("#2612: allowClaudeCompaction resolution — file, env, default OFF", () => { + withConfig(null, () => assert.equal(loadOptions(envWith(undefined)).allowClaudeCompaction, false, "default OFF (guard active)")); + withConfig('{"claude":{"allowNativeCompaction":true}}', () => assert.equal(loadOptions(envWith(undefined)).allowClaudeCompaction, true, "file enables (claude.* section)")); + withConfig('{"claude":{"allowNativeCompaction":true}}', () => assert.equal(loadOptions(envWith("0")).allowClaudeCompaction, false, "env 0 beats file")); + withConfig(null, () => assert.equal(loadOptions(envWith("1")).allowClaudeCompaction, true, "env 1 enables")); + withConfig(null, () => assert.equal(loadOptions(envWith("yes")).allowClaudeCompaction, true, "non-\"0\" env values enable (house BILI_ boolean semantics)")); +}); + +test("#2612: allowClaudeCompactionState source resolution", () => { + withConfig(null, () => assert.deepEqual(allowClaudeCompactionState(envWith(undefined)), { enabled: false, source: null })); + withConfig('{"claude":{"allowNativeCompaction":true}}', () => assert.deepEqual(allowClaudeCompactionState(envWith(undefined)), { enabled: true, source: "file" })); + withConfig('{"claude":{"allowNativeCompaction":true}}', () => assert.deepEqual(allowClaudeCompactionState(envWith("0")), { enabled: false, source: "env" })); + withConfig(null, () => assert.deepEqual(allowClaudeCompactionState(envWith("1")), { enabled: true, source: "env" })); +}); + +// --------------------------------------------------------------------------- +// (e) contract vs the #2619 retry-exemption detector + +test("#2612: division of labor with #2619 — exemption is either-phrase (fail-open), guard is both-phrase (fail-closed)", () => { + // The exemption must still fire on EITHER phrase so a reworded/one-phrase + // CC variant never wastes a draft-tail retry… + assert.equal(requestExpectsProseSummary(HALF_PHRASE), true); + assert.equal(requestExpectsProseSummary(OTHER_HALF_PHRASE), true); + assert.equal(requestExpectsProseSummary("unrelated"), false); + // …while the wire guard requires the full machine template. + assert.equal(isClaudeCompactionCall("anthropic", { messages: [{ role: "user", content: HALF_PHRASE }] }), false); + assert.equal(isClaudeCompactionCall("anthropic", { messages: [{ role: "user", content: CC_COMPACTION_PROMPT }] }), true); +}); + +// --------------------------------------------------------------------------- +// (c) integration: real bili server + mock upstream + +function closeServer(server: http.Server): Promise { + server.closeAllConnections?.(); + return new Promise((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))); +} + +function post(port: number, reqPath: string, payload: unknown, headers: Record = {}): Promise<{ status: number; body: string }> { + const data = JSON.stringify(payload); + const { promise, resolve, reject } = Promise.withResolvers<{ status: number; body: string }>(); + const req = http.request({ + host: "127.0.0.1", + port, + path: reqPath, + method: "POST", + headers: { "content-type": "application/json", "content-length": String(Buffer.byteLength(data)), ...headers }, + }, (res) => { + let body = ""; + res.on("data", (c: Buffer) => { body += c.toString("utf8"); }); + res.on("end", () => resolve({ status: res.statusCode ?? 0, body })); + }); + req.once("error", reject); + req.end(data); + return promise; +} + +test("#2612: the wire guard refuses CC compaction locally; claude.allowNativeCompaction forwards it", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const root = path.join(tmpdir(), `bili-2612-${process.pid}-${Date.now()}`); + mkdirSync(root, { recursive: true }); + const cfgFile = path.join(root, "billion-context.json"); + writeFileSync(cfgFile, '{"providers":{}}\n', "utf8"); + const prevConfig = process.env.BILI_CONFIG_FILE; + process.env.BILI_CONFIG_FILE = cfgFile; + + const received: string[] = []; + const mock = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + received.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "msg_2612", type: "message", role: "assistant", model: "mock", + content: [{ type: "text", text: "ok" }], + stop_reason: "end_turn", + usage: { input_tokens: 10, output_tokens: 5 }, + })); + }); + }); + mock.listen(0, "127.0.0.1"); + await once(mock, "listening"); + const mockPort = (mock.address() as { port: number }).port; + + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: `http://127.0.0.1:${mockPort}`, + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const bili = await startServer(opts); + await once(bili, "listening"); + const port = (bili.address() as { port: number }).port; + + const compactionPost = (): Promise<{ status: number; body: string }> => post(port, "/v1/messages", { + model: "mock", + system: "You are Claude Code, Anthropic's official CLI for Claude.", + messages: [ + { role: "user", content: "help me write a login form" }, + { role: "assistant", content: "sure" }, + { role: "user", content: CC_COMPACTION_PROMPT }, + ], + }, { "x-acp-session": "s-2612" }); + const normalPost = (): Promise<{ status: number; body: string }> => post(port, "/v1/messages", { + model: "mock", + messages: [{ role: "user", content: HALF_PHRASE }], + }, { "x-acp-session": "s-2612b" }); + + try { + // A: default — refused locally with the anthropic error envelope, + // nothing reaches the upstream; the session metadata records it. + let r = await compactionPost(); + assert.equal(r.status, 403, `default must refuse (got ${r.status}: ${r.body})`); + const refused = JSON.parse(r.body) as { type: string; error: { type: string; message: string } }; + assert.equal(refused.type, "error"); + assert.equal(refused.error.type, "invalid_request_error"); + assert.ok(refused.error.message.includes("refused by billion-context"), refused.error.message); + assert.equal(received.length, 0, "refused call never reaches the upstream"); + + // Repeat under the SAME session key: still 403, still nothing + // upstream (idempotence; the once-warn flag must not gate the + // refusal itself). + r = await compactionPost(); + assert.equal(r.status, 403); + assert.equal(received.length, 0); + + // B: paraphrase traffic is untouched by the guard (fails open onto + // the normal pipeline). + r = await normalPost(); + assert.equal(r.status, 200, `half-phrase must forward (got ${r.status}: ${r.body})`); + assert.equal(received.length, 1, "normal traffic reaches the upstream"); + + // C: opt-in through the config file → the envelope forwards. + writeFileSync(cfgFile, '{"providers":{},"claude":{"allowNativeCompaction":true}}\n', "utf8"); + const reloaded = loadOptions(process.env); // fresh resolution reads the file + assert.equal(reloaded.allowClaudeCompaction, true, "file opt-in resolves"); + r = await compactionPost(); + // No hot-reload: startServer captured opts at launch, so the guard + // decision is fixed for this server's lifetime — started without the + // opt-in, it stays refusing even after the file flips above (the + // opt-in path is covered by the fresh-server test below). + assert.equal(r.status, 403, `no hot-reload: guard stays active for the server lifetime (got ${r.status})`); + } finally { + process.env.BILI_CONFIG_FILE = prevConfig ?? ""; + if (prevConfig === undefined) delete process.env.BILI_CONFIG_FILE; + await closeServer(bili); + await closeServer(mock); + rmrf(root); + } +}); + +test("#2612: guard-off server lifetime — opts.allowClaudeCompaction=true forwards the envelope byte-exact", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const root = path.join(tmpdir(), `bili-2612b-${process.pid}-${Date.now()}`); + mkdirSync(root, { recursive: true }); + + const received: string[] = []; + const mock = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + received.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_2612b", type: "message", role: "assistant", model: "mock", content: [{ type: "text", text: "ok" }], stop_reason: "end_turn", usage: { input_tokens: 10, output_tokens: 5 } })); + }); + }); + mock.listen(0, "127.0.0.1"); + await once(mock, "listening"); + const mockPort = (mock.address() as { port: number }).port; + + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: `http://127.0.0.1:${mockPort}`, + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + allowClaudeCompaction: true, // the opt-in as the server would receive it from loadOptions() + }; + const bili = await startServer(opts); + await once(bili, "listening"); + const port = (bili.address() as { port: number }).port; + + try { + const r = await post(port, "/v1/messages", { + model: "mock", + messages: [ + { role: "user", content: "work" }, + { role: "user", content: CC_COMPACTION_PROMPT }, + ], + }, { "x-acp-session": "s-2612-optin" }); + assert.equal(r.status, 200, `opt-in must forward (got ${r.status}: ${r.body})`); + assert.equal(received.length, 1); + assert.ok(received[0].includes("Wrap your summary in"), "envelope forwarded (template text reaches upstream)"); + } finally { + await closeServer(bili); + await closeServer(mock); + rmrf(root); + } +}); diff --git a/website/config-reference/server.yaml b/website/config-reference/server.yaml index 96df38d27f..2add8e467c 100644 --- a/website/config-reference/server.yaml +++ b/website/config-reference/server.yaml @@ -349,6 +349,13 @@ entries: group: behavior desc_en: "Exact port pin for the claude native lane's hook-spawned proxy (strict-port: squatters are refused loudly)." desc_zh: "claude 原生车道钩子派生代理的精确端口钉死(严格端口:被占用时响亮拒绝而非跳端口)。" + - key: claude.allowNativeCompaction + type: boolean + default: "false" + env: BILI_ALLOW_CLAUDE_COMPACTION + group: behavior + desc_en: "Lift bili's local 403 refusal of Claude Code native compaction calls (manual /compact, precomputed compression, auto-compact behind a non-managed install — final user message carrying the CC summarize template) on every wire lane bili serves — anthropic natively, openai/responses behind CC-router installs (#2612/#2728): when true the call forwards; on success CC rewrites its transcript to the summary while bili's fold ledger still anchors the raw history (unannounced-rewrite desync family), so this is an explicit opt-out of bili-owned compression. Default OFF keeps the guard active; the managed install block separately sets DISABLE_AUTO_COMPACT=1 so auto-compact does not fire under a bili-managed CC." + desc_zh: "解除 bili 对 Claude Code 原生压缩调用的本地 403 拒绝(手动 /compact、预计算压缩、非托管安装下的 auto-compact —— 尾部 user 消息携带 CC 摘要模板),覆盖 bili 服务的全部线路 —— CC 原生走 anthropic,经 CC-router 的安装走 openai/responses(#2612/#2728):为 true 时调用照常转发;一旦成功 CC 会把本地转录改写为摘要,而 bili 的折叠账本仍锚定原始历史(无告知改写 / 静默失同步家族),因此这是对 bili 自管压缩的显式 opt-out。默认关闭(守卫生效);托管安装块另行设置 DISABLE_AUTO_COMPACT=1,bili 托管下的 CC 不会触发 auto-compact。" - key: pi.subagents type: "object | boolean" default: "{} (acp_delegate enabled with package defaults)"