Skip to content

test: move seven KAP conversation contract cases onto testkit scenarios - #5200

Merged
RealKai42 merged 1 commit into
mainfrom
test/kap-contract-scenarios
Nov 20, 2026
Merged

RealKai42 merged 1 commit into
mainfrom
test/kap-contract-scenarios

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

packages/kap-server/test/conversationContract.e2e.test.ts 守着 KAP 的对话契约:回合和条目在 WS 帧上的实时变化要和 REST 分页一致,中途插话、审批、后台任务、取消、继续、撤销的协议行为要对。它用一个手写的 OpenAI 流假模型,靠固定延迟(1.5 到 4 秒)让回合停在「进行中」,再每 150 毫秒轮询一次结果。中途操作能不能落在回合进行中,取决于延迟够不够长。最近三周这个文件在 8 个分支上失败了 40 次,其中 2 次同一提交重跑后通过。

这个 PR 是把脆弱用例迁到 testkit 的第一批试点:把其中最依赖时序的 7 条搬到 testkit 场景上,断言逐条保留。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路:用 testkit 的挂起点 held() 把假模型停在确定的位置,等 hold.reached() 后再做中途操作,做完再 release(),不再依赖延迟的长短。等待改为 kap.waitIdle(服务端长轮询)和 socket.waitFor(等具体的帧),不再每 150 毫秒轮询。每条用例有自己的服务端和 home,收尾时自动核对编排的回复是否用完、挂起点是否都已放行。

旧用例 去向(tests/scenarios/conversation.test.ts) 原来怎么让回合停在中途 现在
S1 一轮流式回复 新场景 回复延迟 1.5 秒 第一个分片后挂起,等到 item.started 再放行
S2 回合中再发一条 并入「回复中排队」场景,补每轮的 prompt_ids 和结束状态 第一条延迟 2.5 秒 第一条回复挂起
撤销插话后的回合 新场景 原请求延迟 1.5 秒 原请求挂起,插话时被中止
S3 审批挂在工具条目上 并入「手动审批」场景,补帧和条目断言 不需要;读引擎内部的待审批列表 用 KAP 的 :wait 等待审批
S4 后台子 agent 和通知回合 新场景 子 agent 延迟 2.5 秒 子 agent 回复挂起,主回合结束后再放行
S5 回合中途订阅 新场景 回复延迟 4 秒 回复挂起
S6 REST 和 WS 快照一致 新场景 不需要 不需要

三处有意的差异,其余断言原样保留,逐条对应见下方折叠块:

  • 撤销用例从磁盘读回的方式:原来用引擎内部的 closeSessionById 关会话再读回。KAP 没有关闭会话的接口,所以改为重启服务端后读回。验证的仍然是「落盘的回合和内存里的一致」。
  • S3 等待审批的方式:原来从引擎内部的交互列表取待审批项,现在用 KAP 的 :wait 接口取。tests/scenarios 只允许通过 KAP 和 testkit 观察,boundaries.test.ts 会检查这一点。
  • 工具调用 id 和命令文本:改用 testkit 生成的 model.callId(0, 0) 和原场景里的命令,断言的含义不变。

旧文件还剩 S7、S9、S10、S11、S12、S13 六条,留给后面两批;只被已迁用例用到的导入和辅助成员一并删掉。手写的 SSE 辅助函数仍被剩下的用例使用,所以这个文件的旧替身基线(inline-openai-stream 6 处)这次不变,最后一批删掉旧文件时归零。

每条旧断言在新场景里的位置

新场景都在 tests/scenarios/conversation.test.ts。

S1 → 「keeps streamed item ids in the persisted turn and reports the agent idle only after the turn ends」

  • 订阅时的快照是空的 turns 帧,has_more 为 false:原样保留。
  • 只有一轮;ordinal、turn_number、kind、status、outcome 的值,turn_id 的格式,没有 end_reason,ended_at 是字符串:原样保留。
  • 只有一个 step,用量里 output 为 4,回合用量等于 step 用量:原样保留。用量改由 withUsage({ input: 10, output: 4 }) 给出,数值和旧的假模型相同。
  • 第一个条目是 opener 用户消息,prompt_ids、来源、文本、附件、技能都对得上:原样保留。
  • 回答条目已结束、结果为 completed、文本为 hello world:原样保留。
  • 回答条目有 item.started 和 text 字段的 item.delta 帧,turn.updated 只带 step 1:原样保留。回复在第一个分片后挂起,收到 item.started 后才放行,所以一定会有单独的 item.delta。
  • WS 还原的回合等于 REST:原样保留。
  • agent 帧出现过 running,全部 compacting: false,都不带 turn_number、prompt_id、retry;running 之后的 idle 帧排在 turn.ended 之后:原样保留。

S2 → 并入「queues a prompt sent while the model is still replying and runs it as its own turn」

  • 第二条在第一轮进行中提交:由第一条回复的挂起点保证。
  • 两轮都结束,各自 opener 的 prompt_ids 依次是两次提交的 id:新增断言。
  • 原场景的断言(第二条进入排队,模型请求里的用户消息,两轮各自的回答)保持不变。

撤销插话后的回合 → 「undoes a steered turn on REST and WebSocket and gives the resent prompt the same ordinal」

  • 插话仍走 prompts:steer 加 prompt_ids:接口不变。
  • 两轮;第二轮里有插话后的回答,opener 带插话的 prompt_id;WS 等于 REST:原样保留。
  • 撤销第一轮会删掉两轮,REST 为空;turns.removed 帧带两轮 id,不会补发新的 turns 快照,WS 为空,重新订阅的快照为空:原样保留。
  • 重发后只有一轮,文本相同、序号相同、id 不同,WS 等于 REST:原样保留。
  • 从磁盘读回后与内存一致,再撤销后为空:原来用引擎内部的 closeSessionById 关会话再读回;KAP 没有关闭会话的接口,改为重启服务端后读回。

S3 → 并入「waits for approval in manual mode, shows it on the tool item and runs the tool once approved」

  • 等到待审批的 Bash 调用:原来读引擎内部的交互列表,现在用 KAP 的 :wait(kap.waitForInteraction),并沿用原场景对交互字段的断言。
  • 审批前,WS 上已经出现这个工具条目:等到它的 item.started 帧。
  • 批准后,REST 里的工具条目 id、状态、结果、输入和 interaction_id 都对得上,没有 stage,输出包含命令结果:原样保留。
  • 这个条目的第一帧是 stage 为 input 的 item.started,之后有 stage 为 awaiting_approval、带 interaction_id 的 item.updated,item.ended 只有一次;WS 等于 REST:原样保留。
  • 调用 id 改用 model.callId(0, 0),命令沿用原场景的 echo approved-run。

S4 → 「reports a background subagent as a task and starts a notification turn when it completes」

  • 有 running 的 subagent task 帧,字段对得上,child_agent_id 是字符串:原样保留。
  • task 以 completed 结束;有一轮通知回合,会话回到空闲:原样保留。
  • 通知条目各字段对得上,标题是非空字符串,不含 <notification:原样保留。原来的写法是判断标题长度大于 0。
  • Agent 工具条目的 agent_refs 指向子 agent;WS 等于 REST:原样保留。
  • 子 agent 的回复一直挂起,直到主回合结束才放行,所以一定会另起一轮通知回合。原来是延迟 2.5 秒,只是让这个顺序大概率成立。

S5 → 「shows a client that subscribes mid-turn the running turn and a running agent」

  • 订阅时的快照里是进行中的第 0 轮:原样保留。
  • 收到的第一个 agent 帧是 running:原来是订阅后马上读第一条,现在等到第一条 agent 帧再判断。
  • 放行后,agent 帧变为 idle,WS 收到 turn.ended,WS 等于 REST:原样保留。

S6 → 「gives a client that subscribes after a turn the same turns page as REST」

  • 回合结束后再订阅,快照的回合和 has_more 与 REST 默认分页一致;只有一个已结束的 Bash 工具条目,id 对得上:原样保留。调用 id 改用 model.callId(0, 0)。

Behavior Changes and Affected Users

None。只改了测试:没有改产品代码、KAP 契约文件(packages/kap-server/schema/)、能力清单、CI 工作流或棘轮基线。这 7 个行为原来在 kap-server 项目里跑,现在在 scenarios 项目里跑,两者都属于根目录 pnpm test 的 Linux 分片。

本机验证(macOS):

  • pnpm exec vitest run tests/scenarios/conversation.test.ts packages/kap-server/test/conversationContract.e2e.test.ts tests/scenarios/boundaries.test.ts:24 条全部通过。
  • pnpm exec tsc -p tests/scenarios/tsconfig.json --noEmit 和 pnpm exec tsc -p packages/kap-server/tsconfig.json --noEmit:通过。
  • pnpm lint:通过。
  • pnpm run check:duplication 3f514458d:没有新增重复。
  • 不加负载时,旧的 7 条测试耗时 18.5 秒,新的 7 条 1.9 秒。

满负载对比:用 10 个空转进程占满本机 10 个核,新旧 7 条各跑 50 遍。

通过 测试耗时(平均,最短到最长)
旧用例 50 / 50 19.2 秒(18.5 到 19.6)
新场景 50 / 50 3.0 秒(2.4 到 4.0)

旧用例在本机加负载后也没有失败,CI 上偶发的那几次没能在本机复现。所以这一批能确定的收益是:

  • 测试耗时降到原来的六分之一左右;
  • 中途操作的时机由挂起点保证,不再取决于延迟够不够长;
  • 不再读引擎内部,也不再按提示词原文给假模型分配回复。

迁移前后在 CI 上稳定性的差异,要靠合入后的 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.

Move S1 to S6 and the steered-turn undo case from
packages/kap-server/test/conversationContract.e2e.test.ts into
tests/scenarios/conversation.test.ts. The scenarios hold the fake
model at named points instead of delaying replies by 1.5 to 4 seconds,
wait on KAP frames and the session wait endpoint instead of polling,
and observe the server only through KAP. S2 and S3 fold into the
existing queue and approval scenarios as extra assertions.
@changeset-bot

changeset-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: f22a6fb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T14:57:04.482575Z f22a6fb PR opened
🔒 Security Review ✅ Completed 2026-11-20T14:57:49.069319Z f22a6fb PR opened
ℹ️ 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 20, 2026

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

commit: f22a6fb

@RealKai42
RealKai42 merged commit 2e37a32 into main Nov 20, 2026
28 checks passed
@RealKai42
RealKai42 deleted the test/kap-contract-scenarios branch November 20, 2026 15:02
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.

1 participant