Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(artifacts): reject incomplete inventoried extraction
dakjdakd5e8fb531e255
diff --git a/src/artifacts/extraction/ArtifactExtraction.ts b/src/artifacts/extraction/ArtifactExtraction.tsindex b87beb09a..9bfdd13b4 100644--- a/src/artifacts/extraction/ArtifactExtraction.ts+++ b/src/artifacts/extraction/ArtifactExtraction.ts@@ -57,6 +57,9 @@ export const extractArtifact = async (const selectedIds = new Set(selectedOccurrences.map(({ occurrence_id: id }) => id),);+ const regularPaths = new Set(+ selectedOccurrences.map(({ logical_path: path }) => path),+ );const occurrences = new Map<string, ArtifactOccurrence>();const neededNodes = new Set<string>();collectOccurrences(@@ -98,11 +101,25 @@ export const extractArtifact = async ();return { occurrence, node };});+ const activeSelected = selected.filter(({ occurrence }) => {+ // Nested members remain represented by their containing regular file;+ // only entries exposed by the active reader are materialized.+ let path = occurrence.logical_path;+ for (+ let slash = path.lastIndexOf("/");+ slash >= 0;+ slash = path.lastIndexOf("/")+ ) {+ path = path.slice(0, slash);+ if (regularPaths.has(path)) return false;+ }+ return true;+ });return materializeSelection({input,sourcePath,inventory,- selected,+ selected: activeSelected,signal,});};@@ -143,7 +160,6 @@ const materializeSelection = async ({const extracted: ExtractedOccurrence[] = [];try {output = await SafeOutputTree.create(input.outputRoot);- const materialized: SelectedOccurrence[] = [];const registry = new ArtifactPathRegistry();for await (const entry of reader.entries(signal)) {const path = normalizeArtifactPath(entry.path);@@ -172,8 +188,13 @@ const materializeSelection = async ({bytes_written: written.bytesWritten,created: true,});- materialized.push(selectedItem);+ byPath.delete(path);}+ if (byPath.size > 0)+ throw new ArtifactReaderFailure(+ "integrity",+ `Inventoried regular artifact entries were not materialized: ${[...byPath.keys()].sort(compareUnicodeCodePoints).join(", ")}`,+ );readerCloseAttempted = true;await reader.close();readerClosed = true;@@ -183,7 +204,7 @@ const materializeSelection = async ({const result = createExtractionResult(input,inventory,- materialized,+ selected,extracted,);await output.commit();diff --git a/tests/boundary/providers/native/artifactProvider.extractionCompleteness.test.ts b/tests/boundary/providers/native/artifactProvider.extractionCompleteness.test.tsnew file mode 100644index 000000000..0a160a1da--- /dev/null+++ b/tests/boundary/providers/native/artifactProvider.extractionCompleteness.test.ts@@ -0,0 +1,118 @@+import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";+import { join } from "node:path";++import { createPackage } from "@electron/asar";+import { expect, it, onTestFinished, vi } from "vitest";++import { ArtifactProvider } from "../../../../src/artifacts/ArtifactProvider.js";+import { SafeOutputTree } from "../../../../src/artifacts/SafeOutputTree.js";+import { artifactExtractionResultSchema } from "../../../../src/domain/artifactGraph.js";+import { projectAnalysisError } from "../../../../src/domain/analysisErrorProjection.js";+import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";++it.each(["file", "directory"] as const)(+ "rolls back when an inventoried %s disappears before extraction",+ async (removedKind) => {+ const root = await createTestTempDirectory("rea-extract-missing-");+ const source = join(root, "source");+ await mkdir(join(source, "z"), { recursive: true });+ // Equal bytes share an artifact node, but both occurrences must be copied.+ await writeFile(join(source, "a.txt"), "same bytes\n");+ await writeFile(join(source, "z", "missing.txt"), "same bytes\n");+ const output = join(root, "output");+ afterInventory(async () => {+ await rm(+ removedKind === "file"+ ? join(source, "z", "missing.txt")+ : join(source, "z"),+ { recursive: removedKind === "directory" },+ );+ });++ const result = await extract(source, output);+ if (result.ok) throw new Error("Incomplete extraction must fail");+ expect(projectAnalysisError(result.error)).toMatchObject({+ code: "artifact_operation_failed",+ details: {+ operation: "extract_artifact",+ reason: "integrity",+ detail:+ "Inventoried regular artifact entries were not materialized: z/missing.txt",+ },+ });+ await expect(access(output)).rejects.toThrow();+ expect(await readFile(join(source, "a.txt"), "utf8")).toBe("same bytes\n");+ },+);++it("still refuses a regular file added after inventory and rolls back", async () => {+ const root = await createTestTempDirectory("rea-extract-added-");+ const source = join(root, "source");+ await mkdir(source);+ await writeFile(join(source, "a.txt"), "original\n");+ const output = join(root, "output");+ afterInventory(async () => {+ await writeFile(join(source, "z.txt"), "new\n");+ });+ const result = await extract(source, output);+ if (result.ok) throw new Error("Changed inventory must fail");+ expect(projectAnalysisError(result.error)).toMatchObject({+ details: {+ reason: "integrity",+ detail: "Regular artifact entry is missing from inventory: z.txt",+ },+ });+ await expect(access(output)).rejects.toThrow();+});++it("preserves every equal-content occurrence and copies a nested ASAR as its containing file", async () => {+ const root = await createTestTempDirectory("rea-extract-nested-");+ const source = join(root, "source");+ const nested = join(root, "nested");+ await mkdir(source);+ await mkdir(join(nested, "assets"), { recursive: true });+ await writeFile(join(nested, "assets", "main.js"), "export default 1;\n");+ await createPackage(nested, join(source, "app.asar"));+ await writeFile(join(source, "a.txt"), "same bytes\n");+ await writeFile(join(source, "b.txt"), "same bytes\n");+ const output = join(root, "output");+ const result = await extract(source, output);+ if (!result.ok) throw result.error;+ const extracted = artifactExtractionResultSchema.parse(result.value.result);+ expect(extracted.artifacts.map(({ relative_path }) => relative_path)).toEqual(+ ["a.txt", "app.asar", "b.txt"],+ );+ expect(extracted.extraction_manifest.selected_occurrence_ids).toHaveLength(3);+ expect(extracted.artifacts[0]?.artifact_id).toBe(+ extracted.artifacts[2]?.artifact_id,+ );+ for (const path of ["a.txt", "b.txt", "app.asar"])+ expect(await readFile(join(output, path))).toEqual(+ await readFile(join(source, path)),+ );+});++// Synchronize a real filesystem change after the actual inventory scan. The+// production reader, writes, failure translation and rollback all remain real.+const afterInventory = (change: () => Promise<void>): void => {+ const create = SafeOutputTree.create;+ const hook = vi+ .spyOn(SafeOutputTree, "create")+ .mockImplementationOnce(async (...args) => {+ const output = await create(...args);+ await change();+ return output;+ });+ onTestFinished(() => hook.mockRestore());+};++const extract = (source: string, output: string) =>+ new ArtifactProvider(process.env)+ .createClient({+ path: source,+ sourcePath: source,+ sha256: "0".repeat(64),+ kind: "archive",+ format: "asar",+ })+ .execute("extract_artifact", { output_root: output });