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>
This commit is contained in:
SandivandVincent Koc authored and GitHub committed 2026-11-30 19:38:45 +08:00
1 parent 891a999a12
commit f5055cd2ed
33 files changed
+1501 -111

No files matched your search

@@ -9,6 +9,7 @@ import {
runCliJavaScriptApplicationAnalysis,
} from "../../../src/cli/javascriptApplicationAnalysis.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
const originalClose = ArtifactResourceScope.prototype.close;
@@ -95,6 +96,191 @@ it("retains completed JavaScript Evidence when cancellation arrives during final
}
});
it("retains completed Evidence and inspector remediation when an artifact and the final hook both fail", async () => {
const root = await createTestTempDirectory("cli-js-mixed-cleanup-");
await writeFile(join(root, "index.js"), "export const answer = 42;\n");
const host = new CliJavaScriptAnalysisHost("invocation");
let denied = true;
const inspection = {
capability: "process_table",
command: "/bin/ps",
system_code: "EPERM",
};
const hookFailure = new ProviderCleanupError(
"javascript-static-analysis",
["fixture-worker"],
{ inspection },
{
operation: "analyze_javascript_application",
remediationAction: "Run from a host context that permits /bin/ps.",
},
);
const owner = {
kind: "lease",
resource: "fixture-artifact",
lease: {
async release() {
if (denied) throw new Error("artifact busy");
},
},
} as const;
const scope = host.scope();
const retryCleanup = scope.retryCleanup.bind(scope);
const retry = vi
.spyOn(scope, "retryCleanup")
.mockImplementationOnce(async () => {
scope.retain(owner);
await retryCleanup();
});
host.scope().addCloseHook(async () => {
if (denied) throw hookFailure;
});
try {
const result = await runCliJavaScriptApplicationAnalysis(
{ input_path: root },
{},
undefined,
undefined,
host,
);
expect(result).toMatchObject({
code: "cleanup_incomplete",
details: {
partial_observation: {
operation: "analyze_javascript_application",
normalized_result: { input_path: root },
},
diagnostics: {
cleanup_error: {
details: {
diagnostics: {
cleanup_error_2: {
details: { diagnostics: { inspection } },
remediation: {
action: "Run from a host context that permits /bin/ps.",
},
},
},
},
},
},
},
});
} finally {
denied = false;
retry.mockRestore();
await host.close();
}
});
it.each([false, true])(
"recovers completed Evidence after a final cleanup retry (cancelled=%s)",
async (cancelled) => {
const root = await createTestTempDirectory("cli-js-recovered-cleanup-");
await writeFile(join(root, "index.js"), "export const answer = 42;\n");
const host = new CliJavaScriptAnalysisHost("invocation");
const controller = new AbortController();
let attempts = 0;
const scope = host.scope();
const retryCleanup = scope.retryCleanup.bind(scope);
const retry = vi
.spyOn(scope, "retryCleanup")
.mockImplementationOnce(async () => {
scope.retain({
kind: "lease",
resource: "fixture-artifact",
lease: {
async release() {
attempts += 1;
if (attempts === 1) throw new Error("transient close failure");
if (cancelled) controller.abort("stop during final cleanup");
},
},
});
await retryCleanup();
});
try {
const result = await runCliJavaScriptApplicationAnalysis(
{ input_path: root },
{},
controller.signal,
undefined,
host,
);
expect(attempts).toBe(2);
expect(result).toMatchObject(
cancelled
? {
code: "cancelled",
details: {
partial_observation: {
operation: "analyze_javascript_application",
normalized_result: { input_path: root },
},
},
}
: {
operation: "analyze_javascript_application",
normalized_result: { input_path: root },
},
);
} finally {
retry.mockRestore();
await host.close();
}
},
);
it("keeps the primary analysis error when one-shot finalization fails and retains its owner for retry", async () => {
const host = new CliJavaScriptAnalysisHost("invocation");
let denied = true;
let attempts = 0;
const failure = new ProviderCleanupError(
"javascript-static-analysis",
["fixture-worker"],
{ reason: "controlled inspection denial" },
{ operation: "analyze_javascript_application" },
);
let released = false;
failure.bindCleanupReleaseClaim({
get cleanupReleased() {
return released;
},
});
host.scope().addCloseHook(async () => {
attempts += 1;
if (denied) throw failure;
released = true;
});
try {
const result = await runCliJavaScriptApplicationAnalysis(
{},
{},
undefined,
undefined,
host,
);
expect(attempts).toBe(1);
expect(result).toMatchObject({
error: "JavaScript application analysis failed",
code: "cleanup_incomplete",
details: {
execution_failure: "invalid_request",
resources: ["fixture-worker"],
diagnostics: { primary_error: { code: "invalid_request" } },
},
});
expect(failure.cleanupIncomplete).toBe(true);
denied = false;
await host.close();
expect(attempts).toBe(2);
expect(failure.cleanupIncomplete).toBe(false);
} finally {
denied = false;
await host.close();
}
});
it("retries JavaScript worker shutdown when the first close fails", async () => {
await closeCliResources();
let calls = 0;
@@ -115,6 +301,34 @@ it("retries JavaScript worker shutdown when the first close fails", async () =>
expect(calls).toBe(2);
});
it("preserves the pre-delivery retry and final close retry before projecting the primary", async () => {
const host = new CliJavaScriptAnalysisHost("invocation");
let attempts = 0;
host.scope().retain({
kind: "lease",
resource: "fixture-retained-resource",
lease: {
release: async () => {
attempts += 1;
if (attempts === 1) throw new Error("controlled first retry failure");
},
},
});
try {
const result = await runCliJavaScriptApplicationAnalysis(
{},
{},
undefined,
undefined,
host,
);
expect(attempts).toBe(2);
expect(result).toMatchObject({ code: "invalid_request" });
} finally {
await host.close();
}
});
it("shares one in-flight JavaScript worker shutdown between callers", async () => {
await closeCliResources();
let calls = 0;