Skip to content

fix(kap-server): drop the partial reply of a retried step - #4855

Merged
wbxl2000 merged 1 commit into
release/2026-11-12from
fix/retry-drops-partial-reply
Nov 12, 2026
Merged

wbxl2000 merged 1 commit into
release/2026-11-12from
fix/retry-drops-partial-reply

Conversation

@wbxl2000

Copy link
Copy Markdown
Collaborator

Requirement or Bug

模型回复流到一半断开、自动重试成功后,Web / 桌面端会同时显示失败那次的半截回复和重试的完整回复,开头重复,例如「Affected: eu-west Affected regions are eu-west and us-east.」。

#4630 之后,重试请求不再带上那段半截回复,模型会从头作答,所以这个问题在本版变得必然出现。这个 PR 在重试开始时删除失败那次的回复,界面只保留重试的结果。

Bug Reproduction Steps

  1. 让模型的流式回复在中途断开,例如输出 Affected: eu-west 后断开连接;让自动重试返回完整回复。
  2. 回复结束后看会话:失败那次的半截文字和重试结果相邻显示。重新加载后同样如此。

Root Cause

  • 流断开时,loop 把失败那次的半截内容记成一个 step 的 content.part,然后以 finishReason: 'error' 结束这个 step。
  • 重试时,用新的 step uuid、相同的 step 序号写 step.begin。
  • 会话条目的 id 是 ${stepUuid}:${block},所以重试产生的是新条目,旧条目一直保留,实时推送和重新加载都是两条相邻的 assistant_message。

在 #4630 之前也是两条,但当时重试请求末尾带着半截回复,模型多半会接着写,拼起来是通顺的。#4630 改成从干净的请求重试之后,模型看不到半截回复,就会重答。

本 PR 在会话投影层根本修复,不改 loop 的重试语义。

Code Changes

 conversationFold
   step.end
+    记录这一步是否以 error 结束
   step.begin
+    若上一步以 error 结束且序号相同(即同一步的重试)
+      删除上一步 uuid 名下的条目
     记录新的一步

 conversationFeed.emitItemChanges
+  fold 后不再存在的条目 → 推送 item.removed
   新增或变化的条目 → 推送 item.started / item.updated / item.ended
  • 判断只依赖落盘的 step.end / step.begin 记录,不依赖不落盘的 TurnStepRetrying。所以实时推送和重新加载的结果一致,已有的会话日志重新加载时也会被修正。
  • 所有重试都失败时,不会再出现新的 step.begin,最后一次的半截回复照旧保留。
  • item.removed 是已有的事件,客户端按 item_id 通用处理,不需要改。文档里这个事件的说明同步更新(中英文)。

Behavior Changes and Affected Users

行为 之前 之后 谁依赖旧行为 退路
一步失败后被重试,失败那次已经输出的回复 作为独立条目保留在会话里 重试开始时删除(实时推送 item.removed,重新加载时不再出现) 无(重复显示是 bug) 不需要
所有重试都失败 保留最后一次的半截回复 不变 — —
fold 删除仍存在的轮次中的条目(例如这里的重试,或撤销轮内的一部分条目) 在线客户端收不到通知 推送 item.removed 无 不需要
模型上下文 后续轮次仍包含失败那次的半截回复 不变(本 PR 不处理) — —

受影响的客户端:desktop 与 web(会话 API)。TUI 和 print 模式不走会话投影,不受影响。

已知的小代价:

  • 删除发生在重试请求真正发出时,也就是退避等待之后。等待期间半截回复和重试提示会一起显示,然后被替换,界面会闪动一次。
  • 后续轮次的模型上下文仍包含被取代的半截回复。这来自 fix(desktop): preserve retry and background task behavior #4831 有意加入的行为,要改需要原作者确认,这里不动。

测试覆盖(conversation.test.ts):

  • fold:重试成功后只剩重试结果;所有重试都失败时保留半截回复。
  • feed:实时推送依次为 item.started → item.ended → item.removed(旧条目)→ item.started(新条目),分页结果里只剩新条目。

去掉修复后,这两条测试失败。

验证
  • packages/kap-server 全量:1379 条通过,1 条跳过;typecheck 通过。
  • 另用一个临时端到端测试复现(真实 server + 模拟模型,回复中途断开):
    • release 头上,实时和重新加载都是两条 assistant_message;
    • refactor(agent-core-v2): port agent loop runtime core #4630 之前同样是两条,但重试请求末尾带着半截回复;
    • 修复后,实时在新条目开始前收到 item.removed,实时和重新加载都只剩重试结果。
  • 临时测试没有提交。

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.

@changeset-bot

changeset-bot Bot commented Nov 11, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 36a2adb

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 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-11T17:29:09.836471Z 36a2adb 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 11, 2026

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

commit: 36a2adb

@wbxl2000
wbxl2000 merged commit fb8ea02 into release/2026-11-12 Nov 12, 2026
15 checks passed
@wbxl2000
wbxl2000 deleted the fix/retry-drops-partial-reply branch November 12, 2026 04:45
@github-actions github-actions Bot mentioned this pull request Nov 12, 2026
Grapedge added a commit that referenced this pull request Nov 12, 2026
* fix(agent-core-v2): truncate oversized skill content (#4834)

* fix(kap-server): bind the main agent when a session is created (#4839)

* fix(kap-server): bind the main agent when a session is created

* fix(kap-server): reject a profile on prompt submissions

* fix(kap-server): reject a profile change through the session profile

* fix(kap-server): wait for model configuration before binding a new session

* fix(agent-core-v2): deduplicate restored task notifications (#4836)

* fix(agent-core-v2): deduplicate restored task notifications

* chore: add notification replay fix changeset

* fix(agent-core-v2): preserve task notifications after prompt-only crashes

* fix(agent-core-v2): complete prompts before ending turns

* test: align lifecycle snapshots with prompt completion order

* fix(agent-core-v2): settle all prompts during turn finalization

* fix(kap-server): drop reserved metadata keys from session profile updates (#4842)

* fix(kap-server): drop reserved metadata keys from session profile updates

* fix(kap-server): keep stored reserved metadata keys on profile updates

* docs: split and rewrite the server API reference (#4841)

* docs: split and rewrite the server API reference

* docs: redirect old server API anchors to the split pages

* docs: describe profile metadata as a replacement

* docs: mark question responses as answered-only

* fix(kap-server): avoid heartbeat timeout after scheduling stalls (#4823)

* fix(kap-server): avoid heartbeat timeout reaping after stalls or clock jumps

* fix(kap-server): grace heartbeat window for short scheduling stalls

* fix(kap-server): treat only clearly late heartbeats as scheduling stalls

* fix(kap-server): time heartbeats on a monotonic clock and drain queued input before reaping

---------

Co-authored-by: Kimi Code (Grok Bot) <[email protected]>

* fix(kap-server): undo a turn only once when requests race (#4846)

* docs: describe heartbeat stall handling (#4849)

* docs: describe heartbeat stall handling

* docs: explain the monotonic clock in the heartbeat notes

* fix(agent-core-v2): acquire session lock before loop restoration (#4848)

* fix(agent-core-v2): acquire session lock before loop restoration

* chore: add session lock lifecycle changeset

* fix(agent-core-v2): hold session lock through loop restoration

- add an operation-scoped lease to the session run gate
- hold it across loop initialization, reset, and pending drains
- cover idle timeout and release ordering

* fix(agent-core-v2): keep the output of stopped tool calls (#4854)

* fix(kap-server): drop the partial reply of a retried step (#4855)

* fix(agent-core-v2): dedupe historical tool schemas (#4862)

* fix(agent-core-v2): dedupe historical tool schemas

* chore: add duplicate tool schema changeset

* fix(agent-core-v2): preserve first tool schema declaration

* fix(agent-core-v2): remind on changed tool schemas

* fix(agent-core-v2): block stale tool schema calls

* fix(agent-core-v2): normalize registry schemas in pending checks

* fix(agent-core-v2): import registry tool metadata type

* fix(agent-core-v2): preserve abort handling during validation

* fix(agent-core-v2): refine validation hook preparation

* fix(agent-core-v2): make validation hooks synchronous

* fix(agent-core-v2): preserve tool result notes on fork (#4867)

* fix(agent-core-v2): preserve tool result notes on fork

* chore: add fork tool result note changeset

* fix(agent-core-v2): scope tool result note restoration to forks

* fix(agent-core-v2): normalize inherited tool result notes

* fix(agent-core-v2): isolate fork note restoration

* feat(tui): sweep running titles toward text and add KIMI_CODE_TUI_ANIMATION_FPS (#4843) (#4870)

* chore: sync web dist from code-app (#4871)

code-app: 8ab17294e031323186d357d121c5360b8353900e

Co-authored-by: kimi-agent-bot <[email protected]>

* ci: release packages (#4835)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* chore: sync web dist from code-app

code-app: 5835391dac3a2b9e0c65ffbd418f0d6042a7adb2

---------

Co-authored-by: Haozhe <[email protected]>
Co-authored-by: qer <[email protected]>
Co-authored-by: Kimi Code (Grok Bot) <[email protected]>
Co-authored-by: grapedge <[email protected]>
Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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