Skip to content

[PR] fix: decline the message-threads beta for Claude Code sessions bili compresses - #6918

Closed
MohammadErfan-Jabbari wants to merge 1 commit into
ranxianglei:masterfrom
MohammadErfan-Jabbari:2026-11-16_claude-threads-decline
Closed

MohammadErfan-Jabbari wants to merge 1 commit into
ranxianglei:masterfrom
MohammadErfan-Jabbari:2026-11-16_claude-threads-decline

Conversation

@MohammadErfan-Jabbari

Copy link
Copy Markdown
Contributor

Fixes #6915

Problem

Claude Code sends its turns as Message Threads. After the first create, each tool step is a continue that carries only the newest one or two messages, so bili can neither nudge nor fold a session it is meant to compress (#2701). Measured with a Claude Code session: 2 main-lane requests for 12 tool calls, no [plugin] [acp-usage] and no nudge line.

Fix

  • threadsBetaToDecline (src/server/threads-lane.ts) returns the message-threads-* token of the anthropic-beta header when the body is a threads turn of a session bili compresses.
  • src/server/handle.ts answers such a request with the 400 Anthropic gives for an unknown beta header, before anything is forwarded. Claude Code 2.1.322 matches that error, drops the beta and threading for the session, and retries with the full history, which bili folds as usual. One info line per decline: [threads] declined message-threads-… for a session bili compresses ….
  • The gate needs both signals: the request comes from Claude Code (x-claude-code-session-id), and bili compresses the session (bili's MCP tools on the wire, or, since Claude Code defers MCP tools behind tool search, a registration of that session id by bili's MCP server through consumePluginRegisterFor, which only reads and refreshes LRU order). Another Anthropic-wire client with bili's tools may not drop threads on the 400 and would fail every request, so it keeps the threads lane.

Both compression modes

Tests

  • tests/claude-threads-decline.test.ts (new) drives a real proxy against a mock upstream. Positive rows: a create and a continue with bili's tools are declined in Claude Code's matcher shape and never reach the upstream; the retry without threads is forwarded; a registered session whose tools are deferred is declined. Negative rows: a non-Claude-Code client with bili's tools keeps the threads lane; a threads request without bili's tools is forwarded. It fails on master and passes with the fix.
  • With the fix, claude-threads-decline, issue2701-threads-continue, mcp-native-thread, threads-lane and lane-decisions-matrix pass (135 tests), and npm run typecheck is clean.
  • Not run: the codex e2e suite (ACP_TEST_E2E=1). This touches the request pipeline before forwarding, so the maintainers may want that lane before merge.

Live result

On a private proxy with a Claude Code session (Haiku), the session was declined once, then grew 26 → 29 → 32 → 35 messages as full-history requests. The nudge fired after 25,670 tokens of growth and the model's compress folded about 57,000 tokens, with no rejected request besides the one intended decline. We have run this fix on our own machines since 2026-11-13.

Review tier

This answers some requests with a 400 instead of forwarding them, which is a wire-behaviour change, so per §7.4 it stays with a human reviewer. No config, persistence-format or kernel change.

🤖 Generated with Claude Code

…ompresses

Claude Code uses Message Threads through the proxy: each continue carries
only the new messages, so bili can neither nudge nor fold it (ranxianglei#2701). For
a Claude Code session bili compresses, bili now answers a threads request
with the 400 Anthropic gives for an unknown beta header. Claude Code drops
the header and threading for the session and retries with the full
history, which bili folds as usual.

A session counts as one bili compresses when its bili MCP tools are on
the wire, or, since Claude Code defers MCP tools behind tool search, when
bili's MCP server registered its x-claude-code-session-id. Only Claude
Code is declined: another Anthropic-wire client may not drop threads on
the 400. Requests without bili's tools keep the threads lane.

Fixes ranxianglei#6915

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ranxianglei ranxianglei changed the title fix: decline the message-threads beta for Claude Code sessions bili compresses [PR] fix: decline the message-threads beta for Claude Code sessions bili compresses Nov 16, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

Duplicate screening (查重): No duplicate. Searched open+closed PRs for message-threads / 2701 / 6915 / threads continue. Same-family PRs are all either closed (#2718 fix #2701, #2706 continue→create rewrite, #3332 the threads lane itself, #6113 omp) or take a different mechanism (#3746 over-budget reforge, #6799 Pi compat.supportsMessageThreads=false negotiation, #6067 CC subagent lane separation). The closest open PR is #6920 (same author, "claude MCP shim follows a Remote Control child's session id", Fixes #6917) — complementary, not parallel: its body states the child's requests "are Message Threads deltas until #6915 [lands]", i.e. it depends on this fix. No open PR fixes the same defect.

Review of #6918 (head 96274c7518, +171/-1, 3 files):

Direction (①): Correct. The root mechanism is real and I reproduced the asymmetry: prepare-anthropic.ts:114 runs the threads lane (byte-faithful forward + reforge) only when !pluginMode, so a plugin-mode Claude Code session's continue turns (1–2 message deltas) fall into the normal pipeline where nudge/fold are blind — exactly #2701. The new test fails on master (assertion tests/claude-threads-decline.test.ts:108) and passes with the fix; the issue's live log (nudge at 25,670 growth, ~57K folded) corroborates. The fix is 疏 not 堵: it makes CC drop threads and resend full history — back onto the path bili already compresses — rather than forcing compression on a 2-message delta.

Gate (②/③): The dual-signal gate is sound; I verified both halves are non-destructive. consumePluginRegisterFor (src/plugin.ts:955-968) is read + LRU refresh only (the PR's "only reads and refreshes LRU order" claim is accurate; the later callers at handle.ts:1913/1976 are unaffected), and declaresMcpBiliToolsOnWire (src/session-self-heal.ts:169-172) is a strict mcp__bili__*/mcp__bili match. Proxy mode (no bili tools inbound, no registration) never fires; plugin-mode CC is the only firing case; the message-threads-* prefix covers future dated betas; array anthropic-beta headers are handled. Placement is after the bypass/429-hold/hop checks and before session resolution, so a declined request touches no session state. Two minor edges, both harmless: a chained bili→bili request could be declined before chainSkip is computed (misconfig scenario; the client just drops threads and retries through), and a count_tokens threads turn would be declined (theoretical).

Wire-behaviour change: This answers some requests with a 400 instead of forwarding them — a wire change, so per §7.4 it stays at the human-review tier (the PR self-declares this). Residual risk is the client-behaviour dependency: CC 2.1.322 matches the 400 (status + message naming the header value + /anthropic[-_]beta/) and drops threads for the session; a future CC version that changes that matcher would 400-loop, but the x-claude-code-session-id gate confines the blast radius to Claude Code.

Tests (④): Valid, not tautological. A real startServer proxy against a mock upstream, listen(0) ports, XDG tmp dirs, and a simulated CC 2.1.322 matcher. Positive rows (create/continue with bili tools declined in CC's shape and never reaching upstream; the no-threads retry forwarded; a registered session with deferred tools declined) and negative rows (non-CC client with bili tools keeps the threads lane; a threads request without bili tools forwarded). Targeted run on the PR branch: claude-threads-decline + issue2701-threads-continue + mcp-native-thread + threads-lane + lane-decisions-matrix = 135/135 pass (matches the PR's claim).

CI: 1 failure of 52 checks — test (ubuntu-latest, 24, 1), not ok 2560 - a held tail in the just-sealed generation survives the deferred reclamation (tests/log-retention-followup.test.ts:183). This is a flake in the log-retention subsystem, unrelated to this PR: the test monkey-patches fs.WriteStream.prototype.end to hold completion callbacks, but settle in src/logger.ts:322-337 is attached to both the close event and the end() callback, so it fires on close independently of the held callback — a close-event scheduling race under CI load. The PR diff (an early-return in handle.ts + a pure threads-lane function + a new test) has no mechanism to touch the logger; the test passes 5/5 locally on the PR branch. Master is independently red on a different test (windows e2e #4079 e/f), and several other PR branches are red on different shards today — a flaky-test storm, not a master-level break. Recommend a re-run of the failed job.

Rebase: The PR is 18 commits behind master (tip 642000e7a3). The upstream changes to the touched files are in different regions — handle.ts (the plugin.js import at :56 + the #6880 side-classification at ~:1240) and plugin.ts (new purpose header); threads-lane.ts, session-self-heal.ts, and the decline insertion at :694-707 are untouched on master. The PR's import edit is the threads-lane.js line, three lines from master's plugin.js edit, so the rebase should merge clean. Please rebase before merge.

Docs: The Message Threads lane (#2926) is depicted only in the one-line file map reference/architecture.md:44, which stays accurate; the decline is a new gate not depicted anywhere (no diagram to regenerate). A one-line note in docs/architecture/fastpaths.md (en/zh/ja) would be a nice-to-have but is not a blocker.

Verdict: No code changes requested — the implementation is clean (named constants, doc comments explain why, no dead code, style consistent, exact 400 shape). Approve for a human reviewer to merge after (a) a rebase to current master and (b) a re-run of the flaky test (ubuntu-latest, 24, 1) job.


八问(中文,供人工 reviewer):

  1. 是什么问题:Claude Code 以 Message Threads 形式发请求,首轮 create 之后每个工具步的 continue 只带最新 1–2 条消息,导致 bili 对"本该压缩"的插件模式会话既无法 nudge 也无法 fold([Bug][claude MITM] Threads 下 bili 压缩回路在 continue 轮次全面失效:nudge 失明、mid-turn compress 400(previous_message_id already continued)、折叠只能在 create 送达 #2701)。
  2. 什么原因导致的:根因在 prepare-anthropic.ts:114 —— threads 车道(字节忠实转发 + reforge)只在 !pluginMode 时运行;插件模式的 CC threads continue 落进普通管线,只看到 2 条消息的增量,nudge/fold 对它是盲的。修法是"疏"不是"堵":让 CC 放弃 threads、回退全量历史重发,回到 bili 已能正常压缩的路径,而不是在 2 条消息增量上硬凑压缩。
  3. 影响范围与严重程度:仅影响"插件模式 + Claude Code + bili 压缩该会话"这一组合(双信号门:CC 头 + bili 工具/注册)。该组合下压缩功能完全失效(核心承诺失灵)→ 按 owner 2026-10-19 口径属 p1;其他客户端/代理模式不受影响(门永不触发)。
  4. 怎么修复的:对命中门的请求,在转发前直接回 Anthropic 的"未知 beta 头"400(精确匹配 CC 2.1.322 的识别形状:400 + 消息点名 header 值 + /anthropic[-_]beta/);CC 据此丢弃该会话的 beta 与 threading,用全量历史重试,bili 按既有插件模式机制正常 nudge/fold。
  5. 是否有回归问题:审过既有行为——代理模式(无 bili 工具/注册)门永不触发,threads 请求照旧走 threads 车道([Tracking] SQLite-backed threads-lane compression (endgame for the #2884 cap) #2926/Anthropic Threads lane: unified-store mirror + over-budget reforge into fresh creates (#2926, #2884) #3332);非 CC 客户端(无 x-claude-code-session-id)保留 threads 车道;无 bili 工具的 threads 请求正常转发;consumePluginRegisterFor 只读 + LRU 刷新,后续调用点不受影响。高危模式逐项:无语义漂移、无 fail-fast 变吞错、无阈值/时序/输出格式变化。残留风险=客户端行为依赖(未来 CC 版本若改 400 识别形状会 400 循环,但被 CC 头门限缩在 Claude Code 内);两个无害边缘(链式 bili→bili 在 chainSkip 前被拒——仅误配场景;count_tokens 的 threads 轮被拒——理论)。
  6. 是否破坏缓存:不涉及——400 在转发前返回,不触碰会话状态、前缀缓存、重试或增量游标;被拒请求不产生任何账本/存储写入。
  7. 是否改了设计方向:不涉及架构/边界转向。这是 threads 车道上的一道新"拒绝门",方向与既有"降级不猜"原则一致(命中即整轮回退到全量历史,不做部分映射)。属 wire 行为变更(400 代替转发),按 §7.4 留人工审。
  8. 是否引入了新的配置:无——无新 env/config/默认值。

判定:代码无需改动(整洁度达标)。合并前需 (a) rebase 到当前 master(落后 18 个提交,但上游改动与本 PR 插入区不重叠,应能干净合并);(b) 重跑 flaky 的 test (ubuntu-latest, 24, 1)(log-retention 子系统的 close 事件竞态,与本 PR 无关)。满足 (a)(b) 后,建议人工 reviewer 合并。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

Re-verified live state first: the only red check is test (ubuntu-latest, 24, 1) on head 96274c7518 (not already fixed by an earlier wake). I pulled the job log and isolated the single failing test.

Diagnosis: a flake in a master-level test — not introduced by this PR.

  • Failing test: tests/log-retention-followup.test.ts — "a held tail in the just-sealed generation survives the deferred reclamation" (line 183, assertion one still-pending drain must keep the retention gate closed, expected true / actual false).
  • Not PR-induced: this PR touches only src/server/handle.ts, src/server/threads-lane.ts, and adds tests/claude-threads-decline.test.ts — zero intersection with logging/retention. The failing test's code (src/logger.ts, src/log-retention.ts, the test file) is byte-identical between the PR head and master (no changes since merge-base 8c9bff202).
  • Passes locally: 10/10 green on master (node 22). No CI preload pin affects this test's timing (unlike the codex 命中率问题分析 #2929 BILI_MESSAGE_TIMESTAMPS case), so it is not a deterministic local-vs-CI wire-shape difference.
  • Root cause (test-side timing race): the test gates the retention drain by holding the end() callback, but the production settle is also registered on the stream's close event as a deliberate anti-wedge backup (src/logger.ts:336) — which the test does not hold. On the loaded CI runner (node 24), the close event won the narrow race and drained a stream early, defeating the test's "one drain still pending" assumption. This is a new flake signature (not in CI Control Center: Centralized CI Failure Handling & Status Sync (Comment all CI issues here; do not open new issues) | CI 总控: CI 失败集中处理与状态同步(所有 CI 问题在此评论, 勿开新 issue) #4599's known-red list); I logged it there for fleet coordination.

Action taken: re-ran the failed job (run 43836745116, attempt 2, now in progress). No code change on the PR branch — hardening a master-level test belongs on ci-fixes, not this PR's diff. If the same signature recurs on a clean runner, it escalates to master-level test hardening converged on ci-fixes per the #4599 protocol.


中文八问

  1. 是什么问题:CI test (ubuntu-latest, 24, 1) 分片红,唯一失败是 tests/log-retention-followup.test.ts 的「held tail」用例(第 183 行断言)。
  2. 什么原因导致的:测试自身的 event-loop 时序竞态——测试 hold 住 end() 回调来门控 retention drain,但生产代码的 settle 同时挂在流 close 事件上(防 wedge 备份,src/logger.ts:336),测试没 hold 住 close;CI 负载 runner(node 24)上 close 抢先触发,提前 drain 掉一条流,击穿「还有一条 drain 未完成」的假设。非本 PR 引入(代码与 master 逐字节相同,PR 不碰 logging)。
  3. 影响范围与严重程度:仅影响该测试在负载 CI runner 上的稳定性,无生产行为影响;严重程度 = CI 噪声(体验瑕疵/内部实现,无外部影响)。
  4. 怎么修复的:rerun 失败 job(run 43836745116,attempt 2 进行中),PR 分支零代码改动;若同签名在干净 runner 复现,升档为 master 级测试硬化,收敛到 ci-fixes。
  5. 是否有回归问题:无——本轮零代码改动。
  6. 是否破坏缓存:不涉及。
  7. 是否改了设计方向:不涉及。
  8. 是否引入了新的配置:不涉及。

收工判定:本轮已完成诊断 + rerun,等 attempt 2 结果;若仍红(同签名)则按 #4599 协议升档 ci-fixes 测试硬化。

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b)

Re-run is green — flake confirmed. run 43836745116 attempt 2 completed success: all 28 jobs pass (0 failed). The previously-red test (ubuntu-latest, 24, 1) shard is now green on the same head 96274c7518 with zero code changes.

This confirms the diagnosis: the log-retention-followup "held tail" failure was a one-off event-loop timing race on a loaded runner, not a PR defect and not a deterministic master red. No further action needed on this PR's CI. (Signature logged in #4599; if it recurs on a clean runner it escalates to test hardening on ci-fixes.)

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[ework] CI fix verified green ✓

@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[ework] CI fix verified green ✓

1 similar comment
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

[ework] CI fix verified green ✓

@ranxianglei

Copy link
Copy Markdown
Owner

此 PR 的内容已整体进入批量 PR #7107(逐 commit 原样 cherry-pick,作者与 commit message 保留,已合并)。

This PR's changes have been merged verbatim (commit-by-commit, authorship preserved) via the batch PR #7107. Closing this to avoid a duplicate merge. Thanks!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude Code sessions bili compresses still use Message Threads, so tool steps arrive as 2-message deltas bili cannot fold

2 participants