Files
reverse-engineering-anything/src/process/ProcessOwnership.inspectionFailure.test.ts
T
SandivandVincent Koc f5055cd2ed fix(process): report denied inspection as typed cleanup failures (#9261)
* fix(process): report denied inspection as typed cleanup failures

Preserve the native ps diagnostic through owned-worker cleanup and finalize one-shot JavaScript analysis before Evidence delivery. Retain mandatory inspection and existing cleanup authority.

Preserve the inspector diagnostic when the initial process-table read succeeds but pre-signal revalidation fails, and remove the unreachable typed-error branch so retained artifact retry failures project through the existing cleanup contract.

* fix(resources): preserve independent close failure ownership

* fix(cli): retain cleanup recovery and nested close evidence

* refactor(providers): classify rejected closes at the shared boundary

* test(cli): preserve bundle metadata and physical inspection failures

* fix(cli): preserve the JavaScript failure envelope after finalization

* test(cli): validate the JavaScript error envelope at its boundary

* fix(providers): preserve unnamed close failure resources

* fix(cli): retain invocation owners through outer cleanup

* test(cli): distinguish reported cleanup failures from uncaught errors

* fix(cli): compose each runtime’s declared cleanup owners

* test(cli): preserve the original cleanup rejection

* test(cli): allow cleanup retry recovery after a reported failure

* fix(cli): report final cleanup after returned results

* fix(cli): preserve launcher executable mode

* test(cli): retain invocation cleanup in file sessions

---------

Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
2026-11-30 19:38:45 +08:00

188 lines
5.7 KiB
TypeScript

import { expect, it, vi } from "vitest";
const inspect = vi.hoisted(() => vi.fn());
vi.mock("./ExecFileOutput.js", async (importOriginal) => ({
...(await importOriginal<typeof import("./ExecFileOutput.js")>()),
execFileOutput: inspect,
}));
import { ExecFileOutputCancelledError } from "./ExecFileOutput.js";
import { createSystemProcessOwnershipHost } from "./ProcessOwnershipObservation.js";
import {
cleanupOwnedProcessGroup,
verifyNoTokenOwnedProcesses,
} from "./ProcessOwnership.js";
it.each(["darwin", "linux"] as const)(
"preserves the denied inspector at the %s host and cleanup boundaries",
async (platform) => {
inspect.mockRejectedValue(
Object.assign(new Error("spawn EPERM"), {
code: "EPERM",
syscall: "spawn ps",
}),
);
const signalGroup = vi.fn();
const host = {
...createSystemProcessOwnershipHost(
platform,
{},
{ linuxPsLocations: [] },
),
signalGroup,
};
const command = platform === "darwin" ? "/bin/ps" : "ps";
const diagnostic = {
capability: "process_table",
command,
arguments: ["-axo", "pid=,ppid=,pgid=,uid=,stat=,command="],
system_code: "EPERM",
reason: "spawn EPERM",
};
await expect(host.listProcesses()).rejects.toMatchObject({
inspection: diagnostic,
});
for (const outcome of [
await cleanupOwnedProcessGroup(
{ leaderPid: 100, processGroupId: 100, runId: "fixture" },
host,
),
await verifyNoTokenOwnedProcesses("fixture", host),
]) {
expect(outcome).toMatchObject({ cleaned: false, inspection: diagnostic });
}
expect(signalGroup).not.toHaveBeenCalled();
},
);
it("retains inspection failure on the launcher-exit refresh without replacing the identity failure", async () => {
inspect.mockResolvedValueOnce({ stdout: "100 1 100 501 S fixture\n" });
inspect.mockRejectedValueOnce(
Object.assign(new Error("spawn EPERM"), { code: "EPERM" }),
);
const native = createSystemProcessOwnershipHost(
"linux",
{},
{ linuxPsLocations: [] },
);
const signalGroup = vi.fn();
const result = await cleanupOwnedProcessGroup(
{
leaderPid: 100,
processGroupId: 100,
runId: "fixture",
expectedParentPid: 2,
},
{
listProcesses: native.listProcesses,
environment: async () => ({ REA_PROCESS_RUN_ID: "fixture" }),
signalGroup,
},
);
expect(result).toMatchObject({
cleaned: false,
reason: expect.stringContaining("parent"),
inspection: { command: "ps", system_code: "EPERM" },
});
expect(signalGroup).not.toHaveBeenCalled();
});
it("retains the denied inspector when the pre-signal process-table refresh fails", async () => {
inspect.mockReset();
inspect.mockResolvedValueOnce({ stdout: "100 1 100 501 S fixture\n" });
inspect.mockRejectedValueOnce(
Object.assign(new Error("spawn EPERM"), {
code: "EPERM",
syscall: "spawn ps",
}),
);
const native = createSystemProcessOwnershipHost(
"linux",
{},
{ linuxPsLocations: [] },
);
const signalGroup = vi.fn();
const signalPid = vi.spyOn(process, "kill").mockReturnValue(true);
try {
const result = await cleanupOwnedProcessGroup(
{ leaderPid: 100, processGroupId: 100, runId: "fixture" },
{
listProcesses: native.listProcesses,
environment: async () => ({ REA_PROCESS_RUN_ID: "fixture" }),
signalGroup,
},
);
expect(inspect).toHaveBeenCalledTimes(2);
expect(signalGroup).not.toHaveBeenCalled();
expect(signalPid).not.toHaveBeenCalled();
expect(result).toMatchObject({
cleaned: false,
reason:
"process ownership could not be revalidated: ps process-table inspection failed: spawn EPERM",
inspection: {
capability: "process_table",
command: "ps",
arguments: ["-axo", "pid=,ppid=,pgid=,uid=,stat=,command="],
system_code: "EPERM",
syscall: "spawn ps",
reason: "spawn EPERM",
remediation_action: expect.stringContaining("host context permits"),
},
});
if (result.cleaned) throw new Error("Expected unverified cleanup");
expect(result.inspection?.system_code).toBe("EPERM");
} finally {
signalPid.mockRestore();
}
});
it("keeps unclassified revalidation failures unchanged without inferring inspector facts", async () => {
const signalGroup = vi.fn();
const listProcesses = vi
.fn()
.mockResolvedValueOnce([
{
pid: 100,
parentPid: 1,
processGroupId: 100,
state: "S",
command: "fixture",
},
])
.mockRejectedValueOnce(
Object.assign(new Error("custom inspector failure"), { code: "EPERM" }),
);
expect(
await cleanupOwnedProcessGroup(
{ leaderPid: 100, processGroupId: 100, runId: "fixture" },
{
listProcesses,
environment: async () => ({ REA_PROCESS_RUN_ID: "fixture" }),
signalGroup,
},
),
).toEqual({
cleaned: false,
reason:
"process ownership could not be revalidated: custom inspector failure",
});
expect(listProcesses).toHaveBeenCalledTimes(2);
expect(signalGroup).not.toHaveBeenCalled();
});
it("keeps caller cancellation distinct from an inspection refusal", async () => {
const controller = new AbortController();
const reason = new Error("caller stopped inspection");
controller.abort(reason);
inspect.mockRejectedValueOnce(new ExecFileOutputCancelledError(reason));
const host = createSystemProcessOwnershipHost(
"linux",
{},
{ linuxPsLocations: [] },
);
await expect(host.listProcesses(controller.signal)).rejects.toMatchObject({
_tag: "ProcessOwnershipInspectionCancelled",
reason,
});
});