From 78f4848d70ce462426e9f221d6e7b3e34219bf46 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Wed, 7 Oct 2026 11:40:34 +0800 Subject: [PATCH] refactor(domain): map validation taxonomies and add inventory Result seam (#833) Document the three disjoint failure taxonomies (filesystem reader codes, session reference reasons, inventory TypeErrors) with a test pinning their disjointness. Export ParsedInventorySet and add tryAssembleInventorySet covering the empty-pages leaf as Result; the throwing entry contract is unchanged until callers migrate. --- src/domain/artifactInventoryEvidence.ts | 29 ++++++++++-- src/domain/validationTaxonomy.test.ts | 60 +++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/domain/validationTaxonomy.test.ts diff --git a/src/domain/artifactInventoryEvidence.ts b/src/domain/artifactInventoryEvidence.ts index 0266a0d97..efa459ebb 100644 --- a/src/domain/artifactInventoryEvidence.ts +++ b/src/domain/artifactInventoryEvidence.ts @@ -8,6 +8,7 @@ import { import { canonicalDigest, canonicalJson } from "./comparisonSemantics.js"; import { parseEvidence, type Evidence } from "./evidence.js"; import { artifactInspectionResultSchema } from "./artifactInspection.js"; +import { err, ok, type Result } from "./result.js"; /** Complete or partially assembled inventory used by comparison workflows. */ export interface InventorySet { @@ -20,7 +21,7 @@ export interface InventorySet { readonly complete: boolean; } -interface ParsedInventorySet { +export interface ParsedInventorySet { readonly evidence: readonly Evidence[]; readonly inventory: InventorySet; } @@ -85,14 +86,36 @@ const validateInventoryPage = (inventory: ArtifactInventoryResult): void => { throw new TypeError("Artifact node ID is not content-addressed"); }; +/** Empty-inventory failure, distinct from malformed/unsupported/unavailable taxonomy codes. */ +export interface InventoryAssemblyEmpty { + readonly code: "empty"; + readonly message: string; +} + +/** + * Result-returning seam for the empty-pages leaf. Other assembly failures + * still throw TypeError (migration in progress); parseArtifactInventoryEvidence + * keeps its throwing contract until callers switch. + */ +export const tryAssembleInventorySet = ( + pages: readonly { + readonly evidence: Evidence; + readonly inventory: ArtifactInventoryResult; + }[], +): Result => + pages.length === 0 + ? err({ + code: "empty", + message: "Artifact inventory requires Evidence pages", + }) + : ok(assembleInventorySet(pages)); + const assembleInventorySet = ( pages: readonly { readonly evidence: Evidence; readonly inventory: ArtifactInventoryResult; }[], ): ParsedInventorySet => { - if (pages.length === 0) - throw new TypeError("Artifact inventory requires Evidence pages"); const first = pages[0]; if (first === undefined) throw new TypeError("Artifact inventory requires Evidence pages"); diff --git a/src/domain/validationTaxonomy.test.ts b/src/domain/validationTaxonomy.test.ts new file mode 100644 index 000000000..4407f8097 --- /dev/null +++ b/src/domain/validationTaxonomy.test.ts @@ -0,0 +1,60 @@ +import { describe, expect, it } from "vitest"; + +import { + parseArtifactInventoryEvidence, + tryAssembleInventorySet, +} from "./artifactInventoryEvidence.js"; +import { EvidenceReferenceError } from "./evidenceErrors.js"; +import type { ReferenceSourceReaderError } from "../reference/ReferenceSourceReaderTypes.js"; + +/** + * The three validation taxonomies and their disjoint codes: + * - Filesystem reader: cancelled | invalid-root | io | unsupported + * (per-entry: cancelled | changed | io | symlink | unsupported). + * - Session evidence references: missing | wrong_operation | + * wrong_predicate | identity_mismatch. + * - Inventory assembly: TypeError strings, migrating to Result leaves + * starting with the empty-pages code below. + */ +const readerCodes: readonly ReferenceSourceReaderError["code"][] = [ + "cancelled", + "invalid-root", + "io", + "unsupported", +]; + +const referenceReasons = ( + [ + "missing", + "wrong_operation", + "wrong_predicate", + "identity_mismatch", + ] as const +).map( + (reason) => + new EvidenceReferenceError("ev_test", reason, "expected", null).reason, +); + +describe("validation taxonomy mapping", () => { + it("keeps session reference reasons disjoint from reader codes", () => { + for (const reason of referenceReasons) + expect(readerCodes).not.toContain(reason); + }); + + it("returns the empty-pages leaf as Result without throwing", () => { + const result = tryAssembleInventorySet([]); + expect(result).toEqual({ + ok: false, + error: { + code: "empty", + message: "Artifact inventory requires Evidence pages", + }, + }); + }); + + it("keeps the throwing entry contract for empty input", () => { + expect(() => parseArtifactInventoryEvidence([])).toThrowError( + "Artifact inventory requires Evidence pages", + ); + }); +});