Skip to content

feat(subagent-guardrails): P2.1 子代理护栏 — 并发/会话/深度三限 (CC 2.1.217) - #82

Merged
dahai80 merged 1 commit into
mainfrom
feat/subagent-guardrails-p21
Aug 20, 2026
Merged

dahai80 merged 1 commit into
mainfrom
feat/subagent-guardrails-p21

Conversation

@dahai80

@dahai80 dahai80 commented Aug 20, 2026

Copy link
Copy Markdown
Owner

背景

CC 2.1.217 对齐。enhance-0819.md P2.1 = 子代理护栏 (subagent guardrails)。审计确认 5 维全部缺失 (见 issue #81)。本 PR 落地维度 1+2+3 (spawn 时拒绝), 维度 4/5 延后单独 item。

改动

三限 (spawn 前强制, 超限 throw 走既有 tool-error 路径):

维度 环境变量 默认 作用
并发 FUSION_MAX_CONCURRENT_SUBAGENTS 20 运行中 local_agent 数上限
会话 FUSION_MAX_SUBAGENTS_PER_SESSION 200 会话累计 spawn 数上限, /clear 重置
深度 FUSION_MAX_SUBAGENT_SPAWN_DEPTH 3 嵌套 spawn 深度上限

文件:

  • src/tools/AgentTool/subagentGuardrails.ts (新, 94 行) — 封装三限。parseCap fail-open: 非数/0/负值回退默认 (非静默阻断)。checkSubagentGuardrails 按深度→并发→会话序 (最廉价先查)。
  • src/tools/AgentTool/AgentTool.tsx (+24) — agent 解析后、sync/async 分发前插入校验 + subagentSpawnCount 递增 (rootSetAppState)。
  • src/state/AppStateStore.ts (+5) — AppState 增 subagentSpawnCount: number 字段 + 默认 0。
  • src/commands/clear/conversation.ts (+3) — /clear 重置 subagentSpawnCount: 0。
  • src/main.tsx (+1) — AppState 字面量增 subagentSpawnCount: 0。
  • tests/utils/subagentGuardrails.test.ts (新, 214 行) — 15 单测。

不在本 PR 范围

验证

  • bun run typecheck — 0 errors
  • bun test tests/utils/subagentGuardrails.test.ts — 15 pass / 0 fail
  • bun test src/__tests__/ tests/ — 455 pass / 0 fail (无回归)
  • bun run build + bun run build:dev:full — pass
  • diff surgical: 6 files, +341/-0 (零格式 churn)

测试覆盖

env 解析 (默认/覆盖/无效回退 nope·0·-3/十进制截断 5.9→5), countRunningSubagents (计数 running local_agent / 空时 0), checkSubagentGuardrails (全放行 / 三限各自拒绝+放行 / 深度优先于并发 / 运行时降限生效)。

Closes #81

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

CC 2.1.217 对齐: Agent 工具 spawn 前强制三类上限, 阻断失控子代理递归。
- FUSION_MAX_CONCURRENT_SUBAGENTS 默认 20: 运行中 local_agent 数上限
- FUSION_MAX_SUBAGENTS_PER_SESSION 默认 200: 会话累计 spawn 数上限, /clear 重置
- FUSION_MAX_SUBAGENT_SPAWN_DEPTH 默认 3: 嵌套 spawn 深度上限

新增 src/tools/AgentTool/subagentGuardrails.ts 封装三限 (parseCap fail-open:
非数/0/负值回退默认, 非静默阻断)。AgentTool.call() 解析 agent 后、sync/async
分发前插入校验, 超限 throw (与 fork/MCP guard 同路径)。AppState 增
subagentSpawnCount 字段, spawn 时 rootSetAppState 递增, clearConversation 重置。

维度 4 (budget cap --max-budget-tokens) + 维度 5 背景默认 + per-agent-budget-stop
延后单独 item。worktree 隔离 + agent_completed 通知已在 PR #73 落地。

15 单测 (env 解析/默认/无效回退/十进制截断, 计数, 三限拒绝/放行, 深度优先序,
运行时降限)。typecheck 0, 全测 455 pass, build + build:dev:full pass。

Closes #81

Co-Authored-By: Claude Fable 5 <[email protected]>
@dahai80
dahai80 merged commit fa34a76 into main Aug 20, 2026
1 check passed
@dahai80
dahai80 deleted the feat/subagent-guardrails-p21 branch August 20, 2026 12:01
dahai80 added a commit that referenced this pull request Aug 20, 2026
)

对齐 enhance-0819.md §优先级矩阵 item 13 剩余项 (子代理三限已落地 PR #82,
本 PR 补 web-search per-session cap)。

审计真 gap:
- WebSearchTool.ts:84 max_uses:8 是单次 call 内 server-side 上限, 非会话累计
- webSearchRequests (stats/cost-tracker) 是 usage/cost 累计统计, 非 gate 阈值
- 无 FUSION_MAX_WEB_SEARCHES_PER_SESSION per-session cap, 失控代理可无限刷搜索

方案 (surgical, self-contained — 不依赖 PR #82 未合 main):
- src/tools/WebSearchTool/webSearchGuardrail.ts (新): env
  FUSION_MAX_WEB_SEARCHES_PER_SESSION 默认 200, parseCap fail-open
  (非数/0/负值回退默认, 不静默阻断全搜); checkWebSearchGuardrail 超限返错误
- AppState.webSearchCount: number (AppStateStore type + getDefaultAppState=0
  + main.tsx initialState=0)
- WebSearchTool.call 开头 gate (超限 throw new Error 走既有 tool-error 路径)
  + 通过后 setAppStateForTasks ?? setAppState 递增; logForDebugging 计数
- /clear (clearConversation) 重置 webSearchCount: 0

计数语义: 计 WebSearchTool.call invocation 次数 (粗, 每 call 算 1)。
200/会话远超正常用量 (<20); 超限 = 失控代理征兆, 同 subagent cap 语义。
计实际 server-side search 数需 call 后才知, gate 在 call 前, 选简单实用。

验证:
- typecheck 0
- 单测 13 case (env fail-open 7 + gate pass/reject 6) 全绿
- 全测 453 pass/0 fail (无回归)
- build + build:dev:full pass
- prod 改动 zero format churn (git diff -w = git diff), +28/-0 surgical
- 新文件 biome --write clean

Closes #87

Co-authored-by: Claude Fable 5 <[email protected]>
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(subagent): AgentTool 并发/会话/深度护栏 (P2.1, CC 2.1.217 对齐)

1 participant