Skip to content

feat(mcp): item 4 MCP 工具自动后台化 FUSION_MCP_AUTO_BACKGROUND_MS - #94

Merged
dahai80 merged 1 commit into
mainfrom
feat/mcp-auto-background-item4
Aug 20, 2026
Merged

dahai80 merged 1 commit into
mainfrom
feat/mcp-auto-background-item4

Conversation

@dahai80

@dahai80 dahai80 commented Aug 20, 2026

Copy link
Copy Markdown
Owner

Closes #93

背景

enhance-0819.md §186 item 4 (CC 2.1.212): MCP 工具调用超过阈值自动转后台任务, REPL 不阻塞。当前 MCP 调用全程阻塞 toolExecution.ts:1392 await tool.call() — 单个慢 MCP server 卡死整轮对话。

设计

  • 新 env FUSION_MCP_AUTO_BACKGROUND_MS (default 0=off, 对齐 CC 默认 120s 可配)
  • wrap MCPTool.call() override (client.ts) 用 Promise.race vs autoBackgroundMs timer
  • composite bgController: 跟随 turn-abort 直到超时, 超时后 detach (bgDetached 标志) — 背景调用存活到 turn 结束后, listener {once:true} 移除无泄漏
  • MCP 工具可能 mutating, 不重调, 保活当前 callPromise
  • 超时转 task: spawnMonitorMcpTask 注册到 AppState, 回 interim tool_result 解锁 REPL
  • 完成续: notifyMonitorMcpTaskDone (复用 LocalShellTask task-notification 模式) 写输出文件 + 入队通知 + 标 completed + evict

范围

  • src/services/mcp/client.ts — call() override wrap + 3 helper (getMcpAutoBackgroundMs/McpAutoBackgroundTimeoutError/backgroundMcpCall)
  • src/tasks/MonitorMcpTask/MonitorMcpTask.ts — 原 stub 扩成 spawn/notify/kill 真实实现
  • src/__tests__/tasks/monitorMcpTask.test.ts — 5 新单测

验证

  • typecheck: 0 errors
  • unit tests: 121 pass (+5 新), 0 fail
  • build + build:dev:full: pass
  • 新增文件 lint clean (client.ts 2 pre-existing errors @741/@1646 未触及)

风险

  • abort-signal detach 竞态: bgDetached 标志 + {once:true} listener 保证无泄漏/无 orphan。背景调用完成 catch 路径 notify failed。
  • 默认 off (0), 不改变现有行为, 用户显式设 env 才启用

Co-Authored-By: Claude Fable 5 [email protected]

慢 MCP 工具调用超过 FUSION_MCP_AUTO_BACKGROUND_MS (default 0=off) 自动转
后台 monitor_mcp task, REPL 不阻塞。对齐 CC 2.1.212 默认 120s 可配。

设计:
- wrap MCPTool.call() override (client.ts) 用 Promise.race vs autoBackgroundMs
- composite bgController: 跟随 turn-abort 直到超时, 超时后 detach (bgDetached
  标志) — 背景调用存活到 turn 结束后, listener {once:true} 移除无泄漏
- MCP 工具可能 mutating, 不重调, 保活当前 callPromise
- 超时转 task: spawnMonitorMcpTask 注册到 AppState, 回 interim tool_result
  解锁 REPL, 后台续跑 callPromise
- 完成续: notifyMonitorMcpTaskDone (复用 LocalShellTask task-notification 模式)
  写输出文件 + 入队通知 + 标 completed + evict

落地 MonitorMcpTask.ts (原 stub) 加 spawn/notify/kill 真实实现。

验证: typecheck 0, 121 unit tests pass (+5 新), build + build:dev:full pass,
新增文件 lint clean (client.ts 2 pre-existing errors 741/1646 未触及)。

Closes #93

Co-Authored-By: Claude Fable 5 <[email protected]>
@dahai80
dahai80 merged commit 2abbc4e into main Aug 20, 2026
1 check passed
@dahai80
dahai80 deleted the feat/mcp-auto-background-item4 branch August 20, 2026 12:05
dahai80 added a commit that referenced this pull request Aug 20, 2026
追 CC 2.1.196→2.1.234 Tier-1 特性批量落地 (13 PR):

变更自 v0.4.22:
- WS1 DirectoryAdded hook + Notification agent_* 类型 (#73)
- WS2 堆叠 slash-skill 调用, 最多 5 前导 (#74)
- LSP+filecache 内存泄漏 cap, 对齐 CC 2.1.208 (#76)
- skill body-defer 懒加载, item 9 (#78)
- SessionStart source "fork" 区分 fork 与 resume (#80)
- P2.1 子代理护栏 — 并发/会话/深度三限 (#82)
- P1.1 大输出 tool_result tail 预览 (#84)
- P4.1 --ax-screen-reader 入口点对齐 — env 别名 + settings key (#86)
- item 13 WebSearch per-session 搜索次数上限 (#88)
- P4.2 §6 /doctor FUSION_* vs ANTHROPIC_* 环境变量冲突检测 (#90)
- P4.2 #1+#3 /doctor MLX 网关连通 + 模型加载探测 (#92)
- item 4 MCP 工具自动后台化 FUSION_MCP_AUTO_BACKGROUND_MS (#94)
- item 22 审计日志凭证脱敏 (#96)

- package.json: version 0.4.22 → 0.4.23

Co-authored-by: Claude Fable 5 <[email protected]>
dahai80 added a commit that referenced this pull request Aug 20, 2026
…OKENS (P2.1 维度 4) (#99)

P2.1 子代理护栏第 4 维度: 会话级 token 预算上限。CC `--max-budget-usd`
停后台子代理 → 本地无 USD, 转 token 预算。依赖 PR #82 (guardrail infra,
已合 v0.4.23), 现 main 就位。

默认 off (0=off, fail-open), 仅用户显式设
FUSION_MAX_SUBAGENT_BUDGET_TOKENS 时生效 — 失控刹车防单一会话子代理
耗光本地 MLX 上下文。零默认行为变更 (对齐 PR #82/#94 env-gated opt-in 模式)。

- AppState.subagentBudgetUsedTokens (init 0, reset /clear), 并行
  subagentSpawnCount
- subagentGuardrails.ts: DEFAULT_MAX_SUBAGENT_BUDGET_TOKENS=0 +
  getMaxSubagentBudgetTokens() + parseBudgetCap() (0=合法 off 值非错误,
  区别于 parseCap); checkSubagentGuardrails 加第 4 维度 (默认 off 放末尾)
- AgentTool.tsx: gate input 加 budgetUsedTokens; 3 个 finalizeAgentTool
  返回后各加 1 行 rootSetAppState 累加 agentResult.totalTokens
- agentToolUtils.ts: 第 3 个 finalize site 累加 (async path)
- tests/utils/subagentGuardrails.test.ts: +9 budget case (off/on/reject/
  exceed/omit/order/parse-decimal), 24 pass

近似计数: totalTokens = 最后 turn usage (非全程累加), 失控刹车非精确计费,
低估偏安全方向。3 增量点无统一 seam, 注释标 keep-in-sync。

验证: typecheck 0 / 单测 24+167 pass / build + build:dev:full pass /
diff 纯插入零 churn / 风格对齐。

Closes #98

Co-authored-by: Claude Fable 5 <[email protected]>
dahai80 added a commit that referenced this pull request Aug 22, 2026
…#127)

shouldRunAsync 加第 6 || 项: (!isInProcessTeammate() && isSubagentDefaultBackground())。
env 开关让默认子代理 spawn 即后台 (非队友) 解阻塞主 turn。async-from-start
复用既有 registerAsyncAgent (isBackgrounded:true 立即, 无 timer) + runAsyncAgentLifecycle。

与 item 12 FUSION_SUBAGENT_AUTO_BACKGROUND_MS 正交: item 12 是 foreground else
分支 mid-run 后台化定时器; 本开关是 async-from-start。

!isInProcessTeammate() guard 复用 line 455 既有 throw 模式 (in-process 队友生命周期
绑 leader 进程不可后台)。default off (isEnvTruthy 未设/空/false/0 → false),
byte-identical 旧行为, 无独立 feature flag。对齐 PR #82/#94/#99 env-gated opt-in。

新 isSubagentDefaultBackground() helper (autoBackground.ts, sibling item 12 模式),
+11 单测 (truthy/fail-off/default-off 全覆盖)。

gates: typecheck 0 / 18 新+旧单测 pass / 407 全套无回归 / build:dev pass /
biome baseline 不变 (变更文件 0 新 error)。

closes #126

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(mcp): item 4 MCP 工具自动后台化 FUSION_MCP_AUTO_BACKGROUND_MS

1 participant