Repository navigation
[PR] fix: re-bind the claude MCP shim to the host's current session after /clear - #6478
ranxianglei wants to merge 2 commits into
Conversation
📦 Built Package ArtifactBranch: Latest build — install this exact versionnpm install -g billion-context@0.0.9454-pr.6478.fcc8c58This exact version is immutable and reproducible. The rolling Recent builds (commit → version)
Download artifact
tar xzf billion-context-pr6478.tgz
npm install -g packageInstall instructions are refreshed on each push. |
…/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 #656 404 adoption needs the bound id to be unknown (it is still registered + active, so calls succeed against it) and the #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 #6361
5fccc57 to
9378f68
Compare
(ai: vllm-qwen/qwen3.8-27b) 查重: 不重复 — no other open PR/issue targets the claude Independent review of PR #6478 (Closes #6361) ① Direction — correct. Verified the diagnosis against the code: after ② Regression review — existing behaviors checked, all preserved:
③ Code review — sound. Per-call file re-read (one small JSON) beats file-watching (no watcher lifecycle, no missed events); ④ Test review — one gap found and fixed. The main file (22 tests) is real: resolver matrix uses actual Local evidence (rebased head 9378f68): typecheck clean; Actions taken on the PR branch:
CI: green on the pre-rebase head (30/30 checks, 0 failed); the rebased head's run is in flight — full-suite verdict lands there. 中文八问
判定:可合并 —— 待 rebased head 的 CI 转绿;本地定向验证全绿,机制与范围均已独立核实。 |
(ai: vllm-qwen/qwen3.8-27b) CI on the rebased head Review verdict stands: mergeable — ready for you to merge: #6478 |
What
Claude Code
/clear(or/new) starts a new session id but keeps the stdio MCP child running, so the spawn-timeCLAUDE_CODE_SESSION_IDenv (mcp.ts'sCONVERSATION_FROM_ENV) goes stale. Neither existing recovery path fires:Result: after
/clear, bili MCP tools silently run against the old session, and the new session is never registered → it stays in proxy mode (no[acp-usage],inputTokens:0). The only reliable new-session signal available to the long-lived shim is~/.claude/sessions/<host-pid>.json(rewritten on/clear; the host pid is stable for the child's lifetime).Fix
The claude entry (
src/claude-mcp-entry.ts) now injects a resolver that reads the host's current session id from<CLAUDE_CONFIG_DIR|~/.claude>/sessions/<host-pid>.json. On eachtools/call,src/mcp.tsre-reads it and, when it differs from the bound id, re-binds the moduleconversationIdand re-registers the new session (POST /__bili/plugin/register {conversationId, agent:"mcp", identity:true}) so the new session enters plugin mode immediately.Why it's safe / scoped
dist/mcp.js(shared by codex/zcode/kimi) is untouched — those hosts never set the resolver, so the re-bind seam is a no-op for them. Verified in the build:dist/mcp.jscarries the seam but none of the claude resolver functions.sessionId/session_id/id, UUID-ish shape check; any read/parse/shape failure →undefined= "no re-bind", never a crash). Worst case the resolver returnsundefined→ the bug persists exactly as today, no regression.CLAUDE_CODE_SESSION_ID); a per-call / native override still wins for the current call and never touches the global binding.Config surface
No new config field, env var, or default. The re-bind is gated on the existing identity-binding (on by default for a claude shim, consistent with the
#656orphan-adopt default-on behavior). A single claude process = one MCP child = one session, so there is no multi-session sibling to mis-bind.Tests
tests/issue6361-claude-clear-rebind.test.ts(22 tests, both gate directions per #3617):session_id/idfallbacks,sessionIdprecedence, missing file, malformed JSON, non-object, missing field, non-string field, invalid shape, whitespace trim); wiresCLAUDE_CONFIG_DIR+ the (cached) host pid.maybeRebindConversationre-binds exactly when the id changed; no re-bind for same-id / no-resolver / throwing-resolver / non-identity; thetools/callsequence carries the re-bound id to the proxy; a re-bind fires a register for the new session.Verified locally:
npm run typecheck(exit 0),npm run build(exit 0), targeted test green, related existing suites (issue4168,claude-mcp-entry,issue4580,mcp-native-thread) green on the rebased head. Full-suite verdict = remote CI.Closes #6361