feat(shared): add run budget contract and runaway detectors (step 1 of #17) - #47
Merged
Merged
Conversation
Step 1 of helsome#17: the runtime contract the agent loop can enforce, without wiring it into RunManager yet. - run-budget.ts: RunBudgetLimits / RunBudgetUsage over wall-clock, model calls, tool calls, search iterations, tokens and cost. resolveBudget applies defaults, then per-run overrides, always clamped by the system ceiling and reporting which keys it clamped; checkBudget reports the first exhausted key in a fixed order; budgetStop turns exhaustion into a machine-readable stop_reason with its detail. Invalid limits and negative usage deltas fail loudly instead of being silently ignored. - runaway-detector.ts: deterministic observers for repeated identical tool calls (argument order independent, with tool include/exclude scope), near-identical search queries (token overlap, so widening a query with more words still counts), iterations reporting the same evidence set, and retry storms inside a rolling window. State is plain data and every detection carries its evidence; runawayStop maps a detection to a stop reason. - 32 unit tests cover every budget key, the ceiling/override rules and each detector boundary. Wiring into RunManager, partial-result preservation and the real-provider E2E (a deliberately tiny budget plus an induced repeat-search case) follow in step 2. Refs helsome#17
Collaborator
Author
|
CI 结果说明(避免误判成本 PR 的代码问题):
这 7 个失败不是本 PR 引入的,全部位于 复现方式:干净 clone 上 本 PR 自身范围( |
helsome
added a commit
that referenced
this pull request
Sep 11, 2026
Replay the completed #49 implementation onto clean main after #47 was squash-merged. Preserves runtime budget enforcement, partial-result persistence, runaway detection wiring, AgentKernel configuration, tests, and the real-provider smoke harness. Refs #17 Supersedes #49 Co-authored-by: kbkb628 <278338969+kbkb628@users.noreply.github.com>
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.
背景
#17 要求把"Agent 最多能做多少事"变成 runtime 契约,而不是各 workflow 自己 hardcode。这条 PR 是 step 1/2:只落地契约与判定逻辑,不接线,便于先评审形状;真实 provider 的 E2E 放在 step 2。
改动
packages/shared/src/kernel/run-budget.tsStopReason、resolveBudget(defaults ← per-run override ← 系统 ceiling 强制夹紧并回报被夹紧的键)、checkBudget(固定顺序取第一个耗尽键)、addUsage(不可变累加)、budgetStoppackages/shared/src/kernel/runaway-detector.tstoolCallInclude/Exclude通配,未跟踪工具透明)、近似重复 search query(token 包含度)、同一证据集连续无新增、窗口内 retry storm;状态是可序列化纯数据,每次判定带 evidence;runawayStop映射到 stop reason.test.tskernel/index.ts、src/index.ts设计取舍
"nvidia earnings 2026"→"…2026 q2"在 Jaccard 下相似度只有 0.8,会被"每次加一个词"绕过;包含度|A∩B| / min(|A|,|B|)在该场景恒为 1.0,而无关查询仍接近 0。wallClockMs由调用方以"距运行开始的绝对耗时"提供),因此可重放、可解释。RunManager,避免在形状定稿前改变运行时行为。验证
不在本 PR 内(step 2,对齐 #17 验收标准)
RunManager累计 usage、命中后写stop_reason并保留 partial result本 PR 覆盖验收清单中的:统一 budget contract、四类 runaway 检测、每类预算与检测器的单元/边界测试。