Skip to content

[PR] fix: claude MCP shim follows a Remote Control child's session id - #6920

Closed
MohammadErfan-Jabbari wants to merge 3 commits into
ranxianglei:masterfrom
MohammadErfan-Jabbari:2026-11-16_claude-rc-session-rebind
Closed

MohammadErfan-Jabbari wants to merge 3 commits into
ranxianglei:masterfrom
MohammadErfan-Jabbari:2026-11-16_claude-rc-session-rebind

Conversation

@MohammadErfan-Jabbari

Copy link
Copy Markdown
Contributor

Fixes #6917

Stacked on #6478. The first two commits are #6478's, unchanged (029b7e0, 9378f68); this PR adds only the last commit. Once #6478 lands, the diff shrinks to that commit.

Problem

A Remote Control child starts bili's MCP server under a fresh CLAUDE_CODE_SESSION_ID, then sends every model request under the remote session's id, so the server registers the wrong session. #6478 re-reads the host's sessions file, but only on a bili tool call, and it finds the host only by a process named claude. Remote Control starts its children by the native install's path (~/.local/share/claude/versions/2.1.323), so the lookup finds no host.

Fix

  • watchClaudeSession (src/mcp.ts), started by the claude entry (src/claude-mcp-entry.ts): re-reads the host's sessions file every second from start-up and re-binds and re-registers when the id changes, so the registration is right before any tool call.
  • isClaudeHostArgv (src/claude-native-bootstrap.ts) accepts …/claude/versions/<x.y.z>(.exe) as argv[0].
  • The resolver is injected only by the claude entry, as in [PR] fix: re-bind the claude MCP shim to the host's current session after /clear #6478; dist/mcp.js for codex/zcode/kimi is untouched.

Both compression modes

  • Plugin mode (Claude Code with bili's MCP server): the registration now names the session the child actually uses, so bili binds the session in plugin mode.
  • Proxy mode: no MCP server runs, so nothing changes.

The registration alone does not fold these sessions: their requests are Message Threads deltas until #6915 declines the beta for sessions bili compresses.

Tests

  • tests/claude-rc-session-rebind.test.ts (new) reproduces the measured case: the spawn id is 3847869d-…, the sessions file says cc233f11-…, and there is no tool call. The shim registers the host's id. A negative row: when the host keeps the spawn id, nothing registers. It fails on [PR] fix: re-bind the claude MCP shim to the host's current session after /clear #6478's branch (watchClaudeSession is not a function) and passes with the fix.
  • tests/claude-native.test.ts gains the versioned-path rows, positive and negative (…/versions/2.1.323.tmp, /opt/other/versions/2.1.323); the isClaudeHostArgv test fails without the change.
  • With the fix, claude-rc-session-rebind, claude-native and both issue6361-* suites pass (93 tests) and npm run typecheck is clean. Four claude-native tests fail when run from inside a Claude Code session because they read its environment (CLAUDE_CODE_*, HTTPS_PROXY); in a clean environment all pass, with or without this change.

Live result

On a private proxy (Haiku) with a real claude remote-control session, the registration matched the host's id within a second. Together with #6915, the session was declined once, then folded about 57,000 tokens after a nudge, with no rejected request besides the intended decline.

Review tier

This changes which session the MCP server binds to, which is identity/session-binding logic, so per §7.4 it stays with a human reviewer. No config, persistence-format, wire-shape or kernel change.

🤖 Generated with Claude Code

ci-check and others added 3 commits November 13, 2026 11:42
…/clear

Claude Code /clear (or /new) starts a NEW session id but keeps the stdio MCP
child running, so the spawn-time CLAUDE_CODE_SESSION_ID env (mcp.ts's
CONVERSATION_FROM_ENV) goes stale and neither recovery path fires: the ranxianglei#656 404
adoption needs the bound id to be unknown (it is still registered + active, so
calls succeed against it) and the ranxianglei#4168 409 re-bind needs a witness conflict
(none occurs). Tools silently run against the OLD session and the NEW session
never enters plugin mode.

The claude entry now injects a resolver that reads the host's CURRENT session
id from <CLAUDE_CONFIG_DIR|~/.claude>/sessions/<host-pid>.json (rewritten on
/clear; the host pid is stable for this child's lifetime). On each tools/call
the shim re-reads it and, when it differs from the bound id, re-binds +
re-registers so the new session enters plugin mode. Identity-bound hosts only;
dist/mcp.js (codex/zcode/kimi) is untouched — the resolver is injected solely
by the claude entry.

Closes ranxianglei#6361
A Remote Control child (claude --print --sdk-url … --resume=<session
url>) spawns the shim under a fresh CLAUDE_CODE_SESSION_ID, then sends
its model requests under the remote session's id. The shim registered
the spawn id, so the proxy never saw the session as one bili compresses.

The ranxianglei#6361 re-bind ran only on a bili tool call, and found the host only
by a process named claude. The claude entry now re-checks the host's
sessions file on a 1 s timer from start-up (watchClaudeSession), and
isClaudeHostArgv accepts a native install path
<data dir>/claude/versions/<x.y.z>, which is how Remote Control spawns
its children.

Fixes ranxianglei#6917

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ranxianglei ranxianglei changed the title fix: claude MCP shim follows a Remote Control child's session id [PR] fix: claude MCP shim follows a Remote Control child's session id Nov 16, 2026
@ranxianglei

Copy link
Copy Markdown
Owner

🤖 Powered by ework · qwen3.8-27b

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

查重: 不重复 — no other open PR/issue targets the Remote Control session-id registration. Nearest neighbors, all distinct: stacked base #6478 (same re-bind seam, different trigger — per-call vs timer; explicitly complementary, declared in the PR body); #6918/#6915 (upstream message-threads beta decline — different mechanism, companion work, not a duplicate).

Independent review of PR #6920 (Fixes #6917)

① Direction — correct, root cause. Verified the diagnosis against the code: an RC child spawns the shim under a fresh CLAUDE_CODE_SESSION_ID, then adopts the remote session's id (written to ~/.claude/sessions/<child-pid>.json) before its first model request. The #6361 re-bind ran only on tools/call (an RC session may never call one), and isClaudeHostArgv walked past the native install path (~/.local/share/claude/versions/2.1.323), so the host lookup found nothing and the resolver never ran. The fix attacks both producers of the wrong registration (trigger timing + host identification), not the symptom. Verified the proxy side closes the loop: an identity: true registration lands in registeredIds (src/plugin.ts:993) and is consumed by consumePluginRegisterFor on every model request (src/server/handle.ts:1833-1840), so registering the right id before the first request flips the session into plugin mode.

② Regression review — existing behaviors checked, all preserved:

③ Code review — sound. watchClaudeSession is minimal: immediate check + unref'd interval (process can still exit on stdio close) + stop handle for tests. The per-tick cost is one small file read; the origin resolution + register POST fire only on an actual id change. Fire-and-forget register with 5 s timeout mirrors the initialize path. One cosmetic note, no action needed: the re-bind stderr line says "(claude /clear?)", which is a diagnostic hint inherited from #6478's code and now also fires for RC re-binds — the line still states the operative facts (old id → new id, re-binding).

④ Test review — real tests, both gate directions, regression-verified. tests/claude-rc-session-rebind.test.ts drives a real HTTP mock on 127.0.0.1:0, a real mkdtemp sessions file, and the real resolver — no tool call, no initialize, timer only (exactly the measured case). Positive row (host adopts remote id → registers it) and negative row (host keeps spawn id → registers nothing). I reproduced the PR's regression claims on #6478's head: the new file fails with exactly watchClaudeSession is not a function, and the three new isClaudeHostArgv rows fail the exact-match test (67 pass / 1 fail) without the change. Both hold.

⑤ Diff cleanliness — clean. 7 files / +667 −2 vs master; the PR's own commit is 5 files / +102 −1. Every line serves the purpose; no whitespace churn, no lockfile, no generated artifacts.

Local evidence (PR head b521f47): build OK; npm run typecheck clean; claude-rc-session-rebind 2/2; claude-native 68 pass / 8 skip (live-e2e gated, pre-existing) / 0 fail; issue6361-* 23/23. CI (read live): green on head b521f47120 — 59 success, 29 skipped, 0 failed.

Branch state: 467 commits behind master. I verified master has made zero changes to the three src files this PR touches since the merge base, and the one master-side change to tests/claude-native.test.ts (653df87) is in a different region (UPSTREAM_HOP_HEADERS test) — the rebase is mechanical. The head lives on your fork, so the rebase is yours to run; per your stacking plan it lands cleanest after #6478 merges (diff shrinks to the one commit).

Scope note (as you state it): registration alone does not fold RC sessions — their requests arrive as Message Threads deltas until #6915's decline (PR #6918) lands. This PR fixes exactly what #6917's "Expected" asks for; the fold is the companion's job.

Review tier: this changes which session the MCP server binds — identity/session-binding logic, so per §7.4 the merge decision stays with a human reviewer.


中文八问

  1. 是什么问题:Remote Control 拉起的会话,bili 的 MCP shim 注册的是 spawn 时的会话 id,而该会话所有模型请求都走远程会话 id,bili 永远不认这个会话是自己压缩的 → 会话永不压缩。
  2. 什么原因导致的:RC 子进程用新的 CLAUDE_CODE_SESSION_ID 拉起 shim,随后在首个模型请求前采纳远程会话 id(写入 ~/.claude/sessions/<pid>.json);[Bug][claude] After /clear (or /new), bili MCP stays bound to the pre-clear session ID — tools hit the old session and the new one never enters plugin mode #6361 的重绑只在 bili 工具调用时跑(RC 会话可能从不调用),且 isClaudeHostArgv 不认原生安装路径(~/.local/share/claude/versions/2.1.323),宿主查找落空。
  3. 影响范围与严重程度:仅 claude MCP 车道 + Remote Control 启动的会话;触发面窄,触发后会话压缩能力失效(功能不可用,非数据丢失),其他会话/车道不受影响。判 p2。
  4. 怎么修复的:claude 入口从启动起用 1 秒定时器重读宿主会话文件(watchClaudeSession),id 变化即重绑 + 重注册,使注册在首个模型请求前就位;isClaudeHostArgv 接受锚定的原生安装路径(拒绝 lookalike)。
  5. 是否有回归问题:逐项核查——codex/zcode/kimi(seam 为 no-op,构建级验证 dist/mcp.js 仅导出无调用点、其余入口 0 引用)、非 identity 宿主(gate 不变、测试钉死)、per-call 覆盖 / MCP tools fail with 'unknown plugin conversation' after Claude Code --resume (shim holds a stale session id) #656 / [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 路径保留、spawn-id 残留注册惰性(identity 注册仅在匹配请求到达时绑定,占 LRU 一格)、宿主 walk 变更波及面限于会话文件读取 + watchdog 父进程 + fork lineage 声明——未发现回归。
  6. 是否破坏缓存:不涉及——不动缓存/重试/增量游标/去重键;重注册只改会话的 plugin 模式绑定。
  7. 是否改了设计方向:不涉及——既有 identity-binding 机制的增量扩展(与 [PR] fix: re-bind the claude MCP shim to the host's current session after /clear #6478 同一接缝,触发方式从"工具调用时"扩为"1 秒定时器"),无架构/边界/数据流变化,无 kernel 改动。
  8. 是否引入了新的配置:无——不新增 env/config/默认值;1 秒间隔是函数默认参数,非配置面(与 WATCHDOG_INTERVAL_MS 常量模式一致)。

判定:内容可合并 —— CI 绿(59 success / 29 skipped / 0 failed)+ 本地定向验证全绿 + 回归测试已验证"无修复必失败"。合并前:建议 #6478 先合(或本 PR 先合,顺序均可),作者将分支 rebase 到最新 master(已验证无文件级冲突,纯机械)。按 §7.4,identity/session-binding 逻辑归人类评审,合并决定留给人工评审者。

@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remote Control children start bili's MCP server under a different session id than they send, so Remote Control sessions are never compressed

2 participants