Skip to content

refactor(agent-core-v2): commit turn.ended through LoopJournal and publish at the durable boundary - #5681

Open
sailist wants to merge 1 commit into
refact-477-11-27-core-event-lifecycle-stack/09-source-step-delta-observersfrom
refact-477-11-27-core-event-lifecycle-stack/10-commit-turn-ended-durable-boundary
Open

sailist wants to merge 1 commit into
refact-477-11-27-core-event-lifecycle-stack/09-source-step-delta-observersfrom
refact-477-11-27-core-event-lifecycle-stack/10-commit-turn-ended-durable-boundary

Conversation

@sailist

@sailist sailist commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

本 PR 是 refact-477 事件驱动重构栈的第 10/10 层(base: 09-source-step-delta-observers),解决剩余的两个结构性问题:① onWillBeginStep / onDidFinishStep 抛错或中止时,core 仍发出 done / blocked(step_hook_failed),依赖 v2 旁路折算为 failed / cancelled;② 会同步派生 durable 记录的订阅者若直接消费提交前的 CoreLoopEvent(turn.ended),会把衍生记录插队到 wire turn.ended 之前,破坏落盘行序。此外,按栈维护约定,本栈 10 个逻辑改动的库级 patch changeset 统一集中在这一笔提交。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路:每个 core 事件在内存中只有一个发布时点——流式/瞬时事件继续即时透传,而 durable 边界事件 turn.ended 在 EventDispatcher 提交 TurnEnded 的内存临界区内(同步内存 fold + wire.appendRecord 入队之后、eventBus.publish 之前、物理磁盘 flush 之前)原子发布。

sequenceDiagram
    participant Core as ActorTurn.closeTurn
    participant Journal as LoopJournal
    participant Disp as EventDispatcher
    participant Bus as IEventBus
    Core->>Core: 统一收敛终态(含步骤钩子 terminal / failed / filtered / max_steps)
    Core->>Journal: turn.ended → commitTurnEnded
    Journal->>Disp: dispatch(TurnEnded)
    Disp->>Disp: 同步内存 fold + wire.appendRecord(turn.ended)(dispatching=true)
    Disp->>Bus: onDidCommit → publish CoreLoopEvent(turn.ended)(恰好一次)
    Disp->>Disp: drainQueue()(重入派生的 durable 记录排在 turn.ended 之后)
    Disp->>Bus: publish TurnEnded / 后续 PromptCompleted
Loading

关键改动:

  1. core 步骤钩子终态收敛(packages/core/src/hook/loop/、contract/、turn/create-turn.ts)
    • StepStartingDecision / StepStoppingVerdict 新增可选 turnOutcome?: TerminalTurnOutcome。
    • ActorTurn.closeTurn 按 aborted > 钩子 / errorHandler terminal > failed > filtered > max_steps 统一收敛。
    • v2 删除 ActiveTurn.failedStep / abortReason 旁路,保持 step.ended 与 wire step.end.finishReason 不变。
  2. 提交点钩子与单源发布(packages/agent-core-v2)
    • IEventDispatcher 新增 onDidCommit(cls, listener)(回放不触发)。
    • LoopJournal.commitTurnEnded 接管 durable TurnEnded 的组装与派发(reason、error、durationMs、interruptReason、stopReason、requestId、replyModel、modelSteps、continuation 全部保持一致)。
    • 激活回合在 onDidCommit(TurnEnded) 中恰好发布一次 CoreLoopEvent(turn.ended);未激活回合(gate-blocked 等)仍在原处即时透传。
    • turnInterruptReason 纯函数移至 turnOps.ts 共享。
  3. Changesets:集中包含 @moonshot-ai/core、@moonshot-ai/agent-core-v2、@moonshot-ai/kap-server、@moonshot-ai/acp-server 共 10 份 patch 条目。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
core.loop.event(turn.ended) 发布时间(激活回合) loop.subscribe 顶部(wire turn.ended 入队前) wire.appendRecord(turn.ended) 入队后、v2 TurnEnded 总线发布前(不等磁盘 flush) 已迁移的 core 订阅者(C04~C07 均不派生 Agent durable 记录,观察结果不变) 无
步骤钩子抛错 / 中止时的 core turn.ended done / blocked(step_hook_failed) failed / aborted,与 v2 TurnEnded / StopFailure / Interrupt 完全一致 直读 core 事件的订阅者与 External Hook 无
wire 1.x 物理行序 / KAP WS / Stop Hook 前 flush [turn.ended, 衍生记录..., prompt.completed] 物理行序逐字节不变,Stop 子进程前仍显式 await wire.flush() CLI、Desktop、Web、VS Code、SDK、Hook 脚本 无

受影响模块与验证:

  • @moonshot-ai/core:test/loop/requester.test.ts 覆盖步骤钩子终态收敛优先级。
  • @moonshot-ai/agent-core-v2:eventDispatcher.test.ts 覆盖 onDidCommit 临界区与回放隔离;journal.test.ts、loop.test.ts、coreLoopService.test.ts、requestMetadata.test.ts 覆盖 LoopJournal 提交 TurnEnded 与恰好一次发布(本层定向测试 172/172 通过)。
  • E2E:core-event-passthrough 11 个场景 KAP 帧 / REST / stepIdentity 100% 一致(wire 仅含上游 fix(agent-core-v2): restore loop tool baseline from session journal #5635 引入的 tools.settled 漂移);loop-requester-convergence 10/10、external-hook-io 4/4 通过;loop-lifecycle-timeline 9/9 运行时断言通过(仅 core.event turn.ended 按上述设计位移)。
整栈验证记录
  • 本地 core、agent-core-v2、kap-server、acp-server、klient、node-sdk typecheck 0 错误。
  • m0-dev:core 434/434 通过;agent-core-v2 8496 通过 / 3 失败(2 个为 m0-dev git 2.34.1 config.worktree 已知环境限制,1 个 mcpCore/client-stdio.test.ts 超时抖动单独复跑 26/26 通过)。
  • 本地 kap-server 219/219、acp-server 208/208 通过。
  • 本地 lint-pool.sh(含 check:duplication vs a6ac07d130db)0 退出。

Checklist

  • I have read the CONTRIBUTING document.
  • 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 28, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a2db0f1

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

This PR includes changesets to release 4 packages
Name Type
@moonshot-ai/agent-core-v2 Patch
@moonshot-ai/acp-server Patch
@moonshot-ai/core 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 28, 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-28T08:41:30.090504Z ff49a5b 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 28, 2026 •

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

commit: a2db0f1

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

ℹ️ 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".

return {
blocked: run.decision?.blocked ?? run.blocked,
blocked,
turnOutcome: run.decision?.blocked === undefined ? undefined : run.decision.turnOutcome,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reject or honor standalone step-starting outcomes

StepStartingDecision now permits a hook to return { turnOutcome: { type: 'failed', error } } without blocked, but this branch silently discards that outcome. For such a valid hook result, ActorTurn proceeds to the model request and never applies the requested terminal failure. Either make turnOutcome require blocked in the contract and runtime guard, or honor it as a terminal decision here.

AGENTS.md reference: AGENTS.md:L211-L213

Useful? React with 👍 / 👎.

@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/09-source-step-delta-observers branch from 6434dba to d760a99 Compare November 30, 2026 11:12
@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/10-commit-turn-ended-durable-boundary branch from ff49a5b to d3bd966 Compare November 30, 2026 11:12
@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/09-source-step-delta-observers branch from d760a99 to b10a68b Compare December 1, 2026 07:24
@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/10-commit-turn-ended-durable-boundary branch from d3bd966 to 23a8d20 Compare December 1, 2026 07:24
@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/10-commit-turn-ended-durable-boundary branch from 23a8d20 to a2db0f1 Compare December 1, 2026 07:37
@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/09-source-step-delta-observers branch from b10a68b to b94cb9a Compare December 1, 2026 07:37

This branch has not been deployed

No deployments
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