Repository navigation
refactor(agent-core-v2): commit turn.ended through LoopJournal and publish at the durable boundary - #5681
Conversation
🦋 Changeset detectedLatest commit: a2db0f1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 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, |
There was a problem hiding this comment.
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 👍 / 👎.
6434dba to
d760a99
Compare
ff49a5b to
d3bd966
Compare
d760a99 to
b10a68b
Compare
d3bd966 to
23a8d20
Compare
…blish at the durable boundary
23a8d20 to
a2db0f1
Compare
b10a68b to
b94cb9a
Compare
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),会把衍生记录插队到 wireturn.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关键改动:
packages/core/src/hook/loop/、contract/、turn/create-turn.ts)StepStartingDecision/StepStoppingVerdict新增可选turnOutcome?: TerminalTurnOutcome。ActorTurn.closeTurn按aborted> 钩子 / errorHandlerterminal>failed>filtered>max_steps统一收敛。ActiveTurn.failedStep/abortReason旁路,保持step.ended与 wirestep.end.finishReason不变。packages/agent-core-v2)IEventDispatcher新增onDidCommit(cls, listener)(回放不触发)。LoopJournal.commitTurnEnded接管 durableTurnEnded的组装与派发(reason、error、durationMs、interruptReason、stopReason、requestId、replyModel、modelSteps、continuation全部保持一致)。onDidCommit(TurnEnded)中恰好发布一次CoreLoopEvent(turn.ended);未激活回合(gate-blocked等)仍在原处即时透传。turnInterruptReason纯函数移至turnOps.ts共享。@moonshot-ai/core、@moonshot-ai/agent-core-v2、@moonshot-ai/kap-server、@moonshot-ai/acp-server共 10 份 patch 条目。Behavior Changes and Affected Users
core.loop.event(turn.ended)发布时间(激活回合)loop.subscribe顶部(wireturn.ended入队前)wire.appendRecord(turn.ended)入队后、v2 TurnEnded总线发布前(不等磁盘 flush)turn.endeddone/blocked(step_hook_failed)failed/aborted,与 v2TurnEnded/StopFailure/Interrupt完全一致[turn.ended, 衍生记录..., prompt.completed]Stop子进程前仍显式await wire.flush()受影响模块与验证:
@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 通过)。core-event-passthrough11 个场景 KAP 帧 / REST /stepIdentity100% 一致(wire 仅含上游 fix(agent-core-v2): restore loop tool baseline from session journal #5635 引入的tools.settled漂移);loop-requester-convergence10/10、external-hook-io4/4 通过;loop-lifecycle-timeline9/9 运行时断言通过(仅core.event turn.ended按上述设计位移)。整栈验证记录
core、agent-core-v2、kap-server、acp-server、klient、node-sdktypecheck 0 错误。core434/434 通过;agent-core-v28496 通过 / 3 失败(2 个为 m0-dev git 2.34.1config.worktree已知环境限制,1 个mcpCore/client-stdio.test.ts超时抖动单独复跑 26/26 通过)。kap-server219/219、acp-server208/208 通过。lint-pool.sh(含check:duplicationvsa6ac07d130db)0 退出。Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.