refactor(artifact): simplify extraction and inventory results

This commit is contained in:
morluto
2026-09-29 08:07:04 +08:00
parent 81996df7a5
commit 07ff8b30ee
24 changed files with 200 additions and 416 deletions
@@ -46,17 +46,14 @@ it("returns every artifact occurrence in one MCP call", async () => {
);
const inventory = z
.object({
occurrences: z.object({
items: z.array(z.object({ logical_path: z.string() })),
total: z.number(),
}),
occurrences: z.array(z.object({ logical_path: z.string() })),
})
.parse(
z.object({ result: z.unknown() }).parse(result.structuredContent)
.result,
);
expect(inventory.occurrences.items).toHaveLength(fileCount + 1);
expect(inventory.occurrences.total).toBe(fileCount + 1);
expect(inventory.occurrences).toHaveLength(fileCount + 1);
expect(inventory.occurrences.length).toBe(fileCount + 1);
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
}
+21 -28
View File
@@ -94,12 +94,9 @@ it.each([
);
const artifactInventoryResultSchema = z.object({
occurrences: z.object({
items: z.array(
z.object({ logical_path: z.string(), hash_status: z.string() }),
),
total: z.number(),
}),
occurrences: z.array(
z.object({ logical_path: z.string(), hash_status: z.string() }),
),
integrity_contradictions: z.array(
z.object({
logical_path: z.string(),
@@ -170,7 +167,7 @@ it("records an approved mismatch, preserves verified siblings, and never reports
]),
);
expect(inventory.integrity_contradictions).toHaveLength(2);
expect(inventory.occurrences.items).toEqual(
expect(inventory.occurrences).toEqual(
expect.arrayContaining([
expect.objectContaining({
logical_path: "main.js",
@@ -209,27 +206,25 @@ it("records an approved mismatch, preserves verified siblings, and never reports
const compared = await client.callTool({
name: "compare_artifacts",
arguments: {
left_evidence_ids: [evidence.evidence_id],
right_evidence_ids: [evidence.evidence_id],
left_evidence_id: evidence.evidence_id,
right_evidence_id: evidence.evidence_id,
},
});
expect(compared.isError).not.toBe(true);
expect(compactResult(compared.structuredContent).result).toMatchObject({
status: "contradiction",
summary: { contradiction: 2 },
changes: {
items: expect.arrayContaining([
expect.objectContaining({
logical_path: "main.js",
classification: "contradiction",
dimensions: ["integrity"],
}),
expect.objectContaining({
logical_path: "second.js",
classification: "contradiction",
}),
]),
},
changes: expect.arrayContaining([
expect.objectContaining({
logical_path: "main.js",
classification: "contradiction",
dimensions: ["integrity"],
}),
expect.objectContaining({
logical_path: "second.js",
classification: "contradiction",
}),
]),
});
} finally {
await Promise.allSettled([client.close(), server.close(), session.close()]);
@@ -382,11 +377,9 @@ it("returns full artifact graphs inline and compares changed inventories", async
z
.object({
manifest: z.object({ root_format: z.literal("ipa") }),
occurrences: z.object({
items: z.array(z.object({ logical_path: z.string() })),
}),
occurrences: z.array(z.object({ logical_path: z.string() })),
})
.parse(inventoryResult.result).occurrences.items,
.parse(inventoryResult.result).occurrences,
).toEqual(
expect.arrayContaining([
expect.objectContaining({
@@ -410,8 +403,8 @@ it("returns full artifact graphs inline and compares changed inventories", async
const compared = await client.callTool({
name: "compare_artifacts",
arguments: {
left_evidence_ids: [evidence.evidence_id],
right_evidence_ids: [changedEvidence.evidence_id],
left_evidence_id: evidence.evidence_id,
right_evidence_id: changedEvidence.evidence_id,
unknown_registry_approved: true,
},
});
@@ -28,8 +28,8 @@ describe("artifact archive inventory", () => {
expect(parsed.ok && parsed.value.format).toBe("apk");
if (!parsed.ok) return;
const zipResult = await inventory(parsed.value);
expect(zipResult.occurrences.total).toBe(3);
expect(zipResult.nodes.items.map(({ kind }) => kind)).toEqual(
expect(zipResult.occurrences.length).toBe(3);
expect(zipResult.nodes.map(({ kind }) => kind)).toEqual(
expect.arrayContaining(["javascript", "dynamic-library"]),
);
const cliEvidence = parseEvidence(
@@ -86,7 +86,7 @@ describe("artifact archive inventory", () => {
const asarPath = join(root, "app.asar");
await createPackage(source, asarPath);
const asarResult = await inventory(target(asarPath, "asar"));
expect(asarResult.occurrences.items).toEqual(
expect(asarResult.occurrences).toEqual(
expect.arrayContaining([
expect.objectContaining({ logical_path: "main.js" }),
]),
@@ -95,7 +95,7 @@ describe("artifact archive inventory", () => {
const unpackedPath = join(root, "unpacked.asar");
await createPackageWithOptions(source, unpackedPath, { unpack: "*.js" });
const unpackedResult = await inventory(target(unpackedPath, "asar"));
expect(unpackedResult.occurrences.items).toEqual(
expect(unpackedResult.occurrences).toEqual(
expect.arrayContaining([
expect.objectContaining({
logical_path: "main.js",
@@ -13,7 +13,7 @@ import {
} from "../../../../src/artifacts/ArtifactPaths.js";
import { ArtifactProvider } from "../../../../src/artifacts/ArtifactProvider.js";
import { ArtifactReaderFailure } from "../../../../src/artifacts/ArtifactReader.js";
import { artifactExtractionInputSchema } from "../../../../src/contracts/artifactToolContracts.js";
import { artifactExtractionExecutionSchema } from "../../../../src/contracts/artifactToolContracts.js";
import { artifactInventoryResultSchema } from "../../../../src/domain/artifactGraph.js";
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
import { parseBinaryTarget } from "../../../../src/application/BinaryTargetResolver.js";
@@ -47,10 +47,10 @@ describe("artifact archive safety", () => {
if (!parsed.ok) return;
const result = await inventory(parsed.value);
expect(result.manifest.root_format).toBe(format);
expect(result.nodes.items.map(({ kind }) => kind)).toEqual(
expect(result.nodes.map(({ kind }) => kind)).toEqual(
expect.arrayContaining(["javascript", "native-addon"]),
);
const selected = result.occurrences.items.find(
const selected = result.occurrences.find(
({ logical_path: logicalPath }) => logicalPath === "Assets/main.js",
);
expect(selected).toBeDefined();
@@ -60,8 +60,7 @@ describe("artifact archive safety", () => {
.createClient(parsed.value)
.execute(
"extract_artifact",
artifactExtractionInputSchema.parse({
approved: true,
artifactExtractionExecutionSchema.parse({
output_root: output,
occurrence_ids: [selected.occurrence_id],
}),
@@ -41,7 +41,7 @@ describe("artifact directory inventory", () => {
const left = artifactInventoryResultSchema.parse(first.value.result);
const right = artifactInventoryResultSchema.parse(second.value.result);
expect(left.manifest).toEqual(right.manifest);
expect(left.nodes.items.map(({ kind }) => kind)).toEqual(
expect(left.nodes.map(({ kind }) => kind)).toEqual(
expect.arrayContaining([
"executable",
"framework",
@@ -50,17 +50,17 @@ describe("artifact directory inventory", () => {
]),
);
expect(
left.occurrences.items.find(({ logical_path: path }) =>
left.occurrences.find(({ logical_path: path }) =>
path.endsWith("outside"),
),
).toMatchObject({ artifact_id: null, entry_kind: "symlink" });
const script = left.occurrences.items.find(({ logical_path: path }) =>
const script = left.occurrences.find(({ logical_path: path }) =>
path.endsWith("main.js"),
);
const sourceMap = left.occurrences.items.find(({ logical_path: path }) =>
const sourceMap = left.occurrences.find(({ logical_path: path }) =>
path.endsWith("main.js.MAP"),
);
expect(left.edges.items).toEqual(
expect(left.edges).toEqual(
expect.arrayContaining([
expect.objectContaining({
relation: "maps-source",
@@ -6,7 +6,7 @@ import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
import { ArtifactProvider } from "../../../../src/artifacts/ArtifactProvider.js";
import { artifactExtractionInputSchema } from "../../../../src/contracts/artifactToolContracts.js";
import { artifactExtractionExecutionSchema } from "../../../../src/contracts/artifactToolContracts.js";
import {
artifactExtractionResultSchema,
artifactInventoryResultSchema,
@@ -22,7 +22,7 @@ describe("artifact extraction", () => {
await writeFile(join(source, "assets", "ignored.js"), "ignored();\n");
const targetValue = target(source, "directory");
const graph = await inventory(targetValue);
const selected = graph.occurrences.items.find(
const selected = graph.occurrences.find(
({ logical_path: path }) => path === "assets/selected.js",
);
expect(selected).toBeDefined();
@@ -34,8 +34,7 @@ describe("artifact extraction", () => {
.createClient(targetValue)
.execute(
"extract_artifact",
artifactExtractionInputSchema.parse({
approved: true,
artifactExtractionExecutionSchema.parse({
output_root: cancelledOutput,
occurrence_ids: [selected.occurrence_id],
}),
@@ -51,8 +50,7 @@ describe("artifact extraction", () => {
.createClient(targetValue)
.execute(
"extract_artifact",
artifactExtractionInputSchema.parse({
approved: true,
artifactExtractionExecutionSchema.parse({
output_root: output,
occurrence_ids: [selected.occurrence_id],
}),
@@ -63,9 +61,11 @@ describe("artifact extraction", () => {
result.value.result,
);
expect(firstExtraction).toMatchObject({
output_root: "$OUTPUT_ROOT",
output_root: output,
containment_verified: true,
artifacts: { total: 1 },
artifacts: expect.arrayContaining([
expect.objectContaining({ relative_path: "assets/selected.js" }),
]),
extraction_manifest: { output_root_alias: "$OUTPUT_ROOT" },
});
expect(await readFile(join(output, "assets", "selected.js"), "utf8")).toBe(
@@ -80,8 +80,7 @@ describe("artifact extraction", () => {
.createClient(targetValue)
.execute(
"extract_artifact",
artifactExtractionInputSchema.parse({
approved: true,
artifactExtractionExecutionSchema.parse({
output_root: relocatedOutput,
occurrence_ids: [selected.occurrence_id],
}),
@@ -97,8 +96,7 @@ describe("artifact extraction", () => {
.createClient(targetValue)
.execute(
"extract_artifact",
artifactExtractionInputSchema.parse({
approved: true,
artifactExtractionExecutionSchema.parse({
output_root: output,
occurrence_ids: [selected.occurrence_id],
}),
@@ -6,6 +6,7 @@ import { describe, expect, it } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { inventoryArtifact } from "../../../src/application/ArtifactInventory.js";
import { artifactInventoryResultSchema } from "../../../src/domain/artifactGraph.js";
import {
artifactComparisonResultSchema,
compareArtifacts,
@@ -27,23 +28,8 @@ const PROVIDER = {
version: "1",
} as const;
const observe = async (
path: string,
page: {
readonly nodeOffset?: number;
readonly nodeLimit?: number;
readonly occurrenceOffset?: number;
readonly edgeOffset?: number;
} = {},
) => {
const inventory = await inventoryArtifact(path, LIMITS, {
nodeOffset: page.nodeOffset ?? 0,
nodeLimit: page.nodeLimit ?? 500,
occurrenceOffset: page.occurrenceOffset ?? 0,
occurrenceLimit: 500,
edgeOffset: page.edgeOffset ?? 0,
edgeLimit: 500,
});
const observe = async (path: string) => {
const inventory = await inventoryArtifact(path, LIMITS);
return createEvidence(
{
path,
@@ -76,20 +62,21 @@ describe("artifact comparison", () => {
]);
const left = await observe(leftPath);
const right = await observe(rightPath);
const first = compareArtifacts(left, right, 0, 1);
const second = compareArtifacts(left, right, 0, 1);
const first = compareArtifacts(left, right);
const second = compareArtifacts(left, right);
expect(first).toEqual(second);
expect(artifactComparisonResultSchema.parse(first)).toMatchObject({
status: "changed",
summary: { added: 1, changed: 2, unknown: 0 },
changes: { limit: 1, total: 3, next_offset: 1 },
changes: expect.arrayContaining([
expect.objectContaining({ logical_path: "added.txt" }),
]),
});
expect(first.changes.items[0]?.evidence_links).toEqual([
expect(first.changes[0]?.evidence_links).toEqual([
left.evidence_id,
right.evidence_id,
]);
const all = compareArtifacts(left, right, 0, 500);
expect(all.changes.items).toEqual(
expect(first.changes).toEqual(
expect.arrayContaining([
expect.objectContaining({
logical_path: "added.txt",
@@ -107,8 +94,30 @@ describe("artifact comparison", () => {
it("reports incomplete inventory as truncated, never unchanged", async () => {
const root = await createTestTempDirectory("rea-artifact-truncated-");
await writeFile(join(root, "one.txt"), "one");
const incomplete = await observe(root, { nodeLimit: 1 });
const comparison = compareArtifacts(incomplete, incomplete, 0, 100);
const complete = await observe(root);
const completeInventory = artifactInventoryResultSchema.parse(
complete.normalized_result,
);
const incomplete = createEvidence(
{
path: root,
sha256: complete.subject?.digest.sha256 ?? "0".repeat(64),
format: "directory",
},
PROVIDER,
{
operation: "inventory_artifact",
parameters: {},
result: jsonValueSchema.parse({
...completeInventory,
nodes: [],
}),
confidence: "observed",
authority: "shipped-artifact",
limitations: ["Inventory node observations are unavailable."],
},
);
const comparison = compareArtifacts(incomplete, incomplete);
expect(comparison).toMatchObject({
status: "truncated",
summary: { unchanged: 0, unknown: 2 },
@@ -118,7 +127,7 @@ describe("artifact comparison", () => {
);
});
it("assembles bounded page sets for graphs larger than one page", async () => {
it("compares every graph member for inventories larger than 500 entries", async () => {
const root = await createTestTempDirectory("rea-artifact-pages-");
await Promise.all(
Array.from({ length: 501 }, async (_, index) =>
@@ -128,18 +137,18 @@ describe("artifact comparison", () => {
),
),
);
const pages = await Promise.all([
observe(root),
observe(root, {
nodeOffset: 500,
occurrenceOffset: 500,
edgeOffset: 500,
}),
]);
expect(compareArtifacts(pages, pages, 0, 100)).toMatchObject({
const complete = await observe(root);
expect(complete.normalized_result).toMatchObject({
nodes: expect.arrayContaining([
expect.objectContaining({ kind: expect.any(String) }),
]),
occurrences: expect.any(Array),
edges: expect.any(Array),
});
expect(compareArtifacts(complete, complete)).toMatchObject({
status: "unchanged",
summary: { unchanged: 502, unknown: 0 },
changes: { total: 0 },
changes: [],
});
}, 15_000);
@@ -147,19 +156,14 @@ describe("artifact comparison", () => {
const root = await createTestTempDirectory("rea-artifact-invalid-");
const evidence = await observe(root);
expect(() =>
compareArtifacts(
{ ...evidence, operation: "binary_overview" },
evidence,
0,
10,
),
compareArtifacts({ ...evidence, operation: "binary_overview" }, evidence),
).toThrow(/identifier/u);
const wrongOperation = createEvidence(undefined, PROVIDER, {
operation: "binary_overview",
parameters: {},
result: evidence.normalized_result,
});
expect(() => compareArtifacts(wrongOperation, evidence, 0, 10)).toThrow(
expect(() => compareArtifacts(wrongOperation, evidence)).toThrow(
/inventory_artifact/u,
);
const mismatchedSubject = createEvidence(
@@ -171,7 +175,7 @@ describe("artifact comparison", () => {
result: evidence.normalized_result,
},
);
expect(() => compareArtifacts(mismatchedSubject, evidence, 0, 10)).toThrow(
expect(() => compareArtifacts(mismatchedSubject, evidence)).toThrow(
/root digest/u,
);
});