fix(artifacts)!: preserve descriptor cleanup authority

Keep rejected closes unresolved after descriptor invalidation and share concurrent close attempts. Preserve primary admission failures and transfer the exact failed handle owner into inventory scopes.

BREAKING CHANGE: ArtifactResourceScope file-handle owners require OwnedFileHandle. Root inventory sources retain their single cleanup owner.
This commit is contained in:
morluto
2026-10-11 05:17:10 +08:00
parent 32d13c431e
commit be6272f261
10 changed files with 443 additions and 57 deletions
@@ -0,0 +1,68 @@
import type { Stats } from "node:fs";
import type { FileHandle } from "node:fs/promises";
import { afterEach, expect, it, vi } from "vitest";
import {
NonRegularFileReadError,
openRegularFile,
RegularFileAdmissionFailure,
} from "../filesystem/RegularFile.js";
const { mockOpen } = vi.hoisted(() => ({ mockOpen: vi.fn() }));
vi.mock("node:fs/promises", async (importOriginal) => ({
...(await importOriginal<typeof import("node:fs/promises")>()),
open: mockOpen,
}));
afterEach(() => mockOpen.mockReset());
it.each(["cancellation after open", "non-regular metadata", "stat failure"])(
"retains the exact owner when close fails after %s",
async (failureCase) => {
const closeFailure = new Error("native close failed");
const statFailure = new Error("descriptor stat failed");
const cancellation = new Error("cancelled after open");
const controller = new AbortController();
const file = {
fd: 17,
stat: vi.fn(async () => {
if (failureCase === "cancellation after open") {
controller.abort(cancellation);
return { isFile: () => true } as Stats;
}
if (failureCase === "non-regular metadata")
return {
isFile: () => false,
isDirectory: () => false,
} as Stats;
throw statFailure;
}),
close: vi.fn(async () => {
file.fd = -1;
throw closeFailure;
}),
};
mockOpen.mockResolvedValue(file as unknown as FileHandle);
const cause = await openRegularFile("/selected/input", {
symlinks: "reject",
signal: controller.signal,
}).catch((failure: unknown) => failure);
expect(cause).toBeInstanceOf(RegularFileAdmissionFailure);
if (!(cause instanceof RegularFileAdmissionFailure))
throw new Error("Expected an admission failure that retains its owner");
expect(cause.owner.handle).toBe(file);
expect(cause.cleanupCause).toBe(closeFailure);
expect(file.close).toHaveBeenCalledTimes(1);
await expect(cause.owner.close()).rejects.toBe(closeFailure);
expect(file.close).toHaveBeenCalledTimes(1);
if (failureCase === "non-regular metadata")
expect(cause.cause).toBeInstanceOf(NonRegularFileReadError);
else if (failureCase === "stat failure")
expect(cause.cause).toBe(statFailure);
else expect(cause.cause).toBe(cancellation);
},
);