Repository navigation
fix(agent-core-v2): journal a tool's duration only when the tool ran - #5585
Merged
Merged
Conversation
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 detectedLatest commit: 5e6dc8b The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
Merged
5 of 6 tasks
sailist
approved these changes
Nov 27, 2026
This was referenced Nov 27, 2026
Merged
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.
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.
Requirement or Bug
在
config.toml里写了[[permission.rules]]的用户,规则拒绝过一条命令之后,重新打开会话,这条拒绝的工具结果在发给模型的请求里变了样:多出一行Wall time: 0.000 seconds。请求前缀和重启前接不上,这个会话之后的每次请求都按冷 prefill 计费,直到压缩。用户不会收到任何提示。这个 PR 之后,恢复出来的请求和实时的一样。问题是 #5582 新加的 E2E 场景发现的:它的缓存账本在重启后报出前缀断开。
Bug Reproduction Steps
config.toml里写一条 deny 规则,例如[[permission.rules]] decision = "deny" pattern = "Bash(rm *)"。rm keep.txt:规则直接拒绝,模型收到<system>ERROR: Tool execution failed.</system>开头的工具结果。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
packages/agent-core-v2/test/agent/loop/loop.test.ts:「cancelled during tool execution」这条用例原来断言没开始执行的call-work-2在日志里也有数字耗时。这正是被修的行为:模型实时看到的中止消息上没有耗时,日志里不该有。断言改成这条结果不带durationMs。依据是「恢复后发给模型的请求必须和实时一致」这条契约,E2E 的缓存账本按它检查。执行过的call-work-1有数字耗时的断言保留,仍然通过。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
durationMs: 0Wall time: 0.000 seconds,前缀和重启前接不上duration_mslegacyToolDurationMs)Wall time: 0.000受影响的人群:所有面(TUI、print、Desktop、Web、ACP、SDK)上,配置了 deny 规则,或者在工具开始前停止过回合,之后又恢复会话的用户。
验证:
packages/agent-core-v2/test全套 384 个文件、8436 条通过;packages/kap-server/test、packages/acp-server/test89 个文件、1779 条通过。tests/scenarios:本机第一次跑有 5 条失败,都是 KAP 进程 30 秒内没起来(stderr 为空,当时本机负载在 100 左右);负载降下来后重跑这 5 条所在的cold-recovery.test.ts和concurrent-file-writes.test.ts,7 条全过。pnpm lint通过。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.