Repository navigation
fix(agent-core-v2): send a tool result's note the same way after a session is restored - #5640
Merged
Merged
Conversation
…ssion is restored Live tool messages carry the note as their last text part, because the executor appends it to the output. A restored result keeps the note apart from the output, and renderToolResultForModel merged it into a single-part output with a newline. Write, Edit and other results with a note were therefore sent differently on the first request after a restart or /reload, which broke the prompt cache prefix from that result on. Failed and empty results with a note differed the same way. The renderer now appends the note the way the executor does, with an empty output shown as "Tool output is empty." first, and then renders the error status and the wall time. Live requests do not change. A projection test compares the live and the restored rendering for five kinds of result, and a new E2E cache scenario restarts and reloads after tools of many kinds. The cache catalog gains A9, and A8 now has explicit coverage for a turn stopped before its tool ran.
🦋 Changeset detectedLatest commit: 0fed077 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: |
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.
5 of 6 tasks
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
一个会话里用过 Write 或 Edit 之后,只要重启产品、
/reload或者重新载入这个会话,下一个请求的 prompt 缓存前缀就从第一个 Write 或 Edit 的结果处断开,后面的历史全部重新计费。写代码的会话几乎都会用到这两个工具,用户却收不到任何信号。这个 PR 让恢复出来的工具结果和实时发出时完全一样。这和 #5585 修的问题同一类:同一个工具结果,实时发出的样子和从日志回放的样子不一样。
Bug Reproduction Steps
/reload),回到这个会话,再发一条消息。"content": [{"type":"text","text":"Wrote 8 bytes to out.txt"}, {"type":"text","text":"<system>The change is applied; …</system>"}]"content": "Wrote 8 bytes to out.txt\n<system>The change is applied; …</system>"tool_result里两个 text 块tool_result里一个 text 块Root Cause
所有请求,实时的和恢复后的,都经过同一个投影(
agent/loop/projection.ts的projectedContent,最终调contextMemory/toolResultRender.ts的renderToolResultForModel)。两边的差别在送进来的消息:legacyResult(tool/executor/legacy-adapter.ts)已经把 note 作为最后一个文本块追加在输出后面。投影认出 note 已经在内容里,不再加。tool.result记录把输出和 note 分开存,折叠回来的消息只有输出,note 是单独的字段。renderToolResultForModel再把 note 加回去:输出只有一个文本块时,用\n把 note 拼进这一块。所以恢复后的块结构和实时不同。同一个函数还有两处会让恢复后的结果对不上:
Tool output is empty.,回放时又被换成<system>Tool output is empty.</system>。这是根本修复:
renderToolResultForModel先照执行器的做法把 note 作为最后一块并进输出,再按普通结果渲染错误前缀和耗时。实时那边送进来的内容本来就是这个形状,所以实时请求不变,恢复后的请求变得和实时一样。Code Changes
只改
renderToolResultForModel:阅读顺序:
packages/agent-core-v2/src/agent/contextMemory/toolResultRender.ts是修复本身;packages/agent-core-v2/test/agent/loop/projection.test.ts用执行器真实的legacyResult构造实时那条消息,用日志折叠后的形状构造恢复后的那条,比较两者的投影;tests/e2e/scenarios/cache/tool-results-keep-the-prefix-across-a-restart-and-a-reload.spec.ts从真实入口验证。Behavior Changes and Affected Users
\n拼进输出那一块<system>ERROR…</system>\n输出\nnote一块<system>Tool output is empty.</system>\nnoteTool output is empty.和 note 两块,和实时一样max_chars够不够)<system>Tool output is empty.</system>+ 换行 + note 估算Tool output is empty.+ note 估算,少 18 个字符;max_chars恰好落在这 18 个字符里的空读取,原来报「too small」,现在能读max_chars设得极小去读空文件的调用受影响的是所有经过引擎投影的客户端:TUI、print 模式、Desktop 和 Web、ACP、SDK、VS Code。另外
apps/vis的上下文投影也调这个函数,恢复出来的 note 在那里同样显示成单独一块。测试覆盖:
packages/agent-core-v2/test/agent/loop/projection.test.ts(新):成功的 Write、Edit,出错的结果,带耗时的 Bash,空输出,五种情况下恢复后的投影都等于实时,修复前五条全部失败。tests/e2e/scenarios/cache/tool-results-keep-the-prefix-across-a-restart-and-a-reload.spec.ts(新,三个场景)。场景先让模型用一批不同的工具,然后重启或/reload,再发一条消息,假模型的缓存账本检查前后两次主请求的前缀:/reload(ACP 没有):Write 和 Edit;修复前,重启那个在 acp、tui 上失败,
/reload那个在 tui 上失败;第三个在 acp 上本来就通过,它补上清单 A8 原来写着的缺口。清单
tests/e2e/scenarios/cache/catalog.yaml:新增 A9(重启或 reload 之前跑过各种工具),A8 补上显式覆盖、去掉缺口。验证
基于 main
0e1ceb9c8,macOS。pnpm exec vitest run --reporter=agent packages/agent-core-v2/test/agent/loop/projection.test.ts5 条通过;把toolResultRender.ts换回 main 的版本,5 条全部失败。packages/agent-core-v2/test/下的agent/loop、agent/contextMemory、agent/tokenCounting、session/tokenCounting、wire、agent/mcp、agent/toolExecutor、features/plan、agent/media、os/backends/node-local/tools、agent/tools,59 个文件,1541 条通过、1 条原本就跳过。packages/kap-server/test/sessions.test.ts、tests/scenarios/prompt-cache.test.ts、apps/vis/server/test:20 个文件、362 条通过。/reload场景在 tui 上断在账本上,停止场景在 acp 上通过。scenarios/cache整个目录:mcp-tool-change-keeps-the-tool-list,以及 TUI 上/init的 E6,修复分别在 fix(agent-core-v2): keep the tool list when a connected MCP server adds tools mid-session #5627、fix(tui): stop /init from leaving the terminal UI busy #5628。/reload缓存场景,共 5 条通过。pnpm lint通过,pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit和tests/e2e的类型检查通过,node --test 'scripts/checks/*.test.mjs'124 条通过,pnpm run check:duplication 0e1ceb9c8没有新增重复。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.