Skip to content

[bug] Claude Code Task subagents: bili MCP tools run against the root session (process-wide CLAUDE_CODE_SESSION_ID) → 409 witness conflicts, phantom range errors, and a subagent fold applied to the parent | Claude Code 子代理的 MCP 压缩落到父会话 #4580

Closed

Description

@qiangu2020

Environment

  • Claude Code CLI 2.1.306 / 2.1.308 (terminal), launched through bili … claude. bili 0.1.20x at the time (2026-10-20 → 10-23). On master, src/mcp.ts still binds once per process: CONVERSATION_FROM_ENV = process.env.CLAUDE_CODE_SESSION_ID?.trim() || ….
  • Task tool subagents (general-purpose, background), depth 1 and 2. The proxy-side split works for them: their tags restart at m00001, as on a |sub: lane.

Data

All mcp__bili__compress results in ~/.claude/projects/** transcripts, 2026-10-20 → 10-23 (deduplicated by tool_use_id):

caller calls applied 409 witness conflict "covered by active blocks" dead refs no net shrink too small malformed
main sessions (14 files) 63 56 0 1 0 4 0 2
Task subagents (14 files) 50 4 10 23 4 7 2 0

Two cases that show the binding

Case 1 (depth-1 subagent, Claude Code 2.1.306). At 2026-10-21 08:41:03Z the subagent had 145 transcript messages and no successful compression (its first call at 08:37:54 got bili tool error: outbound tool witness does not match conversationId). It asked for ranges from its own context and got:

Range contains no compressible messages — all are already covered by active blocks or protected. [cause: covered-by-block …] [Current context: 85 visible message(s), refs m00001–m00372, 6 active block(s). …]

At that moment the root session had 361 transcript messages and 6 applied blocks. The status describes the root session.

Case 2 (depth-2 subagent, Claude Code 2.1.308). At 2026-10-22 15:34:28Z the subagent had 40 transcript messages and no blocks. Its compress error said:

[Current context: 249 visible message(s), refs m00001–m00545, 6 active block(s). …] Current compressible ranges (use these refs exactly as listed): Compressible ranges (11, oldest first): m00356–m00388  33 msgs  12.2K [tool 66% | text 34%] · 6 user msgs …

The root session had 536 transcript messages and 6 applied blocks; no sibling subagent was larger than 234 messages. The subagent then did what the error told it to do: it called compress for m00356..m00388 with a summary of its own work and got {"outcome":"applied","blocksCreated":1}. Those refs never existed in the subagent's context. A subagent replaced 33 messages of another conversation with an unrelated summary.

Cause

  • Claude Code runs one bili MCP server process per session and Task subagents share it. The shim reads CLAUDE_CODE_SESSION_ID once, so every subagent call is posted with the root conversation id.
  • The proxy's outbound witness (sha256 of tool name + args, 10-min ring) catches only some of these calls (409 TOOL_CONVERSATION_CONFLICT). When the lookup misses, the call runs against the root session.
  • The [PR] fix(#4168): self-rebind stale claude MCP binding on witness conflict — management tools recover after host-native compaction #4219 shim self-rebind (on master, after v0.1.206) also changes the process-wide binding. One binding cannot serve a parent and its running subagents at the same time.

Proposal

The Claude Code 2.1.308 CLI bundle attaches a per-call id to MCP tools/call: _meta: {"claudecode/toolUseId": <tool_use id>} (I found this in the bundle; I have not captured it on the wire). The proxy already sees that tool_use block in the response stream of the session that emitted it (settleWitnesses / recordJsonToolWitnesses). Suggested:

  1. When settling witnesses, also record tool_use.id → session.id (including |sub: lanes).
  2. In the shim, forward _meta["claudecode/toolUseId"] as host-stamped per-call identity, like _meta.threadId from Codex Code Mode 压缩失败:MCP shim 忽略每次调用的 _meta.threadId,错误使用过期 env ID #2024.
  3. In the plugin tool route, resolve the session by that id first, and use the current ladder only when it is missing.

This would also cover the resume / host-compaction case of #4168 without a process-wide rebind.

Related: #4352 (proxy-side split for VS Code subagents), #4168 / #4219, #2072, #2024, #760, #975.

Activity

  1. ranxianglei commented on Oct 24, 2026

    @ranxianglei
    Owner

    🤖 Powered by ework · qwen3.8-27b

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

    查重: 不重复。相关但不同:#4352/#4409(子代理模型请求的 |sub: 车道拆分——本 issue 的前提,已合入)、#4168/#4219(resume/native-compaction 导致的进程级绑定过期 + 一次性 self-rebind——同一 shim 家族、不同触发机制)、#2728(/compact 后 ACP 状态失配——不同症状)。无在途 PR 做 per-call tool_use id 路由。

    标签: bug claude-code scope/wiring p1(缺陷;Claude Code CLI 车道;MCP shim/插件工具路由接线;p1——Task 子代理压缩实质整体不可用(50 次调用仅 4 次落地且其中一次落错会话),并伴随父会话被无关 fold 污染的跨会话状态破坏)。

    已按 master 代码逐条核实,你的因果链成立:

    1. 绑定是进程级的:src/mcp.ts:83 CONVERSATION_FROM_ENV = CLAUDE_CODE_SESSION_ID || BILI_CONVERSATION_ID,整个 shim 生命周期一个值;每个 tools/call 都把它作为 body conversationId 发出。
    2. 拆分只发生在模型请求侧:子代理的模型请求经 claudeSubagentSplit(src/session-id.ts:427,Subagent first token stalls 1-2min: withSessionLock covers full forward + slow flash upstream first byte #970/Subagent requests share the parent session id and trip fold-reconcile #4352/[PR] fix: split header-less claude code subagents onto their own session lane (#4352) #4409)落到 <id>|sub:<agent> 会话,witness 记录(settleWitnesses, src/plugin.ts:2466)记在拆分后的 session id 下。
    3. 409 类(你的 Case 1):路由阶梯(src/plugin.ts:1853-1894)对「body id=root 且唯一 witness=sub 车道」的非 nativeCaller 调用 fail-closed → TOOL_CONVERSATION_CONFLICT,错误文案与你贴的完全一致。
    4. miss 类(你的 Case 2):(name,args) hash 查不到时没有冲突分支,调用直接在 root 会话执行——status 描述的是 root(249 msgs/m00356–m00388),随后子代理照单抓药把 root 的 33 条消息折进了自己的摘要。这是最危险的一类:静默跨会话写。
    5. [PR] fix(#4168): self-rebind stale claude MCP binding on witness conflict — management tools recover after host-native compaction #4219 的 self-rebind 治不了这个:它改写的是共享 shim 的全局绑定,一个进程服务父+多个并行子代理时必然顾此失彼(甚至可能把绑定改到某个子代理上,反过来打坏主会话)。

    方案评估:你提的 per-call _meta["claudecode/toolUseId"] 通道打在根因上——per-call 宿主盖戳身份替代进程级猜测,409 类和 miss 类都被确定性消除(不再依赖 (name,args) hash 命中),同时覆盖 #4168 的 resume 场景且无需 rebind。我按此实现,两处加固:

    • tool_use id → session 映射记录时连同工具名一起存,查询侧做名字交叉校验(防 id 碰撞误路由),不匹配则软回退旧阶梯;
    • id 查不到(TTL 过期/代理重启/老版本 CLI 不带 _meta)时完整走现有阶梯,fail-closed 行为逐字节不变——老客户端零行为漂移。

    接下来:tool-ring 扩展 + shim 转发 + 路由阶梯第 0 步,三层测试(shim 契约 / 代理路由策略 / 真实 server+shim 事故形态 e2e),PR 引用本 issue。

  2. added 2 commits that reference this issue on Oct 27, 2026
    97d4574
    6ec0e97
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions