Files
reverse-engineering-anything/src/desktop/DesktopBuildDoctorFacts.test.ts
T
morluto 428e006f66 fix(filesystem): close file handles only through the owned-handle owners (#10578)
* fix(filesystem): close file handles only through the owned-handle owners

Three sites still closed a fs/promises FileHandle in a finally block, where a
failed close replaced the operation's own failure or result: the agentenv
runner head read, the Linux cgroup kernel-file read and the JavaScript
analysis transfer writer. They now use withOwnedFileHandle, which retains the
operation outcome alongside a failed close.

A lint rule, rea/no-raw-file-handle-close, refuses close() on a handle opened
from fs/promises or declared as FileHandle, and fs close/closeSync on a
descriptor, in src outside src/filesystem/Owned*.

* fix(lint): distinguish owned handle class fields

* fix(filesystem): retain diagnostic file cleanup owners

* test(hopper): retry after confirmed application exit

* fix(filesystem): settle doctor peers, sample cgroup outside admission, and bind lint this

Desktop doctor waits for every client inspection before reporting a retained close failure. Ghidra memory cgroup sampling no longer passes the resource scope's admission gate, so a pending owner cannot mask the provider failure; failed closes are still retained for session cleanup. The raw FileHandle close rule resolves this through arrow functions only, treats static blocks as static, and keeps #name distinct from constructor parameter properties.

* fix(ghidra): join in-flight cgroup samples before session cleanup

ArtifactResourceScope.track runs optional work outside the admission gate while cleanup drains still join it, so a cancelled request's final retry cannot finish before a sample retains a failed close.

* fix(filesystem): join work tracked during a cleanup drain and report doctor file cleanup as typed errors

A cleanup retry now repeats until no tracked work is active and every retained owner has had one attempt, so diagnostic work admitted while an earlier owner is releasing is joined. Doctor reports a retained runner or configuration file close as a ProviderCleanupError bound to its lease instead of rethrowing a raw file failure, so the error document keeps the retained file.

* fix(desktop): read the agentenv runner head through the artifact file owner

The runner read retained a failed close under one lease while the reported failure carried another, so a successful cleanup retry left the doctor error reporting an already-closed file. withArtifactFileHandle binds one lease to both.
2026-11-15 18:08:56 +08:00

915 lines
29 KiB
TypeScript

import { execFile } from "node:child_process";
import { constants } from "node:fs";
import {
chmod,
mkdir,
open,
readFile,
rm,
symlink,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import { runDoctor, systemDoctorHost } from "../application/Doctor.js";
import { readDesktopBuildFacts } from "./DesktopBuildDoctorFacts.js";
import { ArtifactFileCleanupFailure } from "../artifacts/ArtifactFileOperation.js";
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
import type { ClientRegistrationStatus } from "../application/ClientRegistrationStatus.js";
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js";
import { PRODUCT_IDENTITY } from "../identity.js";
import {
defaultBuildComputerArchive,
resolveBuildComputerEnvironment,
} from "./BuildComputerEnvironment.js";
const execute = promisify(execFile);
const program = (path: string, body: string) =>
writeFile(path, `#!/bin/sh\n${body}\n`, { mode: 0o755 });
const configRunner = (path: string) =>
program(
path,
`
case "$1 $2" in
"config debug") printf '{"search_path":[{"path":"%s","exists":true}]}' "$AGENT_ENV_CONFIG" ;;
"config explain") printf '%s' "$FAKE_EXPLANATION" ;;
*) echo 'agent-env 0.9.1485' ;;
esac`,
);
const buildHost = async (prefix: string) => {
const root = await createTestTempDirectory(prefix);
const bin = join(root, "bin");
const home = join(root, "home");
await mkdir(bin);
await mkdir(home);
await program(join(bin, "ps"), "exit 0");
return { root, bin, home };
};
const readFacts = async (
environment: NodeJS.ProcessEnv,
clientIds: readonly string[],
) => {
const host = systemDoctorHost({
environment,
platform: "linux",
architecture: "x64",
desktopBuildFacts: readDesktopBuildFacts,
});
try {
return await host.desktopBuildFacts!(clientIds);
} finally {
await host.close?.();
}
};
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
"keeps disk and archive permission failures distinct from missing paths",
async () => {
const root = await createTestTempDirectory("rea-doctor-disk-permission-");
const docker = join(root, "docker");
const denied = join(root, "denied");
const target = join(denied, "docker");
const archive = join(denied, "runtime.tar.xz");
await program(docker, "exit 0");
await mkdir(target, { recursive: true });
await writeFile(archive, "unreadable archive");
const resources = new ArtifactResourceScope();
const factsAt = (directory: string, selectedArchive: string) =>
readDesktopBuildFacts({
environment: {
HOME: root,
PATH: "",
REA_DOCKER_COMMAND: docker,
REA_BUILD_COMPUTER_REA_ARCHIVE: selectedArchive,
},
platform: process.platform,
resources,
signal: new AbortController().signal,
run: async (_command, args) => {
const stdout = args[0] === "info" ? `${directory}\n` : "";
return {
stdout,
stderr: "",
stdoutBytes: Buffer.byteLength(stdout),
stderrBytes: 0,
};
},
clientIds: [],
registrations: async () => [],
});
await chmod(denied, 0);
try {
const facts = await factsAt(target, archive);
expect(facts.disk).toMatchObject({
path: target,
error: expect.stringContaining("EACCES"),
});
expect(facts.disk).not.toHaveProperty("freeBytes");
expect(facts.archive).toMatchObject({
path: archive,
status: "unknown",
detail: expect.stringContaining("EACCES"),
});
const missingArchive = join(root, "missing", "runtime.tar.xz");
const missing = await factsAt(
join(root, "missing", "docker"),
missingArchive,
);
expect(missing.disk).toMatchObject({
path: root,
freeBytes: expect.any(Number),
});
expect(missing.disk).not.toHaveProperty("error");
expect(missing.archive).toMatchObject({
path: missingArchive,
status: "missing",
});
} finally {
await chmod(denied, 0o700);
await resources.close();
}
},
);
it.skipIf(process.platform === "win32")(
"refuses a FIFO model configuration without waiting for a writer",
async () => {
const root = await createTestTempDirectory("rea-doctor-fifo-config-");
const fifo = join(root, "config.toml");
const runner = join(root, "agent-env");
await execute("/usr/bin/mkfifo", [fifo]);
await configRunner(runner);
const resources = new ArtifactResourceScope();
const controller = new AbortController();
const pending = readDesktopBuildFacts({
environment: {
HOME: root,
PATH: "",
AGENT_ENV_CONFIG: fifo,
REA_AGENT_ENV_COMMAND: runner,
},
platform: process.platform,
resources,
signal: controller.signal,
run: async (_command, args) => {
if (args[0] === "--version")
return {
stdout: "agent-env 0.9.1485",
stderr: "",
stdoutBytes: 19,
stderrBytes: 0,
};
if (args[1] !== "debug")
throw new Error("config explanation must not run for a FIFO");
const stdout = JSON.stringify({
search_path: [{ path: fifo, exists: true }],
});
return {
stdout,
stderr: "",
stdoutBytes: stdout.length,
stderrBytes: 0,
};
},
clientIds: [],
registrations: async () => [],
});
let timer: ReturnType<typeof setTimeout> | undefined;
try {
const facts = await Promise.race([
pending,
new Promise<never>((_, reject) => {
timer = setTimeout(
() => reject(new Error("FIFO read blocked")),
2_000,
);
}),
]);
expect(facts.solveEndpoint).toBeNull();
} finally {
if (timer !== undefined) clearTimeout(timer);
controller.abort();
const writer = await open(
fifo,
constants.O_WRONLY | constants.O_NONBLOCK,
).catch(() => undefined);
await writer?.close();
await Promise.allSettled([pending]);
await resources.close();
}
},
);
it("propagates cancellation before reading a model configuration", async () => {
const root = await createTestTempDirectory("rea-doctor-cancel-config-");
const config = join(root, "config.toml");
await writeFile(config, "[model]\nname = 'example'\n");
const resources = new ArtifactResourceScope();
const controller = new AbortController();
const reason = new Error("cancel doctor");
try {
const failure: unknown = await readDesktopBuildFacts({
environment: { HOME: root, PATH: "", AGENT_ENV_CONFIG: config },
platform: process.platform,
resources,
signal: controller.signal,
run: async () => {
throw new Error("unexpected command");
},
clientIds: [],
registrations: async () => {
controller.abort(reason);
return [];
},
}).catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(AnalysisCancelledError);
expect(failure).toMatchObject({ cause: reason });
} finally {
await resources.close();
}
});
it.skipIf(process.platform === "win32")(
"reads a symlinked regular model configuration",
async () => {
const root = await createTestTempDirectory("rea-doctor-linked-config-");
const target = join(root, "model.toml");
const link = join(root, "config.toml");
const runner = join(root, "agent-env");
await configRunner(runner);
await writeFile(target, "[model]\nbase_url = 'http://example.test'\n");
await symlink(target, link);
const facts = await readFacts(
{
HOME: root,
PATH: "",
AGENT_ENV_CONFIG: link,
REA_AGENT_ENV_COMMAND: runner,
FAKE_EXPLANATION: JSON.stringify({
path: "model.base_url",
value: "http://example.test",
error: null,
}),
},
[],
);
expect(facts.solveEndpoint).toBe(link);
},
);
it.skipIf(process.platform === "win32")(
"uses the owner's endpoint result and leaves references unknown",
async () => {
const root = await createTestTempDirectory("rea-doctor-model-config-");
const config = join(root, "config.toml");
const runner = join(root, "agent-env");
await configRunner(runner);
await writeFile(config, "[model]\ndefault = 'model-only'\n");
const environment = {
HOME: root,
PATH: "",
AGENT_ENV_CONFIG: config,
REA_AGENT_ENV_COMMAND: runner,
};
const explain = (value: unknown) =>
JSON.stringify({ path: "model.base_url", value, error: null });
expect(
(await readFacts({ ...environment, FAKE_EXPLANATION: explain(null) }, []))
.solveEndpoint,
).toBeUndefined();
expect(
(
await readFacts(
{
...environment,
FAKE_EXPLANATION: JSON.stringify({
path: "model.base_url",
value: "",
error: null,
winner: { kind: "env" },
}),
},
[],
)
).solveEndpoint,
).toBeNull();
expect(
(
await readFacts(
{ ...environment, FAKE_EXPLANATION: explain("env:MODEL_BASE") },
[],
)
).solveEndpoint,
).toBeNull();
expect(
(
await readFacts(
{ ...environment, FAKE_EXPLANATION: explain("http://endpoint.test") },
[],
)
).solveEndpoint,
).toBe(config);
},
);
it.skipIf(process.platform === "win32")(
"reports an endpoint query timeout as unknown under doctor's owned deadline",
async () => {
const root = await createTestTempDirectory("rea-doctor-endpoint-timeout-");
const runner = join(root, "agent-env");
await configRunner(runner);
let deadline: number | undefined;
let stopSignal: NodeJS.Signals | undefined;
const host = systemDoctorHost({
environment: { HOME: root, PATH: "", REA_AGENT_ENV_COMMAND: runner },
platform: "linux",
architecture: "x64",
desktopBuildFacts: readDesktopBuildFacts,
execFileOutput: async (_command, args, options) => {
if (args[0] === "config") {
deadline = options?.timeout;
stopSignal = options?.stopSignal;
throw Object.assign(new Error("timed out"), { code: "ETIMEDOUT" });
}
return {
stdout: "agent-env 0.9.1485",
stderr: "",
stdoutBytes: 19,
stderrBytes: 0,
};
},
});
try {
expect((await host.desktopBuildFacts!([])).solveEndpoint).toBeNull();
expect(deadline).toBe(10_000);
expect(stopSignal).toBe("SIGTERM");
} finally {
await host.close?.();
}
},
);
it.skipIf(process.platform === "win32")(
"uses the runtime PATH fallback for a registered empty runner",
async () => {
const { root, bin, home } = await buildHost("rea-desktop-empty-runner-");
const runner = join(bin, "agent-env");
await program(runner, 'echo "agent-env 0.9.1485"');
const environment = {
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
XDG_CACHE_HOME: join(root, "cache"),
};
await writeFile(
join(home, ".claude.json"),
JSON.stringify({
mcpServers: {
rea: {
command: process.execPath,
args: ["/opt/rea/scripts/rea.mjs", "mcp"],
env: { REA_AGENT_ENV_COMMAND: "" },
},
},
}),
);
expect(
(
await resolveBuildComputerEnvironment(
{ ...environment, REA_AGENT_ENV_COMMAND: "" },
"linux",
)
).REA_AGENT_ENV_COMMAND,
).toBe(runner);
const facts = await readFacts(environment, ["claude_code"]);
expect(facts.registrations[0]).toMatchObject({
runner: { status: "current", version: "0.9.1485" },
});
expect(facts.registrations[0]?.differing ?? []).not.toContain(
"REA_AGENT_ENV_COMMAND",
);
expect(
(
await readFacts(
{ ...environment, REA_AGENT_ENV_COMMAND: "/different/agent-env" },
["claude_code"],
)
).registrations[0]?.differing,
).toContain("REA_AGENT_ENV_COMMAND");
const host = systemDoctorHost({
environment,
platform: "linux",
architecture: "x64",
desktopBuildFacts: readDesktopBuildFacts,
});
try {
const report = await runDoctor(undefined, host, {
providers: ["desktop_build"],
clients: ["claude_code"],
});
expect(
report.scope_checks.find(
({ name }) => name === "desktop_build-agent-env",
),
).toMatchObject({ ok: true });
} finally {
await host.close?.();
}
await program(runner, 'echo "agent-env 0.9.1400"');
expect(
(await readFacts(environment, ["claude_code"])).registrations[0],
).toMatchObject({ runner: { status: "stale", version: "0.9.1400" } });
await rm(runner);
expect(
(await readFacts(environment, ["claude_code"])).registrations[0],
).toMatchObject({ runner: { status: "invalid" } });
},
);
it.skipIf(process.platform === "win32")(
"compares registrations to resolved defaults when the terminal has no overrides",
async () => {
const { root, bin, home } = await buildHost("rea-desktop-doctor-defaults-");
await program(join(bin, "docker"), "exit 1");
await program(
join(bin, "agent-env"),
'if [ "$1" = "--version" ]; then printf x >> "$REA_TEST_RUNNER_LOG"; fi; echo "agent-env 0.9.1485"',
);
const environment = {
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
XDG_CACHE_HOME: join(root, "cache"),
REA_TEST_RUNNER_LOG: join(root, "runner-log"),
};
await writeFile(
join(home, ".claude.json"),
JSON.stringify({
mcpServers: {
rea: {
command: process.execPath,
args: ["/opt/rea/scripts/rea.mjs", "mcp"],
env: {
REA_DOCKER_COMMAND: join(bin, "docker"),
REA_AGENT_ENV_COMMAND: join(bin, "agent-env"),
REA_BUILD_COMPUTER_REA_ARCHIVE: defaultBuildComputerArchive(
environment,
"linux",
),
},
},
},
}),
);
const facts = await readFacts(environment, ["claude_code"]);
expect(await readFile(environment.REA_TEST_RUNNER_LOG, "utf8")).toBe("x");
expect(facts.registrations).toMatchObject([
{
client: "claude_code",
registered: true,
unrecorded: [],
unresolved: [],
},
]);
},
);
it.skipIf(process.platform === "win32")(
"compares explicitly selected paths and reports unchecked recorded paths by name only",
async () => {
const { root, bin, home } = await buildHost(
"rea-desktop-doctor-registration-",
);
await writeFile(
join(home, ".claude.json"),
JSON.stringify({
mcpServers: {
rea: {
command: process.execPath,
args: ["/opt/rea/scripts/rea.mjs", "mcp"],
env: {
REA_AGENT_ENV_COMMAND: "/private/old-agent-env",
REA_DOCKER_COMMAND: "/private/docker",
REA_BUILD_COMPUTER_REA_ARCHIVE: "/private/old-archive",
},
},
},
}),
);
const facts = await readFacts(
{
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
XDG_CACHE_HOME: join(root, "cache"),
REA_AGENT_ENV_COMMAND: "/private/current-agent-env",
REA_DOCKER_COMMAND: "/private/docker",
},
["claude_code"],
);
expect(facts.registrations).toMatchObject([
{
client: "claude_code",
registered: true,
differing: ["REA_AGENT_ENV_COMMAND", "REA_BUILD_COMPUTER_REA_ARCHIVE"],
runner: { status: "invalid" },
},
]);
expect(JSON.stringify(facts.registrations)).not.toContain("/private/");
},
);
it.skipIf(process.platform === "win32")(
"verifies a client's custom docker and runtime archive without requiring the terminal's paths",
async () => {
const { root, bin, home } = await buildHost("rea-doctor-custom-paths-");
const docker = join(root, "custom-docker");
await program(
docker,
'if [ "$1" = info ]; then echo /tmp; else exit 0; fi',
);
await program(
join(bin, "docker"),
'if [ "$1" = info ]; then echo /tmp; else exit 0; fi',
);
const python = (await execute("which", ["python3"])).stdout.trim();
const clientPython = join(root, "python-client");
const pythonCalls = join(root, "python-calls");
await program(
clientPython,
`printf '%s\\n' "$1" >> "${pythonCalls}"\nexec "${python}" "$@"`,
);
await writeFile(
join(bin, "agent-env"),
`#!${clientPython}\nprint("agent-env 0.9.1431")\n`,
{ mode: 0o755 },
);
const packed = join(root, "packed");
await mkdir(join(packed, "runtime", "scripts"), { recursive: true });
await writeFile(join(packed, "runtime", "scripts", "rea.mjs"), "");
await writeFile(
join(packed, "runtime", "package.json"),
JSON.stringify({ version: PRODUCT_IDENTITY.packageVersion }),
);
const archive = join(root, "custom.tar.gz");
await execute("tar", ["-czf", archive, "-C", packed, "runtime"]);
await writeFile(
join(home, ".claude.json"),
JSON.stringify({
mcpServers: {
rea: {
command: process.execPath,
args: ["/opt/rea/scripts/rea.mjs", "mcp"],
env: {
REA_DOCKER_COMMAND: docker,
REA_BUILD_COMPUTER_REA_ARCHIVE: archive,
REA_AGENT_ENV_COMMAND: join(bin, "agent-env"),
},
},
},
}),
);
const facts = await readFacts(
{
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
XDG_CACHE_HOME: join(root, "cache"),
},
["claude_code"],
);
expect(facts.registrations[0]).toMatchObject({
registered: true,
unrecorded: [],
unresolved: [],
});
expect(facts.registrations[0]?.differing ?? []).toEqual([]);
expect(facts.registrations[0]?.unchecked ?? []).toEqual([]);
expect((await readFile(pythonCalls, "utf8")).split("\n")).toContain("-I");
expect(JSON.stringify(facts.registrations)).not.toContain(root);
const registration = (runner: string) =>
JSON.stringify({
mcpServers: {
rea: {
command: process.execPath,
args: ["/opt/rea/scripts/rea.mjs", "mcp"],
env: {
REA_DOCKER_COMMAND: docker,
REA_BUILD_COMPUTER_REA_ARCHIVE: archive,
REA_AGENT_ENV_COMMAND: runner,
},
},
},
});
const clientFacts = () =>
readFacts(
{
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
XDG_CACHE_HOME: join(root, "cache"),
},
["claude_code"],
);
await writeFile(
join(home, ".claude.json"),
registration(join(root, "missing-runner")),
);
expect((await clientFacts()).registrations[0]?.unchecked).toContain(
"REA_BUILD_COMPUTER_REA_ARCHIVE",
);
const wrapper = join(root, "wrapper");
await program(wrapper, 'echo "agent-env 0.9.1431"');
await writeFile(join(home, ".claude.json"), registration(wrapper));
expect((await clientFacts()).registrations[0]?.unchecked).toContain(
"REA_BUILD_COMPUTER_REA_ARCHIVE",
);
await writeFile(
join(home, ".claude.json"),
registration(join(bin, "agent-env")),
);
await rm(archive);
expect(
(
await readFacts(
{
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
XDG_CACHE_HOME: join(root, "cache"),
},
["claude_code"],
)
).registrations[0]?.differing,
).toContain("REA_BUILD_COMPUTER_REA_ARCHIVE");
},
);
it.skipIf(process.platform === "win32")(
"reads a refused daemon and names unrecorded settings without their values",
async () => {
const { root, bin, home } = await buildHost("rea-desktop-doctor-facts-");
await program(
join(bin, "docker"),
'echo "permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock" >&2\nexit 1',
);
await program(join(bin, "agent-env"), 'echo "agent-env 0.9.1400"');
await writeFile(
join(home, ".claude.json"),
JSON.stringify({
mcpServers: {
rea: {
command: process.execPath,
args: ["/opt/rea/scripts/rea.mjs", "mcp"],
env: { REA_AGENT_ENV_COMMAND: "/private/value/agent-env" },
},
},
}),
);
const facts = await readFacts(
{
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
XDG_CACHE_HOME: join(root, "cache"),
},
["claude_code"],
);
expect(facts).toMatchObject({
ps: { found: true },
docker: { status: "found", path: join(bin, "docker"), source: "path" },
daemon: { status: "permission_denied" },
agentEnv: { status: "found", version: "0.9.1400" },
archive: { source: "cache", status: "missing" },
registrations: [
{
client: "claude_code",
registered: true,
unrecorded: ["REA_BUILD_COMPUTER_REA_ARCHIVE", "REA_DOCKER_COMMAND"],
unresolved: [],
},
],
leftoverContainers: undefined,
});
expect(JSON.stringify(facts)).not.toContain("/private/value");
},
);
it.skipIf(process.platform === "win32")(
"reads a selected archive's REA version and the sandboxes Docker still holds",
async () => {
const { root, bin, home } = await buildHost("rea-desktop-doctor-archive-");
await program(
join(bin, "docker"),
[
'if [ "$1" = info ]; then echo /srv/docker; exit 0; fi',
'case "$4" in name=*) echo agent-0f3a;; label=*) printf "agent-0f3a\\nrelay-0f3a\\n";; esac',
].join("\n"),
);
const packed = join(root, "packed");
await mkdir(join(packed, "rea-prod"), { recursive: true });
await mkdir(join(packed, "rea-prod", "scripts"));
await writeFile(join(packed, "rea-prod", "scripts", "rea.mjs"), "");
await writeFile(
join(packed, "rea-prod/package.json"),
JSON.stringify({ name: "rea-agents", version: "6.7.0" }),
);
const archive = join(root, "rea-prod.tar.xz");
await execute("tar", ["-cJf", archive, "-C", packed, "rea-prod"]);
const facts = await readFacts(
{
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
REA_AGENT_ENV_COMMAND: "agent-env",
REA_BUILD_COMPUTER_REA_ARCHIVE: archive,
},
[],
);
expect(facts).toMatchObject({
daemon: { status: "reachable", rootDirectory: "/srv/docker" },
agentEnv: { status: "invalid", value: "agent-env" },
archive: { path: archive, source: "environment", status: "stale" },
environmentSettings: [
"REA_AGENT_ENV_COMMAND",
"REA_BUILD_COMPUTER_REA_ARCHIVE",
],
leftoverContainers: ["agent-0f3a", "relay-0f3a"],
});
expect(facts.archive.detail).toContain("6.7.0");
},
);
it("recognizes a gzip runtime archive with a different top folder", async () => {
const { root, bin, home } = await buildHost(
"rea-desktop-doctor-archive-gzip-",
);
const packed = join(root, "packed");
await mkdir(join(packed, "package", "scripts"), { recursive: true });
await writeFile(join(packed, "package", "scripts", "rea.mjs"), "");
await writeFile(
join(packed, "package", "package.json"),
JSON.stringify({ version: PRODUCT_IDENTITY.packageVersion }),
);
const archive = join(root, "runtime.tar.gz");
await execute("tar", ["-czf", archive, "-C", packed, "package"]);
const facts = await readFacts(
{
HOME: home,
USERPROFILE: home,
PATH: `${bin}:/usr/bin:/bin`,
REA_BUILD_COMPUTER_REA_ARCHIVE: archive,
},
[],
);
expect(facts.archive).toMatchObject({ path: archive, status: "current" });
const python = (await execute("which", ["python3"])).stdout.trim();
const runner = join(bin, "agent-env");
await writeFile(runner, `#!${python}\nprint('agent-env 0.9.1485')\n`, {
mode: 0o755,
});
const withoutSystemPython = await readFacts(
{
HOME: home,
USERPROFILE: home,
PATH: bin,
REA_AGENT_ENV_COMMAND: runner,
REA_BUILD_COMPUTER_REA_ARCHIVE: archive,
},
[],
);
expect(withoutSystemPython.archive).toMatchObject({
path: archive,
status: "current",
});
});
it.skipIf(process.platform === "win32")(
"settles every client inspection before reporting a retained runner close failure",
async () => {
const root = await createTestTempDirectory("rea-doctor-peer-settle-");
const failing = join(root, "agent-env-a");
const slow = join(root, "agent-env-b");
const archive = join(root, "runtime.tar.gz");
await program(failing, "exit 0");
await program(slow, "exit 0");
await writeFile(archive, "archive");
const resources = new ArtifactResourceScope();
let releaseSlow!: () => void;
const slowReleased = new Promise<void>((resolve) => {
releaseSlow = resolve;
});
let reachSlow!: () => void;
const slowReached = new Promise<void>((resolve) => {
reachSlow = resolve;
});
let failClose!: () => void;
const closeFailed = new Promise<void>((resolve) => {
failClose = resolve;
});
const close = vi
.spyOn(OwnedFileHandle.prototype, "close")
.mockImplementationOnce(async () => {
failClose();
throw new Error("close denied");
});
const recorded: Record<string, Record<string, string>> = {
a: {
REA_AGENT_ENV_COMMAND: failing,
REA_BUILD_COMPUTER_REA_ARCHIVE: archive,
},
b: { REA_AGENT_ENV_COMMAND: slow },
};
const status = (client: string, differing: readonly string[]) =>
({
client,
command: [process.execPath, "rea.mjs", "mcp"],
provider_settings: { differing, unrecorded: [], unresolved: [] },
}) as unknown as ClientRegistrationStatus;
let outcome: Promise<unknown> = Promise.resolve();
try {
let settled = false;
outcome = readDesktopBuildFacts({
environment: { HOME: root, PATH: "" },
platform: process.platform,
resources,
signal: new AbortController().signal,
run: async (command) => {
if (command === slow) {
reachSlow();
await slowReleased;
}
const stdout = "agent-env 0.9.1431\n";
return {
stdout,
stderr: "",
stdoutBytes: Buffer.byteLength(stdout),
stderrBytes: 0,
};
},
clientIds: ["a", "b"],
registrations: async (_clients, _settings, _owned, observe) => {
for (const [client, values] of Object.entries(recorded))
for (const [name, value] of Object.entries(values))
observe?.(client, name, value);
return [
status("a", ["REA_BUILD_COMPUTER_REA_ARCHIVE"]),
status("b", []),
];
},
}).then(
() => {
settled = true;
return undefined;
},
(cause: unknown) => {
settled = true;
return cause;
},
);
await awaitAcknowledgement(
Promise.all([slowReached, closeFailed]).then(() => undefined),
outcome,
"slow client probe and failed runner close",
);
for (let turn = 0; turn < 5; turn += 1)
await new Promise((resolve) => setImmediate(resolve));
// The failed close must not report while client b is still inspecting.
expect(settled).toBe(false);
releaseSlow();
// The doctor reports a typed cleanup failure naming the retained runner
// file, so the CLI and MCP error documents keep it and its recovery.
const failure = await outcome;
expect(failure).toBeInstanceOf(ProviderCleanupError);
const cleanup = failure as ProviderCleanupError;
expect(cleanup.operation).toBe("doctor");
expect(cleanup.cause).toBeInstanceOf(ArtifactFileCleanupFailure);
expect(resources.cleanupResources).toHaveLength(1);
expect(cleanup.cleanupResources).toEqual(resources.cleanupResources);
} finally {
releaseSlow();
// Join the inspection before restoring the close it observes.
await outcome;
close.mockRestore();
await resources.retryCleanup();
await resources.close();
}
},
);