Repository navigation
[PR] fix: claude MCP shim follows a Remote Control child's session id - #6920
MohammadErfan-Jabbari wants to merge 3 commits into
Conversation
…/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>
(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 ② Regression review — existing behaviors checked, all preserved:
③ Code review — sound. ④ Test review — real tests, both gate directions, regression-verified. ⑤ 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; 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 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. 中文八问
判定:内容可合并 —— CI 绿(59 success / 29 skipped / 0 failed)+ 本地定向验证全绿 + 回归测试已验证"无修复必失败"。合并前:建议 #6478 先合(或本 PR 先合,顺序均可),作者将分支 rebase 到最新 master(已验证无文件级冲突,纯机械)。按 §7.4,identity/session-binding 逻辑归人类评审,合并决定留给人工评审者。 |
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 namedclaude. 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].dist/mcp.jsfor codex/zcode/kimi is untouched.Both compression modes
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 is3847869d-…, the sessions file sayscc233f11-…, 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.tsgains the versioned-path rows, positive and negative (…/versions/2.1.323.tmp,/opt/other/versions/2.1.323); theisClaudeHostArgvtest fails without the change.claude-rc-session-rebind,claude-nativeand bothissue6361-*suites pass (93 tests) andnpm run typecheckis clean. Fourclaude-nativetests 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-controlsession, 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