Repository navigation
fix(agent): reopen an mcp.json server whose connection retired - #7114
Conversation
M4n5ter
left a comment
There was a problem hiding this comment.
English
Thanks for the clear breakdown in #7112 and the real-process MCP crash/reopen test. There are a few concrete issues to address in this implementation:
-
keep_cachedsilently swallows reopen errors and causes double-spawns per message (plugins/agent/native/src/mcp/projects.rs)
When reopening fails (e.g., broken command / unreachable server) or whencleanup_failuresis already non-empty,keep_cacheddiscards the real error and returnsOk(dead_catalog). On a realagent.sendpath:owner/send.rscallsprepare()-> sees!layers_available()-> callsstart(), fails to reopen, swallows the error, and queues the message with the dead catalog.driver_assembly.rsthen callscapture()-> sees!layers_available()again -> runsstart()and spawns/fails the broken server a second time, swallows the error again, and starts the Run.- The Run then fails at
authorize_model_requestwithunavailable_selected_binding-> the opaqueRecovery is unavailable for adapter supplied no safe interrupted-attempt recovery evidencemessage, hiding the actual startup or cleanup failure. - It also bypasses the existing
cleanup_failureshard gate whenever a retired cache entry exists.
Because nothing removes the old entry fromsnapshots.revisionsuntil a new layer succeeds, returningErr(error)directly on reopen failure preserves the existing cache without silently swallowing the real error.
-
Concurrent reopens of global
~/.maka/mcp.jsonacross different workspaces are not serialized (plugins/agent/native/src/mcp/projects.rs)
startup_gateis keyed byproposal.revision. Two workspaces with different projectmcp.jsonfiles (or one with a project file and one without) have distinctproposal.revisions while sharing the sameglobal_revision. When a global server retires and both prepare concurrently, each acquires a different gate and runsopen_layer(&proposal.global, ...)in parallel, launching duplicate global MCP processes and pushing the losing live process intosnapshots.replaced. Acquiringstartup_gate(&global_revision)around opening the global layer (in consistent global-then-project lock order to prevent deadlocks) avoids duplicate spawns. -
Design trade-off: file-layer reopening interrupts sibling healthy servers in the same
mcp.json(plugins/agent/native/src/mcp/projects.rs,plugins/agent/native/src/owner/access/model.rs)
Supposemcp.jsondefines Server A (healthy) and Server B (crashes in Session 2), while Session 1 is mid-turn using Server A. Reopening the wholemcp.jsonlayer gives Server A a new binding generation. Althoughcurrent_activationnow accepts Session 1's old activation viasnapshots.replaced,current_activationstill returns the newly published catalog (Server A gen 2), so Session 1's next model step failsauthorize_model_request'sinternal_bindingsgeneration check. Worth clarifying whether whole-layer blast radius is acceptable here or if reopening should be scoped to the retired server.
简体中文
感谢在 #7112 里把链路梳理得很清楚,也补了起真实 MCP 进程的端到端测试。当前实现有几个明确可达的问题需要先处理:
-
keep_cached静默吞掉重开失败,且单条消息会连续拉起两次坏进程(plugins/agent/native/src/mcp/projects.rs)
当重开失败(命令异常/远端不可达)或cleanup_failures已非空时,keep_cached会把真实错误吞掉并返回Ok(死 catalog)。走实际发送路径时:owner/send.rs调prepare()看到!layers_available()-> 调start()重开失败,吞错后仍用死 catalog 把消息入队;- 紧接着
driver_assembly.rs调capture()再次看到!layers_available()-> 又进start()第二次拉起并失败,再次吞错启动 Run; - 随后 Run 在
authorize_model_request撞unavailable_selected_binding,落到晦涩的Recovery is unavailable for adapter supplied no safe interrupted-attempt recovery evidence,把真正的启动或清理报错全掩盖了。 - 同时,只要存在已失效的缓存层,原本
cleanup_failures的硬拦截也会被绕过。
由于新层成功前本来就不会从snapshots.revisions删除旧条目,重开失败时直接返回Err(error)即可保留旧缓存状态,同时把真实原因暴露出来,不要静默吞错。
-
跨 workspace 共享全局
~/.maka/mcp.json时startup_gate挡不住并发重开(plugins/agent/native/src/mcp/projects.rs)
startup_gate目前按proposal.revision加锁。两个不同 workspace(项目mcp.json不同,或一个有一个没有)拿的是不同的proposal.revision,但共用同一个global_revision。当全局 MCP 服务退役、两边并发准备时,会各自拿到不同锁并并行执行open_layer(&proposal.global, ...),拉起两份重复的全局进程,其中一份活进程直接被顶进snapshots.replaced。应该对全局层按global_revision单独加 gate(固定先 global 后 project 顺序加锁避免死锁)。 -
设计取舍:整层重开会波及同一
mcp.json中正在运行的健康服务(plugins/agent/native/src/mcp/projects.rs、plugins/agent/native/src/owner/access/model.rs)
如果mcp.json同时配了服务 A(健康)和服务 B(在会话 2 崩溃),而会话 1 正在跑使用服务 A 的多步 turn:会话 2 重开整个mcp.json层会给服务 A 也分配新的 binding generation。虽然current_activation通过snapshots.replaced放行了旧 activation 的身份校验,但它最终返回的仍是新发布层(服务 A 已是新 generation),导致会话 1 下一步在authorize_model_request校验internal_bindings时失败中断。这里需要明确是接受整层重开的波及面,还是把重开粒度收窄到失效的单个 server。
a2674e8 to
8ce9a7c
Compare
|
Thanks for the review. Rebased onto b499e51; the fix for item 1 is a separate commit, 8ce9a7c. 1. 2. Concurrent reopen of the global layer. I think this is already serialized, before and after this PR. 3. Blast radius. Your example is worse than what I wrote in the description: it breaks a Run that is mid-turn on a healthy server, not only a later request. I agree the reopen should be scoped to the retired server. That means per-server binding generations inside a layer in place of one open per layer, which goes further into the connection ownership than this PR does. Would you rather take that part, or should I attempt it here? If you prefer, I can also cut this PR down to the error reporting from item 1 and leave reopening to that design.
(Claude Code, Claude Fable 5.2, with a Claude Opus 5.5 subagent on the implementation.) |
EnglishThanks for the clean follow-up!
简体中文回复和修复都很清楚:
|
8ce9a7c to
e1bb2fe
Compare
|
Item 3 is done in e1bb2fe, rebased onto c7b7bf2. What changed. A reopened layer now starts only the servers whose connection is not available. Every other server of that layer keeps its connection and its authority generation, so the
Limit. A Run whose selected tools include the retired server still stops at its next model step, as it did before this PR. Only the next message gets the new connection. Test. The fixture mcp.json now has a second server. The reopen test asserts that it starts once and still answers after the first server was replaced. It does not run a second Session mid-turn; the kept generation follows from the kept connection. The two red checks on the previous head were — Claude Fable 5.2, working with the PR author |
A call that ends with an unknown result retires its MCP connection, and the cached layer kept handing that connection to every later turn and Session, so each one failed before its model request until the Host restarted. A cached file layer is now used only while all of its connections, and those of the file layers below it, are available. Otherwise the next preparation reopens the layer under the same revision, as a new protocol session with new binding generations; the call itself is never replayed. The startup gate keeps concurrent preparations to one reopen. The replaced layer stays valid for Runs that already hold it and closes once nothing references it, or when the Projects close. A client composition over a replaced layer is rebuilt. If reopening fails, the cached layer stays in use as before. Servers from the installation configuration are unchanged: their tools and bindings belong to the activation's tool basis and change only by reactivation. Refs #7112 Generated-by: Claude Code (Claude Opus 5.5)
…start A failed reopen no longer falls back to the retired layer. The send is rejected with the server's startup error, as for a first open, so one message starts the broken server once and the cleanup-failure gate holds again. Generated-by: Claude Code (Claude Opus 5.5)
The other servers of the same mcp.json layer keep their connection and authority generation, so Runs that use them continue.
e1bb2fe to
fbafd44
Compare
EnglishThe per-server reopen design ( Rebased onto
Verified locally ( 简体中文按 server 粒度重开的这套设计( 已 rebase 到最新
本地已验证通过( |
Summary
Part of #7112 (direction 1 only).
A call that ends with an unknown result retires its MCP connection. The cached file layer kept handing that connection to every later turn and conversation, so each failed before its model request until the Host restarted or
mcp.jsonchanged.A cached
mcp.jsonlayer is now reused only while all of its connections, and those of the file layers below it, are available. Otherwise the next preparation reopens the layer under the same revision: a new protocol session with new binding generations. The call is never replayed.current_activationaccepts it) and is closed byprune()once nothing references its catalog, or whenProjectscloses.Not covered
mcp.json, which this covers.CleanupUnconfirmedhold and the misleading failure text (directions 3 and 4 of bug(agent): one MCP call with an unknown result retires the connection and fails every later request #7112).mcp.jsontoday.@M4n5ter this is in your area and it touches
current_activation; say so if you want the reopen done differently or would rather take it yourself.AI use
Tool(s) and scope: Claude Code. A Claude Opus 5.5 subagent reproduced the problem and wrote the change and test; Claude Fable 5.2 set the scope and reviewed the diff.
Checklist
mcp_server_that_exits_mid_call_is_reopened_for_the_next_turn: a stdio server from the globalmcp.jsonexits during a call; the next turn in that conversation and a new conversation each reach the model and complete, and the server process has been started exactly twice. Tested on the global file layer only; the project layer, the client recomposition and the failed-reopen fallback are covered by reading.maka-agent-nativelib: 197 tests, passing except two that are intermittent on plainrust-preview(see #7111).Does this PR entail a change in behavior?