fix(source-maps): preserve decoder lifecycle facts (#10069)

* fix(source-maps): preserve decoder lifecycle facts

* fix(source-maps): preserve preparation failure on cancellation

* fix(source-maps): retain preparation and retry cleanup facts
This commit is contained in:
morluto authored and GitHub committed 2026-11-11 18:36:02 +08:00
1 parent d36ba58971
commit 8a2eb4ab61
4 files changed
+368 -32

No files matched your search

@@ -1,5 +1,7 @@
import { access } from "node:fs/promises";
import { expect, it } from "vitest";
import { ProviderCleanupError } from "../../domain/providerCleanupError.js";
import { DarwinProcessOwnershipInspectionError } from "../../process/DarwinProcessRunTokenReader.js";
import { SourceMapDecoder } from "./SourceMapDecoder.js";
import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js";
import { projectAnalysisError } from "../../domain/analysisErrorProjection.js";
@@ -35,19 +37,23 @@ it("preserves the exact caller reason before source-map admission", async () =>
expect(response.error.cause).toBe(reason);
});
it("retains failed cleanup ownership and retries it before another decode", async () => {
it("retains physical failure and late cancellation through cleanup failure and retry", async () => {
const root = await PrivateRuntimeRoot.create({
prefix: "rea-source-map-cleanup-retry-test-",
});
let cleanupAttempts = 0;
const controller = new AbortController();
const reason = { caller: "after physical exit during final cleanup" };
const dependencies: SourceMapDecoderDependencies = {
prepareOwnership: async () => {},
createRuntime: async () => ({
path: root.path,
close: async () => {
cleanupAttempts += 1;
if (cleanupAttempts === 1)
if (cleanupAttempts === 1) {
controller.abort(reason);
throw new Error("injected root cleanup failure");
}
await root.close();
},
}),
@@ -62,7 +68,7 @@ it("retains failed cleanup ownership and retries it before another decode", asyn
};
const decoder = new SourceMapDecoder(dependencies);
try {
const failed = await decoder.trace(input);
const failed = await decoder.trace(input, { signal: controller.signal });
expect(failed.ok).toBe(false);
if (failed.ok)
throw new Error("expected physical failure with cleanup failure");
@@ -75,6 +81,11 @@ it("retains failed cleanup ownership and retries it before another decode", asyn
},
});
expect(primary?.capturedOutput?.stderr).toBe("physical exit 7");
expect(primary?.secondaryFailures).toHaveLength(1);
expect(primary?.secondaryFailures[0]).toBeInstanceOf(
AnalysisCancelledError,
);
expect(primary?.secondaryFailures[0]?.cause).toBe(reason);
expect(failed.error.capturedOutput).toEqual(primary?.capturedOutput);
expect(failed.error.cause).toBeInstanceOf(AggregateError);
expect((failed.error.cause as AggregateError).errors[0]).toBe(primary);
@@ -104,6 +115,8 @@ it("retains failed cleanup ownership and retries it before another decode", asyn
expect(failed.error.cleanupResources).toEqual([]);
expect(projectAnalysisError(failed.error).code).toBe("execution_failure");
expect(failed.error.cleanupPrimary).toBe(primary);
expect(primary?.secondaryFailures).toHaveLength(1);
expect(primary?.secondaryFailures[0]?.cause).toBe(reason);
expect(failed.error.diagnostics).toMatchObject({
cleanup_error: {
details: {
@@ -241,3 +254,160 @@ it("attributes cleanup resources to the remaining process and directory owners",
await decoder.close();
}
});
it.each(["already closed", "queued at close"] as const)(
"reports source-map readiness without inventing cleanup ownership when %s",
async (phase) => {
const decoder = new SourceMapDecoder();
const response =
phase === "queued at close" ? decoder.trace(input) : undefined;
await decoder.close();
const result = await (response ?? decoder.trace(input));
if (result.ok) throw new Error("Expected closed decoder refusal");
expect(result.error._tag).toBe("AnalysisCapabilityUnavailableError");
expect(result.error.cleanupIncomplete).toBe(false);
expect(result.error.cleanupResources).toEqual([]);
await decoder.close();
},
);
it("retains cancellation while retrying an earlier failed cleanup", async () => {
const root = await PrivateRuntimeRoot.create({
prefix: "rea-source-map-retry-abort-",
});
const controller = new AbortController();
const reason = { caller: "during retry" };
let attempts = 0;
let release = false;
const decoder = new SourceMapDecoder({
prepareOwnership: async () => {},
createRuntime: async () => ({
path: root.path,
close: async () => {
attempts++;
if (!release) {
if (attempts === 2) controller.abort(reason);
throw new Error("root close failed");
}
await root.close();
},
}),
launcher: async (options) =>
spawnOwnedProviderProcess({
...options,
arguments: [
"-e",
'process.stderr.write("physical exit 7"); process.exit(7)',
],
}),
});
try {
const first = await decoder.trace(input);
if (first.ok) throw new Error("expected retained cleanup");
const retry = await decoder.trace(input, { signal: controller.signal });
if (retry.ok) throw new Error("expected retained cleanup on retry");
expect(controller.signal.aborted).toBe(true);
const cancellation = retry.error.secondaryFailures.find(
(failure) => failure instanceof AnalysisCancelledError,
);
expect(cancellation?.cause).toBe(reason);
expect(retry.error.cleanupIncomplete).toBe(true);
expect(retry.error.cleanupResources).toContain(root.path);
await expect(access(root.path)).resolves.toBeUndefined();
release = true;
await decoder.close();
await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" });
expect(first.error.cleanupIncomplete).toBe(false);
} finally {
release = true;
await decoder.close();
}
});
it("retains Darwin preparation cleanup ownership and cancellation through retry", async () => {
const root = await PrivateRuntimeRoot.create({
prefix: "rea-source-map-prep-owner-",
});
const controller = new AbortController();
const reason = { caller: "after preparation failure" };
const cleanup = new ProviderCleanupError("darwin-helper", [root.path], {
reason: "held",
});
let release = false;
const physical = new DarwinProcessOwnershipInspectionError(
"compiler failure",
{
cleanupFailure: cleanup,
cleanupOwner: {
close: async () => {
if (!release) throw new Error("still held");
await root.close();
cleanup.resolveCleanup();
},
},
},
);
const decoder = new SourceMapDecoder({
prepareOwnership: async () => {
controller.abort(reason);
throw physical;
},
});
try {
const failed = await decoder.trace(input, { signal: controller.signal });
if (failed.ok) throw new Error("expected failed preparation");
expect(failed.error.cleanupIncomplete).toBe(true);
expect(failed.error.cleanupResources).toContain(root.path);
expect(failed.error.cleanupPrimary?.cause).toBe(physical);
expect(
failed.error.cleanupPrimary?.secondaryFailures.find(
(entry) => entry instanceof AnalysisCancelledError,
)?.cause,
).toBe(reason);
await expect(decoder.close()).rejects.toMatchObject({
cleanupIncomplete: true,
});
expect(failed.error.cleanupResources).toContain(root.path);
await expect(access(root.path)).resolves.toBeUndefined();
release = true;
await decoder.close();
await expect(access(root.path)).rejects.toMatchObject({ code: "ENOENT" });
expect(failed.error.cleanupIncomplete).toBe(false);
} finally {
release = true;
await decoder.close();
await root.close();
}
});
it("does not claim an opaque caller reason's Darwin cleanup owner", async () => {
const controller = new AbortController();
const cleanup = new ProviderCleanupError("foreign", ["opaque-resource"], {
reason: "opaque",
});
let ownerClosed = false;
const reason = new DarwinProcessOwnershipInspectionError(
"caller supplied reason",
{
cleanupFailure: cleanup,
cleanupOwner: {
close: async () => {
ownerClosed = true;
},
},
},
);
const decoder = new SourceMapDecoder({
prepareOwnership: async () => {
controller.abort(reason);
throw reason;
},
});
const result = await decoder.trace(input, { signal: controller.signal });
if (result.ok) throw new Error("expected cancellation");
expect(result.error).toBeInstanceOf(AnalysisCancelledError);
expect(result.error.cause).toBe(reason);
expect(result.error.cleanupIncomplete).toBe(false);
await decoder.close();
expect(ownerClosed).toBe(false);
});