test(composition): consolidate one-shot finalization checks (#12977)

This commit is contained in:
morluto authored and GitHub committed 2026-11-20 22:26:15 -05:00
1 parent 225deea31d
commit fc1602eff4
1 file changed
+116 -116
+116 -116
View File
@@ -59,20 +59,25 @@ import { BinarySession } from "../../src/application/binary/BinarySession.js";
import { InvestigationRecords } from "../../src/application/investigation/InvestigationRecords.js";
import { AnalysisCleanupLease } from "../../src/resources/AnalysisCleanupLease.js";
const cleanupError = new ProviderCleanupError(
"fixture",
["fixture-provider-process"],
{ reason: "process exit could not be confirmed" },
{ remediationAction: "Close the retained fixture process before retrying." },
);
const createCleanupError = () =>
new ProviderCleanupError(
"fixture",
["fixture-provider-process"],
{ reason: "process exit could not be confirmed" },
{
remediationAction: "Close the retained fixture process before retrying.",
},
);
const primaryError = new AnalysisTimeoutError("read_bytes", 100, {
capturedOutput: {
stdout: "retained observation",
stderr: "",
truncated: false,
},
});
const CAPTURED_OUTPUT = {
stdout: "retained observation",
stderr: "",
truncated: false,
};
const createPrimaryError = () =>
new AnalysisTimeoutError("read_bytes", 100, {
capturedOutput: CAPTURED_OUTPUT,
});
const factories = (
options: {
@@ -199,6 +204,7 @@ it.each([false, true])(
await writeFile(join(source, "main.js"), "console.log('retained');\n");
await createPackageWithOptions(source, archive, {});
const session = await createArtifactBinarySession({});
const cleanupError = createCleanupError();
const nativeClose = session.close.bind(session);
const controller = new AbortController();
const reason = {
@@ -292,7 +298,7 @@ it.each([false, true])(
});
const { dependencies } = factories({
executionError: failure,
...(closeFails ? { cleanupError } : {}),
...(closeFails ? { cleanupError: createCleanupError() } : {}),
});
const result = analysisCliErrorEnvelopeSchema.parse(
await runDirectAnalysis(dependencies, callerPath, "read_bytes", {
@@ -412,9 +418,8 @@ describe("one-shot finalization cancellation", () => {
async (closeFails) => {
const [path] = await createBinarySessionTargets();
const controller = new AbortController();
const failure = new AnalysisTimeoutError("read_bytes", 100, {
capturedOutput: primaryError.capturedOutput,
});
const failure = createPrimaryError();
const cleanupError = createCleanupError();
const { dependencies } = factories({
executionError: failure,
onClose: () => controller.abort({ later: true }),
@@ -443,6 +448,7 @@ describe("one-shot finalization cancellation", () => {
it("retains completed Evidence and cancellation alongside unsuccessful cleanup", async () => {
const [path] = await createBinarySessionTargets();
const controller = new AbortController();
const cleanupError = createCleanupError();
const { dependencies } = factories({
cleanupError,
onClose: () => controller.abort({ during: "close" }),
@@ -493,7 +499,9 @@ describe("snapshot publication finalization", () => {
onClose: () => {
if (cancelled) controller.abort({ during: "snapshot finalization" });
},
...(mode.includes("close-failure") ? { cleanupError } : {}),
...(mode.includes("close-failure")
? { cleanupError: createCleanupError() }
: {}),
});
const stagingCleanup =
mode === "recovered-write-cancel"
@@ -597,7 +605,7 @@ it.each([false, true])(
reason: "snapshot removal failed during execution",
resources: ["primary-snapshot", "shared-owner"],
},
capturedOutput: primaryError.capturedOutput,
capturedOutput: CAPTURED_OUTPUT,
});
const closeError = new ProviderCleanupError(
"fixture",
@@ -639,39 +647,57 @@ it.each([false, true])(
);
describe("one-shot analysis cleanup outcomes", () => {
it("retains successful evidence and reports CLI failure when close fails", async () => {
const [path] = await createBinarySessionTargets();
const { dependencies, session } = factories({ cleanupError });
const result = await runDirectAnalysis(dependencies, path, "read_bytes", {
const readBytes = (dependencies: DirectAnalysisDependencies, path: string) =>
runDirectAnalysis(dependencies, path, "read_bytes", {
address: "0x1000",
}).then(projectCliAnalysisResult);
expect(result).toMatchObject({
error: "Analysis failed",
code: "cleanup_incomplete",
details: {
resources: ["fixture-provider-process"],
partial_observation: {
operation: "read_bytes",
subject: { local_path: path },
normalized_result: { observation: "retained result" },
raw_result: null,
it.each([
["typed cleanup failure", "fixture-provider-process"],
["unexpected close rejection", "provider-client"],
] as const)(
"retains completed Evidence in the CLI response to %s",
async (kind, resource) => {
const [path] = await createBinarySessionTargets();
const { dependencies, session } = factories(
kind === "typed cleanup failure"
? { cleanupError: createCleanupError() }
: { thrownCleanup: new Error("unexpected close rejection") },
);
const result = await readBytes(dependencies, path);
expect(result).toMatchObject({
error: "Analysis failed",
code: "cleanup_incomplete",
details: {
resources: [resource],
...(kind === "unexpected close rejection" && {
diagnostics: {
reason: "provider client close rejected unexpectedly",
},
}),
partial_observation: {
operation: "read_bytes",
subject: { local_path: path },
normalized_result: { observation: "retained result" },
raw_result: null,
},
},
},
});
expect(isCliOperationFailure(result)).toBe(true);
expect(session.activeTarget()).toBeUndefined();
});
});
expect(isCliOperationFailure(result)).toBe(true);
expect(session.activeTarget()).toBeUndefined();
},
);
it("preserves primary failure diagnostics alongside an unsuccessful close", async () => {
const [path] = await createBinarySessionTargets();
const primaryError = createPrimaryError();
const cleanupError = createCleanupError();
const { dependencies } = factories({
executionError: primaryError,
cleanupError,
});
const primary = projectAnalysisError(primaryError);
const result = await runDirectAnalysis(dependencies, path, "read_bytes", {
address: "0x1000",
}).then(projectCliAnalysisResult);
const result = await readBytes(dependencies, path);
const callerError = analysisCliErrorEnvelopeSchema.parse(result);
expect(callerError).toMatchObject({
code: "cleanup_incomplete",
@@ -693,35 +719,12 @@ describe("one-shot analysis cleanup outcomes", () => {
expect(isCliOperationFailure(result)).toBe(true);
});
it("reports provider close rejection with the successful observation", async () => {
const [path] = await createBinarySessionTargets();
const { dependencies } = factories({
thrownCleanup: new Error("unexpected close rejection"),
});
const result = await runDirectAnalysis(dependencies, path, "read_bytes", {
address: "0x1000",
}).then(projectCliAnalysisResult);
expect(result).toMatchObject({
code: "cleanup_incomplete",
details: {
resources: ["provider-client"],
diagnostics: { reason: "provider client close rejected unexpectedly" },
partial_observation: {
normalized_result: { observation: "retained result" },
},
},
});
});
it("retains both unexpected execution failure and cleanup failure", async () => {
const [path] = await createBinarySessionTargets();
const thrownExecution = new Error("unexpected execution rejection");
const cleanupError = createCleanupError();
const { dependencies } = factories({ thrownExecution, cleanupError });
await expect(
runDirectAnalysis(dependencies, path, "read_bytes", {
address: "0x1000",
}).then(projectCliAnalysisResult),
).rejects.toMatchObject({
await expect(readBytes(dependencies, path)).rejects.toMatchObject({
cause: thrownExecution,
errors: [thrownExecution, cleanupError],
});
@@ -730,9 +733,7 @@ describe("one-shot analysis cleanup outcomes", () => {
it("keeps successful evidence unchanged after confirmed cleanup", async () => {
const [path] = await createBinarySessionTargets();
const { dependencies, session } = factories();
const result = await runDirectAnalysis(dependencies, path, "read_bytes", {
address: "0x1000",
}).then(projectCliAnalysisResult);
const result = await readBytes(dependencies, path);
expect(parseEvidence(result)).toMatchObject({
operation: "read_bytes",
normalized_result: { observation: "retained result" },
@@ -742,55 +743,51 @@ describe("one-shot analysis cleanup outcomes", () => {
});
});
it.each(["complete", "summary"] as const)(
"retains completed Evidence when cancellation arrives during managed close before %s delivery",
async (detail) => {
const [path] = await createBinarySessionTargets();
const files = (await readdir(dirname(path))).sort();
const original = parseEvidence(
await runProviderAnalysis(
factories().dependencies,
path,
"inspect_managed_members",
{},
).then(projectCliAnalysisResult),
);
const controller = new AbortController();
const reason = new Error("cancel during managed close");
const { dependencies, session } = factories({
onClose: () => controller.abort(reason),
});
const analysis = await runProviderAnalysis(
dependencies,
it("retains completed Evidence after managed close cancellation in inline and requested summary delivery", async () => {
const [path] = await createBinarySessionTargets();
const files = (await readdir(dirname(path))).sort();
const original = parseEvidence(
await runProviderAnalysis(
factories().dependencies,
path,
"inspect_managed_members",
{},
undefined,
controller.signal,
);
// The CLI delivery receives the cancelled run and publishes nothing.
const result =
detail === "summary"
? await publishCliAnalysisEvidence(analysis, {
operation: "inspect_managed_members",
signal: controller.signal,
detail,
evidenceOutput: join(dirname(path), "bundle.json"),
})
: projectCliAnalysisResult(analysis);
expect(result).toEqual({
error: "Analysis failed",
...projectAnalysisError(
new AnalysisCancelledError("inspect_managed_members", {
cause: reason,
partialObservation: original,
}),
),
});
expect(session.activeTarget()).toBeUndefined();
expect((await readdir(dirname(path))).sort()).toEqual(files);
},
);
).then(projectCliAnalysisResult),
);
const controller = new AbortController();
const reason = new Error("cancel during managed close");
const { dependencies, session } = factories({
onClose: () => controller.abort(reason),
});
const analysis = await runProviderAnalysis(
dependencies,
path,
"inspect_managed_members",
{},
undefined,
controller.signal,
);
const expected = {
error: "Analysis failed",
...projectAnalysisError(
new AnalysisCancelledError("inspect_managed_members", {
cause: reason,
partialObservation: original,
}),
),
};
expect(projectCliAnalysisResult(analysis)).toEqual(expected);
expect(
await publishCliAnalysisEvidence(analysis, {
operation: "inspect_managed_members",
signal: controller.signal,
detail: "summary",
evidenceOutput: join(dirname(path), "bundle.json"),
}),
).toEqual(expected);
expect(session.activeTarget()).toBeUndefined();
expect((await readdir(dirname(path))).sort()).toEqual(files);
});
it("preserves a managed provider failure when cancellation arrives during close", async () => {
const [path] = await createBinarySessionTargets();
@@ -828,6 +825,7 @@ it("preserves a managed provider failure when cancellation arrives during close"
describe("managed one-shot cleanup outcomes", () => {
it("returns a managed cleanup error retaining the complete successful execution", async () => {
const [path] = await createBinarySessionTargets();
const cleanupError = createCleanupError();
const { dependencies } = factories({ cleanupError });
const result = await runManagedProviderExecution(
dependencies,
@@ -860,6 +858,8 @@ describe("managed one-shot cleanup outcomes", () => {
it("retains the typed managed primary error when close also fails", async () => {
const [path] = await createBinarySessionTargets();
const primaryError = createPrimaryError();
const cleanupError = createCleanupError();
const { dependencies } = factories({
executionError: primaryError,
cleanupError,
@@ -898,7 +898,7 @@ describe("managed one-shot cleanup outcomes", () => {
it("exposes managed cleanup failure to the CLI error classifier", async () => {
const [path] = await createBinarySessionTargets();
const { dependencies } = factories({ cleanupError });
const { dependencies } = factories({ cleanupError: createCleanupError() });
const result = await runProviderAnalysis(
dependencies,
path,