Skip to content

refactor(agent-core-v2): publish enriched core loop events immediately on the agent event bus - #5674

Merged
sailist merged 1 commit into
mainfrom
refact-477-11-27-core-event-lifecycle-stack/03-publish-core-loop-events
Dec 1, 2026
Merged

sailist merged 1 commit into
mainfrom
refact-477-11-27-core-event-lifecycle-stack/03-publish-core-loop-events

Conversation

@sailist

@sailist sailist commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

v2 内部模块与协议出口目前只能订阅 AgentTurnProjection 二次翻译后的 v2 事件,且内存 stepId 与 wire step.begin.uuid 是两套独立 UUID。本 PR 是 refact-477 事件驱动重构栈的第 3/10 层(base: 02-prune-unanswered-tool-calls),在 v2 边界建立带 agentId 的 core 事件即时透传通道,并将正常步骤的内存与落盘 stepId 统一为 core step.started.stepId。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路:复用现有 IEventBus 的类型化订阅、跨 Agent 路由与 Scope 自动退订能力,定义非持久化的 CoreLoopEvent(core.loop.event),在 loop.subscribe 内完成同步 enrich 后立即发布,绝不等待 LoopJournal 或磁盘 I/O。

flowchart LR
    A[Core LoopActor] --> B[turns.enrich]
    B --> C[IEventBus.publish CoreLoopEvent<br/>durable=false, 同步即时透传]
    B --> D[notifications.project / turns.project]
    B --> E[LoopJournal.append<br/>旁路写 wire 1.x]
Loading

关键改动:

  • 新增 packages/agent-core-v2/src/agent/loop/coreLoopEvent.ts:CoreLoopEvent、onCoreLoopEvent 类型收窄订阅助手,以及穷尽映射纯函数 turnOutcomeReason(done→completed、aborted→cancelled、failed→failed、blocked→blocked)。
  • loopService.ts:在 turns.enrich 之后、journal.append 之前同步发布 CoreLoopEvent。
  • journal.ts / turnProjection.ts:优先复用 core step.started.stepId;重试第 N>1 次 attempt 没有新的 step.started,LoopJournal 仍分配独立 wire UUID,以保持 KAP 实时 item_id 前缀与回放 item.removed 行为完全不变。
  • 与上游 fix(agent-core-v2): restore loop tool baseline from session journal #5635 合并:tools.settled 同时作为 core.loop.event 即时透传,并由 LoopJournal 落盘为 [wire] tools.settled。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
IEventBus 实时事件流 只有翻译后的 v2 事件 每个 core 事件同步多发一条 core.loop.event(durable=false) 引擎内 IEventBus 订阅者(本层尚无生产订阅者消费新事件) 无
正常步骤 stepId 与 wire step.begin.uuid 内存与落盘各生成一套 UUID 统一使用 core step.started.stepId(重试 attempt>1 仍独立) 观测内部总线/调试快照;KAP item_id 与回放结构保持一致 无
wire 1.x 记录序列与 KAP WS 帧 保持原样 记录类型、字段、顺序逐字节一致(仅 UUID 值同源化) CLI、Desktop、Web、VS Code、SDK、冷恢复 无

受影响模块与验证:

  • @moonshot-ai/agent-core-v2:新增 coreLoopEvent.test.ts;更新 loop.test.ts、config.test.ts、plan.test.ts、tool.test.ts 总线事件流快照,并在 test/harness/snapshots.ts 归一化 core.loop.event 易变字段。
  • E2E:core-event-passthrough 11 个场景 kapFrames / rest / stepIdentity 100% 一致(wire 差异仅来自上游 fix(agent-core-v2): restore loop tool baseline from session journal #5635 新增 tools.settled,剔除后逐字节一致)。
验证记录
  • 本地 6 包 typecheck 0 错误;本层 5 个快照/集成测试文件 409/409 通过。
  • m0-dev core 434/434、agent-core-v2 8496 通过(仅已知环境用例);lint-pool.sh 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.(本栈 changeset 统一集中在第 10 层提交。)
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 7340278

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:39:12.710327Z cd34d3e 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@7340278
npx https://pkg.pr.new/@moonshot-ai/kimi-code@7340278

commit: 7340278

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

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

this.syncRequester();
loop.subscribe((event) => {
const enriched = this.turns.enrich(event, this.toolExecutor);
this.eventBus.publish(new CoreLoopEvent({ agentId: this.scopeContext.agentId, event: enriched }));

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 Keep core loop envelopes out of the SDK event stream

When a Node SDK session uses the v2 engine, SessionEventWiring subscribes to every IEventBus event (packages/node-sdk/src/v2/session-wiring.ts:164-168), and translateDomainEvent passes unknown types through by default (packages/node-sdk/src/v2/event-mapper.ts:93-110). Consequently, every event published here—including per-token deltas, raw provider response headers, and internal reminder messages—is delivered to SDK listeners as core.loop.event, despite that type not belonging to the public closed Event union. This changes the external SDK stream and can break consumers that exhaustively validate event types; explicitly drop this envelope in the SDK mapper or publish it on an internal-only channel.

AGENTS.md reference: AGENTS.md:L191-L193

Useful? React with 👍 / 👎.

@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/03-publish-core-loop-events branch from cd34d3e to 38c9f44 Compare November 30, 2026 11:12
@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/03-publish-core-loop-events branch from 38c9f44 to ff1b09e Compare December 1, 2026 07:24
@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/02-prune-unanswered-tool-calls branch from 4e18382 to 5f8957b Compare December 1, 2026 07:24
Base automatically changed from refact-477-11-27-core-event-lifecycle-stack/02-prune-unanswered-tool-calls to main December 1, 2026 07:30
@sailist
sailist force-pushed the refact-477-11-27-core-event-lifecycle-stack/03-publish-core-loop-events branch from ff1b09e to 7340278 Compare December 1, 2026 07:37
@sailist
sailist merged commit fae5dad into main Dec 1, 2026
22 checks passed
@sailist
sailist deleted the refact-477-11-27-core-event-lifecycle-stack/03-publish-core-loop-events branch December 1, 2026 07:45
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