mirror of
https://github.com/morluto/rea.git
synced 2026-11-16 11:43:20 +00:00
* refactor(providers): finalize apktool and shared artifact resources through runOwned runOwned moves to the provider layer so provider sites can use it without depending on application workflows. It gains a completed-value projection (partial) for values that are not themselves partial observations, an optional signal, and cleanupLeases that bind a close failure to the exact leases whose confirmed release resolves it. ArtifactAnalysisResources.finish is removed; TShark and sigrok finalize through runOwned with the shared release as close. Apktool's hand-written release, cancellation and lease-group composition is replaced by runOwned. A failed primary now keeps a cancellation observed during final cleanup as a secondary failure, as the documented precedence requires. A counted architecture guard refuses new hand-written completed-result abort reclassification outside runOwned. * refactor(adb): finalize runtime-root work through runOwned (#10660) * refactor(adb): finalize runtime-root work through runOwned The ADB runtime-root finalizer re-derived the precedence by hand and kept its own cancellation-with-cleanup combiner. It now runs through runOwned: the work body maps snapshot failures, close releases the root lease and binds its own cleanup error, and partial projects the completed execution. A cancellation whose cleanup fails is now the shared cleanup composite with the cancellation as its primary, as runOwned produces for every other owner. * refactor(providers): keep completed values as diagnostics in runOwned and migrate four finalizers (#10691) * refactor(providers): keep completed values as diagnostics in runOwned and migrate signature, Ghidra, bundle and LLDB finalization * test(providers): run the lifecycle scenarios over every runOwned adopter shape (#10707) One table of testing.md lifecycle rows runs over partial, diagnostics, publishing and lease adopters, replacing the hand-written precedence cases it covers. * chore(lint): move runOwned's classifier baseline entry with the file
1044 lines
34 KiB
TypeScript
1044 lines
34 KiB
TypeScript
import { EventEmitter } from "node:events";
|
|
import { createHash } from "node:crypto";
|
|
import {
|
|
chmod,
|
|
mkdtemp,
|
|
rm,
|
|
readFile,
|
|
stat,
|
|
writeFile,
|
|
open,
|
|
type FileHandle,
|
|
} from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import { afterEach, expect, it, vi, onTestFinished } from "vitest";
|
|
import { withVerifierCleanup } from "../../scripts/lib/verifier-cleanup.mjs";
|
|
import { awaitAcknowledgement } from "../../tests/fixtures/lifecycleAcknowledgement.js";
|
|
|
|
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
|
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
|
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
|
import { ok, err } from "../domain/result.js";
|
|
import {
|
|
NativeCommandFailure,
|
|
XcrunCommandRunner,
|
|
type NativeCommandRunner,
|
|
} from "./CommandRunner.js";
|
|
import { ProviderProcessSupervisor } from "../process/ProviderProcess.js";
|
|
import type { NativeCallTracer } from "./LldbCallTracer.js";
|
|
import { NativeMacOSProvider } from "./NativeMacOSProvider.js";
|
|
import {
|
|
verifySignatureTarget,
|
|
withSignatureTarget,
|
|
} from "./SignatureTargetBinding.js";
|
|
import { OwnedFileHandle } from "../filesystem/OwnedFileHandle.js";
|
|
import {
|
|
primaryAnalysisError,
|
|
resolvedAnalysisError,
|
|
} from "../domain/analysisErrorBase.js";
|
|
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
|
import { AnalysisOutputError } from "../domain/analysisErrorCore.js";
|
|
import { signatureInspectionFixture } from "../../tests/fixtures/nativeCommands.js";
|
|
|
|
const unusedInspection = async (): Promise<never> => {
|
|
throw new Error("Inspection must not run after failed target binding");
|
|
};
|
|
const roots: string[] = [];
|
|
const targets: string[] = [];
|
|
const unusedRunner: NativeCommandRunner = {
|
|
async run() {
|
|
throw new Error("Changed target must be rejected before a command runs");
|
|
},
|
|
};
|
|
const unusedTracer: NativeCallTracer = {
|
|
async trace() {
|
|
throw new Error("Signature inspection must not start a trace");
|
|
},
|
|
async close() {
|
|
return ok(null);
|
|
},
|
|
};
|
|
|
|
afterEach(async () => {
|
|
await Promise.all(
|
|
[...roots, ...targets].map((path) =>
|
|
rm(path, { recursive: true, force: true }),
|
|
),
|
|
);
|
|
roots.length = 0;
|
|
targets.length = 0;
|
|
});
|
|
|
|
const createTarget = async (
|
|
bytes: string,
|
|
digest = bytes,
|
|
): Promise<BinaryTarget> => {
|
|
const directory = await mkdtemp(join(tmpdir(), "rea-signature-target-"));
|
|
targets.push(directory);
|
|
const path = join(directory, "target.bin");
|
|
await writeFile(path, bytes);
|
|
return {
|
|
path,
|
|
sha256: createHash("sha256").update(digest).digest("hex"),
|
|
kind: "executable",
|
|
format: "mach-o",
|
|
architecture: "arm64",
|
|
availableArchitectures: ["arm64"],
|
|
};
|
|
};
|
|
|
|
const createRoot = async (failedAttempts = 0) => {
|
|
const path = await mkdtemp(join(tmpdir(), "rea-signature-root-"));
|
|
roots.push(path);
|
|
let attempts = 0;
|
|
return {
|
|
path,
|
|
async close() {
|
|
if (attempts++ < failedAttempts) {
|
|
throw new Error("injected root cleanup failure");
|
|
}
|
|
await rm(path, { recursive: true, force: true });
|
|
},
|
|
};
|
|
};
|
|
|
|
it("retains a snapshot root after cleanup fails alongside a digest mismatch", async () => {
|
|
const target = await createTarget("observed", "expected");
|
|
const resources = new ArtifactResourceScope();
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
undefined,
|
|
() => createRoot(1),
|
|
);
|
|
|
|
expect(result.ok).toBe(false);
|
|
if (result.ok) return;
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual(expect.arrayContaining(roots));
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
details: { diagnostics: { primary_error: { code: "artifact_changed" } } },
|
|
});
|
|
|
|
await resources.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
expect(resolvedAnalysisError(result.error)._tag).toBe(
|
|
"AnalysisArtifactChangedError",
|
|
);
|
|
await expect(stat(roots[0]!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
|
|
it("retains and retries snapshot cleanup after snapshot identity drift", async () => {
|
|
const target = await createTarget("registered bytes");
|
|
const resources = new ArtifactResourceScope();
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
async (binding) => {
|
|
await chmod(binding.snapshotPath, 0o600);
|
|
await writeFile(binding.snapshotPath, "replacement snapshot bytes");
|
|
const verified = await verifySignatureTarget(target, binding);
|
|
return verified.ok ? ok(signatureInspectionFixture()) : verified;
|
|
},
|
|
undefined,
|
|
() => createRoot(1),
|
|
);
|
|
|
|
expect(result.ok).toBe(false);
|
|
if (result.ok) return;
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(projectAnalysisError(result.error)).toMatchObject({
|
|
details: {
|
|
diagnostics: {
|
|
primary_error: { code: "artifact_changed" },
|
|
},
|
|
},
|
|
});
|
|
|
|
await resources.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(resolvedAnalysisError(result.error)._tag).toBe(
|
|
"AnalysisArtifactChangedError",
|
|
);
|
|
expect(await readFile(target.path, "utf8")).toBe("registered bytes");
|
|
await expect(stat(roots[0]!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
|
|
it("returns a retained cleanup failure when the next call cannot retry its root", async () => {
|
|
const target = await createTarget("observed", "expected");
|
|
const resources = new ArtifactResourceScope();
|
|
const createFailingRoot = async () => {
|
|
const path = await mkdtemp(join(tmpdir(), "rea-signature-root-sticky-"));
|
|
roots.push(path);
|
|
return {
|
|
path,
|
|
close: async () => {
|
|
throw new Error("still blocked");
|
|
},
|
|
};
|
|
};
|
|
const first = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
undefined,
|
|
createFailingRoot,
|
|
);
|
|
expect(first.ok).toBe(false);
|
|
const second = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
undefined,
|
|
createRoot,
|
|
);
|
|
expect(second.ok).toBe(false);
|
|
if (second.ok) return;
|
|
expect(second.error.cleanupIncomplete).toBe(true);
|
|
expect(second.error.cleanupResources).toEqual(expect.arrayContaining(roots));
|
|
});
|
|
|
|
it("retries a retained signature root when the provider client closes", async () => {
|
|
const target = await createTarget("observed", "expected");
|
|
const provider = new NativeMacOSProvider(
|
|
{},
|
|
unusedRunner,
|
|
"darwin",
|
|
() => unusedTracer,
|
|
() => createRoot(2),
|
|
);
|
|
const client = provider.createClient(target);
|
|
const result = await client.execute("inspect_signature", {});
|
|
|
|
expect(result.ok).toBe(false);
|
|
if (result.ok) return;
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual(expect.arrayContaining(roots));
|
|
const firstClose = await client.close();
|
|
expect(firstClose).toMatchObject({
|
|
ok: false,
|
|
error: { cleanupIncomplete: true, cleanupResources: roots },
|
|
});
|
|
expect((await stat(roots[0]!)).isDirectory()).toBe(true);
|
|
await expect(client.close()).resolves.toMatchObject({
|
|
ok: true,
|
|
value: null,
|
|
});
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
if (!firstClose.ok) {
|
|
expect(firstClose.error.cleanupIncomplete).toBe(false);
|
|
expect(firstClose.error.cleanupResources).toEqual([]);
|
|
}
|
|
await expect(stat(roots[0]!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
|
|
it("waits for an in-flight signature binding before provider close", async () => {
|
|
const target = await createTarget("observed", "expected");
|
|
let rootFactoryEntered!: () => void;
|
|
let releaseRootFactory!: () => void;
|
|
const entered = new Promise<void>(
|
|
(resolve) => (rootFactoryEntered = resolve),
|
|
);
|
|
const gate = new Promise<void>((resolve) => (releaseRootFactory = resolve));
|
|
const provider = new NativeMacOSProvider(
|
|
{},
|
|
unusedRunner,
|
|
"darwin",
|
|
() => unusedTracer,
|
|
async () => {
|
|
rootFactoryEntered();
|
|
await gate;
|
|
return createRoot();
|
|
},
|
|
);
|
|
const client = provider.createClient(target);
|
|
const operation = client.execute("inspect_signature", {});
|
|
let closeSettled = false;
|
|
let closing: ReturnType<typeof client.close> | undefined;
|
|
await withVerifierCleanup(async () => {
|
|
await awaitAcknowledgement(
|
|
entered,
|
|
operation,
|
|
"signature root acquisition",
|
|
);
|
|
closing = client.close().finally(() => {
|
|
closeSettled = true;
|
|
});
|
|
await Promise.resolve();
|
|
expect(closeSettled).toBe(false);
|
|
releaseRootFactory();
|
|
await expect(operation).resolves.toMatchObject({
|
|
ok: false,
|
|
error: { _tag: "AnalysisArtifactChangedError" },
|
|
});
|
|
await expect(closing).resolves.toMatchObject({ ok: true, value: null });
|
|
expect(closeSettled).toBe(true);
|
|
await expect(stat(roots[0]!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
}, [
|
|
releaseRootFactory,
|
|
async () => {
|
|
await operation;
|
|
},
|
|
async () => {
|
|
const result = await closing;
|
|
if (result !== undefined && !result.ok) throw result.error;
|
|
},
|
|
async () => {
|
|
const result = await client.close();
|
|
if (!result.ok) throw result.error;
|
|
},
|
|
]);
|
|
});
|
|
|
|
it("releases retained descriptors before retrying their containing root", async () => {
|
|
const target = await createTarget("observed", "expected");
|
|
const resources = new ArtifactResourceScope();
|
|
const original = OwnedFileHandle.prototype.close;
|
|
let blocked: OwnedFileHandle | undefined;
|
|
let attempts = 0;
|
|
const close = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementation(function (this: OwnedFileHandle) {
|
|
blocked ??= this;
|
|
if (this === blocked && ++attempts <= 2)
|
|
return Promise.reject(
|
|
new Error("controlled snapshot descriptor close failure"),
|
|
);
|
|
return original.call(this);
|
|
});
|
|
onTestFinished(async () => {
|
|
close.mockRestore();
|
|
await resources.close();
|
|
});
|
|
const inspect = vi.fn(unusedInspection);
|
|
const first = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
inspect,
|
|
undefined,
|
|
() => createRoot(1),
|
|
);
|
|
if (first.ok) throw new Error("Expected both retained owners");
|
|
const snapshot = join(roots[0]!, "target.bin");
|
|
expect(new Set(first.error.cleanupResources)).toEqual(
|
|
new Set([snapshot, roots[0]!]),
|
|
);
|
|
const retried = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
inspect,
|
|
undefined,
|
|
createRoot,
|
|
);
|
|
if (retried.ok) throw new Error("Expected retained root retry failure");
|
|
expect(first.error.cleanupIncomplete).toBe(true);
|
|
expect(first.error.cleanupResources).toEqual([roots[0]!]);
|
|
expect(retried.error.cleanupResources).toEqual([roots[0]!]);
|
|
expect(roots).toHaveLength(1);
|
|
expect(inspect).not.toHaveBeenCalled();
|
|
expect((await stat(roots[0]!)).isDirectory()).toBe(true);
|
|
await resources.close();
|
|
expect(attempts).toBe(3);
|
|
await expect(stat(roots[0]!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
for (const failure of [first.error, retried.error]) {
|
|
expect(failure.cleanupIncomplete).toBe(false);
|
|
expect(failure.cleanupResources).toEqual([]);
|
|
}
|
|
expect(resolvedAnalysisError(first.error)._tag).toBe(
|
|
"AnalysisArtifactChangedError",
|
|
);
|
|
});
|
|
|
|
it("retains the snapshot root until its failed descriptor owner confirms release", async () => {
|
|
const target = await createTarget("observed", "expected");
|
|
const resources = new ArtifactResourceScope();
|
|
const original = OwnedFileHandle.prototype.close;
|
|
let blocked: OwnedFileHandle | undefined;
|
|
let allowClose = false;
|
|
const close = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementation(function (this: OwnedFileHandle) {
|
|
blocked ??= this;
|
|
if (this === blocked && !allowClose)
|
|
return Promise.reject(new Error("snapshot descriptor remains open"));
|
|
return original.call(this);
|
|
});
|
|
const root = await createRoot();
|
|
const removeRoot = vi.fn(root.close);
|
|
onTestFinished(async () => {
|
|
allowClose = true;
|
|
await resources.close();
|
|
close.mockRestore();
|
|
});
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
undefined,
|
|
async () => ({ path: root.path, close: removeRoot }),
|
|
);
|
|
if (result.ok) throw new Error("Expected retained snapshot descriptor");
|
|
expect(removeRoot).not.toHaveBeenCalled();
|
|
expect((await stat(root.path)).isDirectory()).toBe(true);
|
|
const retry = await resources.close().then(
|
|
() => undefined,
|
|
(cause: unknown) => cause,
|
|
);
|
|
expect(retry).toBeDefined();
|
|
expect(removeRoot).not.toHaveBeenCalled();
|
|
expect((await stat(root.path)).isDirectory()).toBe(true);
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
allowClose = true;
|
|
await resources.close();
|
|
expect(removeRoot).toHaveBeenCalledTimes(1);
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
expect(resolvedAnalysisError(result.error)._tag).toBe(
|
|
"AnalysisArtifactChangedError",
|
|
);
|
|
await expect(stat(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"retains completed signature facts when private-root cleanup fails: %s",
|
|
async (cleanupFails) => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const unrelated = new ArtifactResourceScope();
|
|
const facts = signatureInspectionFixture();
|
|
const root = await createRoot(cleanupFails ? 1 : 0);
|
|
try {
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
async () => ok(facts),
|
|
undefined,
|
|
async () => root,
|
|
);
|
|
if (!cleanupFails) {
|
|
expect(result).toEqual(ok(facts));
|
|
await expect(stat(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
return;
|
|
}
|
|
if (result.ok) throw new Error("Expected root cleanup failure");
|
|
expect(result.error).toBeInstanceOf(ProviderCleanupError);
|
|
if (!(result.error instanceof ProviderCleanupError))
|
|
throw new Error("Expected retained cleanup owner");
|
|
expect(result.error.partialObservation).toBe(facts);
|
|
expect(result.error.completedObservation).toBe(facts);
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([root.path]);
|
|
expect((await stat(root.path)).isDirectory()).toBe(true);
|
|
const peer = await withSignatureTarget(
|
|
target,
|
|
unrelated,
|
|
async () => ok(facts),
|
|
undefined,
|
|
createRoot,
|
|
);
|
|
expect(peer).toEqual(ok(facts));
|
|
await unrelated.close();
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual([root.path]);
|
|
await resources.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
expect(result.error.partialObservation).toBe(facts);
|
|
expect(result.error.completedObservation).toBe(facts);
|
|
await expect(stat(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
} finally {
|
|
await unrelated.close();
|
|
await resources.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("retains cancellation during failed cleanup retry without losing ownership", async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const reason = { caller: "cancelled during cleanup retry" };
|
|
const root = await createRoot();
|
|
let attempts = 0;
|
|
const create = async () => ({
|
|
path: root.path,
|
|
async close() {
|
|
attempts += 1;
|
|
if (attempts === 2) controller.abort(reason);
|
|
if (attempts <= 2) throw new Error("controlled root close failure");
|
|
await root.close();
|
|
},
|
|
});
|
|
try {
|
|
const first = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
async () => ok(signatureInspectionFixture()),
|
|
undefined,
|
|
create,
|
|
);
|
|
if (first.ok) throw new Error("Expected retained root");
|
|
const second = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
controller.signal,
|
|
create,
|
|
);
|
|
if (second.ok) throw new Error("Expected failed cleanup retry");
|
|
expect(second.error.cleanupIncomplete).toBe(true);
|
|
expect(second.error.cleanupResources).toContain(root.path);
|
|
expect(second.error.cleanupPrimary?.secondaryFailures).toContainEqual(
|
|
expect.objectContaining({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
}),
|
|
);
|
|
await resources.close();
|
|
expect(first.error.cleanupIncomplete).toBe(false);
|
|
expect(second.error.cleanupIncomplete).toBe(false);
|
|
await expect(stat(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
} finally {
|
|
await resources.close();
|
|
}
|
|
});
|
|
|
|
it("retains late cancellation when creating the signature root fails", async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const reason = { caller: "cancelled during root creation" };
|
|
const physical = Object.assign(new Error("denied root creation"), {
|
|
code: "EACCES",
|
|
});
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
controller.signal,
|
|
async () => {
|
|
controller.abort(reason);
|
|
throw physical;
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected root creation failure");
|
|
expect(result.error.cause).toBe(physical);
|
|
expect(result.error.secondaryFailures).toHaveLength(1);
|
|
expect(result.error.secondaryFailures[0]).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
});
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
await resources.close();
|
|
});
|
|
|
|
it("does not adopt a typed caller abort reason as a root-creation failure", async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const reason = new ProviderCleanupError("foreign", ["unowned"], {});
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
controller.signal,
|
|
async () => {
|
|
controller.abort(reason);
|
|
throw reason;
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected cancellation");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
});
|
|
expect(result.error.cleanupResources).not.toContain("unowned");
|
|
await resources.close();
|
|
});
|
|
|
|
it.each([false, true])(
|
|
"retains late cancellation after a signature failure while root cleanup fails: %s",
|
|
async (cleanupFails) => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const reason = { caller: "cancelled during root cleanup" };
|
|
const physical = new AnalysisOutputError(
|
|
"inspect_signature",
|
|
"inspection failed",
|
|
);
|
|
const root = await createRoot(cleanupFails ? 1 : 0);
|
|
try {
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
async () => err(physical),
|
|
controller.signal,
|
|
async () => ({
|
|
path: root.path,
|
|
async close() {
|
|
try {
|
|
await root.close();
|
|
} finally {
|
|
controller.abort(reason);
|
|
}
|
|
},
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Expected inspection failure");
|
|
expect(physical.secondaryFailures).toHaveLength(1);
|
|
expect(physical.secondaryFailures[0]).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
});
|
|
expect(result.error.cleanupIncomplete).toBe(cleanupFails);
|
|
await resources.close();
|
|
expect(resolvedAnalysisError(result.error)).toBe(physical);
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
await expect(stat(root.path)).rejects.toMatchObject({ code: "ENOENT" });
|
|
} finally {
|
|
await resources.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
const opaqueReasons = [
|
|
null,
|
|
0,
|
|
new ProviderCleanupError("foreign", ["unowned"], {}),
|
|
];
|
|
for (const cleanupFails of [false, true])
|
|
it.each(opaqueReasons)(
|
|
`retains completed signature facts and exact reason %s during root cleanup (fails: ${cleanupFails})`,
|
|
async (reason) => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const facts = signatureInspectionFixture();
|
|
const root = await createRoot(cleanupFails ? 1 : 0);
|
|
const close = root.close;
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
async () => ok(facts),
|
|
controller.signal,
|
|
async () => ({
|
|
path: root.path,
|
|
async close() {
|
|
try {
|
|
await close();
|
|
} finally {
|
|
controller.abort(reason);
|
|
}
|
|
},
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Expected finalization cancellation");
|
|
expect(result.error.partialObservation).toEqual(facts);
|
|
expect(result.error.cleanupIncomplete).toBe(cleanupFails);
|
|
expect(result.error.cleanupResources).not.toContain("unowned");
|
|
await resources.close();
|
|
const primary = resolvedAnalysisError(result.error);
|
|
expect(primary._tag).toBe("AnalysisCancelledError");
|
|
expect(primary.cause).toBe(reason);
|
|
expect(projectAnalysisError(primary)).toMatchObject({
|
|
code: "cancelled",
|
|
details: { partial_observation: facts },
|
|
});
|
|
},
|
|
);
|
|
|
|
for (const ownerIndex of [0, 1])
|
|
for (const cleanupFails of [false, true])
|
|
it(`does not inspect after cancellation during descriptor ${ownerIndex} cleanup (fails: ${cleanupFails})`, async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const reason = { stage: "descriptor-close" };
|
|
const original = OwnedFileHandle.prototype.close;
|
|
let calls = 0;
|
|
let blocked: OwnedFileHandle | undefined;
|
|
let allowClose = false;
|
|
const close = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementation(async function (this: OwnedFileHandle) {
|
|
const cancels = calls++ === ownerIndex;
|
|
if (cancels && cleanupFails) blocked = this;
|
|
try {
|
|
if (this === blocked && !allowClose)
|
|
throw new Error("controlled close failure");
|
|
await original.call(this);
|
|
} finally {
|
|
if (cancels) controller.abort(reason);
|
|
}
|
|
});
|
|
onTestFinished(async () => {
|
|
close.mockRestore();
|
|
await resources.close();
|
|
});
|
|
const inspect = vi.fn(async () => ok(signatureInspectionFixture()));
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
inspect,
|
|
controller.signal,
|
|
createRoot,
|
|
);
|
|
expect(result.ok).toBe(false);
|
|
if (result.ok)
|
|
throw new Error("Expected descriptor finalization cancellation");
|
|
expect(inspect).not.toHaveBeenCalled();
|
|
expect(result.error.cleanupIncomplete).toBe(cleanupFails);
|
|
allowClose = true;
|
|
await resources.close();
|
|
expect(resolvedAnalysisError(result.error)).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
// The completed binding is not an observation; it stays as diagnostics.
|
|
diagnostics: {
|
|
snapshot_binding: {
|
|
identity: expect.any(String),
|
|
snapshot_path: expect.stringContaining("target.bin"),
|
|
},
|
|
},
|
|
});
|
|
});
|
|
|
|
it("keeps a physical source read failure primary when the caller also aborts", async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const probe = await open(target.path, "r");
|
|
const prototype = Object.getPrototypeOf(probe) as FileHandle;
|
|
await probe.close();
|
|
const failure = new Error("controlled physical read failure");
|
|
const read = vi.spyOn(prototype, "read").mockImplementationOnce(async () => {
|
|
controller.abort({ stage: "source-read" });
|
|
throw failure;
|
|
});
|
|
onTestFinished(async () => {
|
|
read.mockRestore();
|
|
await resources.close();
|
|
});
|
|
const inspect = vi.fn(async () => ok(signatureInspectionFixture()));
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
inspect,
|
|
controller.signal,
|
|
createRoot,
|
|
);
|
|
if (result.ok) throw new Error("Expected physical failure");
|
|
expect(result.error._tag).toBe("ProviderAdapterError");
|
|
expect(result.error.cause).toBe(failure);
|
|
expect(inspect).not.toHaveBeenCalled();
|
|
});
|
|
|
|
for (const cleanupFails of [false, true])
|
|
it(`keeps a failed inspection primary when the caller aborts during root close (fails: ${cleanupFails})`, async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const primary = new AnalysisOutputError(
|
|
"inspect_signature",
|
|
"controlled inspection failure",
|
|
);
|
|
const root = await createRoot(cleanupFails ? 1 : 0);
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
async () => err(primary),
|
|
controller.signal,
|
|
async () => ({
|
|
path: root.path,
|
|
async close() {
|
|
controller.abort(null);
|
|
await root.close();
|
|
},
|
|
}),
|
|
);
|
|
if (result.ok) throw new Error("Expected failed inspection");
|
|
expect(result.error.cleanupIncomplete).toBe(cleanupFails);
|
|
await resources.close();
|
|
expect(resolvedAnalysisError(result.error)).toBe(primary);
|
|
});
|
|
|
|
it.each(opaqueReasons)(
|
|
"preserves pre-entry reason %s without allocating a root or adopting foreign ownership",
|
|
async (reason) => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const makeRoot = vi.fn(createRoot);
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
AbortSignal.abort(reason),
|
|
makeRoot,
|
|
);
|
|
if (result.ok) throw new Error("Expected pre-entry cancellation");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
cleanupIncomplete: false,
|
|
});
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
expect(makeRoot).not.toHaveBeenCalled();
|
|
await resources.close();
|
|
},
|
|
);
|
|
|
|
it("does not replace a failed root creation with a simultaneous abort", async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const failure = new Error("controlled root creation failure");
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
controller.signal,
|
|
async () => {
|
|
controller.abort(null);
|
|
throw failure;
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected root creation failure");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "ProviderAdapterError",
|
|
cause: failure,
|
|
cleanupIncomplete: false,
|
|
});
|
|
await resources.close();
|
|
});
|
|
|
|
it("releases a late root when cancellation arrives during creation", async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const controller = new AbortController();
|
|
const reason = { stage: "root-creation" };
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
unusedInspection,
|
|
controller.signal,
|
|
async () => {
|
|
const root = await createRoot();
|
|
controller.abort(reason);
|
|
return root;
|
|
},
|
|
);
|
|
if (result.ok) throw new Error("Expected root handoff cancellation");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
cleanupIncomplete: false,
|
|
});
|
|
await expect(stat(roots[0]!)).rejects.toMatchObject({ code: "ENOENT" });
|
|
await resources.close();
|
|
});
|
|
|
|
it("does not adopt an inspection's arbitrary thrown cleanup marker", async () => {
|
|
const target = await createTarget("same");
|
|
const resources = new ArtifactResourceScope();
|
|
const marker = new ProviderCleanupError("foreign", ["unowned"], {});
|
|
const result = await withSignatureTarget(
|
|
target,
|
|
resources,
|
|
async () => {
|
|
throw marker;
|
|
},
|
|
undefined,
|
|
createRoot,
|
|
);
|
|
if (result.ok) throw new Error("Expected opaque producer failure");
|
|
expect(result.error).toMatchObject({
|
|
_tag: "AnalysisOutputError",
|
|
cause: marker,
|
|
cleanupIncomplete: false,
|
|
});
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
await resources.close();
|
|
});
|
|
|
|
it.each(["io", "cancelled"] as const)(
|
|
"preserves a codesign %s failure when later target verification observes cancellation",
|
|
async (kind) => {
|
|
const target = await createTarget("registered source bytes");
|
|
const controller = new AbortController();
|
|
const reason = { caller: "cancel after codesign result", kind };
|
|
const output = {
|
|
stdout: "physical output",
|
|
stderr: "physical diagnostic",
|
|
stdoutBytes: 15,
|
|
stderrBytes: 19,
|
|
exitCode: null,
|
|
signal: null,
|
|
truncated: false,
|
|
};
|
|
const failure = new NativeCommandFailure(
|
|
"codesign",
|
|
kind,
|
|
null,
|
|
{ cancellation: { reason } },
|
|
output,
|
|
"command cleanup remains unknown",
|
|
["command-owner"],
|
|
);
|
|
const runner: NativeCommandRunner = {
|
|
run: async () => {
|
|
controller.abort(reason);
|
|
return err(failure);
|
|
},
|
|
};
|
|
const client = new NativeMacOSProvider(
|
|
{},
|
|
runner,
|
|
"darwin",
|
|
() => unusedTracer,
|
|
createRoot,
|
|
).createClient(target);
|
|
try {
|
|
const result = await client.execute(
|
|
"inspect_signature",
|
|
{},
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok) throw new Error("Expected codesign failure");
|
|
expect(result.error._tag).toBe(
|
|
kind === "cancelled"
|
|
? "AnalysisCancelledError"
|
|
: "ProviderAdapterError",
|
|
);
|
|
expect(result.error.capturedOutput).toMatchObject({
|
|
stdout: output.stdout,
|
|
stderr: output.stderr,
|
|
});
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual(["command-owner"]);
|
|
if (kind === "cancelled") expect(result.error.cause).toBe(reason);
|
|
else {
|
|
expect(result.error.cause).toBe(failure);
|
|
expect(result.error.secondaryFailures).toContainEqual(
|
|
expect.objectContaining({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
}),
|
|
);
|
|
}
|
|
} finally {
|
|
await client.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
it.each(["unchanged", "cancelled", "changed"] as const)(
|
|
"keeps a codesign cleanup marker live after %s target verification",
|
|
async (outcome) => {
|
|
const target = await createTarget("registered source bytes");
|
|
const controller = new AbortController();
|
|
const reason = { request: "cancel after native command failure" };
|
|
const process = Object.assign(new EventEmitter(), {
|
|
exitCode: 1,
|
|
signalCode: null,
|
|
stdout: null,
|
|
stderr: null,
|
|
kill: () => {
|
|
throw new Error("Exited fixture must not be signalled");
|
|
},
|
|
});
|
|
let mayClean = false;
|
|
let cleanupAttempts = 0;
|
|
const owner = new ProviderProcessSupervisor({
|
|
process,
|
|
ownsProcessLifetime: true,
|
|
cleanup: async () => {
|
|
cleanupAttempts++;
|
|
return mayClean
|
|
? { cleaned: true, signaled: false }
|
|
: { cleaned: false, reason: "codesign worker remains owned" };
|
|
},
|
|
});
|
|
process.emit("close", 1, null);
|
|
const failure = new NativeCommandFailure(
|
|
"codesign",
|
|
"io",
|
|
1,
|
|
{ cleanupOwner: owner },
|
|
{
|
|
stdout: "physical output",
|
|
stderr: "physical diagnostic",
|
|
stdoutBytes: 15,
|
|
stderrBytes: 19,
|
|
exitCode: 1,
|
|
signal: null,
|
|
truncated: false,
|
|
},
|
|
"codesign worker remains owned",
|
|
["native-command-process"],
|
|
);
|
|
const runner = new XcrunCommandRunner({}, async () => {
|
|
if (outcome === "cancelled") controller.abort(reason);
|
|
if (outcome === "changed")
|
|
await writeFile(target.path, "changed source bytes");
|
|
return err(failure);
|
|
});
|
|
const client = new NativeMacOSProvider(
|
|
{},
|
|
runner,
|
|
"darwin",
|
|
() => unusedTracer,
|
|
createRoot,
|
|
).createClient(target);
|
|
onTestFinished(async () => {
|
|
mayClean = true;
|
|
expect((await client.close()).ok).toBe(true);
|
|
});
|
|
const result = await client.execute(
|
|
"inspect_signature",
|
|
{},
|
|
{ signal: controller.signal },
|
|
);
|
|
if (result.ok) throw new Error("Expected codesign failure");
|
|
const primary = primaryAnalysisError(result.error);
|
|
expect(primary._tag).toBe(
|
|
outcome === "changed"
|
|
? "AnalysisArtifactChangedError"
|
|
: "ProviderAdapterError",
|
|
);
|
|
if (outcome !== "changed") {
|
|
if (!(primary.cause instanceof NativeCommandFailure))
|
|
throw new Error("Expected physical codesign failure");
|
|
expect(primary.cause.cause).toBe(failure);
|
|
expect(primary.capturedOutput).toMatchObject({
|
|
stdout: "physical output",
|
|
stderr: "physical diagnostic",
|
|
});
|
|
}
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(result.error.cleanupResources).toEqual(["native-command-process"]);
|
|
expect(primary.secondaryFailures).toEqual(
|
|
outcome === "cancelled"
|
|
? [
|
|
expect.objectContaining({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
}),
|
|
]
|
|
: [],
|
|
);
|
|
expect((await client.close()).ok).toBe(false);
|
|
expect(result.error.cleanupIncomplete).toBe(true);
|
|
expect(cleanupAttempts).toBe(1);
|
|
mayClean = true;
|
|
expect((await client.close()).ok).toBe(true);
|
|
expect(cleanupAttempts).toBe(2);
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
expect(result.error.cleanupResources).toEqual([]);
|
|
},
|
|
);
|