Skip to content

feat(kap-server): push approvals and questions as interaction frames - #4428

Merged
wbxl2000 merged 11 commits into
mainfrom
qer/interaction-bubble
Oct 27, 2026
Merged

wbxl2000 merged 11 commits into
mainfrom
qer/interaction-bubble

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Requirement or Bug

用单个平铺的 interaction 帧推送审批与提问(含 subagent 与侧聊发起的),替换旧的 event.approval.* / event.question.*;答复统一到 POST /sessions/{sid}/interactions/{id},删除待处理审批 / 提问的 GET 列表接口;顺带修复被取消的提问在历史里显示为已回答。

Bug Reproduction Steps

  1. 让 agent 发起一个提问(AskUserQuestion)。
  2. 不回答,直接取消这一轮(或让引擎因中断取消该提问)。
  3. 重新打开会话历史:该提问显示为「已回答」。

Root Cause

引擎取消交互时的响应是 { cancelled: true, reason }。kap-server 的 mapInteractionEndState 和 transcript 的 foldFacts 只区分「有响应 → answered / 无响应 → dismissed」,把取消响应也当成了回答。现在先识别 cancelled: true 并映射为 cancelled,属于根本修复。

Code Changes

WS 帧:不进事件信封,没有 seq / offset / timestamp,不进 journal、不回放;已注册进 WS schema 与 AsyncAPI(protocol/interaction.ts)。

type InteractionEventMessage = {
  type: 'interaction';
  session_id: string;
  agent_id: string;
  event_created_at: string;
  interaction_id: string;
  kind: 'approval' | 'question';
  status: 'pending' | 'ended';
  outcome?: 'approved' | 'rejected' | 'cancelled' | 'answered' | 'dismissed';
  ended_at?: string;
  tool_call_id?: string;
  request:
    | { tool_name; action; tool_input_display?; expires_at }
    | { questions };
  response?:
    | { decision; scope?; feedback?; selected_label? }
    | { answers: Record<string, string | true>; method? };
};

投递与恢复:每次订阅确认后,服务端立即把该会话当前待处理的交互发给这条连接,客户端据此重建列表。

sequenceDiagram
    participant Client
    participant WS as wsConnectionV1
    participant B as SessionEventBroadcaster
    Client->>WS: client_hello / subscribe / subscribe_v2
    WS->>B: attach + replay
    WS-->>Client: ack
    WS->>B: sendPendingInteractions(accepted)
    B-->>Client: interaction (pending) × N
    Note over B,Client: live
    B-->>Client: interaction (pending / ended)
Loading

答复接口:

-POST /sessions/{sid}/approvals/{id}             { decision, scope?, feedback?, selected_label? } → { resolved, resolved_at }
-POST /sessions/{sid}/questions/{id}             { answers, method?, note? }                      → { resolved, resolved_at }
-POST /sessions/{sid}/questions/{id}:dismiss                                                      → 40909 / { dismissed, dismissed_at }
+POST /sessions/{sid}/interactions/{id}
+  { kind: 'approval', decision, scope?, feedback?, selected_label? }
+  { kind: 'question', answers, method?, note? } | { kind: 'question', dismissed: true }
+  → { outcome, ended_at }
+  40001 请求体不合法或 kind 不符 / 40404 交互不存在 / 40902 交互已结束
-GET  /sessions/{sid}/approvals?status=pending
-GET  /sessions/{sid}/questions?status=pending
 SessionEventBroadcaster
-  event.approval.requested / resolved / expired
-  event.question.requested / answered / dismissed
-  isInteractionEvent filter exemption
+  interaction(平铺帧,经 state.queue 发给会话全部订阅者)
+  sendPendingInteractions(sessionId, target)
 wsConnectionV1
+  send(EventEnvelope | InteractionEventMessage)
+  after each ack: sendPendingInteractions(accepted)
 routes
+  interactions.ts(统一答复)
-  approvals.ts / questions.ts
 protocol
+  interaction.ts(WS 帧与答复 schema)
+  interaction-wire.ts(广播与 /snapshot 共用的转换函数,从 routes/approvals.ts 移来)
-  rest-approval.ts / rest-question.ts
 coreEventMap.mapInteractionEndState / transcript foldFacts
+  { cancelled: true } → 'cancelled'
 kimi-inspect / klient e2e harness
+  改用 /interactions/{id}

interaction 不带 camelCase agentId,因此不受 agent_filter 和 transcript 抑制影响,session 的每个订阅者都能收到 subagent 的交互。草案中的 turn_id、agent_name 暂不提供:引擎 turn id 到 transcript turn id 的映射只在 transcript projector 内,agent 名称与 AgentState.name 重复。

Behavior Changes and Affected Users

Behavior Before After Who relies on the old behavior Escape hatch
WS 上审批 / 提问的事件 event.approval.*、event.question.*,持久、带 seq、可回放 单个平铺 interaction 帧,无 seq,不回放 desktop 与 web 客户端(code-app) 无;客户端同步改造
断线重连后的待处理交互 靠 journal 回放旧事件 每次订阅确认后补发当前待处理交互 同上 无
答复审批 / 提问 /approvals/{id}、/questions/{id}、/questions/{id}:dismiss 统一 /interactions/{id},旧路由 404 desktop、web、kimi-inspect、klient e2e harness 无;kimi-inspect 与 harness 本 PR 内已迁移
忽略已忽略的提问 40909 40902(交互已结束) 旧 web 客户端曾放行 40909 无
找不到提问 40405 40404 同上 无
被取消的提问在历史 / transcript 中的状态 answered cancelled 无(旧行为是 bug) —
待处理审批 / 提问的列表 GET /approvals、GET /questions 删除,只通过订阅确认后补发的 interaction 帧获得 kimi-inspect 的交互卡片、klient e2e harness 无;harness 本 PR 内已删,kimi-inspect 暂不改,卡片列表会 404。/snapshot 的 pending_approvals / pending_questions 暂保留

受影响的消费者与兼容:

  • desktop(code-app):内嵌 server 随包发布,配套客户端改动在 MoonshotAI/kimi-code-app#984,bump 到本 PR 合入后的提交。
  • web(apps/kimi-code/dist-web):本 PR 不同步 dist。合入后、下一次 dist 同步前,打包的旧 web 页面仍能从 transcript 看到主 agent 和已打开面板的 agent 的审批与提问,但批准、回答、忽略的请求会 404;需要在 code-app 那边合入后尽快按常规流程提 dist PR。
  • TUI / print / SDK / ACP:不走 WS v1 的这些事件和这几个 REST 接口,无影响。

测试覆盖:

  • sessionEventBroadcaster.test.ts:帧按 interactionEventMessageSchema 校验;提问 pending → answered 且不进回放;dismissed / cancelled 提问无 response;审批 approved 与 cancelled、expires_at;agent_filter: ['main'] 加 transcript grade 下 subagent 交互照常投递;sendPendingInteractions 只发给请求方、立即发送、结束后不再补发。
  • wsConnectionV1.test.ts:subscribe / subscribe_v2 的 ack 之后紧跟 interaction 帧。
  • approvals.test.ts、questions.test.ts:新路由的批准 / 回答 / 忽略,kind 不符与 answers、dismissed 同时有或都没有返回 40001,未知 id 返回 40404,重复答复返回 40902,未知会话返回 40401;待处理状态直接查引擎。
  • transcriptContract.e2e.test.ts、klient legacy 测试、kimi-inspect 测试随路由迁移。
  • services/transcript.test.ts、transcript/test/layers.test.ts:被取消的提问折叠为 cancelled。
  • apiSurface 快照更新。

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.

Replace event.approval.* / event.question.* with a single volatile
interaction event carrying the draft Interaction shape. Every subscriber
of a session receives it, including interactions raised by sub-agents
whose transcripts are subscribed. After each subscribe ack the server
sends the session's pending interactions to that connection, so clients
rebuild their list on (re)subscribe instead of relying on journal replay.

Map engine-cancelled questions to cancelled instead of answered.
@changeset-bot

changeset-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5646dc8

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 Oct 26, 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-27T04:45:05.739062Z 5646dc8 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 Oct 26, 2026 •

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

commit: 2f4d9bc

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

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

Copy link
Copy Markdown
Collaborator Author

@codex i updated, continue review

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

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

ℹ️ 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/interaction-ws-event.md Resolved
…oute

Send `interaction` as a flat, unsequenced frame `{ type, session_id, agent_id,
event_created_at, interaction_id, kind, status, ... }` outside the event
envelope, add approval `expires_at`, and register the frame in the WS schema
and AsyncAPI document.

Replace `POST /sessions/{sid}/approvals/{id}`, `POST /sessions/{sid}/questions/{id}`
and `:dismiss` with `POST /sessions/{sid}/interactions/{id}`, whose body is
discriminated by `kind` and whose result is `{ outcome, ended_at }`; dismissing a
question now succeeds with code 0. Error codes become 40404 (interaction not
found), 40902 (interaction ended) and 40001 (kind mismatch).
@wbxl2000 wbxl2000 changed the title feat(kap-server): push approvals and questions as interaction events feat(kap-server): push approvals and questions as interaction frames Oct 26, 2026
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 679662dd73

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

Pending interactions now reach clients only through the interaction frames
sent after each subscribe ack, so drop GET /sessions/{sid}/approvals and
GET /sessions/{sid}/questions with their schemas, docs and harness helpers.
The wire helpers still used by the WS broadcaster and the snapshot route move
to protocol/interaction-wire.ts.
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 5807d9178d

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

Copy link
Copy Markdown
Collaborator Author

@codex review

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

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

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 2f4d9bcbe2

ℹ️ 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 461c6e9 into main Oct 27, 2026
15 checks passed
@wbxl2000
wbxl2000 deleted the qer/interaction-bubble branch October 27, 2026 04:43
This was referenced Oct 27, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
Ports upstream MoonshotAI#4428. Approvals and questions now reach clients as one
flat, unsequenced WebSocket `interaction` frame instead of the
`event.approval.*` / `event.question.*` envelopes, and one route answers
either kind:

- `POST /api/v1/sessions/{id}/interactions/{interaction_id}` takes a body
  discriminated by `kind` and returns `{ outcome, ended_at }`; dismissing
  a question now succeeds with code 0 instead of a 40909.
- `GET .../approvals`, `POST .../approvals/{id}`, `GET .../questions`,
  `POST .../questions/{tail}` and `.../{id}:dismiss` are removed. Pending
  interactions are replayed to a subscriber right after each accepted
  subscribe ack, so a client no longer lists them over REST.
- Error codes: 40404 becomes `INTERACTION_NOT_FOUND`, 40902 becomes
  `INTERACTION_ENDED`, 40505/40909 (`QUESTION_NOT_FOUND`,
  `QUESTION_DISMISSED`) are dropped.
- The frame carries approval `expires_at`, the tool-call id, and — on
  `status: "ended"` — the decision or the question answers, so the
  transcript and the clients close a card from one event.

Cancelled questions no longer read as answered in history: the shared
interaction-state mapping treats the engine's cancellation shape
(`{cancelled: true, reason}`) as cancelled before it looks at the
question/approval split, and only that exact shape counts — a bare
`{cancelled: true}` still reads as answered.

Local shape notes:

- The fork's `defineRoute` rejects a request body schema that is not
  strict, so both union members are `.strict()`; upstream relied on the
  outer schema only. `check:strict-request-bodies` reads the declaration,
  so the members are inlined into `interactionRespondRequestSchema`
  rather than exported separately.
- `mapInteractionEndState` lives in `packages/transcript`'s contract layer
  here (one shared copy for cold and hot projection), not in
  kap-server's `coreEventMap`, so the cancellation-shape fix landed there;
  the projector now derives its state from `interactionOutcome`.
- The apiSurface snapshot was still a Vitest-format file holding a stale
  duplicate of the same snapshot; regenerating with bun keeps one
  canonical entry, so the diff drops the dead copy as well.
- The klient e2e harness loses its reverse-RPC approval/question helpers
  (`reverse-rpc.ts` deleted) and gains `respondInteraction`.

Gates: kap-server 1403 pass / 9 skip, transcript 164 pass, klient 34
pass / 12 skip, kimi-inspect and apps/kimi-code `tsc` clean,
`check:no-comments`, `check:no-hardcoded-prose`, `check:locale-keys`,
`check:deep-imports`, `check:strict-request-bodies`,
`check:service-naming` green. Pre-existing review findings left alone:
`orphan-exports` on the in-progress TUI code-highlight file,
`silent-catch` in `workspaceService.ts`, `text-as-protocol` in
`search/indexCore.ts`, and the stale `agent-core-v2` state manifest.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
…tions

Ports the remaining upstream MoonshotAI#4428 assertions. A subscriber now gets the
pending interactions of each accepted session immediately after the ack,
so the connection test drives subscribe and subscribe_v2 through a
broadcaster that replays one frame and asserts the ack/frame ordering.

The projector tests pin the shape rule behind the history fix: the
engine's cancellation (`{cancelled: true, reason: 'turn_ended'}`) resolves
a question as cancelled, while an answer whose question happens to be
named "cancelled" still resolves as answered.

Gates: wsConnectionV1 39 pass, services/transcript 135 pass.
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Nov 4, 2026
The respond route's own test is the probe; --verify runs it green. The row records the one unported piece: upstream's change to test/transcriptContract.e2e.test.ts, which bunfig excludes from the default run.
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