Skip to content

test(agent-core-v2): check that a resumed session sends every tool result as it did live - #5658

Merged
RealKai42 merged 1 commit into
mainfrom
test/resumed-history-matches-live
Nov 27, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
test/resumed-history-matches-live

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

恢复会话(重启、/reload、重新载入)之后,第一个请求发给模型的历史必须和实时发出的一模一样,否则 prompt 缓存从第一处不同开始全部失效,用户看不到任何信号。最近修的两个缓存问题都属于这一类:#5585(被拒的工具恢复后多出 Wall time)和 #5640(Write、Edit 的说明恢复后被拼成一段)。两次都是很晚才被偶然发现:已有的检查只比较恢复前后的引擎状态,不比较真正发出去的请求;而且只有测试作者想到的那几种工具结果才经过重启。

这个 PR 在引擎测试里加一条通用检查:恢复后投影出的下一个请求,必须和实时投影出的一样。另外加一个工具结果矩阵,按结果类型的字段组合出各种形状(细节和没覆盖的字段见下文),而不是挑几个工具。它跑在每个 PR 都必过的 test 作业里。

Bug Reproduction Steps

N/A:只改测试。

Root Cause

N/A。

Code Changes

测试工具 packages/agent-core-v2/test/harness/agent.ts:

 expectResumeMatches()
-  resume from the wire records, compare engine state
+  checkResume(compare engine state)                      # unchanged behavior
+
+expectResumeSendsTheSameHistory()
+  checkResume(compare engine state, and compare the next request's messages)
+
+checkResume(check)                                        # the old body: resume from the wire records, then check(resumed)
+
+nextRequestMessages(agent)
+  project(previewRequestContent().messages)               # the projection every request goes through
+  drop attributes                                         # lowering reads only isError, which the projection already drops
  • 比较什么:两边用同一个投影函数 project(),处理各自 loop 里的原始消息(previewRequestContent().messages,也就是请求实际用的那份)。
    • 提醒不过滤:原有的状态比较会先过滤掉提醒,而提醒也在前缀里。
    • 工具清单不在这里比:恢复后、发第一个请求之前,工具选择还没初始化。「恢复后第一次请求的工具和恢复前一样」已有专门的用例,会真的发请求去比。
  • 矩阵用例:packages/agent-core-v2/test/wire/resume.test.ts 的「carries every kind of tool result exactly as the live session sent it」,用 testkit 的假模型。
    • 一个按参数返回不同结果的测试工具,覆盖纯文本、带说明、出错带说明、空输出带说明、空输出、多块带说明、出错、被截断、图片带说明,以及执行时抛错。
    • 一个名字像 MCP 工具的测试工具,用来走会渲染耗时的那条路径(带说明、不带说明),再加一个被权限规则拒绝的工具。
    • 最后一个调用工具的步骤是一次并行调用。
    • 结果形状从 ExecutableToolResult 的字段出发,所以以后新加的结果字段能直接加进表里,不依赖记得去测某个具体工具。这次没覆盖 spill、spillExempt、stopTurn、stopTurnReason 四个字段:stopTurn 会结束回合,要单独一个用例;spill 要真的写出落盘文件。
  • 清单:tests/e2e/scenarios/cache/catalog.yaml 的 A8、A9 各加一条引擎覆盖,指向这个用例。

为什么新比较还不对所有调用方打开:expectResumeMatches() 已经被 31 个测试文件调用。把新比较加进去,有两条现有用例会失败,而且都是真问题。另外还有一个问题,这次的矩阵为了绕开它,改变了编排方式:

现有情况 实时的下一个请求 恢复后
一次并行调用里有一个被权限规则拒绝 工具调用和结果按模型给出的顺序 被拒的调用排到最前面(日志按工具开始执行的先后写 tool.call)
工具开始前的钩子抛错,回合失败(loop.test.ts「lets a loop error handler recover a non-context loop error by retrying」) 助手消息带着这个工具调用和一条「结果不可用」 这个工具调用不见了
中断后撤销掉所有回合(loop.test.ts「keeps an interruption reminder when undo only removes a later turn」) 历史为空 历史里还剩那条中断提醒

这三处会另外修。修好之后,expectResumeMatches() 改成也做这项比较,所有调用方就都会检查;那两条现有用例,以及「并行批次里有被拒的调用」,就是它们的回归测试。这个 PR 里的矩阵因此把被拒的调用单独放成一步,并行批次里不放。

阅读顺序:先看 agent.ts 里的 checkResume 和 nextRequestMessages,再看 resume.test.ts 里的矩阵(最后一个 describe,后面是原有的辅助函数)。

Behavior Changes and Affected Users

None:只改了测试工具、一个测试文件和缓存清单,产品代码没动。expectResumeMatches() 的行为不变,31 个调用它的测试文件结果不变。

验证

基于 main 87058b908(#5585、#5640 都已合入)。

  • 新用例证明有效:
  • 新比较临时对所有调用方打开时,31 个文件、1263 条里有 2 条失败,就是上表的第 2、3 行;第 1 行由另外的临时用例确认,有被拒调用的批次失败、没有的通过。
  • pnpm exec vitest run --reporter=agent 跑全部 31 个调用 expectResumeMatches() 的测试文件,加上新用例,共 1264 条通过。
  • pnpm lint 通过(包括 test-doubles:新用例用 testkit 的假模型),pnpm exec tsc -p packages/agent-core-v2/tsconfig.json --noEmit 通过,node --test 'scripts/checks/*.test.mjs' 124 条通过,pnpm run check:duplication 87058b908 没有新增重复。

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.

…sult as it did live

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.
@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: f9b2673

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 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-27T14:43:57.851384Z f9b2673 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 27, 2026

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

commit: f9b2673

@RealKai42
RealKai42 merged commit 74c75cb into main Nov 27, 2026
20 of 29 checks passed
@RealKai42
RealKai42 deleted the test/resumed-history-matches-live branch November 27, 2026 15:24
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.

1 participant