feat(guardrails): item 14 子代理 token 预算上限 FUSION_MAX_SUBAGENT_BUDGET_TOKENS (P2.1 维度 4) - #99
Merged
Merged
Conversation
…OKENS (P2.1 维度 4) 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 <noreply@anthropic.com>
This was referenced Aug 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
Closes #98. P2.1 子代理护栏第 4 维度 (会话级 token 预算上限)。
enhance-0819.md §201 item 14: CC
--max-budget-usd停后台子代理 → fusion-code 本地无 USD, 转 token 预算上限。维度 1/2/3 (并发/会话/深度) 已落地 PR #82, 维度 4 当时 deferred — 跨 PR 依赖 (PR #82 未合 main)。现 PR #82 已合 v0.4.23, guardrail infra 就位。动机
fusion-code 子代理无 token 预算刹车。失控子代理 (无限循环 spawn / 大量 tool call) 可耗尽本地 MLX 上下文。预算上限 = 失控刹车, 防单一会话子代理耗光 token。默认 off (fail-open), 仅用户显式设
FUSION_MAX_SUBAGENT_BUDGET_TOKENS时生效。零默认行为变更。改动 (surgical, 跟 PR #82 同模式)
AppStateStore.tssubagentBudgetUsedTokens: number(init 0), 并行subagentSpawnCountclear/conversation.ts0main.tsx0subagentGuardrails.tsDEFAULT_MAX_SUBAGENT_BUDGET_TOKENS=0+getMaxSubagentBudgetTokens()+parseBudgetCap()(0=合法 off 值);checkSubagentGuardrails加第 4 维度 (默认 off, 末尾)AgentTool.tsxbudgetUsedTokens; 2 个 finalizeAgentTool site 累加 (backgrounded + sync)agentToolUtils.tstests/.../subagentGuardrails.test.ts设计决策
finalizeAgentTool.totalTokens= 最后 turn usage (input+cache+output), 非全程累加。失控刹车非精确计费, 低估偏安全方向 (刹车略晚触发), default off 用户自选。精确全程累加需改 ProgressTracker 透传, M-size 超 surgical验证
bun run typecheck: 0 errorsbun test tests/utils/subagentGuardrails.test.ts: 24 pass (15 + 9 budget)bun test src/__tests__/: 167 pass, 0 fail (无回归)bun run build+bun run build:dev:full: passgit diff -w: 纯插入, 零 churn, 风格对齐 (tab/double-quote/semi)风险
--max-budget-usd(亦默认 off)关联
architecture/fusion-code-enhance-0819.md§201 item 14 + §551-577 P2.1 维度 4