Skip to content

fix(core): settle aborted turns through lifecycle hooks - #5209

Closed
sailist wants to merge 1 commit into
refact-405-loop-contractsfrom
refact-405-turn-abort
Closed

sailist wants to merge 1 commit into
refact-405-loop-contractsfrom
refact-405-turn-abort

Conversation

@sailist

@sailist sailist commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

abort 活跃 Turn 时,部分路径无法保证 Step/Turn 终态事件完整,晚到的模型结果还可能继续进入正常工具批次或 continue。需要让取消、请求完成、工具完成和 abort grace 到期都经过同一个封闭的 Actor 结算路径。

Bug Reproduction Steps

N/A

Root Cause

Turn 的正常 continuation 与 abort 收尾共享入口,缺少明确的来源阶段和 abort 专用结算,导致晚到结果可以触发正常生命周期副作用,active Step 的 ended 顺序也不完整。

Code Changes

  • 新增 aborting/finishing 与 abortFrom,在 Actor 内统一处理 abort、晚到请求结果、批次完成与 grace 到期。
  • 晚到 done + toolCalls 只生成 aborted 工具结果,不再执行 tool.batch-starting;活跃 Step 不启动新的 step.continuing。
  • runStepEnding 对 aborted draft 调用 step.stopping,但保持 aborted outcome,不允许 hook 反转为 done/continue。
  • TurnStep.ending 复用正在进行的 ending,避免 abort 时重复执行 step.stopping。
  • owner.ending(at=abort来源) 保留 requesting 阶段的 reminder 恢复行为;force abort 只缩短等待,不跳过工具结果或终态事件。
  • 增加晚到 tool calls 与 ending 中 abort 的对抗测试,覆盖事件顺序、禁止重复 stopping 和禁止反转 abort。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
活跃 Step 的 abort 结算 可能缺少 step.stopping 或完整 Step ended 顺序 固定为 step.stopping → step.ended → owner.ending → turn.ended → done,outcome 始终为 aborted Loop/v2 的 abort、取消与中断消费者 这是明确的生命周期修复,无配置迁移
abort 后晚到模型 tool calls 可能进入正常 batch hook 或 continue 仅合成 aborted 工具结果,不进入正常 batch/continue 工具宿主、hook 与 late callback 路径 晚到结果不得恢复执行
无 active Step 的 abort 无统一 ending 保证 不执行 Step hook/事件,但仍经过 owner.ending → turn.ended → done Turn 完成等待方 N/A
force/重复 abort 等待 grace 后结算 只缩短等待,仍保留全部工具与终态结算 取消路径 N/A

验证:本地 Turn abort 对抗测试 3/3;core 与 agent-core-v2 tsc --noEmit 通过;Linux 远程环境相关回归 53 个文件、633/633;仓库完整 lint 与 diff 检查通过。

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; the change is limited to private internal packages, so no release changeset is required.
  • No user documentation update is required for this internal lifecycle fix.

@changeset-bot

changeset-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 57f0d14

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-21T10:18:36.723103Z 57f0d14 New commits
ℹ️ 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 21, 2026 •

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

commit: 57f0d14

- route abort completion through actor-owned settlement
- run step stopping before aborted step and turn endings

@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: e563adb8af

ℹ️ 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 +742 to +744
if (isPromiseLike(ending)) {
ctrl.spawn((proc) => this.finishAfterEnding(proc, at, step, ending));
return;

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 Preserve a forced exit from stopping hooks

When an onDidFinishStep/step.stopping handler ignores the already-aborted signal and never resolves, this path leaves the turn in finishing while awaiting it indefinitely. Further forced aborts return immediately because closing is already true, so neither abortGraceMs nor shutdown can settle AgentLoopService.cancel(), loop.settled(), or the turn's done promise. Before this change, aborted drafts bypassed step.stopping; retain the new hook ordering but add a bounded forced-settlement path.

AGENTS.md reference: AGENTS.md:L111-L115

Useful? React with 👍 / 👎.

): StepEndingRun | Promise<StepEndingRun> {
const failures: StepEndingFailure[] = [];
if (draft.type === 'aborted' || draft.type === 'blocked') return { outcome: draft, failures };
if (draft.type === 'blocked') return { outcome: draft, failures };

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 Add the required core changeset

This commit changes source behavior in the @moonshot-ai/core library but adds no .changeset entry; the existing core changeset describes an unrelated JSONL migration. Without a dedicated patch changeset, this cancellation fix will not be represented in the library's versioning and changelog, contrary to the repository's mandatory library-source workflow.

AGENTS.md reference: AGENTS.md:L169-L170

Useful? React with 👍 / 👎.

): StepEndingRun | Promise<StepEndingRun> {
const failures: StepEndingFailure[] = [];
if (draft.type === 'aborted' || draft.type === 'blocked') return { outcome: draft, failures };
if (draft.type === 'blocked') return { outcome: draft, failures };

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 Do not start compaction from an aborted step

For users with compaction_trigger_ratio < 0.85, cancelling after context usage crosses that trigger now runs the v2 onDidFinishStep chain for the aborted draft. The built-in full-compaction handler consequently calls checkAutoCompaction(false) and launches a new automatic model request without the aborted step's signal, so pressing cancel can still consume API work and later rewrite the conversation context. Aborted drafts previously returned here before any stopping hook ran; skip completion-only after-step work for aborts or propagate cancellation into the compaction task.

AGENTS.md reference: AGENTS.md:L111-L115

Useful? React with 👍 / 👎.

@sailist

sailist commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #5211.

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