@edjbarron/ux-guard 0.0.0-stage → 0.6.1
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 +201 -0
- package/NOTICE +2 -0
- package/README.md +239 -2
- package/bin/ux-guard.mjs +4 -0
- package/docs/cli-reference.md +145 -0
- package/docs/scoring-and-backslide.md +100 -0
- package/docs/test-suite.md +175 -0
- package/lib/authoritative.mjs +463 -0
- package/lib/baselines.mjs +1840 -0
- package/lib/catalog.mjs +197 -0
- package/lib/cli.mjs +1980 -0
- package/lib/config.mjs +209 -0
- package/lib/discovery.mjs +581 -0
- package/lib/journey-policy.mjs +590 -0
- package/lib/journeys.mjs +900 -0
- package/lib/observations.mjs +268 -0
- package/lib/patterns.mjs +247 -0
- package/lib/playwright.mjs +344 -0
- package/lib/run-contract.mjs +413 -0
- package/lib/shards.mjs +445 -0
- package/lib/targets.mjs +764 -0
- package/lib/verdicts.mjs +1769 -0
- package/lib/visuals.mjs +1263 -0
- package/lib/yaml.mjs +140 -0
- package/package.json +56 -4
- package/skills/ux-guard/SKILL.md +134 -0
package/lib/targets.mjs
ADDED
|
@@ -0,0 +1,764 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JOURNEY_METRICS,
|
|
3
|
+
validateJourneyReport,
|
|
4
|
+
} from './journeys.mjs';
|
|
5
|
+
import { canonicalize, sha256 } from './run-contract.mjs';
|
|
6
|
+
|
|
7
|
+
export const OWNER_TARGETS_KIND = 'ux-guard-owner-targets';
|
|
8
|
+
export const OWNER_TARGETS_VERSION = 2;
|
|
9
|
+
export const TARGET_ATTAINMENT_KIND = 'ux-guard-target-attainment';
|
|
10
|
+
export const TARGET_ATTAINMENT_VERSION = 1;
|
|
11
|
+
|
|
12
|
+
const SHA256 = /^[a-f0-9]{64}$/;
|
|
13
|
+
const DIGEST = /^sha256:[a-f0-9]{64}$/;
|
|
14
|
+
const ID = /^[a-z0-9]+(?:[._-][a-z0-9]+)*$/;
|
|
15
|
+
const TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/;
|
|
16
|
+
const APPROVAL_STATUSES = new Set(['candidate', 'reviewed']);
|
|
17
|
+
const OPERATORS = new Set(['at-most', 'at-least', 'exactly']);
|
|
18
|
+
const ATTAINMENT_STATUSES = new Set([
|
|
19
|
+
'met',
|
|
20
|
+
'breached',
|
|
21
|
+
'partial',
|
|
22
|
+
'not-configured',
|
|
23
|
+
'not-evaluated',
|
|
24
|
+
'incompatible',
|
|
25
|
+
]);
|
|
26
|
+
const RESULT_STATUSES = new Set(['met', 'breached', 'not-evaluated']);
|
|
27
|
+
const EXECUTION_STATUSES = new Set([
|
|
28
|
+
'successful',
|
|
29
|
+
'failed',
|
|
30
|
+
'skipped',
|
|
31
|
+
'unexecutable',
|
|
32
|
+
'infrastructure-error',
|
|
33
|
+
'unmeasured',
|
|
34
|
+
]);
|
|
35
|
+
const HARD_FLOORS = Object.freeze([
|
|
36
|
+
['minimumMeasuredVariants', 'at-least', (report) => report.run.completion.received],
|
|
37
|
+
['minimumSuccessfulVariants', 'at-least', (report) => report.run.completion.successful],
|
|
38
|
+
['maximumUnexecutableVariants', 'at-most', (report) => report.run.completion.unexecutable],
|
|
39
|
+
['maximumFailedVariants', 'at-most', (report) => report.run.completion.failed],
|
|
40
|
+
['maximumSkippedVariants', 'at-most', (report) => report.run.completion.skipped],
|
|
41
|
+
[
|
|
42
|
+
'maximumInfrastructureErrorVariants',
|
|
43
|
+
'at-most',
|
|
44
|
+
(report) => report.run.completion.infrastructureError,
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
'maximumCriticalFindings',
|
|
48
|
+
'at-most',
|
|
49
|
+
(report) =>
|
|
50
|
+
report.findings.filter((finding) => finding.severity === 'critical').length,
|
|
51
|
+
],
|
|
52
|
+
]);
|
|
53
|
+
const HARD_FLOOR_IDS = new Set(HARD_FLOORS.map(([id]) => id));
|
|
54
|
+
|
|
55
|
+
export const OWNER_TARGETS_SCHEMA = Object.freeze({
|
|
56
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
57
|
+
$id: 'urn:ux-guard:owner-targets:v2',
|
|
58
|
+
type: 'object',
|
|
59
|
+
additionalProperties: false,
|
|
60
|
+
required: [
|
|
61
|
+
'kind',
|
|
62
|
+
'version',
|
|
63
|
+
'canonicalHash',
|
|
64
|
+
'approval',
|
|
65
|
+
'policy',
|
|
66
|
+
'requirements',
|
|
67
|
+
'metrics',
|
|
68
|
+
'metricPolicy',
|
|
69
|
+
'hardFloors',
|
|
70
|
+
'sourceArtifacts',
|
|
71
|
+
],
|
|
72
|
+
properties: {
|
|
73
|
+
kind: { const: OWNER_TARGETS_KIND },
|
|
74
|
+
version: { const: OWNER_TARGETS_VERSION },
|
|
75
|
+
canonicalHash: { type: 'string', pattern: '^sha256:[a-f0-9]{64}$' },
|
|
76
|
+
approval: { type: 'object', additionalProperties: false },
|
|
77
|
+
policy: { type: 'object', additionalProperties: false },
|
|
78
|
+
requirements: { type: 'array' },
|
|
79
|
+
metrics: { type: 'array' },
|
|
80
|
+
metricPolicy: { type: 'object', additionalProperties: false },
|
|
81
|
+
hardFloors: { type: 'object', additionalProperties: false },
|
|
82
|
+
sourceArtifacts: { type: 'array' },
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
export const TARGET_ATTAINMENT_SCHEMA = Object.freeze({
|
|
87
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
88
|
+
$id: 'urn:ux-guard:target-attainment:v1',
|
|
89
|
+
type: 'object',
|
|
90
|
+
additionalProperties: false,
|
|
91
|
+
required: [
|
|
92
|
+
'kind',
|
|
93
|
+
'version',
|
|
94
|
+
'status',
|
|
95
|
+
'reason',
|
|
96
|
+
'canonicalHash',
|
|
97
|
+
'summary',
|
|
98
|
+
'hardFloors',
|
|
99
|
+
'journeys',
|
|
100
|
+
],
|
|
101
|
+
properties: {
|
|
102
|
+
kind: { const: TARGET_ATTAINMENT_KIND },
|
|
103
|
+
version: { const: TARGET_ATTAINMENT_VERSION },
|
|
104
|
+
status: { enum: [...ATTAINMENT_STATUSES] },
|
|
105
|
+
reason: { type: 'string' },
|
|
106
|
+
canonicalHash: { type: ['string', 'null'] },
|
|
107
|
+
summary: { type: 'object', additionalProperties: false },
|
|
108
|
+
hardFloors: { type: 'array' },
|
|
109
|
+
journeys: { type: 'array' },
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
export class OwnerTargetsError extends Error {
|
|
114
|
+
constructor(message) {
|
|
115
|
+
super(message);
|
|
116
|
+
this.name = 'OwnerTargetsError';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function validateOwnerTargets(targets) {
|
|
121
|
+
assertMap(targets, 'targets');
|
|
122
|
+
assertExactKeys(
|
|
123
|
+
targets,
|
|
124
|
+
[
|
|
125
|
+
'kind',
|
|
126
|
+
'version',
|
|
127
|
+
'canonicalHash',
|
|
128
|
+
'approval',
|
|
129
|
+
'policy',
|
|
130
|
+
'requirements',
|
|
131
|
+
'metrics',
|
|
132
|
+
'metricPolicy',
|
|
133
|
+
'hardFloors',
|
|
134
|
+
'sourceArtifacts',
|
|
135
|
+
],
|
|
136
|
+
'targets',
|
|
137
|
+
);
|
|
138
|
+
if (
|
|
139
|
+
targets.kind !== OWNER_TARGETS_KIND ||
|
|
140
|
+
targets.version !== OWNER_TARGETS_VERSION
|
|
141
|
+
) {
|
|
142
|
+
throw new OwnerTargetsError('targets have an unsupported kind or version');
|
|
143
|
+
}
|
|
144
|
+
if (
|
|
145
|
+
typeof targets.canonicalHash !== 'string' ||
|
|
146
|
+
!DIGEST.test(targets.canonicalHash) ||
|
|
147
|
+
targets.canonicalHash !== ownerTargetsHash(targets)
|
|
148
|
+
) {
|
|
149
|
+
throw new OwnerTargetsError('targets canonicalHash does not match canonical content');
|
|
150
|
+
}
|
|
151
|
+
validateApproval(targets.approval);
|
|
152
|
+
validatePolicy(targets.policy);
|
|
153
|
+
validateRequirements(targets.requirements);
|
|
154
|
+
validateMetrics(targets.metrics);
|
|
155
|
+
validateMetricPolicy(targets.metricPolicy);
|
|
156
|
+
validateHardFloors(targets.hardFloors);
|
|
157
|
+
validateArtifacts(targets.sourceArtifacts);
|
|
158
|
+
return targets;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function parseOwnerTargets(text) {
|
|
162
|
+
if (typeof text !== 'string') throw new OwnerTargetsError('owner targets must be JSON text');
|
|
163
|
+
let targets;
|
|
164
|
+
try {
|
|
165
|
+
targets = JSON.parse(text);
|
|
166
|
+
} catch {
|
|
167
|
+
throw new OwnerTargetsError('owner targets are not valid JSON');
|
|
168
|
+
}
|
|
169
|
+
if (
|
|
170
|
+
targets?.kind === OWNER_TARGETS_KIND &&
|
|
171
|
+
targets.version !== OWNER_TARGETS_VERSION
|
|
172
|
+
) {
|
|
173
|
+
throw new OwnerTargetsError(
|
|
174
|
+
`unsupported owner targets version ${String(targets.version)}; migrate to version ${OWNER_TARGETS_VERSION}`,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
return validateOwnerTargets(targets);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function serializeOwnerTargets(targets) {
|
|
181
|
+
validateOwnerTargets(targets);
|
|
182
|
+
return `${JSON.stringify(canonicalize(targets), null, 2)}\n`;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function ownerTargetsHash(targets) {
|
|
186
|
+
const content = structuredClone(targets);
|
|
187
|
+
delete content.canonicalHash;
|
|
188
|
+
return `sha256:${sha256(content)}`;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function evaluateTargetAttainment({ targets, report } = {}) {
|
|
192
|
+
validateOwnerTargets(targets);
|
|
193
|
+
validateJourneyReport(report);
|
|
194
|
+
if (targets.approval.status !== 'reviewed') {
|
|
195
|
+
throw new OwnerTargetsError('only reviewed owner targets can be evaluated');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const hardFloors = HARD_FLOORS.map(([id, operator, actualValue]) => {
|
|
199
|
+
const target = targets.hardFloors[id];
|
|
200
|
+
const actual = actualValue(report);
|
|
201
|
+
return {
|
|
202
|
+
id,
|
|
203
|
+
operator,
|
|
204
|
+
target,
|
|
205
|
+
actual,
|
|
206
|
+
status: targetStatus(operator, actual, target),
|
|
207
|
+
};
|
|
208
|
+
});
|
|
209
|
+
const measurements = new Map(
|
|
210
|
+
report.journeys.map((journey) => [journey.id, journey]),
|
|
211
|
+
);
|
|
212
|
+
const grouped = new Map();
|
|
213
|
+
for (const target of targets.metrics) {
|
|
214
|
+
const measurement = measurements.get(target.journeyRef);
|
|
215
|
+
const actual =
|
|
216
|
+
measurement?.status === 'successful' &&
|
|
217
|
+
measurement.metrics &&
|
|
218
|
+
Object.hasOwn(measurement.metrics, target.metric)
|
|
219
|
+
? measurement.metrics[target.metric]
|
|
220
|
+
: null;
|
|
221
|
+
const metric = {
|
|
222
|
+
metric: target.metric,
|
|
223
|
+
operator: target.operator,
|
|
224
|
+
target: target.value,
|
|
225
|
+
actual,
|
|
226
|
+
status:
|
|
227
|
+
actual === null
|
|
228
|
+
? 'not-evaluated'
|
|
229
|
+
: targetStatus(target.operator, actual, target.value),
|
|
230
|
+
};
|
|
231
|
+
const journey =
|
|
232
|
+
grouped.get(target.journeyRef) ??
|
|
233
|
+
{
|
|
234
|
+
journeyRef: target.journeyRef,
|
|
235
|
+
useCaseId: measurement?.useCaseId ?? useCaseFromJourneyRef(target.journeyRef),
|
|
236
|
+
executionStatus: measurement?.status ?? 'unmeasured',
|
|
237
|
+
status: 'not-evaluated',
|
|
238
|
+
metrics: [],
|
|
239
|
+
};
|
|
240
|
+
journey.metrics.push(metric);
|
|
241
|
+
grouped.set(target.journeyRef, journey);
|
|
242
|
+
}
|
|
243
|
+
const journeys = [...grouped.values()]
|
|
244
|
+
.map((journey) => ({
|
|
245
|
+
...journey,
|
|
246
|
+
status: combinedJourneyTargetStatus(
|
|
247
|
+
journey.metrics.map(({ status }) => status),
|
|
248
|
+
),
|
|
249
|
+
metrics: journey.metrics.sort((left, right) =>
|
|
250
|
+
left.metric.localeCompare(right.metric),
|
|
251
|
+
),
|
|
252
|
+
}))
|
|
253
|
+
.sort((left, right) => left.journeyRef.localeCompare(right.journeyRef));
|
|
254
|
+
const results = [
|
|
255
|
+
...hardFloors.map(({ status }) => status),
|
|
256
|
+
...journeys.flatMap((journey) =>
|
|
257
|
+
journey.metrics.map(({ status }) => status),
|
|
258
|
+
),
|
|
259
|
+
];
|
|
260
|
+
const summary = targetSummary(results);
|
|
261
|
+
const status = combinedTargetStatus(results);
|
|
262
|
+
const attainment = {
|
|
263
|
+
kind: TARGET_ATTAINMENT_KIND,
|
|
264
|
+
version: TARGET_ATTAINMENT_VERSION,
|
|
265
|
+
status,
|
|
266
|
+
reason:
|
|
267
|
+
status === 'breached'
|
|
268
|
+
? `${summary.breached} configured target${summary.breached === 1 ? '' : 's'} breached.`
|
|
269
|
+
: status === 'partial'
|
|
270
|
+
? `${summary.notEvaluated} configured target${summary.notEvaluated === 1 ? '' : 's'} could not be evaluated.`
|
|
271
|
+
: 'Every configured target was attained.',
|
|
272
|
+
canonicalHash: targets.canonicalHash,
|
|
273
|
+
summary,
|
|
274
|
+
hardFloors,
|
|
275
|
+
journeys,
|
|
276
|
+
};
|
|
277
|
+
return validateTargetAttainment(attainment);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export function unavailableTargetAttainment(
|
|
281
|
+
status,
|
|
282
|
+
reason,
|
|
283
|
+
canonicalHash = null,
|
|
284
|
+
) {
|
|
285
|
+
if (!['not-configured', 'not-evaluated', 'incompatible'].includes(status)) {
|
|
286
|
+
throw new OwnerTargetsError('unavailable target attainment status is invalid');
|
|
287
|
+
}
|
|
288
|
+
assertText(reason, 'reason');
|
|
289
|
+
const attainment = {
|
|
290
|
+
kind: TARGET_ATTAINMENT_KIND,
|
|
291
|
+
version: TARGET_ATTAINMENT_VERSION,
|
|
292
|
+
status,
|
|
293
|
+
reason,
|
|
294
|
+
canonicalHash,
|
|
295
|
+
summary: {
|
|
296
|
+
configured: 0,
|
|
297
|
+
evaluated: 0,
|
|
298
|
+
met: 0,
|
|
299
|
+
breached: 0,
|
|
300
|
+
notEvaluated: 0,
|
|
301
|
+
},
|
|
302
|
+
hardFloors: [],
|
|
303
|
+
journeys: [],
|
|
304
|
+
};
|
|
305
|
+
return validateTargetAttainment(attainment);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function validateTargetAttainment(attainment) {
|
|
309
|
+
assertMap(attainment, 'attainment');
|
|
310
|
+
assertExactKeys(
|
|
311
|
+
attainment,
|
|
312
|
+
[
|
|
313
|
+
'kind',
|
|
314
|
+
'version',
|
|
315
|
+
'status',
|
|
316
|
+
'reason',
|
|
317
|
+
'canonicalHash',
|
|
318
|
+
'summary',
|
|
319
|
+
'hardFloors',
|
|
320
|
+
'journeys',
|
|
321
|
+
],
|
|
322
|
+
'attainment',
|
|
323
|
+
);
|
|
324
|
+
if (
|
|
325
|
+
attainment.kind !== TARGET_ATTAINMENT_KIND ||
|
|
326
|
+
attainment.version !== TARGET_ATTAINMENT_VERSION
|
|
327
|
+
) {
|
|
328
|
+
throw new OwnerTargetsError('target attainment has an unsupported contract');
|
|
329
|
+
}
|
|
330
|
+
if (!ATTAINMENT_STATUSES.has(attainment.status)) {
|
|
331
|
+
throw new OwnerTargetsError('attainment.status is invalid');
|
|
332
|
+
}
|
|
333
|
+
assertText(attainment.reason, 'attainment.reason');
|
|
334
|
+
if (
|
|
335
|
+
attainment.canonicalHash !== null &&
|
|
336
|
+
(typeof attainment.canonicalHash !== 'string' ||
|
|
337
|
+
!DIGEST.test(attainment.canonicalHash))
|
|
338
|
+
) {
|
|
339
|
+
throw new OwnerTargetsError('attainment.canonicalHash is invalid');
|
|
340
|
+
}
|
|
341
|
+
assertMap(attainment.summary, 'attainment.summary');
|
|
342
|
+
assertExactKeys(
|
|
343
|
+
attainment.summary,
|
|
344
|
+
['configured', 'evaluated', 'met', 'breached', 'notEvaluated'],
|
|
345
|
+
'attainment.summary',
|
|
346
|
+
);
|
|
347
|
+
for (const [name, value] of Object.entries(attainment.summary)) {
|
|
348
|
+
assertCount(value, `attainment.summary.${name}`);
|
|
349
|
+
}
|
|
350
|
+
if (
|
|
351
|
+
!Array.isArray(attainment.hardFloors) ||
|
|
352
|
+
!Array.isArray(attainment.journeys)
|
|
353
|
+
) {
|
|
354
|
+
throw new OwnerTargetsError(
|
|
355
|
+
'attainment hardFloors and journeys must be arrays',
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
const floorIds = new Set();
|
|
359
|
+
for (const [index, floor] of attainment.hardFloors.entries()) {
|
|
360
|
+
const path = `attainment.hardFloors[${index}]`;
|
|
361
|
+
validateTargetResult(floor, path, true);
|
|
362
|
+
if (floorIds.has(floor.id)) {
|
|
363
|
+
throw new OwnerTargetsError(`${path}.id is duplicated`);
|
|
364
|
+
}
|
|
365
|
+
floorIds.add(floor.id);
|
|
366
|
+
if (floor.status === 'not-evaluated') {
|
|
367
|
+
throw new OwnerTargetsError(`${path} must always be evaluated`);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const journeyIds = new Set();
|
|
371
|
+
const resultStatuses = attainment.hardFloors.map(({ status }) => status);
|
|
372
|
+
for (const [index, journey] of attainment.journeys.entries()) {
|
|
373
|
+
const path = `attainment.journeys[${index}]`;
|
|
374
|
+
assertMap(journey, path);
|
|
375
|
+
assertExactKeys(
|
|
376
|
+
journey,
|
|
377
|
+
[
|
|
378
|
+
'journeyRef',
|
|
379
|
+
'useCaseId',
|
|
380
|
+
'executionStatus',
|
|
381
|
+
'status',
|
|
382
|
+
'metrics',
|
|
383
|
+
],
|
|
384
|
+
path,
|
|
385
|
+
);
|
|
386
|
+
assertId(journey.journeyRef, `${path}.journeyRef`);
|
|
387
|
+
assertId(journey.useCaseId, `${path}.useCaseId`);
|
|
388
|
+
if (journeyIds.has(journey.journeyRef)) {
|
|
389
|
+
throw new OwnerTargetsError(`${path}.journeyRef is duplicated`);
|
|
390
|
+
}
|
|
391
|
+
journeyIds.add(journey.journeyRef);
|
|
392
|
+
if (!EXECUTION_STATUSES.has(journey.executionStatus)) {
|
|
393
|
+
throw new OwnerTargetsError(`${path}.executionStatus is invalid`);
|
|
394
|
+
}
|
|
395
|
+
if (!RESULT_STATUSES.has(journey.status)) {
|
|
396
|
+
throw new OwnerTargetsError(`${path}.status is invalid`);
|
|
397
|
+
}
|
|
398
|
+
if (!Array.isArray(journey.metrics) || journey.metrics.length === 0) {
|
|
399
|
+
throw new OwnerTargetsError(`${path}.metrics must not be empty`);
|
|
400
|
+
}
|
|
401
|
+
const metrics = new Set();
|
|
402
|
+
for (const [metricIndex, metric] of journey.metrics.entries()) {
|
|
403
|
+
const metricPath = `${path}.metrics[${metricIndex}]`;
|
|
404
|
+
validateTargetResult(metric, metricPath, false);
|
|
405
|
+
if (metrics.has(metric.metric)) {
|
|
406
|
+
throw new OwnerTargetsError(`${metricPath}.metric is duplicated`);
|
|
407
|
+
}
|
|
408
|
+
metrics.add(metric.metric);
|
|
409
|
+
resultStatuses.push(metric.status);
|
|
410
|
+
}
|
|
411
|
+
if (
|
|
412
|
+
journey.status !==
|
|
413
|
+
combinedJourneyTargetStatus(journey.metrics.map(({ status }) => status))
|
|
414
|
+
) {
|
|
415
|
+
throw new OwnerTargetsError(`${path}.status does not match its metrics`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
const summary = targetSummary(resultStatuses);
|
|
419
|
+
if (
|
|
420
|
+
Object.entries(summary).some(
|
|
421
|
+
([name, value]) => attainment.summary[name] !== value,
|
|
422
|
+
)
|
|
423
|
+
) {
|
|
424
|
+
throw new OwnerTargetsError('attainment.summary does not match its results');
|
|
425
|
+
}
|
|
426
|
+
const available = ['met', 'breached', 'partial'].includes(attainment.status);
|
|
427
|
+
if (
|
|
428
|
+
available &&
|
|
429
|
+
attainment.status !== combinedTargetStatus(resultStatuses)
|
|
430
|
+
) {
|
|
431
|
+
throw new OwnerTargetsError('attainment.status does not match its results');
|
|
432
|
+
}
|
|
433
|
+
if (
|
|
434
|
+
!available &&
|
|
435
|
+
(resultStatuses.length !== 0 || attainment.canonicalHash !== null &&
|
|
436
|
+
attainment.status === 'not-configured')
|
|
437
|
+
) {
|
|
438
|
+
throw new OwnerTargetsError('unavailable target attainment is inconsistent');
|
|
439
|
+
}
|
|
440
|
+
return attainment;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function validateTargetResult(result, path, floor) {
|
|
444
|
+
assertMap(result, path);
|
|
445
|
+
assertExactKeys(
|
|
446
|
+
result,
|
|
447
|
+
floor
|
|
448
|
+
? ['id', 'operator', 'target', 'actual', 'status']
|
|
449
|
+
: ['metric', 'operator', 'target', 'actual', 'status'],
|
|
450
|
+
path,
|
|
451
|
+
);
|
|
452
|
+
if (floor) {
|
|
453
|
+
if (!HARD_FLOOR_IDS.has(result.id)) {
|
|
454
|
+
throw new OwnerTargetsError(`${path}.id is invalid`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
else if (!JOURNEY_METRICS.includes(result.metric)) {
|
|
458
|
+
throw new OwnerTargetsError(`${path}.metric is invalid`);
|
|
459
|
+
}
|
|
460
|
+
if (!OPERATORS.has(result.operator)) {
|
|
461
|
+
throw new OwnerTargetsError(`${path}.operator is invalid`);
|
|
462
|
+
}
|
|
463
|
+
assertCount(result.target, `${path}.target`);
|
|
464
|
+
if (result.actual !== null) assertCount(result.actual, `${path}.actual`);
|
|
465
|
+
if (!RESULT_STATUSES.has(result.status)) {
|
|
466
|
+
throw new OwnerTargetsError(`${path}.status is invalid`);
|
|
467
|
+
}
|
|
468
|
+
if (
|
|
469
|
+
(result.actual === null) !== (result.status === 'not-evaluated') ||
|
|
470
|
+
(result.actual !== null &&
|
|
471
|
+
result.status !== targetStatus(result.operator, result.actual, result.target))
|
|
472
|
+
) {
|
|
473
|
+
throw new OwnerTargetsError(`${path}.status does not match its values`);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function targetStatus(operator, actual, target) {
|
|
478
|
+
const met =
|
|
479
|
+
operator === 'at-most'
|
|
480
|
+
? actual <= target
|
|
481
|
+
: operator === 'at-least'
|
|
482
|
+
? actual >= target
|
|
483
|
+
: actual === target;
|
|
484
|
+
return met ? 'met' : 'breached';
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
function combinedTargetStatus(statuses) {
|
|
488
|
+
if (statuses.includes('breached')) return 'breached';
|
|
489
|
+
if (statuses.includes('not-evaluated')) return 'partial';
|
|
490
|
+
return 'met';
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function combinedJourneyTargetStatus(statuses) {
|
|
494
|
+
if (statuses.includes('breached')) return 'breached';
|
|
495
|
+
if (statuses.includes('not-evaluated')) return 'not-evaluated';
|
|
496
|
+
return 'met';
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function targetSummary(statuses) {
|
|
500
|
+
const met = statuses.filter((status) => status === 'met').length;
|
|
501
|
+
const breached = statuses.filter((status) => status === 'breached').length;
|
|
502
|
+
const notEvaluated = statuses.filter(
|
|
503
|
+
(status) => status === 'not-evaluated',
|
|
504
|
+
).length;
|
|
505
|
+
return {
|
|
506
|
+
configured: statuses.length,
|
|
507
|
+
evaluated: met + breached,
|
|
508
|
+
met,
|
|
509
|
+
breached,
|
|
510
|
+
notEvaluated,
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function useCaseFromJourneyRef(journeyRef) {
|
|
515
|
+
const match = /^journey\.([a-z0-9]+(?:[._-][a-z0-9]+)*)\./.exec(journeyRef);
|
|
516
|
+
return match?.[1] ?? 'unknown';
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function validateApproval(approval) {
|
|
520
|
+
assertMap(approval, 'targets.approval');
|
|
521
|
+
assertExactKeys(
|
|
522
|
+
approval,
|
|
523
|
+
['status', 'reviewer', 'reviewedAt', 'rationale'],
|
|
524
|
+
'targets.approval',
|
|
525
|
+
);
|
|
526
|
+
if (!APPROVAL_STATUSES.has(approval.status)) {
|
|
527
|
+
throw new OwnerTargetsError('targets.approval.status is invalid');
|
|
528
|
+
}
|
|
529
|
+
if (approval.status === 'candidate') {
|
|
530
|
+
if (
|
|
531
|
+
approval.reviewer !== null ||
|
|
532
|
+
approval.reviewedAt !== null ||
|
|
533
|
+
approval.rationale !== null
|
|
534
|
+
) {
|
|
535
|
+
throw new OwnerTargetsError('candidate targets cannot claim review metadata');
|
|
536
|
+
}
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
assertText(approval.reviewer, 'targets.approval.reviewer');
|
|
540
|
+
assertTimestamp(approval.reviewedAt, 'targets.approval.reviewedAt');
|
|
541
|
+
assertText(approval.rationale, 'targets.approval.rationale');
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function validatePolicy(policy) {
|
|
545
|
+
assertMap(policy, 'targets.policy');
|
|
546
|
+
assertExactKeys(policy, ['algorithm', 'digest'], 'targets.policy');
|
|
547
|
+
if (
|
|
548
|
+
policy.algorithm !== 'sha256' ||
|
|
549
|
+
typeof policy.digest !== 'string' ||
|
|
550
|
+
!DIGEST.test(policy.digest)
|
|
551
|
+
) {
|
|
552
|
+
throw new OwnerTargetsError('targets.policy is invalid');
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function validateRequirements(requirements) {
|
|
557
|
+
if (!Array.isArray(requirements)) {
|
|
558
|
+
throw new OwnerTargetsError('targets.requirements must be an array');
|
|
559
|
+
}
|
|
560
|
+
const ids = new Set();
|
|
561
|
+
for (const [index, requirement] of requirements.entries()) {
|
|
562
|
+
const path = `targets.requirements[${index}]`;
|
|
563
|
+
assertMap(requirement, path);
|
|
564
|
+
assertExactKeys(
|
|
565
|
+
requirement,
|
|
566
|
+
['useCaseId', 'statements'],
|
|
567
|
+
path,
|
|
568
|
+
);
|
|
569
|
+
assertId(requirement.useCaseId, `${path}.useCaseId`);
|
|
570
|
+
if (ids.has(requirement.useCaseId)) {
|
|
571
|
+
throw new OwnerTargetsError(`targets.requirements repeats ${requirement.useCaseId}`);
|
|
572
|
+
}
|
|
573
|
+
ids.add(requirement.useCaseId);
|
|
574
|
+
if (
|
|
575
|
+
!Array.isArray(requirement.statements) ||
|
|
576
|
+
requirement.statements.length === 0 ||
|
|
577
|
+
new Set(requirement.statements).size !== requirement.statements.length ||
|
|
578
|
+
requirement.statements.some(
|
|
579
|
+
(statement) => typeof statement !== 'string' || !statement.trim(),
|
|
580
|
+
)
|
|
581
|
+
) {
|
|
582
|
+
throw new OwnerTargetsError(`${path}.statements must be unique non-empty text`);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
function validateMetrics(metrics) {
|
|
588
|
+
if (!Array.isArray(metrics)) {
|
|
589
|
+
throw new OwnerTargetsError('targets.metrics must be an array');
|
|
590
|
+
}
|
|
591
|
+
const ids = new Set();
|
|
592
|
+
for (const [index, target] of metrics.entries()) {
|
|
593
|
+
const path = `targets.metrics[${index}]`;
|
|
594
|
+
assertMap(target, path);
|
|
595
|
+
assertExactKeys(
|
|
596
|
+
target,
|
|
597
|
+
['journeyRef', 'metric', 'operator', 'value'],
|
|
598
|
+
path,
|
|
599
|
+
);
|
|
600
|
+
assertId(target.journeyRef, `${path}.journeyRef`);
|
|
601
|
+
if (!JOURNEY_METRICS.includes(target.metric)) {
|
|
602
|
+
throw new OwnerTargetsError(`${path}.metric is invalid`);
|
|
603
|
+
}
|
|
604
|
+
if (!OPERATORS.has(target.operator)) {
|
|
605
|
+
throw new OwnerTargetsError(`${path}.operator is invalid`);
|
|
606
|
+
}
|
|
607
|
+
assertCount(target.value, `${path}.value`);
|
|
608
|
+
const id = `${target.journeyRef}\0${target.metric}`;
|
|
609
|
+
if (ids.has(id)) throw new OwnerTargetsError(`${path} duplicates a metric target`);
|
|
610
|
+
ids.add(id);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function validateMetricPolicy(metricPolicy) {
|
|
615
|
+
assertMap(metricPolicy, 'targets.metricPolicy');
|
|
616
|
+
assertExactKeys(
|
|
617
|
+
metricPolicy,
|
|
618
|
+
['nonRegressionAfterReview', 'reportOnlyUntilCalibrated'],
|
|
619
|
+
'targets.metricPolicy',
|
|
620
|
+
);
|
|
621
|
+
for (const field of [
|
|
622
|
+
'nonRegressionAfterReview',
|
|
623
|
+
'reportOnlyUntilCalibrated',
|
|
624
|
+
]) {
|
|
625
|
+
const metrics = metricPolicy[field];
|
|
626
|
+
if (
|
|
627
|
+
!Array.isArray(metrics) ||
|
|
628
|
+
metrics.length === 0 ||
|
|
629
|
+
new Set(metrics).size !== metrics.length ||
|
|
630
|
+
metrics.some((metric) => !JOURNEY_METRICS.includes(metric))
|
|
631
|
+
) {
|
|
632
|
+
throw new OwnerTargetsError(
|
|
633
|
+
`targets.metricPolicy.${field} must contain unique journey metrics`,
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
const nonRegression = new Set(metricPolicy.nonRegressionAfterReview);
|
|
638
|
+
const reportOnly = new Set(metricPolicy.reportOnlyUntilCalibrated);
|
|
639
|
+
if (
|
|
640
|
+
metricPolicy.nonRegressionAfterReview.some((metric) =>
|
|
641
|
+
reportOnly.has(metric),
|
|
642
|
+
)
|
|
643
|
+
) {
|
|
644
|
+
throw new OwnerTargetsError('targets.metricPolicy groups must be disjoint');
|
|
645
|
+
}
|
|
646
|
+
if (
|
|
647
|
+
JOURNEY_METRICS.some(
|
|
648
|
+
(metric) => !nonRegression.has(metric) && !reportOnly.has(metric),
|
|
649
|
+
)
|
|
650
|
+
) {
|
|
651
|
+
throw new OwnerTargetsError(
|
|
652
|
+
'targets.metricPolicy must classify every journey metric',
|
|
653
|
+
);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
function validateHardFloors(floors) {
|
|
658
|
+
assertMap(floors, 'targets.hardFloors');
|
|
659
|
+
assertExactKeys(
|
|
660
|
+
floors,
|
|
661
|
+
[
|
|
662
|
+
'minimumMeasuredVariants',
|
|
663
|
+
'minimumSuccessfulVariants',
|
|
664
|
+
'maximumUnexecutableVariants',
|
|
665
|
+
'maximumFailedVariants',
|
|
666
|
+
'maximumSkippedVariants',
|
|
667
|
+
'maximumInfrastructureErrorVariants',
|
|
668
|
+
'maximumCriticalFindings',
|
|
669
|
+
],
|
|
670
|
+
'targets.hardFloors',
|
|
671
|
+
);
|
|
672
|
+
for (const [name, value] of Object.entries(floors)) {
|
|
673
|
+
assertCount(value, `targets.hardFloors.${name}`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function validateArtifacts(artifacts) {
|
|
678
|
+
if (!Array.isArray(artifacts)) {
|
|
679
|
+
throw new OwnerTargetsError('targets.sourceArtifacts must be an array');
|
|
680
|
+
}
|
|
681
|
+
const ids = new Set();
|
|
682
|
+
const paths = new Set();
|
|
683
|
+
for (const [index, artifact] of artifacts.entries()) {
|
|
684
|
+
const path = `targets.sourceArtifacts[${index}]`;
|
|
685
|
+
assertMap(artifact, path);
|
|
686
|
+
assertExactKeys(artifact, ['id', 'path', 'sha256'], path);
|
|
687
|
+
assertId(artifact.id, `${path}.id`);
|
|
688
|
+
assertSafeRelativePath(artifact.path, `${path}.path`);
|
|
689
|
+
if (typeof artifact.sha256 !== 'string' || !SHA256.test(artifact.sha256)) {
|
|
690
|
+
throw new OwnerTargetsError(`${path}.sha256 is invalid`);
|
|
691
|
+
}
|
|
692
|
+
if (ids.has(artifact.id)) {
|
|
693
|
+
throw new OwnerTargetsError(`targets.sourceArtifacts repeats id ${artifact.id}`);
|
|
694
|
+
}
|
|
695
|
+
if (paths.has(artifact.path)) {
|
|
696
|
+
throw new OwnerTargetsError(`targets.sourceArtifacts repeats path ${artifact.path}`);
|
|
697
|
+
}
|
|
698
|
+
ids.add(artifact.id);
|
|
699
|
+
paths.add(artifact.path);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function assertMap(value, path) {
|
|
704
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
705
|
+
throw new OwnerTargetsError(`${path} must be an object`);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
function assertExactKeys(value, expected, path) {
|
|
710
|
+
const actual = Object.keys(value).sort();
|
|
711
|
+
const wanted = [...expected].sort();
|
|
712
|
+
if (
|
|
713
|
+
actual.length !== wanted.length ||
|
|
714
|
+
actual.some((key, index) => key !== wanted[index])
|
|
715
|
+
) {
|
|
716
|
+
throw new OwnerTargetsError(`${path} must contain ${wanted.join(', ')} only`);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
function assertText(value, path) {
|
|
721
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
722
|
+
throw new OwnerTargetsError(`${path} must be non-empty text`);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
function assertId(value, path) {
|
|
727
|
+
if (typeof value !== 'string' || !ID.test(value)) {
|
|
728
|
+
throw new OwnerTargetsError(`${path} is not a safe id`);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
function assertCount(value, path) {
|
|
733
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
734
|
+
throw new OwnerTargetsError(`${path} must be a non-negative safe integer`);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function assertTimestamp(value, path) {
|
|
739
|
+
const parsed = typeof value === 'string' ? new Date(value) : null;
|
|
740
|
+
const normalized =
|
|
741
|
+
parsed && !Number.isNaN(parsed.valueOf())
|
|
742
|
+
? parsed.toISOString().replace('.000Z', 'Z')
|
|
743
|
+
: null;
|
|
744
|
+
if (
|
|
745
|
+
typeof value !== 'string' ||
|
|
746
|
+
!TIMESTAMP.test(value) ||
|
|
747
|
+
normalized !== value.replace('.000Z', 'Z')
|
|
748
|
+
) {
|
|
749
|
+
throw new OwnerTargetsError(`${path} must be an explicit UTC timestamp`);
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
function assertSafeRelativePath(value, path) {
|
|
754
|
+
if (
|
|
755
|
+
typeof value !== 'string' ||
|
|
756
|
+
!value ||
|
|
757
|
+
value.startsWith('/') ||
|
|
758
|
+
value.includes('\\') ||
|
|
759
|
+
value.includes('\0') ||
|
|
760
|
+
value.split('/').some((part) => !part || part === '.' || part === '..')
|
|
761
|
+
) {
|
|
762
|
+
throw new OwnerTargetsError(`${path} must be a safe repository-relative path`);
|
|
763
|
+
}
|
|
764
|
+
}
|