Skip to content

[PR] fix(#4168): self-rebind stale claude MCP binding on witness conflict — management tools recover after host-native compaction - #4219

Closed
ranxianglei wants to merge 1 commit into
masterfrom
2026-10-22_claude-mcp-stale-binding-rebind
Closed

ranxianglei wants to merge 1 commit into
masterfrom
2026-10-22_claude-mcp-stale-binding-rebind

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Closes #4168

Problem

After Claude Code's host-native compaction (or any host-side session-id rotation — resume/fork), all bili management tools (compress / acp_status / …) fail permanently for the rest of that session:

  • compress / acp_status → bili tool error: outbound tool witness does not match conversationId (409);
  • once the 10-min witness TTL expires, calls silently execute against the wrong (often empty) session: "every ref is unknown to this session … highest allocated ref=none … 0 char(s)";
  • no recovery within the session.

Root cause (verified at reporter's v0.1.197): the MCP shim captures CLAUDE_CODE_SESSION_ID exactly once at spawn (src/mcp.ts:83). When the host later moves the conversation to a new session id, the stale id still resolves — so every call fails closed at the outbound-witness conflict (routing ladder's !nativeCaller branch). #656's orphan adoption only fires on 404 ("no model request has arrived"), so it never sees this failure mode and the binding stays stale forever. A latent second defect: even when adoption DID fire (#656's 404 path), the immediate retry re-POSTed the stale id because effectiveConversationId was captured above the retry loop.

Changes

  1. Proxy — make the divergence visible (src/plugin.ts): the TOOL_CONVERSATION_CONFLICT 409 keeps its fail-closed semantics, machine-readable code, and the original error prefix, but now also
  2. Shim — self-heal (src/mcp.ts): forwardTool adopts the UNIQUE witnessing session on that 409 — one retry only — under the same gate as MCP tools fail with 'unknown plugin conversation' after Claude Code --resume (shim holds a stale session id) #656 (ORPHAN_ADOPT: identity-bound claude shims; opt out with BILI_MCP_NO_ORPHAN_ADOPT=1). Adoption requires the proxy's recency facts to prove traffic moved to the witness AFTER the bound session's last activity. Multi-witness, still-fresh binding, missing facts (older proxy), per-call-override ids, and a second consecutive conflict all stay on the loud 409 — fail closed, never guess.
  3. Shim — retry correctness: effectiveConversationId is recomputed per attempt, so an in-loop adoption (both the new [Bug] Claude Code native compaction: compress/acp_status permanently broken after resumption — outbound tool witness doe | [Bug] Claude Code 原生 compaction 续接后 compress/acp_status 永久失效:outbound tool witness does not match conversationId,ref 空间为空 #4168 path and the pre-existing MCP tools fail with 'unknown plugin conversation' after Claude Code --resume (shim holds a stale session id) #656 path) retries with the NEW id instead of silently re-POSTing the stale one.

Net effect for the reported incident: the first conflicting call after the host moves the session transparently re-binds the shim to the live conversation and succeeds — compress/acp_status keep working without user action. Callers that don't run this shim get an actionable error naming the live conversation instead of an opaque string.

Behavior changes (old → new)

Surface Old New Why
409 error text fixed string outbound tool witness does not match conversationId same phrase as prefix + actionable suffix naming the live conversation (unique-witness case) or "refuse to guess" (multi) model/user can see what drifted and how to recover; code unchanged, prefix preserved for existing consumers
409 body {ok, code, error} additive fields conversationId, namedSessionId, witnessSessions, namedSessionLastSeen, witnessLastSeen machine-readable facts for the shim's guarded adoption
MCP shim on 409 conflict always threw (binding stuck forever) one-shot adoption of the unique witnessing session under strict recency guards, then retry restores the session after host-side id rotation (#4168)
MCP shim retry after ANY adoption silently re-POSTed the stale id (the immediate retry always failed again) retries with the adopted id latent #656 defect; the immediate call now actually recovers

No config surface added (reuses existing BILI_MCP_NO_ORPHAN_ADOPT), no kernel change, no wire/model-traffic change, no cache/retry/cursor impact.

Tests

New: tests/issue4168-claude-compaction-rebind.test.ts (10 tests, both gate directions per #3617 discipline):

  • proxy: enriched payload shape; multi-witness names nothing; matching witness unaffected (negative); native caller still outranks a conflicting witness (adjacent-branch regression).
  • shim: adoption positive (retry carries the adopted id); negatives — fresh bound session (multi-window safety), multi-witness, per-call override leaves the global binding untouched, missing facts (older proxy), second consecutive conflict throws instead of looping.

Targeted local runs (green): the new suite + tool-ring, host-native-caller, issue2072-native-caller-stamping, issue2601-persona-fork-lineage, plugin-fork, claude-mcp-entry, mcp-native-thread (real server + real shim over stdio). Typecheck + build green on the rebased head (c87185a). Full-suite verdict: remote CI on this head.

Note: master is currently in a CI red storm unrelated to this PR (#4196 — node-22 lanes fail 15 window/preflight tests identically on master).

Out of scope (tracked elsewhere)

…e session

A claude MCP shim captures CLAUDE_CODE_SESSION_ID once at spawn. When the
host later moves the conversation to a new session id (native-compaction
continuation / resume), the stale id still RESOLVES, so every management
call fails closed on the outbound-witness 409 forever - #656's adoption
only ever saw 404s and never fired, leaving the session lost for good.

- proxy: the TOOL_CONVERSATION_CONFLICT 409 keeps its fail-closed code and
  error prefix but now names the unique witnessing conversation (so the
  model can re-route via the #760 per-call channel) and carries additive
  recency facts (conversationId/namedSessionId/witnessSessions/
  namedSessionLastSeen/witnessLastSeen) for machine consumers.
- shim: forwardTool adopts the UNIQUE witnessing session on that 409, one
  retry, under the same ORPHAN_ADOPT gate (BILI_MCP_NO_ORPHAN_ADOPT=1
  opt-out) as #656 - only when the recency facts prove traffic moved
  after the bound session's last activity; multi-witness, fresh-binding,
  missing-facts and per-call-override all stay on the loud 409.
- shim: effectiveConversationId is recomputed per attempt so an in-loop
  adoption retry actually carries the NEW id (previously the #656 404
  adoption's immediate retry silently re-POSTed the stale id).

Regression tests: tests/issue4168-claude-compaction-rebind.test.ts
(proxy payload shape incl. both gate directions; shim adoption positive +
five negative cases).
@ranxianglei ranxianglei changed the title fix(#4168): self-rebind stale claude MCP binding on witness conflict — management tools recover after host-native compaction [PR] fix(#4168): self-rebind stale claude MCP binding on witness conflict — management tools recover after host-native compaction Oct 21, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-22_claude-mcp-stale-binding-rebind (90f5f6d)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.5010-pr.4219.9e59a3a

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-pr4219.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

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

查重:不重复——检索 open/closed PR 中引用 #4168 的条目,仅本 PR;#2849(线级拒绝 CC 原生 compaction)是触发器侧预防、方向不同,正文已声明 out-of-scope、与本 PR 互补而非并列。

标签:bug, claude, scope/wiring, p1(宿主侧会话 id 轮换后压缩管理面整场停摆、会话内无恢复路径、用户反馈多次复发——核心能力可用性缺陷)

收到。本 PR 带 ework-agent-pr 标记,属已完成的 agent 工作——不重做,改为拉分支核对代码与描述是否一致 + 核查该 head 的 CI 状态。正在审,结构化结论稍后给。

@ranxianglei ranxianglei added bug Something isn't working claude p1 scope/wiring labels Oct 21, 2026
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

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

审核完成:方向正确、实现扎实、证据链闭合——可合(需人工合并);独立审出 1 个边界缺陷需要你拍板(见第 5 问发现 A)。

独立核验证据(未照抄 PR 正文结论,全部自己重做):

  1. 根因在你的版本 v0.1.197 上重新钉死:shim 出生即捕获 CLAUDE_CODE_SESSION_ID、永不重读(src/mcp.ts:83);唯一自愈只认 404「no model request has arrived」(src/mcp.ts:198-199);effectiveConversationId 在循环外捕获(:155)——「收养后重试仍带旧 id」的潜伏缺陷属实。三个代码级事实全部成立。
  2. 全 diff + 周边逐段精读(3 文件 +339/-3):409 分支位于 session && witnessIds.size>0 && !witnessIds.has(session.id) 守卫内,session.id/lastSeen 访问安全;resident 路径走非 stamping 的 peekSession(时序事实真实);nativeCaller 优先序、[Bug] dsh 会话反复 model/provider 切换时主对话迁移到 |sub:<fp> persona fork,宿主盖章的 compress/decompress/acp_status 仍解析在陈旧父 id —— ref 跨空间不可解析直至熔断 #2601 lineage 分支、witness 匹配路由、无 body-id 多 witness 400 等相邻路径均未触碰。
  3. CI(本 head 90f5f6d):124 个 check-run 全部 success/skipped(skipped 均为 env-gated e2e 车道,设计内跳过),与 master 红风暴无关——本 head 的 node-22 test 车道是绿的。
  4. 本地定向实跑(我在 PR head 上亲自跑,非引用声称):新套件 + tool-ring / host-native-caller / issue2072 / issue2601-persona-fork-lineage / plugin-fork / claude-mcp-entry / mcp-native-thread 共 124 例全绿;typecheck + build 绿。
  5. 测试质量:10 例全有牙——proxy 侧驱动真 handlePluginTool,用日志行断言「冲突分支什么都没执行」(fail-closed 钉死);shim 侧真 HTTP mock(port 0,确定性),重试契约按 POST 体断言(第一条带旧 id、第二条带收养 id);gate 双向齐全(多窗口 fresh 绑定/多 witness/per-call 覆盖/缺时序事实/二次冲突全部钉为「不猜」)。无恒真断言。
  6. 行为变更披露核对:PR 正文四项与代码逐项一致、无遗漏;全仓 grep 无任何其他位置断言旧 409 全串(新测试用前缀正则钉住),既有消费者不受影响。

结构化八问:

  1. 是什么问题:Claude Code 宿主侧 compaction/resume 轮换会话 id 后,MCP 通道的静态绑定永久锁死在旧 id,compress/acp_status 在该会话剩余时间硬 409(witness TTL 过期后甚至会在空会话上静默执行)。
  2. 什么原因导致的:三层——触发器=宿主侧 id 轮换;机制=绑定时出生即定格、永不重读,而旧 id 在 proxy 侧仍可解析,于是每次调用都撞上 outbound-witness 冲突的 fail-closed;无自愈的原因=唯一自愈(MCP tools fail with 'unknown plugin conversation' after Claude Code --resume (shim holds a stale session id) #656 孤儿收养)只认 404,本形态永不产生 404。另含潜伏缺陷:即便 404 路径收养成功,重试仍 POST 旧 id(变量在循环外)。
  3. 影响范围与严重程度:所有经 bili 的 Claude Code 长会话一旦宿主侧换 id 即压缩管理面整场停摆、会话内不可恢复;聊天本身仍可用。用户反馈多次复发 → p1。修复覆盖主形态(stale 会话仍驻留内存);一个未覆盖的边界形态见第 5 问发现 A。
  4. 怎么修复的:双层契约——proxy 409 保持 fail-closed(code/前缀不变)但说出真相:唯一 witness 时点名活会话(模型可经 feat: per-call conversation_id for MCP tools — let the model supply its session id #760 per-call 通道自行改道)+ 附双方身份与时序事实(增量字段);claude 身份绑定 shim 在严格护栏(唯一 witness + 时序证明流量在被绑会话最后活动之后才移走 + 复用 BILI_MCP_NO_ORPHAN_ADOPT 开关)下一次性重绑并重试一次;同时把 effectiveConversationId 移进循环,修掉「重试带旧 id」潜伏缺陷。
  5. 是否有回归问题:触碰路径的既有行为逐项清点——① code/错误前缀兼容 ✓;② fail-closed 语义不变、冲突分支不执行任何东西(测试钉住)✓;③ nativeCaller 优先序不变(相邻分支回归测试)✓;④ witness 匹配路由不变(gate 负向)✓;⑤ [Bug] dsh 会话反复 model/provider 切换时主对话迁移到 |sub:<fp> persona fork,宿主盖章的 compress/decompress/acp_status 仍解析在陈旧父 id —— ref 跨空间不可解析直至熔断 #2601 lineage 分支不变 ✓;⑥ 无 body-id 多 witness 400 不变 ✓;⑦ MCP tools fail with 'unknown plugin conversation' after Claude Code --resume (shim holds a stale session id) #656 原路径严格变好(重试终于携带新 id)✓。独立审出两个发现:
  6. 是否破坏缓存:不涉及——只动管理调用的路由响应体与 shim 绑定状态,前缀缓存/去重键/增量游标零触碰。
  7. 是否改了设计方向:不转向——补全既有 MCP tools fail with 'unknown plugin conversation' after Claude Code --resume (shim holds a stale session id) #656 自愈机制漏掉的两条路,让 Codex Code Mode 压缩失败:MCP shim 忽略每次调用的 _meta.threadId,错误使用过期 env ID #2024 的 fail-closed 至少说出真相;架构/边界/数据流不变。
  8. 是否引入新的配置:无(复用既有 BILI_MCP_NO_ORPHAN_ADOPT opt-out;config surface 未动,docs-coverage/docs-sync gate 绿)。

判定:主机制端到端验证通过、CI 在本 head 全绿、测试有牙——可合。session-binding 属人工审核层,请手动合并:#4219 (mergeable_state=blocked 系人工审批要求,checks 本身全绿)。发现 A 请你拍板(并入 vs follow-up);发现 B 仅记录。线②决策(D1 合并 #2849 / D2 继任者跟随)仍待你拍板,见 #4168 floor 4。

@ranxianglei

Copy link
Copy Markdown
Owner Author

(ai: zhipuai-lb/glm-5.3)

已并入合并列车 #4525(master 6d7c587,合并方式 merge commit,提交保留原作者署名)。本 PR 关单收尾;若列车中对应提交出问题,回滚点为 #4525 单提交 revert。

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

Labels

bug Something isn't working claude p1 scope/wiring

Projects

None yet

1 participant