Repository navigation
feat(kap-server): push agent, usage, goal and todo state over WebSocket - #4606
Conversation
Subscribers of an agent now receive four flat frames that carry the agent's own state, read straight from the engine services: - agent: run status, model, thinking effort, permission mode, modes - usage: context tokens, coalesced to a few frames per second - goal: the main agent's goal, or null - todo: the agent's todo store, so it follows undo and cold restores Each frame is sent in full when a connection starts receiving the agent and again whenever its value changes. Run status follows turn events and is flushed in a microtask, so it always lands after the transcript batch of the same turn change. The transcript drops the state these frames replace: the meta.merge and todo.upsert ops, the meta and todo models and their cold folding, the tool frame todoId, the phase tracker behind meta.agent.phase, and the meta and todos fields of the REST transcript.
code-app: a0d00377f772d2cf53f8193a06e5dcacfae0e4a2
🦋 Changeset detectedLatest commit: 446ed24 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 SummaryThis 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: aa4128b632
ℹ️ 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".
| @@ -0,0 +1,5 @@ | |||
| --- | |||
| "@moonshot-ai/kimi-code": patch | |||
There was a problem hiding this comment.
Preserve transcript fields across the patch update
For existing desktop, web, and VS Code clients, this patch removes the previously documented meta and todos REST fields and their transcript operations, while the replacement WebSocket frames require a coordinated client upgrade. A patch auto-update can therefore silently leave prior-release clients without agent, goal, or todo state. Preserve the deprecated fields during a transition, or treat this as a breaking release rather than a patch.
AGENTS.md reference: AGENTS.md:L62-L62
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
转录 meta / todos 的读取方只有 desktop 和 web:desktop 内嵌的 server 随安装包一起发布,web 产物在本 PR 里一起更新;VS Code 插件、TUI、SDK、ACP 都不读这两个字段(SDK 的 getTodos 直接读引擎的待办服务)。"旧客户端连新 server"不是受支持的组合,所以不保留过渡字段。版本级别沿用第四组删除 REST 任务接口时的 patch,删除内容写在 changeset 和 PR 描述的行为变化表里。
| objective: goal.objective, | ||
| status: goal.status, | ||
| completion_criterion: text(goal.completionCriterion), | ||
| turns_used: goal.turnsUsed, | ||
| tokens_used: goal.tokensUsed, |
There was a problem hiding this comment.
Preserve the complete goal snapshot in goal frames
When a goal has token, turn, or wall-clock budgets, IAgentGoalService.getGoal() also returns goalId, the complete budget report, and terminalReason, but this projection discards all of them. The committed web client applies each goal frame by replacing its previously REST-loaded goal object, so subscribing immediately erases those fields and subsequent budget progress cannot reach the client. Reuse the existing full goal snapshot contract or include these fields in the frame.
AGENTS.md reference: AGENTS.md:L126-L128
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
有意为之:goal 帧只带界面用到的字段(目标内容、状态、完成标准、已用轮数 / token / 时长)。desktop / web 不读预算、goalId 和 terminalReason,目标工具卡片上的预算来自工具调用参数。需要预算的调用方仍可读 GET /sessions/{id}/goal。
| bus.subscribe((event) => { | ||
| this.onEvent(event); | ||
| }), |
There was a problem hiding this comment.
Filter delta events before recomputing every state slice
While a client watches an active session, this unfiltered subscription schedules a full agent/goal/todo/usage read for every AssistantDelta, ThinkingDelta, and ToolCallDelta emitted during streaming, even though none of those events changes these state slices. In particular, the usage read calls statusSize(), which can repeatedly estimate the conversation context (notably with the supported estimated strategy), so long contexts and high-frequency streams incur avoidable CPU work despite the 250 ms throttle applying only after recomputation. Subscribe only to state-changing event types or throttle the reads themselves.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
已修复(240dcaa46):feed 不再订阅整条事件总线,只订阅 turn.started、turn.ended、agent.status.updated、goal.updated 和待办服务的变化事件,和旧的 meta 投影听的是同一组;流式增量不再触发重读,sessionEventBroadcaster.test.ts 里加了断言。
The agent frame carries plan_mode, swarm_mode and tower_mode booleans instead of a modes object of empty objects, matching the profile write fields. The goal frame omits goal when the session has none, following the protocol rule that optional fields are omitted rather than null.
code-app: 206b70b98f7ce77bccdbeef37b15cd97a0933fd0
…main agent on subscribe The agent feed listened to the whole agent event bus and re-read every state slice on each streaming delta. It now listens only to turn.started, turn.ended, agent.status.updated and goal.updated, plus the todo service's change event, which are the events that change those slices. Subscribing to a session now creates its main agent when it does not exist yet, so a fresh session sends its main agent frames right after the subscription. GET /status used to create it as a side effect, and clients no longer need that read for subscribed sessions.
code-app: cb0dbffca84f67e0cf72cc00d4f05eb71ad3c079
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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: ef7c84980d26c156295c2d56bf001b34a84a9c4e
Desktop and web read the main agent's state from the agent and usage frames and no longer call this endpoint, and subscribing now creates the main agent that the status read used to create as a side effect. - The status route, its response schema and ISessionLegacyService.status are gone; the :undo response keeps only messages. - The profile and tower tests check the main agent state through the same reader the agent frame uses, and the default-model context window test goes through the usage reader. - A broadcaster test pins the usage frame's context limit rules.
|
@codex review |
💡 Codex Reviewhttps://github.com/MoonshotAI/kimi-code/blob/3376ff0dcffee9454f9215f95dd6da1ae620da6a/apps/kimi-code/dist-web/assets/index-Boxa5rQt.js#L191 When a session switches from a model with a known context limit to one whose capabilities do not provide a positive limit, the new AGENTS.md reference: AGENTS.md:L18-L18 ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
已修复:code-app 源码改为 |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
First half of upstream MoonshotAI#4606. A connection that starts receiving an agent now gets four more flat frames next to `task.list`: `agent` (status, model, thinking level, permission/plan/swarm/tower modes), `usage` (context tokens, throttled to one frame per 250 ms), `goal` (the main agent's goal, or a frame with no goal) and `todo` (the item list). Each frame is re-sent on change, so a client no longer has to poll `GET /sessions/{id}/status` for any of it. `SessionAgentFeed` watches every agent the session exposes, subscribes to its event bus for `turn.started`/`turn.ended`/`agent.status.updated`/ `goal.updated`, to the todo service, and to `onDidCreate`/`onDidClose`; `agentState.ts` reads the state through the scoped services. Local shape notes: - The fork's goal status has two extra values, `budget_limited` and `usage_limited`, so `goalStateSchema` accepts them. - `subscribe` now calls `ensureMainAgent` after resuming, matching upstream: without a main agent the feed has nothing to watch and no frames are produced. A failure is logged, not fatal. - `sendTaskLists`/`sendTask` became `sendAgentLists`/`sendAgentFrame` so the task and agent frames share one subscription-bookkeeping path; the broadcaster test's collecting target buckets the four new frames with the task ones, and six fake profile services gained the `getEffectiveThinkingLevel` method the state reader calls. Not yet done in this row: removing `GET /sessions/{id}/status`, the transcript's `meta`/`todo` entities, and the docs/changesets. Gates: kap-server 1386 pass / 9 skip, both packages `tsc` clean, `check:no-comments`, `check:no-hardcoded-prose`, `check:strict-request-bodies` green, structural review unchanged at the three pre-existing findings.
The four agent/usage/goal/todo frames are served and covered end to end; removing the REST status endpoint and the transcript meta/todo entities is what remains.
Completes MoonshotAI#4606: the agent, usage, goal and todo frames pushed over the server WebSocket replace the polling surface, so the REST side goes away. `GET /sessions/{id}/status` and its schema are removed, the `:undo` response no longer carries `status`, and `legacyActivity` — which existed only to project the polled view — is deleted. The transcript contract loses its `meta` and `todo` entities: `TranscriptMeta`, `TranscriptTodo`, the operation kinds that maintained them, the `goalMeta`/`modesMeta`/ `agentPhaseMeta` schemas and the `toolCallFrame.todoId` link all go, with the fold and store following. Clients read the same state from the `agent`, `usage`, `goal` and `todo` frames instead. The todo list is now derived from the tool frames that produce it, so an undo reverts it with the turn that wrote it rather than leaving a stale copy behind. Verified: kap-server 315 pass across services/transcript, sessionEvent- Broadcaster and sessions; transcript 154 pass; agent-core-v2, kap-server, transcript and apps/kimi-code typechecks clean; oxlint 0 errors; check:no-comments, check:deep-imports and check:no-hardcoded-prose green. Not from this change: the `S2` transcript-contract e2e case fails its afterEach hook with a 10s timeout on a clean HEAD as well, so it is a pre-existing flake, not a regression here.
Requirement or Bug
Agent 的运行状态、设置(模型、思考强度、权限、计划 / swarm / tower 模式)、上下文用量、目标和待办,之前混在转录里用
meta.merge/todo.upsert推送;desktop / web 还要另外拉GET /status补齐,两路来源互相覆盖,客户端为此维护了一套防竞态逻辑。本 PR 把它们改成四种独立的 WebSocket 帧,状态直接取自引擎服务,转录只保留对话本身。顺带修复:撤销一条消息后,待办列表仍停在撤销前的内容。
客户端配套:MoonshotAI/kimi-code-app#1094。
Bug Reproduction Steps
N/A(主体是协议改动)。顺带修复的问题:让 agent 用 TodoList 写几条待办 → 撤销这条消息 → 待办栏仍显示撤销前的列表,刷新也不变。
Root Cause
旧路径从 wire 记录里取最后一次
tools.update_store作为待办,撤销不会回滚这条记录。引擎的待办服务是可撤销的持久状态,撤销时会回到上一个值;改为从它读取后,这个问题从根本上消失,不需要单独处理。Code Changes
核心思路
agent(运行状态 + 设置,变化少)、usage(上下文 token,每一步都变,尾部节流 250 ms)、goal(只有主 agent 有)、todo。分开推送,一种状态变化不会让另外几种跟着重发。subscribe_v2里该 agent 的粒度从off或未订阅变为非off),先发它的四种帧,再按变化推送。这和第四组的task.list用的是同一份"每个连接已经收到过哪些 agent"的记录。status: "idle"时,这一轮的 turn 结束 op 已经到了。turn.started、turn.ended、agent.status.updated、goal.updated四类事件和待办服务的变化事件,和旧的meta投影听的是同一组;流式增量(assistant.delta等)不会触发重读。GET /status在读取时顺带创建它,客户端选中会话时总会读一次。现在订阅时创建,主 agent 的四种帧总能紧跟订阅到达。GET /sessions/{id}/status。 desktop / web 已改为从帧读取,仓内其他客户端(VS Code、SDK、TUI、kimi-inspect)都不调用它;:undo响应里同一份status也一并去掉,只保留messages。源码(不含测试和文档)+513 / −1033:kap-server +508 / −564(其中新增帧 feed 248 行、状态读取 84 行、帧 schema 76 行;删除
legacyStatus的活动跟踪 146 行和转录里的状态投影约 170 行),transcript +5 / −469(meta、todos的模型、schema、op 和折叠逻辑整体删除)。协议
agent:每个 agent 都有。status跟随turn.started/turn.ended事件,初值取 loop 快照。不直接读 loop 快照,是因为 loop 在派发TurnEnded之前就清掉了当前 turn,直接读会让 idle 先于转录里的 turn 结束到达。usage:每个 agent 都有,没有上下文数据时不发。连续变化在 250 ms 窗口里合并成一帧。agent的三个模式字段是布尔值,和 profile 接口写入用的plan_mode/swarm_mode同名;不再带计划文件路径、swarm 触发方式这类细节,客户端没有读取方。goal:只有主 agent。没有目标时省略goal字段,不发null,与 KAP 草案"可选字段没有值时省略"的约定一致。去重时忽略wall_clock_ms(它是实时计算的),所以目标没变时不会每步重发。todo:每个 agent 都有,整份列表替换。off的连接。会话重新变为 live 时重新挂上监听,和任务帧一样。结构
旧路径的功能清单与新位置
LegacyActivityTracker跟踪activity和phaseagent.status跟随 turn 事件;phase不再提供(见行为表)activity取 loop 快照modes.towerisActiveagent.status.updated→ 模型、思考强度、权限、上下文 tokenreadLegacyStatus(与GET /status同源)goal.updated→meta.goalisActive;marker 仍留在转录里tools.update_store记录get(),监听onDidChange提交
feat(kap-server):四种帧、删除转录里的meta/todos、更新 API 文档。chore:三条 changeset(四种帧并删除转录里的meta/todos;撤销后待办回退;删除GET /sessions/{id}/status和:undo响应里的status),均为 patch,第三条在第 7 个提交里。refactor(kap-server):agent帧的模式改为三个布尔字段;没有目标时goal帧省略goal。fix(kap-server):feed 改为只订阅上述四类状态事件;订阅时确保主 agent 存在。chore: sync web dist from code-app(多次,以最后一次为准):apps/kimi-code/dist-web当前由 MoonshotAI/kimi-code-app#1094 在43cc54ef9构建(基于 code-app main 的e75e3752f,所以产物同时带着 main 的 web 改动),构建时 submodule 对齐本 PR 的2d7e9ce39,产物注入的 CLI 版本是 2.4.1。refactor(kap-server):删除GET /sessions/{id}/status、它的响应 schema 和ISessionLegacyService.status,:undo响应去掉status;原来借/status检查状态的测试改用 agent 帧的读取函数,另加一条 usage 帧上下文上限的测试;更新 API 文档和apiSurface快照。Behavior Changes and Affected Users
meta/todos,以及meta.merge/todo.upsertop(WebSockettranscript.ops和快照)agent/usage/goal/todo帧。客户端随配套 PR 一起改;desktop 内嵌 server 随包发布。changeset 写明了删除GET …/transcript等)里的meta、todos字段GET /sessions/{id}/status、GET /sessions/{id}/goal不变meta里没有新去处的字段:phase(running / tool_call / retrying / awaiting_approval / interrupted / ended)、activity的disposing/unknown、agent.usage(按模型、本轮、累计用量)、modes.plan.reviewPath/version、modes.swarm.trigger、goal.budgetUsed/budgetLimitusage;每步用量在 step 上;目标预算在GET /sessions/{id}/goalmeta.mergemeta里GET /sessions/{id}/statusagent/usage帧。changeset 写明了删除:undo响应{ messages, status }{ messages }status的外部脚本GET /status、GET /goal读取时才创建/status,效果相同apps/kimi-code/dist-web)meta/todoskimi web用户TUI、print 模式、SDK(
getTodos直接读引擎待办服务)、ACP 和 VS Code 插件都不读转录的meta/todos,不受影响。契约文件中docs/*-manifest和apiSurface快照没有变化。受影响的模块与测试:
sessionEventBroadcaster.test.ts:订阅时下发快照;没有任何 agent 时订阅会创建主 agent;流式增量不触发状态重读;agent 帧跟随 turn 事件且排在transcript.ops之后;待办、目标、权限变化时推送,其他时候不发;用量合并;后加入的 agent 发出初始帧。transcriptContract.e2e.test.ts:S1 断言 idle 帧排在 turn 结束的 ops 帧之后,转录里没有meta.merge;S3–S6 改为断言帧;新增 S10(新会话订阅后立即收到主 agent 的帧,TodoList 写入产生todo帧)。transcript.test.ts:agent 状态更新不再产生转录 op。transcriptlayers.test.ts、store.test.ts去掉meta/todos的用例。docs/{en,zh}/reference/server-api.md去掉 REST 转录的meta/todos,补上四种帧和重连说明。pnpm typecheck、pnpm lint(0 error)通过。sessions.test.ts:GET /status返回 404;profile 写入的模式和权限、Tower 不可用时的拒绝、未绑定模型时的默认上下文窗口,改用 agent / usage 帧的读取函数检查。agent-core-v2 删除只测status()的sessionLegacy.test.ts;klient 的撤销用例去掉status。apiSurface快照只少了这一条路由。connections.test.ts:订阅后改为轮询等订阅生效,不再依赖固定的 50 毫秒(订阅多了一步创建主 agent)。/status;运行中 → 空闲、待办栏、撤销后待办清空、经 REST profile 改权限 / 计划模式 / 目标后页面无需刷新即更新、刷新页面后从快照恢复、重启 server 后冷会话恢复待办和设置,均符合预期。没有验证 tower 模式(需要实验开关)。Checklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.