Skip to content

[PR] Task sub-agent bili calls are no longer compressed into the parent session (#4580) | Task 子代理的 bili 调用不再压缩到父会话 (#4580) - #4978

Merged
ranxianglei merged 3 commits into
masterfrom
2026-10-27_claude-subagent-identity
Oct 28, 2026
Merged

ranxianglei merged 3 commits into
masterfrom
2026-10-27_claude-subagent-identity

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

修什么

Claude Code 的 Task 子代理和主线程共用一个 MCP shim 进程。shim 启动时抓的绑定只认主会话,但子代理的模型流量走的是代理里另一条 lane —— 结果子代理的 compress/搜索调用全部按父会话路由:refs 全 unknown、压错对象,或者撞上 witness 409 永久失败(issue #4580)。

怎么修(三层)

Claude Code ≥2.1.308 在每次 MCP tools/call 上盖 _meta["claudecode/toolUseId"](即本次调用的 tool_use 块 id)。代理是模型输出的唯一通道,出站时见过每个 tool_use 属于哪条 lane —— 两侧对上号,id 即 lane:

  1. 记录:tool-ring 新增 tool_use id → 会话映射(TTL 10 分钟,容量 1024);每条响应管道在 witness settle 的同时记 id(anthropic SSE / openai SSE / responses SSE / 非流式 JSON,仅在 id 真实存在时)。
  2. 路由:工具路由阶梯最前插入 —— body toolCallId(复用 agent/shared.ts 既有字段)命中且 lane 存活 → 直接路由到该 lane(host-stamped 权威,同 Codex Code Mode 压缩失败:MCP shim 忽略每次调用的 _meta.threadId,错误使用过期 env ID #2024);未命中回退旧阶梯,fail-closed 语义不变。
  3. shim:mcp.ts 读 _meta id 转发为 body toolCallId,不动 routeOverride、不动全局绑定、不误标 nativeCaller。

与 PR #4774 的关系

#4774 把无 header 的子代理切到独立 session lane(修 #4769/#4862 的根因)。本 PR 补的是它没覆盖的最后一环:lane 切分之后,共用 shim 的绑定仍指向父会话,子代理的 MCP 调用仍会路由错 —— 本 PR 用 host 盖的 id 直接路由。两个 PR 独立成立,可分别合并。

测试

  • 新增 tests/issue4580-claudecode-tooluseid-routing.test.ts:12 例(4 条管道记录、双 lane 分离、TTL、畸形 id、子代理路由命中+日志、环 miss 回退、空/超长 id 400、无 id 时 409 不变、shim 转发 body)。
  • 相邻回归全绿:tool-ring / tool-witness-pipes / issue4168 / issue2601 / plugin-fork / mcp-native-thread / issue3472 / claude-mcp-entry(97+8+35 用例)。
  • devlog: devlog/2026-10-27_issue4580-tooluse-id-routing/{REQ.md,DESIGN.md}

Fixes #4580

Claude Code 的 Task 子代理和主线程共用一个 MCP shim 进程:shim 启动时
抓的绑定只认主会话,而子代理的模型流量走的是代理里另一条 lane,于是子代
理的 compress 全部压到父会话上(refs 全 unknown/误折),或撞 witness 409
永久失败。

Claude Code ≥2.1.308 在每次 tools/call 盖 _meta["claudecode/toolUseId"]。
本改动三层:

- tool-ring 新增 tool_use id → 会话 映射(TTL 10 分钟,容量 1024);
- plugin 的每条响应管道在 witness settle 时同时记 id;工具路由阶梯最前
  插入:id 命中且 lane 存活 → 直接路由(host-stamped 权威,同 #2024),
  未命中回退旧阶梯,fail-closed 不变;
- mcp shim 把 _meta id 转发为 body toolCallId(复用 agent/shared.ts 既有
  字段),不动 routeOverride、不动全局绑定。

tests: issue4580-claudecode-tooluseid-routing.test.ts (12 例,记录/路由/shim)
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-27_claude-subagent-identity (97d4574)

Latest build — install this exact version

npm install -g billion-context@0.0.6527-pr.4978.eee7d4d

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

Recent builds (commit → version)

commit version build
97d4574 billion-context@0.0.6527-pr.4978.eee7d4d build
473be2a billion-context@0.0.6516-pr.4978.bae305a build
7009565 billion-context@0.0.6499-pr.4978.13c01dc build

Download artifact

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

Install instructions are refreshed on each push.

@ranxianglei ranxianglei changed the title Task 子代理的 bili 调用不再压缩到父会话 (#4580) [PR] Task sub-agent bili calls are no longer compressed into the parent session (#4580) | Task 子代理的 bili 调用不再压缩到父会话 (#4580) Oct 27, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复。 本 PR 是原 issue #4580(qiangu2020 报告:Claude Code Task 子代理的 bili MCP 压缩落到父会话)的实现——PR 正文「Fixes #4580」对应关系成立。相关但不同的在途/已立项条目:

标签: bug claude-code scope/wiring p1(缺陷;Claude Code CLI 车道;MCP shim/插件工具路由接线;p1——原 issue 数据:子代理 50 次 compress 仅 4 次落地且其中一次把父会话 33 条消息折成无关摘要 = 跨会话数据破坏)。

开始审核:已拉取分支 pr-4978(head 7009565fc,base 7f2e11904,即当前 master;diff = 7 文件 +449/-6:src/mcp.ts、src/plugin.ts、src/tool-ring.ts、新测试 267 行、devlog×2、CHANGELOG 一行)。接下来逐层审代码与回归面,定向跑相邻测试,读 CI 状态后给八问结论。

ranxianglei and others added 2 commits October 27, 2026 23:48
Review of #4978: the capacity-eviction scan broke on its first entry —
the early-exit condition is always true for the element that just set the
running minimum — so eviction actually dropped the FIRST-INSERTED id
regardless of age. A re-recorded id keeps its insertion slot while
refreshing its timestamp, so position order is not age order. Remove the
early break (full scan over a <=1024-entry map, only past the cap) and pin
the intended behavior with a regression test that fails on the old code.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

(ai: vllm-qwen/qwen3.8-27b) Merge-guard "behind" was stale — no action needed: branch 2026-10-27_claude-subagent-identity @ 97d4574 is already in sync with master (0 behind, 3 ahead; master merged in at 473be2a). No conflicts (mergeable: true); state blocked is pending CI only — 22 checks queued/running on head 97d4574, 0 failed.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

审核完成。独立重读了 PR 全量 diff、路由阶梯相邻代码与全部 settle 管道,本地在真实 head(97d457492)定向跑了测试。结论:代码可合,待 CI 绿;期间发现并顺手修掉 1 处真实缺陷(见下)。

结构化八问

  1. 是什么问题:Claude Code Task 子代理与主线程共用一个 MCP shim 进程,shim 启动时抓的绑定只指向父会话;子代理的 compress/search 等 bili 调用全部按父会话路由——压错对象(原 issue 实例:子代理把父会话 33 条消息折成无关摘要,跨会话数据破坏)、refs 全 unknown、或撞 witness 409 永久失败。
  2. 什么原因导致的:进程级绑定(CLAUDE_CODE_SESSION_ID shim 启动时取一次)无法表达 per-call 身份;(name,args) witness hash 对父/子同名同参调用不可区分,miss 时静默落到 body id(父)。根因是缺一条 per-call 宿主盖戳身份通道,不是路由阶梯判错——阶梯在现有证据下都是对的。
  3. 影响范围与严重程度:Claude Code CLI 车道 + Task 子代理 + MCP 模式。子代理压缩实质不可用(报告者实测 50 次仅 4 次落地,其中 1 次污染父会话);主线程行为不受影响。p1。
  4. 怎么修复的:三层——①每条响应管道 settle witness 的同时把 tool_use id→lane 记入新 id 环(TTL 10min、容量 1024、last-write-wins,observe-only);②工具路由阶梯最前插入:id 环命中且 lane 存活 → 直接路由(host-stamped 权威,与 Codex Code Mode 压缩失败:MCP shim 忽略每次调用的 _meta.threadId,错误使用过期 env ID #2024 native caller 同信任类),未命中走旧阶梯,fail-closed 逐字节不变;③shim 把 _meta["claudecode/toolUseId"] 严格校验后转发到 body 既有 toolCallId 字段,不碰 routeOverride/全局绑定/nativeCaller。
  5. 是否有回归问题:无。逐项审过(清单见下)。关键判定:toolCallId 字段此前已被原生车道使用但只参与校验与 fold callId、不参与路由——现在它获得路由权威,实际受影响面只有 pi 车道(唯一现存发送方,src/agent/pi.ts:622;dsh/opencode/kimi 等均不发该字段):pi 发出的 id 必然在自身 lane 的出站流里被见证过,环命中指向的 lane == 旧阶梯解析出的 lane,实践为 no-op;环 miss(重启/TTL)时与旧行为完全一致。[Bug] dsh 会话反复 model/provider 切换时主对话迁移到 |sub:<fp> persona fork,宿主盖章的 compress/decompress/acp_status 仍解析在陈旧父 id —— ref 跨空间不可解析直至熔断 #2601/Compression fails in long sessions while new sessions work fine | 压缩失败.新会话反而没事 #3397 的 persona lineage follow 路径不受触碰(dsh 不发 toolCallId)。
  6. 是否破坏缓存:不涉及——前缀缓存接缝、消息重建、块状态零改动,纯工具路由决策面新增。
  7. 是否改了设计方向:架构不变(kernel/、存储、更新路径未碰);路由权威增加一档「host-stamped call id > body/witness 阶梯」,属既有 Codex Code Mode 压缩失败:MCP shim 忽略每次调用的 _meta.threadId,错误使用过期 env ID #2024 信任类的自然延伸,非新信任模型(宿主本就能在 body 里指名任意 conversationId)。
  8. 是否引入新配置:无——复用既有 body 字段(已有 ≤512 校验,src/plugin.ts:1923),无新 env/config/默认值。CHANGELOG 已按惯例补 Fixed 条目。

回归审查清单(diff 触碰路径的既有行为)

测试有效性

12 例是真行为测试(真入口 pipePluginChatWithStrip/pipePluginResponsesWithStrip/pipePluginJson/handlePluginTool/forwardTool + 真 HTTP server),断言有牙,且双向完整(命中路由+日志 / miss 回退+无 #4580 日志 / 409 不变 / 400 校验 / shim 不误标)——符合 #3617 门禁纪律。本地结果:新文件 13 例 + 相邻 9 套件(tool-ring / tool-witness-pipes / issue4168 / issue2601×2 / plugin-fork / mcp-native-thread / issue3472 / claude-mcp-entry)全部通过(共 163 例,tsc --noEmit 干净)。小缺口一处:「环命中但 lane 不驻留(peekSession undefined)→ 回退」无专门用例(TTL 测试覆盖了 lookup 层过期,未覆盖驻留检查)——低风险,不拦。

审核发现并已当场修掉(1 处,commit 97d457492,已推到 PR 分支)

id 环容量逐出扫描有个早退条件恒真于首个元素(刚刷新最小值的元素必然满足 oldestAt === v.at),导致扫描永远只看第一个条目——实际逐出的是「最先插入」而非「最旧时间戳」。由于重记(retry/replay)保留插入位却刷新时间戳,先插入者可能恰是最新鲜的一条,极端容量压力下会误逐新 id。功能影响很小(TTL 通常先于容量生效),但与注释声明的行为不符,属真缺陷:删掉早退改为全量扫描(≤1024 条、仅在超容时执行),并补了回归测试——该测试在修前代码上红、修后绿(已验证)。同 PR 内扫过同类模式,无其他发现。

需注意的前提(不拦)

  • 「Claude Code ≥2.1.308 每次 tools/call 盖 _meta["claudecode/toolUseId"]」来自原报告者在 CLI bundle 里的发现,未见 wire 抓包确认。设计对此安全降级:不带 _meta 的老 CLI / 形状漂移 → shim 视为缺失 → 行为与从前完全一致,只是修复不生效。建议合并后由报告者或 owner 实机确认一次生效日志(routed by host call id (#4580))。

CI 状态

push 后 CI 正在新 head 97d457492 上跑(提交时 22 queued / 0 failed,gate 已绿)。本地定向验证已过,全量以 CI 为准。

判定

审核通过,建议 CI 绿后合并。 本 PR 触碰会话路由权威面(§7.4 人审区),合并由 owner 执行。与 #4774 独立成立、可分别合并的关系声明属实(两者修同一故障链的不同环节:lane 切分 vs 共用 shim 绑定)。

@ranxianglei
ranxianglei merged commit 6ec0e97 into master Oct 28, 2026
51 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.890-master.6ec0e97 (rolling master tag).

npm install -g billion-context@0.0.890-master.6ec0e97  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

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

Projects

None yet

1 participant