Files
reverse-engineering-anything/tests/boundary/filesystem/assetCatalogFinalization.test.ts
T
morluto 035d876d1f fix(lifecycle): supervise assetutil, keep cleanup-time interruptions, and cover browser-transport handshakes (#11502)
* test(lifecycle): cover browser-transport handshakes and cleanup retry

Close six provider lifecycle gaps: Electron CDP handshake, cleanup and retry; V8 inspector handshake; web-module-trace handshake and retry.

* test(lifecycle): assert retained cleanup finishes before the next acquisition

Hold the retried browser and Electron transport close and check that no replacement launch or connect starts until it completes. Track gated upgrade sockets in the fake CDP browser so close() destroys them.

* test(lifecycle): release held retry gates and join gated work on failure

Teardown for the resolver and Electron retry tests releases the held close and awaits the gated request even when an assertion fails while it is held.

* test(lifecycle): cover worker and tool launch, worker handshake, and decoder completion (#11512)

* test(lifecycle): cover worker and tool launch, worker handshake, and decoder completion

Close six provider lifecycle gaps: SQLite and JavaScript worker launch, JavaScript worker handshake, assetutil launch, and qlog and mitmproxy decoding through the owned decoder child.

* test(lifecycle): run the worker relaunch in a built project and check exits before close

The relaunch starts the built worker entry, so it moves to tests/boundary/javascript. The handshake tests now check that the stand-in child has exited when analyze() returns, before close() runs.

* test(lifecycle): cover in-process cancellation and PE certificate completion (#11524)

* test(lifecycle): cover in-process cancellation and PE certificate completion

Close six provider lifecycle gaps: caller cancellation for the managed, Harmony, JavaScript workflow, runtime reconciliation and V8 code cache providers, and PE certificate completion (an existing CLI/MCP regression). Two Apple application gaps remain (#11509).

* test(lifecycle): compare the whole retained V8 code cache observation

* test(javascript): hold the startup deadline clock until the stand-in worker is running

* test(javascript): restore real timers in a void teardown

* test(lifecycle): join the held Electron request on failure, refuse unmodeled Inspector commands, drop a wording assertion

* fix(apple): run assetutil under owned process supervision and retain unproven cleanup (#11586)

* fix(process): report a cancellation or deadline during refused final cleanup as that interruption

* fix(apple): prepare ownership before the assetutil deadline, keep per-stream allowances, join interrupted-cleanup tests

* fix(artifacts): classify assetutil launch failures through the shared launch owner

After #11615, owned-command launch failures are one launch-failed outcome
classified by the shared owner. The assetutil dialect keeps only its
unavailable report (launchUnavailable) and names its provider; host
capacity and E2BIG become resource constraints.

* fix(artifacts): keep a completed assetutil run's output on its cleanup-only report

When assetutil exits 0 but its process cleanup is unproven, the cleanup
error is the primary report. retainOwnedCommandCleanup now accepts the
completed run's captured output for such a caller, so CLI and MCP keep the
retained output beside the cleanup owner.
2026-11-15 20:58:47 -05:00

283 lines
9.6 KiB
TypeScript

import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect, it, vi } from "vitest";
import { ArtifactProvider } from "../../../src/artifacts/ArtifactProvider.js";
import { ArtifactReaderFailure } from "../../../src/resources/ArtifactReader.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import * as ownedCommand from "../../../src/process/OwnedCommand.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { DirectoryArtifactReader } from "../../../src/resources/index.js";
it.skipIf(process.platform !== "darwin").each([
["success", false, false],
["integrity", false, false],
["timeout", false, false],
["success", true, false],
["integrity", true, false],
["timeout", true, false],
["integrity", true, true],
] as const)(
"preserves asset catalog %s with reader cleanup failure %s and join cleanup %s",
async (primary, failCleanup, failJoinCleanup) => {
const bundle = await createTestTempDirectory("rea-asset-finalization-");
const resources = join(bundle, "Contents", "Resources");
await mkdir(resources, { recursive: true });
const catalog = join(resources, "Assets.car");
await writeFile(catalog, "inert catalog bytes");
let catalogReader: DirectoryArtifactReader | undefined;
let denyCleanup = failCleanup;
let closeAttempts = 0;
const failedReaders = new Set<DirectoryArtifactReader>();
const originalOpen = DirectoryArtifactReader.prototype.open;
const open = vi
.spyOn(DirectoryArtifactReader.prototype, "open")
.mockImplementation(function (
this: DirectoryArtifactReader,
entry,
signal,
) {
if (entry.path.endsWith("/Assets.car") || entry.path === "Assets.car")
catalogReader = this;
return originalOpen.call(this, entry, signal);
});
const originalClose = DirectoryArtifactReader.prototype.close;
const close = vi
.spyOn(DirectoryArtifactReader.prototype, "close")
.mockImplementation(async function (this: DirectoryArtifactReader) {
if (this === catalogReader || failJoinCleanup) {
closeAttempts++;
if (denyCleanup) {
failedReaders.add(this);
throw new ArtifactReaderFailure(
"io",
"controlled catalog reader close",
{
cleanup: {
reason: "controlled catalog reader close",
resources: [resources],
},
},
);
}
}
await originalClose.call(this);
});
const stream = (text: string) => ({
text,
bytes: text.length,
observedBytes: text.length,
});
const utility = vi
.spyOn(ownedCommand, "runOwnedCommand")
.mockImplementationOnce(async () => {
if (primary === "integrity")
await writeFile(
catalog,
"changed catalog bytes with a different length",
);
if (primary === "timeout")
throw new ownedCommand.OwnedCommandFailure(
"timeout",
"Command deadline elapsed.",
{
stdout: stream(""),
stderr: stream("assetutil timeout detail"),
exitCode: null,
signal: "SIGKILL",
},
);
return {
stdout: stream("[]"),
stderr: stream(""),
exitCode: 0,
signal: null,
};
});
const client = new ArtifactProvider(
{},
"darwin",
).createBundleResourceClient({
callerPath: bundle,
canonicalPath: bundle,
infoPlistPath: join(bundle, "Contents", "Info.plist"),
layout: "contents",
executableSha256: null,
});
try {
const result = await client.execute("inspect_asset_catalog", {});
if (primary === "success" && !failCleanup) {
expect(result.ok).toBe(true);
return;
}
if (result.ok) throw new Error("Expected catalog failure");
const projected = projectAnalysisError(result.error);
expect(result.error.cleanupIncomplete).toBe(failCleanup);
if (primary === "integrity")
expect(projected).toMatchObject({
category: "integrity_mismatch",
details: {
reason: "integrity",
integrity_check: "member-identity",
},
});
if (primary === "timeout")
expect(projected).toMatchObject({
category: "timeout",
details: { operation: "inspect_asset_catalog", timeout_ms: 60000 },
});
if (failCleanup) {
expect(projected.code).toBe("cleanup_incomplete");
expect(result.error.cleanupResources).toContain(resources);
expect((await client.close()).ok).toBe(false);
expect(result.error.cleanupIncomplete).toBe(true);
denyCleanup = false;
expect((await client.close()).ok).toBe(true);
expect(result.error.cleanupIncomplete).toBe(false);
expect(closeAttempts).toBeGreaterThanOrEqual(3);
if (failJoinCleanup) expect(failedReaders.size).toBe(2);
}
} finally {
denyCleanup = false;
await client.close();
utility.mockRestore();
close.mockRestore();
open.mockRestore();
}
},
);
it.each(["decode_interface_builder", "inspect_keyed_archive"] as const)(
"retains the %s primary failure and its exact reader until cleanup succeeds",
async (operation) => {
const bundle = await createTestTempDirectory(
"rea-apple-reader-finalization-",
);
let denyCleanup = true;
const originalClose = DirectoryArtifactReader.prototype.close;
const close = vi
.spyOn(DirectoryArtifactReader.prototype, "close")
.mockImplementation(async function (this: DirectoryArtifactReader) {
if (denyCleanup)
throw new ArtifactReaderFailure(
"io",
"controlled bundle reader close",
{
cleanup: {
reason: "controlled bundle reader close",
resources: [bundle],
},
},
);
await originalClose.call(this);
});
const entries =
operation === "decode_interface_builder"
? vi
.spyOn(DirectoryArtifactReader.prototype, "entries")
.mockImplementationOnce(async function* () {
throw new ArtifactReaderFailure(
"integrity",
"controlled Interface Builder source change",
{ integrityCheck: "container-identity" },
);
})
: undefined;
const client = new ArtifactProvider(
{},
process.platform,
).createBundleResourceClient({
callerPath: bundle,
canonicalPath: bundle,
infoPlistPath: join(bundle, "Info.plist"),
layout: "root",
executableSha256: null,
});
try {
const result = await client.execute(
operation,
operation === "inspect_keyed_archive" ? { path: "Missing.plist" } : {},
);
if (result.ok)
throw new Error("Expected source/selection and cleanup failure");
expect(projectAnalysisError(result.error)).toMatchObject({
code: "cleanup_incomplete",
category:
operation === "decode_interface_builder"
? "integrity_mismatch"
: "invalid_input",
});
expect(result.error.cleanupResources).toContain(bundle);
expect((await client.close()).ok).toBe(false);
expect(result.error.cleanupIncomplete).toBe(true);
denyCleanup = false;
expect((await client.close()).ok).toBe(true);
expect(result.error.cleanupIncomplete).toBe(false);
} finally {
denyCleanup = false;
await client.close();
entries?.mockRestore();
close.mockRestore();
}
},
);
it.skipIf(process.platform !== "darwin")(
"prepares process ownership before the assetutil deadline and bounds each stream separately",
async () => {
const bundle = await createTestTempDirectory("rea-asset-limits-");
const resources = join(bundle, "Contents", "Resources");
await mkdir(resources, { recursive: true });
await writeFile(join(resources, "Assets.car"), "inert catalog bytes");
const stages: string[] = [];
const prepare = vi
.spyOn(ownedCommand, "prepareOwnedCommand")
.mockImplementationOnce(async () => {
stages.push("prepare");
});
const run = vi
.spyOn(ownedCommand, "runOwnedCommand")
.mockImplementationOnce(async (_spawn, limits) => {
stages.push("run");
// A cold helper build must not spend the command deadline, and
// stderr diagnostics must not shrink the stdout allowance.
expect(limits).toMatchObject({
timeoutMs: 60_000,
diagnosticBytes: {
stdout: 64 * 1024 * 1024,
stderr: 64 * 1024 * 1024,
},
});
const stream = (text: string) => ({
text,
bytes: text.length,
observedBytes: text.length,
});
return {
stdout: stream("[]"),
stderr: stream(""),
exitCode: 0,
signal: null,
};
});
const client = new ArtifactProvider(
{},
"darwin",
).createBundleResourceClient({
callerPath: bundle,
canonicalPath: bundle,
infoPlistPath: join(bundle, "Contents", "Info.plist"),
layout: "contents",
executableSha256: null,
});
try {
const result = await client.execute("inspect_asset_catalog", {});
if (!result.ok) throw result.error;
expect(stages).toEqual(["prepare", "run"]);
} finally {
await client.close();
run.mockRestore();
prepare.mockRestore();
}
},
);