fix(artifacts): resolve parents after archive enumeration (#578)
This commit is contained in:
@@ -73,6 +73,12 @@ export const scanReader = async (
|
||||
registry: new ArtifactPathRegistry(),
|
||||
};
|
||||
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)
|
||||
?.occurrence_id ?? null;
|
||||
return { nodes, occurrences, pendingContradictions };
|
||||
};
|
||||
|
||||
@@ -115,12 +121,7 @@ const visitArtifactEntries = async (
|
||||
logicalPath,
|
||||
expandableAsar ? "directory" : entry.kind,
|
||||
);
|
||||
const parent = nearestParent(logicalPath, context.occurrenceByPath);
|
||||
const occurrence = createOccurrence(
|
||||
entry,
|
||||
logicalPath,
|
||||
parent?.occurrence_id ?? null,
|
||||
);
|
||||
const occurrence = createOccurrence(entry, logicalPath, null);
|
||||
let digested:
|
||||
| { readonly node: ArtifactNode; readonly mismatched: boolean }
|
||||
| undefined;
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
import { mkdir, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { createPackage } from "@electron/asar";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { inventoryArtifact } from "../../../src/application/ArtifactInventory.js";
|
||||
import {
|
||||
artifactOccurrenceAt,
|
||||
artifactParentPaths,
|
||||
writeOrderedZip,
|
||||
} from "../../fixtures/artifactEntryOrder.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
const permutations = [
|
||||
["pkg/", "pkg/sub/", "pkg/sub/data.txt"],
|
||||
["pkg/", "pkg/sub/data.txt", "pkg/sub/"],
|
||||
["pkg/sub/", "pkg/", "pkg/sub/data.txt"],
|
||||
["pkg/sub/", "pkg/sub/data.txt", "pkg/"],
|
||||
["pkg/sub/data.txt", "pkg/", "pkg/sub/"],
|
||||
["pkg/sub/data.txt", "pkg/sub/", "pkg/"],
|
||||
];
|
||||
const expectedParents = {
|
||||
".": null,
|
||||
empty: ".",
|
||||
pkg: ".",
|
||||
"pkg/sub": "pkg",
|
||||
"pkg/sub/data.txt": "pkg/sub",
|
||||
};
|
||||
|
||||
describe("artifact inventory entry order", () => {
|
||||
it.each(permutations.map((entries) => ({ entries })))(
|
||||
"parents ZIP entries correctly for $entries",
|
||||
async ({ entries }) => {
|
||||
const root = await createTestTempDirectory("rea-entry-order-");
|
||||
const archive = join(root, "ordered.zip");
|
||||
const referencePath = join(root, "reference.zip");
|
||||
await writeOrderedZip(archive, [...entries, "empty/"]);
|
||||
await writeOrderedZip(referencePath, [
|
||||
"pkg/",
|
||||
"pkg/sub/",
|
||||
"pkg/sub/data.txt",
|
||||
"empty/",
|
||||
]);
|
||||
const observed = await inventoryArtifact(archive);
|
||||
const reference = await inventoryArtifact(referencePath);
|
||||
expect(artifactParentPaths(observed)).toEqual(expectedParents);
|
||||
expect(
|
||||
observed.occurrences.map(({ logical_path }) => logical_path),
|
||||
).toEqual(Object.keys(expectedParents));
|
||||
for (const [path, parentPath] of Object.entries(expectedParents)) {
|
||||
if (parentPath === null) continue;
|
||||
const occurrence = artifactOccurrenceAt(observed, path);
|
||||
const parent = artifactOccurrenceAt(observed, parentPath);
|
||||
expect(observed.edges).toContainEqual(
|
||||
expect.objectContaining({
|
||||
logical_path: path,
|
||||
relation: "contains",
|
||||
parent_artifact_id: parent.artifact_id,
|
||||
child_artifact_id: occurrence.artifact_id,
|
||||
}),
|
||||
);
|
||||
}
|
||||
const empty = artifactOccurrenceAt(observed, "empty").artifact_id;
|
||||
for (const path of ["pkg", "pkg/sub"]) {
|
||||
const identity = artifactOccurrenceAt(observed, path).artifact_id;
|
||||
expect(identity).not.toBeNull();
|
||||
expect(identity).not.toBe(empty);
|
||||
expect(identity).toBe(
|
||||
artifactOccurrenceAt(reference, path).artifact_id,
|
||||
);
|
||||
}
|
||||
expect(await inventoryArtifact(archive)).toEqual(observed);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([
|
||||
{
|
||||
entries: ["pkg/sub/data.txt"],
|
||||
parents: { ".": null, "pkg/sub/data.txt": "." },
|
||||
},
|
||||
{
|
||||
entries: ["pkg/sub/data.txt", "pkg/"],
|
||||
parents: { ".": null, pkg: ".", "pkg/sub/data.txt": "pkg" },
|
||||
},
|
||||
{
|
||||
entries: ["pkg/sub/data.txt", "pkg/sub/"],
|
||||
parents: { ".": null, "pkg/sub": ".", "pkg/sub/data.txt": "pkg/sub" },
|
||||
},
|
||||
])(
|
||||
"uses only observed directory parents for $entries",
|
||||
async ({ entries, parents }) => {
|
||||
const root = await createTestTempDirectory("rea-implicit-directory-");
|
||||
const archive = join(root, "implicit.zip");
|
||||
await writeOrderedZip(archive, entries);
|
||||
expect(artifactParentPaths(await inventoryArtifact(archive))).toEqual(
|
||||
parents,
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("entry-order validation and reader controls", () => {
|
||||
it.each([
|
||||
["pkg/data.txt", "pkg/data.txt/"],
|
||||
["pkg/data.txt/", "pkg/data.txt"],
|
||||
["pkg/data.txt", "PKG/other.txt"],
|
||||
["pkg/data.txt", "pkg"],
|
||||
["../escape.txt"],
|
||||
])("keeps invalid/colliding paths rejected for %j", async (...entries) => {
|
||||
const root = await createTestTempDirectory("rea-entry-order-invalid-");
|
||||
const archive = join(root, "invalid.zip");
|
||||
await writeOrderedZip(archive, entries);
|
||||
await expect(inventoryArtifact(archive)).rejects.toMatchObject({
|
||||
reason: "path",
|
||||
});
|
||||
});
|
||||
|
||||
it("preserves ordinary directory and ASAR structure", async () => {
|
||||
const root = await createTestTempDirectory("rea-entry-order-readers-");
|
||||
const directory = join(root, "source");
|
||||
await mkdir(join(directory, "pkg", "sub"), { recursive: true });
|
||||
await mkdir(join(directory, "empty"));
|
||||
await writeFile(join(directory, "pkg", "sub", "data.txt"), "evidence\n");
|
||||
const archive = join(root, "fixture.asar");
|
||||
await createPackage(directory, archive);
|
||||
const filesystem = await inventoryArtifact(directory);
|
||||
const asar = await inventoryArtifact(archive);
|
||||
expect(artifactParentPaths(filesystem)).toEqual(expectedParents);
|
||||
expect(artifactParentPaths(asar)).toEqual(expectedParents);
|
||||
for (const path of ["empty", "pkg", "pkg/sub"])
|
||||
expect(artifactOccurrenceAt(asar, path).artifact_id).toBe(
|
||||
artifactOccurrenceAt(filesystem, path).artifact_id,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,67 @@
|
||||
import { join } from "node:path";
|
||||
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { expect, it } from "vitest";
|
||||
import { z } from "zod";
|
||||
|
||||
import { parseConfig } from "../../../src/config.js";
|
||||
import { createBinarySession } from "../../../src/application/runtime.js";
|
||||
import { createServer } from "../../../src/server/createServer.js";
|
||||
import { artifactInspectionResultSchema } from "../../../src/domain/artifactInspection.js";
|
||||
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
|
||||
import {
|
||||
artifactParentPaths,
|
||||
writeOrderedZip,
|
||||
} from "../../fixtures/artifactEntryOrder.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
it("returns correct child-first ZIP containment through MCP inspection", async () => {
|
||||
const root = await createTestTempDirectory("rea-entry-order-mcp-");
|
||||
const archive = join(root, "child-first.zip");
|
||||
await writeOrderedZip(archive, ["pkg/sub/data.txt", "pkg/sub/", "pkg/"]);
|
||||
const configured = parseConfig({});
|
||||
if (!configured.ok) throw configured.error;
|
||||
const session = createBinarySession(configured.value);
|
||||
const server = createServer(session, session);
|
||||
const client = new Client({ name: "entry-order-test", version: "1" });
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
try {
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
expect(
|
||||
(
|
||||
await client.callTool({
|
||||
name: "open_binary",
|
||||
arguments: { path: archive },
|
||||
})
|
||||
).isError,
|
||||
).not.toBe(true);
|
||||
const called = await client.callTool({
|
||||
name: "inspect_artifact",
|
||||
arguments: {},
|
||||
});
|
||||
expect(called.isError, JSON.stringify(called.structuredContent)).not.toBe(
|
||||
true,
|
||||
);
|
||||
const inspection = artifactInspectionResultSchema.parse(
|
||||
z.object({ result: z.unknown() }).parse(called.structuredContent).result,
|
||||
);
|
||||
const substep = inspection.substeps[0];
|
||||
if (substep === undefined) throw new Error("Missing inventory Evidence");
|
||||
const inventory = artifactInventoryResultSchema.parse(
|
||||
substep.evidence.normalized_result,
|
||||
);
|
||||
expect(artifactParentPaths(inventory)).toEqual({
|
||||
".": null,
|
||||
pkg: ".",
|
||||
"pkg/sub": "pkg",
|
||||
"pkg/sub/data.txt": "pkg/sub",
|
||||
});
|
||||
expect(inspection.coverage.status).toBe("complete-within-substeps");
|
||||
} finally {
|
||||
await client.close();
|
||||
await server.close();
|
||||
await session.close();
|
||||
}
|
||||
});
|
||||
Vendored
+58
@@ -0,0 +1,58 @@
|
||||
import { writeFile } from "node:fs/promises";
|
||||
|
||||
import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js";
|
||||
|
||||
import type { ArtifactInventoryResult } from "../../src/domain/artifactGraph.js";
|
||||
|
||||
/** Write real ZIP entries in the supplied central-directory order. */
|
||||
export const writeOrderedZip = async (
|
||||
path: string,
|
||||
entries: readonly string[],
|
||||
): Promise<void> => {
|
||||
const writer = new ZipWriter(new Uint8ArrayWriter());
|
||||
for (const entry of entries) {
|
||||
const directory = entry.endsWith("/");
|
||||
await writer.add(
|
||||
entry,
|
||||
directory ? undefined : new TextReader("evidence\n"),
|
||||
{
|
||||
directory,
|
||||
lastModDate: new Date("2020-01-01T00:00:00Z"),
|
||||
},
|
||||
);
|
||||
}
|
||||
await writeFile(path, await writer.close());
|
||||
};
|
||||
|
||||
/** Express occurrence parents by logical path for structural assertions. */
|
||||
export const artifactParentPaths = (
|
||||
inventory: ArtifactInventoryResult,
|
||||
): Record<string, string | null> => {
|
||||
const paths = new Map(
|
||||
inventory.occurrences.map((item) => [
|
||||
item.occurrence_id,
|
||||
item.logical_path,
|
||||
]),
|
||||
);
|
||||
return Object.fromEntries(
|
||||
inventory.occurrences.map((item) => [
|
||||
item.logical_path,
|
||||
item.parent_occurrence_id === null
|
||||
? null
|
||||
: (paths.get(item.parent_occurrence_id) ?? "missing-parent"),
|
||||
]),
|
||||
);
|
||||
};
|
||||
|
||||
/** Require an explicitly observed occurrence in a real inventory. */
|
||||
export const artifactOccurrenceAt = (
|
||||
inventory: ArtifactInventoryResult,
|
||||
path: string,
|
||||
) => {
|
||||
const occurrence = inventory.occurrences.find(
|
||||
(item) => item.logical_path === path,
|
||||
);
|
||||
if (occurrence === undefined)
|
||||
throw new Error(`Missing inventory path: ${path}`);
|
||||
return occurrence;
|
||||
};
|
||||
Reference in New Issue
Block a user