@orkestrel/capture 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,7 +8,9 @@ let _orkestrel_contract = require("@orkestrel/contract");
8
8
  * `USAGE` is a refused command line and exits 64; every other condition exits 1. `SELECTION`
9
9
  * names a selector, variant, case, state, or portfolio the host does not declare. `HOST` names a
10
10
  * wrapper, configuration, or declaration fault. `STAGE` names a runner stage that did not
11
- * complete. `EVIDENCE` names a record, report, registry, or manifest that fails its protocol.
11
+ * complete. `EVIDENCE` names a record, report, journal, or manifest that fails its protocol, and a
12
+ * document of another version. `REGISTRY` names an accepted registry that is absent, fails
13
+ * authentication, or lags the declarations. `LOOKUP` names a state lookup that is absent or stale.
12
14
  * `CONTAINMENT` names a path that leaves the authorized roots.
13
15
  *
14
16
  * @example
@@ -22,6 +24,8 @@ var CAPTURE_ERROR_CODES = Object.freeze([
22
24
  "HOST",
23
25
  "STAGE",
24
26
  "EVIDENCE",
27
+ "REGISTRY",
28
+ "LOOKUP",
25
29
  "CONTAINMENT"
26
30
  ]);
27
31
  /**
@@ -71,10 +75,10 @@ var CAPTURE_REQUEST_PREFIX = "journey-selection";
71
75
  *
72
76
  * @example
73
77
  * ```ts
74
- * CAPTURE_VERSION // 1
78
+ * CAPTURE_VERSION // 2
75
79
  * ```
76
80
  */
77
- var CAPTURE_VERSION = 1;
81
+ var CAPTURE_VERSION = 2;
78
82
  /**
79
83
  * Holds the eight bytes every PNG file opens with.
80
84
  *
@@ -120,6 +124,31 @@ var CAPTURE_MANIFEST_FILE = "manifest.json";
120
124
  * ```
121
125
  */
122
126
  var CAPTURE_LOOKUPS_FILE = "lookups.json";
127
+ /**
128
+ * Names the tag prefix that marks a case's owner path and each ancestor of it.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * `${CAPTURE_OWNER_PREFIX}pages:mail` // 'owner:pages:mail'
133
+ * ```
134
+ */
135
+ var CAPTURE_OWNER_PREFIX = "owner:";
136
+ /**
137
+ * Names the journey artifact line prefixes that bind a run record and each case that ran.
138
+ *
139
+ * @remarks
140
+ * The preflight reader requires the line `record=<record path>` and one `case-ran=<full name>`
141
+ * line for each stamping case.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * `${CAPTURE_ARTIFACT_PREFIXES.case}mail opens the inbox` // 'case-ran=mail opens the inbox'
146
+ * ```
147
+ */
148
+ var CAPTURE_ARTIFACT_PREFIXES = Object.freeze({
149
+ record: "record=",
150
+ case: "case-ran="
151
+ });
123
152
  // src/core/errors.ts
124
153
  /**
125
154
  * Reports one capture failure under a stable condition axis.
@@ -314,11 +343,11 @@ function isCaptureIdentity(value) {
314
343
  * @example
315
344
  * ```ts
316
345
  * isCaptureRequest(JSON.parse(text)) // true for a well-formed runner request
317
- * isCaptureRequest({ version: 2 }) // false
346
+ * isCaptureRequest({ version: 1 }) // false
318
347
  * ```
319
348
  */
320
349
  function isCaptureRequest(value) {
321
- return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && isCaptureIdentity(value) && value.version === 1 && (value.command === "journey" || value.command === "capture") && (value.mode === void 0 || typeof value.mode === "string" && value.mode.trim() !== "") && isCaptureSelector(value.selector) && (0, _orkestrel_contract.arrayOf)(isCaptureSegment)(value.variants) && value.variants.length > 0 && (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString)(value.args) && (value.case === void 0 || typeof value.case === "string" && value.case.trim() !== "") && (value.state === void 0 || value.command === "capture" && isCaptureSegment(value.state)));
350
+ return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && isCaptureIdentity(value) && value.version === 2 && (value.command === "journey" || value.command === "capture") && (value.mode === void 0 || typeof value.mode === "string" && value.mode.trim() !== "") && isCaptureSelector(value.selector) && (0, _orkestrel_contract.arrayOf)(isCaptureSegment)(value.variants) && value.variants.length > 0 && (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString)(value.args) && (value.case === void 0 || typeof value.case === "string" && value.case.trim() !== "") && (value.state === void 0 || value.command === "capture" && isCaptureSegment(value.state)));
322
351
  }
323
352
  /**
324
353
  * Checks whether a collected declaration preserves its full owner and state population.
@@ -398,7 +427,7 @@ function isCaptureArtifact(value) {
398
427
  * ```
399
428
  */
400
429
  function isCaptureRun(value) {
401
- return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && value.version === 1 && isCaptureSegment(value.variant) && typeof value.time === "string" && Number.isFinite(Date.parse(value.time)) && typeof value.complete === "boolean" && typeof value.reference === "boolean" && (0, _orkestrel_contract.arrayOf)(isCaptureDeclaration)(value.declarations) && (0, _orkestrel_contract.arrayOf)(isCaptureDestination)(value.registry) && (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString)(value.passed) && (0, _orkestrel_contract.arrayOf)(isCaptureSegment)(value.observed) && (0, _orkestrel_contract.arrayOf)(isCaptureEntry)(value.entries) && (value.artifact === void 0 || isCaptureArtifact(value.artifact)) && (value.identity === void 0 && value.request === void 0 || isCaptureIdentity(value.identity) && isCaptureRequest(value.request)));
430
+ return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && value.version === 2 && isCaptureSegment(value.variant) && typeof value.time === "string" && Number.isFinite(Date.parse(value.time)) && typeof value.complete === "boolean" && typeof value.reference === "boolean" && (0, _orkestrel_contract.arrayOf)(isCaptureDeclaration)(value.declarations) && (0, _orkestrel_contract.arrayOf)(isCaptureDestination)(value.registry) && (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString)(value.passed) && (0, _orkestrel_contract.arrayOf)(isCaptureSegment)(value.observed) && (0, _orkestrel_contract.arrayOf)(isCaptureEntry)(value.entries) && (value.artifact === void 0 || isCaptureArtifact(value.artifact)) && (value.identity === void 0 && value.request === void 0 || isCaptureIdentity(value.identity) && isCaptureRequest(value.request)));
402
431
  }
403
432
  /**
404
433
  * Checks whether a shared-source digest retains both patterns and expanded file hashes.
@@ -428,7 +457,7 @@ function isCaptureDigest(value) {
428
457
  * ```
429
458
  */
430
459
  function isCaptureReceipt(value) {
431
- return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && value.version === 1 && isCaptureSegment(value.variant) && typeof value.time === "string" && Number.isFinite(Date.parse(value.time)) && (0, _orkestrel_contract.isRecord)(value.report) && isCapturePath(value.report.file) && isCaptureHash(value.report.digest) && isCaptureHashes(value.inputs) && (0, _orkestrel_contract.arrayOf)(isCaptureDeclaration)(value.declarations) && (0, _orkestrel_contract.arrayOf)(isCaptureDestination)(value.registry) && typeof value.complete === "boolean");
460
+ return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && value.version === 2 && isCaptureSegment(value.variant) && typeof value.time === "string" && Number.isFinite(Date.parse(value.time)) && (0, _orkestrel_contract.isRecord)(value.report) && isCapturePath(value.report.file) && isCaptureHash(value.report.digest) && isCaptureHashes(value.inputs) && (0, _orkestrel_contract.arrayOf)(isCaptureDeclaration)(value.declarations) && (0, _orkestrel_contract.arrayOf)(isCaptureDestination)(value.registry) && typeof value.complete === "boolean");
432
461
  }
433
462
  /**
434
463
  * Checks whether a registry document retains its receipts and required output pairs.
@@ -443,7 +472,7 @@ function isCaptureReceipt(value) {
443
472
  * ```
444
473
  */
445
474
  function isCaptureRegistry(value) {
446
- return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && value.version === 1 && (0, _orkestrel_contract.arrayOf)(isCaptureReceipt)(value.receipts) && (0, _orkestrel_contract.arrayOf)(isCaptureDestination)(value.entries) && isCaptureHash(value.digest));
475
+ return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && value.version === 2 && (0, _orkestrel_contract.arrayOf)(isCaptureReceipt)(value.receipts) && (0, _orkestrel_contract.arrayOf)(isCaptureDestination)(value.entries) && isCaptureHash(value.digest));
447
476
  }
448
477
  /**
449
478
  * Checks whether a lookup binds a passing case to its full run record and input hashes.
@@ -469,12 +498,12 @@ function isCaptureLookup(value) {
469
498
  *
470
499
  * @example
471
500
  * ```ts
472
- * isCaptureLookupJournal({ version: 1, inputs: {}, lookups: [] }) // true
473
- * isCaptureLookupJournal({ version: 2, inputs: {}, lookups: [] }) // false
501
+ * isCaptureLookupJournal({ version: 2, populations: {}, lookups: [] }) // true
502
+ * isCaptureLookupJournal({ version: 1, populations: {}, lookups: [] }) // false
474
503
  * ```
475
504
  */
476
505
  function isCaptureLookupJournal(value) {
477
- return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && (value.collections === void 0 || (0, _orkestrel_contract.arrayOf)(isCaptureCollection)(value.collections)) && value.version === 1 && (value.manifest === void 0 || isCaptureHash(value.manifest)) && (0, _orkestrel_contract.isRecord)(value.inputs) && Object.entries(value.inputs).every(([digest, inputs]) => isCaptureHash(digest) && isCaptureHashes(inputs)) && (0, _orkestrel_contract.arrayOf)((entry) => (0, _orkestrel_contract.isRecord)(entry) && isCaptureHash(entry.inputs) && isCaptureLookup({
506
+ return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && (value.collections === void 0 || (0, _orkestrel_contract.arrayOf)(isCaptureCollection)(value.collections)) && value.version === 2 && (value.manifest === void 0 || isCaptureHash(value.manifest)) && (0, _orkestrel_contract.isRecord)(value.populations) && Object.entries(value.populations).every(([digest, inputs]) => isCaptureHash(digest) && isCaptureHashes(inputs)) && (0, _orkestrel_contract.arrayOf)((entry) => (0, _orkestrel_contract.isRecord)(entry) && isCaptureHash(entry.inputs) && isCaptureLookup({
478
507
  ...entry,
479
508
  inputs: {}
480
509
  }))(value.lookups));
@@ -495,19 +524,19 @@ function isCaptureStamp(value) {
495
524
  return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && isCaptureEntry(value) && isCaptureIdentity(value.identity) && typeof value.time === "string" && Number.isFinite(Date.parse(value.time)) && typeof value.commit === "string" && /^[a-f0-9]{40,64}$/u.test(value.commit) && (0, _orkestrel_contract.arrayOf)(isCapturePath)(value.dirty) && isCaptureHashes(value.own) && isCaptureDigest(value.shared) && isCaptureHashes(value.tests) && isCaptureHash(value.registry) && (0, _orkestrel_contract.isRecord)(value.image) && isCaptureHash(value.image.digest) && (0, _orkestrel_contract.isFiniteNumber)(value.image.size) && Number.isSafeInteger(value.image.size) && value.image.size > 0 && (0, _orkestrel_contract.isFiniteNumber)(value.image.modified) && value.image.modified >= 0);
496
525
  }
497
526
  /**
498
- * Checks whether an inherited manifest has the supported schema without discarding malformed rows.
527
+ * Checks whether an expanded manifest has the supported schema without discarding malformed rows.
499
528
  *
500
529
  * @param value - The manifest
501
530
  * @returns True if every inherited member validates; false otherwise
502
531
  *
503
532
  * @example
504
533
  * ```ts
505
- * isCaptureManifest(JSON.parse(text)) // true for a published manifest.json
534
+ * isCaptureManifest(readCaptureManifest(JSON.parse(text), file)) // true for an expanded manifest.json
506
535
  * isCaptureManifest('manifest') // false
507
536
  * ```
508
537
  */
509
538
  function isCaptureManifest(value) {
510
- return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && value.version === 1 && isCaptureRegistry(value.registry) && (0, _orkestrel_contract.isRecord)(value.entries) && Object.entries(value.entries).every(([key, stamp]) => isCapturePath(key) && isCaptureStamp(stamp)) && (0, _orkestrel_contract.arrayOf)(isCaptureLookup)(value.lookups));
539
+ return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && value.version === 2 && isCaptureRegistry(value.registry) && (0, _orkestrel_contract.isRecord)(value.entries) && Object.entries(value.entries).every(([key, stamp]) => isCapturePath(key) && isCaptureStamp(stamp)) && (0, _orkestrel_contract.arrayOf)(isCaptureLookup)(value.lookups));
511
540
  }
512
541
  /**
513
542
  * Checks whether collection evidence pins complete input membership and both external artifacts.
@@ -525,21 +554,68 @@ function isCaptureCollection(value) {
525
554
  return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && isCaptureHash(value.registry) && isCaptureSegment(value.variant) && typeof value.project === "string" && value.project.trim() !== "" && isCaptureDigest(value.inputs) && (0, _orkestrel_contract.isRecord)(value.run) && isCapturePath(value.run.file) && isCaptureHash(value.run.digest) && (0, _orkestrel_contract.isRecord)(value.report) && isCapturePath(value.report.file) && isCaptureHash(value.report.digest));
526
555
  }
527
556
  /**
528
- * Checks optional collection metadata without upgrading historical image evidence.
557
+ * Checks whether an expanded manifest and its optional collection receipts have the supported schema.
529
558
  *
530
- * @param value - The inherited image transaction
531
- * @returns True if its existing manifest and any collection receipts are valid; false otherwise
559
+ * @param value - The expanded manifest
560
+ * @returns True if the manifest and any collection receipts are valid; false otherwise
532
561
  *
533
562
  * @example
534
563
  * ```ts
535
- * isCaptureStoredManifest(JSON.parse(text)) // true for a manifest with or without collections
536
- * isCaptureStoredManifest(null) // false
564
+ * isCaptureExpandedManifest(readCaptureManifest(JSON.parse(text), file)) // true with or without collections
565
+ * isCaptureExpandedManifest(null) // false
537
566
  * ```
538
567
  */
539
- function isCaptureStoredManifest(value) {
568
+ function isCaptureExpandedManifest(value) {
540
569
  return (0, _orkestrel_contract.holds)(() => isCaptureManifest(value) && (0, _orkestrel_contract.isRecord)(value) && (value.collections === void 0 || (0, _orkestrel_contract.arrayOf)(isCaptureCollection)(value.collections)));
541
570
  }
542
571
  /**
572
+ * Checks whether a digest names its file map by a table key beside its patterns and combined digest.
573
+ *
574
+ * @param value - The stored digest reference
575
+ * @returns True if the patterns are valid and both digests are SHA-256 hexadecimal; false otherwise
576
+ *
577
+ * @example
578
+ * ```ts
579
+ * isCaptureDigestReference({ patterns: ['src/**'], files: 'a'.repeat(64), digest: 'b'.repeat(64) }) // true
580
+ * isCaptureDigestReference({ patterns: ['src/**'], files: {}, digest: 'b'.repeat(64) }) // false
581
+ * ```
582
+ */
583
+ function isCaptureDigestReference(value) {
584
+ return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && (0, _orkestrel_contract.arrayOf)(isCapturePattern)(value.patterns) && isCaptureHash(value.files) && isCaptureHash(value.digest));
585
+ }
586
+ /**
587
+ * Checks whether a stored manifest holds its population table and every member that names a table key.
588
+ *
589
+ * @param value - The decoded manifest file
590
+ * @returns True if the version, table, registry, stamps, lookups, and collections are valid; false otherwise
591
+ * @remarks Table digests and reference existence are verified when the document is read.
592
+ *
593
+ * @example
594
+ * ```ts
595
+ * isCaptureManifestDocument(JSON.parse(text)) // true for a written manifest.json
596
+ * isCaptureManifestDocument({ version: 1, populations: {}, entries: {}, lookups: [] }) // false
597
+ * ```
598
+ */
599
+ function isCaptureManifestDocument(value) {
600
+ return (0, _orkestrel_contract.holds)(() => (0, _orkestrel_contract.isRecord)(value) && isCaptureLookupJournal({
601
+ version: value.version,
602
+ populations: value.populations,
603
+ lookups: value.lookups
604
+ }) && isCaptureRegistry(value.registry) && (0, _orkestrel_contract.isRecord)(value.entries) && Object.entries(value.entries).every(([key, stamp]) => isCapturePath(key) && (0, _orkestrel_contract.isRecord)(stamp) && isCaptureDigestReference(stamp.shared) && isCaptureStamp({
605
+ ...stamp,
606
+ shared: {
607
+ ...stamp.shared,
608
+ files: {}
609
+ }
610
+ })) && (value.collections === void 0 || (0, _orkestrel_contract.arrayOf)((entry) => (0, _orkestrel_contract.isRecord)(entry) && isCaptureDigestReference(entry.inputs) && isCaptureCollection({
611
+ ...entry,
612
+ inputs: {
613
+ ...entry.inputs,
614
+ files: {}
615
+ }
616
+ }))(value.collections)));
617
+ }
618
+ /**
543
619
  * Checks the host's declared viewport without deriving geometry from an output name.
544
620
  *
545
621
  * @param value - The published viewport data
@@ -871,8 +947,8 @@ function readCaptureReport(value) {
871
947
  }));
872
948
  requireCaptureUnique(assertions.map(resolveLookupKey), "reporter case identities");
873
949
  return {
874
- start: Math.min(value.startTime, ...files.map((file) => file.start)),
875
- end: Math.max(...files.map((file) => file.end)),
950
+ start: files.reduce((start, file) => Math.min(start, file.start), value.startTime),
951
+ end: files.reduce((end, file) => Math.max(end, file.end), -Infinity),
876
952
  assertions
877
953
  };
878
954
  }
@@ -986,16 +1062,371 @@ function validateCaptureReceipt(receipt, report) {
986
1062
  }
987
1063
  if (receipt.registry.some((entry) => !eligible.some((declaration) => declaration.case === entry.case)) || new Set(receipt.registry.map((entry) => entry.project)).size > 1) throw new CaptureError("Registry receipt contains an undeclared output or mixes producing projects", { code: "EVIDENCE" });
988
1064
  }
1065
+ /**
1066
+ * Expands an owner path into its prefixes, from its scope through the owner itself.
1067
+ *
1068
+ * @param owner - A canonical owner path
1069
+ * @returns Each colon-bounded prefix, shortest first
1070
+ *
1071
+ * @example
1072
+ * ```ts
1073
+ * collectCaptureAncestors('pages:mail:inbox') // ['pages', 'pages:mail', 'pages:mail:inbox']
1074
+ * ```
1075
+ */
1076
+ function collectCaptureAncestors(owner) {
1077
+ return owner.split(":").map((_part, index, parts) => parts.slice(0, index + 1).join(":"));
1078
+ }
1079
+ /**
1080
+ * Builds the owner and capture tags a wrapper declares for its owner population.
1081
+ *
1082
+ * @param owners - Every canonical owner path the host declares
1083
+ * @returns The unique `owner:` tag of each owner and ancestor, and each owner's scope and part tags
1084
+ * @throws Thrown as a `HOST` capture error naming each owner that is not a canonical selector.
1085
+ *
1086
+ * @example
1087
+ * ```ts
1088
+ * buildCaptureTags(['pages:mail'])
1089
+ * // ['owner:pages', 'owner:pages:mail', 'pages', 'pages:mail']
1090
+ * ```
1091
+ */
1092
+ function buildCaptureTags(owners) {
1093
+ const invalid = owners.filter((owner) => !isCaptureSelector(owner));
1094
+ if (invalid.length > 0) throw new CaptureError(`Invalid capture owners: ${invalid.map((owner) => (0, _orkestrel_contract.preview)(owner)).join(", ")}; expected colon-separated portable segments such as "pages:mail"`, {
1095
+ code: "HOST",
1096
+ context: { owners: invalid }
1097
+ });
1098
+ return [...new Set(owners.flatMap((owner) => [...collectCaptureAncestors(owner).map((path) => `${CAPTURE_OWNER_PREFIX}${path}`), ...collectCaptureAncestors(owner).slice(0, 2)]))];
1099
+ }
1100
+ /**
1101
+ * Builds the Vitest test options of one capture case from its declaration.
1102
+ *
1103
+ * @param input - The case's owner, states, producing variant, and harness values
1104
+ * @param rules - The host's owners and part resolver
1105
+ * @returns The derived tags, the capture metadata, and the optional timeout
1106
+ * @throws Thrown as a `HOST` capture error for an undeclared owner or tag, tags beside an owner, capture tags with no states, or a state whose part is undeclared or outside the owner.
1107
+ *
1108
+ * @example
1109
+ * ```ts
1110
+ * buildCaptureTest({ owner: 'pages:mail', states: ['inbox'], variant: 'light-390' }, rules).tags
1111
+ * // ['owner:pages', 'owner:pages:mail', 'pages', 'pages:mail']
1112
+ * ```
1113
+ */
1114
+ function buildCaptureTest(input, rules) {
1115
+ const declared = buildCaptureTags(rules.owners);
1116
+ const owner = input.owner;
1117
+ if (owner !== void 0 && !declared.includes(`owner:${owner}`)) throw new CaptureError(`Unknown capture owner: ${(0, _orkestrel_contract.preview)(owner)}; expected a declared owner path or one of its ancestors`, {
1118
+ code: "HOST",
1119
+ context: { owner }
1120
+ });
1121
+ if (owner !== void 0 && input.tags !== void 0) throw new CaptureError(`Capture case owned by ${owner} derives its tags from its owner and states; remove its tags`, {
1122
+ code: "HOST",
1123
+ context: { owner }
1124
+ });
1125
+ const unknown = (input.tags ?? []).filter((tag) => !declared.includes(tag));
1126
+ if (unknown.length > 0) throw new CaptureError(`Unknown capture tags: ${unknown.map((tag) => (0, _orkestrel_contract.preview)(tag)).join(", ")}; expected tags the owners declare`, {
1127
+ code: "HOST",
1128
+ context: { tags: unknown }
1129
+ });
1130
+ if (input.states === void 0 && (input.tags ?? []).some((tag) => !tag.startsWith("owner:"))) throw new CaptureError("Capture case names capture tags but declares no states; declare its states, an empty list included", { code: "HOST" });
1131
+ const parts = (input.states ?? []).map((state) => {
1132
+ const part = rules.part(state);
1133
+ if (!declared.includes(`owner:${part}`)) throw new CaptureError(`Capture state ${state} resolves to an undeclared owner: ${(0, _orkestrel_contract.preview)(part)}`, {
1134
+ code: "HOST",
1135
+ context: {
1136
+ state,
1137
+ part
1138
+ }
1139
+ });
1140
+ if (owner !== void 0 && !matchesCaptureSelector(part, owner)) throw new CaptureError(`Capture state ${state} belongs to ${part}, outside owner ${owner}; split the case by owner`, {
1141
+ code: "HOST",
1142
+ context: {
1143
+ state,
1144
+ part,
1145
+ owner
1146
+ }
1147
+ });
1148
+ return part;
1149
+ });
1150
+ return {
1151
+ tags: [.../* @__PURE__ */ new Set([...input.owner === void 0 ? input.tags ?? [] : collectCaptureAncestors(input.owner).map((path) => `${CAPTURE_OWNER_PREFIX}${path}`), ...parts.flatMap((part) => collectCaptureAncestors(part).slice(0, 2))])],
1152
+ meta: {
1153
+ ...input.owner === void 0 ? {} : { owner: input.owner },
1154
+ ...input.states === void 0 ? {} : { states: [...input.states] },
1155
+ variant: input.variant,
1156
+ ...input.eligible === void 0 ? {} : { eligible: input.eligible }
1157
+ },
1158
+ ...input.timeout === void 0 ? {} : { timeout: input.timeout }
1159
+ };
1160
+ }
1161
+ /**
1162
+ * Collects every leaf case under a task, keeping cases a runner filter excluded and their results.
1163
+ *
1164
+ * @param task - A Vitest file, suite, or test task
1165
+ * @param ancestors - The titles of the suites that enclose the task, excluding the file name. Default: none
1166
+ * @returns Each case with its suite titles and own title joined by spaces, as the Vitest JSON reporter names it
1167
+ *
1168
+ * @example
1169
+ * ```ts
1170
+ * collectCaptureCases({ name: 'mail', mode: 'run', meta: {}, tasks: [inbox] })[0]?.name
1171
+ * // 'mail opens the inbox'
1172
+ * ```
1173
+ */
1174
+ function collectCaptureCases(task, ancestors = []) {
1175
+ const names = task.filepath === void 0 ? [...ancestors, task.name] : ancestors;
1176
+ if (task.tasks !== void 0) return task.tasks.flatMap((child) => collectCaptureCases(child, names));
1177
+ return [{
1178
+ name: names.join(" "),
1179
+ mode: task.mode,
1180
+ tags: [...task.tags ?? []],
1181
+ meta: {
1182
+ ...task.meta.owner === void 0 ? {} : { owner: task.meta.owner },
1183
+ ...task.meta.states === void 0 ? {} : { states: [...task.meta.states] },
1184
+ ...task.meta.variant === void 0 ? {} : { variant: task.meta.variant },
1185
+ ...task.meta.eligible === void 0 ? {} : { eligible: task.meta.eligible }
1186
+ },
1187
+ ...task.result === void 0 ? {} : { result: {
1188
+ state: task.result.state,
1189
+ ...task.result.duration === void 0 ? {} : { duration: task.result.duration }
1190
+ } }
1191
+ }];
1192
+ }
1193
+ /**
1194
+ * Refuses a selection whose cases place states owned outside the selected owner.
1195
+ *
1196
+ * @param cases - Every collected case, including cases a runner filter excluded
1197
+ * @param rules - The host's owners and part resolver
1198
+ * @param selection - The runner's selection, absent when no runner is the parent
1199
+ * @throws Thrown as a `SELECTION` capture error for an undeclared selector, and as a `HOST` capture error naming each case that places a selected state from outside the selected owner.
1200
+ *
1201
+ * @example
1202
+ * ```ts
1203
+ * requireCaptureCases(cases, rules, { selector: 'pages:mail' }) // returns when no case is parked
1204
+ * ```
1205
+ */
1206
+ function requireCaptureCases(cases, rules, selection) {
1207
+ if (selection === void 0 || selection.selector === "all") return;
1208
+ const { selector } = selection;
1209
+ if (!buildCaptureTags(rules.owners).includes(`owner:${selector}`)) throw new CaptureError(`Unknown capture selector: ${(0, _orkestrel_contract.preview)(selector)}; expected all or a declared owner path`, {
1210
+ code: "SELECTION",
1211
+ context: { selector }
1212
+ });
1213
+ const parked = cases.filter((entry) => selection.case === void 0 || entry.name === selection.case).flatMap((entry) => {
1214
+ const owner = entry.meta.owner;
1215
+ if (owner === void 0 || matchesCaptureSelector(owner, selector)) return [];
1216
+ const states = (entry.meta.states ?? []).filter((state) => matchesCaptureSelector(rules.part(state), selector));
1217
+ return states.length > 0 ? [`${entry.name}: owner ${owner}; foreign states ${states.join(", ")}`] : [];
1218
+ });
1219
+ if (parked.length > 0) throw new CaptureError(`Capture selection ${selector} is parked; split each case so its states stay inside its owner:\n${parked.join("\n")}`, {
1220
+ code: "HOST",
1221
+ context: {
1222
+ selector,
1223
+ parked
1224
+ }
1225
+ });
1226
+ }
1227
+ /**
1228
+ * Builds the required output pair of each state an eligible owner-bearing case declares.
1229
+ *
1230
+ * @param cases - Every collected case, including cases a runner filter excluded
1231
+ * @param rules - The host's part and source resolvers
1232
+ * @param options - The producing project and the portfolio mapping of each state
1233
+ * @returns One destination per declared state, in case and state order
1234
+ * @throws Thrown as a `HOST` capture error when a state has no single mapping or resolves outside its case's owner or the scope, part, and optional section form.
1235
+ *
1236
+ * @example
1237
+ * ```ts
1238
+ * buildCaptureDestinations(cases, rules, { project: 'journey:light-390', portfolios })[0]?.file
1239
+ * // 'tmp/captures/states/pages/inbox--light-390.png'
1240
+ * ```
1241
+ */
1242
+ function buildCaptureDestinations(cases, rules, options) {
1243
+ return cases.flatMap((entry) => {
1244
+ const declared = entry.meta.owner;
1245
+ if (declared === void 0 || entry.meta.eligible === false) return [];
1246
+ return (entry.meta.states ?? []).map((state) => {
1247
+ const mappings = options.portfolios.filter((portfolio) => portfolio.states.includes(state));
1248
+ const [mapping] = mappings;
1249
+ if (mapping === void 0 || mappings.length > 1) throw new CaptureError(`Capture state ${state} needs exactly one portfolio mapping; mapped by: ${mappings.map((candidate) => (0, _orkestrel_contract.preview)(candidate.portfolio)).join(", ") || "none"}`, {
1250
+ code: "HOST",
1251
+ context: { state }
1252
+ });
1253
+ const owner = rules.part(state);
1254
+ if (!matchesCaptureSelector(owner, declared)) throw new CaptureError(`Capture state ${state} belongs to ${owner}, outside owner ${declared} of ${entry.name}`, {
1255
+ code: "HOST",
1256
+ context: {
1257
+ state,
1258
+ owner,
1259
+ declared
1260
+ }
1261
+ });
1262
+ const [scope, part, section, ...rest] = owner.split(":");
1263
+ if (scope === void 0 || part === void 0 || rest.length > 0) throw new CaptureError(`Capture state ${state} resolves to ${(0, _orkestrel_contract.preview)(owner)}; expected scope:part or scope:part:section`, {
1264
+ code: "HOST",
1265
+ context: {
1266
+ state,
1267
+ owner
1268
+ }
1269
+ });
1270
+ return {
1271
+ portfolio: mapping.portfolio,
1272
+ state,
1273
+ scope,
1274
+ part,
1275
+ ...section === void 0 ? {} : { section },
1276
+ variant: mapping.variant,
1277
+ project: options.project,
1278
+ file: `${mapping.directory}/${mapping.scoped ? `${scope}/` : ""}${state}--${mapping.variant}.png`,
1279
+ case: entry.name,
1280
+ sources: rules.sources(owner)
1281
+ };
1282
+ });
1283
+ });
1284
+ }
1285
+ /**
1286
+ * Builds a run record from collected cases, the run's destinations, and its observed and emitted states.
1287
+ *
1288
+ * @param options - The cases, registry, observed states, emitted state-to-path pairs, and execution context
1289
+ * @returns The final record, without the journey artifact the caller binds after rendering it
1290
+ * @throws Thrown as a `HOST` capture error for an owner-bearing case with no variant, and as an `EVIDENCE` capture error for an emitted state with no destination, a path other than its destination's file, or a producing case with no measured duration.
1291
+ *
1292
+ * @example
1293
+ * ```ts
1294
+ * const run = buildCaptureRun({ cases, registry, observed, emitted, variant: 'light-390', time })
1295
+ * validateCaptureRun(run, readCaptureReport(parseJSON(text)), 'journey:light-390')
1296
+ * ```
1297
+ */
1298
+ function buildCaptureRun(options) {
1299
+ const declarations = options.cases.flatMap((entry) => {
1300
+ const { owner, variant } = entry.meta;
1301
+ if (owner === void 0) return [];
1302
+ if (variant === void 0) throw new CaptureError(`Owner-bearing case declares no variant: ${entry.name}`, { code: "HOST" });
1303
+ return [{
1304
+ case: entry.name,
1305
+ owner,
1306
+ variant,
1307
+ eligible: entry.meta.eligible !== false,
1308
+ states: [...entry.meta.states ?? []]
1309
+ }];
1310
+ });
1311
+ const passed = declarations.filter((declaration) => declaration.eligible && options.cases.some((entry) => entry.name === declaration.case && entry.result?.state === "pass")).map((declaration) => declaration.case);
1312
+ const entries = options.emitted.map(([state, path]) => {
1313
+ const destination = options.registry.find((entry) => entry.state === state);
1314
+ if (destination === void 0) throw new CaptureError(`Emitted state has no registry destination: ${(0, _orkestrel_contract.preview)(state)}`, {
1315
+ code: "EVIDENCE",
1316
+ context: { state }
1317
+ });
1318
+ const written = path.replaceAll("\\", "/");
1319
+ if (written !== destination.file && !written.endsWith(`/${destination.file}`)) throw new CaptureError(`Emitted state ${state} was written at ${(0, _orkestrel_contract.preview)(path)}, not at its destination ${destination.file}`, {
1320
+ code: "EVIDENCE",
1321
+ context: {
1322
+ state,
1323
+ path,
1324
+ file: destination.file
1325
+ }
1326
+ });
1327
+ const duration = options.cases.find((entry) => entry.name === destination.case)?.result?.duration;
1328
+ if (duration === void 0) throw new CaptureError(`Emitting case has no measured duration: ${destination.case}`, {
1329
+ code: "EVIDENCE",
1330
+ context: {
1331
+ state,
1332
+ case: destination.case
1333
+ }
1334
+ });
1335
+ return {
1336
+ ...destination,
1337
+ duration
1338
+ };
1339
+ });
1340
+ const eligible = declarations.filter((declaration) => declaration.eligible);
1341
+ const reference = options.registry.filter((entry) => entry.section === entry.state);
1342
+ const request = options.request;
1343
+ return {
1344
+ version: 2,
1345
+ ...request === void 0 ? {} : {
1346
+ identity: {
1347
+ id: request.id,
1348
+ stage: request.stage,
1349
+ pid: request.pid,
1350
+ config: request.config
1351
+ },
1352
+ request
1353
+ },
1354
+ variant: options.variant,
1355
+ time: options.time,
1356
+ complete: eligible.length > 0 && eligible.every((entry) => passed.includes(entry.case)),
1357
+ reference: reference.length > 0 && reference.every((entry) => passed.includes(entry.case)),
1358
+ declarations,
1359
+ registry: [...options.registry],
1360
+ passed,
1361
+ observed: [...options.observed],
1362
+ entries
1363
+ };
1364
+ }
1365
+ /**
1366
+ * Renders the journey artifact lines that bind a run record and each case that passed in it.
1367
+ *
1368
+ * @param record - The run record's portable path
1369
+ * @param run - The run whose passed cases the artifact names
1370
+ * @returns The `record=` line, then one `case-ran=` line per passed case, without line endings
1371
+ * @throws Thrown as an `EVIDENCE` capture error for a record path that is not portable or a case name that holds a line break.
1372
+ *
1373
+ * @example
1374
+ * ```ts
1375
+ * renderCaptureArtifact('tmp/captures/states/runs/run-1/selected/light-390.json', {
1376
+ * passed: ['mail opens the inbox'],
1377
+ * })
1378
+ * // ['record=tmp/captures/states/runs/run-1/selected/light-390.json', 'case-ran=mail opens the inbox']
1379
+ * ```
1380
+ */
1381
+ function renderCaptureArtifact(record, run) {
1382
+ if (!isCapturePath(record)) throw new CaptureError(`Artifact record path is not a portable relative path: ${(0, _orkestrel_contract.preview)(record)}`, {
1383
+ code: "EVIDENCE",
1384
+ context: { record }
1385
+ });
1386
+ const broken = run.passed.filter((name) => /[\r\n]/u.test(name));
1387
+ if (broken.length > 0) throw new CaptureError(`Artifact cannot carry case names with line breaks: ${broken.map((name) => (0, _orkestrel_contract.preview)(name)).join(", ")}`, {
1388
+ code: "EVIDENCE",
1389
+ context: { cases: broken }
1390
+ });
1391
+ return [`${CAPTURE_ARTIFACT_PREFIXES.record}${record}`, ...run.passed.map((name) => `${CAPTURE_ARTIFACT_PREFIXES.case}${name}`)];
1392
+ }
1393
+ /**
1394
+ * Reads the record bindings and the ran cases from a journey artifact's text.
1395
+ *
1396
+ * @param text - The artifact text, with LF or CRLF line endings
1397
+ * @returns The path of each `record=` line and the full name of each `case-ran=` line, in text order
1398
+ *
1399
+ * @example
1400
+ * ```ts
1401
+ * readCaptureArtifact('# light-390\r\nrecord=tmp/run.json\r\ncase-ran=mail opens the inbox\r\n')
1402
+ * // { records: ['tmp/run.json'], cases: ['mail opens the inbox'] }
1403
+ * ```
1404
+ */
1405
+ function readCaptureArtifact(text) {
1406
+ const lines = text.split(/\r\n|\n/u);
1407
+ return {
1408
+ records: lines.filter((line) => line.startsWith(CAPTURE_ARTIFACT_PREFIXES.record)).map((line) => line.slice(CAPTURE_ARTIFACT_PREFIXES.record.length)),
1409
+ cases: lines.filter((line) => line.startsWith(CAPTURE_ARTIFACT_PREFIXES.case)).map((line) => line.slice(CAPTURE_ARTIFACT_PREFIXES.case.length))
1410
+ };
1411
+ }
1412
+ exports.CAPTURE_ARTIFACT_PREFIXES = CAPTURE_ARTIFACT_PREFIXES;
989
1413
  exports.CAPTURE_ERROR_BRAND = CAPTURE_ERROR_BRAND;
990
1414
  exports.CAPTURE_ERROR_CODES = CAPTURE_ERROR_CODES;
991
1415
  exports.CAPTURE_LOOKUPS_FILE = CAPTURE_LOOKUPS_FILE;
992
1416
  exports.CAPTURE_MANIFEST_FILE = CAPTURE_MANIFEST_FILE;
1417
+ exports.CAPTURE_OWNER_PREFIX = CAPTURE_OWNER_PREFIX;
993
1418
  exports.CAPTURE_PNG_SIGNATURE = CAPTURE_PNG_SIGNATURE;
994
1419
  exports.CAPTURE_REGISTRY_FILE = CAPTURE_REGISTRY_FILE;
995
1420
  exports.CAPTURE_REQUEST_PREFIX = CAPTURE_REQUEST_PREFIX;
996
1421
  exports.CAPTURE_STATUS_PRECEDENCE = CAPTURE_STATUS_PRECEDENCE;
997
1422
  exports.CAPTURE_VERSION = CAPTURE_VERSION;
998
1423
  exports.CaptureError = CaptureError;
1424
+ exports.buildCaptureDestinations = buildCaptureDestinations;
1425
+ exports.buildCaptureRun = buildCaptureRun;
1426
+ exports.buildCaptureTags = buildCaptureTags;
1427
+ exports.buildCaptureTest = buildCaptureTest;
1428
+ exports.collectCaptureAncestors = collectCaptureAncestors;
1429
+ exports.collectCaptureCases = collectCaptureCases;
999
1430
  exports.collectCaptureChanges = collectCaptureChanges;
1000
1431
  exports.collectCaptureDuplicates = collectCaptureDuplicates;
1001
1432
  exports.describeCaptureDeclaration = describeCaptureDeclaration;
@@ -1009,8 +1440,10 @@ exports.isCaptureCollection = isCaptureCollection;
1009
1440
  exports.isCaptureDeclaration = isCaptureDeclaration;
1010
1441
  exports.isCaptureDestination = isCaptureDestination;
1011
1442
  exports.isCaptureDigest = isCaptureDigest;
1443
+ exports.isCaptureDigestReference = isCaptureDigestReference;
1012
1444
  exports.isCaptureEntry = isCaptureEntry;
1013
1445
  exports.isCaptureError = isCaptureError;
1446
+ exports.isCaptureExpandedManifest = isCaptureExpandedManifest;
1014
1447
  exports.isCaptureGeometry = isCaptureGeometry;
1015
1448
  exports.isCaptureHash = isCaptureHash;
1016
1449
  exports.isCaptureHashes = isCaptureHashes;
@@ -1018,6 +1451,7 @@ exports.isCaptureIdentity = isCaptureIdentity;
1018
1451
  exports.isCaptureLookup = isCaptureLookup;
1019
1452
  exports.isCaptureLookupJournal = isCaptureLookupJournal;
1020
1453
  exports.isCaptureManifest = isCaptureManifest;
1454
+ exports.isCaptureManifestDocument = isCaptureManifestDocument;
1021
1455
  exports.isCapturePath = isCapturePath;
1022
1456
  exports.isCapturePattern = isCapturePattern;
1023
1457
  exports.isCaptureReceipt = isCaptureReceipt;
@@ -1028,11 +1462,13 @@ exports.isCaptureSegment = isCaptureSegment;
1028
1462
  exports.isCaptureSelector = isCaptureSelector;
1029
1463
  exports.isCaptureSources = isCaptureSources;
1030
1464
  exports.isCaptureStamp = isCaptureStamp;
1031
- exports.isCaptureStoredManifest = isCaptureStoredManifest;
1032
1465
  exports.matchesCaptureMembers = matchesCaptureMembers;
1033
1466
  exports.matchesCaptureSelector = matchesCaptureSelector;
1467
+ exports.readCaptureArtifact = readCaptureArtifact;
1034
1468
  exports.readCaptureDirty = readCaptureDirty;
1035
1469
  exports.readCaptureReport = readCaptureReport;
1470
+ exports.renderCaptureArtifact = renderCaptureArtifact;
1471
+ exports.requireCaptureCases = requireCaptureCases;
1036
1472
  exports.requireCaptureUnique = requireCaptureUnique;
1037
1473
  exports.resolveCaptureKey = resolveCaptureKey;
1038
1474
  exports.resolveCaptureOwner = resolveCaptureOwner;