@orkestrel/capture 0.0.0-stage → 0.0.2
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.
- package/LICENSE +21 -0
- package/README.md +262 -2
- package/dist/bin/main.js +6 -0
- package/dist/bin/main.js.map +1 -0
- package/dist/src/core/index.cjs +1043 -0
- package/dist/src/core/index.cjs.map +1 -0
- package/dist/src/core/index.d.cts +1269 -0
- package/dist/src/core/index.d.ts +1269 -0
- package/dist/src/core/index.js +989 -0
- package/dist/src/core/index.js.map +1 -0
- package/dist/src/server/index.cjs +5209 -0
- package/dist/src/server/index.cjs.map +1 -0
- package/dist/src/server/index.d.cts +3953 -0
- package/dist/src/server/index.d.ts +3953 -0
- package/dist/src/server/index.js +5094 -0
- package/dist/src/server/index.js.map +1 -0
- package/package.json +138 -5
|
@@ -0,0 +1,989 @@
|
|
|
1
|
+
import { INTRINSICS, arrayOf, holds, isError, isFiniteNumber, isFunction, isObject, isRecord, isString } from "@orkestrel/contract";
|
|
2
|
+
// src/core/constants.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lists the condition axes a capture failure carries, in the order the executable reports them.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* `USAGE` is a refused command line and exits 64; every other condition exits 1. `SELECTION`
|
|
8
|
+
* names a selector, variant, case, state, or portfolio the host does not declare. `HOST` names a
|
|
9
|
+
* wrapper, configuration, or declaration fault. `STAGE` names a runner stage that did not
|
|
10
|
+
* complete. `EVIDENCE` names a record, report, registry, or manifest that fails its protocol.
|
|
11
|
+
* `CONTAINMENT` names a path that leaves the authorized roots.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* CAPTURE_ERROR_CODES.includes('USAGE') // true
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
var CAPTURE_ERROR_CODES = Object.freeze([
|
|
19
|
+
"USAGE",
|
|
20
|
+
"SELECTION",
|
|
21
|
+
"HOST",
|
|
22
|
+
"STAGE",
|
|
23
|
+
"EVIDENCE",
|
|
24
|
+
"CONTAINMENT"
|
|
25
|
+
]);
|
|
26
|
+
/**
|
|
27
|
+
* Keys the global brand a capture error carries, so a duplicate installation recognizes it.
|
|
28
|
+
*
|
|
29
|
+
* @remarks
|
|
30
|
+
* The key follows the package-and-class form, `Symbol.for('<package>.<ClassName>')`. The brand is
|
|
31
|
+
* a recognition marker and never proof that the class's constructor ran.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* CAPTURE_ERROR_BRAND === Symbol.for('@orkestrel/capture.CaptureError') // true
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
var CAPTURE_ERROR_BRAND = Symbol.for("@orkestrel/capture.CaptureError");
|
|
39
|
+
/**
|
|
40
|
+
* Lists the non-current freshness classifications from highest precedence to lowest.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* `resolveCaptureStatus` reads this order to choose a frame's primary classification while the
|
|
44
|
+
* reading keeps every finding separately. A frame with none of these is current.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* CAPTURE_STATUS_PRECEDENCE[0] // 'duplicate'
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
var CAPTURE_STATUS_PRECEDENCE = Object.freeze([
|
|
52
|
+
"duplicate",
|
|
53
|
+
"orphan",
|
|
54
|
+
"missing",
|
|
55
|
+
"unstamped",
|
|
56
|
+
"stale-shared",
|
|
57
|
+
"stale-own"
|
|
58
|
+
]);
|
|
59
|
+
/**
|
|
60
|
+
* Names the file prefix of a runner request, completed by the runner's process id and `.json`.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* `${CAPTURE_REQUEST_PREFIX}-${process.pid}.json` // 'journey-selection-4242.json'
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
var CAPTURE_REQUEST_PREFIX = "journey-selection";
|
|
68
|
+
/**
|
|
69
|
+
* Holds the protocol version every request, record, receipt, registry, and manifest declares.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* CAPTURE_VERSION // 1
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
var CAPTURE_VERSION = 1;
|
|
77
|
+
/**
|
|
78
|
+
* Holds the eight bytes every PNG file opens with.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* CAPTURE_PNG_SIGNATURE.map((byte) => byte.toString(16)).join('') // '89504e470d0a1a0a'
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
var CAPTURE_PNG_SIGNATURE = Object.freeze([
|
|
86
|
+
137,
|
|
87
|
+
80,
|
|
88
|
+
78,
|
|
89
|
+
71,
|
|
90
|
+
13,
|
|
91
|
+
10,
|
|
92
|
+
26,
|
|
93
|
+
10
|
|
94
|
+
]);
|
|
95
|
+
/**
|
|
96
|
+
* Names the default accepted complete-run registry file inside a portfolio directory.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* `tmp/captures/states/${CAPTURE_REGISTRY_FILE}` // 'tmp/captures/states/registry.json'
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
var CAPTURE_REGISTRY_FILE = "registry.json";
|
|
104
|
+
/**
|
|
105
|
+
* Names the default image-stamp manifest file inside a portfolio directory.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* `tmp/captures/states/${CAPTURE_MANIFEST_FILE}` // 'tmp/captures/states/manifest.json'
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
var CAPTURE_MANIFEST_FILE = "manifest.json";
|
|
113
|
+
/**
|
|
114
|
+
* Names the default lookup journal file inside a portfolio directory.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```ts
|
|
118
|
+
* `tmp/captures/states/${CAPTURE_LOOKUPS_FILE}` // 'tmp/captures/states/lookups.json'
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
var CAPTURE_LOOKUPS_FILE = "lookups.json";
|
|
122
|
+
// src/core/errors.ts
|
|
123
|
+
/**
|
|
124
|
+
* Reports one capture failure under a stable condition axis.
|
|
125
|
+
*
|
|
126
|
+
* @remarks
|
|
127
|
+
* Every refusal the engine, the runner, and the readers raise arrives as one of these, so the
|
|
128
|
+
* executable branches on `code` rather than on message text: `USAGE` exits 64 and every other
|
|
129
|
+
* condition exits 1. Narrow a caught value with `isCaptureError` before reading `code`.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* const error = new CaptureError('Unknown variant: phone', {
|
|
134
|
+
* code: 'SELECTION',
|
|
135
|
+
* context: { variant: 'phone' },
|
|
136
|
+
* })
|
|
137
|
+
* error.code // 'SELECTION'
|
|
138
|
+
* error.context?.variant // 'phone'
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
var CaptureError = class extends Error {
|
|
142
|
+
name = "CaptureError";
|
|
143
|
+
code;
|
|
144
|
+
context;
|
|
145
|
+
/**
|
|
146
|
+
* Creates one classified capture failure.
|
|
147
|
+
*
|
|
148
|
+
* @param message - Human-readable description of what failed
|
|
149
|
+
* @param options - The condition, optional structured context, and optional cause
|
|
150
|
+
*/
|
|
151
|
+
constructor(message, options) {
|
|
152
|
+
super(message, "cause" in options ? { cause: options.cause } : void 0);
|
|
153
|
+
Object.defineProperty(this, CAPTURE_ERROR_BRAND, { value: this });
|
|
154
|
+
this.code = options.code;
|
|
155
|
+
this.context = options.context;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Checks whether an unknown value is a capture error.
|
|
160
|
+
*
|
|
161
|
+
* @remarks
|
|
162
|
+
* Recognition requires a native `Error` whose prototype is not `Error.prototype`, an own brand
|
|
163
|
+
* property holding the tested value itself, a declared condition code, and an object, callable, or absent
|
|
164
|
+
* context. It recognizes an error from another installed copy of this package, and refuses a
|
|
165
|
+
* look-alike with the same code and no brand, a proxy over a branded error, and a plain object
|
|
166
|
+
* that only carries the brand.
|
|
167
|
+
*
|
|
168
|
+
* @param value - The value to inspect
|
|
169
|
+
* @returns True if the value is a `CaptureError`; false otherwise
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* isCaptureError(new CaptureError('Empty selection', { code: 'USAGE' })) // true
|
|
174
|
+
* isCaptureError(new Error('Empty selection')) // false
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
function isCaptureError(value) {
|
|
178
|
+
return holds(() => {
|
|
179
|
+
if (!isError(value) || INTRINSICS.prototype(value) === INTRINSICS.error.prototype) return false;
|
|
180
|
+
if (INTRINSICS.describe(value, CAPTURE_ERROR_BRAND)?.value !== value) return false;
|
|
181
|
+
const code = INTRINSICS.reflect.read(value, "code");
|
|
182
|
+
const context = INTRINSICS.reflect.read(value, "context");
|
|
183
|
+
return CAPTURE_ERROR_CODES.some((declared) => declared === code) && (context === void 0 || isObject(context) || isFunction(context));
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
// src/core/validators.ts
|
|
187
|
+
/**
|
|
188
|
+
* Checks whether a serialized path stays within the portable relative-path vocabulary.
|
|
189
|
+
*
|
|
190
|
+
* @param value - The serialized file or directory path
|
|
191
|
+
* @returns True if every segment is portable and relative; false otherwise
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* isCapturePath('tmp/captures/states/pages/mail--inbox.png') // true
|
|
196
|
+
* isCapturePath('../escape.png') // false
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
function isCapturePath(value) {
|
|
200
|
+
return typeof value === "string" && value.length > 0 && [...value].every((character) => character.charCodeAt(0) > 31) && value.split("/").every((segment) => segment !== "" && segment !== "." && segment !== ".." && !/[<>:"\\|?*]/u.test(segment) && !/[. ]$/u.test(segment) && !/^(?:con|prn|aux|nul|com[1-9¹²³]|lpt[1-9¹²³])(?:\.|$)/iu.test(segment));
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Checks whether a native glob declaration uses relative portable path segments.
|
|
204
|
+
*
|
|
205
|
+
* @param value - A source attribution pattern
|
|
206
|
+
* @returns True if the pattern has no path traversal or host-specific spelling; false otherwise
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* isCapturePattern('src/**') // true
|
|
211
|
+
* isCapturePattern('src\\**') // false
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
function isCapturePattern(value) {
|
|
215
|
+
return typeof value === "string" && value.length > 0 && [...value].every((character) => character.charCodeAt(0) > 31) && value.split("/").every((segment) => segment !== "" && segment !== "." && segment !== ".." && !/[<>:"\\|]/u.test(segment) && !/[. ]$/u.test(segment) && !/^(?:con|prn|aux|nul|com[1-9¹²³]|lpt[1-9¹²³])(?:\.|$)/iu.test(segment));
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Checks whether a protocol identity can occupy one portable path segment.
|
|
219
|
+
*
|
|
220
|
+
* @param value - The identity segment
|
|
221
|
+
* @returns True if the segment is safe and nonempty; false otherwise
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* isCaptureSegment('light-390') // true
|
|
226
|
+
* isCaptureSegment('light 390') // false
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
function isCaptureSegment(value) {
|
|
230
|
+
return isCapturePath(value) && /^[a-zA-Z0-9][a-zA-Z0-9_-]*$/u.test(value);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Checks whether an owner selector uses canonical colon-separated segments.
|
|
234
|
+
*
|
|
235
|
+
* @param value - The owner selector
|
|
236
|
+
* @returns True if every owner segment is canonical; false otherwise
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* isCaptureSelector('pages:mail') // true
|
|
241
|
+
* isCaptureSelector('pages::mail') // false
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
function isCaptureSelector(value) {
|
|
245
|
+
return typeof value === "string" && value.split(":").every(isCaptureSegment);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Checks whether a value is a SHA-256 hexadecimal digest.
|
|
249
|
+
*
|
|
250
|
+
* @param value - The digest
|
|
251
|
+
* @returns True if the digest has the expected hexadecimal width; false otherwise
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* ```ts
|
|
255
|
+
* isCaptureHash('a'.repeat(64)) // true
|
|
256
|
+
* isCaptureHash('abc') // false
|
|
257
|
+
* ```
|
|
258
|
+
*/
|
|
259
|
+
function isCaptureHash(value) {
|
|
260
|
+
return typeof value === "string" && /^[a-f0-9]{64}$/u.test(value);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Checks whether a file-hash map has portable keys and valid digests.
|
|
264
|
+
*
|
|
265
|
+
* @param value - The hash map
|
|
266
|
+
* @returns True if every key and digest is valid and no two keys differ only by case; false otherwise
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```ts
|
|
270
|
+
* isCaptureHashes({ 'src/index.ts': 'a'.repeat(64) }) // true
|
|
271
|
+
* isCaptureHashes({ 'src/index.ts': 'x' }) // false
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
function isCaptureHashes(value) {
|
|
275
|
+
return holds(() => isRecord(value) && Object.entries(value).every(([file, digest]) => isCapturePath(file) && isCaptureHash(digest)) && new Set(Object.keys(value).map((file) => file.toLowerCase())).size === Object.keys(value).length);
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Checks whether source attribution separates own, shared, and behavioral patterns.
|
|
279
|
+
*
|
|
280
|
+
* @param value - The attribution
|
|
281
|
+
* @returns True if every declared pattern is valid; false otherwise
|
|
282
|
+
*
|
|
283
|
+
* @example
|
|
284
|
+
* ```ts
|
|
285
|
+
* isCaptureSources({ own: ['src/mail/**'], shared: [], tests: [] }) // true
|
|
286
|
+
* isCaptureSources({ own: ['../x'], shared: [], tests: [] }) // false
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
function isCaptureSources(value) {
|
|
290
|
+
return holds(() => isRecord(value) && arrayOf(isCapturePattern)(value.own) && arrayOf(isCapturePattern)(value.shared) && arrayOf(isCapturePattern)(value.tests));
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Checks whether an execution identity carries its required authorization fields.
|
|
294
|
+
*
|
|
295
|
+
* @param value - The identity
|
|
296
|
+
* @returns True if the identity is valid; false otherwise
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* isCaptureIdentity({ id: 'run-1', stage: 'selected', pid: 4242, config: 'configs/journey.ts' }) // true
|
|
301
|
+
* isCaptureIdentity({ id: 'run-1', stage: 'selected', pid: 0, config: 'configs/journey.ts' }) // false
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
function isCaptureIdentity(value) {
|
|
305
|
+
return holds(() => isRecord(value) && isCaptureSegment(value.id) && isCaptureSegment(value.stage) && typeof value.pid === "number" && Number.isSafeInteger(value.pid) && value.pid > 0 && isCapturePath(value.config));
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Checks whether a request has the supported version and complete execution identity.
|
|
309
|
+
*
|
|
310
|
+
* @param value - The transported request
|
|
311
|
+
* @returns True if every request field has its protocol shape; false otherwise
|
|
312
|
+
*
|
|
313
|
+
* @example
|
|
314
|
+
* ```ts
|
|
315
|
+
* isCaptureRequest(JSON.parse(text)) // true for a well-formed runner request
|
|
316
|
+
* isCaptureRequest({ version: 2 }) // false
|
|
317
|
+
* ```
|
|
318
|
+
*/
|
|
319
|
+
function isCaptureRequest(value) {
|
|
320
|
+
return holds(() => isRecord(value) && isCaptureIdentity(value) && value.version === 1 && (value.command === "journey" || value.command === "capture") && isCaptureSelector(value.selector) && arrayOf(isCaptureSegment)(value.variants) && value.variants.length > 0 && arrayOf(isString)(value.args) && (value.case === void 0 || typeof value.case === "string" && value.case.trim() !== "") && (value.state === void 0 || value.command === "capture" && isCaptureSegment(value.state)));
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Checks whether a collected declaration preserves its full owner and state population.
|
|
324
|
+
*
|
|
325
|
+
* @param value - The declaration
|
|
326
|
+
* @returns True if the declaration has its supported shape; false otherwise
|
|
327
|
+
*
|
|
328
|
+
* @example
|
|
329
|
+
* ```ts
|
|
330
|
+
* isCaptureDeclaration({
|
|
331
|
+
* case: 'mail shows the inbox',
|
|
332
|
+
* owner: 'pages:mail',
|
|
333
|
+
* variant: 'light-390',
|
|
334
|
+
* eligible: true,
|
|
335
|
+
* states: ['inbox'],
|
|
336
|
+
* }) // true
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
339
|
+
function isCaptureDeclaration(value) {
|
|
340
|
+
return holds(() => isRecord(value) && typeof value.case === "string" && value.case.trim() !== "" && isCaptureSelector(value.owner) && isCaptureSegment(value.variant) && typeof value.eligible === "boolean" && arrayOf(isCaptureSegment)(value.states));
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Checks whether a destination supplies canonical identity and source attribution fields.
|
|
344
|
+
*
|
|
345
|
+
* @param value - The destination
|
|
346
|
+
* @returns True if the destination has its supported shape; false otherwise
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```ts
|
|
350
|
+
* isCaptureDestination(JSON.parse(text)) // true for a registry destination row
|
|
351
|
+
* isCaptureDestination({}) // false
|
|
352
|
+
* ```
|
|
353
|
+
*/
|
|
354
|
+
function isCaptureDestination(value) {
|
|
355
|
+
return holds(() => isRecord(value) && isCaptureSegment(value.portfolio) && isCaptureSegment(value.state) && isCaptureSegment(value.scope) && isCaptureSegment(value.part) && (value.section === void 0 || isCaptureSegment(value.section)) && isCaptureSegment(value.variant) && typeof value.project === "string" && value.project !== "" && isCapturePath(value.file) && typeof value.case === "string" && value.case.trim() !== "" && isCaptureSources(value.sources));
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Checks whether an emitted entry includes a finite nonnegative case duration.
|
|
359
|
+
*
|
|
360
|
+
* @param value - The emitted entry
|
|
361
|
+
* @returns True if the entry is valid; false otherwise
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```ts
|
|
365
|
+
* isCaptureEntry({ ...destination, duration: 812 }) // true
|
|
366
|
+
* isCaptureEntry({ ...destination, duration: -1 }) // false
|
|
367
|
+
* ```
|
|
368
|
+
*/
|
|
369
|
+
function isCaptureEntry(value) {
|
|
370
|
+
return holds(() => isRecord(value) && isCaptureDestination(value) && isFiniteNumber(value.duration) && value.duration >= 0);
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Checks whether a record names its journey artifact by portable path and SHA-256 digest.
|
|
374
|
+
*
|
|
375
|
+
* @param value - The decoded artifact reference
|
|
376
|
+
* @returns True if the reference has its supported shape; false otherwise
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```ts
|
|
380
|
+
* isCaptureArtifact({ file: 'tmp/runs/a/light-390.txt', digest: 'a'.repeat(64) }) // true
|
|
381
|
+
* isCaptureArtifact({ file: '/abs.txt', digest: 'a'.repeat(64) }) // false
|
|
382
|
+
* ```
|
|
383
|
+
*/
|
|
384
|
+
function isCaptureArtifact(value) {
|
|
385
|
+
return holds(() => isRecord(value) && isCapturePath(value.file) && isCaptureHash(value.digest));
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Checks whether a final record preserves declarations, observations, and output separately.
|
|
389
|
+
*
|
|
390
|
+
* @param value - The decoded run record
|
|
391
|
+
* @returns True if the record has the supported protocol shape; false otherwise
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```ts
|
|
395
|
+
* isCaptureRun(JSON.parse(text)) // true for a final run record
|
|
396
|
+
* isCaptureRun(null) // false
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
function isCaptureRun(value) {
|
|
400
|
+
return holds(() => 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" && arrayOf(isCaptureDeclaration)(value.declarations) && arrayOf(isCaptureDestination)(value.registry) && arrayOf(isString)(value.passed) && arrayOf(isCaptureSegment)(value.observed) && 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)));
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Checks whether a shared-source digest retains both patterns and expanded file hashes.
|
|
404
|
+
*
|
|
405
|
+
* @param value - The digest reading
|
|
406
|
+
* @returns True if the digest reading has its supported shape; false otherwise
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```ts
|
|
410
|
+
* isCaptureDigest({ patterns: ['src/**'], files: {}, digest: 'a'.repeat(64) }) // true
|
|
411
|
+
* isCaptureDigest({ patterns: [], files: {} }) // false
|
|
412
|
+
* ```
|
|
413
|
+
*/
|
|
414
|
+
function isCaptureDigest(value) {
|
|
415
|
+
return holds(() => isRecord(value) && arrayOf(isCapturePattern)(value.patterns) && isCaptureHashes(value.files) && isCaptureHash(value.digest));
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Checks whether a complete-run receipt binds the report, inputs, and declaration registry.
|
|
419
|
+
*
|
|
420
|
+
* @param value - The receipt
|
|
421
|
+
* @returns True if the receipt has its supported shape; false otherwise
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* isCaptureReceipt(JSON.parse(text)) // true for a complete-run receipt
|
|
426
|
+
* isCaptureReceipt({}) // false
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
function isCaptureReceipt(value) {
|
|
430
|
+
return holds(() => isRecord(value) && value.version === 1 && isCaptureSegment(value.variant) && typeof value.time === "string" && Number.isFinite(Date.parse(value.time)) && isRecord(value.report) && isCapturePath(value.report.file) && isCaptureHash(value.report.digest) && isCaptureHashes(value.inputs) && arrayOf(isCaptureDeclaration)(value.declarations) && arrayOf(isCaptureDestination)(value.registry) && typeof value.complete === "boolean");
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Checks whether a registry document retains its receipts and required output pairs.
|
|
434
|
+
*
|
|
435
|
+
* @param value - The registry document
|
|
436
|
+
* @returns True if the registry has the supported shape; false otherwise
|
|
437
|
+
*
|
|
438
|
+
* @example
|
|
439
|
+
* ```ts
|
|
440
|
+
* isCaptureRegistry(JSON.parse(text)) // true for a published registry.json
|
|
441
|
+
* isCaptureRegistry([]) // false
|
|
442
|
+
* ```
|
|
443
|
+
*/
|
|
444
|
+
function isCaptureRegistry(value) {
|
|
445
|
+
return holds(() => isRecord(value) && value.version === 1 && arrayOf(isCaptureReceipt)(value.receipts) && arrayOf(isCaptureDestination)(value.entries) && isCaptureHash(value.digest));
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Checks whether a lookup binds a passing case to its full run record and input hashes.
|
|
449
|
+
*
|
|
450
|
+
* @param value - The lookup receipt
|
|
451
|
+
* @returns True if the receipt has its supported shape; false otherwise
|
|
452
|
+
*
|
|
453
|
+
* @example
|
|
454
|
+
* ```ts
|
|
455
|
+
* isCaptureLookup(JSON.parse(text)) // true for a lookup row
|
|
456
|
+
* isCaptureLookup(undefined) // false
|
|
457
|
+
* ```
|
|
458
|
+
*/
|
|
459
|
+
function isCaptureLookup(value) {
|
|
460
|
+
return holds(() => isRecord(value) && isCaptureHash(value.registry) && isCaptureSegment(value.variant) && typeof value.case === "string" && value.case.trim() !== "" && isCaptureSelector(value.owner) && arrayOf(isCaptureSegment)(value.states) && isCaptureHashes(value.inputs) && isRecord(value.run) && isCapturePath(value.run.file) && isCaptureHash(value.run.digest));
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Checks the separate lookup journal and its optional predecessor manifest digest.
|
|
464
|
+
*
|
|
465
|
+
* @param value - The decoded journal
|
|
466
|
+
* @returns True if the journal has its supported shape; false otherwise
|
|
467
|
+
* @remarks Input-map digests and reference existence are verified when the journal is read.
|
|
468
|
+
*
|
|
469
|
+
* @example
|
|
470
|
+
* ```ts
|
|
471
|
+
* isCaptureLookupJournal({ version: 1, inputs: {}, lookups: [] }) // true
|
|
472
|
+
* isCaptureLookupJournal({ version: 2, inputs: {}, lookups: [] }) // false
|
|
473
|
+
* ```
|
|
474
|
+
*/
|
|
475
|
+
function isCaptureLookupJournal(value) {
|
|
476
|
+
return holds(() => isRecord(value) && (value.collections === void 0 || arrayOf(isCaptureCollection)(value.collections)) && value.version === 1 && (value.manifest === void 0 || isCaptureHash(value.manifest)) && isRecord(value.inputs) && Object.entries(value.inputs).every(([digest, inputs]) => isCaptureHash(digest) && isCaptureHashes(inputs)) && arrayOf((entry) => isRecord(entry) && isCaptureHash(entry.inputs) && isCaptureLookup({
|
|
477
|
+
...entry,
|
|
478
|
+
inputs: {}
|
|
479
|
+
}))(value.lookups));
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Checks whether a stamp binds an emitted image to source and execution provenance.
|
|
483
|
+
*
|
|
484
|
+
* @param value - The stamp
|
|
485
|
+
* @returns True if the stamp has its supported shape; false otherwise
|
|
486
|
+
*
|
|
487
|
+
* @example
|
|
488
|
+
* ```ts
|
|
489
|
+
* isCaptureStamp(manifest.entries['pages/mail--inbox.png']) // true for a stamped frame
|
|
490
|
+
* isCaptureStamp({}) // false
|
|
491
|
+
* ```
|
|
492
|
+
*/
|
|
493
|
+
function isCaptureStamp(value) {
|
|
494
|
+
return holds(() => 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) && arrayOf(isCapturePath)(value.dirty) && isCaptureHashes(value.own) && isCaptureDigest(value.shared) && isCaptureHashes(value.tests) && isCaptureHash(value.registry) && isRecord(value.image) && isCaptureHash(value.image.digest) && isFiniteNumber(value.image.size) && Number.isSafeInteger(value.image.size) && value.image.size > 0 && isFiniteNumber(value.image.modified) && value.image.modified >= 0);
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Checks whether an inherited manifest has the supported schema without discarding malformed rows.
|
|
498
|
+
*
|
|
499
|
+
* @param value - The manifest
|
|
500
|
+
* @returns True if every inherited member validates; false otherwise
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
* ```ts
|
|
504
|
+
* isCaptureManifest(JSON.parse(text)) // true for a published manifest.json
|
|
505
|
+
* isCaptureManifest('manifest') // false
|
|
506
|
+
* ```
|
|
507
|
+
*/
|
|
508
|
+
function isCaptureManifest(value) {
|
|
509
|
+
return holds(() => isRecord(value) && value.version === 1 && isCaptureRegistry(value.registry) && isRecord(value.entries) && Object.entries(value.entries).every(([key, stamp]) => isCapturePath(key) && isCaptureStamp(stamp)) && arrayOf(isCaptureLookup)(value.lookups));
|
|
510
|
+
}
|
|
511
|
+
/**
|
|
512
|
+
* Checks whether collection evidence pins complete input membership and both external artifacts.
|
|
513
|
+
*
|
|
514
|
+
* @param value - The collection receipt
|
|
515
|
+
* @returns True if every field has the supported shape; false otherwise
|
|
516
|
+
*
|
|
517
|
+
* @example
|
|
518
|
+
* ```ts
|
|
519
|
+
* isCaptureCollection(manifest.collections?.[0]) // true for a collection receipt
|
|
520
|
+
* isCaptureCollection({}) // false
|
|
521
|
+
* ```
|
|
522
|
+
*/
|
|
523
|
+
function isCaptureCollection(value) {
|
|
524
|
+
return holds(() => isRecord(value) && isCaptureHash(value.registry) && isCaptureSegment(value.variant) && typeof value.project === "string" && value.project.trim() !== "" && isCaptureDigest(value.inputs) && isRecord(value.run) && isCapturePath(value.run.file) && isCaptureHash(value.run.digest) && isRecord(value.report) && isCapturePath(value.report.file) && isCaptureHash(value.report.digest));
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Checks optional collection metadata without upgrading historical image evidence.
|
|
528
|
+
*
|
|
529
|
+
* @param value - The inherited image transaction
|
|
530
|
+
* @returns True if its existing manifest and any collection receipts are valid; false otherwise
|
|
531
|
+
*
|
|
532
|
+
* @example
|
|
533
|
+
* ```ts
|
|
534
|
+
* isCaptureStoredManifest(JSON.parse(text)) // true for a manifest with or without collections
|
|
535
|
+
* isCaptureStoredManifest(null) // false
|
|
536
|
+
* ```
|
|
537
|
+
*/
|
|
538
|
+
function isCaptureStoredManifest(value) {
|
|
539
|
+
return holds(() => isCaptureManifest(value) && isRecord(value) && (value.collections === void 0 || arrayOf(isCaptureCollection)(value.collections)));
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Checks the host's declared viewport without deriving geometry from an output name.
|
|
543
|
+
*
|
|
544
|
+
* @param value - The published viewport data
|
|
545
|
+
* @returns True if the named viewport has usable dimensions; false otherwise
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* ```ts
|
|
549
|
+
* isCaptureGeometry({ name: 'light-390', width: 390, height: 844 }) // true
|
|
550
|
+
* isCaptureGeometry({ name: 'light-390', width: 0, height: 844 }) // false
|
|
551
|
+
* ```
|
|
552
|
+
*/
|
|
553
|
+
function isCaptureGeometry(value) {
|
|
554
|
+
return holds(() => isObject(value) && "name" in value && isCaptureSegment(value.name) && "width" in value && isFiniteNumber(value.width) && value.width > 0 && "height" in value && isFiniteNumber(value.height) && value.height > 0);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Checks a journey artifact's recorded sampling box.
|
|
558
|
+
*
|
|
559
|
+
* @param value - The recorded viewport box
|
|
560
|
+
* @returns True if its coordinates and dimensions are finite; false otherwise
|
|
561
|
+
*
|
|
562
|
+
* @example
|
|
563
|
+
* ```ts
|
|
564
|
+
* isCaptureBox({ x: 0, y: 64, width: 390, height: 780 }) // true
|
|
565
|
+
* isCaptureBox({ x: 0, y: 64, width: 0, height: 780 }) // false
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
function isCaptureBox(value) {
|
|
569
|
+
return holds(() => isRecord(value) && isFiniteNumber(value.x) && isFiniteNumber(value.y) && isFiniteNumber(value.width) && value.width > 0 && isFiniteNumber(value.height) && value.height > 0);
|
|
570
|
+
}
|
|
571
|
+
// src/core/helpers.ts
|
|
572
|
+
/**
|
|
573
|
+
* Describes a caught failure as one reportable line.
|
|
574
|
+
*
|
|
575
|
+
* @param error - The caught value, which need not be an `Error`
|
|
576
|
+
* @returns The error's message, or the value's string form
|
|
577
|
+
*
|
|
578
|
+
* @example
|
|
579
|
+
* ```ts
|
|
580
|
+
* describeCaptureError(new Error('Unknown variant: phone')) // 'Unknown variant: phone'
|
|
581
|
+
* describeCaptureError('refused') // 'refused'
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
function describeCaptureError(error) {
|
|
585
|
+
return isError(error) ? error.message : String(error);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Finds repeated identities before a keyed collection could discard them.
|
|
589
|
+
*
|
|
590
|
+
* @param values - The identity population
|
|
591
|
+
* @param folded - If `true`, compares case-folded keys; if `false`, compares exact identities. Default: `false`
|
|
592
|
+
* @returns Every member whose comparison key appears more than once
|
|
593
|
+
*
|
|
594
|
+
* @example
|
|
595
|
+
* ```ts
|
|
596
|
+
* collectCaptureDuplicates(['a', 'b', 'a']) // ['a', 'a']
|
|
597
|
+
* collectCaptureDuplicates(['Pages', 'pages'], true) // ['Pages', 'pages']
|
|
598
|
+
* ```
|
|
599
|
+
*/
|
|
600
|
+
function collectCaptureDuplicates(values, folded = false) {
|
|
601
|
+
const keys = values.map((value) => folded ? value.toLowerCase() : value);
|
|
602
|
+
return values.filter((_value, index) => keys.indexOf(keys[index] ?? "") !== keys.lastIndexOf(keys[index] ?? ""));
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Refuses duplicate declarations or portable storage-key collisions before indexing.
|
|
606
|
+
*
|
|
607
|
+
* @param values - The complete identity population
|
|
608
|
+
* @param label - The diagnostic subject
|
|
609
|
+
* @param folded - If `true`, refuses case-colliding keys; if `false`, requires exact uniqueness. Default: `false`
|
|
610
|
+
* @throws Thrown as an `EVIDENCE` capture error when an identity repeats.
|
|
611
|
+
*
|
|
612
|
+
* @example
|
|
613
|
+
* ```ts
|
|
614
|
+
* requireCaptureUnique(['mail', 'files'], 'owners') // returns
|
|
615
|
+
* requireCaptureUnique(['mail', 'mail'], 'owners') // throws 'Duplicate owners: mail, mail'
|
|
616
|
+
* ```
|
|
617
|
+
*/
|
|
618
|
+
function requireCaptureUnique(values, label, folded = false) {
|
|
619
|
+
const duplicates = collectCaptureDuplicates(values, folded);
|
|
620
|
+
if (duplicates.length > 0) throw new CaptureError(`Duplicate ${label}: ${duplicates.join(", ")}`, {
|
|
621
|
+
code: "EVIDENCE",
|
|
622
|
+
context: {
|
|
623
|
+
label,
|
|
624
|
+
duplicates
|
|
625
|
+
}
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Compares complete populations without erasing duplicates.
|
|
630
|
+
*
|
|
631
|
+
* @param left - The first population
|
|
632
|
+
* @param right - The second population
|
|
633
|
+
* @returns True if the sorted populations are equal; false otherwise
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```ts
|
|
637
|
+
* matchesCaptureMembers(['b', 'a'], ['a', 'b']) // true
|
|
638
|
+
* matchesCaptureMembers(['a', 'a'], ['a']) // false
|
|
639
|
+
* ```
|
|
640
|
+
*/
|
|
641
|
+
function matchesCaptureMembers(left, right) {
|
|
642
|
+
const ordered = right.toSorted();
|
|
643
|
+
return left.length === right.length && left.toSorted().every((entry, index) => entry === ordered[index]);
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* Compares an owner with an explicit selector at colon boundaries.
|
|
647
|
+
*
|
|
648
|
+
* @param owner - The canonical owner
|
|
649
|
+
* @param selector - The requested owner or the `all` selector
|
|
650
|
+
* @returns True if the owner belongs to the selection; false otherwise
|
|
651
|
+
*
|
|
652
|
+
* @example
|
|
653
|
+
* ```ts
|
|
654
|
+
* matchesCaptureSelector('pages:mail', 'pages') // true
|
|
655
|
+
* matchesCaptureSelector('pages:mailbox', 'pages:mail') // false
|
|
656
|
+
* ```
|
|
657
|
+
*/
|
|
658
|
+
function matchesCaptureSelector(owner, selector) {
|
|
659
|
+
return selector === "all" || owner === selector || owner.startsWith(`${selector}:`);
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Builds the canonical owner from a destination's registry identity.
|
|
663
|
+
*
|
|
664
|
+
* @param entry - The destination
|
|
665
|
+
* @returns The scope, part, and optional section selector
|
|
666
|
+
*
|
|
667
|
+
* @example
|
|
668
|
+
* ```ts
|
|
669
|
+
* resolveCaptureOwner({ ...destination, scope: 'pages', part: 'mail' }) // 'pages:mail'
|
|
670
|
+
* ```
|
|
671
|
+
*/
|
|
672
|
+
function resolveCaptureOwner(entry) {
|
|
673
|
+
return [
|
|
674
|
+
entry.scope,
|
|
675
|
+
entry.part,
|
|
676
|
+
...entry.section === void 0 ? [] : [entry.section]
|
|
677
|
+
].join(":");
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Builds a manifest identity independently of the physical image path.
|
|
681
|
+
*
|
|
682
|
+
* @param entry - The registry destination
|
|
683
|
+
* @returns The scope-qualified state and output-variant key
|
|
684
|
+
*
|
|
685
|
+
* @example
|
|
686
|
+
* ```ts
|
|
687
|
+
* resolveCaptureKey({ ...destination, scope: 'pages', state: 'inbox', variant: 'light-390' })
|
|
688
|
+
* // 'pages/inbox--light-390.png'
|
|
689
|
+
* ```
|
|
690
|
+
*/
|
|
691
|
+
function resolveCaptureKey(entry) {
|
|
692
|
+
return `${entry.scope}/${entry.state}--${entry.variant}.png`;
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Serializes a destination's identity and attribution in a deterministic field order.
|
|
696
|
+
*
|
|
697
|
+
* @param entry - The destination
|
|
698
|
+
* @returns The comparison representation, excluding image and duration readings
|
|
699
|
+
*
|
|
700
|
+
* @example
|
|
701
|
+
* ```ts
|
|
702
|
+
* describeCaptureDestination(destination) === describeCaptureDestination({ ...destination })
|
|
703
|
+
* // true
|
|
704
|
+
* ```
|
|
705
|
+
*/
|
|
706
|
+
function describeCaptureDestination(entry) {
|
|
707
|
+
return JSON.stringify([
|
|
708
|
+
entry.portfolio,
|
|
709
|
+
entry.state,
|
|
710
|
+
entry.scope,
|
|
711
|
+
entry.part,
|
|
712
|
+
entry.section,
|
|
713
|
+
entry.variant,
|
|
714
|
+
entry.project,
|
|
715
|
+
entry.file,
|
|
716
|
+
entry.case,
|
|
717
|
+
entry.sources.own.toSorted(),
|
|
718
|
+
entry.sources.shared.toSorted(),
|
|
719
|
+
entry.sources.tests.toSorted()
|
|
720
|
+
]);
|
|
721
|
+
}
|
|
722
|
+
/**
|
|
723
|
+
* Serializes only the registry's execution and output identity, independent of input revisions.
|
|
724
|
+
*
|
|
725
|
+
* @param entry - The destination or inherited stamp
|
|
726
|
+
* @returns The identity used for relevant manifest agreement during state resolution
|
|
727
|
+
*
|
|
728
|
+
* @example
|
|
729
|
+
* ```ts
|
|
730
|
+
* describeCaptureIdentity(destination) === describeCaptureIdentity({
|
|
731
|
+
* ...destination,
|
|
732
|
+
* sources: { own: [], shared: [], tests: [] },
|
|
733
|
+
* })
|
|
734
|
+
* // true
|
|
735
|
+
* ```
|
|
736
|
+
*/
|
|
737
|
+
function describeCaptureIdentity(entry) {
|
|
738
|
+
return JSON.stringify([
|
|
739
|
+
entry.portfolio,
|
|
740
|
+
entry.state,
|
|
741
|
+
entry.scope,
|
|
742
|
+
entry.part,
|
|
743
|
+
entry.section,
|
|
744
|
+
entry.variant,
|
|
745
|
+
entry.project,
|
|
746
|
+
entry.file,
|
|
747
|
+
entry.case
|
|
748
|
+
]);
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* Serializes a complete behavioral declaration without dropping eligibility or states.
|
|
752
|
+
*
|
|
753
|
+
* @param entry - The collected declaration
|
|
754
|
+
* @returns The deterministic comparison representation
|
|
755
|
+
*
|
|
756
|
+
* @example
|
|
757
|
+
* ```ts
|
|
758
|
+
* describeCaptureDeclaration({ ...declaration, states: ['b', 'a'] }) ===
|
|
759
|
+
* describeCaptureDeclaration({ ...declaration, states: ['a', 'b'] })
|
|
760
|
+
* // true
|
|
761
|
+
* ```
|
|
762
|
+
*/
|
|
763
|
+
function describeCaptureDeclaration(entry) {
|
|
764
|
+
return JSON.stringify([
|
|
765
|
+
entry.case,
|
|
766
|
+
entry.owner,
|
|
767
|
+
entry.variant,
|
|
768
|
+
entry.eligible,
|
|
769
|
+
entry.states.toSorted()
|
|
770
|
+
]);
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Serializes every request field, retaining the exact argv order.
|
|
774
|
+
*
|
|
775
|
+
* @param request - The transported request
|
|
776
|
+
* @returns The exact request comparison representation
|
|
777
|
+
*
|
|
778
|
+
* @example
|
|
779
|
+
* ```ts
|
|
780
|
+
* describeCaptureRequest(request) === describeCaptureRequest({ ...request, args: [] })
|
|
781
|
+
* // false when the request carried argv
|
|
782
|
+
* ```
|
|
783
|
+
*/
|
|
784
|
+
function describeCaptureRequest(request) {
|
|
785
|
+
return JSON.stringify([
|
|
786
|
+
request.version,
|
|
787
|
+
request.id,
|
|
788
|
+
request.stage,
|
|
789
|
+
request.pid,
|
|
790
|
+
request.config,
|
|
791
|
+
request.command,
|
|
792
|
+
request.selector,
|
|
793
|
+
request.case,
|
|
794
|
+
request.state,
|
|
795
|
+
request.variants,
|
|
796
|
+
request.args
|
|
797
|
+
]);
|
|
798
|
+
}
|
|
799
|
+
/**
|
|
800
|
+
* Names every changed, added, or removed file in hash populations.
|
|
801
|
+
*
|
|
802
|
+
* @param previous - The retained file hashes
|
|
803
|
+
* @param current - The observed file hashes
|
|
804
|
+
* @returns The sorted changed membership
|
|
805
|
+
*
|
|
806
|
+
* @example
|
|
807
|
+
* ```ts
|
|
808
|
+
* collectCaptureChanges({ 'a.ts': 'x', 'b.ts': 'y' }, { 'a.ts': 'x', 'b.ts': 'z', 'c.ts': 'w' })
|
|
809
|
+
* // ['b.ts', 'c.ts']
|
|
810
|
+
* ```
|
|
811
|
+
*/
|
|
812
|
+
function collectCaptureChanges(previous, current) {
|
|
813
|
+
return [.../* @__PURE__ */ new Set([...Object.keys(previous), ...Object.keys(current)])].filter((file) => previous[file] !== current[file]).toSorted();
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Builds the stable case identity used to merge separate lookup receipts.
|
|
817
|
+
*
|
|
818
|
+
* @param lookup - The lookup identity
|
|
819
|
+
* @returns The producing variant and full case key
|
|
820
|
+
*
|
|
821
|
+
* @example
|
|
822
|
+
* ```ts
|
|
823
|
+
* resolveLookupKey({ variant: 'light-390', case: 'mail shows the inbox' })
|
|
824
|
+
* // '["light-390","mail shows the inbox"]'
|
|
825
|
+
* ```
|
|
826
|
+
*/
|
|
827
|
+
function resolveLookupKey(lookup) {
|
|
828
|
+
return JSON.stringify([lookup.variant, lookup.case]);
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* Reads successful Vitest JSON output and validates every owner-bearing assertion.
|
|
832
|
+
*
|
|
833
|
+
* @param value - The parsed external reporter document
|
|
834
|
+
* @returns A validated projection of the fields used by integrity
|
|
835
|
+
* @throws Thrown as an `EVIDENCE` capture error for failed reporters, malformed assertions, or duplicate case identities.
|
|
836
|
+
*
|
|
837
|
+
* @example
|
|
838
|
+
* ```ts
|
|
839
|
+
* const report = readCaptureReport(parseJSON(text))
|
|
840
|
+
* report.assertions.map((assertion) => assertion.case)
|
|
841
|
+
* ```
|
|
842
|
+
*/
|
|
843
|
+
function readCaptureReport(value) {
|
|
844
|
+
if (!isRecord(value) || value.success !== true || value.numFailedTests !== 0 || value.numFailedTestSuites !== 0 || !isFiniteNumber(value.startTime) || !arrayOf(isRecord)(value.testResults) || value.testResults.length === 0) throw new CaptureError("Invalid or unsuccessful Vitest JSON report", { code: "EVIDENCE" });
|
|
845
|
+
const files = value.testResults.map((file) => {
|
|
846
|
+
if (file.status !== "passed" || !isFiniteNumber(file.startTime) || !isFiniteNumber(file.endTime) || file.endTime < file.startTime || !arrayOf(isRecord)(file.assertionResults)) throw new CaptureError("Invalid Vitest file report", { code: "EVIDENCE" });
|
|
847
|
+
return {
|
|
848
|
+
start: file.startTime,
|
|
849
|
+
end: file.endTime,
|
|
850
|
+
assertions: file.assertionResults
|
|
851
|
+
};
|
|
852
|
+
});
|
|
853
|
+
const assertions = files.flatMap((file) => file.assertions.flatMap((assertion) => {
|
|
854
|
+
if (!isRecord(assertion.meta)) throw new CaptureError("Invalid Vitest assertion metadata", { code: "EVIDENCE" });
|
|
855
|
+
if (assertion.meta.owner === void 0) return [];
|
|
856
|
+
const declaration = {
|
|
857
|
+
case: assertion.fullName,
|
|
858
|
+
owner: assertion.meta.owner,
|
|
859
|
+
variant: assertion.meta.variant,
|
|
860
|
+
eligible: assertion.meta.eligible === void 0 ? true : assertion.meta.eligible,
|
|
861
|
+
states: assertion.meta.states === void 0 ? [] : assertion.meta.states
|
|
862
|
+
};
|
|
863
|
+
if (!isCaptureDeclaration(declaration) || !isString(assertion.status) || assertion.duration !== void 0 && assertion.duration !== null && (!isFiniteNumber(assertion.duration) || assertion.duration < 0)) throw new CaptureError("Invalid owner-bearing Vitest assertion", { code: "EVIDENCE" });
|
|
864
|
+
return [{
|
|
865
|
+
...declaration,
|
|
866
|
+
status: assertion.status,
|
|
867
|
+
...typeof assertion.duration === "number" ? { duration: assertion.duration } : {}
|
|
868
|
+
}];
|
|
869
|
+
}));
|
|
870
|
+
requireCaptureUnique(assertions.map(resolveLookupKey), "reporter case identities");
|
|
871
|
+
return {
|
|
872
|
+
start: Math.min(value.startTime, ...files.map((file) => file.start)),
|
|
873
|
+
end: Math.max(...files.map((file) => file.end)),
|
|
874
|
+
assertions
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Validates declaration, reporter, observation, completeness, and output-projection agreement.
|
|
879
|
+
*
|
|
880
|
+
* @param run - The final record
|
|
881
|
+
* @param report - The validated external reporter
|
|
882
|
+
* @param project - The actual producing project name
|
|
883
|
+
* @throws Thrown as an `EVIDENCE` capture error when a record differs from the actual selected results or declarations.
|
|
884
|
+
*
|
|
885
|
+
* @example
|
|
886
|
+
* ```ts
|
|
887
|
+
* validateCaptureRun(run, readCaptureReport(parseJSON(text)), 'journey:light-390')
|
|
888
|
+
* ```
|
|
889
|
+
*/
|
|
890
|
+
function validateCaptureRun(run, report, project) {
|
|
891
|
+
const assertions = report.assertions.filter((entry) => entry.variant === run.variant);
|
|
892
|
+
requireCaptureUnique(run.declarations.map((entry) => entry.case), "declaring cases");
|
|
893
|
+
requireCaptureUnique(run.declarations.flatMap((entry) => entry.states), "declared states", true);
|
|
894
|
+
requireCaptureUnique(run.registry.map(resolveCaptureKey), "registry identities", true);
|
|
895
|
+
requireCaptureUnique(run.registry.map((entry) => entry.file), "registry paths", true);
|
|
896
|
+
requireCaptureUnique(run.entries.map(resolveCaptureKey), "emitted identities", true);
|
|
897
|
+
requireCaptureUnique(run.passed, "passed cases");
|
|
898
|
+
requireCaptureUnique(run.observed, "observed states", true);
|
|
899
|
+
if (!matchesCaptureMembers(run.declarations.map(describeCaptureDeclaration), assertions.map(describeCaptureDeclaration))) throw new CaptureError("Run declarations differ from full reporter declarations", { code: "EVIDENCE" });
|
|
900
|
+
const eligible = run.declarations.filter((entry) => entry.eligible);
|
|
901
|
+
const passed = assertions.filter((entry) => entry.eligible && entry.status === "passed");
|
|
902
|
+
if (!matchesCaptureMembers(run.passed, passed.map((entry) => entry.case))) throw new CaptureError("Run passed cases differ from reporter results", { code: "EVIDENCE" });
|
|
903
|
+
if (!matchesCaptureMembers(run.observed, passed.flatMap((entry) => entry.states))) throw new CaptureError("Run observations differ from full passed declarations", { code: "EVIDENCE" });
|
|
904
|
+
if (run.complete !== (eligible.length > 0 && eligible.length === passed.length)) throw new CaptureError("Run completeness differs from eligible results", { code: "EVIDENCE" });
|
|
905
|
+
for (const declaration of eligible) {
|
|
906
|
+
const destinations = run.registry.filter((entry) => entry.case === declaration.case);
|
|
907
|
+
if (!matchesCaptureMembers(destinations.map((entry) => entry.state), declaration.states) || destinations.some((entry) => entry.project !== project || !matchesCaptureSelector(resolveCaptureOwner(entry), declaration.owner))) throw new CaptureError(`Registry differs from eligible declaration: ${declaration.case}`, { code: "EVIDENCE" });
|
|
908
|
+
}
|
|
909
|
+
if (run.registry.some((entry) => !eligible.some((declaration) => declaration.case === entry.case))) throw new CaptureError("Registry contains an ineligible or undeclared case", { code: "EVIDENCE" });
|
|
910
|
+
const reference = run.registry.filter((entry) => entry.section === entry.state);
|
|
911
|
+
if (run.reference !== (reference.length > 0 && reference.every((entry) => run.passed.includes(entry.case)))) throw new CaptureError("Reference completeness differs from declaring section results", { code: "EVIDENCE" });
|
|
912
|
+
const request = run.request;
|
|
913
|
+
if (request !== void 0) {
|
|
914
|
+
if (run.identity === void 0 || run.identity.id !== request.id || run.identity.stage !== request.stage || run.identity.pid !== request.pid || run.identity.config !== request.config || !request.variants.includes(run.variant)) throw new CaptureError("Run identity differs from its request", { code: "EVIDENCE" });
|
|
915
|
+
const selected = eligible.filter((entry) => matchesCaptureSelector(entry.owner, request.selector) && (request.case === void 0 || entry.case === request.case) && (request.command === "journey" || entry.states.length > 0));
|
|
916
|
+
if (!matchesCaptureMembers(selected.map((entry) => entry.case), run.passed)) throw new CaptureError("Selected full case names differ from passed results", { code: "EVIDENCE" });
|
|
917
|
+
if (request.state !== void 0 && selected.some((entry) => !entry.states.includes(request.state ?? ""))) throw new CaptureError("Requested state differs from its selected declaring case", { code: "EVIDENCE" });
|
|
918
|
+
}
|
|
919
|
+
const expected = request?.command === "journey" || request === void 0 && run.entries.length === 0 ? [] : run.registry.filter((entry) => run.passed.includes(entry.case) && (request?.state === void 0 || entry.state === request.state));
|
|
920
|
+
if (!matchesCaptureMembers(run.entries.map(describeCaptureDestination), expected.map(describeCaptureDestination))) throw new CaptureError("Emitted entries differ from exact requested projection", { code: "EVIDENCE" });
|
|
921
|
+
for (const entry of run.entries) if (passed.find((assertion) => assertion.case === entry.case)?.duration !== entry.duration) throw new CaptureError(`Emitted duration differs from its producing case: ${entry.case}`, { code: "EVIDENCE" });
|
|
922
|
+
}
|
|
923
|
+
/**
|
|
924
|
+
* Chooses the primary classification while retaining all findings separately.
|
|
925
|
+
*
|
|
926
|
+
* @param statuses - Every classification justified by the reading
|
|
927
|
+
* @returns The highest-precedence classification, or `current` when none applies
|
|
928
|
+
*
|
|
929
|
+
* @example
|
|
930
|
+
* ```ts
|
|
931
|
+
* resolveCaptureStatus(['stale-own', 'missing']) // 'missing'
|
|
932
|
+
* resolveCaptureStatus([]) // 'current'
|
|
933
|
+
* ```
|
|
934
|
+
*/
|
|
935
|
+
function resolveCaptureStatus(statuses) {
|
|
936
|
+
return CAPTURE_STATUS_PRECEDENCE.find((status) => statuses.includes(status)) ?? "current";
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Reads Git's NUL-delimited porcelain paths without treating spaces or quoted names as separators.
|
|
940
|
+
*
|
|
941
|
+
* @param output - The exact `status --porcelain=v1 -z` output
|
|
942
|
+
* @returns Dirty repository-relative paths, including both sides of renames, sorted without repeats
|
|
943
|
+
* @throws Thrown as an `EVIDENCE` capture error when a rename or copy record has no original path.
|
|
944
|
+
*
|
|
945
|
+
* @example
|
|
946
|
+
* ```ts
|
|
947
|
+
* readCaptureDirty(' M src/a.ts\0R src/b.ts\0src/c.ts\0') // ['src/a.ts', 'src/b.ts', 'src/c.ts']
|
|
948
|
+
* ```
|
|
949
|
+
*/
|
|
950
|
+
function readCaptureDirty(output) {
|
|
951
|
+
const result = output.split("\0").filter((record) => record !== "").reduce((state, record) => state.pending ? {
|
|
952
|
+
paths: [...state.paths, record],
|
|
953
|
+
pending: false
|
|
954
|
+
} : {
|
|
955
|
+
paths: [...state.paths, record.slice(3)],
|
|
956
|
+
pending: /[RC]/u.test(record.slice(0, 2))
|
|
957
|
+
}, {
|
|
958
|
+
paths: [],
|
|
959
|
+
pending: false
|
|
960
|
+
});
|
|
961
|
+
if (result.pending) throw new CaptureError("Incomplete Git rename provenance", { code: "EVIDENCE" });
|
|
962
|
+
return [...new Set(result.paths)].toSorted();
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Validates a complete receipt against full reporter declarations and eligible output coverage.
|
|
966
|
+
*
|
|
967
|
+
* @param receipt - The retained complete-run receipt
|
|
968
|
+
* @param report - The authenticated reporter projection
|
|
969
|
+
* @throws Thrown as an `EVIDENCE` capture error when the receipt omits, invents, or changes a collected declaration or output.
|
|
970
|
+
*
|
|
971
|
+
* @example
|
|
972
|
+
* ```ts
|
|
973
|
+
* validateCaptureReceipt(receipt, readCaptureReport(parseJSON(text)))
|
|
974
|
+
* ```
|
|
975
|
+
*/
|
|
976
|
+
function validateCaptureReceipt(receipt, report) {
|
|
977
|
+
const assertions = report.assertions.filter((entry) => entry.variant === receipt.variant);
|
|
978
|
+
if (!receipt.complete || !matchesCaptureMembers(assertions.map(describeCaptureDeclaration), receipt.declarations.map(describeCaptureDeclaration)) || !assertions.some((entry) => entry.eligible) || assertions.some((entry) => entry.eligible && entry.status !== "passed")) throw new CaptureError("Registry receipt does not prove complete eligible behavior", { code: "EVIDENCE" });
|
|
979
|
+
const eligible = receipt.declarations.filter((entry) => entry.eligible);
|
|
980
|
+
for (const declaration of eligible) {
|
|
981
|
+
requireCaptureUnique(declaration.states, "states within a declaration", true);
|
|
982
|
+
const entries = receipt.registry.filter((entry) => entry.case === declaration.case);
|
|
983
|
+
if (!matchesCaptureMembers(entries.map((entry) => entry.state), declaration.states) || entries.some((entry) => !matchesCaptureSelector(resolveCaptureOwner(entry), declaration.owner))) throw new CaptureError("Registry receipt destination coverage differs from declarations", { code: "EVIDENCE" });
|
|
984
|
+
}
|
|
985
|
+
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" });
|
|
986
|
+
}
|
|
987
|
+
export { CAPTURE_ERROR_BRAND, CAPTURE_ERROR_CODES, CAPTURE_LOOKUPS_FILE, CAPTURE_MANIFEST_FILE, CAPTURE_PNG_SIGNATURE, CAPTURE_REGISTRY_FILE, CAPTURE_REQUEST_PREFIX, CAPTURE_STATUS_PRECEDENCE, CAPTURE_VERSION, CaptureError, collectCaptureChanges, collectCaptureDuplicates, describeCaptureDeclaration, describeCaptureDestination, describeCaptureError, describeCaptureIdentity, describeCaptureRequest, isCaptureArtifact, isCaptureBox, isCaptureCollection, isCaptureDeclaration, isCaptureDestination, isCaptureDigest, isCaptureEntry, isCaptureError, isCaptureGeometry, isCaptureHash, isCaptureHashes, isCaptureIdentity, isCaptureLookup, isCaptureLookupJournal, isCaptureManifest, isCapturePath, isCapturePattern, isCaptureReceipt, isCaptureRegistry, isCaptureRequest, isCaptureRun, isCaptureSegment, isCaptureSelector, isCaptureSources, isCaptureStamp, isCaptureStoredManifest, matchesCaptureMembers, matchesCaptureSelector, readCaptureDirty, readCaptureReport, requireCaptureUnique, resolveCaptureKey, resolveCaptureOwner, resolveCaptureStatus, resolveLookupKey, validateCaptureReceipt, validateCaptureRun };
|
|
988
|
+
|
|
989
|
+
//# sourceMappingURL=index.js.map
|