Repository navigation
refactor(kap-server): compact session state, global events, and snake_case contracts - #4963
Conversation
|
Codex Review Summary: ✅ CompletedThis 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: bc6c8255c1
ℹ️ 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".
5f2bc0a to
747c49b
Compare
bc6c825 to
573b6f5
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ff5e83a679
ℹ️ 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".
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! 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". |
747c49b to
b282d7a
Compare
e291814 to
52e6f0b
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 52e6f0bcbd
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f1521528e7
ℹ️ 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".
16d898c to
199cb75
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 199cb75234
ℹ️ 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".
…ity transitions, and type profile response
…cle session events
199cb75 to
d094752
Compare
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d094752c35
ℹ️ 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".
…rializing main agent on profile metadata updates
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b79653fcae
ℹ️ 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".
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
Requirement or Bug
收敛 KAP 会话与事件的核心数据形态,消除历史空占位字段、嵌套全局信封和混合命名风格,为后续清理旧路由与发布协议文档打底。本 PR 落地精简版
Session、把上下文窗口水位帧从usage正名为context、将 12 种旧event.*全局帧收敛为平铺帧、GET /api/v1/meta补充协议版本号并移除恒定为v2的backend字段,同时将/mcp/*、/plugins/*、/capabilities/*的公开载荷统一为snake_case。Bug Reproduction Steps
N/A
Root Cause
N/A
Code Changes
核心思路
服务端对会话与全局状态只输出一套无历史包袱的平铺投影:客户端收到
session、workspace、config、invalidated等全局帧或context状态帧时直接整包替换或按需重拉,不再合并残缺增量,也不再区分两套字段命名约定。因为上下文窗口当前占用(可涨可跌的水位)与累计 Token 计费账单语义截然不同,所以将原 WebSocket
usage帧改名为context;同时在路由边界增加显式 Wire 映射函数,把引擎内部的驼峰对象转换为snake_case,既不侵入磁盘mcp.json/installed.json存储格式,又保证对外协议命名一致。协议事件与字段
+/-清单1.
Session对象与会话接口(GET /sessions、GET /sessions/{id}、POST /sessions、POST /sessions/{id}/profile){ id: string workspace_id: string + cwd: string // 从 metadata.cwd 提升到顶层必填 title: string created_at: string updated_at: string - busy: boolean - main_turn_active?: boolean + activity: 'idle' | 'turn' | 'background' // 合一 busy + main_turn_active pending_interaction?: 'none' | 'approval' | 'question' - last_turn_reason?: 'completed' | 'cancelled' | 'failed' + last_turn_outcome?: 'completed' | 'interrupted' | 'failed' // 对齐 Turn.outcome archived?: boolean archived_at?: string - current_prompt_id?: string last_prompt?: string - metadata: { cwd: string, ... } + metadata?: Record<string, unknown> // 仅保留自定义键值,空时省略 - agent_config: SessionAgentConfig // 移至 WS `agent` 帧 & `/profile` 响应 - usage: SessionUsage // 单轮在 `Turn.usage`,水位在 WS `context` 帧 - permission_rules: PermissionRule[] - message_count: number git?: SessionGit } // POST /api/v1/sessions 请求体 + cwd?: string // 新增顶层 cwd(metadata.cwd 仍兼容) // POST /api/v1/sessions/{session_id}/profile 响应 data - Session + { session: Session, agent: AgentState } // 同时支持 goal_control: 'edit' 原地改目标2. WebSocket Agent 状态帧(
/api/v1/ws订阅推送)// agent 帧:新增已终结状态 'ended' { type: 'agent', event_created_at: string, session_id: string, agent_id: string, - status: 'idle' | 'running', + status: 'idle' | 'running' | 'ended', compacting: boolean, model?: string, thinking?: string, permission_mode?: string, plan_mode: boolean, swarm_mode: boolean, tower_mode: boolean, } // usage 帧重命名为 context 帧(字段不变,250ms 节流) { - type: 'usage', + type: 'context', event_created_at: string, session_id: string, agent_id: string, context_tokens: number, max_context_tokens?: number, }3. WebSocket 全局事件帧(去
{ session_id, timestamp, payload }外层信封 + 归并平铺)4.
GET /api/v1/meta与插件 / MCP / 能力snake_case收口阅读指南
packages/kap-server/src/protocol/session.ts、src/routes/sessions.ts:精简后的Session(顶层cwd、activity、last_turn_outcome)、创建会话顶层cwd支持,以及POST /sessions/{id}/profile返回{ session, agent }(纯修改标题/metadata 时不额外实体化未创建的主 Agent)。packages/kap-server/src/protocol/ws-agent.ts、src/transport/ws/v1/sessionAgentFeed.ts:原usage帧正名为context,以及 Agent 关闭时推送最终status: "ended"帧并自动释放空订阅条目。packages/kap-server/src/protocol/events-zod.ts、src/transport/ws/v1/sessionEventBroadcaster.ts:平铺全局事件构建与广播(优先读取内存实时ISessionMetadata,归档/恢复事件显式投影archived状态,:reload保留main订阅)。packages/kap-server/src/routes/meta.ts:kimi_agent_protocol_version: "0.1.0"与移除backend字段。packages/kap-server/src/routes/{mcp,plugins,capabilities}.ts与src/protocol/rest-{plugin,capability}.ts:请求与响应边界上的snake_case双向映射。packages/kap-server/test/*、packages/klient/test/e2e/*)验证上述新契约,可按需略读。Behavior Changes and Affected Users
Session字段busy、main_turn_active、last_turn_reason,以及恒为空/零的agent_config、usage、permission_rules、message_count;cwd仅在metadata.cwdcwd、activity(idle/turn/background)、last_turn_outcome(completed/interrupted/failed);移除空占位字段;创建会话接受顶层cwd(仍兼容传入metadata.cwd)cwd、activity、last_turn_outcome;模型与上下文水位从对应 Agent 帧读取(维护者已在本系列计划中确认,无旧字段回退)POST /api/v1/sessions/{id}/profile响应与目标编辑Session;goal_control不支持edit{ session, agent };支持goal_control: "edit"配合goal_objective原地修改目标data.session与data.agent读取(客户端同层 PR 同步适配)type: "usage"type: "context"(载荷保持context_tokens、max_context_tokens)type: "context"消费session_id、timestamp、payload外层信封的event.*/session.meta.updated帧session、workspace、config、invalidated(及扁平化event.capability.changed),统一带event_created_at与完整对象GET /api/v1/metabackend: "v2",无协议版本号kimi_agent_protocol_version: "0.1.0",移除backend字段/meta的客户端与探针kimi_agent_protocol_version识别协议版本/api/v1/mcp/*、/plugins/*、/capabilities/*字段命名camelCase(如flowId、authStatus、displayName、marketplaceId、stopRunningProcesses)snake_case(如flow_id、auth_status、display_name、marketplace_id、stop_running_processes)snake_case字段名(CLI/plugins走进程内服务不受影响)受影响模块与验证
packages/kap-server、packages/agent-core-v2(工作区与会话删除事件附带快照、goal_control: "edit"schema)、packages/klient(live e2e 适配平铺session帧)。kap-server全量 70 个测试文件通过;agent-core-v2workspaceService.test.ts通过;kap-server、agent-core-v2、klient类型检查与oxlint --type-aware0 错误。验证命令与输出摘要
pnpm --filter @moonshot-ai/kap-server test→70 passed (70),1366 passed | 1 skippedpnpm --filter @moonshot-ai/agent-core-v2 exec vitest run test/app/workspace/workspaceService.test.ts→71 passedpnpm --filter @moonshot-ai/agent-core-v2 --filter @moonshot-ai/kap-server --filter @moonshot-ai/klient typecheck→ 通过pnpm exec oxlint --type-aware --quiet packages/agent-core-v2 packages/kap-server packages/klient→0 errorsChecklist
/approve).gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.