Skip to content

test(agent-core-v2): take wall-clock races out of the SSH probe, MCP crash and compaction failure cases - #5486

Merged
RealKai42 merged 3 commits into
mainfrom
test/race-fixes
Nov 26, 2026
Merged

RealKai42 merged 3 commits into
mainfrom
test/race-fixes

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

接着 #5458、#5476 和 #5479,继续清理 agent-core-v2 全量测试里的偶发失败。这个 PR 修三处:两处是用例拿真实的短时限和子进程的启动速度赛跑,一处是两条用例真等了重试退避。三处都只改测试,产品代码不动。

Bug Reproduction Steps

同时运行 6 份下面的命令,另开 6 个吃满 CPU 的进程:

  1. pnpm exec vitest run packages/agent-core-v2/test/remote/session.test.ts -t "SSH platform detection before executor launch":bounds a stalled probe and never starts the executor 18 次失败 2 次,报 ENOENT ... /calls。
  2. pnpm exec vitest run packages/agent-core-v2/test/mcpCore/connection-manager.test.ts -t "flips connected stdio servers to failed":这次压测没有复现。之前一次全量里失败过,报 expected 'failed' to be 'connected'。
  3. pnpm exec vitest run packages/agent-core-v2/test/agent/fullCompaction/fullCompaction.test.ts:不会失败,但有两条用例每次各跑约 8.5 秒。

Root Cause

SSH 探测用例。 用例给平台探测一个真实的 300ms 时限,让假的 ssh 一直卡住,最后读假 ssh 写下的调用记录。假 ssh 是一个 Node 脚本,启动后才写记录。机器一忙,它还没被调度到写记录那一步,就被时限杀掉了,用例读记录文件时报 ENOENT。换成 /bin/sh 写的假 ssh 也没用:在同样的负载下量了 240 次,从启动到写记录,Node 脚本的中位数是 56ms,sh 是 7ms,但两者都有 6 次超过 300ms。长尾来自进程排不上调度,不是解释器启动慢。

MCP 崩溃用例。 夹具在传输层启动后过一段固定时间退出:连接管理器那条是 500ms,stdio 客户端那条是 50ms。传输层启动早于客户端完成握手和工具列表,所以握手慢于这个时间时,connect 返回之前子进程就已经退出了。

两条压缩失败用例。 同一个文件里的其他重试用例都用了假计时器,这两条没有。所以每次都真等 4 轮重试退避。

Code Changes

改动 说明
SSH 探测用例断言契约本身 原来断言记录正好是 ['uname -s'],这依赖假进程来得及写记录。现在断言两件事:一是 connect 失败的原因是探测超时(kind: 'timeout',报错含 SSH platform probe timed out);二是记录要么为空、要么只有一次 uname -s,绝不出现执行器命令。只有记录文件不存在时才视为空,其他读取错误照常抛出
MCP 夹具改为由测试触发退出 夹具不再按固定时间退出,改成等环境变量 KIMI_TEST_MCP_EXIT_WHEN_FILE 指定的文件出现再退出。两条用例都先确认已经连上,再创建这个文件。stdio 客户端那条还新增断言:触发之前没有收到关闭证据
压缩失败用例改用假计时器 照同文件其他重试用例的写法:等第一次请求失败后,把假时间推进 60 秒。断言不变,包括 retry_count: 4
为什么 SSH 探测用例改断言不算削弱

这条用例要证明两件事:卡住的探测会被时限截断;截断后不会去启动执行器。

  • 第一件:原来只断言 kind: 'timeout'。可是执行器初始化超时的 kind 也是 timeout,光靠 kind 分不清是哪一步超时,只能再靠记录文件。现在加上报错文本,能直接确认是探测超时。
  • 第二件:记录里出现任何执行器命令,或者出现第二次探测,用例都会失败。只有在记录还没写出来时,才会读到空列表。
  • 变强的地方:临时把产品代码改成「探测超时后退回 POSIX、继续启动执行器」,新断言因报错文本不符而稳定失败。原来的断言要靠记录文件才能发现这种情况,而记录文件恰恰可能来不及写出。
阅读指南

三个提交各改一处,彼此独立。MCP 那个提交同时改了夹具 test/mcpCore/fixtures/crash-after-connect-stdio-server.mjs 和使用它的两个测试文件。原来的 KIMI_TEST_MCP_EXIT_AFTER_MS 只有这两条用例在用,已经删除。

Behavior Changes and Affected Users

None。产品代码、配置和契约文件都没有改动,只影响 agent-core-v2 的测试。

没有 changeset:只改了测试。

验证

只在 macOS 上验证过,Linux 和 Windows 交给 CI。SSH 探测那组用例在 Windows 上本来就跳过。MCP 夹具改用文件信号,而不用 SIGUSR2 这类信号,就是为了在 Windows 上也能用。

  • 负载对比(6 份副本同时跑,另有 6 个吃满 CPU 的进程):
    • SSH 探测:修改前 18 次失败 2 次,修改后 36 次 0 失败。
    • MCP 崩溃用例:两条各 24 次,0 失败。连接管理器那条在负载下最慢一次,从 1.1 秒降到 0.2 秒。
    • 两条压缩失败用例:从各约 8.5 秒降到约 0.3 秒。
  • 变异检验:临时改坏产品代码,让探测超时后继续启动执行器,SSH 探测用例失败,产品代码已还原。
  • 四个改动的测试文件 211 条全部通过。
  • pnpm lint 通过,固定等待计数没有增加。agent-core-v2 类型检查通过,pnpm run check:duplication 没有新增重复。

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.

…cases

Both cases make every compaction attempt fail with a retryable error and
assert what happens once the retries are exhausted. Unlike the other
retry cases in the file they ran on real timers, so each sat through four
rounds of real backoff, about 8.5 s per case.

Use fake timers and advance past the backoff the way the neighbouring
retry cases do. The assertions, including retry_count: 4, are unchanged;
the two cases now take about 0.3 s each.
…ssh startup

The case gives the platform probe a real 300 ms budget against a fake ssh
that stalls, then read the fake's call log. Under load the fake can be
killed by the timeout before it is scheduled far enough to write that
log, and the case failed reading a missing file (2 of 18 runs with six
copies and six CPU burners). A /bin/sh fake does not help: its median
start is far lower, but the long tail that loses the race is scheduling,
not interpreter start-up.

Assert what the case is about instead of the fake's progress: the
connect fails because the probe timed out, which the error message now
pins (the executor's initialize timeout has the same kind), and the log
holds at most the one uname probe and never an executor launch. A
mutation that falls back to POSIX and launches the executor after a probe
timeout still fails the case. 36 of 36 runs pass under the same load.
…exits

The crash-after-connect fixture exited a fixed delay after its transport
started, which is before the client finishes the initialize handshake
and the tool listing. When the handshake took longer than the delay
(500 ms in the connection manager case, 50 ms in the stdio client case),
connect returned with the server already failed.

The fixture now exits once the test creates a signal file, and both
cases create it only after they have observed the connected state. The
assertions are unchanged; the client case also checks that no close
evidence arrived before the signal.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 506045e

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 26, 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-26T04:41:42.526355Z 506045e PR opened
🔒 Security Review ✅ Completed 2026-11-26T04:43:05.299101Z 506045e 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 26, 2026

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

commit: 506045e

@RealKai42
RealKai42 merged commit c80bb2d into main Nov 26, 2026
21 checks passed
@RealKai42
RealKai42 deleted the test/race-fixes branch November 26, 2026 05:20
RealKai42 added a commit that referenced this pull request Nov 26, 2026
#5474 landed on main as one squashed commit, so the prompt cache files
this branch carried from it now come from main as merged, including the
review fixes made after this branch forked.

Two main changes met the MCP migration:

- #5486 made the crash-after-connect fixture exit when the test creates
  a signal file instead of after a fixed delay. The fixture is gone, so
  the testkit's exitAfterConnect now takes whenFile in place of afterMs,
  crashAfterConnectServer passes it on, and both crash cases keep the
  signal they create once they have seen the connected state.
- #5488 added hostPathClass to the MCP stubs for Windows path classes.
  The stubs keep it beside the testkit-based servers.
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