Skip to content

test: move four more KAP conversation contract cases onto testkit scenarios - #5202

Merged
RealKai42 merged 2 commits into
mainfrom
test/kap-contract-scenarios-2
Nov 23, 2026
Merged

RealKai42 merged 2 commits into
mainfrom
test/kap-contract-scenarios-2

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

这是把 packages/kap-server/test/conversationContract.e2e.test.ts 迁到 testkit 场景的第二批,第一批是 #5200。这一批搬的是会话控制相关的 4 条:取消(S11)、继续(S13)、新会话的 agent 帧和 todo 帧(S10),以及关闭旁聊 agent(S12)。

这几条原来的写法有三个问题:

  • 靠手写假模型在流中途暂停 5 秒,让回合停在「进行中」;
  • 靠提示词原文识别压缩请求和继续请求;
  • 有一条绕过模型,直接调用引擎执行后台命令。

Bug Reproduction Steps

N/A

Root Cause

N/A

Code Changes

核心思路和第一批相同:用 held() 把假模型停在确定的位置,用 KAP 帧和 :wait 接口等待,只通过 KAP 观察,每条用例有自己的服务端和 home。

旧用例 去向(tests/scenarios/conversation.test.ts) 原来怎么让回合停在中途 现在
S10 新会话的 agent 帧和 todo 帧 新场景 不需要 不需要
S11 前半:取消进行中的回合,空闲时再取消 并入「取消」场景,补帧、end_reason、二次取消和会话状态的断言 第一个分片后暂停 5 秒 第一个分片前挂起
S11 后半:取消时是否停后台命令 新场景 绕过模型,直接让引擎执行后台 Bash 由假模型在一轮里发起后台命令
S12 关闭旁聊 agent 新场景 不需要 不需要
S13 :continue 新场景 第一个分片后暂停 5 秒;按提示词原文识别压缩请求和继续请求 第一个分片后挂起;压缩请求按元数据进 compaction 通道

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

  • S11 后半的后台命令:原来绕过模型直接让引擎执行,现在由假模型在一轮里发起,所以多了一轮。取消相关的断言放在这一轮结束之后,并新增一条:会话 activity 为 background。
  • S11 前半的 cwd:旧用例用 /tmp 建会话,testkit 用自己隔离的工作区,断言改为和它比较。
  • S13 的请求识别:压缩请求和继续请求不再按提示词原文识别。改提示词不会再让请求落到默认回复上,报出的错和真正原因对不上。

旧文件只剩 S7、S9 两条,留给第三批。下面这些只被已迁用例用到,一并删掉了:

  • 辅助函数:sseTextChunks、帧还原、WS 订阅封装、超时诊断;
  • 手写假模型服务里的状态码、延迟、流中暂停和请求记录。

这个文件的旧替身基线(inline-openai-stream)按 test-doubles 检查的要求从 6 降到 4。

关闭旁聊 agent 的场景单条约 3.2 秒,时间全花在关闭接口本身:引擎移除 agent 时最多等它静止 3 秒,空闲的旁聊 agent 每次都等满。旧用例同样如此(约 3.4 秒)。这是产品问题,另行处理,这个 PR 不改。

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

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

S11 前半 → 并入「cancels a turn while the model request is in flight and has nothing to cancel once idle」

  • 新建的会话 cwd 等于请求里的 cwd,activity 为 idle:保留。旧用例请求的是 /tmp,testkit 传的是自己隔离的工作区,断言改为和它比较。
  • 取消前,WS 上已有进行中的回合:原来轮询 WS 还原的回合,现在等它的 turn.started 帧。
  • 第一次取消返回 { cancelled: true }:原场景已有。
  • 这一轮在 WS 上以 ended、interrupted、end_reason: user_cancelled 结束:保留。
  • 第二次取消返回 { cancelled: false };会话 activity 为 idle,last_turn_outcome 为 interrupted:保留。
  • 原场景的断言(模型请求被中止,空闲后的结局为 interrupted)保持不变。
  • 差异:旧用例在第一个分片后暂停 5 秒再取消,原场景在第一个分片前挂起。两者都是在模型请求进行中取消,旧用例对已流出的分片没有断言。

S11 后半 → 「keeps a background command running on cancel until the client also asks to stop background work」

  • 有一个已脱离回合、正在运行的后台命令 sleep 10:保留。旧用例绕过模型直接调用引擎执行 Bash;现在由假模型在一轮里发起 bash({ ..., run_in_background: true }),等这一轮结束后再取消。
  • 不带 include_background 取消返回 { cancelled: false },任务最后一帧仍是 running:保留。新增一条:会话 activity 为 background。
  • 带 include_background 取消返回 { cancelled: true },任务以 killed 结束:保留。

S13 → 「continues an interrupted or failed turn and refuses when nothing is left to continue or a turn is running」

  • 新会话调用 :continue 报 40940;回合进行中报 40901:保留。回合在第一个分片后挂起,旧用例是暂停 5 秒。
  • 取消后最后一轮以 interrupted 结束:保留。
  • 继续返回 { continued: true },续上的一轮已完成、没有用户消息、回答为 resumed reply,回合数至少为 2:保留。已完成时再继续报 40940:保留。
  • 不可重试的 400 让回合失败,继续后完成:保留。旧用例在手写服务里用一个开关返回 400,现在脚本里写 httpError(400)。
  • 撤销第一轮后继续报 40940:保留。
  • 再失败一次后压缩,最后一轮是已完成的 system 回合;继续后的 agent 回合完成:保留。
  • 差异:
    • 旧用例靠提示词原文 Summarize the conversation 和 Continue the interrupted work from where it stopped 识别压缩请求和继续请求。现在压缩请求按请求元数据进 testkit 的 compaction 通道,继续请求按顺序由主通道回答。
    • 旧用例轮询 WS 还原的回合,直到最后一轮满足条件。现在每个动作前记下帧的位置,等其后的下一个 turn.ended,再检查最后一轮。

S10 → 「sends a new subscriber the idle main agent and its empty todo list, then pushes TodoList writes」

  • 第一条 agent 帧是该会话的 idle agent,第一条 todo 帧的条目为空:保留。
  • TodoList 调用之后,最新的 todo 帧带两条待办:保留。调用改用 todoList(...) 生成。

S12 → 「closes a side agent with an ended agent frame and refuses to close the main agent」

  • 建旁聊 agent 并订阅它,收到它的 agent 帧:保留。
  • 关闭 main 报 40939:保留。
  • 关闭旁聊 agent 返回 { closed: true },它的最后一条 agent 帧是 ended、compacting: false:保留。
  • 再次关闭、再查它的回合都报 40401:保留。错误码从 testkit 的 KapEnvelopeError.code 读取。

Behavior Changes and Affected Users

None。只改了测试和 scripts/checks/baselines/test-doubles.json 里这个文件的计数(只降不升)。没有改产品代码、KAP 契约文件、能力清单或 CI 工作流。旧文件里这 4 条用例覆盖的行为原来在 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 2e37a3208:没有新增重复。
  • 不加负载时,旧的 4 条测试耗时 6.2 秒,新的 5 条 4.4 秒。新场景多一条,是因为 S11 拆成了两半,前半并入了原有的取消场景。

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

通过 测试耗时(平均,最短到最长)
旧用例(4 条) 50 / 50 8.1 秒(6.5 到 16.4)
新场景(5 条) 50 / 50 5.6 秒(4.9 到 10.3)

两边的耗时里都有关闭旁聊 agent 时那 3 秒左右的等待。和第一批一样,旧用例在本机加负载后也没有失败。所以这一批能确定的收益是:

  • 回合进行中的取消和 :continue(应报 40901)都发生在挂起点上,不再取决于 5 秒的暂停够不够长;
  • 不再按提示词原文给假模型分配回复,也不再绕过模型直接让引擎执行后台命令;
  • 测试耗时降低约三成。

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.

@changeset-bot

changeset-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 5be5ffe

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-23T05:25:28.883368Z c14c0ac New commits
🔒 Security Review ✅ Completed 2026-11-20T16:16:55.407530Z 5be5ffe 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@c14c0ac
npx https://pkg.pr.new/@moonshot-ai/kimi-code@c14c0ac

commit: c14c0ac

Keep both testkit model imports in conversation.test.ts: call from the invalid tool argument retry scenario on main and todoList from this branch.
@RealKai42
RealKai42 merged commit 634db7a into main Nov 23, 2026
27 checks passed
@RealKai42
RealKai42 deleted the test/kap-contract-scenarios-2 branch November 23, 2026 06:20
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