Skip to content

fix(agent-core-v2): keep media recovery across turns and compaction - #4413

Merged
7Sageer merged 8 commits into
mainfrom
test/media-recovery-prefix-parity
Oct 26, 2026
Merged

7Sageer merged 8 commits into
mainfrom
test/media-recovery-prefix-parity

Conversation

@7Sageer

@7Sageer 7Sageer commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

修复媒体恢复在后续 turn 和压缩时失效,导致原始媒体被重复发送、新增媒体被误删的问题。

Bug Reproduction Steps

  1. 多图请求因 413 降级后,再发消息或 /compact:原图被重发并再次被拒。
  2. 图片因格式错误被 strip 后新增图片:正常图可能被旧快照误删;新的异常图无法再次恢复。
  3. undo 触发恢复的 turn 后继续会话:旧恢复状态需要失效,以重新尝试原媒体。

Root Cause

恢复状态按 turnId 保存且仅 turn 请求读取;继承的 strip 快照与单次请求的重试预算耦合;undo 恢复历史时未递增 prefixRevision。

这是根本修复:恢复状态改为跟随模型和上下文前缀,并独立限制单次请求的 strip 重试。

Code Changes

 media recovery
- 按 turnId 保存,仅当前 turn 使用
+ 按 model alias + context revision 保存
+ 后续 turn 和 auto / manual compaction 复用
+ 每次请求独立限制 strip 重试;operation 不回写 turn 状态

 context restore
+ 递增 prefixRevision,使 undo 前的恢复状态失效
  • 模型或历史前缀变化后重新尝试原始媒体;普通追加不失效。状态仅保存在内存,不改磁盘格式。
  • 测试 harness 新增 prompt-cache 前缀守卫,检查后续 turn / step / full compaction 是否延续最近成功请求的前缀。
  • 调试 state manifest 同步为 llmRequester.mediaRecovery。

Behavior Changes and Affected Users

影响范围:通过 TUI、print、desktop、web、VS Code、ACP 或 SDK 使用图片,并遇到 413 或图片格式拒绝的会话。

行为 改动前 改动后 谁依赖旧行为 退路
自动 / 手动压缩 从原始媒体重新恢复 沿用最近 turn 的投影;局部重试不回写 自动压缩或 /compact 用户 同第 1 行
undo 后首次请求 按 turnId 保存的旧状态自然失效 通过 revision 使 agent 级状态失效 undo 用户 无需迁移或配置
恢复警告 后续 turn 可能重复出现 仅新媒体触发进一步恢复时警告 触发恢复的会话 无需配置
调试 state key mediaDegradedTurns / mediaStrippedTurns mediaRecovery /api/v1/debug、kimi-inspect 及旧 key 读取方 改用新 key;会话文件不受影响

未触发媒体恢复的请求,以及配置、命令、wire 和 SDK 导出契约不变。

测试覆盖与验证:

  • llmRequester:跨 turn、auto / manual compaction、模型 / revision 失效、新增媒体与 strip 重试上限。
  • undo:degraded / stripped 恢复后 undo 会恢复原媒体。
  • fullCompaction:18 个前缀矩阵用例;harness 守卫覆盖后续 turn / step / compaction。
  • agent-core-v2 全量:7,433 通过、1 跳过;另有 tool.test.ts 的既有超时和 rpc-events.test.ts 的 ENOTEMPTY(单独重跑通过),均与 PR 无关。类型、无注释和 manifest 检查通过;lint:imports 剩 4 处既有问题。
  • 沿用 patch changeset;无新增命令、配置或文档更新。

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.

…very

Add failing tests showing that requests after a media-degraded or
media-stripped recovery do not extend the previous request's prefix:
the next turn and compaction requests resend full media first, and a
compaction request re-captures the strip snapshot, hiding media that
the loop request still showed.
Copilot AI balanced review requested due to automatic review settings October 26, 2026 06:30
@changeset-bot

changeset-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 83fd0ad

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

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

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.

Media recovery after a provider rejection was keyed by turn id and only
honored for turn requests, so the next turn and compaction requests
resent full media, got rejected again, and re-captured the strip
snapshot. Keep a single agent-level recovery keyed by model alias and
context prefix revision, applied to every later request until the model
changes or the history is rewritten.
@7Sageer 7Sageer changed the title test(agent-core-v2): pin prompt-cache prefix parity across media recovery fix(agent-core-v2): keep media recovery across turns and compaction Oct 26, 2026
Keep the per-request strip retry budget separate from the inherited media
snapshot so newly added media can recover from 413 and image-format
rejections. Stop recapturing once this request has stripped its media or
an inherited snapshot leaves no visible media.

Advance the context prefix revision when context state is restored so
undo invalidates media recovery without adding service dependencies.

Cover later turns, automatic and manual compaction, bounded retries, and
real prompt-to-undo flows for both degraded and stripped media.
@7Hanrui

7Hanrui commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-26T14:13:09.830329Z 83fd0ad 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.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 9e16361b19

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

…ness

Every harness agent now checks that the first attempt of each turn
request (a new turn or a later step) and of each full compaction request
extends the last successful turn request: same model profile fields
(cache key, thinking, extra params, response format, tool message
conversion), same system prompt, same tools unless the tool-name set
changed, and a history prefix. Requests after a model switch or a context
prefix rewrite are skipped, and media turned into the degraded
placeholder at the same position is allowed while the request runs under
media-degraded recovery. Violations fail the running test.

Add an end-to-end case that runs a turn after media recovery so the
next-turn check sees both degraded and stripped projections.
Bring the branch up to main (including the compaction prompt-cache prefix
matrix and its findGenerateCallPrefixBreak helper) together with the
rebased media recovery change, which folds the standalone compaction media
test into that matrix, and the test-harness guard that checks next-turn,
next-step, and full compaction requests extend the last turn request.
…empt

Recovery retries were recorded as the next baseline without being compared.
Compare profile fields, system prompt, and tools on every attempt, and only
treat a same-position media replacement as a prefix when it is the degraded
placeholder or a single media path tag. Count real comparisons so an unwired
guard fails the media-recovery test.
@7Hanrui

7Hanrui commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@codex review

1 similar comment
@7Hanrui

7Hanrui commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@codex review

@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: 83fd0ade82

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +679 to +680
recovery.modelAlias !== request.modelAlias ||
recovery.contextRevision !== request.contextRevision

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Invalidate recovery when an alias is reconfigured

When an active session's provider or model configuration is replaced without changing its alias, the model catalog rebuilds the requester but the history revision remains unchanged, so this check continues applying the old backend's degraded/stripped-media policy. A newly configured media-capable backend therefore silently receives omitted or reduced media until the history is rewritten or the process restarts; before this commit, each new turn retried the full media. Include the resolved provider/model identity in the recovery key or invalidate recovery on catalog changes.

AGENTS.md reference: AGENTS.md:L131-L133

Useful? React with 👍 / 👎.

@7Sageer
7Sageer marked this pull request as ready for review October 26, 2026 14:06
@7Sageer
7Sageer merged commit 8dabf4a into main Oct 26, 2026
19 checks passed
@7Sageer
7Sageer deleted the test/media-recovery-prefix-parity branch October 26, 2026 14:07
This was referenced Oct 27, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
The behaviour of MoonshotAI#4421 has been in the requester since 94397aa, but its
suite still asserted only that the lowered system message lost its
`tools` field, which a message kept in place would also satisfy. Assert
instead that no system message survives and the history keeps its
length, and rename the case after what it protects.

Also settle ten ledger rows that were already implemented here: MoonshotAI#3733,
MoonshotAI#4413, MoonshotAI#4418, MoonshotAI#4421, MoonshotAI#4435, MoonshotAI#4460, MoonshotAI#4465, MoonshotAI#4516, MoonshotAI#4523, MoonshotAI#4532 and the
MoonshotAI#4558 test calibration, each with a probe over the suite that pins it.
Two rows carried a probe without a test-name selector (MoonshotAI#4557, MoonshotAI#4558),
which aborted `--verify` for every other row; MoonshotAI#4557 is a changelog sync
and now carries a manual probe.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
MoonshotAI#4410 MoonshotAI#4413 MoonshotAI#4431 MoonshotAI#4450 MoonshotAI#4451 MoonshotAI#4485 MoonshotAI#4524 were each implemented earlier —
the compaction prefix suite, the media-recovery case, the OAuth callback
socket drain, the split delivering-work section, the compaction wording,
the reasoning-key order and the 10-step todo reminder are all in the tree
and pinned by their own tests. Each row now carries that probe.

Only MoonshotAI#4410 needed care: upstream patched paths that this fork renamed
(`human/llm/builtin/protocol` versus `human/llm/requester/bases`), so the
matching was done on the behaviour — stripDynamicToolContext is gone and
extractEventUsage already reads usage only on terminal response events.

Ledger: closed 22 -> 28, remaining 71.
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.

4 participants