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:
@@ -0,0 +1,6 @@
|
||||
/** Observe concrete Hopper runtime resources for a before/after cleanup check. */
|
||||
export function snapshotHopperRuntime(
|
||||
parent: string,
|
||||
targetLeaseDirectory: string,
|
||||
ownedProcessIds?: ReadonlySet<number>,
|
||||
): Promise<Set<string>>;
|
||||
@@ -0,0 +1,52 @@
|
||||
import { lstat, readFile, readdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
/** Observe session paths and target leases without treating their shared parent as a session. */
|
||||
export async function snapshotHopperRuntime(
|
||||
parent,
|
||||
targetLeaseDirectory,
|
||||
ownedProcessIds,
|
||||
) {
|
||||
const resources = new Set();
|
||||
for (const entry of await readdir(parent, { withFileTypes: true })) {
|
||||
const path = join(parent, entry.name);
|
||||
if (!entry.name.startsWith("rea-") || path === targetLeaseDirectory)
|
||||
continue;
|
||||
if (
|
||||
ownedProcessIds !== undefined &&
|
||||
(!entry.isDirectory() ||
|
||||
!(await belongsToOwnedProcess(path, ownedProcessIds)))
|
||||
)
|
||||
continue;
|
||||
resources.add(path);
|
||||
}
|
||||
const leaseRoot = await lstat(targetLeaseDirectory).catch(missingIsAbsent);
|
||||
if (leaseRoot === undefined) return resources;
|
||||
if (!leaseRoot.isDirectory() || leaseRoot.isSymbolicLink()) {
|
||||
resources.add(targetLeaseDirectory);
|
||||
return resources;
|
||||
}
|
||||
const leases = await readdir(targetLeaseDirectory).catch(missingIsAbsent);
|
||||
for (const name of leases ?? [])
|
||||
resources.add(join(targetLeaseDirectory, name));
|
||||
return resources;
|
||||
}
|
||||
|
||||
const missingIsAbsent = (cause) => {
|
||||
if (cause?.code === "ENOENT") return undefined;
|
||||
throw cause;
|
||||
};
|
||||
|
||||
const belongsToOwnedProcess = async (path, ownedProcessIds) => {
|
||||
const encoded = await readFile(join(path, "ownership.json"), "utf8").catch(
|
||||
missingIsAbsent,
|
||||
);
|
||||
if (encoded === undefined) return false;
|
||||
const ownership = JSON.parse(encoded);
|
||||
return (
|
||||
ownership !== null &&
|
||||
typeof ownership === "object" &&
|
||||
Number.isSafeInteger(ownership.parent_pid) &&
|
||||
ownedProcessIds.has(ownership.parent_pid)
|
||||
);
|
||||
};
|
||||
@@ -1,7 +1,5 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { readFile, readdir } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
@@ -14,6 +12,7 @@ import {
|
||||
requirePseudocode,
|
||||
} from "../dist/application/RealHopperAssertions.js";
|
||||
import { HOPPER_PROVIDER_IDENTITY } from "../dist/hopper/HopperProvider.js";
|
||||
import { HOPPER_TARGET_LEASE_DIRECTORY } from "../dist/hopper/HopperTargetLease.js";
|
||||
import { REA_WORKFLOW_PROVIDER } from "../dist/application/InvestigationProviders.js";
|
||||
import { loadRealHopperFixtureTargets } from "./lib/real-hopper-fixture.mjs";
|
||||
import {
|
||||
@@ -40,6 +39,7 @@ import {
|
||||
verifyHopperLifecycleAndCli,
|
||||
} from "./lib/real-hopper-boundaries.mjs";
|
||||
import { startUnrelatedHopperSentinel } from "./lib/unrelated-hopper-sentinel.mjs";
|
||||
import { snapshotHopperRuntime } from "./lib/real-hopper-cleanup.mjs";
|
||||
const execFileAsync = promisify(execFile);
|
||||
const verifierRun = createVerifierRun();
|
||||
const timeout = 180_000;
|
||||
@@ -54,7 +54,10 @@ const parseServerArgs = (encoded) => {
|
||||
};
|
||||
|
||||
const runtimeParent = process.platform === "darwin" ? "/tmp" : tmpdir();
|
||||
const sessionsBefore = new Set(await readdir(runtimeParent));
|
||||
const sessionsBefore = await snapshotHopperRuntime(
|
||||
runtimeParent,
|
||||
HOPPER_TARGET_LEASE_DIRECTORY,
|
||||
);
|
||||
const ownedProcessIds = new Set();
|
||||
const textValue = mcpTextValue;
|
||||
const requireSuccessfulTool = requireMcpResult;
|
||||
@@ -519,26 +522,25 @@ try {
|
||||
}
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
for (const entry of await readdir(runtimeParent, { withFileTypes: true })) {
|
||||
if (
|
||||
!entry.isDirectory() ||
|
||||
!entry.name.startsWith("rea-") ||
|
||||
sessionsBefore.has(entry.name)
|
||||
)
|
||||
continue;
|
||||
let ownership;
|
||||
try {
|
||||
ownership = JSON.parse(
|
||||
await readFile(join(runtimeParent, entry.name, "ownership.json"), "utf8"),
|
||||
);
|
||||
} catch (cause) {
|
||||
if (cause.code === "ENOENT") continue;
|
||||
throw cause;
|
||||
}
|
||||
if (ownedProcessIds.has(ownership.parent_pid))
|
||||
throw new Error(
|
||||
`An owned Hopper runtime leaked its directory: ${entry.name}`,
|
||||
);
|
||||
const sessionsAfter = [
|
||||
...(await snapshotHopperRuntime(
|
||||
runtimeParent,
|
||||
HOPPER_TARGET_LEASE_DIRECTORY,
|
||||
ownedProcessIds,
|
||||
)),
|
||||
]
|
||||
.filter((path) => !sessionsBefore.has(path))
|
||||
.sort();
|
||||
if (sessionsAfter.length > 0) {
|
||||
await writeVerificationReport({
|
||||
...summary,
|
||||
unrelatedHopperSurvived,
|
||||
cleanShutdown: false,
|
||||
retainedRuntimePaths: sessionsAfter,
|
||||
});
|
||||
throw new Error(
|
||||
`The MCP runtime retained Hopper resources: ${sessionsAfter.join(", ")}`,
|
||||
);
|
||||
}
|
||||
if (summary === undefined)
|
||||
throw new Error("Real-Hopper verification did not produce a summary");
|
||||
@@ -550,12 +552,18 @@ if (
|
||||
throw new Error(
|
||||
"The verifier's process lineage did not prove owned child cleanup",
|
||||
);
|
||||
await new Promise((resolve, reject) => {
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({ verifier_run: completedVerifierRun, ...summary, unrelatedHopperSurvived, cleanShutdown: true }, null, 2)}\n`,
|
||||
(cause) => {
|
||||
await writeVerificationReport({
|
||||
verifier_run: completedVerifierRun,
|
||||
...summary,
|
||||
unrelatedHopperSurvived,
|
||||
cleanShutdown: true,
|
||||
});
|
||||
|
||||
async function writeVerificationReport(report) {
|
||||
await new Promise((resolve, reject) => {
|
||||
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`, (cause) => {
|
||||
if (cause) reject(cause);
|
||||
else resolve();
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user