Skip to content

refactor(kap-server): merge WebSocket session subscriptions into subscribe_v2 - #4470

Merged
wbxl2000 merged 4 commits into
mainfrom
qer/merge-subscriptions
Oct 28, 2026
Merged

wbxl2000 merged 4 commits into
mainfrom
qer/merge-subscriptions

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

把 WebSocket 的两套会话订阅合并成一套:删除原始 subscribe 路径及其 seq / 回放机制,subscribe_v2 成为唯一的会话订阅。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

服务端以前同时维护两套会话订阅:

  • 原始 subscribe:推送原始 agent 事件,带 seq / epoch 事件日志、游标回放、resync_required、agent_filter,以及"被转录流覆盖的事件要抑制"的表。
  • subscribe_v2:推送转录流。

客户端已经全部改用转录流和 work_changed(code-app 侧见 kimi-code-app#1002),原始路径只剩维护成本,这次整条删掉。

文件树:

 packages/kap-server/src/
 ├── protocol/
-│   ├── rest-snapshot.ts
 │   ├── events-zod.ts           # only global push events remain
 │   ├── ws-control.ts           # client ops: client_hello / subscribe_v2 / unsubscribe_v2 / abort / pong
 │   └── session.ts              # drop last_seq
 ├── routes/
-│   ├── snapshot.ts
 │   ├── registerApiV1Routes.ts
 │   └── sessions.ts             # drop last_seq / event cursor
 └── transport/ws/v1/
-    ├── sessionEventJournal.ts
-    ├── inFlightTurnTracker.ts
-    ├── subagentRosterTracker.ts
     ├── events.ts               # only global event types
     ├── sessionEventBroadcaster.ts
     ├── wsConnectionV1.ts
     ├── protocol.ts             # server_hello without max_event_buffer_size
     └── registerWsV1.ts

广播器在每个连接上的状态:

 SessionEventBroadcaster
   globalTargets: every connection
-  per-session state for every subscribed session
-    raw event fan-out + agent filter + suppression table
-    journal (seq / epoch) + replay buffer
-    work tracking (subscribed sessions only)
+  workTrackers: every live session (list() at startup + onDidCreateSession)
+    -> event.session.work_changed to every connection
+  per-session state only for subscribe_v2 sessions
+    transcript stream (grades, transcript_since)
+    interactions

连接上的订阅表从 { agentFilter, transcriptGrades } 收窄为每个会话一个 TranscriptGradeSpec。unsubscribe_v2 按 agent 退订后如果一个 agent 都不剩,就整个取消该会话的订阅。

  • 兼容细节:
    • 非会话的全局事件保留 session_id: '__global__' 的外层,payload 里仍带 { agentId: 'main', sessionId: '__global__' }。
    • event.session.archived / deleted 的外层 session_id 仍是 __global__,与改动前一致。
    • client_hello 的 schema 仍是非严格的 zod,旧客户端多带的字段会被忽略。
  • 旧事件日志目录:~/.kimi-code/server/events/ 不再读写,也不删,启动和删除会话时都不碰。
  • klient e2e:
    • harness 改用 subscribe_v2;新增 runUntilIdle,靠 work_changed 先忙后闲来判断 prompt 和 compaction 完成。
    • 删除 refresh-replay。
    • 删除 send-and-cancel 和 prompt-queue-steer:它们依赖的 /debug/prompts/* 已在 feat(server): default to kap-server and remove the v1 server package #1617 删除,一直处于跳过状态,而且只靠原始事件判断。
  • 文档:中英文 server-api.md 同步更新。
  • web dist:apps/kimi-code/dist-web 用上述 code-app 构建替换,只更新产物,不附 web 汇总 changeset。

Behavior Changes and Affected Users

行为 改动前 改动后 谁依赖旧行为 退路
WS subscribe / unsubscribe 帧 订阅原始会话事件,返回 ack 忽略,不回 ack 用原始订阅的外部脚本;#1002 之前构建的 web dist(本 PR 已替换) 无;改用 subscribe_v2
原始 agent 事件(turn.*、assistant.delta、tool.*、prompt.*、compaction.*、error / warning 等) 推给订阅了会话的连接 不再推送 外部脚本;旧版 web / desktop 客户端 无;改用 transcript.* 与 event.session.work_changed
断线重连 带 cursors 回放缺口,否则收到 resync_required 重新发 subscribe_v2,可带 transcript_since 续传转录,不带则收到新的 transcript.reset 依赖游标回放的客户端 无
GET /sessions/{id}/snapshot 返回重建快照 路由已删除 实现了 resync 恢复的客户端 无;改用 GET .../transcript 与 GET /sessions/{id}
session 对象 last_seq 始终为 0 字段删除 读取该字段的客户端(值一直是 0) 无
server_hello.max_event_buffer_size 1000 字段删除 读取该字段的客户端 无
client_hello 可带 subscriptions / cursors 直接订阅,确认后补发待处理交互 只做鉴权与 kimi-inspect 识别,其余字段忽略,不补发交互 靠 hello 订阅的旧客户端 无;用 subscribe_v2,确认后补发交互
event.session.work_changed 只推给订阅了该会话的连接 每个已加载会话都推给所有连接 无(范围只增不减) —
session.created / session.meta.updated 会话未加载时可能被丢弃 总是推送 无 —
删除会话时的旧事件日志文件 一并删除 events/<id>.jsonl 不再触碰该目录 无(文件不再被读取) —
subscribe_v2 / unsubscribe_v2、interaction 帧、转录协议 — 不变 — —

受影响的客户端:

  • desktop 和 web:kimi-code-app#1002 已删除客户端的原始订阅路径,新旧服务端都能用。desktop 内嵌的服务端随包发布,submodule bump 时一起更新。
  • web UI:由本仓的 apps/kimi-code/dist-web 提供。本 PR 一并替换了 dist:从 code-app 7ac243c1a(code-app main 加上 Need multi repo support real bad #1002,即 Need multi repo support real bad #1002 rebase 后的 HEAD)构建,注入的 CLI 版本对应本分支。新 bundle 只用 subscribe_v2,不含原始 subscribe。
  • kimi-inspect:只用 client_hello 和全局事件,不需要改。
  • TUI / print / ACP / SDK:不走 WebSocket,不受影响。

测试覆盖:

  • test/sessionEventBroadcaster.test.ts:
    • App 级 work_changed(未订阅的会话、推给所有连接、启动时已存在的会话、关闭后重建、归档、last_turn_reason、后台任务、subagent);
    • 交互与转录流的订阅、按 agent 退订、取消订阅;
    • 全局事件与 DI 事件分发;
    • schema 拒绝原始 turn.started。
  • test/wsConnectionV1.test.ts:client_hello 的 ack 与忽略旧字段、忽略旧 subscribe / unsubscribe、subscribe_v2 / unsubscribe_v2 的 ack 与状态、关闭连接时全部退订、server_hello 不带缓冲大小。
  • test/sessions.test.ts:会话对象没有 last_seq。
  • test/connections.test.ts:订阅关系的展示。
  • test/workspaceLayout.test.ts:改用会话读取接口。
  • test/__snapshots__/apiSurface.snapshot.test.ts.snap:路由表少了 /snapshot。
  • klient live e2e:在本地服务端上用真实模型跑过,握手、订阅、提交 prompt 到空闲、compact、undo、session-resume 都通过。fork 用例的失败与本改动无关(fork 路由没动)。
  • web UI 冒烟:本分支服务端 + 新 dist,在浏览器里新建会话并发消息,回复实时推到页面,忙碌状态先出现后消失,没有错误提示和 console 错误;整页刷新后会话和消息都还在,刷新后再发一条也能正常推送。

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.

…cribe_v2

Drop the raw `subscribe` / `unsubscribe` path together with its seq/epoch
journal, cursor replay, `resync_required`, agent filters and legacy-event
suppression. `subscribe_v2` is now the only session subscription and carries
transcript streams plus interactions; reconnecting clients resubscribe and
receive a fresh `transcript.reset`.

`event.session.work_changed` becomes an App-level push to every connection,
tracked for every live session instead of only subscribed ones.

Remove `GET /sessions/{id}/snapshot`, the session `last_seq` field and
`server_hello.max_event_buffer_size`. The legacy journal directory on disk is
left untouched.

klient e2e: the harness subscribes with `subscribe_v2` and waits on
`work_changed`; drop the replay suite and the suites that depended on the
removed debug prompt endpoints.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-27T11:45:27.051243Z d64b440 Manual request
ℹ️ 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.

@changeset-bot

changeset-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d64b440

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

@pkg-pr-new

pkg-pr-new Bot commented Oct 27, 2026 •

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

commit: d64b440

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

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

code-app: 7ac243c1a817e0cf329c6b0dc25016006d5f5286
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

Changes since the last review:

  • Synced the bundled web UI (apps/kimi-code/dist-web) from code-app 7ac243c1a, which is code-app main plus MoonshotAI/kimi-code-app#1002. The bundle now subscribes only through subscribe_v2 and no longer depends on the removed legacy subscribe path. This resolves the previous dist finding.
  • Added the summary web changeset .changeset/web-interaction-polish.md.
  • Rebased onto the latest main; no source changes beyond the dist sync.

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

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

Comment thread .changeset/web-interaction-polish.md Resolved
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

Changes since the last review:

  • Removed the web dist summary changeset .changeset/web-interaction-polish.md (d64b440). The web UI side of this PR now ships only the rebuilt apps/kimi-code/dist-web assets; the only remaining changeset is .changeset/merge-ws-session-subscriptions.md.
  • The version level of merge-ws-session-subscriptions.md stays patch, as confirmed by the maintainer (see the reply on that thread). No code changes otherwise.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: d64b440d74

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

@wbxl2000
wbxl2000 merged commit 8535bd7 into main Oct 28, 2026
17 checks passed
@wbxl2000
wbxl2000 deleted the qer/merge-subscriptions branch October 28, 2026 04:17
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 5, 2026
…cribe_v2

Port of upstream MoonshotAI/kimi-code 8535bd7 (MoonshotAI#4470).

The raw `subscribe` / `unsubscribe` path is gone together with its
seq/epoch journal, cursor replay, `resync_required`, agent filters and
legacy-event suppression. `subscribe_v2` is now the only session
subscription and carries transcript streams plus interactions; a
reconnecting client resubscribes and receives a fresh `transcript.reset`
instead of replaying from a cursor. Global push events such as
`event.session.work_changed` reach every open connection without a
session subscription first.

Removed with it: `sessionEventJournal`, `inFlightTurnTracker`,
`subagentRosterTracker`, `GET /sessions/{id}/snapshot` and its
`rest-snapshot` protocol, the `seq`/`epoch`/`offset` envelope fields,
`server_hello.max_event_buffer_size`, the session `last_seq` field, and
the `at_seq` field of the abort ack.

Local shape notes: this tree's `sessionEventBroadcaster` also serves the
agent/usage/goal/todo frames, the task frames and the interaction frames
added after upstream wrote this commit, so its own feeds stay and only
the journal-backed event pipeline is dismantled. The broadcaster keeps a
local `EventEnvelope` without the sequence fields, and `agentEventSchema`
stays as the internal stream union for the turn/roster bookkeeping the
broadcaster no longer does but the v1 event types still describe.

Verified: kap-server 1305 pass / 9 skip / 0 fail across 64 files,
transcript 154 pass, klient and apps/kimi-code typechecks clean,
oxlint --type-aware 0 errors, check:no-comments / check:deep-imports /
check:no-hardcoded-prose green. The apiSurface snapshot is regenerated.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 5, 2026
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