Skip to content

fix(kap-server): document missing session actions and add snake_case :undo error fields - #5332

Merged
wbxl2000 merged 11 commits into
mainfrom
fix/kap-openapi-session-actions
Nov 25, 2026
Merged

wbxl2000 merged 11 commits into
mainfrom
fix/kap-openapi-session-actions

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

kap.md 说每个接口的请求体和响应 schema 以 GET /openapi.json 为准,但会话的 :fork、:compact、:undo、:btw、:restore 五个动作不在这份文档里,调用方查不到它们的 schema。这五个接口运行时一直可用,kap.md 也把它们列为 baseline。

同时,:undo 撤销不了时返回的 40911 错误,data 里是 camelCase 的 requestedCount、undoableCount,不符合 kap-server 公开字段用 snake_case 的约定,而文档一直写这里的 data 是 null。这个 PR 补上五个动作的文档,并在 40911 的 data 里补上 snake_case 的 requested_count、undoable_count;原来的 camelCase 字段继续返回,在文档里标为废弃。

在 #5290 的评审中发现,与该 PR 无关,单独修。

Bug Reproduction Steps

  1. 在 main 上列出文档里的会话动作路径(schema/openapi.json 是服务端 /openapi.json 的生成快照):
    jq -r '.paths | keys[] | select(startswith("/api/v1/sessions/{session_id}:"))' packages/kap-server/schema/openapi.json
    只输出 :archive、:cancel、:continue、:delete、:reload、:takeover、:wait 七个。
  2. :undo 要撤销的回合跨过了上下文压缩时返回 40911,响应的 data 是 { "reason": "compaction_boundary", "requestedCount": …, "undoableCount": … }。

Root Cause

文档缺口:会话动作在服务端共用一个分发路由 POST /api/v1/sessions/{tail}。生成 OpenAPI 文档时,patchSessionAction 把这个通用路由按动作逐个复制成 /api/v1/sessions/{session_id}:<action>(下称"投影"),再删掉通用路由。投影列表是手写的,fork、compact、undo、btw、restore 一直没补,通用路由删掉后它们就不在文档里了。

字段命名:引擎内部的撤销详情是 camelCase,kap-server 的 sendMappedError 把它原样放进 40911 的 data,没有转换成公开协议的 snake_case。

两处都是根本修复。

Code Changes

投影改成一张以动作名为键的表。 表的键是分发器的 SessionAction 联合,减去显式列出的实验性动作:

type ExperimentalSessionAction = 'predict-prompt' | 'cancel-prompt-prediction';

const sessionActionDocs: Readonly<
  Record<Exclude<SessionAction, ExperimentalSessionAction>, SessionActionDoc>
> = {
  fork: { response: forkSessionResponseSchema, body: forkSessionRequestSchema },
  undo: {
    response: undoSessionResponseSchema,
    body: undoSessionRequestSchema,
    bodyRequired: true,
    errorData: { [ErrorCode.SESSION_UNDO_UNAVAILABLE]: undoUnavailableDataSchema.nullable() },
  },
  // ...the other ten documented actions
};

新增会话动作时,要么在表里写上它的文档,要么加进 ExperimentalSessionAction,否则类型检查不通过。#5180 新加的 :predict-prompt、:cancel-prompt-prediction 在默认关闭的 prompt_prediction 实验开关后面,这次不写进文档,与 main 一致。operationId 沿用 runSession<Action>Action 的规律由动作名生成,已有 7 个不变。

40911 的 data 在 kap-server 出口补上 snake_case 字段。 sendMappedError 按引擎给出的详情返回 requested_count、undoable_count,同时照常返回原来的 requestedCount、undoableCount,后两者在文档里标为 deprecated,做法与旧的 cwd 字段相同;reason 不变;kap-server 自己判定不可撤销的那条路径不带详情,仍返回 null。引擎内部的字段名不动。文档只在 :undo 的投影里声明这个 data:分发路由的错误列表由所有会话动作共用,而带数据的 40911 只有 :undo 会返回,所以其他动作的 40911 仍写 data: null。

关键决策

决策 选择 没选 理由
实验性动作是否写进文档 不写,在文档表的类型里显式排除 与其他动作一样写进文档 它们默认关闭,契约还可能变;写进 /openapi.json 就成了对外承诺
camelCase 字段怎么处理 补上 snake_case 字段,camelCase 照常返回并标为 deprecated 直接改名;只把 camelCase 写进文档 公开字段统一用 snake_case;旧字段虽没写进文档,但已对外返回了一个月,直接改名会让读它们的外部调用方失效;先标废弃、以后再删,与旧的 cwd 字段做法一致
40911 的 data 在哪声明 只在 :undo 的投影里 分发路由的错误列表 错误列表由所有会话动作共用,在路由上声明会让每个动作的文档都出现这个形状

阅读指南

  1. packages/kap-server/src/openapi/transforms.ts:文档表、实验性动作的排除,以及 :undo 的 40911 data 如何进入文档。
  2. packages/kap-server/src/routes/sessionErrors.ts:40911 的 data 在哪补上 snake_case 字段、保留 camelCase 字段。
  3. packages/kap-server/src/protocol/rest-session.ts:SessionAction 联合(从 routes/sessions.ts 移来,openapi/ 因此不必依赖 routes/)、为文档拆出的 compactSessionBodySchema(与已有的 cancelSessionBodySchema、waitSessionBodySchema 同一写法,compactSessionRequestSchema 接受的输入不变)和 undoUnavailableDataSchema。
  4. packages/kap-server/test/openapi.test.ts、packages/kap-server/test/sessions.test.ts:文档断言,以及 fork 和 :undo 的真实请求。

可以略读:routes/sessions.ts 改为从 protocol/ 导入 SessionAction;middleware/defineRoute.ts 导出已有的 buildErrorEnvelopeSchema;schema/* 是生成文件;apiSurface.test.ts 只更新契约摘要;oxlint-ratchet.json 删掉了 transforms.ts 里已消除的两条存量告警。

Behavior Changes and Affected Users

行为 之前 之后 谁依赖旧行为 退路
/openapi.json 里的会话动作 7 个:archive、cancel、continue、delete、reload、takeover、wait 新增 fork、compact、undo、btw、restore,含请求体和响应 schema;已有 7 个不变 无 不需要
:predict-prompt、:cancel-prompt-prediction 的文档 不在文档里 不变 — —
:undo 返回 40911 时的 data { reason, requestedCount, undoableCount };kap-server 自己判定时为 null 多出 requested_count、undoable_count,原字段照常返回,文档里标为 deprecated;kap-server 自己判定时仍为 null 读 requestedCount / undoableCount 的外部脚本或自建客户端,不受影响。仓库内的 desktop、web、VS Code、klient、node-sdk 都不读这两个字段;TUI 经 SDK 在进程内调用引擎,不经过 REST 不需要:原字段保留
会话动作的其他运行时行为 — 不变:分发逻辑和各处理函数没改 — —
contract.txt 与 HOST_CONTRACT 摘要 — 只新增 5 个端点的行,已有行不变;摘要随之更新,HOST_API_LEVEL 不变 SSH 远端桥接用 HOST_API_LEVEL 判断远端是否兼容 不需要:当前客户端不读 40911 的 data,连上一版的远端也不会坏

测试覆盖:

  • test/openapi.test.ts:projects every session action onto its own path 钉住 12 条动作路径、operationId 和 session_id 路径参数,以及通用的 {tail} 路径已删除;keeps the experimental session actions out of the document 钉住两个实验性动作不在文档里;documents the request body each session action accepts 钉住哪些动作必须带请求体;documents the data :undo returns when the requested turns cannot be undone 钉住 :undo 的 40911 data 含 snake_case 字段、camelCase 字段标为 deprecated,:fork 的仍为 null。
  • test/sessions.test.ts:returns snake_case data beside the deprecated camelCase fields when :undo cannot remove the requested turns 用真实请求验证引擎给出详情时同时返回 snake_case 字段和原来的 camelCase 字段、不给时返回 null,钉住读旧字段的调用方不受影响;forks a session from a request without a body 验证 fork 不带请求体也能成功,对应文档里 fork 的请求体可选。
  • test/apiSurface.test.ts:生成的契约快照与摘要。
验证记录
  • 与 base 对比 schema/openapi.json:只新增 5 条路径;其余路径(含已有的 7 个会话动作)和非路径部分逐字节相同。contract.txt 只有新增行。
  • 把 sendMappedError 改回原样透传后运行新的 :undo 测试:失败,缺少 snake_case 字段;恢复后通过。
  • 去掉 camelCase 字段后运行同一个测试:失败,缺少 requestedCount、undoableCount;恢复后通过。
  • 让 40911 的 data 声明对所有动作生效后运行 test/openapi.test.ts::fork 的断言失败;恢复后通过。
  • 改动涉及的三个测试文件全部通过;kap-server 类型检查、pnpm lint 与 pnpm run check:duplication 通过。本机高负载下跑 kap-server 全量测试时,有一批与本改动无关的计时和临时目录类失败,每次失败的文件不同,单独重跑均通过;CI 的全量测试通过。

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.

Project fork, compact, undo, btw and restore onto their own OpenAPI paths. The projection is now a table keyed by the shared SessionAction union, so a session action without docs fails to compile.
@changeset-bot

changeset-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5863543

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/kap-server 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 24, 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-25T06:30:13.404111Z 5863543 New commits
ℹ️ 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 24, 2026 •

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

commit: 5863543

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

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

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

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

main added :predict-prompt and :cancel-prompt-prediction to the session action dispatcher. Project them like every other action, and build operation ids from each hyphen-separated part of the action name so they stay valid identifiers.

@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: 2579fd84f8

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

The 40911 error from :undo passed the engine's camelCase details through as its data. Return requested_count and undoable_count instead, as public fields are snake_case, and document that data on :undo only; the other session actions keep documenting a null data for 40911.

Keep :predict-prompt and :cancel-prompt-prediction out of /openapi.json while prompt prediction stays behind its experimental flag, and drop the hyphen-aware operation id naming that only they needed.

@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: 161af31f26

ℹ️ 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 changed the title fix(kap-server): document the missing session actions in OpenAPI fix(kap-server): document missing session actions and snake_case :undo error data Nov 24, 2026
…aliases

Return requested_count and undoable_count beside the existing requestedCount and undoableCount in the data of the 40911 error from :undo, and mark the camelCase pair deprecated in /openapi.json, so callers that read the old names keep working.
@wbxl2000 wbxl2000 changed the title fix(kap-server): document missing session actions and snake_case :undo error data fix(kap-server): document missing session actions and add snake_case :undo error fields Nov 25, 2026
@wbxl2000
wbxl2000 merged commit 3aa2588 into main Nov 25, 2026
23 checks passed
@wbxl2000
wbxl2000 deleted the fix/kap-openapi-session-actions branch November 25, 2026 07:49
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 25, 2026
MoonshotAI#5359 and MoonshotAI#5404 need the fork's loop/ACP shapes (LiveTurn state machine,
session extensions), MoonshotAI#5434 depends on testkit signal imports, and MoonshotAI#5332
needs a merged SessionAction table. Each is in_progress with the precise
remaining work rather than a forced apply.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 26, 2026
…ata (MoonshotAI#5332)

Project session actions from a SessionAction-keyed docs table: each action
gets its request body, success envelope and, for :undo, the 40911 data with
requested_count/undoable_count plus the deprecated camelCase fields. The
generic {tail} action route is removed from the document.

Verified: openapi + apiSurface 18 pass, sessions 135 pass, schema refreshed
and the reviewed digest updated; check:no-comments and typecheck clean.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 26, 2026
…AI#5332

The docs-table port replaced the fork's strict request schemas with the
upstream shapes, so fork, predict-prompt and cancel-prompt-prediction
bodies would strip unknown keys instead of rejecting them. Restore
.strict() on all three and keep the documentation body schemas strict.

Verified: check:strict-request-bodies OK, openapi + apiSurface 18 pass,
sessions 135 pass, typecheck clean.
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