Files
reverse-engineering-anything/tests/boundary/cli/javascriptInspectionCleanupCli.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

105 lines
3.9 KiB
TypeScript

import { readFile, rm, writeFile } from "node:fs/promises";
import { join, resolve } from "node:path";
import { expect, onTestFinished } from "vitest";
import { cliTest } from "../../support/cli/cliFixture.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { analysisErrorProjectionSchema } from "../../../src/domain/analysisErrorProjectionSchema.js";
import { analysisCliErrorEnvelopeSchema } from "../../../src/contracts/errorSchemas.js";
import { parseEvidence } from "../../../src/domain/evidence.js";
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
import { z } from "zod";
const workerRecord = z.object({
kind: z.literal("worker"),
root: z.string(),
leaderPid: z.number().int().positive(),
processGroupId: z.number().int().positive(),
runId: z.string(),
});
cliTest.skipIf(process.platform === "win32").for([
["analyze", "deny"],
["analyze-javascript-application", "deny"],
["analyze", "cancel"],
["analyze-javascript-application", "cancel"],
["analyze", "normal"],
["analyze-javascript-application", "normal"],
] as const)(
"finalizes %s before delivering its %s outcome",
async ([command, mode], { processes }) => {
const root = await createTestTempDirectory("rea-inspection-cli-");
const trace = join(root, "inspection.txt");
onTestFinished(async () => {
const records = (await readFile(trace, "utf8")).trim().split("\n");
for (const record of records) {
const parsed = workerRecord.safeParse(JSON.parse(record));
if (!parsed.success) continue;
expect(await cleanupOwnedProcessGroup(parsed.data)).toMatchObject({
cleaned: true,
});
await rm(parsed.data.root, { recursive: true, force: true });
}
});
await writeFile(join(root, "app.mjs"), "export const observed = 7;");
const result = await processes.run(
process.execPath,
[
"--import",
resolve("tests/fixtures/javascriptInspectionPreload.mjs"),
resolve("scripts/rea.mjs"),
command,
root,
"--json",
],
{
env: {
...process.env,
REA_INSPECTION_TRACE: trace,
REA_INSPECTION_MODE: mode,
},
},
);
// The outer retry can recover after the command reports incomplete cleanup.
const cleanupReported = result.stderr.includes(
"Failed to close CLI resources:",
);
if (mode !== "deny" || !cleanupReported)
expect(result.stderr).not.toMatch(/^\s+at /m);
expect(result.stderr).not.toContain("triggerUncaughtException");
if (mode === "normal") {
expect(result.stderr).not.toContain("Failed to close CLI resources:");
expect(result.exitCode, result.stderr).toBe(0);
expect(parseEvidence(JSON.parse(result.stdout)).operation).toBe(
"analyze_javascript_application",
);
return;
}
expect(result.exitCode, result.stderr).toBe(mode === "deny" ? 1 : 130);
const { error: label, ...projection } =
analysisCliErrorEnvelopeSchema.parse(JSON.parse(result.stdout));
expect(label).toBe("JavaScript application analysis failed");
const failure = analysisErrorProjectionSchema.parse(projection);
expect(failure.code).toBe(
mode === "deny" ? "cleanup_incomplete" : "cancelled",
);
if (mode === "deny") {
expect(await readFile(trace, "utf8")).toContain('"kind":"denied"');
expect(failure.details).toMatchObject({
cleanup: "incomplete",
diagnostics: {
inspection: {
capability: "process_table",
command: expect.stringMatching(/(^|[\\/])ps$/u),
system_code: "EPERM",
},
},
});
expect(failure.remediation.action).toContain("ps");
}
expect(parseEvidence(failure.details?.partial_observation)).toMatchObject({
operation: "analyze_javascript_application",
subject: { local_path: root },
});
},
);