Skip to content

refactor(core): stamp turn and step envelope on loop events - #5672

Merged
sailist merged 1 commit into
mainfrom
refact-477-11-27-core-event-lifecycle-stack/01-stamp-turn-step-envelope
Dec 1, 2026
Merged

sailist merged 1 commit into
mainfrom
refact-477-11-27-core-event-lifecycle-stack/01-stamp-turn-step-envelope

Conversation

@sailist

@sailist sailist commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

core LoopEmitted 目前缺少完整的路由信封:LlmRunEvent 全家没有 turnId / step,tool.ended / tool.async_ended 没有归属回合,且内存投影与 wire.jsonl 各自生成一套步骤 UUID,导致上层无法直接消费 core 事件。本 PR 是 refact-477 事件驱动重构栈的第 1/10 层(base: main),在 loop 合同层以纯增量方式补齐 turnId、step 与统一 stepId,为后续内存消费者直读 core 事件打地基。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路:不在 LLM pipeline 层强行注入 turn 概念(下层 kosong / requester 并不知道 loop 的回合边界),而是在 core loop 合同层定义带作用域的交叉类型,并在 ActorTurn / LoopActor 的发射点统一注入已有的 this.point({ turnId, step })。

 LoopEmitted
-  llm.* / tool.delta / tool.ended / tool.async_ended  # 无 turnId / step
-  step.started                                      # 无 stepId
+  ScopedLlmRunEvent = LlmRunEvent & { turnId, step }
+  tool.delta / tool.ended / tool.async_ended + { turnId, step }
+  step.started + { stepId }                         # 由 ActorTurn 进入新 step 时生成

关键改动:

  • packages/core/src/loop/contract/emitted.ts:新增 ScopedLlmRunEvent;为工具 delta/结束/异步结束事件增加 turnId、step;为 step.started 增加 stepId。
  • packages/core/src/loop/turn/contract.ts:TurnStep 增加 readonly id: string。
  • packages/core/src/loop/turn/create-turn.ts:创建 step 时生成 randomUUID();在 step.started、requestProgress、completeRequest(含 llm.delta.* 与 llm.retry)及工具发射点注入路由信封。
  • packages/core/src/loop/loop-actor.ts:为 6 处 tool.ended 与 2 处 tool.async_ended 补齐归属 turnId / step。

阅读顺序:contract/emitted.ts(看类型契约)→ turn/create-turn.ts(看 stepId 生成与发射注入)→ loop-actor.ts(看跨回合异步工具归属)。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
core LoopEmitted 事件对象字段 LLM / 工具事件无 turnId、step;step.started 无 stepId 增量携带 turnId、step、stepId,既有字段不变 core loop.subscribe 订阅者(新增可选/只读字段,不破坏既有读取) 无(纯增量)
wire 1.x / KAP / ACP / SDK / Hook 输出 保持原样 逐字段不变(v2 本层尚未读取新字段) CLI、Desktop、Web、VS Code、外部 KAP/ACP 客户端 无

受影响模块与验证:

  • @moonshot-ai/core:test/loop/requester.test.ts 覆盖跨步、重试、中止的信封断言;m0-dev core 套件 434/434 通过。
  • @moonshot-ai/agent-core-v2:test/agent/loop/core/{hooks,record,requester}.test.ts、journal.test.ts、toolAdapter.test.ts 补齐跨步、恢复、detach 信封断言。
验证记录
  • 本地 packages/core、packages/agent-core-v2、packages/kap-server、packages/acp-server、packages/klient、packages/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 通过)。
  • 本地 lint-pool.sh(含 check:duplication)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.(按栈维护约定,本栈 10 层库级 patch changeset 统一集中在第 10 层提交。)
  • Ran gen-docs skill, or this PR needs no doc update.

Scope LoopEmitted LLM events with turnId and step via ScopedLlmRunEvent, stamp originating turnId and step on tool.delta, tool.ended, and tool.async_ended, and emit a per-step stepId on step.started.
@changeset-bot

changeset-bot Bot commented Nov 28, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 23f5c6a

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 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:40:02.236128Z 23f5c6a 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@23f5c6a
npx https://pkg.pr.new/@moonshot-ai/kimi-code@23f5c6a

commit: 23f5c6a

@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: 23f5c6abcc

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


export type TurnContinuation = 'turn' | 'background' | 'none';

export type ScopedLlmRunEvent = LlmRunEvent & { readonly turnId: number; readonly step: number };

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 Add this core contract change to a changeset

This alters the @moonshot-ai/core event contract, but the commit contains no changeset and explicitly defers it to a later stack layer. If this layer lands or releases before that later commit, the library version and changelog will omit the new required event fields, leaving consumers without an accurate compatibility signal; add a patch changeset for this commit rather than relying on a future PR.

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

Useful? React with 👍 / 👎.

this.currentStep = step;
this.runtime.transition(() => {
this.emit({ type: 'step.started', ...this.point });
this.emit({ type: 'step.started', ...this.point, stepId: step.id });

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 Reuse the emitted step ID in existing projections

When agent-core-v2 handles this event, turnProjection.ts still calls createStepState(event.step, ...) and publishes that newly generated UUID, while journal.ts independently stores another randomUUID() for the same (turnId, step). Consequently the new core stepId, the observable turn.step.started ID, and the persisted wire step UUID remain different, so consumers still cannot correlate the same step across the event surfaces this change is intended to unify; pass event.stepId through both existing consumers instead of creating parallel IDs.

AGENTS.md reference: AGENTS.md:L109-L110

Useful? React with 👍 / 👎.

@7Sageer 7Sageer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM for layer 1/10. Reusing the emitted stepId in the existing v2 projections is handled in #5674, and per stack convention the library changesets are consolidated in #5681.

@sailist
sailist merged commit 1784fac into main Dec 1, 2026
23 checks passed
@sailist
sailist deleted the refact-477-11-27-core-event-lifecycle-stack/01-stamp-turn-step-envelope branch December 1, 2026 04:43
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.

2 participants