@edjbarron/ux-guard 0.0.0-stage → 0.6.0
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 +153 -2
- package/bin/ux-guard.mjs +4 -0
- package/docs/cli-reference.md +98 -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 +1938 -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 +122 -0
package/lib/cli.mjs
ADDED
|
@@ -0,0 +1,1938 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
4
|
+
import {
|
|
5
|
+
AuthoritativeUpgradeError,
|
|
6
|
+
createAuthoritativeAdvisory,
|
|
7
|
+
} from './authoritative.mjs';
|
|
8
|
+
import {
|
|
9
|
+
JourneyBaselineError,
|
|
10
|
+
compareJourneyBaseline,
|
|
11
|
+
compareJourneyBaselineEnvelopes,
|
|
12
|
+
createJourneyBaselineCandidate,
|
|
13
|
+
parseJourneyBaseline,
|
|
14
|
+
promoteJourneyBaseline,
|
|
15
|
+
renderJourneyBaselineMarkdown,
|
|
16
|
+
renderJourneyComparisonMarkdown,
|
|
17
|
+
serializeJourneyBaseline,
|
|
18
|
+
serializeJourneyComparison,
|
|
19
|
+
unavailableJourneyComparison,
|
|
20
|
+
runJourneyComparisonCanaries,
|
|
21
|
+
} from './baselines.mjs';
|
|
22
|
+
import { catalogPolicy, loadCatalog, loadSurfaces, renderCatalog, validateCatalog } from './catalog.mjs';
|
|
23
|
+
import {
|
|
24
|
+
CONFIG_FILE,
|
|
25
|
+
loadConfig,
|
|
26
|
+
loadTypeScript,
|
|
27
|
+
readTrusted,
|
|
28
|
+
resolveRepositoryPath,
|
|
29
|
+
resolveTrustedRevision,
|
|
30
|
+
trustedFileExists,
|
|
31
|
+
validateConfig,
|
|
32
|
+
} from './config.mjs';
|
|
33
|
+
import {
|
|
34
|
+
candidateFromDiscovery,
|
|
35
|
+
discoverProject,
|
|
36
|
+
renderDiscovery,
|
|
37
|
+
setupProposalFromDiscovery,
|
|
38
|
+
} from './discovery.mjs';
|
|
39
|
+
import {
|
|
40
|
+
createJourneyArtifact,
|
|
41
|
+
parseJourneyPolicy,
|
|
42
|
+
} from './journey-policy.mjs';
|
|
43
|
+
import {
|
|
44
|
+
createJourneyReport,
|
|
45
|
+
parseJourneyReport,
|
|
46
|
+
} from './journeys.mjs';
|
|
47
|
+
import {
|
|
48
|
+
DEBT_RULES,
|
|
49
|
+
baselineFromScan,
|
|
50
|
+
compare,
|
|
51
|
+
listSourceFiles,
|
|
52
|
+
renderComparison,
|
|
53
|
+
scanFiles,
|
|
54
|
+
scanSource,
|
|
55
|
+
validateBaseline,
|
|
56
|
+
} from './patterns.mjs';
|
|
57
|
+
import {
|
|
58
|
+
createVerdictReport,
|
|
59
|
+
renderVerdictMarkdown,
|
|
60
|
+
serializeVerdictReport,
|
|
61
|
+
} from './verdicts.mjs';
|
|
62
|
+
import {
|
|
63
|
+
createRunEnvelope,
|
|
64
|
+
policyDigest,
|
|
65
|
+
resolveRunIdentity,
|
|
66
|
+
sha256,
|
|
67
|
+
} from './run-contract.mjs';
|
|
68
|
+
import {
|
|
69
|
+
OwnerTargetsError,
|
|
70
|
+
evaluateTargetAttainment,
|
|
71
|
+
parseOwnerTargets,
|
|
72
|
+
unavailableTargetAttainment,
|
|
73
|
+
} from './targets.mjs';
|
|
74
|
+
import {
|
|
75
|
+
compareVisualManifests,
|
|
76
|
+
parseVisualCheckpointPolicy,
|
|
77
|
+
parseVisualManifest,
|
|
78
|
+
parseVisualProfileSet,
|
|
79
|
+
promoteVisualBundle,
|
|
80
|
+
renderVisualComparisonMarkdown,
|
|
81
|
+
renderVisualManifestMarkdown,
|
|
82
|
+
runVisualComparisonCanaries,
|
|
83
|
+
unavailableVisualComparison,
|
|
84
|
+
VisualContractError,
|
|
85
|
+
} from './visuals.mjs';
|
|
86
|
+
import { ConfigError, parseYamlSubset } from './yaml.mjs';
|
|
87
|
+
|
|
88
|
+
const DEFAULT_SETUP_PROPOSAL = 'ux-guard/setup-proposal.json';
|
|
89
|
+
|
|
90
|
+
const USAGE = `usage: ux-guard <command> [options]
|
|
91
|
+
|
|
92
|
+
commands:
|
|
93
|
+
setup inspect this repo and save an unapproved setup proposal
|
|
94
|
+
check compare current UI code with reviewed UX policy
|
|
95
|
+
report explain UX evidence, gaps, and recommendations
|
|
96
|
+
|
|
97
|
+
help:
|
|
98
|
+
ux-guard setup --help
|
|
99
|
+
ux-guard check --help
|
|
100
|
+
ux-guard report --help
|
|
101
|
+
ux-guard help advanced
|
|
102
|
+
|
|
103
|
+
common options:
|
|
104
|
+
--root <dir> repository root (default: current directory)
|
|
105
|
+
--base <ref> use reviewed policy from a trusted Git revision
|
|
106
|
+
--format text|json human-readable or machine-readable output
|
|
107
|
+
`;
|
|
108
|
+
|
|
109
|
+
const SETUP_HELP = `usage: ux-guard setup [options]
|
|
110
|
+
|
|
111
|
+
Inspect UI source without approving it, then save a setup proposal for human
|
|
112
|
+
review. Setup never creates active policy, a reviewed baseline, a waiver, or
|
|
113
|
+
enforcement.
|
|
114
|
+
|
|
115
|
+
options:
|
|
116
|
+
--root <dir> repository root (default: current directory)
|
|
117
|
+
--proposal <path> proposal path (default: ${DEFAULT_SETUP_PROPOSAL})
|
|
118
|
+
--format text|json human-readable or machine-readable output
|
|
119
|
+
--typescript <path> advanced: explicit TypeScript package path
|
|
120
|
+
`;
|
|
121
|
+
|
|
122
|
+
const CHECK_HELP = `usage: ux-guard check [options]
|
|
123
|
+
|
|
124
|
+
Compare current UI code with reviewed policy. Findings use exit code 1;
|
|
125
|
+
missing, malformed, or unsafe inputs use exit code 2.
|
|
126
|
+
|
|
127
|
+
options:
|
|
128
|
+
--root <dir> repository root (default: current directory)
|
|
129
|
+
--base <ref> use reviewed policy from a trusted Git revision
|
|
130
|
+
--strict require every navigation surface to be catalogued
|
|
131
|
+
--format text|json human-readable or machine-readable output
|
|
132
|
+
`;
|
|
133
|
+
|
|
134
|
+
const REPORT_HELP = `usage: ux-guard report [options]
|
|
135
|
+
|
|
136
|
+
Explain configured journey evidence, UX quality, target attainment, regression,
|
|
137
|
+
and recommendations. Missing evidence stays unknown; it is never reported as
|
|
138
|
+
success.
|
|
139
|
+
|
|
140
|
+
options:
|
|
141
|
+
--root <dir> repository root (default: current directory)
|
|
142
|
+
--base <ref> compare with reviewed evidence from a trusted Git revision
|
|
143
|
+
--format json|markdown
|
|
144
|
+
machine-readable output or a human review document
|
|
145
|
+
`;
|
|
146
|
+
|
|
147
|
+
const ADVANCED_USAGE = `usage: ux-guard <advanced-command> [options]
|
|
148
|
+
|
|
149
|
+
advanced policy and evidence lifecycle:
|
|
150
|
+
authoritative aggregate shards and regenerate trusted advisory artifacts
|
|
151
|
+
discover audit observed UI conventions and emit review candidates
|
|
152
|
+
discover write-candidate --candidate <path>
|
|
153
|
+
write a new, explicitly unapproved review artifact
|
|
154
|
+
discover promote --candidate <path>
|
|
155
|
+
install a human-approved candidate without overwriting
|
|
156
|
+
baseline propose --candidate <path>
|
|
157
|
+
write an unapproved snapshot from the completed report
|
|
158
|
+
baseline inspect --candidate <path>
|
|
159
|
+
validate and render a candidate for human review
|
|
160
|
+
baseline diff --candidate <path> [--baseline <path>]
|
|
161
|
+
compare a candidate with a reviewed snapshot
|
|
162
|
+
baseline promote --candidate <path> --reviewer <id> --reviewed-at <UTC> --rationale <text>
|
|
163
|
+
create the configured reviewed baseline without overwriting
|
|
164
|
+
baseline canary prove regression, improvement, compatibility, and known-bad behavior
|
|
165
|
+
visual inspect --candidate <path>
|
|
166
|
+
validate and render an unapproved visual bundle
|
|
167
|
+
visual diff --candidate <path> [--baseline <path>]
|
|
168
|
+
compare visual manifests checkpoint by checkpoint
|
|
169
|
+
visual promote --candidate <path> --reviewer <id> --reviewed-at <UTC> --rationale <text>
|
|
170
|
+
create a reviewed visual baseline without overwriting
|
|
171
|
+
visual canary prove visual change and fail-closed behavior
|
|
172
|
+
catalog [--strict] validate the use-case catalog and list unmapped surfaces
|
|
173
|
+
patterns scan compare UI patterns with the baseline
|
|
174
|
+
patterns update rewrite the baseline; refuses growth without --accept-growth
|
|
175
|
+
patterns init create the baseline when none exists
|
|
176
|
+
patterns canary prove a known-bad file is rejected
|
|
177
|
+
|
|
178
|
+
options:
|
|
179
|
+
--root <dir> repository root (default: current directory)
|
|
180
|
+
--base <ref> load policy and baseline from a trusted git revision
|
|
181
|
+
--typescript <path> TypeScript package path for discovery only
|
|
182
|
+
--shards <path> candidate shard directory for authoritative regeneration
|
|
183
|
+
--visuals <path> candidate visual bundle for authoritative validation
|
|
184
|
+
--candidate-sha <sha> candidate commit expected in authoritative shards
|
|
185
|
+
--project <id> expected journey project (default: chromium)
|
|
186
|
+
--browser <name> expected browser name (default: chromium)
|
|
187
|
+
--profile <name> expected browser profile (default: Desktop Chrome)
|
|
188
|
+
--repetitions <count> expected stable repetitions (default: 3)
|
|
189
|
+
--candidate <path> discovery lifecycle artifact (repository-relative)
|
|
190
|
+
--baseline <path> reviewed baseline used by baseline diff
|
|
191
|
+
--reviewer <id> explicit baseline approval identity
|
|
192
|
+
--reviewed-at <UTC> explicit deterministic approval timestamp
|
|
193
|
+
--rationale <text> explicit baseline approval rationale
|
|
194
|
+
--format json|markdown
|
|
195
|
+
machine-readable output, or Markdown for report
|
|
196
|
+
`;
|
|
197
|
+
|
|
198
|
+
export async function main(argv, io = { out: process.stdout, err: process.stderr }) {
|
|
199
|
+
let options;
|
|
200
|
+
try {
|
|
201
|
+
options = parseArgs(argv);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
writeCliError(error, argv, io);
|
|
204
|
+
return 2;
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
return run(options, io);
|
|
208
|
+
} catch (error) {
|
|
209
|
+
if (error instanceof AuthoritativeUpgradeError) {
|
|
210
|
+
io.err.write(`ux-guard: upgrade required: ${error.message}\n`);
|
|
211
|
+
return 3;
|
|
212
|
+
}
|
|
213
|
+
if (
|
|
214
|
+
error instanceof ConfigError ||
|
|
215
|
+
error instanceof JourneyBaselineError ||
|
|
216
|
+
error instanceof OwnerTargetsError ||
|
|
217
|
+
error instanceof VisualContractError
|
|
218
|
+
) {
|
|
219
|
+
writeCliError(error, argv, io);
|
|
220
|
+
return 2;
|
|
221
|
+
}
|
|
222
|
+
throw error;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function writeCliError(error, argv, io) {
|
|
227
|
+
const command = ['setup', 'check', 'report'].find((name) => argv.includes(name));
|
|
228
|
+
const guidance = errorGuidance(command, error.message);
|
|
229
|
+
if (
|
|
230
|
+
command === 'setup' &&
|
|
231
|
+
argv.some((value, index) => value === '--format' && argv[index + 1] === 'json')
|
|
232
|
+
) {
|
|
233
|
+
io.err.write(
|
|
234
|
+
`${JSON.stringify(
|
|
235
|
+
{
|
|
236
|
+
kind: 'ux-guard-setup-error',
|
|
237
|
+
version: 1,
|
|
238
|
+
command: 'setup',
|
|
239
|
+
status: 'error',
|
|
240
|
+
exitCode: 2,
|
|
241
|
+
error: error.message,
|
|
242
|
+
nextAction: guidance,
|
|
243
|
+
},
|
|
244
|
+
null,
|
|
245
|
+
2,
|
|
246
|
+
)}\n`,
|
|
247
|
+
);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
io.err.write(`ux-guard: ${error.message}\n${guidance}\n`);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function errorGuidance(command, message) {
|
|
254
|
+
if (command === 'setup') {
|
|
255
|
+
if (message.includes(`${CONFIG_FILE} already exists`)) {
|
|
256
|
+
return 'Next: run ux-guard check. Setup will not replace the repository’s active policy.';
|
|
257
|
+
}
|
|
258
|
+
if (message.includes('already exists; refusing to overwrite')) {
|
|
259
|
+
return 'Next: review the existing setup proposal, or deliberately choose a different repository-local proposal path.';
|
|
260
|
+
}
|
|
261
|
+
return 'Next: fix the reported input or choose a new proposal path, then run ux-guard setup again. Setup never overwrites files.';
|
|
262
|
+
}
|
|
263
|
+
if (command === 'check') {
|
|
264
|
+
if (message.includes(CONFIG_FILE)) {
|
|
265
|
+
return 'Next: review and explicitly activate the setup proposal. If no proposal exists, run ux-guard setup to create one safely.';
|
|
266
|
+
}
|
|
267
|
+
return 'Next: run ux-guard check --help, correct the reported input, and retry.';
|
|
268
|
+
}
|
|
269
|
+
if (command === 'report') {
|
|
270
|
+
if (message.includes(CONFIG_FILE)) {
|
|
271
|
+
return 'Next: review and activate a setup proposal, then configure reviewed journey evidence. If no proposal exists, run ux-guard setup. Reporting remains unavailable until those reviews are complete.';
|
|
272
|
+
}
|
|
273
|
+
if (message.includes('journey reporting is not configured')) {
|
|
274
|
+
return 'Next: keep using ux-guard check, and configure reviewed journey evidence before asking for a report.';
|
|
275
|
+
}
|
|
276
|
+
return 'Next: run ux-guard report --help, correct the reported evidence or policy input, and retry.';
|
|
277
|
+
}
|
|
278
|
+
return 'Next: run ux-guard --help for the primary workflow or ux-guard help advanced for reference commands.';
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function parseArgs(argv) {
|
|
282
|
+
const options = {
|
|
283
|
+
positional: [],
|
|
284
|
+
root: process.cwd(),
|
|
285
|
+
base: null,
|
|
286
|
+
typescript: null,
|
|
287
|
+
format: 'text',
|
|
288
|
+
strict: false,
|
|
289
|
+
acceptGrowth: false,
|
|
290
|
+
candidate: null,
|
|
291
|
+
baseline: null,
|
|
292
|
+
reviewer: null,
|
|
293
|
+
reviewedAt: null,
|
|
294
|
+
rationale: null,
|
|
295
|
+
proposal: DEFAULT_SETUP_PROPOSAL,
|
|
296
|
+
proposalExplicit: false,
|
|
297
|
+
shards: null,
|
|
298
|
+
visuals: null,
|
|
299
|
+
candidateSha: null,
|
|
300
|
+
project: 'chromium',
|
|
301
|
+
browser: 'chromium',
|
|
302
|
+
profile: 'Desktop Chrome',
|
|
303
|
+
repetitions: 3,
|
|
304
|
+
help: false,
|
|
305
|
+
};
|
|
306
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
307
|
+
const arg = argv[index];
|
|
308
|
+
const value = () => {
|
|
309
|
+
const next = argv[index + 1];
|
|
310
|
+
if (next === undefined || next.startsWith('--')) throw new Error(`${arg} needs a value`);
|
|
311
|
+
index += 1;
|
|
312
|
+
return next;
|
|
313
|
+
};
|
|
314
|
+
if (arg === '--root') options.root = resolve(value());
|
|
315
|
+
else if (arg === '--base') options.base = value();
|
|
316
|
+
else if (arg === '--typescript') options.typescript = value();
|
|
317
|
+
else if (arg === '--candidate') options.candidate = value();
|
|
318
|
+
else if (arg === '--baseline') options.baseline = value();
|
|
319
|
+
else if (arg === '--reviewer') options.reviewer = value();
|
|
320
|
+
else if (arg === '--reviewed-at') options.reviewedAt = value();
|
|
321
|
+
else if (arg === '--rationale') options.rationale = value();
|
|
322
|
+
else if (arg === '--proposal') {
|
|
323
|
+
options.proposal = value();
|
|
324
|
+
options.proposalExplicit = true;
|
|
325
|
+
}
|
|
326
|
+
else if (arg === '--shards') options.shards = value();
|
|
327
|
+
else if (arg === '--visuals') options.visuals = value();
|
|
328
|
+
else if (arg === '--candidate-sha') options.candidateSha = value();
|
|
329
|
+
else if (arg === '--project') options.project = value();
|
|
330
|
+
else if (arg === '--browser') options.browser = value();
|
|
331
|
+
else if (arg === '--profile') options.profile = value();
|
|
332
|
+
else if (arg === '--repetitions') {
|
|
333
|
+
const parsed = Number(value());
|
|
334
|
+
if (!Number.isSafeInteger(parsed) || parsed < 1) {
|
|
335
|
+
throw new Error('--repetitions must be a positive integer');
|
|
336
|
+
}
|
|
337
|
+
options.repetitions = parsed;
|
|
338
|
+
}
|
|
339
|
+
else if (arg === '--format') options.format = value();
|
|
340
|
+
else if (arg === '--help' || arg === '-h') options.help = true;
|
|
341
|
+
else if (arg === '--strict') options.strict = true;
|
|
342
|
+
else if (arg === '--accept-growth') options.acceptGrowth = true;
|
|
343
|
+
else if (arg.startsWith('--')) throw new Error(`unknown option ${arg}`);
|
|
344
|
+
else options.positional.push(arg);
|
|
345
|
+
}
|
|
346
|
+
if (!['text', 'json', 'markdown'].includes(options.format)) {
|
|
347
|
+
throw new Error('--format must be text, json, or markdown');
|
|
348
|
+
}
|
|
349
|
+
return options;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function run(options, io) {
|
|
353
|
+
const [command, sub, ...extra] = options.positional;
|
|
354
|
+
if (extra.length) throw new ConfigError(`unexpected argument ${extra[0]}`);
|
|
355
|
+
if (command === 'help') {
|
|
356
|
+
if (sub === 'advanced') io.out.write(ADVANCED_USAGE);
|
|
357
|
+
else if (sub === 'setup') io.out.write(SETUP_HELP);
|
|
358
|
+
else if (sub === 'check') io.out.write(CHECK_HELP);
|
|
359
|
+
else if (sub === 'report') io.out.write(REPORT_HELP);
|
|
360
|
+
else if (sub) throw new ConfigError(`unknown help topic ${sub}`);
|
|
361
|
+
else io.out.write(USAGE);
|
|
362
|
+
return 0;
|
|
363
|
+
}
|
|
364
|
+
if (options.help) {
|
|
365
|
+
if (!command) io.out.write(USAGE);
|
|
366
|
+
else if (command === 'setup') io.out.write(SETUP_HELP);
|
|
367
|
+
else if (command === 'check') io.out.write(CHECK_HELP);
|
|
368
|
+
else if (command === 'report') io.out.write(REPORT_HELP);
|
|
369
|
+
else io.out.write(ADVANCED_USAGE);
|
|
370
|
+
return 0;
|
|
371
|
+
}
|
|
372
|
+
if (command === 'setup') return setupCommand(options, io);
|
|
373
|
+
if (options.proposalExplicit) {
|
|
374
|
+
throw new ConfigError('--proposal is only valid with setup');
|
|
375
|
+
}
|
|
376
|
+
if (command === 'discover') return discoverCommand(options, io);
|
|
377
|
+
if (command === 'baseline') return baselineCommand(options, io);
|
|
378
|
+
if (command === 'visual') return visualCommand(options, io);
|
|
379
|
+
if (command === 'authoritative') return authoritativeCommand(options, io);
|
|
380
|
+
if (!['catalog', 'patterns', 'check', 'report'].includes(command)) {
|
|
381
|
+
throw new ConfigError(`unknown command ${command ?? '(none)'}\n${USAGE}`);
|
|
382
|
+
}
|
|
383
|
+
if ((command === 'catalog' || command === 'check' || command === 'report') && sub) {
|
|
384
|
+
throw new ConfigError(`unexpected argument ${sub}`);
|
|
385
|
+
}
|
|
386
|
+
if (options.typescript) {
|
|
387
|
+
throw new ConfigError('--typescript is only valid with discover');
|
|
388
|
+
}
|
|
389
|
+
if (options.format === 'markdown' && command !== 'report') {
|
|
390
|
+
throw new ConfigError('--format markdown is only valid with report or baseline inspect/diff');
|
|
391
|
+
}
|
|
392
|
+
if (options.candidate) throw new ConfigError('--candidate is only valid with discover');
|
|
393
|
+
if (options.baseline) throw new ConfigError('--baseline is only valid with baseline diff');
|
|
394
|
+
if (options.reviewer || options.reviewedAt || options.rationale) {
|
|
395
|
+
throw new ConfigError('approval options are only valid with baseline promote');
|
|
396
|
+
}
|
|
397
|
+
if (options.strict && command !== 'catalog' && command !== 'check') {
|
|
398
|
+
throw new ConfigError('--strict is only valid with catalog or check');
|
|
399
|
+
}
|
|
400
|
+
if (options.acceptGrowth && !(command === 'patterns' && sub === 'update')) {
|
|
401
|
+
throw new ConfigError('--accept-growth is only valid with patterns update');
|
|
402
|
+
}
|
|
403
|
+
if (
|
|
404
|
+
options.shards ||
|
|
405
|
+
options.visuals ||
|
|
406
|
+
options.candidateSha ||
|
|
407
|
+
options.project !== 'chromium' ||
|
|
408
|
+
options.browser !== 'chromium' ||
|
|
409
|
+
options.profile !== 'Desktop Chrome' ||
|
|
410
|
+
options.repetitions !== 3
|
|
411
|
+
) {
|
|
412
|
+
throw new ConfigError('authoritative run options are only valid with authoritative');
|
|
413
|
+
}
|
|
414
|
+
const trust = resolveTrustedRevision(options.root, options.base);
|
|
415
|
+
const config = loadConfig(options.root, trust);
|
|
416
|
+
const ts = loadTypeScript();
|
|
417
|
+
switch (command) {
|
|
418
|
+
case 'catalog':
|
|
419
|
+
return catalogCommand(ts, config, trust, options, io);
|
|
420
|
+
case 'patterns':
|
|
421
|
+
return patternsCommand(sub, ts, config, trust, options, io);
|
|
422
|
+
case 'check':
|
|
423
|
+
return checkCommand(ts, config, trust, options, io);
|
|
424
|
+
case 'report':
|
|
425
|
+
return reportCommand(ts, config, trust, options, io);
|
|
426
|
+
}
|
|
427
|
+
throw new ConfigError(`unknown command ${command}`);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function authoritativeCommand(options, io) {
|
|
431
|
+
const [, sub] = options.positional;
|
|
432
|
+
if (sub) throw new ConfigError(`unexpected argument ${sub}`);
|
|
433
|
+
if (options.format !== 'json') {
|
|
434
|
+
throw new ConfigError('authoritative requires --format json');
|
|
435
|
+
}
|
|
436
|
+
if (
|
|
437
|
+
options.typescript ||
|
|
438
|
+
options.strict ||
|
|
439
|
+
options.acceptGrowth ||
|
|
440
|
+
options.candidate ||
|
|
441
|
+
options.baseline ||
|
|
442
|
+
options.reviewer ||
|
|
443
|
+
options.reviewedAt ||
|
|
444
|
+
options.rationale
|
|
445
|
+
) {
|
|
446
|
+
throw new ConfigError('authoritative accepts only trusted run options');
|
|
447
|
+
}
|
|
448
|
+
try {
|
|
449
|
+
const bundle = createAuthoritativeAdvisory({
|
|
450
|
+
root: options.root,
|
|
451
|
+
base: options.base,
|
|
452
|
+
candidate: options.candidateSha,
|
|
453
|
+
shards: options.shards,
|
|
454
|
+
visuals: options.visuals,
|
|
455
|
+
project: options.project,
|
|
456
|
+
browser: options.browser,
|
|
457
|
+
profile: options.profile,
|
|
458
|
+
repetitions: options.repetitions,
|
|
459
|
+
});
|
|
460
|
+
io.out.write(`${JSON.stringify(bundle, null, 2)}\n`);
|
|
461
|
+
return 0;
|
|
462
|
+
} catch (error) {
|
|
463
|
+
if (error instanceof AuthoritativeUpgradeError) throw error;
|
|
464
|
+
if (error instanceof ConfigError) throw error;
|
|
465
|
+
throw new ConfigError(`authoritative advisory: ${error.message}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function setupCommand(options, io) {
|
|
470
|
+
const [, sub] = options.positional;
|
|
471
|
+
if (sub) throw new ConfigError(`unexpected argument ${sub}`);
|
|
472
|
+
if (options.base) throw new ConfigError('setup inspects the working tree; do not pass --base');
|
|
473
|
+
if (options.format === 'markdown') {
|
|
474
|
+
throw new ConfigError('--format markdown is not valid with setup; use text or json');
|
|
475
|
+
}
|
|
476
|
+
if (
|
|
477
|
+
options.strict ||
|
|
478
|
+
options.acceptGrowth ||
|
|
479
|
+
options.candidate ||
|
|
480
|
+
options.baseline ||
|
|
481
|
+
options.reviewer ||
|
|
482
|
+
options.reviewedAt ||
|
|
483
|
+
options.rationale ||
|
|
484
|
+
options.shards ||
|
|
485
|
+
options.visuals ||
|
|
486
|
+
options.candidateSha ||
|
|
487
|
+
options.project !== 'chromium' ||
|
|
488
|
+
options.browser !== 'chromium' ||
|
|
489
|
+
options.profile !== 'Desktop Chrome' ||
|
|
490
|
+
options.repetitions !== 3
|
|
491
|
+
) {
|
|
492
|
+
throw new ConfigError('setup accepts only --root, --proposal, --format, and --typescript');
|
|
493
|
+
}
|
|
494
|
+
if (existsSync(join(options.root, CONFIG_FILE))) {
|
|
495
|
+
throw new ConfigError(
|
|
496
|
+
`${CONFIG_FILE} already exists; setup never replaces active policy. Run ux-guard check instead`,
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
let report;
|
|
501
|
+
try {
|
|
502
|
+
report = discoverProject(options.root, options.typescript);
|
|
503
|
+
} catch (error) {
|
|
504
|
+
if (
|
|
505
|
+
error instanceof ConfigError &&
|
|
506
|
+
[
|
|
507
|
+
'discover found no tsconfig files',
|
|
508
|
+
'discover found no non-test JSX or TSX source files',
|
|
509
|
+
].includes(error.message)
|
|
510
|
+
) {
|
|
511
|
+
const result = setupEvidenceGap(options.proposal, error.message);
|
|
512
|
+
writeSetupResult(result, options.format, io);
|
|
513
|
+
return 0;
|
|
514
|
+
}
|
|
515
|
+
throw error;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
const proposal = setupProposalFromDiscovery(report);
|
|
519
|
+
writeCandidate(options.root, options.proposal, proposal, 'setup proposal');
|
|
520
|
+
const result = setupResult(report, options.proposal);
|
|
521
|
+
writeSetupResult(result, options.format, io);
|
|
522
|
+
return 0;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function setupResult(report, proposalPath) {
|
|
526
|
+
const assessment = assessSetup(report);
|
|
527
|
+
return {
|
|
528
|
+
kind: 'ux-guard-setup-result',
|
|
529
|
+
version: 1,
|
|
530
|
+
command: 'setup',
|
|
531
|
+
status: 'review-required',
|
|
532
|
+
exitCode: 0,
|
|
533
|
+
inspected: {
|
|
534
|
+
status: 'complete',
|
|
535
|
+
uiSourceFiles: report.observed.files,
|
|
536
|
+
sourceRoots: report.observed.sourceRoots.map(
|
|
537
|
+
({ path, productionFiles }) => ({ path, uiSourceFiles: productionFiles }),
|
|
538
|
+
),
|
|
539
|
+
tsconfigs: report.inputs.tsconfigs,
|
|
540
|
+
componentLibraries: report.observed.componentLibraries.map(({ module }) => module),
|
|
541
|
+
iconLibraries: report.observed.iconLibraries.map(({ module }) => module),
|
|
542
|
+
signals: ['component use', 'control props', 'icons', 'styling', 'pattern debt'],
|
|
543
|
+
},
|
|
544
|
+
results: assessment,
|
|
545
|
+
proposal: {
|
|
546
|
+
status: 'unapproved',
|
|
547
|
+
path: proposalPath,
|
|
548
|
+
written: true,
|
|
549
|
+
},
|
|
550
|
+
safety: {
|
|
551
|
+
observedUsageApproved: false,
|
|
552
|
+
policyCreated: false,
|
|
553
|
+
baselineCreated: false,
|
|
554
|
+
waiversCreated: false,
|
|
555
|
+
enforcementEnabled: false,
|
|
556
|
+
filesWritten: [proposalPath],
|
|
557
|
+
},
|
|
558
|
+
nextAction:
|
|
559
|
+
`Review ${proposalPath} with the people responsible for product, design, and engineering. ` +
|
|
560
|
+
'Choose the missing policy and catalog decisions, then require explicit human approval before activation.',
|
|
561
|
+
};
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
function setupEvidenceGap(proposalPath, reason) {
|
|
565
|
+
return {
|
|
566
|
+
kind: 'ux-guard-setup-result',
|
|
567
|
+
version: 1,
|
|
568
|
+
command: 'setup',
|
|
569
|
+
status: 'insufficient-evidence',
|
|
570
|
+
exitCode: 0,
|
|
571
|
+
inspected: {
|
|
572
|
+
status: 'incomplete',
|
|
573
|
+
uiSourceFiles: 0,
|
|
574
|
+
sourceRoots: [],
|
|
575
|
+
tsconfigs: [],
|
|
576
|
+
componentLibraries: [],
|
|
577
|
+
iconLibraries: [],
|
|
578
|
+
signals: [],
|
|
579
|
+
},
|
|
580
|
+
results: {
|
|
581
|
+
good: [],
|
|
582
|
+
needsAttention: [],
|
|
583
|
+
unknown: [
|
|
584
|
+
reason,
|
|
585
|
+
'UX conventions, workflow coverage, and regression from main could not be evaluated.',
|
|
586
|
+
],
|
|
587
|
+
},
|
|
588
|
+
proposal: {
|
|
589
|
+
status: 'not-created',
|
|
590
|
+
path: proposalPath,
|
|
591
|
+
written: false,
|
|
592
|
+
},
|
|
593
|
+
safety: {
|
|
594
|
+
observedUsageApproved: false,
|
|
595
|
+
policyCreated: false,
|
|
596
|
+
baselineCreated: false,
|
|
597
|
+
waiversCreated: false,
|
|
598
|
+
enforcementEnabled: false,
|
|
599
|
+
filesWritten: [],
|
|
600
|
+
},
|
|
601
|
+
nextAction:
|
|
602
|
+
'Add or point ux-guard to a TypeScript UI project with non-test JSX or TSX source, then run ux-guard setup again.',
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
function assessSetup(report) {
|
|
607
|
+
const labels = {
|
|
608
|
+
'inline-style': 'inline styles',
|
|
609
|
+
'hard-coded-color': 'hard-coded colors',
|
|
610
|
+
'px-literal': 'pixel literals',
|
|
611
|
+
'raw-button': 'raw button elements',
|
|
612
|
+
'icon-library': 'icon-library uses',
|
|
613
|
+
};
|
|
614
|
+
const good = [`Inspected ${report.observed.files} non-test UI source files.`];
|
|
615
|
+
const needsAttention = [];
|
|
616
|
+
for (const rule of DEBT_RULES) {
|
|
617
|
+
const locations = Object.entries(report.observed.debt[rule])
|
|
618
|
+
.map(([path, count]) => ({ path, count }))
|
|
619
|
+
.sort((left, right) => left.path.localeCompare(right.path));
|
|
620
|
+
const count = locations.reduce((sum, item) => sum + item.count, 0);
|
|
621
|
+
if (count === 0) {
|
|
622
|
+
good.push(`No ${labels[rule]} were observed in the inspected files.`);
|
|
623
|
+
} else {
|
|
624
|
+
needsAttention.push({
|
|
625
|
+
signal: rule,
|
|
626
|
+
summary: `${count} ${labels[rule]} observed; review before deciding policy.`,
|
|
627
|
+
count,
|
|
628
|
+
locations,
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
const unknown = [
|
|
633
|
+
'Observed conventions have not been approved as policy.',
|
|
634
|
+
'No use-case catalog was inspected, so workflow coverage is unknown.',
|
|
635
|
+
'No trusted baseline was used, so regression from main is unknown.',
|
|
636
|
+
'Enforcement is not enabled.',
|
|
637
|
+
];
|
|
638
|
+
for (const diagnostic of report.inputs.diagnostics) {
|
|
639
|
+
unknown.push(`${diagnostic.tsconfig}: ${diagnostic.message}`);
|
|
640
|
+
}
|
|
641
|
+
return { good, needsAttention, unknown };
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function writeSetupResult(result, format, io) {
|
|
645
|
+
if (format === 'json') {
|
|
646
|
+
io.out.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
const lines = ['ux-guard setup', '', 'Inspected'];
|
|
650
|
+
if (result.inspected.status === 'complete') {
|
|
651
|
+
lines.push(
|
|
652
|
+
` ${result.inspected.uiSourceFiles} non-test UI source files across ${result.inspected.sourceRoots.length} source roots.`,
|
|
653
|
+
` TypeScript projects: ${result.inspected.tsconfigs.join(', ') || '(none)'}.`,
|
|
654
|
+
` Component libraries: ${result.inspected.componentLibraries.join(', ') || '(none observed)'}.`,
|
|
655
|
+
` Icon libraries: ${result.inspected.iconLibraries.join(', ') || '(none observed)'}.`,
|
|
656
|
+
` Signals: ${result.inspected.signals.join(', ')}.`,
|
|
657
|
+
);
|
|
658
|
+
} else {
|
|
659
|
+
lines.push(' No executable UI source evidence was available.');
|
|
660
|
+
}
|
|
661
|
+
lines.push('', 'Good');
|
|
662
|
+
if (result.results.good.length === 0) {
|
|
663
|
+
lines.push(' Nothing can be confirmed from the available evidence.');
|
|
664
|
+
} else {
|
|
665
|
+
for (const item of result.results.good) lines.push(` - ${item}`);
|
|
666
|
+
}
|
|
667
|
+
lines.push('', 'Needs attention');
|
|
668
|
+
if (result.results.needsAttention.length === 0) {
|
|
669
|
+
lines.push(' No potential pattern problems were observed in the inspected files.');
|
|
670
|
+
} else {
|
|
671
|
+
for (const item of result.results.needsAttention) {
|
|
672
|
+
lines.push(` - ${item.summary}`);
|
|
673
|
+
for (const location of item.locations) {
|
|
674
|
+
lines.push(` ${location.path}: ${location.count}`);
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
lines.push('', 'Unknown');
|
|
679
|
+
for (const item of result.results.unknown) lines.push(` - ${item}`);
|
|
680
|
+
lines.push('', 'Setup proposal');
|
|
681
|
+
if (result.proposal.written) {
|
|
682
|
+
lines.push(
|
|
683
|
+
` Saved ${result.proposal.path}.`,
|
|
684
|
+
' Status: unapproved. It records observations and decisions still needing human review.',
|
|
685
|
+
);
|
|
686
|
+
} else {
|
|
687
|
+
lines.push(` Not created at ${result.proposal.path}; there was not enough UI evidence.`);
|
|
688
|
+
}
|
|
689
|
+
lines.push(
|
|
690
|
+
'',
|
|
691
|
+
'Safety',
|
|
692
|
+
' No active policy, reviewed baseline, waiver, or enforcement was created.',
|
|
693
|
+
'',
|
|
694
|
+
'Next safe action',
|
|
695
|
+
` ${result.nextAction}`,
|
|
696
|
+
);
|
|
697
|
+
io.out.write(`${lines.join('\n')}\n`);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function discoverCommand(options, io) {
|
|
701
|
+
if (options.base) throw new ConfigError('discover audits the working tree; do not pass --base');
|
|
702
|
+
if (options.strict || options.acceptGrowth) throw new ConfigError('--strict and --accept-growth are not valid with discover');
|
|
703
|
+
if (options.baseline || options.reviewer || options.reviewedAt || options.rationale) {
|
|
704
|
+
throw new ConfigError('baseline approval options are not valid with discover');
|
|
705
|
+
}
|
|
706
|
+
if (options.format === 'markdown') {
|
|
707
|
+
throw new ConfigError('--format markdown is only valid with report');
|
|
708
|
+
}
|
|
709
|
+
const [, sub] = options.positional;
|
|
710
|
+
if (!sub) {
|
|
711
|
+
if (options.candidate) throw new ConfigError('--candidate requires discover write-candidate or discover promote');
|
|
712
|
+
const report = discoverProject(options.root, options.typescript);
|
|
713
|
+
if (options.format === 'json') io.out.write(`${JSON.stringify({ discovery: report }, null, 2)}\n`);
|
|
714
|
+
else io.out.write(renderDiscovery(report));
|
|
715
|
+
return 0;
|
|
716
|
+
}
|
|
717
|
+
if (sub === 'write-candidate') {
|
|
718
|
+
if (!options.candidate) throw new ConfigError('discover write-candidate requires --candidate <path>');
|
|
719
|
+
const report = discoverProject(options.root, options.typescript);
|
|
720
|
+
const artifact = candidateFromDiscovery(report);
|
|
721
|
+
writeCandidate(options.root, options.candidate, artifact);
|
|
722
|
+
const emitted = structuredClone(report);
|
|
723
|
+
emitted.approval.filesWritten = true;
|
|
724
|
+
emitted.approval.writtenArtifacts = [options.candidate];
|
|
725
|
+
if (options.format === 'json') {
|
|
726
|
+
io.out.write(
|
|
727
|
+
`${JSON.stringify(
|
|
728
|
+
{
|
|
729
|
+
discovery: emitted,
|
|
730
|
+
lifecycle: {
|
|
731
|
+
action: 'write-candidate',
|
|
732
|
+
status: 'unapproved',
|
|
733
|
+
candidate: options.candidate,
|
|
734
|
+
},
|
|
735
|
+
},
|
|
736
|
+
null,
|
|
737
|
+
2,
|
|
738
|
+
)}\n`,
|
|
739
|
+
);
|
|
740
|
+
} else {
|
|
741
|
+
io.out.write(renderDiscovery(emitted));
|
|
742
|
+
io.out.write(`ux-guard: wrote unapproved candidate ${options.candidate}; human review is required before promotion\n`);
|
|
743
|
+
}
|
|
744
|
+
return 0;
|
|
745
|
+
}
|
|
746
|
+
if (sub === 'promote') {
|
|
747
|
+
if (!options.candidate) throw new ConfigError('discover promote requires --candidate <path>');
|
|
748
|
+
if (options.typescript) throw new ConfigError('discover promote uses the TypeScript path in the reviewed candidate');
|
|
749
|
+
return promoteCandidate(options, io);
|
|
750
|
+
}
|
|
751
|
+
throw new ConfigError(`unknown discover command ${sub}`);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function baselineCommand(options, io) {
|
|
755
|
+
const [, sub] = options.positional;
|
|
756
|
+
if (!['propose', 'inspect', 'diff', 'promote', 'canary'].includes(sub)) {
|
|
757
|
+
throw new ConfigError(`unknown baseline command ${sub ?? '(none)'}`);
|
|
758
|
+
}
|
|
759
|
+
if (options.base) {
|
|
760
|
+
throw new ConfigError('baseline lifecycle commands use explicit artifacts; do not pass --base');
|
|
761
|
+
}
|
|
762
|
+
if (options.typescript || options.strict || options.acceptGrowth) {
|
|
763
|
+
throw new ConfigError('baseline commands do not accept TypeScript, strict, or growth options');
|
|
764
|
+
}
|
|
765
|
+
if (sub === 'canary') {
|
|
766
|
+
if (
|
|
767
|
+
options.candidate ||
|
|
768
|
+
options.baseline ||
|
|
769
|
+
options.reviewer ||
|
|
770
|
+
options.reviewedAt ||
|
|
771
|
+
options.rationale ||
|
|
772
|
+
options.format === 'markdown'
|
|
773
|
+
) {
|
|
774
|
+
throw new ConfigError('baseline canary does not accept artifact or approval options');
|
|
775
|
+
}
|
|
776
|
+
const canaries = runJourneyComparisonCanaries();
|
|
777
|
+
if (options.format === 'json') {
|
|
778
|
+
io.out.write(
|
|
779
|
+
`${JSON.stringify(
|
|
780
|
+
{
|
|
781
|
+
version: 1,
|
|
782
|
+
command: 'baseline canary',
|
|
783
|
+
status: 'passed',
|
|
784
|
+
exitCode: 0,
|
|
785
|
+
canaries,
|
|
786
|
+
},
|
|
787
|
+
null,
|
|
788
|
+
2,
|
|
789
|
+
)}\n`,
|
|
790
|
+
);
|
|
791
|
+
} else {
|
|
792
|
+
io.out.write(
|
|
793
|
+
'ux-guard: regression, improvement, compatibility, and known-bad comparison canaries passed\n',
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
return 0;
|
|
797
|
+
}
|
|
798
|
+
if (!options.candidate) {
|
|
799
|
+
throw new ConfigError(`baseline ${sub} requires --candidate <path>`);
|
|
800
|
+
}
|
|
801
|
+
if (options.format === 'markdown' && !['inspect', 'diff'].includes(sub)) {
|
|
802
|
+
throw new ConfigError('--format markdown is only valid with baseline inspect or diff');
|
|
803
|
+
}
|
|
804
|
+
if (options.baseline && sub !== 'diff') {
|
|
805
|
+
throw new ConfigError('--baseline is only valid with baseline diff');
|
|
806
|
+
}
|
|
807
|
+
const approvalOptions = [options.reviewer, options.reviewedAt, options.rationale];
|
|
808
|
+
if (sub === 'promote') {
|
|
809
|
+
if (approvalOptions.some((value) => !value)) {
|
|
810
|
+
throw new ConfigError(
|
|
811
|
+
'baseline promote requires --reviewer, --reviewed-at, and --rationale',
|
|
812
|
+
);
|
|
813
|
+
}
|
|
814
|
+
} else if (approvalOptions.some(Boolean)) {
|
|
815
|
+
throw new ConfigError('approval options are only valid with baseline promote');
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
const config = loadConfig(options.root, null);
|
|
819
|
+
if (!config.journeys?.baseline || !config.journeys?.report) {
|
|
820
|
+
throw new ConfigError('journey baseline and report paths are not configured');
|
|
821
|
+
}
|
|
822
|
+
if (options.candidate === config.journeys.baseline) {
|
|
823
|
+
throw new ConfigError('candidate path must differ from the reviewed baseline path');
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
if (sub === 'propose') {
|
|
827
|
+
const policy = parseJourneyPolicy(
|
|
828
|
+
readTrusted(options.root, config.journeys.policy, null),
|
|
829
|
+
);
|
|
830
|
+
const reportText = readTrusted(options.root, config.journeys.report, null);
|
|
831
|
+
const report = parseJourneyReport(reportText);
|
|
832
|
+
const candidate = createJourneyBaselineCandidate({
|
|
833
|
+
report,
|
|
834
|
+
policy,
|
|
835
|
+
reportPath: config.journeys.report,
|
|
836
|
+
reportSha256: sha256(reportText),
|
|
837
|
+
canaries: runJourneyComparisonCanaries(),
|
|
838
|
+
});
|
|
839
|
+
writeLifecycleArtifact(
|
|
840
|
+
options.root,
|
|
841
|
+
options.candidate,
|
|
842
|
+
serializeJourneyBaseline(candidate),
|
|
843
|
+
'baseline candidate',
|
|
844
|
+
);
|
|
845
|
+
if (options.format === 'json') {
|
|
846
|
+
io.out.write(
|
|
847
|
+
`${JSON.stringify(
|
|
848
|
+
{
|
|
849
|
+
version: 1,
|
|
850
|
+
command: 'baseline propose',
|
|
851
|
+
status: 'unapproved',
|
|
852
|
+
exitCode: 0,
|
|
853
|
+
candidate: options.candidate,
|
|
854
|
+
canonicalHash: candidate.canonicalHash,
|
|
855
|
+
},
|
|
856
|
+
null,
|
|
857
|
+
2,
|
|
858
|
+
)}\n`,
|
|
859
|
+
);
|
|
860
|
+
} else {
|
|
861
|
+
io.out.write(
|
|
862
|
+
`ux-guard: wrote unapproved journey baseline candidate ${options.candidate}; owner review is required before promotion\n`,
|
|
863
|
+
);
|
|
864
|
+
}
|
|
865
|
+
return 0;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
const candidate = readLifecycleBaseline(
|
|
869
|
+
options.root,
|
|
870
|
+
options.candidate,
|
|
871
|
+
'baseline candidate',
|
|
872
|
+
);
|
|
873
|
+
if (sub === 'inspect') {
|
|
874
|
+
if (options.format === 'json') io.out.write(serializeJourneyBaseline(candidate));
|
|
875
|
+
else io.out.write(renderJourneyBaselineMarkdown(candidate));
|
|
876
|
+
return 0;
|
|
877
|
+
}
|
|
878
|
+
if (sub === 'diff') {
|
|
879
|
+
const baselinePath = options.baseline ?? config.journeys.baseline;
|
|
880
|
+
let comparison;
|
|
881
|
+
if (!trustedFileExists(options.root, baselinePath, null)) {
|
|
882
|
+
comparison = unavailableJourneyComparison(
|
|
883
|
+
`reviewed baseline ${baselinePath} is not available`,
|
|
884
|
+
);
|
|
885
|
+
} else {
|
|
886
|
+
const reviewed = readLifecycleBaseline(
|
|
887
|
+
options.root,
|
|
888
|
+
baselinePath,
|
|
889
|
+
'reviewed baseline',
|
|
890
|
+
);
|
|
891
|
+
if (reviewed.approval.status !== 'reviewed') {
|
|
892
|
+
throw new ConfigError(`${baselinePath} is not a reviewed baseline`);
|
|
893
|
+
}
|
|
894
|
+
comparison = compareJourneyBaselineEnvelopes(reviewed, candidate);
|
|
895
|
+
}
|
|
896
|
+
if (options.format === 'json') io.out.write(serializeJourneyComparison(comparison));
|
|
897
|
+
else io.out.write(renderJourneyComparisonMarkdown(comparison));
|
|
898
|
+
return 0;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
const reviewed = promoteJourneyBaseline(candidate, {
|
|
902
|
+
reviewer: options.reviewer,
|
|
903
|
+
reviewedAt: options.reviewedAt,
|
|
904
|
+
rationale: options.rationale,
|
|
905
|
+
});
|
|
906
|
+
if (existsSync(join(options.root, config.journeys.baseline))) {
|
|
907
|
+
throw new ConfigError(
|
|
908
|
+
`${config.journeys.baseline} already exists; promotion never overwrites a reviewed baseline`,
|
|
909
|
+
);
|
|
910
|
+
}
|
|
911
|
+
writeLifecycleArtifact(
|
|
912
|
+
options.root,
|
|
913
|
+
config.journeys.baseline,
|
|
914
|
+
serializeJourneyBaseline(reviewed),
|
|
915
|
+
'reviewed baseline',
|
|
916
|
+
);
|
|
917
|
+
if (options.format === 'json') {
|
|
918
|
+
io.out.write(
|
|
919
|
+
`${JSON.stringify(
|
|
920
|
+
{
|
|
921
|
+
version: 1,
|
|
922
|
+
command: 'baseline promote',
|
|
923
|
+
status: 'reviewed',
|
|
924
|
+
exitCode: 0,
|
|
925
|
+
source: options.candidate,
|
|
926
|
+
baseline: config.journeys.baseline,
|
|
927
|
+
canonicalHash: reviewed.canonicalHash,
|
|
928
|
+
reviewer: reviewed.approval.reviewer,
|
|
929
|
+
},
|
|
930
|
+
null,
|
|
931
|
+
2,
|
|
932
|
+
)}\n`,
|
|
933
|
+
);
|
|
934
|
+
} else {
|
|
935
|
+
io.out.write(
|
|
936
|
+
`ux-guard: promoted ${options.candidate}; created reviewed baseline ${config.journeys.baseline}\n`,
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
return 0;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
function visualCommand(options, io) {
|
|
943
|
+
const [, sub] = options.positional;
|
|
944
|
+
if (!['inspect', 'diff', 'promote', 'canary'].includes(sub)) {
|
|
945
|
+
throw new ConfigError(`unknown visual command ${sub ?? '(none)'}`);
|
|
946
|
+
}
|
|
947
|
+
if (options.base || options.typescript || options.strict || options.acceptGrowth) {
|
|
948
|
+
throw new ConfigError('visual lifecycle commands use explicit local artifacts');
|
|
949
|
+
}
|
|
950
|
+
if (sub === 'canary') {
|
|
951
|
+
if (
|
|
952
|
+
options.candidate ||
|
|
953
|
+
options.baseline ||
|
|
954
|
+
options.reviewer ||
|
|
955
|
+
options.reviewedAt ||
|
|
956
|
+
options.rationale ||
|
|
957
|
+
options.format === 'markdown'
|
|
958
|
+
) {
|
|
959
|
+
throw new ConfigError('visual canary does not accept artifact or approval options');
|
|
960
|
+
}
|
|
961
|
+
const canaries = runVisualComparisonCanaries();
|
|
962
|
+
if (options.format === 'json') {
|
|
963
|
+
io.out.write(
|
|
964
|
+
`${JSON.stringify(
|
|
965
|
+
{
|
|
966
|
+
version: 1,
|
|
967
|
+
command: 'visual canary',
|
|
968
|
+
status: 'passed',
|
|
969
|
+
exitCode: 0,
|
|
970
|
+
canaries,
|
|
971
|
+
},
|
|
972
|
+
null,
|
|
973
|
+
2,
|
|
974
|
+
)}\n`,
|
|
975
|
+
);
|
|
976
|
+
} else {
|
|
977
|
+
io.out.write(
|
|
978
|
+
'ux-guard: one-pixel, missing-checkpoint, profile-mismatch, and fail-closed visual canaries passed\n',
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
return 0;
|
|
982
|
+
}
|
|
983
|
+
if (!options.candidate) {
|
|
984
|
+
throw new ConfigError(`visual ${sub} requires --candidate <manifest>`);
|
|
985
|
+
}
|
|
986
|
+
if (options.format === 'markdown' && !['inspect', 'diff'].includes(sub)) {
|
|
987
|
+
throw new ConfigError('--format markdown is only valid with visual inspect or diff');
|
|
988
|
+
}
|
|
989
|
+
if (options.baseline && sub !== 'diff') {
|
|
990
|
+
throw new ConfigError('--baseline is only valid with visual diff');
|
|
991
|
+
}
|
|
992
|
+
const approvals = [options.reviewer, options.reviewedAt, options.rationale];
|
|
993
|
+
if (sub === 'promote') {
|
|
994
|
+
if (approvals.some((value) => !value)) {
|
|
995
|
+
throw new ConfigError(
|
|
996
|
+
'visual promote requires --reviewer, --reviewed-at, and --rationale',
|
|
997
|
+
);
|
|
998
|
+
}
|
|
999
|
+
} else if (approvals.some(Boolean)) {
|
|
1000
|
+
throw new ConfigError('approval options are only valid with visual promote');
|
|
1001
|
+
}
|
|
1002
|
+
const config = loadConfig(options.root, null);
|
|
1003
|
+
if (!config.visuals) throw new ConfigError('visual policy is not configured');
|
|
1004
|
+
const profileSet = parseVisualProfileSet(
|
|
1005
|
+
readTrusted(options.root, config.visuals.profiles, null),
|
|
1006
|
+
);
|
|
1007
|
+
const checkpointPolicy = parseVisualCheckpointPolicy(
|
|
1008
|
+
readTrusted(options.root, config.visuals.checkpoints, null),
|
|
1009
|
+
);
|
|
1010
|
+
const candidate = readLocalVisualManifest(
|
|
1011
|
+
options.root,
|
|
1012
|
+
options.candidate,
|
|
1013
|
+
profileSet,
|
|
1014
|
+
checkpointPolicy,
|
|
1015
|
+
);
|
|
1016
|
+
if (sub === 'inspect') {
|
|
1017
|
+
if (options.format === 'json') {
|
|
1018
|
+
io.out.write(`${JSON.stringify(candidate, null, 2)}\n`);
|
|
1019
|
+
} else {
|
|
1020
|
+
io.out.write(renderVisualManifestMarkdown(candidate));
|
|
1021
|
+
}
|
|
1022
|
+
return 0;
|
|
1023
|
+
}
|
|
1024
|
+
if (sub === 'diff') {
|
|
1025
|
+
const baselinePath = options.baseline ?? config.visuals.baseline;
|
|
1026
|
+
let comparison;
|
|
1027
|
+
if (!existsSync(join(options.root, baselinePath))) {
|
|
1028
|
+
comparison = unavailableVisualComparison(
|
|
1029
|
+
`reviewed visual baseline ${baselinePath} is not available`,
|
|
1030
|
+
candidate,
|
|
1031
|
+
);
|
|
1032
|
+
} else {
|
|
1033
|
+
const baseline = readLocalVisualManifest(
|
|
1034
|
+
options.root,
|
|
1035
|
+
baselinePath,
|
|
1036
|
+
profileSet,
|
|
1037
|
+
checkpointPolicy,
|
|
1038
|
+
);
|
|
1039
|
+
comparison = compareVisualManifests(baseline, candidate);
|
|
1040
|
+
}
|
|
1041
|
+
if (options.format === 'json') {
|
|
1042
|
+
io.out.write(`${JSON.stringify(comparison, null, 2)}\n`);
|
|
1043
|
+
} else {
|
|
1044
|
+
io.out.write(renderVisualComparisonMarkdown(comparison));
|
|
1045
|
+
}
|
|
1046
|
+
return 0;
|
|
1047
|
+
}
|
|
1048
|
+
const promoted = promoteVisualBundle({
|
|
1049
|
+
root: options.root,
|
|
1050
|
+
candidatePath: options.candidate,
|
|
1051
|
+
baselinePath: config.visuals.baseline,
|
|
1052
|
+
reviewer: options.reviewer,
|
|
1053
|
+
reviewedAt: options.reviewedAt,
|
|
1054
|
+
rationale: options.rationale,
|
|
1055
|
+
profileSet,
|
|
1056
|
+
checkpointPolicy,
|
|
1057
|
+
});
|
|
1058
|
+
if (options.format === 'json') {
|
|
1059
|
+
io.out.write(
|
|
1060
|
+
`${JSON.stringify(
|
|
1061
|
+
{
|
|
1062
|
+
version: 1,
|
|
1063
|
+
command: 'visual promote',
|
|
1064
|
+
status: 'reviewed',
|
|
1065
|
+
exitCode: 0,
|
|
1066
|
+
source: options.candidate,
|
|
1067
|
+
baseline: config.visuals.baseline,
|
|
1068
|
+
canonicalHash: promoted.canonicalHash,
|
|
1069
|
+
reviewer: promoted.approval.reviewer,
|
|
1070
|
+
},
|
|
1071
|
+
null,
|
|
1072
|
+
2,
|
|
1073
|
+
)}\n`,
|
|
1074
|
+
);
|
|
1075
|
+
} else {
|
|
1076
|
+
io.out.write(
|
|
1077
|
+
`ux-guard: promoted ${options.candidate}; created reviewed visual baseline ${config.visuals.baseline}\n`,
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
return 0;
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
function readLocalVisualManifest(root, path, profileSet, checkpointPolicy) {
|
|
1084
|
+
const full = resolveRepositoryPath(root, path, 'visual manifest path');
|
|
1085
|
+
ensureLocalDirectory(root, dirname(full), 'visual manifest path', false);
|
|
1086
|
+
let stats;
|
|
1087
|
+
try {
|
|
1088
|
+
stats = lstatSync(full);
|
|
1089
|
+
} catch {
|
|
1090
|
+
throw new ConfigError(`visual manifest ${path} does not exist`);
|
|
1091
|
+
}
|
|
1092
|
+
if (stats.isSymbolicLink() || !stats.isFile()) {
|
|
1093
|
+
throw new ConfigError('visual manifest must be a regular file, not a symbolic link');
|
|
1094
|
+
}
|
|
1095
|
+
return parseVisualManifest(readFileSync(full, 'utf8'), {
|
|
1096
|
+
profileSet,
|
|
1097
|
+
checkpointPolicy,
|
|
1098
|
+
bundleRoot: dirname(full),
|
|
1099
|
+
requireFiles: true,
|
|
1100
|
+
});
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
function readLifecycleBaseline(root, path, label) {
|
|
1104
|
+
const full = resolveRepositoryPath(root, path, `${label} path`);
|
|
1105
|
+
ensureLocalDirectory(root, dirname(full), `${label} path`, false);
|
|
1106
|
+
try {
|
|
1107
|
+
const stats = lstatSync(full);
|
|
1108
|
+
if (stats.isSymbolicLink() || !stats.isFile()) {
|
|
1109
|
+
throw new ConfigError(`${label} must be a regular repository file, not a symbolic link`);
|
|
1110
|
+
}
|
|
1111
|
+
return parseJourneyBaseline(readFileSync(full, 'utf8'));
|
|
1112
|
+
} catch (error) {
|
|
1113
|
+
if (error instanceof ConfigError) throw error;
|
|
1114
|
+
if (error?.code === 'ENOENT') throw new ConfigError(`${label} ${path} does not exist`);
|
|
1115
|
+
throw new ConfigError(`${label} ${path}: ${error.message}`);
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
function writeLifecycleArtifact(root, path, text, label) {
|
|
1120
|
+
const full = resolveRepositoryPath(root, path, `${label} path`);
|
|
1121
|
+
ensureLocalDirectory(root, dirname(full), `${label} path`, true);
|
|
1122
|
+
try {
|
|
1123
|
+
writeFileSync(full, text, { flag: 'wx', mode: 0o600 });
|
|
1124
|
+
} catch (error) {
|
|
1125
|
+
if (error?.code === 'EEXIST') {
|
|
1126
|
+
throw new ConfigError(`${path} already exists; refusing to overwrite it`);
|
|
1127
|
+
}
|
|
1128
|
+
throw new ConfigError(`${label} ${path} could not be written`);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
function writeCandidate(root, path, artifact, label = 'candidate') {
|
|
1133
|
+
if (path === CONFIG_FILE) throw new ConfigError(`${label} path cannot be ${CONFIG_FILE}`);
|
|
1134
|
+
const full = resolveRepositoryPath(root, path, `${label} path`);
|
|
1135
|
+
ensureLocalDirectory(root, dirname(full), `${label} path`, true);
|
|
1136
|
+
try {
|
|
1137
|
+
writeFileSync(full, `${JSON.stringify(artifact, null, 2)}\n`, { flag: 'wx' });
|
|
1138
|
+
} catch (error) {
|
|
1139
|
+
if (error?.code === 'EEXIST') throw new ConfigError(`${label} ${path} already exists; refusing to overwrite it`);
|
|
1140
|
+
throw new ConfigError(`${label} ${path} could not be written`);
|
|
1141
|
+
}
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
function promoteCandidate(options, io) {
|
|
1145
|
+
const artifact = readCandidate(options.root, options.candidate);
|
|
1146
|
+
validateCandidateApproval(artifact);
|
|
1147
|
+
const config = validateConfig(artifact.config);
|
|
1148
|
+
const baselinePath = config.patterns.baseline;
|
|
1149
|
+
if (existsSync(join(options.root, CONFIG_FILE))) {
|
|
1150
|
+
throw new ConfigError(`${CONFIG_FILE} already exists; promotion never overwrites policy`);
|
|
1151
|
+
}
|
|
1152
|
+
if (existsSync(join(options.root, baselinePath))) {
|
|
1153
|
+
throw new ConfigError(`${baselinePath} already exists; promotion never overwrites a baseline`);
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
const ts = loadTypeScript(options.root, config);
|
|
1157
|
+
const catalog = validateCatalog(loadCatalog(options.root, config), loadSurfaces(ts, options.root, config));
|
|
1158
|
+
if (catalog.inventory === 'complete' && catalog.unmapped.length > 0) {
|
|
1159
|
+
throw new ConfigError('candidate policy has a complete catalog with unmapped navigation surfaces');
|
|
1160
|
+
}
|
|
1161
|
+
const scan = scanFiles(ts, options.root, config, listSourceFiles(options.root, config));
|
|
1162
|
+
const baseline = baselineFromScan(scan);
|
|
1163
|
+
writePromotedPolicy(options.root, config, baseline);
|
|
1164
|
+
|
|
1165
|
+
const result = {
|
|
1166
|
+
candidate: options.candidate,
|
|
1167
|
+
reviewer: artifact.approval.reviewer,
|
|
1168
|
+
files: [CONFIG_FILE, baselinePath],
|
|
1169
|
+
};
|
|
1170
|
+
if (options.format === 'json') {
|
|
1171
|
+
io.out.write(
|
|
1172
|
+
`${JSON.stringify(
|
|
1173
|
+
{
|
|
1174
|
+
version: 1,
|
|
1175
|
+
command: 'discover promote',
|
|
1176
|
+
status: 'promoted',
|
|
1177
|
+
exitCode: 0,
|
|
1178
|
+
promotion: result,
|
|
1179
|
+
},
|
|
1180
|
+
null,
|
|
1181
|
+
2,
|
|
1182
|
+
)}\n`,
|
|
1183
|
+
);
|
|
1184
|
+
} else {
|
|
1185
|
+
io.out.write(`ux-guard: promoted ${options.candidate}; created ${CONFIG_FILE} and ${baselinePath}\n`);
|
|
1186
|
+
}
|
|
1187
|
+
return 0;
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
function readCandidate(root, path) {
|
|
1191
|
+
const full = resolveRepositoryPath(root, path, 'candidate path');
|
|
1192
|
+
ensureLocalDirectory(root, dirname(full), 'candidate path', false);
|
|
1193
|
+
try {
|
|
1194
|
+
if (lstatSync(full).isSymbolicLink()) throw new ConfigError('candidate path cannot be a symbolic link');
|
|
1195
|
+
} catch (error) {
|
|
1196
|
+
if (error instanceof ConfigError) throw error;
|
|
1197
|
+
throw new ConfigError(`candidate ${path} could not be read`);
|
|
1198
|
+
}
|
|
1199
|
+
let artifact;
|
|
1200
|
+
try {
|
|
1201
|
+
artifact = JSON.parse(readFileSync(full, 'utf8'));
|
|
1202
|
+
} catch (error) {
|
|
1203
|
+
if (error instanceof SyntaxError) throw new ConfigError(`candidate ${path} is not valid JSON`);
|
|
1204
|
+
throw new ConfigError(`candidate ${path} could not be read`);
|
|
1205
|
+
}
|
|
1206
|
+
if (
|
|
1207
|
+
artifact?.version !== 1 ||
|
|
1208
|
+
!['ux-guard-policy-candidate', 'ux-guard-setup-proposal'].includes(artifact.kind)
|
|
1209
|
+
) {
|
|
1210
|
+
throw new ConfigError(`candidate ${path} is not a ux-guard policy candidate`);
|
|
1211
|
+
}
|
|
1212
|
+
return artifact;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
function validateCandidateApproval(artifact) {
|
|
1216
|
+
const approval = artifact.approval;
|
|
1217
|
+
if (
|
|
1218
|
+
approval?.status !== 'approved' ||
|
|
1219
|
+
typeof approval.reviewer !== 'string' ||
|
|
1220
|
+
!approval.reviewer.trim() ||
|
|
1221
|
+
typeof approval.reviewedAt !== 'string' ||
|
|
1222
|
+
!approval.reviewedAt.trim() ||
|
|
1223
|
+
approval.patternPolicyReviewed !== true
|
|
1224
|
+
) {
|
|
1225
|
+
throw new ConfigError(
|
|
1226
|
+
'candidate is unapproved; a human must set status, reviewer, reviewedAt, and patternPolicyReviewed before promotion',
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
function writePromotedPolicy(root, config, baseline) {
|
|
1232
|
+
const baselinePath = config.patterns.baseline;
|
|
1233
|
+
const baselineFull = join(root, baselinePath);
|
|
1234
|
+
const configFull = join(root, CONFIG_FILE);
|
|
1235
|
+
ensureLocalDirectory(root, dirname(baselineFull), 'patterns.baseline', true);
|
|
1236
|
+
let baselineWritten = false;
|
|
1237
|
+
try {
|
|
1238
|
+
writeFileSync(baselineFull, `${JSON.stringify(baseline, null, 2)}\n`, { flag: 'wx' });
|
|
1239
|
+
baselineWritten = true;
|
|
1240
|
+
writeFileSync(configFull, `${JSON.stringify(config, null, 2)}\n`, { flag: 'wx' });
|
|
1241
|
+
} catch (error) {
|
|
1242
|
+
if (baselineWritten) {
|
|
1243
|
+
try {
|
|
1244
|
+
unlinkSync(baselineFull);
|
|
1245
|
+
} catch {
|
|
1246
|
+
// The original write error remains the actionable failure.
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
if (error?.code === 'EEXIST') throw new ConfigError('promotion target appeared during promotion; nothing was overwritten');
|
|
1250
|
+
throw new ConfigError('promoted policy could not be written');
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
function ensureLocalDirectory(root, directory, label, create) {
|
|
1255
|
+
const path = relative(root, directory);
|
|
1256
|
+
if (!path) return;
|
|
1257
|
+
let current = root;
|
|
1258
|
+
for (const part of path.split(sep)) {
|
|
1259
|
+
current = join(current, part);
|
|
1260
|
+
if (!existsSync(current)) {
|
|
1261
|
+
if (!create) throw new ConfigError(`${label} parent does not exist`);
|
|
1262
|
+
try {
|
|
1263
|
+
mkdirSync(current);
|
|
1264
|
+
} catch {
|
|
1265
|
+
throw new ConfigError(`${label} parent could not be created safely`);
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
let stats;
|
|
1269
|
+
try {
|
|
1270
|
+
stats = lstatSync(current);
|
|
1271
|
+
} catch {
|
|
1272
|
+
throw new ConfigError(`${label} parent could not be inspected`);
|
|
1273
|
+
}
|
|
1274
|
+
if (stats.isSymbolicLink() || !stats.isDirectory()) {
|
|
1275
|
+
throw new ConfigError(`${label} parent must be a repository directory, not a symbolic link`);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
function catalogCommand(ts, config, trust, options, io) {
|
|
1281
|
+
const evaluation = evaluateCatalog(ts, config, trust, options);
|
|
1282
|
+
if (options.format === 'json') {
|
|
1283
|
+
io.out.write(`${JSON.stringify(resultDocument('catalog', evaluation.code, { catalog: evaluation.summary }), null, 2)}\n`);
|
|
1284
|
+
} else {
|
|
1285
|
+
io.out.write(renderCatalog(evaluation.report));
|
|
1286
|
+
}
|
|
1287
|
+
emitCatalogDiagnostics(evaluation.findings, io);
|
|
1288
|
+
return evaluation.code;
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
function evaluateCatalog(ts, config, trust, options) {
|
|
1292
|
+
const report = validateCatalog(loadCatalog(options.root, config), loadSurfaces(ts, options.root, config));
|
|
1293
|
+
let policyInventory = report.inventory;
|
|
1294
|
+
if (trust) {
|
|
1295
|
+
policyInventory = catalogPolicy(parseYamlSubset(readTrusted(options.root, config.catalog.path, trust))).inventory;
|
|
1296
|
+
}
|
|
1297
|
+
const findings = [];
|
|
1298
|
+
if (policyInventory === 'complete' && report.inventory !== 'complete') {
|
|
1299
|
+
findings.push({
|
|
1300
|
+
rule: 'catalog-policy-downgrade',
|
|
1301
|
+
message: 'trusted catalog inventory is complete and the candidate cannot downgrade it to partial',
|
|
1302
|
+
});
|
|
1303
|
+
}
|
|
1304
|
+
const effectiveComplete = policyInventory === 'complete' || report.inventory === 'complete';
|
|
1305
|
+
if (effectiveComplete && report.unmapped.length > 0) {
|
|
1306
|
+
findings.push({
|
|
1307
|
+
rule: 'unmapped-complete-inventory',
|
|
1308
|
+
count: report.unmapped.length,
|
|
1309
|
+
message: 'a complete inventory cannot leave a navigation surface unmapped',
|
|
1310
|
+
});
|
|
1311
|
+
}
|
|
1312
|
+
if (options.strict && report.unmapped.length > 0) {
|
|
1313
|
+
findings.push({
|
|
1314
|
+
rule: 'unmapped-strict',
|
|
1315
|
+
count: report.unmapped.length,
|
|
1316
|
+
message: 'strict mode rejects unmapped navigation surfaces',
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
const code = findings.length ? 1 : 0;
|
|
1320
|
+
return {
|
|
1321
|
+
code,
|
|
1322
|
+
findings,
|
|
1323
|
+
report,
|
|
1324
|
+
summary: summarizeCatalog(report, policyInventory, findings),
|
|
1325
|
+
};
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
function emitCatalogDiagnostics(findings, io) {
|
|
1329
|
+
for (const finding of findings) io.err.write(`ux-guard: ${finding.message}\n`);
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
function checkCommand(ts, config, trust, options, io) {
|
|
1333
|
+
const catalog = evaluateCatalog(ts, config, trust, options);
|
|
1334
|
+
const patterns = evaluatePatternScan(ts, config, trust, options);
|
|
1335
|
+
const journeyEvaluation = evaluateJourneys(config, trust, options, catalog.report);
|
|
1336
|
+
const visuals = evaluateVisuals(config, trust, options);
|
|
1337
|
+
const journeys = journeyEvaluation.summary;
|
|
1338
|
+
if (journeyEvaluation.policy) {
|
|
1339
|
+
journeys.verdicts = evaluateVerdicts(
|
|
1340
|
+
catalog.report,
|
|
1341
|
+
journeyEvaluation,
|
|
1342
|
+
patterns.summary,
|
|
1343
|
+
visuals,
|
|
1344
|
+
);
|
|
1345
|
+
}
|
|
1346
|
+
const code = Math.max(catalog.code, patterns.code);
|
|
1347
|
+
if (options.format === 'json') {
|
|
1348
|
+
io.out.write(
|
|
1349
|
+
`${JSON.stringify(
|
|
1350
|
+
resultDocument('check', code, {
|
|
1351
|
+
catalog: catalog.summary,
|
|
1352
|
+
patterns: patterns.summary,
|
|
1353
|
+
journeys,
|
|
1354
|
+
visuals,
|
|
1355
|
+
}),
|
|
1356
|
+
null,
|
|
1357
|
+
2,
|
|
1358
|
+
)}\n`,
|
|
1359
|
+
);
|
|
1360
|
+
} else {
|
|
1361
|
+
io.out.write(renderCatalog(catalog.report));
|
|
1362
|
+
io.out.write(renderComparison(patterns.scan, patterns.result));
|
|
1363
|
+
io.out.write(renderJourneySummary(journeys));
|
|
1364
|
+
}
|
|
1365
|
+
emitCatalogDiagnostics(catalog.findings, io);
|
|
1366
|
+
return code;
|
|
1367
|
+
}
|
|
1368
|
+
|
|
1369
|
+
function reportCommand(ts, config, trust, options, io) {
|
|
1370
|
+
if (options.strict || options.acceptGrowth || options.candidate) {
|
|
1371
|
+
throw new ConfigError('report does not accept strict, growth, or candidate options');
|
|
1372
|
+
}
|
|
1373
|
+
const catalog = evaluateCatalog(ts, config, trust, { ...options, strict: false });
|
|
1374
|
+
const patterns = evaluatePatternScan(ts, config, trust, options);
|
|
1375
|
+
const journeys = evaluateJourneys(config, trust, options, catalog.report);
|
|
1376
|
+
const visuals = evaluateVisuals(config, trust, options);
|
|
1377
|
+
if (!journeys.policy) {
|
|
1378
|
+
throw new ConfigError('journey reporting is not configured');
|
|
1379
|
+
}
|
|
1380
|
+
const report = evaluateVerdicts(
|
|
1381
|
+
catalog.report,
|
|
1382
|
+
journeys,
|
|
1383
|
+
patterns.summary,
|
|
1384
|
+
visuals,
|
|
1385
|
+
);
|
|
1386
|
+
if (options.format === 'json') io.out.write(serializeVerdictReport(report));
|
|
1387
|
+
else io.out.write(renderVerdictMarkdown(report));
|
|
1388
|
+
return 0;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
function evaluateVerdicts(catalog, journeys, patterns, visuals) {
|
|
1392
|
+
try {
|
|
1393
|
+
return createVerdictReport({
|
|
1394
|
+
catalog,
|
|
1395
|
+
policy: journeys.policy,
|
|
1396
|
+
measurements: journeys.measurements,
|
|
1397
|
+
patterns,
|
|
1398
|
+
comparison: journeys.comparison,
|
|
1399
|
+
targets: journeys.targets,
|
|
1400
|
+
readinessEvidence: journeys.readinessEvidence,
|
|
1401
|
+
visuals,
|
|
1402
|
+
});
|
|
1403
|
+
} catch (error) {
|
|
1404
|
+
throw new ConfigError(`journey verdict report: ${error.message}`);
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
function evaluateJourneys(config, trust, options, catalog) {
|
|
1409
|
+
if (!config.journeys) {
|
|
1410
|
+
return {
|
|
1411
|
+
summary: {
|
|
1412
|
+
status: 'unconfigured',
|
|
1413
|
+
rollout: 'measurement-only',
|
|
1414
|
+
},
|
|
1415
|
+
policy: null,
|
|
1416
|
+
measurements: null,
|
|
1417
|
+
comparison: unavailableJourneyComparison('journey reporting is not configured'),
|
|
1418
|
+
targets: unavailableTargetAttainment(
|
|
1419
|
+
'not-configured',
|
|
1420
|
+
'owner targets are not configured',
|
|
1421
|
+
),
|
|
1422
|
+
readinessEvidence: null,
|
|
1423
|
+
};
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
const { policy: policyPath, report: reportPath } = config.journeys;
|
|
1427
|
+
let policy;
|
|
1428
|
+
try {
|
|
1429
|
+
policy = parseJourneyPolicy(readTrusted(options.root, policyPath, trust));
|
|
1430
|
+
} catch (error) {
|
|
1431
|
+
if (error instanceof ConfigError) throw error;
|
|
1432
|
+
throw new ConfigError(`${policyPath}: ${error.message}`);
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
const reportPresent = existsSync(join(options.root, reportPath));
|
|
1436
|
+
let report = null;
|
|
1437
|
+
if (reportPresent) {
|
|
1438
|
+
try {
|
|
1439
|
+
report = parseJourneyReport(readTrusted(options.root, reportPath, null));
|
|
1440
|
+
} catch (error) {
|
|
1441
|
+
if (error instanceof ConfigError) throw error;
|
|
1442
|
+
throw new ConfigError(`${reportPath}: ${error.message}`);
|
|
1443
|
+
}
|
|
1444
|
+
} else {
|
|
1445
|
+
const identity = resolveRunIdentity(options.root, {
|
|
1446
|
+
policy,
|
|
1447
|
+
baseCommit: trust?.commit,
|
|
1448
|
+
browser: { name: 'none', profile: 'not-run' },
|
|
1449
|
+
adapters: { journey: '1' },
|
|
1450
|
+
});
|
|
1451
|
+
report = createJourneyReport([], {
|
|
1452
|
+
catalog,
|
|
1453
|
+
policy,
|
|
1454
|
+
run: createRunEnvelope(identity, { statuses: [] }),
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
let artifact;
|
|
1459
|
+
try {
|
|
1460
|
+
artifact = createJourneyArtifact({ catalog, policy, report });
|
|
1461
|
+
} catch (error) {
|
|
1462
|
+
throw new ConfigError(`journey measurement: ${error.message}`);
|
|
1463
|
+
}
|
|
1464
|
+
const comparisonEvaluation = evaluateJourneyComparison({
|
|
1465
|
+
config,
|
|
1466
|
+
trust,
|
|
1467
|
+
root: options.root,
|
|
1468
|
+
report,
|
|
1469
|
+
reportPresent,
|
|
1470
|
+
policy,
|
|
1471
|
+
});
|
|
1472
|
+
const targets = evaluateOwnerTargets(
|
|
1473
|
+
config,
|
|
1474
|
+
trust,
|
|
1475
|
+
options.root,
|
|
1476
|
+
policy,
|
|
1477
|
+
report,
|
|
1478
|
+
);
|
|
1479
|
+
return {
|
|
1480
|
+
summary: {
|
|
1481
|
+
status: 'measurement-only',
|
|
1482
|
+
policyPath,
|
|
1483
|
+
reportPath,
|
|
1484
|
+
reportPresent,
|
|
1485
|
+
...artifact,
|
|
1486
|
+
comparison: comparisonEvaluation.comparison,
|
|
1487
|
+
targets,
|
|
1488
|
+
},
|
|
1489
|
+
policy,
|
|
1490
|
+
measurements: report,
|
|
1491
|
+
comparison: comparisonEvaluation.comparison,
|
|
1492
|
+
targets,
|
|
1493
|
+
readinessEvidence: comparisonEvaluation.readinessEvidence,
|
|
1494
|
+
};
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
function evaluateJourneyComparison({
|
|
1498
|
+
config,
|
|
1499
|
+
trust,
|
|
1500
|
+
root,
|
|
1501
|
+
report,
|
|
1502
|
+
reportPresent,
|
|
1503
|
+
policy,
|
|
1504
|
+
}) {
|
|
1505
|
+
const baselinePath = config.journeys.baseline;
|
|
1506
|
+
if (!baselinePath) {
|
|
1507
|
+
return {
|
|
1508
|
+
comparison: unavailableJourneyComparison('reviewed journey baseline is not configured'),
|
|
1509
|
+
readinessEvidence: null,
|
|
1510
|
+
};
|
|
1511
|
+
}
|
|
1512
|
+
if (!trust) {
|
|
1513
|
+
return {
|
|
1514
|
+
comparison: unavailableJourneyComparison(
|
|
1515
|
+
'reviewed journey baseline was not evaluated; pass --base with an explicit trusted revision',
|
|
1516
|
+
),
|
|
1517
|
+
readinessEvidence: null,
|
|
1518
|
+
};
|
|
1519
|
+
}
|
|
1520
|
+
if (!trustedFileExists(root, baselinePath, trust)) {
|
|
1521
|
+
return {
|
|
1522
|
+
comparison: unavailableJourneyComparison(
|
|
1523
|
+
`reviewed journey baseline is not present at trusted revision ${trust.ref}`,
|
|
1524
|
+
),
|
|
1525
|
+
readinessEvidence: null,
|
|
1526
|
+
};
|
|
1527
|
+
}
|
|
1528
|
+
if (!reportPresent) {
|
|
1529
|
+
return {
|
|
1530
|
+
comparison: unavailableJourneyComparison(
|
|
1531
|
+
'current deterministic journey report is not available',
|
|
1532
|
+
),
|
|
1533
|
+
readinessEvidence: null,
|
|
1534
|
+
};
|
|
1535
|
+
}
|
|
1536
|
+
let baseline;
|
|
1537
|
+
try {
|
|
1538
|
+
baseline = parseJourneyBaseline(readTrusted(root, baselinePath, trust));
|
|
1539
|
+
} catch (error) {
|
|
1540
|
+
throw new ConfigError(`${baselinePath}: ${error.message}`);
|
|
1541
|
+
}
|
|
1542
|
+
return {
|
|
1543
|
+
comparison: compareJourneyBaseline({
|
|
1544
|
+
baseline,
|
|
1545
|
+
report,
|
|
1546
|
+
policy,
|
|
1547
|
+
trusted: true,
|
|
1548
|
+
}),
|
|
1549
|
+
readinessEvidence: structuredClone(baseline.readinessEvidence),
|
|
1550
|
+
};
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
function evaluateOwnerTargets(config, trust, root, policy, report) {
|
|
1554
|
+
const targetsPath = config.journeys.targets;
|
|
1555
|
+
if (!targetsPath) {
|
|
1556
|
+
return unavailableTargetAttainment(
|
|
1557
|
+
'not-configured',
|
|
1558
|
+
'owner targets are not configured',
|
|
1559
|
+
);
|
|
1560
|
+
}
|
|
1561
|
+
if (!trust) {
|
|
1562
|
+
return unavailableTargetAttainment(
|
|
1563
|
+
'not-evaluated',
|
|
1564
|
+
'owner targets require an explicit trusted --base revision',
|
|
1565
|
+
);
|
|
1566
|
+
}
|
|
1567
|
+
if (!trustedFileExists(root, targetsPath, trust)) {
|
|
1568
|
+
return unavailableTargetAttainment(
|
|
1569
|
+
'not-configured',
|
|
1570
|
+
`owner targets are not present at trusted revision ${trust.ref}`,
|
|
1571
|
+
);
|
|
1572
|
+
}
|
|
1573
|
+
let targets;
|
|
1574
|
+
try {
|
|
1575
|
+
targets = parseOwnerTargets(readTrusted(root, targetsPath, trust));
|
|
1576
|
+
} catch (error) {
|
|
1577
|
+
throw new ConfigError(`${targetsPath}: ${error.message}`);
|
|
1578
|
+
}
|
|
1579
|
+
if (targets.approval.status !== 'reviewed') {
|
|
1580
|
+
return unavailableTargetAttainment(
|
|
1581
|
+
'not-evaluated',
|
|
1582
|
+
'owner targets at the trusted revision are not reviewed',
|
|
1583
|
+
targets.canonicalHash,
|
|
1584
|
+
);
|
|
1585
|
+
}
|
|
1586
|
+
if (targets.policy.digest !== policyDigest(policy)) {
|
|
1587
|
+
return unavailableTargetAttainment(
|
|
1588
|
+
'incompatible',
|
|
1589
|
+
'owner target policy digest differs from the trusted journey policy',
|
|
1590
|
+
targets.canonicalHash,
|
|
1591
|
+
);
|
|
1592
|
+
}
|
|
1593
|
+
return evaluateTargetAttainment({ targets, report });
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
function evaluateVisuals(config, trust, options) {
|
|
1597
|
+
if (!config.visuals) {
|
|
1598
|
+
return visualSummary({
|
|
1599
|
+
candidatePath: null,
|
|
1600
|
+
baselinePath: null,
|
|
1601
|
+
candidate: null,
|
|
1602
|
+
baseline: null,
|
|
1603
|
+
comparison: unavailableVisualComparison('visual policy is not configured'),
|
|
1604
|
+
});
|
|
1605
|
+
}
|
|
1606
|
+
const { profiles, checkpoints, candidate: candidatePath, baseline: baselinePath } =
|
|
1607
|
+
config.visuals;
|
|
1608
|
+
const profileSet = parseVisualProfileSet(
|
|
1609
|
+
readTrusted(options.root, profiles, trust),
|
|
1610
|
+
);
|
|
1611
|
+
const checkpointPolicy = parseVisualCheckpointPolicy(
|
|
1612
|
+
readTrusted(options.root, checkpoints, trust),
|
|
1613
|
+
);
|
|
1614
|
+
let candidate = null;
|
|
1615
|
+
const candidateFull = join(options.root, candidatePath);
|
|
1616
|
+
if (existsSync(candidateFull)) {
|
|
1617
|
+
candidate = readLocalVisualManifest(
|
|
1618
|
+
options.root,
|
|
1619
|
+
candidatePath,
|
|
1620
|
+
profileSet,
|
|
1621
|
+
checkpointPolicy,
|
|
1622
|
+
);
|
|
1623
|
+
if (candidate.approval.status !== 'unapproved') {
|
|
1624
|
+
throw new VisualContractError('working visual candidate must remain unapproved');
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
if (!candidate) {
|
|
1628
|
+
return visualSummary({
|
|
1629
|
+
candidatePath,
|
|
1630
|
+
baselinePath,
|
|
1631
|
+
candidate: null,
|
|
1632
|
+
baseline: null,
|
|
1633
|
+
comparison: unavailableVisualComparison(
|
|
1634
|
+
`visual candidate ${candidatePath} is not available`,
|
|
1635
|
+
),
|
|
1636
|
+
});
|
|
1637
|
+
}
|
|
1638
|
+
if (!trust) {
|
|
1639
|
+
return visualSummary({
|
|
1640
|
+
candidatePath,
|
|
1641
|
+
baselinePath,
|
|
1642
|
+
candidate,
|
|
1643
|
+
baseline: null,
|
|
1644
|
+
comparison: unavailableVisualComparison(
|
|
1645
|
+
'visual comparison requires an explicit trusted --base revision',
|
|
1646
|
+
candidate,
|
|
1647
|
+
),
|
|
1648
|
+
});
|
|
1649
|
+
}
|
|
1650
|
+
if (!trustedFileExists(options.root, baselinePath, trust)) {
|
|
1651
|
+
return visualSummary({
|
|
1652
|
+
candidatePath,
|
|
1653
|
+
baselinePath,
|
|
1654
|
+
candidate,
|
|
1655
|
+
baseline: null,
|
|
1656
|
+
comparison: unavailableVisualComparison(
|
|
1657
|
+
`reviewed visual baseline is not present at trusted revision ${trust.ref}`,
|
|
1658
|
+
candidate,
|
|
1659
|
+
),
|
|
1660
|
+
});
|
|
1661
|
+
}
|
|
1662
|
+
const baseline = parseVisualManifest(
|
|
1663
|
+
readTrusted(options.root, baselinePath, trust),
|
|
1664
|
+
{
|
|
1665
|
+
profileSet,
|
|
1666
|
+
checkpointPolicy,
|
|
1667
|
+
readImage: trustedVisualImageReader(
|
|
1668
|
+
options.root,
|
|
1669
|
+
trust,
|
|
1670
|
+
dirname(baselinePath),
|
|
1671
|
+
),
|
|
1672
|
+
requireFiles: true,
|
|
1673
|
+
},
|
|
1674
|
+
);
|
|
1675
|
+
return visualSummary({
|
|
1676
|
+
candidatePath,
|
|
1677
|
+
baselinePath,
|
|
1678
|
+
candidate,
|
|
1679
|
+
baseline,
|
|
1680
|
+
comparison: compareVisualManifests(baseline, candidate),
|
|
1681
|
+
});
|
|
1682
|
+
}
|
|
1683
|
+
|
|
1684
|
+
function trustedVisualImageReader(root, trust, manifestDirectory) {
|
|
1685
|
+
return (imagePath) => {
|
|
1686
|
+
const repositoryPath = `${manifestDirectory}/${imagePath}`.replace(/^\.\//, '');
|
|
1687
|
+
resolveRepositoryPath(root, repositoryPath, 'trusted visual image path');
|
|
1688
|
+
const record = execFileSync(
|
|
1689
|
+
'git',
|
|
1690
|
+
['ls-tree', trust.commit, '--', repositoryPath],
|
|
1691
|
+
{ cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] },
|
|
1692
|
+
).trim();
|
|
1693
|
+
if (!record.startsWith('100644 blob ') || !record.endsWith(`\t${repositoryPath}`)) {
|
|
1694
|
+
throw new VisualContractError(
|
|
1695
|
+
`trusted visual image ${repositoryPath} is missing or not a regular file`,
|
|
1696
|
+
);
|
|
1697
|
+
}
|
|
1698
|
+
return execFileSync(
|
|
1699
|
+
'git',
|
|
1700
|
+
['show', `${trust.commit}:${repositoryPath}`],
|
|
1701
|
+
{
|
|
1702
|
+
cwd: root,
|
|
1703
|
+
encoding: 'buffer',
|
|
1704
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
1705
|
+
maxBuffer: 64 * 1024 * 1024,
|
|
1706
|
+
},
|
|
1707
|
+
);
|
|
1708
|
+
};
|
|
1709
|
+
}
|
|
1710
|
+
|
|
1711
|
+
function visualSummary({
|
|
1712
|
+
candidatePath,
|
|
1713
|
+
baselinePath,
|
|
1714
|
+
candidate,
|
|
1715
|
+
baseline,
|
|
1716
|
+
comparison,
|
|
1717
|
+
}) {
|
|
1718
|
+
return {
|
|
1719
|
+
rollout: 'advisory',
|
|
1720
|
+
status: comparison.status,
|
|
1721
|
+
candidate: {
|
|
1722
|
+
status: candidate ? 'available-unapproved' : 'unavailable',
|
|
1723
|
+
manifestPath: candidatePath,
|
|
1724
|
+
canonicalHash: candidate?.canonicalHash ?? null,
|
|
1725
|
+
images: candidate?.images.length ?? 0,
|
|
1726
|
+
repetitions: candidate?.capture.repetitions ?? 0,
|
|
1727
|
+
},
|
|
1728
|
+
baseline: {
|
|
1729
|
+
status: baseline ? 'reviewed' : 'not-reviewed',
|
|
1730
|
+
manifestPath: baselinePath,
|
|
1731
|
+
canonicalHash: baseline?.canonicalHash ?? null,
|
|
1732
|
+
},
|
|
1733
|
+
comparison: {
|
|
1734
|
+
...comparison,
|
|
1735
|
+
checkpoints: comparison.checkpoints.filter(
|
|
1736
|
+
(checkpoint) => checkpoint.status !== 'unchanged',
|
|
1737
|
+
),
|
|
1738
|
+
},
|
|
1739
|
+
};
|
|
1740
|
+
}
|
|
1741
|
+
|
|
1742
|
+
function renderJourneySummary(journeys) {
|
|
1743
|
+
if (journeys.status === 'unconfigured') {
|
|
1744
|
+
return 'Journey measurement: unconfigured (advisory)\n';
|
|
1745
|
+
}
|
|
1746
|
+
const { coverage } = journeys;
|
|
1747
|
+
const lines = [
|
|
1748
|
+
`Journey measurement: ${coverage.measuredVariants}/${coverage.requiredVariants} required variants measured; ${coverage.successfulVariants} successful across ${coverage.cataloguedUseCases} catalogued use cases (advisory)`,
|
|
1749
|
+
` Policy: ${journeys.policyPath}`,
|
|
1750
|
+
` Report: ${journeys.reportPath}${journeys.reportPresent ? '' : ' (not generated)'}`,
|
|
1751
|
+
];
|
|
1752
|
+
if (coverage.findings.length === 0) {
|
|
1753
|
+
lines.push(' Lifecycle and measurement coverage: complete');
|
|
1754
|
+
} else {
|
|
1755
|
+
lines.push(' Lifecycle and measurement coverage:');
|
|
1756
|
+
for (const finding of coverage.findings) {
|
|
1757
|
+
lines.push(
|
|
1758
|
+
` - ${finding.useCaseId}${finding.journeyId ? `/${finding.journeyId}` : ''}: ${finding.rule}`,
|
|
1759
|
+
);
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
return `${lines.join('\n')}\n`;
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
function summarizeCatalog(report, policyInventory, findings) {
|
|
1766
|
+
return {
|
|
1767
|
+
status: findings.length ? 'findings' : 'clean',
|
|
1768
|
+
inventory: report.inventory,
|
|
1769
|
+
policyInventory,
|
|
1770
|
+
useCases: report.useCases.map(({ id, title, tier, effect }) => ({ id, title, tier, effect })),
|
|
1771
|
+
surfaces: report.surfaces.length,
|
|
1772
|
+
unmapped: report.unmapped.map(({ id, label, route }) => ({ id, label, route })),
|
|
1773
|
+
findings,
|
|
1774
|
+
};
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
function patternsCommand(sub, ts, config, trust, options, io) {
|
|
1778
|
+
if (!['init', 'scan', 'update', 'canary'].includes(sub)) {
|
|
1779
|
+
throw new ConfigError(`unknown patterns command ${sub ?? '(none)'}`);
|
|
1780
|
+
}
|
|
1781
|
+
if (trust && (sub === 'init' || sub === 'update')) {
|
|
1782
|
+
throw new ConfigError(`patterns ${sub} writes the working tree; do not pass --base`);
|
|
1783
|
+
}
|
|
1784
|
+
const scan = scanFiles(ts, options.root, config, listSourceFiles(options.root, config));
|
|
1785
|
+
const baselinePath = config.patterns.baseline;
|
|
1786
|
+
if (sub === 'init') {
|
|
1787
|
+
if (existsSync(join(options.root, baselinePath))) {
|
|
1788
|
+
throw new ConfigError(`${baselinePath} already exists; use patterns update`);
|
|
1789
|
+
}
|
|
1790
|
+
writeBaseline(options.root, baselinePath, baselineFromScan(scan), true);
|
|
1791
|
+
io.out.write(`ux-guard: created ${baselinePath}\n`);
|
|
1792
|
+
return 0;
|
|
1793
|
+
}
|
|
1794
|
+
const baseline = loadBaseline(options.root, baselinePath, trust);
|
|
1795
|
+
if (sub === 'scan') {
|
|
1796
|
+
const evaluation = patternEvaluation(scan, baseline);
|
|
1797
|
+
if (options.format === 'json') {
|
|
1798
|
+
io.out.write(`${JSON.stringify(resultDocument('patterns scan', evaluation.code, { patterns: evaluation.summary }), null, 2)}\n`);
|
|
1799
|
+
} else {
|
|
1800
|
+
io.out.write(renderComparison(scan, evaluation.result));
|
|
1801
|
+
}
|
|
1802
|
+
return evaluation.code;
|
|
1803
|
+
}
|
|
1804
|
+
if (sub === 'update') {
|
|
1805
|
+
const result = compare(scan, baseline);
|
|
1806
|
+
if (result.findings.length && !options.acceptGrowth) {
|
|
1807
|
+
io.out.write(renderComparison(scan, result));
|
|
1808
|
+
io.err.write('ux-guard: baseline growth requires an explicit --accept-growth\n');
|
|
1809
|
+
return 1;
|
|
1810
|
+
}
|
|
1811
|
+
writeBaseline(options.root, baselinePath, baselineFromScan(scan));
|
|
1812
|
+
io.out.write(`ux-guard: updated ${baselinePath} (${result.stale.length} stale entries removed, ${result.findings.length} accepted)\n`);
|
|
1813
|
+
return 0;
|
|
1814
|
+
}
|
|
1815
|
+
if (sub === 'canary') return canary(ts, config, scan, baseline, io);
|
|
1816
|
+
throw new ConfigError(`unknown patterns command ${sub}`);
|
|
1817
|
+
}
|
|
1818
|
+
|
|
1819
|
+
function evaluatePatternScan(ts, config, trust, options) {
|
|
1820
|
+
const scan = scanFiles(ts, options.root, config, listSourceFiles(options.root, config));
|
|
1821
|
+
const baseline = loadBaseline(options.root, config.patterns.baseline, trust);
|
|
1822
|
+
return patternEvaluation(scan, baseline);
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
function patternEvaluation(scan, baseline) {
|
|
1826
|
+
const result = compare(scan, baseline);
|
|
1827
|
+
const code = result.findings.length ? 1 : 0;
|
|
1828
|
+
return {
|
|
1829
|
+
code,
|
|
1830
|
+
result,
|
|
1831
|
+
scan,
|
|
1832
|
+
summary: {
|
|
1833
|
+
status: code ? 'findings' : 'clean',
|
|
1834
|
+
files: scan.files.length,
|
|
1835
|
+
inventory: sortedInventory(scan.inventory),
|
|
1836
|
+
findings: result.findings,
|
|
1837
|
+
stale: result.stale,
|
|
1838
|
+
},
|
|
1839
|
+
};
|
|
1840
|
+
}
|
|
1841
|
+
|
|
1842
|
+
function sortedInventory(inventory) {
|
|
1843
|
+
return Object.fromEntries(
|
|
1844
|
+
Object.entries(inventory)
|
|
1845
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
1846
|
+
.map(([key, counts]) => [
|
|
1847
|
+
key,
|
|
1848
|
+
Object.fromEntries(Object.entries(counts).sort(([a], [b]) => a.localeCompare(b))),
|
|
1849
|
+
]),
|
|
1850
|
+
);
|
|
1851
|
+
}
|
|
1852
|
+
|
|
1853
|
+
function resultDocument(command, code, results) {
|
|
1854
|
+
return {
|
|
1855
|
+
kind: 'ux-guard-command-result',
|
|
1856
|
+
version: 2,
|
|
1857
|
+
command,
|
|
1858
|
+
status: code === 0 ? 'clean' : 'findings',
|
|
1859
|
+
exitCode: code,
|
|
1860
|
+
...results,
|
|
1861
|
+
};
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
function loadBaseline(root, path, trust) {
|
|
1865
|
+
let parsed;
|
|
1866
|
+
try {
|
|
1867
|
+
parsed = JSON.parse(readTrusted(root, path, trust));
|
|
1868
|
+
} catch (error) {
|
|
1869
|
+
if (error instanceof ConfigError) throw error;
|
|
1870
|
+
throw new ConfigError(`${path} is not valid JSON`);
|
|
1871
|
+
}
|
|
1872
|
+
return validateBaseline(parsed);
|
|
1873
|
+
}
|
|
1874
|
+
|
|
1875
|
+
function writeBaseline(root, path, baseline, exclusive = false) {
|
|
1876
|
+
const full = join(root, path);
|
|
1877
|
+
ensureLocalDirectory(root, dirname(full), 'patterns.baseline', true);
|
|
1878
|
+
if (!exclusive && existsSync(full) && lstatSync(full).isSymbolicLink()) {
|
|
1879
|
+
throw new ConfigError(`${path} is a symbolic link; refusing to overwrite it`);
|
|
1880
|
+
}
|
|
1881
|
+
try {
|
|
1882
|
+
writeFileSync(full, `${JSON.stringify(baseline, null, 2)}\n`, exclusive ? { flag: 'wx' } : undefined);
|
|
1883
|
+
} catch (error) {
|
|
1884
|
+
if (exclusive && error?.code === 'EEXIST') throw new ConfigError(`${path} already exists; use patterns update`);
|
|
1885
|
+
throw new ConfigError(`${path} could not be written`);
|
|
1886
|
+
}
|
|
1887
|
+
}
|
|
1888
|
+
|
|
1889
|
+
export const CANARY_FILE = '__ux-guard-canary__/KnownBad.tsx';
|
|
1890
|
+
export const CANARY_VALUE = 'ux-guard-canary';
|
|
1891
|
+
|
|
1892
|
+
export function canarySource(config) {
|
|
1893
|
+
const { components, icons } = config.patterns;
|
|
1894
|
+
const component = components.names[0];
|
|
1895
|
+
const prop = components.props[0];
|
|
1896
|
+
const icon = icons.modules.find((module) => !icons.allowed.includes(module));
|
|
1897
|
+
if (!icon) throw new ConfigError('canary needs at least one icon module that is not allowed');
|
|
1898
|
+
return [
|
|
1899
|
+
`import { ${component} } from '${components.module}';`,
|
|
1900
|
+
`import { Canary } from '${icon}';`,
|
|
1901
|
+
'export function KnownBad() {',
|
|
1902
|
+
' return (',
|
|
1903
|
+
` <div style={{ width: '13px', color: '#abcdef' }}>`,
|
|
1904
|
+
` <${component} ${prop}="${CANARY_VALUE}">x</${component}>`,
|
|
1905
|
+
' <button type="button"><Canary /></button>',
|
|
1906
|
+
' </div>',
|
|
1907
|
+
' );',
|
|
1908
|
+
'}',
|
|
1909
|
+
'',
|
|
1910
|
+
].join('\n');
|
|
1911
|
+
}
|
|
1912
|
+
|
|
1913
|
+
function canary(ts, config, scan, baseline, io) {
|
|
1914
|
+
const injected = scanSource(ts, CANARY_FILE, canarySource(config), config);
|
|
1915
|
+
const combined = structuredClone(scan);
|
|
1916
|
+
for (const { key, value } of injected.controls) {
|
|
1917
|
+
combined.inventory[key] ??= {};
|
|
1918
|
+
combined.inventory[key][value] = (combined.inventory[key][value] ?? 0) + 1;
|
|
1919
|
+
}
|
|
1920
|
+
for (const rule of DEBT_RULES) {
|
|
1921
|
+
if (injected.debt[rule] > 0) combined.debt[rule][CANARY_FILE] = injected.debt[rule];
|
|
1922
|
+
}
|
|
1923
|
+
const { findings } = compare(combined, baseline);
|
|
1924
|
+
const expectedKey = `${config.patterns.components.names[0]}.${config.patterns.components.props[0]}`;
|
|
1925
|
+
const missing = [];
|
|
1926
|
+
if (!findings.some((f) => f.rule === 'new-control-value' && f.key === expectedKey && f.value === CANARY_VALUE)) {
|
|
1927
|
+
missing.push('new-control-value');
|
|
1928
|
+
}
|
|
1929
|
+
for (const rule of DEBT_RULES) {
|
|
1930
|
+
if (!findings.some((f) => f.rule === rule && f.file === CANARY_FILE)) missing.push(rule);
|
|
1931
|
+
}
|
|
1932
|
+
if (missing.length) {
|
|
1933
|
+
io.err.write(`ux-guard: known-bad canary was not rejected for ${missing.join(', ')}\n`);
|
|
1934
|
+
return 1;
|
|
1935
|
+
}
|
|
1936
|
+
io.out.write('ux-guard: known-bad pattern canary was rejected by every rule\n');
|
|
1937
|
+
return 0;
|
|
1938
|
+
}
|