fix(native): retain signature snapshot cleanup ownership

Use the shared resource scope and private runtime root for signature snapshots. Retain failed file-handle/root cleanup, preserve primary failures alongside cleanup diagnostics, and wait for binding operations during client close before retrying owned resources.

Validate root cleanup failure, blocked retry, repeated client close and in-flight acquisition. Existing provider/MCP/CLI tests and the real Darwin codesign ancestor-swap case pass. Dedicated native file-handle close-failure injection remains unverified.
This commit is contained in:
morluto
2026-10-13 08:08:40 +08:00
parent 9a18251426
commit ebc22d99d6
5 changed files with 467 additions and 87 deletions
@@ -15,6 +15,7 @@ import {
import { dirname, join } from "node:path";
import { describe, expect, it } from "vitest";
import { NativeMacOSProvider } from "../../../../src/native/NativeMacOSProvider.js";
import { ArtifactResourceScope } from "../../../../src/artifacts/ArtifactResourceScope.js";
import {
withSignatureTarget,
verifySignatureTarget,
@@ -277,6 +278,7 @@ it("returns typed cancellation at binding entry and final verification", async (
await expect(
withSignatureTarget(
target,
new ArtifactResourceScope(),
async () => {
throw new Error("Cancelled acquisition must not invoke inspection");
},
@@ -286,14 +288,18 @@ it("returns typed cancellation at binding entry and final verification", async (
ok: false,
error: { _tag: "AnalysisCancelledError" },
});
await withSignatureTarget(target, async (binding) => {
const result = await verifySignatureTarget(target, binding, signal);
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError" },
});
return result;
});
await withSignatureTarget(
target,
new ArtifactResourceScope(),
async (binding) => {
const result = await verifySignatureTarget(target, binding, signal);
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisCancelledError" },
});
return result;
},
);
});
it("preserves cancellation after a capture instead of classifying it as invalid output", async () => {
@@ -348,21 +354,31 @@ it("copies and hashes the same bytes across multiple snapshot chunks", async ()
const bytes = Buffer.alloc(128 * 1024 + 13, 42);
await writeFile(path, bytes);
const target = await nativeMachoTargetForFile(path);
await withSignatureTarget(target, async (binding) => {
expect(await readFile(binding.snapshotPath)).toEqual(bytes);
return verifySignatureTarget(target, binding);
});
await withSignatureTarget(
target,
new ArtifactResourceScope(),
async (binding) => {
expect(await readFile(binding.snapshotPath)).toEqual(bytes);
return verifySignatureTarget(target, binding);
},
);
});
it("cleans the private snapshot when inspection throws", async () => {
const { target } = await fixture();
let snapshotPath: string | undefined;
await expect(
withSignatureTarget(target, async (binding) => {
const result = await withSignatureTarget(
target,
new ArtifactResourceScope(),
async (binding) => {
snapshotPath = binding.snapshotPath;
throw new Error("Invalid parsed output");
}),
).rejects.toThrow("Invalid parsed output");
},
);
expect(result).toMatchObject({
ok: false,
error: { _tag: "AnalysisOutputError" },
});
if (snapshotPath === undefined) throw new Error("Expected snapshot");
await expect(lstat(dirname(snapshotPath))).rejects.toMatchObject({
code: "ENOENT",