Skip to content

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

Merged
wbxl2000 merged 1 commit into
release/2026-11-12from
fix/undo-concurrent-same-turn
Nov 11, 2026
Merged

wbxl2000 merged 1 commit into
release/2026-11-12from
fix/undo-concurrent-same-turn

Conversation

@wbxl2000

Copy link
Copy Markdown
Collaborator

Requirement or Bug

两个撤销请求同时针对同一轮(例如用户连点两次撤销,或两个客户端同时撤销)时,两个请求都会成功,第二个会把用户没要求撤销的更早一轮也删掉。对话被静默多删,用户没有任何提示。

Bug Reproduction Steps

  1. 会话里有三轮对话 t0、t1、t2。
  2. 同时发两个 POST /api/v1/sessions/{session_id}:undo,turn_id 都是 t2。
  3. 两个请求都返回 0,对话只剩 t0(t1 也被删了)。

Root Cause

ConversationFeed.undo 先按当时的轮次算出要撤销的锚点数,再把这个数量交给引擎的撤销服务。引擎把并发的撤销排队依次执行,但执行的是请求时算好的数量:第一个撤掉 t2 之后,第二个仍然从新的末尾再撤同样的数量,删掉了 t1。

根本修复:在 feed 上把撤销请求串行化,每个请求在轮到自己时才按最新状态查找目标轮次、计算数量。第一个撤销完成后,第二个找不到 t2,返回 40421。

Code Changes

 ConversationFeed
-  async undo(turnId)
-    find turnId in current turns, count anchors, engine.undo(count)
+  undo(turnId)
+    queue undoNow(turnId) after the previous undo of this feed
+  undoNow(turnId)
+    find turnId in current turns, count anchors, engine.undo(count)

每个 agent 只有一个 ConversationFeed 实例(ConversationService 按 agent 缓存),所以按 feed 串行化覆盖了同一 agent 的所有撤销请求。

Behavior Changes and Affected Users

行为 之前 之后 谁依赖旧行为 退路
同时撤销同一轮的两个请求 都成功,第二个多删更早的轮次 第一个成功,第二个返回 40421(这一轮不存在) 无(旧行为是数据丢失) 无需
同时撤销不同轮次 都按请求时的数量执行,结果取决于顺序 依次按最新状态执行;目标轮次已被前一个请求删掉的,返回 40421 无 无需
单个撤销请求 不变 不变 — —

受影响的是通过服务端 API 撤销的客户端:desktop、web 和直接调用 API 的脚本。终端界面走 SDK,不经过这个 feed,不受影响。

测试:packages/kap-server/test/sessions.test.ts 新增「两个请求同时撤销同一轮只撤一次」,修复前两个请求都成功且多删了一轮;修复后连跑 10 次稳定通过。kap-server 全部 1377 个测试通过。

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: b158f54

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-11T14:29:19.083366Z b158f54 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@b158f54
npx https://pkg.pr.new/@moonshot-ai/kimi-code@b158f54

commit: b158f54

@wbxl2000
wbxl2000 merged commit ac09c02 into release/2026-11-12 Nov 11, 2026
15 checks passed
@wbxl2000
wbxl2000 deleted the fix/undo-concurrent-same-turn branch November 11, 2026 14:51
@github-actions github-actions Bot mentioned this pull request Nov 11, 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