Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

perf(artifacts): index observed parents for deep archive paths

morlutoa147ecd4b487
diff --git a/src/artifacts/inventory/ArtifactGraphConstruction.ts b/src/artifacts/inventory/ArtifactGraphConstruction.ts
index 4868b9fab..96770f85e 100644
--- a/src/artifacts/inventory/ArtifactGraphConstruction.ts
+++ b/src/artifacts/inventory/ArtifactGraphConstruction.ts
@@ -295,23 +295,48 @@ export const createArtifactEdges = (
return edges;
};
-export const nearestParent = (
- path: string,
- occurrences: ReadonlyMap<string, MutableOccurrence>,
+interface ArtifactParentIndex {
+ readonly children: Map<string, ArtifactParentIndex>;
+ occurrence?: MutableOccurrence;
+}
+
+/** Resolve strict observed ancestors without rebuilding every path prefix. */
+export const resolveOccurrenceParents = (
+ occurrences: readonly MutableOccurrence[],
+ byPath: ReadonlyMap<string, MutableOccurrence>,
expandedContainerIds: ReadonlySet<string>,
-): MutableOccurrence | undefined => {
- const parts = path.split("/");
- while (parts.length > 1) {
- parts.pop();
- const candidate = occurrences.get(parts.join("/"));
+): void => {
+ const root: ArtifactParentIndex = { children: new Map() };
+ for (const occurrence of byPath.values()) {
if (
- candidate !== undefined &&
- (candidate.entry_kind === "directory" ||
- expandedContainerIds.has(candidate.occurrence_id))
+ occurrence.entry_kind !== "directory" &&
+ !expandedContainerIds.has(occurrence.occurrence_id)
)
- return candidate;
+ continue;
+ let branch = root;
+ for (const segment of occurrence.logical_path.split("/")) {
+ let child = branch.children.get(segment);
+ if (child === undefined) {
+ child = { children: new Map() };
+ branch.children.set(segment, child);
+ }
+ branch = child;
+ }
+ branch.occurrence = occurrence;
+ }
+ for (const occurrence of occurrences) {
+ const segments = occurrence.logical_path.split("/");
+ let branch = root;
+ let parent: MutableOccurrence | undefined;
+ // The final segment identifies this occurrence, not one of its parents.
+ for (const segment of segments.slice(0, -1)) {
+ const child = branch.children.get(segment);
+ if (child === undefined) break;
+ branch = child;
+ parent = branch.occurrence ?? parent;
+ }
+ occurrence.parent_occurrence_id = parent?.occurrence_id ?? null;
}
- return undefined;
};
/**
diff --git a/src/artifacts/inventory/scanReader.ts b/src/artifacts/inventory/scanReader.ts
index a68f4030e..aa2a0b391 100644
--- a/src/artifacts/inventory/scanReader.ts
+++ b/src/artifacts/inventory/scanReader.ts
@@ -18,7 +18,7 @@ import {
createArtifactNode,
createOccurrence,
noteUnreadContainerSuffix,
- nearestParent,
+ resolveOccurrenceParents,
type MutableOccurrence,
} from "./ArtifactGraphConstruction.js";
import {
@@ -91,13 +91,11 @@ export const scanReader = async (
await visitArtifactEntries(context, reader, "");
// Archive directories may appear after their children. Resolve containment
// against the complete index before directory identities are materialized.
- for (const occurrence of occurrences)
- occurrence.parent_occurrence_id =
- nearestParent(
- occurrence.logical_path,
- context.occurrenceByPath,
- context.expandedContainerIds,
- )?.occurrence_id ?? null;
+ resolveOccurrenceParents(
+ occurrences,
+ context.occurrenceByPath,
+ context.expandedContainerIds,
+ );
return {
nodes,
occurrences,
diff --git a/tests/boundary/filesystem/artifactInventoryEntryOrder.test.ts b/tests/boundary/filesystem/artifactInventoryEntryOrder.test.ts
index 4d40227f3..64c8e2f7c 100644
--- a/tests/boundary/filesystem/artifactInventoryEntryOrder.test.ts
+++ b/tests/boundary/filesystem/artifactInventoryEntryOrder.test.ts
@@ -69,6 +69,31 @@ describe("artifact inventory entry order", () => {
},
);
+ it("resolves deep ZIP members without inventing missing directories", async () => {
+ const root = await createTestTempDirectory("rea-deep-archive-parent-");
+ const archive = join(root, "deep.zip");
+ // Fits the ZIP filename field while exceeding the formerly expensive depth.
+ const member = `observed/${"d/".repeat(30_000)}data.txt`;
+ await writeOrderedZip(archive, [member, "observed/", "unrelated/"]);
+ const inventory = await inventoryArtifact(archive);
+ expect(artifactParentPaths(inventory)).toEqual({
+ ".": null,
+ observed: ".",
+ unrelated: ".",
+ [member]: "observed",
+ });
+ const occurrence = artifactOccurrenceAt(inventory, member);
+ expect(occurrence.hash_status).toBe("verified");
+ expect(inventory.edges).toContainEqual(
+ expect.objectContaining({
+ occurrence_id: occurrence.occurrence_id,
+ parent_artifact_id: artifactOccurrenceAt(inventory, "observed")
+ .artifact_id,
+ relation: "contains",
+ }),
+ );
+ });
+
it.each([
{
entries: ["pkg/sub/data.txt"],