Skip to content

refactor(core): unify turn hooks and execution - #5210

Closed
sailist wants to merge 2 commits into
refact-405-turn-abortfrom
refact-405-turn-execution
Closed

sailist wants to merge 2 commits into
refact-405-turn-abortfrom
refact-405-turn-execution

Conversation

@sailist

@sailist sailist commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

Turn 同时支持同步值与 Promise 时,executeStep 需要维护同步快路径、直接 .then() 和多组 continuation,执行路径分裂且 ending 只能同步完成。本层统一 hook 的 Promise 边界与 Actor 等待方式,并让正常 Step 与 Turn 关闭各自保持线性、封闭的控制流。

Bug Reproduction Steps

N/A

Root Cause

blocking 串行语义被同步返回优化耦合,导致 hook 组合器和 Turn 消费者分别处理多种返回形态。统一 Promise 后,原先依赖同步相邻的事件可以发生外部交错,因此关闭路径也必须显式异步化并保留一次性守卫。

Code Changes

  • starting/requesting/continuing/ending 四个 Turn 宿主 hook 全部 Promise 化;业务 hook 仍可同步返回值,组合器通过 await 保持 blocking 顺序与短路。
  • executeStep 使用单个 Promise<void> 线性函数和英文阶段注释;blocked、直接 done、请求前置失败、abort 与非 continue ending 采用 guard clause,正常路径保持扁平。
  • 所有 hook 与工具等待统一经过 proc.step,删除同步快路径和 finishStep/completeStepEnding/completeContinuing/advanceStep continuation 链。
  • finishTurn 同步封闭一次后启动唯一 close process,复用或创建 aborted Step ending,等待异步 owner.ending,再提交消息、发一次 turn.ended、resolve 一次 done。
  • 保留 step.ended 重入 abort 写入的 outcome,防止局部 done 覆盖;晚到 hook/请求结果继续受 closing 与 Step identity 隔离。
  • 增加 Deferred blocking 顺序、异步 ending 等待、append 过期和 once 验证;通知测试显式等待真实 onConsume。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
core 内部 TurnHooks 同步值或 Promise 均可,ending 必须同步 四个组合 hook 均返回 Promise,blocking 顺序不变 仅 LoopActor 与直接 Turn 测试等内部调用方 业务 hook 仍可同步返回;无可发布公共 API 变化
外部事件时序 部分 hook、Step/Turn 收尾同步相邻 允许外部事件在 Promise 恢复点与收尾之间交错 依赖同步相邻而非因果顺序的消费者 保持 blocking 顺序、owner.ending 最后提交、turn.ended 先于 done、active 清理后再调度下一 Turn
通知消费时序 前一 Turn 的 result 可能等待通知 Turn 的 append/onConsume 通知 Turn 可晚于前一 Turn result 启动,随后仍按序消费 使用前一 Turn result 推断通知已消费的调用方 应等待实际通知结果/onConsume,而不是依赖偶然调度顺序
abort 与重入 aborted outcome 与专用结算顺序 保持上一层的 abort 语义,并防止重入 done 覆盖 aborted 取消与 late callback 路径 N/A

验证:本地相关测试 3 个文件、93/93;core 与 agent-core-v2 tsc --noEmit 通过;Linux 远程环境相关回归 53 个文件、633/633;仓库完整 lint 与 diff 检查通过。未运行全 monorepo;无 Windows 专属路径,未做 Windows 验证。

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; changes are limited to private internal packages, so no release changeset is required.
  • No user documentation update is required for these internal execution changes.

@changeset-bot

changeset-bot Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 74213b7

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:14.679938Z 74213b7 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@74213b7
npx https://pkg.pr.new/@moonshot-ai/kimi-code@74213b7

commit: 74213b7

- await blocking hooks through actor steps
- linearize step execution and asynchronous turn closing
- document turn and step lifecycle phases
- handle terminal step decisions with guard clauses
@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