Skip to content

test(agent-core-v2): keep the process-group and HEIC cases inside the timeout under load - #5476

Merged
RealKai42 merged 2 commits into
mainfrom
test/process-group-timers
Nov 26, 2026
Merged

RealKai42 merged 2 commits into
mainfrom
test/process-group-timers

Conversation

@RealKai42

Copy link
Copy Markdown
Collaborator

Requirement or Bug

接着 #5458,继续清理 agent-core-v2 全量测试里在 main 上也会失败的用例。这个 PR 修两处:remote/process.test.ts 的进程组缓存上限用例,以及 read-media.test.ts 的两条 HEIC 用例。它们单独跑能过,全量负载下会超过 30 秒的用例时限。

Bug Reproduction Steps

  1. 同时运行 6 份 pnpm exec vitest run packages/agent-core-v2/test/remote/process.test.ts -t "finished process-group ownership",另开 6 个吃满 CPU 的进程。缓存上限用例最慢一次 23.8 秒,离时限只剩不到 30%。在 agent-core-v2 全量里它会超时,紧接着的 does not retain POSIX process groups for a Windows executor 跟着失败,报 process.kill 被调用了上千次。
  2. 同样的负载下运行 read-media.test.ts -t "preserves a HEIC above":两条用例最慢一次 15.5 秒,全量里 40–58 秒。

Root Cause

进程组用例。 用例为 4097 个已退出的进程组各排一个 5 秒的回收计时器,然后用 vi.advanceTimersByTimeAsync 推进假时间。异步推进每触发一个计时器都要让出一次真实的事件循环,4097 个计时器就是 4097 次让出,机器一忙耗时就会成倍增长。超时后,这次异步推进还在后台继续触发旧计时器,下一条用例刚装上的 process.kill 监视就收到了这些调用,所以下一条是被连累的。

HEIC 用例。 工具执行只要几毫秒,时间都花在最后的 expect(Buffer.from(stored!.bytes)).toEqual(heic) 上。Vitest 的深度比较会逐个元素地遍历 typed array,单独测一个 5 MB 的 Buffer:toEqual 要 4.1 秒,Buffer.compare 和 equals 各约 1 毫秒。

两处都是测试侧的根本修复:去掉了与被测契约无关的开销,产品代码没有改动。

Code Changes

改动 说明
进程组用例改成同步推进假时间 两处 await vi.advanceTimersByTimeAsync(5_000) 改成 vi.advanceTimersByTime(5_000)。ProcessManager.scheduleGroupReap 的回调是同步的:查一次进程组,进程组不存在就删除,否则重排计时器。同步推进触发的计时器和顺序都不变,断言也不变。同步推进在返回前跑完,不会在用例结束后还留在后台运行
HEIC 用例改用长度加 Buffer.compare 先断言字节长度,再断言 Buffer.compare(...) === 0,和 image-originals.test.ts、sessionMediaStore.test.ts 的写法一致。被测的是「超出预算的 HEIC 原样保存」,最可能的回归是截断或压缩,长度不一致时报错会直接给出两个长度
阅读指南

两处改动彼此独立,各一个提交,每个提交只改一个测试文件的两到四行。进程组用例的回调见 src/remote/server/processManager.ts 的 scheduleGroupReap。

Behavior Changes and Affected Users

None。产品代码、配置和契约文件都没有改动;两个测试文件的用例数和要证明的契约也没变。

没有 changeset:只改了测试。

验证

只在 macOS 上验证过,Linux 和 Windows 交给 CI。两处改动不涉及路径、子进程或平台分支。

  • 负载对比用的是同一个脚本:6 份副本同时跑,另有 6 个吃满 CPU 的进程,每组各 12 次。

    用例 修改前最慢 修改后最慢
    does not evict live groups at the former cache limit and reaps missing groups 23.8 秒 9.0 秒
    preserves a HEIC above the former provider inline budget 15.5 秒 18 毫秒
    preserves a HEIC above the read budget for deferred inline resolution 10.1 秒 18 毫秒
  • 进程组用例剩下的 9 秒,来自假计时器库每触发一个计时器都要扫描全部计时器的开销。这条用例要证明的正是「超过原来的缓存上限 4096 也不会淘汰」,所以没有减少计时器数量。

  • HEIC 的新断言仍然能抓到回归。临时改坏 readMediaFileTool.ts 里存入的字节后,两条用例都失败了,产品代码已还原:

    • 少存 1 个字节:报 expected 4194327 to be 4194328。
    • 长度不变、改掉最后 1 个字节:Buffer.compare 的断言失败。
  • 扫过 test/agent/media 下全部 345 条用例,其他对 Buffer 用 toEqual 的地方数据都很小,或者已经在用 .equals。修改后这个目录里最慢的用例是 1.8 秒。

  • remote/process.test.ts 23 条通过;read-media.test.ts 60 条通过,用例执行时间从约 10 秒降到 1.7 秒。

  • 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.

…s-group reap timers

The cache-limit case schedules one 5 s reap timer per exited group and
advanced fake time with advanceTimersByTimeAsync, which yields to the real
event loop after every one of the 4097 timers. Idle that took about 7 s;
with six copies and six CPU burners it reached 24 s, and the full suite
pushed it past the 30 s test timeout.

When it timed out, the async advance kept firing the old timers in the
background, so the next case's process.kill spy saw thousands of calls
and failed too.

The reap callback is synchronous, so advanceTimersByTime fires the same
timers in the same order with the same assertions. Under the same load the
slowest run drops to 9 s, and a sync advance cannot outlive its test.
…e deep equality

Two read-media cases check that a 4 MB and a 5 MB HEIC are stored
untouched, and did it with toEqual on the Buffers. Vitest's deep equality
walks typed arrays element by element: on 5 MB that alone takes about 4 s
idle, 15 s with six copies and six CPU burners, and 40-58 s in the full
suite, past the 30 s test timeout. The tool itself finishes in a few
milliseconds.

Assert the byte length, then Buffer.compare, as the other media tests
already do. A truncated store now reports both sizes, and a changed byte
still fails. Under the same load the slowest run drops to 18 ms.
@changeset-bot

changeset-bot Bot commented Nov 26, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 16eb510

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-26T02:57:15.825319Z 16eb510 PR opened
🔒 Security Review ✅ Completed 2026-11-26T03:04:28.464219Z 16eb510 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@16eb510
npx https://pkg.pr.new/@moonshot-ai/kimi-code@16eb510

commit: 16eb510

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