Skip to content

fix(loop): cancel prompts while their turn is starting - #5394

Merged
sailist merged 1 commit into
mainfrom
codex/ci-cancelled-turn-restore
Nov 25, 2026
Merged

sailist merged 1 commit into
mainfrom
codex/ci-cancelled-turn-restore

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

取消提示可能发生在引擎已经选中它、还在准备首步的时候。这个窗口内,调用方会收到取消成功,模型随后仍收到这条提示。该竞态也让主 CI 的引擎取消测试在核对恢复历史时失败。GitHub Actions 失败作业

Bug Reproduction Steps

在 main 上运行 pnpm exec vitest run --reporter=agent packages/agent-core-v2/test/agent/loop/loop.test.ts -t 'does not run loop error handlers for aborted turns'。测试中的实时历史包含 go,刚恢复的历史为空。

门控复现把第一条提示停在提交前回调,排入第二条,再取消第一条。旧代码向模型发送了两次请求,历史从已取消的提示开始。

Root Cause

Loop 在提交前把提示从收件队列移到正在启动的回合。取消路径只尝试从收件队列移除它,随后把提示标记为已取消。回合持有的输入继续执行,模型和回放观察到了与取消结果矛盾的状态。

Code Changes

flowchart LR
    C[按 ID 取消提示] --> P{提示仍在准备首步}
    P -->|是| A[中止启动并按 ID 移除提示]
    A --> Q[按原顺序处理其余排队输入]
    A --> J[为未宣布的回合写入 queued TurnCancel]
    P -->|仍在队列| M[按 ID 从队列移除]
Loading

引擎按提示 ID 取消时启用启动阶段的处理。Loop 在同一 actor 操作中退回尚未提交的输入并移除目标,从而避免目标被再次取走。取消投影清除暂存的提示状态。未宣布的回合使用现有 TurnCancel 记录编号,后续恢复可以继续编号,撤销历史也只包含已经形成的提示边界。

Behavior Changes and Affected Users

行为 改动前 改动后 受影响人群
按 ID 取消正在启动的提示 返回成功后,提示仍可能进入模型 启动中止,后续排队提示继续按顺序执行 Desktop 和 Web 中停止刚发送提示的用户,以及调用 prompt abort 的 KAP 客户端
为尚未宣布的取消保留编号 取消与启动争用,恢复可能看到不同历史 使用既有 TurnCancel 表示取消并保留编号 恢复或撤销会话的客户端

macOS 上的两条回归连续 8 轮通过,共 16 次执行。完整的受影响套件通过 15 个测试文件、303 条用例,覆盖引擎回合、撤销与恢复、KAP 提示及会话场景。Core 和引擎类型检查及 pnpm lint 通过。

Checklist

  • 阅读贡献指南并从 main 复现红灯。
  • 新增回归覆盖模型请求、队列、持久化取消和恢复。
  • 核对 KAP 现有的取消约定和回放记录。
  • 按 gen-changesets 记录 patch 变更。

@changeset-bot

changeset-bot Bot commented Nov 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: c465305

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

This PR includes changesets to release 5 packages
Name Type
@moonshot-ai/kimi-code Patch
kimi-code-app Patch
@moonshot-ai/core Patch
@moonshot-ai/agent-core-v2 Patch
@moonshot-ai/kap-server 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 25, 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-25T07:24:09.774686Z c465305 PR opened
🔒 Security Review ✅ Completed 2026-11-25T07:24:14.557005Z c465305 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 25, 2026

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

commit: c465305

@wbxl2000
wbxl2000 self-requested a review November 25, 2026 13:01
@sailist
sailist merged commit 4d833db into main Nov 25, 2026
24 checks passed
@sailist
sailist deleted the codex/ci-cancelled-turn-restore branch November 25, 2026 13:25
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 26, 2026
Port upstream 4d833db onto the fork's LiveTurn actor. Loop.cancel accepts
includeStarting; when the prompt still sits in an unannounced starting turn,
the actor marks cancelledBeforeStart, aborts that turn and returns the
startingTurnId, announceTurn skips it, the journal writes a queued
turn.cancel and the prompt projection drops the pending turn. The loop
service passes includeStarting from its prompt-cancel path.

Verified: core and agent-core-v2 typecheck clean; coreLoopService 20 pass
including the new cancel-during-start regression; journal/record/wait 12
pass. The remaining loop.test.ts failures are the pre-existing loopEnd and
system-reminder snapshot drift.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 26, 2026
Mark MoonshotAI#5394 ported with its cancel-during-start regression; record MoonshotAI#5359 and MoonshotAI#5351 as blocked on the TUI continuation capability and the tool activation model, and keep MoonshotAI#5432 partial until those rows close.
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.

3 participants