Files
reverse-engineering-anything/tests/process-global/processObservationCommandFinalization.test.ts
T
morluto ab84ba6839 fix(process)!: retain inspection cancellation and cleanup ownership (#10113)
* fix(process): preserve producer-owned inspection cancellation

Prototype the explicit inspection origin and retained file/batch owner protocol. Downstream adapter migration and lease binding remain required before integration.

* fix(process): type inspection ownership handoffs precisely

* fix(process): retain plain inspection preparation cancellation

* fix(process): retain explicit inspection cleanup and cancellation facts

* fix(process): require explicit inspection facts and cleanup scope

* refactor(process): remove redundant cleanup forwarding wrapper

* fix(firmware): retain inspection cancellation and cleanup ownership

* fix(wakaru): retain typed inspection outcomes and caller scope

* fix(ghidra): preserve inspection facts and launch cleanup scope

* fix(providers): preserve typed process inspection outcomes

* refactor(process): remove obsolete inspection cancellation re-export

* fix(native): preserve inspection preparation cleanup and cancellation

* fix(registration): retain typed inspection cleanup during Git discovery

* fix(process): preserve inspection deadline origin in adapter handoff

* fix(process): consume the canonical inspection selector in binding

* fix(native): preserve inspection deadlines and cleanup retry completion

* test(process): narrow the retained Darwin preparation owner

* fix(providers): preserve preparation ownership and timeout policy

Narrow preparation facts through their canonical owner, preserve actual cleanup capabilities and recorded cancellation, and keep enclosing command deadlines authoritative. Retain raw inspection records through Java refusal and configuration projections without changing availability fallback policy.

* refactor(process): use one inspection failure type owner

* fix(providers): retain recorded secondary failures when rebinding

* fix(ghidra): preserve normalized inspection facts through public projection

* fix(process): normalize native process listing cancellation

* docs(development): clarify failure provenance and cleanup verification

* fix(process): preserve direct inspection cancellation facts

* fix(process): retain inspection owners through termination

* fix(process): preserve command inspection cleanup provenance

* fix(desktop): preserve inspection facts after cleanup retry

* refactor(process)!: migrate desktop capture with inspection producers

* fix(desktop)!: preserve capture failure origins and cleanup owners (#10115)

* fix(desktop): preserve capture failure origins and cleanup

* fix(desktop): type keyring cleanup cancellation

* fix(kuna)!: retain copy cleanup before retiring workspaces (#10114)

* fix(kuna): own copy cancellation and descriptor cleanup

* fix(kuna): close copy descriptors before workspace removal

* test(kuna): type copy lifecycle gates for project library

* docs(kuna): explain retained copy-file cleanup and staging ownership

* docs(kuna): separate cache publication, integrity and cleanup guidance

* fix(desktop): retain sampling inspection owners before release

* fix(process): preserve capture file read cancellation origin

* fix(process): retain capture inspection cancellation and cleanup owners

* fix(process): retain sampling inspection failures in capture scope

* fix(process): retain inspection failures through mandatory cleanup

* fix(process): distinguish sampler shutdown from capture cancellation

* fix(browser): retain startup EOF before later cancellation

* fix(process): bind capture inspection cleanup to the owning run

* test(process): migrate cancellation fixtures to typed owners

Record explicit preparation and command cancellation facts, retain exact opaque reasons and physical causes, and guard the mocked version probe from native preparation. Keep the live Wakaru command control while adding a recording equivalent for bounded source verification.

* fix(process): own sampling reads and settle inspection peers

* fix(errors): retain authored recovery through cleanup composition

* fix(process): retain all sampling inspection leaves for final cleanup

* test(browser): report recording cleanup signal outcome

* fix(process): keep inspection error context out of implicit JSON

* fix(desktop): retain normalized sampling cleanup ownership

* test(process): assert tagged startup cancellation
2026-11-11 16:37:48 -05:00

287 lines
9.3 KiB
TypeScript

import { createTestProcessCaptureResourceScope } from "../fixtures/processCaptureResourceScope.js";
import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it, onTestFinished } from "vitest";
import { createDarwinProcessRunTokenReader } from "../../src/process/DarwinProcessRunTokenReader.js";
import { createSystemProcessOwnershipHost } from "../../src/process/ProcessOwnershipObservation.js";
import { startProcessSampler } from "../../src/process/capture/ProcessSampling.js";
const waitForFile = async (path: string, timeoutMs = 4_000): Promise<void> => {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
await readFile(path);
return;
} catch (cause) {
if (
typeof cause !== "object" ||
cause === null ||
!("code" in cause) ||
cause.code !== "ENOENT"
)
throw cause;
}
await new Promise((resolve) => setTimeout(resolve, 5));
}
throw new Error(`Timed out waiting for ${path}`);
};
const waitForCommandOrFailure = async (
ready: Promise<void>,
pending: Promise<unknown>,
): Promise<void> => {
const outcome = await Promise.race([
ready.then(() => ({ ready: true as const })),
pending.then(
() => ({ ready: false as const, cause: undefined }),
(cause: unknown) => ({ ready: false as const, cause }),
),
]);
if (!outcome.ready)
throw new Error("The command completed before its ready marker", {
cause: outcome.cause,
});
};
it("rejects an early command failure even when its reason is undefined", async () => {
const ready = new Promise<void>(() => {});
await expect(
waitForCommandOrFailure(ready, Promise.reject(undefined)),
).rejects.toBeInstanceOf(Error);
});
const delayedCommandSource = (
readyPath: string,
donePath: string,
stdout = "123 1 123 123 501 S fixture-process",
): string =>
[
`const fs = require("node:fs");`,
`process.on("SIGTERM", () => { setTimeout(() => { fs.writeFileSync(${JSON.stringify(donePath)}, "done"); process.exit(0); }, 150); });`,
`fs.writeFileSync(${JSON.stringify(readyPath)}, "ready");`,
`process.stdout.write(${JSON.stringify(`${stdout}\n`)});`,
`setInterval(() => {}, 1000);`,
"",
].join("\n");
const createDelayedCommand = async (directory: string) => {
const readyPath = join(directory, "ready");
const donePath = join(directory, "done");
const executable = join(directory, "ps");
const runtime = `#!${process.execPath}\n${delayedCommandSource(readyPath, donePath)}`;
await writeFile(executable, runtime);
await chmod(executable, 0o755);
return { readyPath, donePath, executable };
};
const expectOwnedCommandToFinishOnAbort = async (
run: (signal: AbortSignal, directory: string) => Promise<unknown>,
): Promise<void> => {
const directory = await mkdtemp(join(tmpdir(), "rea-process-command-"));
const command = await createDelayedCommand(directory);
const controller = new AbortController();
const reason = new Error("cancel owned process command");
let pending: Promise<unknown> | undefined;
onTestFinished(async () => {
try {
controller.abort(reason);
await pending?.catch(() => {});
if (
await readFile(command.readyPath).then(
() => true,
() => false,
)
)
await waitForFile(command.donePath);
} finally {
await rm(directory, { recursive: true, force: true });
}
});
pending = run(controller.signal, directory);
void pending.catch(() => {});
await waitForCommandOrFailure(waitForFile(command.readyPath), pending);
controller.abort(reason);
const failure = await pending.then(
() => undefined,
(cause: unknown) => cause,
);
if (!(failure instanceof Error))
throw new Error("The cancelled process command did not return an error");
expect(failure.cause).toBe(reason);
await expect(readFile(command.donePath, "utf8")).resolves.toBe("done");
};
const createFakeDarwinReader = async (
directory: string,
command: Awaited<ReturnType<typeof createDelayedCommand>>,
) => {
const compiler = join(directory, "swiftc");
const generatedRuntime = `#!${process.execPath}\n${delayedCommandSource(command.readyPath, command.donePath)}`;
const compilerSource = [
`const fs = require("node:fs");`,
`const args = process.argv.slice(2);`,
`const outputIndex = args.indexOf("-o");`,
`const output = args[outputIndex + 1];`,
`fs.writeFileSync(output, ${JSON.stringify(generatedRuntime)});`,
`fs.chmodSync(output, 0o755);`,
].join("\n");
await writeFile(compiler, `#!${process.execPath}\n${compilerSource}\n`);
await chmod(compiler, 0o755);
const reader = createDarwinProcessRunTokenReader({
compiler: { kind: "swiftc", executable: compiler, sdkRoot: directory },
compileTimeoutMs: 2_000,
moduleCachePath: join(directory, "module-cache"),
});
return reader;
};
const processEntry = {
pid: 123,
parentPid: 1,
processGroupId: 123,
uid: 501,
state: "S",
command: "fixture-process",
} as const;
it.skipIf(process.platform === "win32")(
"waits for the owned ps command to exit when sampling stops",
async () => {
const directory = await mkdtemp(join(tmpdir(), "rea-process-sampler-"));
const readyPath = join(directory, "ready");
const donePath = join(directory, "done");
const psPath = join(directory, "ps");
const originalPath = process.env.PATH;
const platformDescriptor = Object.getOwnPropertyDescriptor(
process,
"platform",
);
if (platformDescriptor === undefined)
throw new Error("process.platform descriptor is unavailable");
let stopSampler: (() => Promise<unknown>) | undefined;
let restoreProcessGlobals = (): void => {};
onTestFinished(async () => {
try {
await stopSampler?.();
if (
await readFile(readyPath).then(
() => true,
() => false,
)
)
await waitForFile(donePath);
} finally {
restoreProcessGlobals();
await rm(directory, { recursive: true, force: true });
}
});
await writeFile(
psPath,
`#!${process.execPath}\n${delayedCommandSource(readyPath, donePath, "100 1 100 100 fixture-ps-root")}`,
);
await chmod(psPath, 0o755);
process.env.PATH = `${directory}${originalPath === undefined ? "" : `:${originalPath}`}`;
Object.defineProperty(process, "platform", {
...platformDescriptor,
value: "darwin",
});
restoreProcessGlobals = () => {
if (originalPath === undefined) delete process.env.PATH;
else process.env.PATH = originalPath;
Object.defineProperty(process, "platform", platformDescriptor);
};
stopSampler = startProcessSampler({
resourceScope: createTestProcessCaptureResourceScope(),
rootPid: 100,
runId: "fixture-run-token",
started: 0,
limit: 20,
samples: [],
});
await waitForFile(readyPath);
await stopSampler();
await expect(readFile(donePath, "utf8")).resolves.toBe("done");
},
);
it
.skipIf(process.platform === "win32")
.each(["darwin-list", "freebsd-environment"] as const)(
"waits for the %s ownership command to exit after cancellation",
async (kind) => {
await expectOwnedCommandToFinishOnAbort(async (signal, directory) => {
const platform = kind === "darwin-list" ? "darwin" : "freebsd";
const host = createSystemProcessOwnershipHost(platform, {
PATH: directory,
});
return kind === "darwin-list"
? host.listProcesses(signal)
: host.environment(123, signal);
});
},
);
const expectDarwinReaderCommandToFinishOnAbort = async (
operation: "read" | "identities",
): Promise<void> => {
const directory = await mkdtemp(join(tmpdir(), `rea-darwin-${operation}-`));
const command = await createDelayedCommand(directory);
const reader = await createFakeDarwinReader(directory, command);
const controller = new AbortController();
const reason = new Error(`cancel ${operation}`);
let pending: Promise<unknown> | undefined;
onTestFinished(async () => {
try {
controller.abort(reason);
await pending?.catch(() => {});
if (
await readFile(command.readyPath).then(
() => true,
() => false,
)
)
await waitForFile(command.donePath);
await reader.close();
} finally {
await rm(directory, { recursive: true, force: true });
}
});
pending =
operation === "read"
? reader.read([processEntry], controller.signal)
: reader.identities([processEntry], controller.signal);
void pending.catch(() => {});
await waitForCommandOrFailure(waitForFile(command.readyPath), pending);
controller.abort(reason);
const failure = await pending.then(
() => undefined,
(cause: unknown) => cause,
);
if (!(failure instanceof Error))
throw new Error(
`The cancelled ${operation} command did not return an error`,
);
expect(failure.cause).toBe(reason);
await expect(readFile(command.donePath, "utf8")).resolves.toBe("done");
};
it.skipIf(process.platform === "win32").each(["read", "identities"] as const)(
"waits for the compiled Darwin %s command to exit after cancellation",
async (operation) => {
await expectDarwinReaderCommandToFinishOnAbort(operation);
},
);