Skip to content

fix: anthropic global-scope gate — narrow predicate + default threads-lane passthrough cap (#2884) - #2888

Closed
ranxianglei wants to merge 4 commits into
masterfrom
2026-10-12_claude-global-scope-narrow-gate
Closed

ranxianglei wants to merge 4 commits into
masterfrom
2026-10-12_claude-global-scope-narrow-gate

Conversation

@ranxianglei

@ranxianglei ranxianglei commented Oct 12, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes Claude Code compression breakage from #2650's over-broad global-scope passthrough, per @ranxianglei's direction on #2884. Two layers:

  1. Narrowed stand-down predicate + thread-granular stickiness (the fix: anthropic global-scope gate — narrow predicate + default threads-lane passthrough cap (#2884) #2888 core, kept as directed): a request stands down only when it matches WC-016's actual reject shapes — no tools, OR a scope:"global" breakpoint on system[0]. The inject/passthrough decision is made at thread create and inherited by every continue, so one upstream thread never mixes raw and injected forwards (which caused thread_fingerprint_mismatch).
  2. Temporary threads-lane cap (added this round per owner decision): any anthropic request carrying a thread field (create AND continue alike) now forwards byte-for-byte by default — no tool injection, no nudge, no system/tools/cache_control rewrite. Rationale: layer 1 restores Claude Code main-conversation compression only under a shape assumption (the client's own unmarked tools + a mid-system scope:"global" breakpoint survive upstream with bili's extra unmarked tools rendered ahead) that was never proven against the real upstream — the e2e tests run against a permissive fake. An unproven assumption that could 400 an entire Claude Code conversation is worse than "works but doesn't compress". So we ship the conservative default and keep the full logic behind an opt-in until a real post-injection 200 is captured. Endgame tracked in [Tracking] SQLite-backed threads-lane compression (endgame for the #2884 cap) #2926.

Behavior change (old → new)

scenario before (#2650 era) after
CC main convo (tools present + scope:"global" mid-system) whole-request passthrough → compression fully disabled default: byte-for-byte passthrough (cap); BILI_ANTHROPIC_THREADS_INJECT=1 → injects (full #2888 logic)
bare side call (no tools, scope:"global" on system[0]) passthrough passthrough (unchanged)
non-threaded global-scope requests passthrough if ANY global block stand down only for WC-016 reject shapes; otherwise compress (narrowed predicate)
threaded create/continue consistency per-request → thread_fingerprint_mismatch decision sticky per thread (inherited from create)

Visibility (owner req ②)

Config surface (owner-gated)

New env var BILI_ANTHROPIC_THREADS_INJECT (default unset = cap active). Env-only by design: a transient forensic toggle slated for deletion the moment the cap lifts, deliberately NOT given a config-file key. Owner-directed in #2884. Documented in CONFIGURATION.md (en + zh), hand-written env table (not the generated twin table).

Changes

Verification

Rebased onto master 9972ff2df; typecheck ✓, build ✓, targeted suites green on the rebased head: e2e-anthropic 10/10, wire-contract 18/18 (WC-016 intact), side-lane-invariants 8/8, advisory-endpoint-parity + build-info ✓. Full suite left to CI.

Acceptance mapping (owner's 盖子版 checklist)

  1. ✅ byte-for-byte passthrough for thread requests — regression test asserts tools/system/messages deepEqual inbound.
  2. ✅ WARN log + /__bili/status field.
  3. ✅ non-threaded global requests keep the narrowed-predicate behavior.
  4. ✅ opt-in env runs full fix: anthropic global-scope gate — narrow predicate + default threads-lane passthrough cap (#2884) #2888 logic; the injection assertions moved to the env-enabled branch.
  5. ⚠️ create observer-mode ingestion — deferred to phase 2 (see below).

Deferred: observer-mode ingestion (acceptance ⑤)

Per owner note "若改动面大,PR 里说明后可二期": the durable substrate for threads-lane compression is the unified SQLite store (#2671), not ephemeral session-state. Building observer writes into session-state now would be throwaway, and running the kernel pipeline purely to observe risks diverging session state from the raw-forwarded wire. Tracked in #2926 alongside the endgame.

Closes #2884

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 开始 review PR #2884(fix: narrow anthropic global-scope passthrough)。按规范走:先审原 issue #2884 的方向与层次,再逐行读 diff、回归审查、测试有效性。结论稍后按结构化八问给出。

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-12_claude-global-scope-narrow-gate (3a9f3da)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.3257-pr.2888.329179b

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2888.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR #2888 review 结论(rebased onto master 56063ceae 后验证)

结构化八问:

  1. 是什么问题:fix: passthrough anthropic requests carrying a global-scope system block (#2648) #2650 的「任一 system 块带 scope:"global" → 整请求 passthrough」门槛过宽——把 Claude Code 主对话整体放行,静默关闭了全部压缩(无工具注入 / nudge / fold);且按请求逐次判定,同一 thread 的 create/continue 形状漂移时混发 raw+injected,上游 400 thread_fingerprint_mismatch。
  2. 什么原因导致的:两层根因。① 上游 400 的真实触发条件很具体——「有 tools 且 global 断点在 system[0]」(上游原话 "not a true prefix when tools are present"),fix: passthrough anthropic requests carrying a global-scope system block (#2648) #2650 把它泛化成了「任何位置出现 global」;② inject/passthrough 决策必须在同一上游 thread 的所有请求间保持一致(fingerprint 含 system/tools/cache_control),fix: passthrough anthropic requests carrying a global-scope system block (#2648) #2650 却是 per-request 重判,形状一漂就自相矛盾。
  3. 影响范围与严重程度:v0.1.193 起所有 Claude Code proxy-mode 主对话完全失去压缩(条件:带 tools、global@system[2] 的主循环 turn);叠加偶发的 mismatch 400 打断会话。属功能级失效,非数据损坏。
  4. 怎么修复的:双管齐下——① 谓词收窄到实证会 400 的形状(!hasTools || globalIdx===0),CC 主形状恢复全量注入;② 决策线程粒度 sticky:create(或中途首次见到该 thread)时求值并存 session.metadata.anthropicGlobalStandDown,每个 continue 继承,一个 thread 永不混发。非 threaded 请求无服务端 thread 状态可保一致,仍按自身形状逐请求判定(正确)。
  5. 是否有回归问题:逐项审过既有行为——[Bug][claude MITM] 注入 ACP 工具破坏宿主请求:scope:"global" 缓存前缀 400 + Threads continue 指纹不符(thread_fingerprint_mismatch) #2648 原始 no-tools side call passthrough 保持(e2e test 5 钉住);global@system[0]+tools 仍 stand down;无 global 的请求路径不变;pluginMode 不受影响(gate 仍在 !pluginMode 内);auto-mode classifier passthrough 顺序未动;WC-016 ledger 只增不缩、goldens 未动;与 [Bug][claude MITM] Threads 下 bili 压缩回路在 continue 轮次全面失效:nudge 失明、mid-turn compress 400(previous_message_id already continued)、折叠只能在 create 送达 #2701 tail-tracking 的「一会话一线程」假设一致(create 同时重置两者)。一处残留风险已如实披露:「bili 追加 unmarked 工具后上游仍接受 CC 主形状」是从 raw 形状被接受的实证外推(0.1.192 dump 已丢)——最坏情况是 CC 主 turn 出现可见 400,不是静默损坏;无 tools 一律 stand down 也保守覆盖了「deep-global + bili 加 tools」这个未直接验证的形状。
  6. 是否破坏缓存:不涉及缓存/重试/游标/去重策略变化。新增的 anthropicGlobalStandDown 是 session.metadata 开放袋里的一个新键:随会话落盘持久化(跨重启继承成立)、旧版本忽略未知键,不构成 persistence 格式变更。
  7. 是否改了设计方向:无架构转向。沿用既有 per-session metadata sticky 模式(同类先例:pluginAgent、anthropicThreadTail),边界记录遵循 KDD#9 evidence-permitlist 纪律(tools[]-global 缺口记在 WC-016 provenance,待流量证据再扩谓词)。
  8. 是否引入了新的配置:无新 env/config/CLI。

验证证据(本地定向,全量留给 CI):rebase 到当前 master 56063ceae 干净无冲突(#2873 触碰的文件与本 PR 零重叠);npm run typecheck ✓;定向套件 tests/e2e-anthropic.test.ts 9/9(含 3 个新回归测试,断言的是捕获到的上游 body 里 ACP 工具的实际存在/缺失——旧代码下必红)、tests/wire-contract.test.ts 18/18(WC-016 intact)、tests/side-lane-invariants.test.ts 8/8 全绿。另核对了安全前提的事实基础:kernel ACP 工具定义不带 cache_control(unmarked)、stampAnthropicSystemCacheControl 在客户端块已带 cc 时不动手、bili 自己的 message marks 只落在 messages(渲染在全部 system 之后,即 global 断点之后——合法)。三个新测试中 test 3 用 max_tokens:64 低于 #388 侧车道阈值 200,但 isSideRequest 对带 tools 的请求直接否决降档,故两请求都到达 gate——行为正确。

Review 顺手小修(已推 PR 分支,独立 commit 37ce311fa):WC-016 ledger 的 summary 行还停留在 #2650 时代的过严读法(「任何 unmarked/narrower-scope 前置内容即 400」),与 #2884 实证矛盾——CC 主循环正是 56 个 unmarked tools + unmarked system[0..1] 在前、global 在 system[2],上游接受。summary 已改写为观察到的边界:400 触发 = 有 tools 且 global@system[0];deeper global 前的 unmarked 内容被接受。provenance/gates/goldens 均未动,wire-contract 18/18 复跑绿。

判定:可合(§7.4:wire 行为变更属 human-merge 类,请手动合并 #2888 )。分支落后 master 一个提交但文件零重叠,本地 rebase 已证干净,GitHub mergeable=true。合并后建议在你的 CC 环境观察一个真实主会话:预期日志出现正常注入(不再每 turn 一条 "global-scope system passthrough"),且 /acp-cache 命中率回到 95–97% 区间。

ework-agent added 4 commits October 12, 2026 19:39
…nd-down (#2884)

#2650 whole-request passthrough fired on ANY system block carrying
cache_control.scope:"global". That stood down Claude Code main-conversation
requests wholesale -- their main loop carries unmarked tools plus a global
breakpoint mid-system that upstream accepts WITH extra unmarked tools ahead --
silently disabling compression there (no inject / nudge / fold). The decision
was also evaluated per request, so one threads create/continue pair could mix
raw and injected forwards -> upstream 400 thread_fingerprint_mismatch.

Narrow the gate to the shapes WC-016 actually rejects: stand down only when the
request carries NO tools OR the global breakpoint sits on system[0]. Make it
thread-granular -- computed at the thread create and inherited by every continue
through session.metadata -- so a single upstream thread never mixes raw and
injected forwards.

Regression tests: CC main-convo shape keeps compress injection; thread-granular
stand-down and inject inheritance across truncated continues.
The #2650 repair line in WC-016's provenance still described the gate as
"whole-request passthrough when any SYSTEM block pins scope:global". That
gate was narrowed in the preceding commit (#2884) to globalScopeRequiresStandDown
(NO tools OR global-on-system[0]) and made thread-granular. Add a short
refinement note so the ledger documents the live gate accurately. No wire
constraint changed; goldens do not pin provenance text.
…er cap, #2884)

#2888 restored Claude Code main-conversation compression by narrowing the
global-scope stand-down predicate and making the inject/passthrough decision
thread-granular. But both rely on a shape assumption -- the client's own
unmarked tools plus a mid-system scope:"global" breakpoint survive upstream WITH
bili's extra unmarked tools rendered ahead -- that was never proven against the
real upstream (the e2e tests run against a permissive fake). An unproven
assumption that 400s the whole Claude Code conversation is worse than "works but
doesn't compress".

Per owner decision, land a temporary cap on top of #2888 instead of shipping the
unproven injection by default:

- Any anthropic request carrying a `thread` field (create AND continue alike) now
  forwards byte-for-byte by DEFAULT: no tool injection, no nudge, no
  system/tools/cache_control rewrite. Non-threaded global-scope requests are
  unaffected and keep the #2888 narrowed predicate + thread stickiness intact.
- BILI_ANTHROPIC_THREADS_INJECT=1 opts back into full #2888 behavior (forensics /
  capturing a real post-injection 200 -- the evidence bar that lifts the cap).
- Visibility: the first passthrough per session logs WARN (then info), and
  /__bili/status + /__bili/overview expose `anthropicThreadsLane` so a user can
  discover bili is not compressing this lane.

Tests: new byte-for-byte passthrough regression (acceptance item 1); the two
thread-field #2884 tests now set the opt-in env to exercise the full logic; the
non-thread CC-main test stays in default mode. WC-016 ledger unchanged (the wire
rule is unchanged; only bili's gate becomes more conservative).

Closes #2884
…2884)

En + zh. Env-only by design (transient forensic toggle slated for deletion when
the cap ships), deliberately no config-file key. Documents the default byte-for-byte
passthrough of the anthropic threads lane, the opt-in escape hatch, and the
/__bili/status `anthropicThreadsLane` field.
@ranxianglei

Copy link
Copy Markdown
Owner Author

Closing — superseded by #3332 (the Anthropic Threads lane, tracking #2926).

#3332 claims every threads create/continue via isThreadsBody before the #2648 global-scope stand-down, mirrors the turn into the unified store, and forwards byte-faithfully until anthropicThreadsReforge arms. That makes this PR's threads-lane gate (isThreadsWireRequest) unreachable dead code, and the two PRs conflict on src/server/prepare-anthropic.ts.

The one surviving piece — the narrowed non-threads stand-down predicate — is dropped too, deliberately: it permits injection under the same "client tools + mid-system global survive upstream with bili's extra tools" shape assumption that motivated the cap and #3332 (never proven against a real upstream), and #2884's data showed no non-threads client hitting this path (dsh/pi/codex passthrough = 0). Keeping it would ship an unproven behavior change for a case with no observed demand; dropping it leaves the non-threads path at today's #2650 behavior (zero change).

Net effect: non-threads anthropic path unchanged from master; the Claude Code threads path is fixed properly by #3332 (capture + reforge) rather than waved through. Trivially reopenable if the non-threads predicate is wanted back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment