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
+8 -26
View File
@@ -42,10 +42,8 @@ const comparisonDimensionSchema = z.enum([
/** Strict Evidence-backed input for deterministic artifact comparison. */
export const artifactComparisonInputSchema = z.strictObject({
left_evidence_ids: z.array(evidenceIdSchema).min(1).max(100),
right_evidence_ids: z.array(evidenceIdSchema).min(1).max(100),
offset: z.number().int().min(0).default(0),
limit: z.number().int().min(1).max(500).default(100),
left_evidence_id: evidenceIdSchema,
right_evidence_id: evidenceIdSchema,
unknown_registry_approved: z
.literal(true)
.optional()
@@ -76,7 +74,7 @@ const artifactChangeSchema = z.object({
evidence_links: z.array(evidenceIdSchema).min(2).max(200),
});
/** Bounded deterministic artifact comparison with an independently paged delta. */
/** Deterministic artifact comparison with every change returned inline. */
export const artifactComparisonResultSchema = z.object({
status: comparisonStatusSchema,
left_manifest_id: z.string().regex(/^agm_[a-f0-9]{64}$/u),
@@ -89,13 +87,7 @@ export const artifactComparisonResultSchema = z.object({
unknown: z.number().int().min(0),
contradiction: z.number().int().min(0).default(0),
}),
changes: z.object({
items: z.array(artifactChangeSchema).max(500),
offset: z.number().int().min(0),
limit: z.number().int().min(1).max(500),
total: z.number().int().min(0),
next_offset: z.number().int().min(0).nullable(),
}),
changes: z.array(artifactChangeSchema),
limitations: z.array(z.string()),
});
@@ -104,12 +96,10 @@ export type ArtifactComparisonResult = z.infer<
>;
type ArtifactChange = z.infer<typeof artifactChangeSchema>;
/** Compare complete inventory pages without treating missing evidence as equality. */
/** Compare complete inventory evidence without treating missing evidence as equality. */
export const compareArtifacts = (
leftEvidence: unknown,
rightEvidence: unknown,
offset: number,
limit: number,
): ArtifactComparisonResult => {
const left = parseArtifactInventoryEvidence(leftEvidence);
const right = parseArtifactInventoryEvidence(rightEvidence);
@@ -140,7 +130,6 @@ export const compareArtifacts = (
changes,
leftCovered && rightCovered,
);
const page = changes.slice(offset, offset + limit);
return artifactComparisonResultSchema.parse({
status:
!leftComplete || !rightComplete
@@ -159,14 +148,7 @@ export const compareArtifacts = (
left_manifest_id: left.inventory.manifest.manifest_id,
right_manifest_id: right.inventory.manifest.manifest_id,
summary,
changes: {
items: page,
offset,
limit,
total: changes.length,
next_offset:
offset + page.length < changes.length ? offset + page.length : null,
},
changes,
limitations: [...new Set(limitations)].sort((a, b) => a.localeCompare(b)),
});
};
@@ -400,10 +382,10 @@ interface RelationProjection {
/** Logical parent is stable across graph-root ID changes. */
readonly parent_logical_path: string | null;
readonly child_artifact_id: string;
readonly relation: ArtifactInventoryResult["edges"]["items"][number]["relation"];
readonly relation: ArtifactInventoryResult["edges"][number]["relation"];
readonly logical_path: string | null;
/** Producer is semantic provenance; edge ordinal is presentation order. */
readonly producer: ArtifactInventoryResult["edges"]["items"][number]["producer"];
readonly producer: ArtifactInventoryResult["edges"][number]["producer"];
}
const summarize = (
+10 -23
View File
@@ -182,28 +182,15 @@ const artifactGraphManifestSchema = z.object({
edge_count: z.number().int().min(0),
});
/** Independently bounded offset page used for deterministic graph traversal. */
const artifactPageSchema = <Item extends z.ZodType>(item: Item) =>
z.object({
items: z.array(item),
offset: z.number().int().min(0),
limit: z.number().int().min(1),
total: z.number().int().min(0),
next_offset: z.number().int().min(0).nullable(),
});
/** Inventory observation containing stable manifest identity and bounded pages. */
/** Complete inventory observation containing the stable artifact graph. */
export const artifactInventoryResultSchema = z.object({
manifest: artifactGraphManifestSchema,
nodes: artifactPageSchema(artifactNodeSchema),
occurrences: artifactPageSchema(artifactOccurrenceSchema),
edges: artifactPageSchema(artifactEdgeSchema),
nodes: z.array(artifactNodeSchema),
occurrences: z.array(artifactOccurrenceSchema),
edges: z.array(artifactEdgeSchema),
limits: artifactTraversalLimitsSchema,
provenance: z.array(artifactCommandSchema).max(256),
integrity_contradictions: z
.array(integrityContradictionSchema)
.max(100)
.default([]),
provenance: z.array(artifactCommandSchema),
integrity_contradictions: z.array(integrityContradictionSchema).default([]),
limitations: z.array(z.string()),
});
@@ -220,7 +207,7 @@ const extractedArtifactSchema = z.object({
const artifactExtractionManifestSchema = z.object({
extraction_id: extractionIdSchema,
source_manifest_id: manifestIdSchema,
selected_occurrence_ids: z.array(occurrenceIdSchema).min(1).max(500),
selected_occurrence_ids: z.array(occurrenceIdSchema).min(1),
files_sha256: sha256Schema,
output_root_alias: z.literal("$OUTPUT_ROOT"),
});
@@ -230,15 +217,15 @@ export const artifactExtractionResultSchema = z.object({
manifest: artifactGraphManifestSchema,
extraction_manifest: artifactExtractionManifestSchema,
output_root: z.string().min(1),
artifacts: artifactPageSchema(extractedArtifactSchema),
artifacts: z.array(extractedArtifactSchema),
containment_verified: z.boolean(),
cleanup: z.object({
attempted: z.boolean(),
verified: z.boolean(),
residual_paths: z.array(boundedRelativePathSchema).max(500),
residual_paths: z.array(boundedRelativePathSchema),
}),
limits: artifactTraversalLimitsSchema,
provenance: z.array(artifactCommandSchema).max(256),
provenance: z.array(artifactCommandSchema),
limitations: z.array(z.string()),
});
+5 -11
View File
@@ -231,10 +231,10 @@ const allObservations = (
inventory.manifest,
evidenceId,
),
...inventory.nodes.items.map((node) =>
...inventory.nodes.map((node) =>
observation("artifact", node.artifact_id, node, evidenceId),
),
...inventory.occurrences.items.map((occurrence) =>
...inventory.occurrences.map((occurrence) =>
observation("occurrence", occurrence.occurrence_id, occurrence, evidenceId),
),
...inventory.integrity_contradictions.map((value) =>
@@ -261,7 +261,7 @@ const allRelationships = (
inventory: ArtifactInventoryResult,
evidenceId: string,
): ArtifactInspectionResult["derived_relationships"] =>
inventory.edges.items.map((edge) => {
inventory.edges.map((edge) => {
const semantic = {
relation: edge.relation,
source_artifact_id: edge.parent_artifact_id,
@@ -412,17 +412,11 @@ const branch = (
};
const inspectionCoverage = (
inventory: ArtifactInventoryResult,
_inventory: ArtifactInventoryResult,
omissions: Readonly<Record<string, number>>,
): ArtifactInspectionResult["coverage"]["status"] => {
if (Object.values(omissions).some((count) => count > 0)) return "truncated";
return [
inventory.nodes.next_offset,
inventory.occurrences.next_offset,
inventory.edges.next_offset,
].some((offset) => offset !== null)
? "partial"
: "complete-within-substeps";
return "complete-within-substeps";
};
const digestCanonical = (value: unknown): string => {
+12 -15
View File
@@ -16,7 +16,7 @@ export interface InventorySet {
readonly manifest: ArtifactInventoryResult["manifest"];
readonly nodes: readonly ArtifactNode[];
readonly occurrences: readonly ArtifactOccurrence[];
readonly edges: readonly ArtifactInventoryResult["edges"]["items"][number][];
readonly edges: readonly ArtifactInventoryResult["edges"][number][];
readonly integrityContradictions: readonly IntegrityContradiction[];
readonly limitations: readonly string[];
readonly complete: boolean;
@@ -56,22 +56,22 @@ export const parseArtifactInventoryEvidence = (
const validateInventoryPage = (inventory: ArtifactInventoryResult): void => {
assertUnique(
inventory.nodes.items.map(({ artifact_id: id }) => id),
inventory.nodes.map(({ artifact_id: id }) => id),
"node ID",
);
assertUnique(
inventory.occurrences.items.map(({ occurrence_id: id }) => id),
inventory.occurrences.map(({ occurrence_id: id }) => id),
"occurrence ID",
);
assertUnique(
inventory.occurrences.items.map(({ logical_path: path }) => path),
inventory.occurrences.map(({ logical_path: path }) => path),
"logical path",
);
assertUnique(
inventory.edges.items.map(({ edge_id: id }) => id),
inventory.edges.map(({ edge_id: id }) => id),
"edge ID",
);
for (const node of inventory.nodes.items)
for (const node of inventory.nodes)
if (node.artifact_id !== `art_${digestCanonical({ sha256: node.sha256 })}`)
throw new TypeError("Artifact node ID is not content-addressed");
};
@@ -91,10 +91,7 @@ const assembleInventorySet = (
const nodes = new Map<string, ArtifactNode>();
const occurrences = new Map<string, ArtifactOccurrence>();
const paths = new Map<string, ArtifactOccurrence>();
const edges = new Map<
string,
ArtifactInventoryResult["edges"]["items"][number]
>();
const edges = new Map<string, ArtifactInventoryResult["edges"][number]>();
for (const page of pages) {
if (canonicalJson(page.inventory.manifest) !== manifestJson)
throw new TypeError("Artifact inventory pages do not share one manifest");
@@ -105,9 +102,9 @@ const assembleInventorySet = (
throw new TypeError(
"Artifact inventory pages do not share integrity contradictions",
);
for (const node of page.inventory.nodes.items)
for (const node of page.inventory.nodes)
mergeExact(nodes, node.artifact_id, node, "node");
for (const occurrence of page.inventory.occurrences.items) {
for (const occurrence of page.inventory.occurrences) {
mergeExact(
occurrences,
occurrence.occurrence_id,
@@ -116,7 +113,7 @@ const assembleInventorySet = (
);
mergeExact(paths, occurrence.logical_path, occurrence, "logical path");
}
for (const edge of page.inventory.edges.items)
for (const edge of page.inventory.edges)
mergeExact(edges, edge.edge_id, edge, "edge");
}
const inventory = orderedInventory(first.inventory, pages, {
@@ -136,7 +133,7 @@ const orderedInventory = (
readonly occurrences: ReadonlyMap<string, ArtifactOccurrence>;
readonly edges: ReadonlyMap<
string,
ArtifactInventoryResult["edges"]["items"][number]
ArtifactInventoryResult["edges"][number]
>;
},
): InventorySet => {
@@ -242,7 +239,7 @@ const validateOccurrence = (
};
const validateEdge = (
edge: ArtifactInventoryResult["edges"]["items"][number],
edge: ArtifactInventoryResult["edges"][number],
nodeIds: ReadonlySet<string>,
occurrenceIds: ReadonlySet<string>,
): void => {
-61
View File
@@ -1,61 +0,0 @@
/** A caller-visible page whose total population is known exactly. */
export interface KnownCollectionPage<Item> {
readonly items: readonly Item[];
readonly offset: number;
readonly limit: number;
readonly total: number;
readonly returned: number;
readonly complete: boolean;
}
/** Fail-closed completeness derived from a known-total source page. */
export type KnownPageCoverageAssessment = {
readonly includedCount: number;
readonly omittedCount: number;
readonly sourceOmittedCount: number;
} & (
| {
readonly complete: true;
readonly sourceComplete: true;
readonly omittedCount: 0;
readonly sourceOmittedCount: 0;
}
| { readonly complete: false; readonly sourceComplete: boolean }
);
/**
* Assess source-page and downstream-projection completeness without treating an
* unreturned row as absent. `includedCount` is the number retained downstream.
*/
export const assessKnownPageCoverage = <Item>(
page: KnownCollectionPage<Item>,
includedCount: number = page.items.length,
): KnownPageCoverageAssessment => {
const observedCount = Math.min(page.total, page.returned, page.items.length);
const retainedCount = Math.min(
observedCount,
Math.max(0, Math.floor(includedCount)),
);
const sourceOmittedCount = Math.max(0, page.total - observedCount);
const sourceComplete =
page.complete &&
page.offset === 0 &&
page.returned === page.total &&
sourceOmittedCount === 0;
const omittedCount = Math.max(0, page.total - retainedCount);
return sourceComplete && omittedCount === 0
? {
complete: true,
sourceComplete: true,
includedCount: retainedCount,
omittedCount: 0,
sourceOmittedCount: 0,
}
: {
complete: false,
sourceComplete,
includedCount: retainedCount,
omittedCount,
sourceOmittedCount,
};
};