Skip to content

test(agent-core-v2): catch unnecessary prompt-cache misses in tests - #4452

Open
RealKai42 wants to merge 3 commits into
mainfrom
test/prompt-cache-guard
Open

RealKai42 wants to merge 3 commits into
mainfrom
test/prompt-cache-guard

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

用测试拦住不合理的 prompt cache miss。起因是 compaction 在默认触发点裁掉历史开头、导致 cache 失效这一反馈。

Bug Reproduction Steps

  1. 使用默认配置和 256k 及以上窗口的模型,让上下文增长到自动 compaction 的触发点(85% 窗口,或"窗口 − 50k")。
  2. 自动或手动触发 compaction。
  3. compaction 请求从历史开头裁掉消息,不再是上一次 turn 请求的前缀延伸,前缀 cache 几乎全部失效。从第二次 compaction 起,被裁掉的开头还包括上一轮的摘要。

本 PR 新增的 FullCompaction prompt-cache prefix 用例可以在 main 上稳定复现。

Root Cause

两层原因:

  1. 引擎:fix(agent-core-v2): pre-shrink compaction history to the effective model window #3911 引入的 pre-shrink 预算是 0.85 × (W − min(128Ki, W/8)) − 开销,约为 74% 窗口,低于默认自动触发点(85% 窗口或 W − 50k)。所以在约 196k 以上的窗口里,常规的自动和手动 compaction 都会裁掉历史开头。本 PR 不修引擎,引擎修复在另一个 PR 里做。
  2. 测试:test/harness/prompt-cache-guard.ts 对所有带裁剪的 compaction 请求、所有 context revision 变化、所有工具名集合变化都直接豁免,正好覆盖了这个 bug 所在的路径;同时没有任何用例让上下文达到默认触发点。本 PR 修的是这一层。

Code Changes

收紧前,先在 guard 里临时插桩,跑了一遍 agent-core-v2 全量(7460 个用例),统计每一次豁免。下表是收紧的依据:

规则 收紧前 全量审计数据 收紧后
R2 system prompt / tools / 请求参数 context revision 一变就完全不查 24 次 revision 变化,0 次稳定部分变化 跨 revision 也必须稳定(同一模型)
R3 工具名集合变化 名字一变就跳过 tools 比对 2 次,均为测试显式注册/注销工具 只有 tool registry 或 activeToolNames 确实变了才放行
R4 compaction 裁剪历史 droppedCount ≠ 0 就整体跳过 16 次主动裁剪中 13 次放得下却裁了 放得下却裁 = 违规(判定见下)
R5 operation 请求类型 不认识的 kind 直接不查 只有测试用的 direct_test 必须声明进 PREFIX_SHARING_OPERATIONS 或 INDEPENDENT_OPERATIONS

R4 的判定也是引擎修复需要满足的约定:

允许 compaction 首个请求裁剪历史,当且仅当满足其一:
  max(启发式估算的完整请求, 引擎 token 计数) + 指令 + min(max_tokens, W / 8) > W
  该 agent 之前出现过 context overflow / 413
同一 revision 上的后续 compaction 请求(截断、空响应、overflow 后的重试)不受此限制

文件变化:

 packages/agent-core-v2/
 ├── test/harness/
 │   └── prompt-cache-guard.ts             # R2–R5,导出 takePromptCacheViolations() 供自测使用
 ├── test/agent/fullCompaction/
 │   └── fullCompaction.test.ts            # +10:默认触发点 × 128k/256k/1M/2M × manual/auto,以及连续两轮
 ├── test/agent/llmRequester/
 │   └── llmRequester.test.ts              # +10 guard 自测(每条规则一正一反),+1 resume 场景
 ├── test/agent/undo/
 │   └── undo.test.ts                      # +1:undo 之前的历史仍是下一次请求的前缀
 └── src/human/test/llm/
     └── cache-key.test.ts                 # +3:openai / kimi / anthropic 序列化后的 body 逐个请求保持前缀延伸
  • 复现测试用一个按完整请求(sp + tools + 历史)上报 usage 的 provider,触发点直接取自 RuntimeCompactionStrategy,全程走真实 turn。
  • guard 自测做过变异验证:把 R2–R5 和两条宽松逻辑逐一改坏,每次都只有对应的自测失败。序列化测试也验证过:把 openai 的 lowering 改成"只保留最后一条 assistant 的 reasoning",openai 和 kimi 两个用例都会失败。
  • undo / cancel 没有做成全局规则:相邻 user 消息在请求里会被合并,合并逻辑分散在 projection、tool-select 整形和 provider lowering 好几层,guard 无法精确判断切点,所以改用场景测试。cancel 不改 revision,本来就在 R1 覆盖范围内。
  • 顺带精确钉住了一个已知行为:通用 openai 适配器在没有配置 thinking 时,一旦历史里出现 reasoning,会自动补 reasoning_effort: 'medium',请求参数会因此变一次。kimi 和 anthropic 不受影响。是否要改留给后续讨论。

合入顺序:在当前 main 上有 13 个预期内的失败,全部是上述 bug 的实例,必须和引擎修复一起合入,或在它之后合入:

Behavior Changes and Affected Users

运行时行为:None。只改了 test/ 和 src/human/test/ 下的测试与 harness,没有改任何运行时代码、默认值或契约文件(config/wire/state manifest、apiSurface 快照、node-sdk 入口、apps/kimi-code/src/cli/ 都没动)。

受影响的只有在 agent-core-v2 里写测试的贡献者:

Behavior Before After Who relies on the old behavior Escape hatch
不必要的 compaction 裁剪 静默通过 测试失败 #3911 的 4 个 pre-shrink 用例和 plain 413 用例 让测试历史确实超出窗口
跨 context 改写的 sp/tools/参数变化 静默通过 测试失败 无(审计 0 次) 无
没有 registry 变化的工具名集合变化 静默通过 测试失败 无(审计中的 2 次都有 registry 变化) 先注册或注销工具
未声明的 operation 请求类型 静默通过 测试失败 无(只有 direct_test,已声明) 在 guard 的两个集合之一里声明

测试覆盖:每一行都有 llmRequester.test.ts > prompt-cache guard 里对应的自测。

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.

…ult threshold

Drive real turns up to the default auto-compaction threshold with a
provider that meters the full request, for 128k, 256k, 1M and 2M
windows and for manual and auto triggers, and assert the compaction
request extends the last turn request's prompt-cache prefix. A second
round also asserts the previous compaction summary is still sent.
- keep the system prompt, tools and request params stable across
  context revisions for the same model
- accept a tool table change only when the tool registry or the
  active tool names changed
- flag a first compaction request that drops history although the
  full request plus the output reserve fits the context window,
  unless the provider already rejected the context
- require every operation request kind to declare whether it shares
  the turn prefix

Add self-tests for each rule and a resume scenario that starts the
first request after a resume as a prefix extension of the last one.
…alization

Assert that undo keeps everything before the undone turn as the next
request's prefix, and that the openai, kimi and anthropic requesters
serialize each request body as a prefix extension of the previous one.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 16:25
@changeset-bot

changeset-bot Bot commented Oct 26, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: fe3c7bd

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

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@RealKai42
RealKai42 marked this pull request as ready for review October 27, 2026 03:51
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T03:53:09.130925Z fe3c7bd Draft marked ready
🔒 Security Review ✅ Completed 2026-10-27T03:54:41.003160Z fe3c7bd Draft marked ready
ℹ️ 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 Oct 27, 2026

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

commit: fe3c7bd

This branch has not been deployed

No deployments
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