Skip to content

test: add the e2e prompt cache topic - #5526

Merged
RealKai42 merged 3 commits into
mainfrom
test/e2e-cache-topic
Nov 26, 2026
Merged

RealKai42 merged 3 commits into
mainfrom
test/e2e-cache-topic

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Prompt 缓存命中直接决定每一轮对话的成本和延迟,用户在会话里做的很多事(重启、重试、改文件、MCP 服务变工具、压缩……)都可能让产品改写请求前缀。E2E 的假模型默认已经开着缓存账本,但只有一个场景专门针对缓存。把现有 36 个场景在 6 个 project 上的账本导出来看,有 10 个场景只有一个被接受的主请求,在缓存上什么都没验证;产品如果在会话中途换了缓存键,账本也不会报错;场景还没法模拟用户配置 MCP 服务、改 provider 配置这些情况。

这个 PR 在 tests/e2e 下建立 cache 专题:一份清单列出会改变请求前缀的用户操作和每项的承诺,lint 保证清单和场景对得上;补上让场景能模拟这些情况的基建;先写第一批 6 个场景,并把现有的重启缓存场景搬进来。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路:判定只看产品发给模型服务的请求,场景只用用户能做的操作驱动产品。tests/e2e/scenarios/cache/catalog.yaml 是这个专题的总账:每一项写明用户操作、承诺(保持前缀、有意断开,或者还没查清)和哪些测试覆盖它,没覆盖的写明缺什么。新的 lint 检查保证账和场景一致。场景框架多了三个选项(provider、config、mcp)和一个对所有场景都生效的收尾检查:同一个会话的主请求只能用一个缓存键。

概念 回答什么问题 本 PR 之后
cache 清单 有哪些用户操作可能改写请求前缀,每项承诺什么,谁在测 catalog.yaml,39 项,其中 20 项写明了缺口
覆盖种类 这一项靠什么测试守着 explicit(scenarios/cache/ 下的场景)、implicit(别的 E2E 场景会走到,由默认的账本检查覆盖)、kap(tests/scenarios)、engine(包内测试)
缓存键收尾检查 产品有没有在会话中途换缓存键 主请求用到的缓存键个数超过「1 + newSession() 的次数」就失败
flowchart LR
  catalog["scenarios/cache/catalog.yaml<br>用户操作、承诺、覆盖、缺口"]
  check["scripts/checks/e2e-cache-catalog.mjs<br>pnpm lint"]
  specs["scenarios/cache/*.spec.ts"]
  session["support/scenario.ts<br>provider / config / mcp 选项<br>缓存键收尾检查<br>prompt-cache 失败附件"]
  testkit["testkit<br>假模型的缓存账本<br>HTTP 版 MCP 假服务"]
  product["真实产品<br>TUI、ACP、Desktop、Web"]
  check --> catalog
  check --> specs
  specs --> session
  session --> testkit
  session --> product
  product --> testkit
Loading

清单和检查只管「账对不对」,缓存本身的判定仍然由 testkit 的账本做:场景把用户操作做完,账本按服务端的规则比较前后两次请求,场景再显式断言延续关系。

决策 选择 没选 理由
清单格式 YAML 加 lint 检查 写在 Markdown 里;写成 TS 常量 检查读得了、评审看得懂;和 tests/quarantine.yaml 一个做法。场景只能 import support/scenario.ts 和 testkit 的剧本,TS 常量要改边界规则
MCP 假服务 一律用本机的 HTTP 假服务 stdio 子进程 场景要在会话中途改工具,测试进程里的 HTTP 服务能直接改,子进程改不了
工具变化后的同步 addTool 一类的方法等假服务收到产品新的 tools/list 才返回 固定等待;在界面上找证据 产品收到通知后是异步重新读取的,不等的话下一条消息可能赶在它前面;界面上没有能看到工具清单的通用位置
缓存键检查 收尾检查 每个场景自己断言 账本对新缓存键只开一条新线、不报错,放在收尾检查里所有场景都能拦住
基建和场景 放在一个 PR 基建单独开 PR 没有场景用的选项看不出对不对

阅读指南:

  1. tests/e2e/scenarios/cache/catalog.yaml:专题覆盖什么、缺什么;
  2. scripts/checks/e2e-cache-catalog.mjs 和它的测试:清单要守的规则;
  3. tests/e2e/support/scenario.ts:provider、config、mcp 怎么变成测试 home 里的 config.toml 和 mcp.json,mcp() 句柄怎么等产品重新读取工具清单,收尾检查和失败附件;
  4. tests/e2e/scenarios/cache/ 下的场景。

可以略读:packages/testkit 里 writeMcpConfig 多接受 HTTP 和 SSE 条目、步骤标题、CODEOWNERS、lint 接线和 tests/e2e/AGENTS.md 的说明。

第一批场景:

清单项 场景 证明什么
A1 restart-keeps-the-prefix(从 conversation/ 搬来,内容不变) 工具调用、审批和重启之后,所有请求在同一个前缀上
A4 automatic-compaction-keeps-the-prefix 回合中途上下文超过压缩阈值:压缩请求延续前缀,压缩后只有历史重新开始,下一回合接着压缩后的那次
C1 mcp-tool-change-keeps-the-tool-list 没开按需加载时,MCP 服务中途新增工具:工具清单不变,重启后也不变,只用提醒告诉模型
C1 mcp-tool-change-with-on-demand-loading-keeps-the-prefix 开了按需加载、服务设为 deferred 时,新工具从对话里公告,前缀不变
D6 retry-after-a-finished-turn-keeps-the-prefix 成功的回合之后服务报错重试:重试的请求和失败的那次一模一样,延续之前成功的那次
E2 files-changed-outside-the-product-keep-the-prefix 两个回合之间用户在外面改文件、加文件:system 不变(里面的目录清单在会话内是固定的),模型读到新内容
E4 trusting-a-new-folder-keeps-the-prefix TUI 上首次信任工作区之后,连续对话和重启都在同一个前缀上

Behavior Changes and Affected Users

没有改产品代码,产品行为不变。测试一侧的变化:

行为 之前 之后 谁依赖旧行为 退路
E2E 收尾检查 不看缓存键 主请求的缓存键个数超过「1 + newSession() 的次数」就失败 现有 E2E 场景和驱动自测 不需要:整套 E2E 在 6 个 project 上都满足
E2E 失败附件 只有 model-requests 多一个 prompt-cache:每个请求的通道、缓存键、基线、断点和复用量,以及账本报出的问题 看失败报告的人 —
重启缓存场景的路径 scenarios/conversation/keeps-one-prompt-cache-prefix-across-a-restart.spec.ts scenarios/cache/restart-keeps-the-prefix.spec.ts,内容不变 按路径跑它的人 —
pnpm lint 没有清单检查 多跑 scripts/checks/e2e-cache-catalog.mjs 所有贡献者 —
testkit 的 writeMcpConfig 只接受 stdio 条目 也接受 { transport: 'http' | 'sse', url, deferred? } KAP 场景和 testkit 的使用者 旧写法不变

验证:整套 E2E(场景加驱动自测)在 macOS 上 6 个 project 都跑过;同步到最新的 main 之后,又重跑了受影响的部分。结果见下面的折叠块。新的收尾检查做过破坏性验证:临时让场景会话不统计 newSession(),approval/new-session-asks-again 在收尾时报出两个缓存键,失败报告里带上了 prompt-cache 附件。只在 macOS 上跑过;Linux 和 Windows 交给这个 PR 的 E2E 作业。

运行的命令和结果
  • pnpm lint:通过,包括新的 e2e-cache-catalog(39 项,7 个 cache 场景都已登记,20 项写明了缺口)。
  • pnpm exec tsc -p tests/e2e/tsconfig.json --noEmit、pnpm exec tsc -p packages/testkit/tsconfig.json --noEmit:通过。
  • node --test 'scripts/checks/*.test.mjs':115 条通过,其中 8 条是新检查的。
  • pnpm exec vitest run packages/testkit/test/fixtures.test.ts:60 条通过。
  • 整套 E2E,同步 main 之前的基线 32b0ddcab,跑构建产物(TESTKIT_CLI_BUILD=dist):
    • --project=tui --project=tui-kitty --project=acp --project=acp-bare:707 条通过、113 条不适用,用时 6.0 分钟;
    • --project=web --project=desktop:374 条通过、36 条不适用,用时 24.6 分钟。
  • 同步到 main 5a6d97c1d 之后,在 6 个 project 上重跑 scenarios/cache/、conversation/quit-during-turn-keeps-the-prompt、files/file-tool-cards-survive-restart,以及 restart、restartDuringTurn、newSession 的自测:
    • CLI 侧 82 条通过、2 条不适用;
    • Desktop 和 Web 40 条通过、2 条不适用。
  • 破坏性检查:临时让场景会话不统计 newSession(),approval/new-session-asks-again 在 TUI 上收尾失败,报出两个缓存键;json reporter 里能看到 prompt-cache 附件。改动已还原。
写场景时查清的两件事
  • 会话的工具清单是固定的。 读代码时我以为没开按需加载时,MCP 服务新增工具会改写工具清单,场景跑出来不是这样。会话开始时工具清单就定下来,重启后也不变(toolSelectService.ts 的 state.initial)。新增的工具只在提醒里出现,提醒原文是 The current model cannot load runtime tool additions。这对缓存是好的,清单里 C1 的承诺按实测写成「保持」。
  • ACP 上重读一个被外面改过的文件。 产品先通过编辑器读了文件,然后才发出这次 Read 的 tool_call。ACP 驱动的 expectTool 要求读取发生在工具运行期间,所以 E2 场景只对第一次读取查工具卡,第二次读取用回复和模型收到的内容来验证。这和缓存无关,单独记录,没有在这个 PR 里处理。

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.

Add tests/e2e/scenarios/cache with a catalog of the user operations that
may change the prompt prefix the product sends to the model service, a
lint check that keeps the catalog and the cache scenarios in step, and
the first cache scenarios: automatic compaction, an MCP server adding a
tool with and without on-demand tool loading, a retry after a finished
turn, files changed outside the product, and trusting a new folder. The
existing restart cache scenario moves into the same directory.

Scenarios can now set the fake provider, merge config.toml and declare
MCP servers whose tools change in the middle of a session. Every
scenario fails when the main requests of a session use more than one
prompt cache key, and failed scenarios attach the prompt cache ledger.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 2698180

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 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-26T11:47:31.090738Z 2698180 New commits
ℹ️ 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@2698180
npx https://pkg.pr.new/@moonshot-ai/kimi-code@2698180

commit: 2698180

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ab100c5666

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/e2e/support/scenario.ts Resolved
The MCP server handle in the e2e scenarios returned once the product had
asked for the tool list again, before the fake server had answered, so
the next prompt could reach the product before it applied the change.
Record answered requests on the fake HTTP MCP server and wait for the
answer to the next tools/list instead.

The product applies a refreshed tool list asynchronously and does not
promise that the very next request reflects it, so the two MCP cache
scenarios now require the new tool to be announced exactly once after
the change rather than in the next request.
@RealKai42
RealKai42 merged commit b6326df into main Nov 26, 2026
30 checks passed
@RealKai42
RealKai42 deleted the test/e2e-cache-topic branch November 26, 2026 12:09
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