fix: bind identity and coverage to the producing rule (#1513)
* fix: bind identity and coverage to the producing rule Logical paths, container families, examined bytes, path-segment conventions, destination collisions, file-id leases, Mach-O CPU selection, and firmware cleanup now follow the observation that owns each rule. * fix: fail unmatched Mach-O slices and keep firmware roots independent A fat target whose architecture lipo did not list now stops before slice tools run. Chained fixups still count unread LC_DYLD_INFO rebase bytes, and a firmware cleanup failure no longer blocks the next workspace. * test: cover path identity and runtime families at the projection boundary NFC joins and segment conventions are now asserted by the path resolver, the Apple projection, and a real APK inventory instead of helper unit tests. * test: expect unread dyld rebase bytes in the Apple dispatch fixture The legacy LC_DYLD_INFO fixture keeps a rebase range this reader does not decode, so the public CLI and MCP check now requires that partial coverage beside the bind opcodes. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
|
||||
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
|
||||
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
|
||||
import { err, ok } from "../domain/result.js";
|
||||
import { firmwareRequestSchema } from "../domain/firmware/firmwareAnalysis.js";
|
||||
import { finishFirmwareWorkspace } from "./FirmwareProvider.js";
|
||||
|
||||
const request = firmwareRequestSchema.parse({
|
||||
operation: "extract_firmware",
|
||||
input: {
|
||||
path: "/tmp/firmware.bin",
|
||||
output_directory: "/tmp/extracted",
|
||||
},
|
||||
});
|
||||
|
||||
const execution = ok({
|
||||
result: { files: ["kept"] },
|
||||
rawResult: null,
|
||||
provider: { id: "unblob", name: "unblob", version: "1" },
|
||||
limitations: [],
|
||||
locations: [],
|
||||
subject: null,
|
||||
});
|
||||
|
||||
describe("firmware workspace cleanup", () => {
|
||||
it("returns the completed extraction beside a cleanup failure and does not delete the leaked root", async () => {
|
||||
const close = vi.fn(async () => {
|
||||
throw new Error("cleanup failed");
|
||||
});
|
||||
const finished = await finishFirmwareWorkspace(
|
||||
{ path: "/tmp/runtime", close },
|
||||
execution,
|
||||
"unblob",
|
||||
request,
|
||||
);
|
||||
expect(close).toHaveBeenCalledOnce();
|
||||
if (finished.ok || !(finished.error instanceof ProviderAdapterError))
|
||||
throw new Error("Expected cleanup failure");
|
||||
expect(finished.error.cleanupResources).toEqual([
|
||||
"/tmp/runtime",
|
||||
"/tmp/extracted",
|
||||
]);
|
||||
expect(finished.error.partialObservation).toEqual({
|
||||
kind: "firmware",
|
||||
result: { files: ["kept"] },
|
||||
});
|
||||
expect(finished.error.diagnostics).toEqual({ reason: "cleanup failed" });
|
||||
expect(projectAnalysisError(finished.error).details).toMatchObject({
|
||||
partial_observation: {
|
||||
kind: "firmware",
|
||||
result: { files: ["kept"] },
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("does not replay a cleanup failure for a later workspace", async () => {
|
||||
const failed = await finishFirmwareWorkspace(
|
||||
{
|
||||
path: "/tmp/runtime",
|
||||
close: async () => {
|
||||
throw new Error("cleanup failed");
|
||||
},
|
||||
},
|
||||
execution,
|
||||
"unblob",
|
||||
request,
|
||||
);
|
||||
expect(failed.ok).toBe(false);
|
||||
const next = await finishFirmwareWorkspace(
|
||||
{ path: "/tmp/next", close: async () => undefined },
|
||||
execution,
|
||||
"unblob",
|
||||
request,
|
||||
);
|
||||
expect(next).toBe(execution);
|
||||
});
|
||||
|
||||
it("leaves an uncertain workspace recorded and does not remove it", async () => {
|
||||
const close = vi.fn(async () => undefined);
|
||||
const cleanup = new ProviderCleanupError(
|
||||
"unblob",
|
||||
["/tmp/runtime"],
|
||||
{ reason: "ownership uncertain" },
|
||||
{ operation: "extract_firmware" },
|
||||
);
|
||||
const outcome = err(cleanup);
|
||||
const finished = await finishFirmwareWorkspace(
|
||||
{ path: "/tmp/runtime", close },
|
||||
outcome,
|
||||
"unblob",
|
||||
request,
|
||||
);
|
||||
expect(close).not.toHaveBeenCalled();
|
||||
expect(finished).toBe(outcome);
|
||||
});
|
||||
});
|
||||
@@ -65,7 +65,6 @@ const limitations = [
|
||||
/** Queued bring-your-own firmware adapters with private snapshots and owned cleanup. */
|
||||
export class FirmwareProvider implements FirmwareAnalysisPort {
|
||||
#tail: Promise<void> = Promise.resolve();
|
||||
#cleanupFailure: AnalysisError | undefined;
|
||||
constructor(
|
||||
readonly environment: Readonly<
|
||||
Record<string, string | undefined>
|
||||
@@ -115,7 +114,6 @@ export class FirmwareProvider implements FirmwareAnalysisPort {
|
||||
: AbortSignal.any([options.signal, deadline]);
|
||||
let outcome: Outcome;
|
||||
try {
|
||||
if (this.#cleanupFailure !== undefined) return err(this.#cleanupFailure);
|
||||
if (signal.aborted) throw new AnalysisCancelledError(request.operation);
|
||||
const command = await resolveFirmwareCommand(
|
||||
this.environment,
|
||||
@@ -316,29 +314,49 @@ export class FirmwareProvider implements FirmwareAnalysisPort {
|
||||
},
|
||||
}),
|
||||
);
|
||||
if (!outcome.ok && outcome.error.cleanupIncomplete) {
|
||||
this.#cleanupFailure =
|
||||
root === undefined ||
|
||||
outcome.error.cleanupResources.includes(root.path)
|
||||
? outcome.error
|
||||
: new ProviderCleanupError(
|
||||
engineName,
|
||||
[...outcome.error.cleanupResources, root.path],
|
||||
outcome.error instanceof ProviderAdapterError
|
||||
? (outcome.error.diagnostics ?? {
|
||||
reason: outcome.error.message,
|
||||
})
|
||||
: { reason: outcome.error.message },
|
||||
{ cause: outcome.error },
|
||||
);
|
||||
}
|
||||
}
|
||||
// Never remove a workspace while process ownership remains uncertain.
|
||||
if (this.#cleanupFailure !== undefined) return err(this.#cleanupFailure);
|
||||
try {
|
||||
await root?.close();
|
||||
} catch (cause: unknown) {
|
||||
this.#cleanupFailure = new ProviderCleanupError(
|
||||
// A leaked root stays recorded on this result and is not deleted. The next
|
||||
// operation may create a new root; this failure is not replayed for it.
|
||||
return finishFirmwareWorkspace(root, outcome, engineName, request);
|
||||
}
|
||||
}
|
||||
|
||||
/** Keep a completed extraction beside cleanup failure, or skip removal when ownership is uncertain. */
|
||||
export const finishFirmwareWorkspace = async (
|
||||
root: { readonly path: string; close(): Promise<void> } | undefined,
|
||||
outcome: Outcome,
|
||||
engineName: string,
|
||||
request: FirmwareRequest,
|
||||
): Promise<Outcome> => {
|
||||
if (!outcome.ok && outcome.error.cleanupIncomplete) {
|
||||
if (
|
||||
root === undefined ||
|
||||
outcome.error.cleanupResources.includes(root.path)
|
||||
)
|
||||
return outcome;
|
||||
return err(
|
||||
new ProviderCleanupError(
|
||||
engineName,
|
||||
[...outcome.error.cleanupResources, root.path],
|
||||
outcome.error instanceof ProviderAdapterError
|
||||
? (outcome.error.diagnostics ?? { reason: outcome.error.message })
|
||||
: { reason: outcome.error.message },
|
||||
{
|
||||
cause: outcome.error,
|
||||
operation: request.operation,
|
||||
...(outcome.error.partialObservation === undefined
|
||||
? {}
|
||||
: { partialObservation: outcome.error.partialObservation }),
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
try {
|
||||
await root?.close();
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
new ProviderCleanupError(
|
||||
engineName,
|
||||
[
|
||||
root?.path ?? "unknown",
|
||||
@@ -346,13 +364,23 @@ export class FirmwareProvider implements FirmwareAnalysisPort {
|
||||
? [request.input.output_directory]
|
||||
: []),
|
||||
],
|
||||
{ reason: cause instanceof Error ? cause.message : String(cause) },
|
||||
{
|
||||
reason: cause instanceof Error ? cause.message : String(cause),
|
||||
previous_result: outcome.ok ? outcome.value.result : null,
|
||||
cause,
|
||||
operation: request.operation,
|
||||
...(outcome.ok
|
||||
? {
|
||||
partialObservation: {
|
||||
kind: "firmware",
|
||||
result: outcome.value.result,
|
||||
},
|
||||
}
|
||||
: outcome.error.partialObservation === undefined
|
||||
? {}
|
||||
: { partialObservation: outcome.error.partialObservation }),
|
||||
},
|
||||
);
|
||||
return err(this.#cleanupFailure);
|
||||
}
|
||||
return outcome;
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
return outcome;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user