Skip to content

fix(kap-server): forward swarm member task events - #4922

Merged
sailist merged 2 commits into
release/2026-11-13-2from
bug-368-11-13-swarm-panel-events
Nov 13, 2026
Merged

sailist merged 2 commits into
release/2026-11-13-2from
bug-368-11-13-swarm-panel-events

Conversation

@sailist

@sailist sailist commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Requirement or Bug

桌面端与 Web 端用户在开启 /swarm 执行 AgentSwarm 期间,对话中的 Swarm 面板一直显示“等待子任务加入…”,完成计数始终停留在 0 / N,直到整个 AgentSwarm 工具调用全部结束后才一次性从结果文本回填。本 PR 恢复 Swarm 成员生命周期事件到 /api/v1/ws 实时 task 帧的转发,使运行中的子代理列表与完成进度恢复实时展示。

Bug Reproduction Steps

  1. 在桌面端或 Web 端会话中执行 /swarm 开启 Swarm 模式。
  2. 发送需要并行启动多个子代理的任务,使主 agent 调用 AgentSwarm。
  3. 在子代理尚在运行、AgentSwarm 工具结果尚未返回期间观察 Swarm 卡片:成员区域始终为空并提示“等待子任务加入…”,卡片计数保持 0 / N 不随单个子代理完成而递增。

Root Cause

根本修复。

flowchart LR
    A[普通 Bash / 单个 Agent] --> B[ToolExecutor handler]
    B --> C[IAgentTaskService.onDidChangeTask]
    D[SwarmMemberTasks] --> E[IEventDispatcher: TaskStarted / TaskTerminatedNotice]
    C --> F[SessionTaskFeed]
    E -. "#4831 移除监听后断链" .-> F
    F --> G["/api/v1/ws: task 帧"]
Loading

因为 SwarmMemberTasks 不经过 ToolExecutor handler,只通过 IEventDispatcher 发布 durable TaskStarted 与 TaskTerminated(经 taskKey 折叠发出 TaskTerminatedNotice);而 #4831 为恢复普通前台任务的实时通知,将 SessionTaskFeed 改为仅订阅 IAgentTaskService.onDidChangeTask,漏掉了 Swarm 成员所在的 IEventBus 旁路,所以客户端 Task store 在运行期间收不到任何 Swarm 成员的实时 task 帧。

Code Changes

在 packages/kap-server/src/transport/ws/v1/sessionTaskFeed.ts 的 SessionTaskFeed.watch 中保留 IAgentTaskService.onDidChangeTask 订阅,同时恢复对 IEventBus 上 TaskStarted 与 TaskTerminatedNotice 的订阅,仅转发带 swarmIndex 的 Swarm 成员事件:

 watch(agentId)
-  tasks = handle.accessor.get(IAgentTaskService)
-  subscribe tasks.onDidChangeTask -> onChange(agentId, info)
+  tasks = optionalService(handle, IAgentTaskService)
+  bus = optionalService(handle, IEventBus)
+  if tasks !== undefined
+    subscribe tasks.onDidChangeTask -> onChange(agentId, info)
+  if bus !== undefined
+    subscribe TaskStarted, TaskTerminatedNotice ->
+      if event.agentId === agentId && info.kind === 'agent' && info.swarmIndex !== undefined
+        onChange(agentId, info)

这里按 info.kind === 'agent' && info.swarmIndex !== undefined 识别 Swarm 成员,而不是按 toolExecutor.handler(taskId) === undefined 判重,是因为 LoopService 处理普通后台任务的 tool.async_ended 时会先同步 release(taskId) 再异步派发 TaskTerminated;若按 handler === undefined 判断,普通后台任务在终态到来时已被移除,会导致终态 task 帧重复发送。运行中途重连或刷新时的 task.list 则继续由 IAgentTaskService.list(true) 从 taskKey 状态读取在跑的 Swarm 成员,无需额外拼装。

Behavior Changes and Affected Users

行为 改动前 改动后 依赖旧行为的人群 退路
AgentSwarm 成员运行与结束时的 /api/v1/ws task 帧 不推送实时 task 帧;运行期间客户端 Task store 无 Swarm 成员记录 每个带 swarmIndex 的成员在启动(TaskStarted)与结束(TaskTerminatedNotice)时向已订阅父 agent 的连接推送 running 与 ended task 帧 无(恢复 #4831 之前的既有服务端实时行为,载荷字段与 schema 不变) 无
普通前台/后台 Bash、单个 Agent、AskUserQuestion 的 task 帧 经 IAgentTaskService.onDidChangeTask 推送一次 不变;普通任务的 swarmIndex 为 undefined,不会命中 IEventBus 转发分支产生重复帧 desktop 与 web 用户(code-app 仓消费 /api/v1/ws 的 task / task.list) N/A
  • 受影响模块与客户端核对:修改 packages/kap-server/src/transport/ws/v1/sessionTaskFeed.ts。消费方位于 code-app 仓(desktop 与 web 客户端通过 /api/v1/ws 的 task / task.list 帧更新 tasksStore 并驱动 SwarmTool 面板),本次未改动任何公开 schema 或字段名(apiSurface.snapshot.test.ts 通过),新旧客户端均可直接解析。
  • 测试覆盖:packages/kap-server/test/sessionEventBroadcaster.test.ts 的 agent frames 用例覆盖普通前台任务 live 推送、普通后台任务 durable 事件不重复推送、跨 agent 不串台以及 Swarm 成员 running / ended 帧。
  • 验证结论:已完成修改前红灯复现、修改后单元与契约回归、全仓构建,以及真实 kap-server + /api/v1/ws + 客户端 Swarm 面板数据投影的三路并行子代理端到端验证(含运行中实时加入、中途新连接 task.list 恢复与逐个完成计数递增);未单独启动桌面 Electron 窗口做手工像素截图。
验证明细
  • packages/kap-server/test/sessionEventBroadcaster.test.ts:54/54 通过
  • packages/kap-server/test/wsConnectionV1.test.ts、packages/kap-server/test/conversationContract.e2e.test.ts、packages/agent-core-v2/test/agent/task/rpc-events.test.ts、packages/agent-core-v2/test/features/swarm/sessionSwarm.test.ts:209/209 通过
  • packages/kap-server/test/apiSurface.snapshot.test.ts:1/1 通过
  • @moonshot-ai/kap-server typecheck、scoped oxlint --type-aware 与根仓库 corepack pnpm run build:全部通过

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, 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 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 75104eb

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

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code 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 13, 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-13T11:09:28.169075Z 75104eb 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 13, 2026

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

commit: 75104eb

@sailist

sailist commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

Desktop 实景验证

使用 aimock 脚本驱动真实 Desktop + Core + WebSocket,覆盖前台 Bash、前台 Agent 与三路 AgentSwarm;运行、结束及模型请求结构检查全部通过。

  • Bash 与 Agent 的运行中卡片正常显示。
  • AgentSwarm 运行中三名成员即时出现,不再停留在“等待子任务加入…”。
  • 成员结束后,完成计数从 0 / 3 更新到 3 / 3。

Bash 运行中

Bash 运行中

Agent 运行中

Agent 运行中

三路 AgentSwarm 运行中:0 / 3,三名成员全部可见

AgentSwarm 运行中

全部完成:3 / 3

AgentSwarm 全部完成

@sailist
sailist merged commit a27bd82 into release/2026-11-13-2 Nov 13, 2026
15 checks passed
@sailist
sailist deleted the bug-368-11-13-swarm-panel-events branch November 13, 2026 11:32
@github-actions github-actions Bot mentioned this pull request Nov 13, 2026
wbxl2000 pushed a commit that referenced this pull request Nov 16, 2026
* fix(agent-core-v2): avoid quadratic cloning during session replay (#4885)

* fix(agent-core-v2): avoid quadratic cloning during session replay

* fix(agent-core-v2): isolate replay assignments and preserve rollback order

* refactor(agent-core-v2): make participant mutable replay explicit opt-in

---------

Co-authored-by: kimi-agent-bot <[email protected]>

* fix(agent-core-v2): validate Kimi media upload status (#4905)

* fix(agent-core-v2): reject failed Kimi image extraction

* chore: add Kimi image extraction changeset

* fix(agent-core-v2): validate Kimi media upload status

* fix(kap-server): forward swarm member task events (#4922)

* test(agent-core-v2): make zero-timeout task wait deterministic (#4925)

Co-authored-by: kimi-agent-bot <[email protected]>

* feat(registry): support search_url and feedback_url on custom registry providers (#4875)

* feat(registry): support search_url and feedback_url on custom registry providers

* fix(sdk): honor provider.env credentials and custom_headers for feedback_url

* refactor(core): submit feedback through the engine

Web and desktop reach this machine through kap-server, so feedback submission resolves the active model's provider in the engine and is exposed on the same route and klient method. The SDK facade keeps the managed OAuth path and no longer re-derives provider credentials.

* refactor(sdk): drop the unused feedback facade

Product clients submit feedback through the engine. The SDK facade and oauth toolkit no longer keep a second managed-only submission path.

* refactor(feedback): share engine schemas across route and klient

The HTTP body uses the engine field names, so the route forwards it and the klient contract imports the same schemas.

* refactor(feedback): drop unused kap and klient surfaces

TUI reaches IFeedbackService through the harness. The HTTP routes, klient facade, and the schemas that only served them have no caller.

* refactor(feedback): package and upload feedback attachments in the engine

* refactor(feedback): keep FeedbackService internal to agent-core-v2

* test(cli): provide execFile in node:child_process mocks

The feedback codebase scanner added in this branch calls
promisify(execFile) at module scope and is now reachable from the
agent-core-v2 index, so partial child_process mocks in these suites
failed module evaluation.

* feat(feedback): disclose submitted files, attach request_id, and drop text-only option (#4876)

* feat(registry): support search_url and feedback_url on custom registry providers

* fix(sdk): honor provider.env credentials and custom_headers for feedback_url

* refactor(core): submit feedback through the engine

Web and desktop reach this machine through kap-server, so feedback submission resolves the active model's provider in the engine and is exposed on the same route and klient method. The SDK facade keeps the managed OAuth path and no longer re-derives provider credentials.

* refactor(sdk): drop the unused feedback facade

Product clients submit feedback through the engine. The SDK facade and oauth toolkit no longer keep a second managed-only submission path.

* refactor(feedback): share engine schemas across route and klient

The HTTP body uses the engine field names, so the route forwards it and the klient contract imports the same schemas.

* refactor(feedback): drop unused kap and klient surfaces

TUI reaches IFeedbackService through the harness. The HTTP routes, klient facade, and the schemas that only served them have no caller.

* refactor(feedback): package and upload feedback attachments in the engine

* refactor(feedback): keep FeedbackService internal to agent-core-v2

* test(cli): provide execFile in node:child_process mocks

The feedback codebase scanner added in this branch calls
promisify(execFile) at module scope and is now reachable from the
agent-core-v2 index, so partial child_process mocks in these suites
failed module evaluation.

* feat(feedback): disclose submitted files, attach request_id, and drop text-only option

* fix(session-export): keep SESSION_NOT_FOUND for missing non-empty session ids

The global-log-only fallback exists for feedback submissions without an
active session (empty id). Scoping it to empty ids restores the 40401
envelope that kap-server's export route promises for unknown sessions.

* test(agent-core-v2): pin retained request id semantics in llmRequester assertions

* chore: sync web dist from code-app

code-app: 11563df26b92f0035659083327976a2e9b8e6727

code-app-release: b0b6d9c7a45c96ca48039ff05789c6e90f76ef20

code-app-local-build: 005d2b437493b7ab04d01f70542f3b6356699f4d

* ci: release packages (#4918)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix(agent-core-v2): align request id back-sync with main

---------

Co-authored-by: kimi-agent-bot <[email protected]>
Co-authored-by: Haozhe <[email protected]>
Co-authored-by: 7Sageer <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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