refactor(artifacts): centralize stable inventory identities

This commit is contained in:
morluto
2026-10-09 00:49:30 +08:00
parent d4c2c3c18f
commit 135d102f9d
6 changed files with 347 additions and 82 deletions
@@ -1,4 +1,9 @@
import { canonicalDigest } from "../../domain/comparisonSemantics.js";
import {
artifactEdgeId,
artifactIdForContent,
occurrenceIdForLocation,
} from "../../domain/artifactIdentity.js";
import type { ArtifactEntry } from "../ArtifactReader.js";
import type {
ArtifactCommand,
@@ -159,7 +164,7 @@ export const createArtifactNode = (input: {
readonly contentState: ArtifactNode["content_state"];
readonly limitations?: readonly string[];
}): ArtifactNode => ({
artifact_id: `art_${canonicalDigest({ sha256: input.sha256 }, "Artifact")}`,
artifact_id: artifactIdForContent(input.sha256),
kind: input.kind,
format: input.format,
sha256: input.sha256,
@@ -175,7 +180,11 @@ export const rootOccurrenceFor = (
node: ArtifactNode,
declaredSize: number,
): MutableOccurrence => ({
occurrence_id: `occ_${canonicalDigest({ root: node.artifact_id }, "Artifact")}`,
occurrence_id: occurrenceIdForLocation({
rootArtifactId: node.artifact_id,
logicalPath: ".",
entryKind: node.format === "directory" ? "directory" : "file",
}),
artifact_id: node.artifact_id,
parent_occurrence_id: null,
logical_path: ".",
@@ -197,14 +206,11 @@ export const rekeyOccurrences = (
for (const occurrence of occurrences)
replacements.set(
occurrence.occurrence_id,
`occ_${canonicalDigest(
{
root_artifact_id: rootArtifactId,
logical_path: occurrence.logical_path,
entry_kind: occurrence.entry_kind,
},
"Artifact",
)}`,
occurrenceIdForLocation({
rootArtifactId,
logicalPath: occurrence.logical_path,
entryKind: occurrence.entry_kind,
}),
);
for (const occurrence of occurrences) {
occurrence.occurrence_id =
@@ -248,7 +254,7 @@ export const createArtifactEdges = (
logical_path: occurrence.logical_path,
};
edges.push({
edge_id: `edge_${canonicalDigest(semantic, "Artifact")}`,
edge_id: artifactEdgeId(semantic),
...semantic,
producer: occurrence.entry_kind === "slice" ? (producer ?? null) : null,
ordinal: edges.length,
+18 -26
View File
@@ -21,7 +21,11 @@ import {
rootOccurrenceFor,
type MutableOccurrence,
} from "./ArtifactGraphConstruction.js";
import { canonicalDigest } from "../../domain/comparisonSemantics.js";
import {
artifactContradictionId,
artifactGraphDigest,
artifactManifestId,
} from "../../domain/artifactIdentity.js";
import { classifyRoot } from "./classify.js";
import { hashReadable, type HashResult } from "../ArtifactHash.js";
import { createReader, inventoryLimitations } from "./reader.js";
@@ -125,15 +129,12 @@ const buildInventorySnapshot = async (
await verifyRootDigest(path, rootDigest, signal);
const graphSha256 = canonicalDigest(
{
nodes: orderedNodes,
occurrences: orderedOccurrences,
edges: orderedEdges,
integrity_contradictions: integrityContradictions,
},
"Artifact",
);
const graphSha256 = artifactGraphDigest({
nodes: orderedNodes,
occurrences: orderedOccurrences,
edges: orderedEdges,
contradictions: integrityContradictions,
});
const manifest = buildManifest({
rootNode,
graphSha256,
@@ -171,15 +172,12 @@ const buildIntegrityContradictions = (
: occurrenceById.get(occurrence.parent_occurrence_id);
const parentArtifactId = parent?.artifact_id ?? rootNode.artifact_id;
return {
contradiction_id: `ic_${canonicalDigest(
{
root_artifact_id: rootNode.artifact_id,
logical_path: contradiction.logicalPath,
declared_sha256: contradiction.declaredSha256,
observed_sha256: contradiction.observedSha256,
},
"Artifact",
)}`,
contradiction_id: artifactContradictionId({
rootArtifactId: rootNode.artifact_id,
logicalPath: contradiction.logicalPath,
declaredSha256: contradiction.declaredSha256,
observedSha256: contradiction.observedSha256,
}),
occurrence_id: occurrence.occurrence_id,
parent_artifact_id: parentArtifactId,
logical_path: contradiction.logicalPath,
@@ -241,13 +239,7 @@ const buildManifest = ({
readonly orderedEdges: readonly { edge_id: string }[];
}): ArtifactInventoryResult["manifest"] =>
artifactInventoryResultSchema.shape.manifest.parse({
manifest_id: `agm_${canonicalDigest(
{
root_artifact_id: rootNode.artifact_id,
graph_sha256: graphSha256,
},
"Artifact",
)}`,
manifest_id: artifactManifestId(rootNode.artifact_id, graphSha256),
root_artifact_id: rootNode.artifact_id,
root_sha256: rootNode.sha256,
root_format: rootNode.format,