fix(hopper): verify lease cleanup and repair the macOS test lane (#1111)

* fix(hopper): distinguish shared lease roots from leaked sessions

* fix(ci): follow the moved process CLI regression tests
This commit is contained in:
Ysopwnable
2026-10-09 02:41:34 +08:00
committed by GitHub
parent ec6db0f063
commit b743bd20a0
6 changed files with 253 additions and 46 deletions
@@ -0,0 +1,103 @@
import { mkdir, mkdtemp, readdir, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, onTestFinished } from "vitest";
import { snapshotHopperRuntime } from "../../../../scripts/lib/real-hopper-cleanup.mjs";
import { acquireHopperTargetLease } from "../../../../src/hopper/HopperTargetLease.js";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
it.skipIf(process.platform === "win32")(
"does not classify a released shared lease directory as a session leak",
async () => {
const parent = await createLeaseWorkspace();
const directory = join(parent, "rea-hopper-501");
const targetPath = join(parent, "target");
await writeFile(targetPath, "source-owned lease target");
const before = await snapshotHopperRuntime(parent, directory);
const acquired = await acquireHopperTargetLease({
targetPath,
targetKind: "executable",
loaderArgs: [],
runId: "released-verifier-lease",
directory,
});
if (!acquired.acquired) throw new Error("Expected the owned target lease");
await acquired.lease.release();
expect(await readdir(directory)).toEqual([]);
expect(await snapshotHopperRuntime(parent, directory)).toEqual(before);
},
);
it.skipIf(process.platform === "win32")(
"retains an unreleased real target-lease socket in cleanup observations",
async () => {
const parent = await createLeaseWorkspace();
const directory = join(parent, "rea-hopper-501");
const targetPath = join(parent, "target");
await writeFile(targetPath, "source-owned lease target");
const acquired = await acquireHopperTargetLease({
targetPath,
targetKind: "executable",
loaderArgs: [],
runId: "retained-verifier-lease",
directory,
});
if (!acquired.acquired) throw new Error("Expected the owned target lease");
try {
const children = await readdir(directory);
expect(children).toHaveLength(1);
expect(await snapshotHopperRuntime(parent, directory)).toEqual(
new Set(children.map((name) => join(directory, name))),
);
} finally {
await acquired.lease.release();
}
},
);
it("reports retained sessions belonging to observed MCP processes", async () => {
const parent = await createTestTempDirectory("rea-cleanup-retained-session-");
const directory = join(parent, "rea-hopper-501");
const previous = join(parent, "rea-existing-session");
await mkdir(previous);
const before = await snapshotHopperRuntime(parent, directory);
const retained = join(parent, "rea-retained-session");
const unrelated = join(parent, "rea-unrelated-session");
const unknown = join(parent, "rea-no-ownership-record");
for (const path of [retained, unrelated, unknown]) await mkdir(path);
await writeFile(join(retained, "bootstrap.py"), "source-owned fixture");
await writeFile(
join(retained, "ownership.json"),
JSON.stringify({ parent_pid: process.pid }),
);
await writeFile(
join(unrelated, "ownership.json"),
JSON.stringify({ parent_pid: process.pid + 1 }),
);
const after = await snapshotHopperRuntime(
parent,
directory,
new Set([process.pid]),
);
expect([...after].filter((path) => !before.has(path))).toEqual([retained]);
});
it("reports an unexpected file at the shared lease root", async () => {
const parent = await createTestTempDirectory("rea-cleanup-invalid-lease-");
const directory = join(parent, "rea-hopper-501");
await writeFile(directory, "unexpected lease root file");
expect(await snapshotHopperRuntime(parent, directory)).toEqual(
new Set([directory]),
);
});
const createLeaseWorkspace = async (): Promise<string> => {
// Unix socket paths must fit the host sockaddr_un limit, independently of
// the test runner's potentially long redirected temporary directory.
const parent = await mkdtemp("/tmp/rhv-");
onTestFinished(() => rm(parent, { recursive: true, force: true }));
return parent;
};
+48 -14
View File
@@ -4,23 +4,48 @@ import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { parse } from "yaml";
import { z } from "zod";
const WORKFLOW = join(".github", "workflows", "ci.yml");
const curatedPaths = async (): Promise<string[]> => {
const workflow = parse(await readFile(WORKFLOW, "utf8")) as {
jobs?: Record<string, { steps?: { run?: string }[] }>;
};
const run = workflow.jobs?.["windows-curated"]?.steps
?.map((step) => step.run)
.find((value) => value?.includes("npx vitest run"));
expect(run, "expected a curated Windows vitest invocation").toBeDefined();
return (
run
?.split(/\s+/)
.filter((token) => token.endsWith(".test.ts"))
.sort() ?? []
const workflowSchema = z.object({
jobs: z.record(
z.string(),
z.object({
steps: z.array(z.object({ run: z.string().optional() })).optional(),
}),
),
});
const explicitPaths = async (
lane: "windows" | "focused",
): Promise<string[]> => {
const workflow = workflowSchema.parse(
parse(await readFile(WORKFLOW, "utf8")),
);
const runs =
lane === "windows"
? (workflow.jobs["windows-curated"]?.steps
?.map((step) => step.run)
.filter((value) => value?.includes("npx vitest run")) ?? [])
: Object.values(workflow.jobs)
.flatMap((job) => job.steps ?? [])
.map((step) => step.run)
.filter((value) =>
value?.trim().startsWith("npm run test:focused -- "),
);
expect(
runs.length,
`expected explicit ${lane} CI test invocations`,
).toBeGreaterThan(0);
return [
...new Set(
runs.flatMap(
(run) =>
run?.split(/\s+/).filter((token) => token.endsWith(".test.ts")) ?? [],
),
),
].sort();
};
const exists = async (path: string): Promise<boolean> =>
@@ -31,7 +56,7 @@ const exists = async (path: string): Promise<boolean> =>
describe("curated Windows test lane", () => {
it("references only test files that exist", async () => {
const paths = await curatedPaths();
const paths = await explicitPaths("windows");
expect(paths.length).toBeGreaterThan(0);
const missing = await Promise.all(
paths.map(async (path) => ((await exists(path)) ? undefined : path)),
@@ -39,3 +64,12 @@ describe("curated Windows test lane", () => {
expect(missing.filter((path) => path !== undefined)).toEqual([]);
}, 10_000);
});
it("keeps every explicit focused CI test path executable after file moves", async () => {
const paths = await explicitPaths("focused");
expect(paths.length).toBeGreaterThan(0);
const missing = await Promise.all(
paths.map(async (path) => ((await exists(path)) ? undefined : path)),
);
expect(missing.filter((path) => path !== undefined)).toEqual([]);
}, 10_000);