Skip to content

fix(agent-core-v2): journal a tool's duration only when the tool ran - #5585

Merged
sailist merged 1 commit into
mainfrom
fix/denied-tool-duration
Nov 27, 2026
Merged

sailist merged 1 commit into
mainfrom
fix/denied-tool-duration

Conversation

@RealKai42

@RealKai42 RealKai42 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

在 config.toml 里写了 [[permission.rules]] 的用户,规则拒绝过一条命令之后,重新打开会话,这条拒绝的工具结果在发给模型的请求里变了样:多出一行 Wall time: 0.000 seconds。请求前缀和重启前接不上,这个会话之后的每次请求都按冷 prefill 计费,直到压缩。用户不会收到任何提示。

这个 PR 之后,恢复出来的请求和实时的一样。问题是 #5582 新加的 E2E 场景发现的:它的缓存账本在重启后报出前缀断开。

Bug Reproduction Steps

  1. config.toml 里写一条 deny 规则,例如 [[permission.rules]] decision = "deny" pattern = "Bash(rm *)"。
  2. 让模型跑一次 rm keep.txt:规则直接拒绝,模型收到 <system>ERROR: Tool execution failed.</system> 开头的工具结果。
  3. 关掉产品再打开同一个会话,发一条消息。
  4. 修复前:这次请求里,那条工具结果变成 Wall time: 0.000 seconds 开头,和重启前的请求不一致。

E2E 复现:TESTKIT_CLI_BUILD=dist pnpm --filter @moonshot-ai/e2e exec playwright test --project=tui scenarios/config/permission-rules-from-the-config-file-apply.spec.ts。修复前收尾检查报 request #3 (main) does not extend request #2 … messages[4] tool changed from …"<system>ERROR: Tool execution failed.</system>\n… to …"Wall time: 0.000 seconds\n<system>ERROR: Tool ex…;修复后 TUI、ACP 都通过。

Root Cause

模型实时看到的工具消息来自核心循环。规则拒绝的工具走 blockedToolOutcome,回合取消时还没开始的工具走 abortedToolOutcome,两种消息都不带耗时。

写会话日志的是 packages/agent-core-v2/src/agent/loop/turnProjection.ts 对 tool.ended 的投影。它给每个结束的工具都填了 legacyToolDurationMs(snapshot)。这个函数是给遥测用的,测不到耗时就返回 0(snapshot.prepared?.durationMs ?? 0)。于是没执行过的工具在日志里记了 durationMs: 0。恢复会话时从日志重建历史,renderToolResultForModel 看到有耗时,就补上 Wall time 那一行(Bash、Grep、MCP 工具等会显示耗时的工具)。

根本修复:日志里只记真实测到的耗时 prepared?.durationMs。执行过的工具取值和以前一样,没执行的工具不再记一个编出来的 0。遥测仍然用 legacyToolDurationMs,不受影响。

Code Changes

 turnProjection.decorate(tool.ended)
   result.durationMs =
-    detached || no snapshot ? undefined : legacyToolDurationMs(snapshot)   // 0 when nothing was measured
+    detached ? undefined : snapshot.prepared?.durationMs                    // only what was measured
  • packages/agent-core-v2/test/agent/loop/loop.test.ts:「cancelled during tool execution」这条用例原来断言没开始执行的 call-work-2 在日志里也有数字耗时。这正是被修的行为:模型实时看到的中止消息上没有耗时,日志里不该有。断言改成这条结果不带 durationMs。依据是「恢复后发给模型的请求必须和实时一致」这条契约,E2E 的缓存账本按它检查。执行过的 call-work-1 有数字耗时的断言保留,仍然通过。
  • 新增 E2E 场景 tests/e2e/scenarios/config/permission-rules-from-the-config-file-apply.spec.ts(从 test: add E2E scenarios for config wiring, slow MCP servers and terminal screen switches #5582 挪到这里,和修复一起合入):配置里的 deny 直接拒、不弹审批,allow 在 Always Ask 下直接放行,ask 在 Ask When Needed 下照样问,重启后 deny 仍生效。重启前后请求的连续性由收尾的缓存账本检查。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
被权限规则拒绝、或回合取消时还没开始的工具,会话日志里的耗时 durationMs: 0 不记 无:0 不是测出来的值 —
恢复会话后,这些工具在模型请求里的渲染 多一行 Wall time: 0.000 seconds,前缀和重启前接不上 和实时一致 无 —
执行过的工具的耗时(日志、恢复、实时) 测到的值 不变 — —
遥测里工具调用的 duration_ms 没测到时为 0 不变(仍用 legacyToolDurationMs) 遥测看板 —
旧版本写的会话里已经记下的 0 恢复时渲染 Wall time: 0.000 不变:日志里的旧记录不改写,这类会话恢复时还会断一次前缀 — —

受影响的人群:所有面(TUI、print、Desktop、Web、ACP、SDK)上,配置了 deny 规则,或者在工具开始前停止过回合,之后又恢复会话的用户。

验证:

  • 新 E2E 场景修复前在 TUI 上红(缓存账本),修复后在 TUI、ACP 上绿(本机 macOS,构建产物);Desktop、Web、Windows 交给 CI。
  • packages/agent-core-v2/test 全套 384 个文件、8436 条通过;packages/kap-server/test、packages/acp-server/test 89 个文件、1779 条通过。
  • tests/scenarios:本机第一次跑有 5 条失败,都是 KAP 进程 30 秒内没起来(stderr 为空,当时本机负载在 100 左右);负载降下来后重跑这 5 条所在的 cold-recovery.test.ts 和 concurrent-file-writes.test.ts,7 条全过。
  • pnpm lint 通过。

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: issue must have a maintainer's /approve).
  • I have added tests that prove my feature works.
  • The behavior-change table above is complete, and every removed behavior or flipped default is named in the changeset and either has an escape hatch or was explicitly approved by a maintainer in this PR.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

The turn projection journaled every ended tool with legacyToolDurationMs(),
which returns 0 when nothing was measured. A tool that a permission rule
denied, or that a stop cut off before it started, reached the model live
without a duration but was journaled with 0, so a resumed session rendered
it with a "Wall time: 0.000 seconds" line and the prompt cache prefix broke.

Journal the measured duration from the preparation only. The new E2E scenario
applies deny, allow and ask rules from config.toml across a restart; its cache
ledger caught the drift.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5e6dc8b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
kimi-code-app Patch
@moonshot-ai/kimi-code Patch
@moonshot-ai/agent-core-v2 Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-26T18:37:26.628680Z 5e6dc8b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Nov 26, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@5e6dc8b
npx https://pkg.pr.new/@moonshot-ai/kimi-code@5e6dc8b

commit: 5e6dc8b

@sailist
sailist merged commit a4bc573 into main Nov 27, 2026
30 checks passed
@sailist
sailist deleted the fix/denied-tool-duration branch November 27, 2026 03:37
RealKai42 added a commit that referenced this pull request Nov 27, 2026
…sult as it did live (#5658)

expectResumeMatches compares the engine state before and after a resume,
and it drops reminders first. Two prompt cache bugs slipped past it,
because the history a resumed session sends can differ from the live one
while the state matches.

The harness gains expectResumeSendsTheSameHistory, which also projects
the next request's messages on both sides with the projection every
request goes through and compares them. A new resume test registers tools
that return results of many shapes (notes, errors, empty and multi-part
output, truncation, an image, a crash, a wall-time tool and a denied
tool, then a parallel batch) and checks the resume with it. Without the
fixes from #5585 or #5640 the test fails.

expectResumeMatches keeps its behavior for now: the new comparison still
finds three divergences in existing cases (a parallel batch with a denied
call is reordered, a tool call that failed before it started disappears,
and an interruption reminder comes back after undoing every turn). Once
they are fixed, expectResumeMatches will compare the next request too.

The cache catalog lists the new test under A8 and A9.
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.

2 participants