A local-first multi-agent app development platform, built on PenguinHarness
test(core): retry the hook tests' teardown while Windows still holds the directory (#711)
Yaowei Zhengcc00d2b7c74d
diff --git a/changelog/unreleased/2026-09-12-windows-hook-temp-lock.md b/changelog/unreleased/2026-09-12-windows-hook-temp-lock.mdnew file mode 100644index 00000000..83101066--- /dev/null+++ b/changelog/unreleased/2026-09-12-windows-hook-temp-lock.md@@ -0,0 +1,30 @@+# The hook tests stop failing on Windows when a script is still exiting++- **Date:** 2026-09-12+- **Type:** process+- **Scope:** `core`, `ci`++[中文版](2026-09-12-windows-hook-temp-lock.zh.md)++`test-windows (core)` failed intermittently in `hooks.test.ts` with `EBUSY: resource busy or+locked, rmdir`, taking whole runs red on branches that had not touched hooks at all. The teardown+was racing the operating system rather than the code under test, and the retry CI already performs+could not help, because the same race is there on the second attempt.++## Details++- Windows holds a directory locked while any live process is still using it, including a script it+ has not finished exiting from, and answers `rmdir` with `EBUSY`, `EPERM` or `ENOTEMPTY` until+ that process is gone. The five hook suites write scripts into a temporary directory, run them,+ and remove the directory as soon as the assertions pass — which can be before the last child has+ exited. The case that failed is the one that runs a hook to its timeout, so a child outliving the+ test is precisely what it arranges.+- POSIX unlinks a busy directory's entries without complaint, so nothing about this is visible on+ Linux or macOS. It appeared only in CI, and only sometimes.+- The five teardowns now retry to a deadline instead of removing once. A directory that is+ genuinely stuck still fails, one deadline later, with the error it actually got rather than a+ swallowed one.+- The retry itself already existed, written for the same reason in the MCP tests, where a stdio+ server's child holds its working directory. It moved to `packages/core/test/rm-eventually.ts` so+ both suites share one copy: a second hand-written version is how one of them ends up fixed and+ the other left racing.diff --git a/changelog/unreleased/2026-09-12-windows-hook-temp-lock.zh.md b/changelog/unreleased/2026-09-12-windows-hook-temp-lock.zh.mdnew file mode 100644index 00000000..2c7fdae4--- /dev/null+++ b/changelog/unreleased/2026-09-12-windows-hook-temp-lock.zh.md@@ -0,0 +1,25 @@+# Windows 上脚本尚未退出时,hook 测试不再失败++- **Date:** 2026-09-12+- **Type:** process+- **Scope:** `core`, `ci`++[English](2026-09-12-windows-hook-temp-lock.md)++`test-windows (core)` 会在 `hooks.test.ts` 中间歇性地以 `EBUSY: resource busy or locked, rmdir`+失败,连带把整次运行判红——哪怕该分支根本没有改动 hook。这里的清理步骤争抢的是操作系统,而不是被测+代码,因此 CI 已有的那次重试也救不了:第二次运行时同一个竞态依然在。++## 细节++- 只要仍有活着的进程在使用某个目录(包括它尚未退出完毕的脚本),Windows 就会锁住该目录,`rmdir`+ 会返回 `EBUSY`、`EPERM` 或 `ENOTEMPTY`,直到该进程消失为止。五个 hook 测试套件都会把脚本写进一个+ 临时目录、运行它,然后在断言一通过就删除该目录——而此时最后一个子进程未必已经退出。真正失败的那个+ 用例正是把 hook 跑到超时的那个,也就是说「子进程活得比测试久」恰恰是它刻意安排的。+- POSIX 删除忙碌目录中的条目时不会有任何抱怨,所以这件事在 Linux 与 macOS 上根本看不见。它只出现在+ CI 上,而且只是偶尔出现。+- 这五处清理现在改为按时限重试,而不是只删一次。真正卡住的目录仍然会失败,只是晚一个时限,并且带着+ 它实际拿到的那个错误,而不是被吞掉的错误。+- 这段重试本来就已存在,当初是为同样的原因写在 MCP 测试里的——那里是 stdio server 的子进程占着自己的+ 工作目录。现在它移到 `packages/core/test/rm-eventually.ts`,两套测试共用同一份:手写第二份,正是+ 其中一份被修好、另一份继续踩坑的由来。diff --git a/packages/core/test/hooks.test.ts b/packages/core/test/hooks.test.tsindex 36585b7b..e992293d 100644--- a/packages/core/test/hooks.test.ts+++ b/packages/core/test/hooks.test.ts@@ -2,6 +2,7 @@ import fs from "node:fs/promises";import os from "node:os";import path from "node:path";import { afterEach, beforeEach, describe, expect, it } from "vitest";+import { rmEventually } from "./rm-eventually.js";import {Session,assistantText,@@ -135,7 +136,7 @@ describe("script hooks", () => {dir = await fs.mkdtemp(path.join(os.tmpdir(), "penguin-script-hook-"));});afterEach(async () => {- await fs.rm(dir, { recursive: true, force: true });+ await rmEventually(dir);});const write = async (name: string, body: string): Promise<string> => {@@ -229,7 +230,7 @@ describe("script pre-tool-use hooks", () => {dir = await fs.mkdtemp(path.join(os.tmpdir(), "penguin-tool-script-"));});afterEach(async () => {- await fs.rm(dir, { recursive: true, force: true });+ await rmEventually(dir);});it("scriptPreToolUseHook feeds the call on stdin and narrows the parsed answer", async () => {@@ -273,7 +274,7 @@ describe("Session user-prompt hook", () => {dir = await fs.mkdtemp(path.join(os.tmpdir(), "penguin-prompt-hook-"));});afterEach(async () => {- await fs.rm(dir, { recursive: true, force: true });+ await rmEventually(dir);});it("runs the named package's hook with the Session's own id and scratchpad, null when absent", async () => {@@ -327,7 +328,7 @@ describe("Session stop hooks", () => {dir = await fs.mkdtemp(path.join(os.tmpdir(), "penguin-hooks-"));});afterEach(async () => {- await fs.rm(dir, { recursive: true, force: true });+ await rmEventually(dir);});const fakeEnvironment: EnvironmentInterface = {@@ -542,7 +543,7 @@ describe("Session pre-tool-use hooks", () => {dir = await fs.mkdtemp(path.join(os.tmpdir(), "penguin-tool-hooks-"));});afterEach(async () => {- await fs.rm(dir, { recursive: true, force: true });+ await rmEventually(dir);});/** Turn 1 calls exec_command; turn 2 answers with the tool output it saw. */diff --git a/packages/core/test/mcp-tools.test.ts b/packages/core/test/mcp-tools.test.tsindex d8c5c53f..c5ee0732 100644--- a/packages/core/test/mcp-tools.test.ts+++ b/packages/core/test/mcp-tools.test.ts@@ -9,6 +9,7 @@ import { McpServer } from "@modelcontextprotocol/server";import { NodeStreamableHTTPServerTransport } from "@modelcontextprotocol/node";import { z } from "zod";import { Environment } from "../src/environment/index.js";+import { rmEventually } from "./rm-eventually.js";import { McpToolProvider, renderCallToolResult } from "../src/environment/mcp/provider.js";import { Session } from "../src/session.js";import {@@ -30,27 +31,6 @@ function fixtureEntry(extra: Record<string, unknown> = {}): MCPServerConfig {return { name: "fx", config: { command: process.execPath, args: [FIXTURE], ...extra } };}-/**- * Removes a test dir, retrying transient Windows locks: the dir is the stdio server child's- * cwd, `dispose()` kills that child fire-and-forget, and Windows keeps a directory locked- * (EBUSY/EPERM on rmdir) while it is any live process's working directory — the lock lifts- * once the child finishes exiting. A genuinely stuck dir still fails, one deadline later.- */-async function rmEventually(dir: string, timeoutMs = 20_000): Promise<void> {- const deadline = Date.now() + timeoutMs;- for (;;) {- try {- await rm(dir, { recursive: true, force: true });- return;- } catch (err) {- const code = (err as NodeJS.ErrnoException).code;- const transient = code === "EBUSY" || code === "ENOTEMPTY" || code === "EPERM";- if (!transient || Date.now() > deadline) throw err;- await new Promise((resolve) => setTimeout(resolve, 200));- }- }-}-async function collect(gen: AsyncGenerator<OmniMessage>): Promise<OmniMessage[]> {const out: OmniMessage[] = [];for await (const msg of gen) out.push(msg);diff --git a/packages/core/test/rm-eventually.ts b/packages/core/test/rm-eventually.tsnew file mode 100644index 00000000..f546bf59--- /dev/null+++ b/packages/core/test/rm-eventually.ts@@ -0,0 +1,32 @@+/**+ * Removing a test directory that a child process has not finished letting go of.+ *+ * Windows keeps a directory locked while any live process has it open — as a working+ * directory, or as the image of a script it is still running — and answers `rmdir` with+ * EBUSY, EPERM or ENOTEMPTY until that process finishes exiting. Tests that spawn a child+ * and then tear down its directory therefore race the operating system rather than their+ * own code: the assertions have all passed by the time the teardown fails, and the failure+ * lands on whichever test happened to be last. POSIX unlinks a busy directory's entries+ * without complaint, so the race is invisible on Linux and macOS and shows up only in CI.+ *+ * Retrying to a deadline is the fix rather than ignoring the error: a directory that is+ * genuinely stuck — a process that never exits, a handle nothing releases — still fails,+ * one deadline later, with the original error rather than a swallowed one.+ */+import { rm } from "node:fs/promises";++/** Removes `dir`, retrying while Windows still holds it, then failing with the real error. */+export async function rmEventually(dir: string, timeoutMs = 20_000): Promise<void> {+ const deadline = Date.now() + timeoutMs;+ for (;;) {+ try {+ await rm(dir, { recursive: true, force: true });+ return;+ } catch (err) {+ const code = (err as NodeJS.ErrnoException).code;+ const transient = code === "EBUSY" || code === "ENOTEMPTY" || code === "EPERM";+ if (!transient || Date.now() > deadline) throw err;+ await new Promise((resolve) => setTimeout(resolve, 200));+ }+ }+}