@astryxdesign/cli 0.6.3-canary.29be688 → 0.6.3-canary.2bb99ed

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.
Files changed (62) hide show
  1. package/README.md +2 -0
  2. package/api/docs/detail/section/section.mjs +9 -15
  3. package/api/docs/detail/section/section.test.mjs +15 -6
  4. package/api/doctor/doctor.mjs +1 -1
  5. package/api/doctor/doctor.test.mjs +6 -6
  6. package/api/upgrade/_adapter.d.mts +22 -4
  7. package/api/upgrade/_adapter.mjs +19 -6
  8. package/api/upgrade/run/run.mjs +355 -59
  9. package/api/upgrade/upgrade.doc.mjs +5 -1
  10. package/api/upgrade/upgrade.type.d.mts +34 -0
  11. package/api/upgrade/upgrade.type.mjs +15 -0
  12. package/assets/codemods/__tests__/runner.test.mjs +330 -8
  13. package/assets/codemods/integration-runner.mjs +56 -4
  14. package/assets/codemods/integration-runner.protection.test.mjs +153 -0
  15. package/assets/codemods/run-codemod.mjs +177 -34
  16. package/assets/codemods/runner.mjs +248 -110
  17. package/assets/codemods/transforms/next/__tests__/migrate-native-picker-to-presentation.test.mjs +63 -0
  18. package/assets/codemods/transforms/next/__tests__/migrate-theme-catalog-to-descriptors.test.mjs +4 -0
  19. package/assets/codemods/transforms/next/index.mjs +8 -0
  20. package/assets/codemods/transforms/next/migrate-native-picker-to-presentation.mjs +148 -0
  21. package/assets/docs/cli-integrations.doc.mjs +13 -0
  22. package/assets/templates/blocks/components/DateInput/DateInputDateRange.tsx +1 -1
  23. package/assets/templates/blocks/components/Item/ItemDocumentTabs.doc.mjs +14 -0
  24. package/assets/templates/blocks/components/Item/ItemDocumentTabs.tsx +100 -0
  25. package/assets/templates/blocks/components/TimeInput/TimeInputConstrained.tsx +1 -0
  26. package/authoring/codemod/type.ts +12 -0
  27. package/authoring/doctypes/_schema.d.mts +71 -141
  28. package/authoring/doctypes/_schema.mjs +29 -2
  29. package/authoring/doctypes/load-contract.test.mjs +1 -1
  30. package/authoring/doctypes/namespace/namespace.doc.mjs +2 -2
  31. package/authoring/doctypes/namespace/parse.test.mjs +23 -25
  32. package/authoring/doctypes/namespace/type.ts +5 -2
  33. package/authoring/doctypes/parse.mjs +3 -2
  34. package/authoring/doctypes/reference/reference.doc.mjs +5 -3
  35. package/authoring/doctypes/reference/type.ts +7 -8
  36. package/authoring/index.d.ts +1 -0
  37. package/clients/cli/commands/build-theme.mjs +10 -44
  38. package/clients/cli/commands/docs.mjs +15 -58
  39. package/clients/cli/commands/docs.test.mjs +11 -14
  40. package/clients/cli/commands/text-json-parity.test.mjs +718 -0
  41. package/clients/cli/commands/theme-targets.behavior.test.mjs +4 -3
  42. package/clients/cli/commands/upgrade.doc.mjs +2 -1
  43. package/clients/cli/commands/upgrade.file-protection.test.mjs +228 -0
  44. package/clients/cli/commands/upgrade.mjs +29 -7
  45. package/clients/cli/formatters/index.mjs +2 -0
  46. package/clients/cli/formatters/index.test.mjs +6 -0
  47. package/foundation/discovery/authoring-self-docs.test.mjs +3 -2
  48. package/foundation/discovery/docs-discovery.mjs +6 -5
  49. package/foundation/discovery/docs-discovery.test.mjs +7 -10
  50. package/foundation/discovery/docs-section-key.d.mts +19 -8
  51. package/foundation/discovery/docs-section-key.mjs +119 -32
  52. package/foundation/discovery/docs-section-key.test.mjs +41 -19
  53. package/foundation/doc-compiler/compile.mjs +5 -4
  54. package/foundation/fs/file-protection.d.mts +33 -0
  55. package/foundation/fs/file-protection.mjs +825 -0
  56. package/foundation/fs/file-protection.test.mjs +250 -0
  57. package/foundation/response/error-codes.d.mts +3 -1
  58. package/foundation/response/error-codes.d.ts +2 -0
  59. package/foundation/response/error-codes.doc.mjs +10 -0
  60. package/foundation/response/error-codes.mjs +6 -0
  61. package/foundation/response/error-codes.test.mjs +55 -11
  62. package/package.json +10 -9
@@ -17,6 +17,7 @@
17
17
  * integration; execution errors abort before the agent-doc write.
18
18
  */
19
19
 
20
+ import * as fs from 'node:fs';
20
21
  import * as path from 'node:path';
21
22
  import {
22
23
  detectInstalledTargetVersion,
@@ -47,7 +48,58 @@ import {getCliInvocation} from '../../../foundation/env/package-manager.mjs';
47
48
  import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';
48
49
  import {AstryxError} from '../../error.mjs';
49
50
  import {logger} from '../../logger.mjs';
50
- import {assertWithin, PathSafetyError} from '../../../foundation/fs/path-safety.mjs';
51
+ import {
52
+ assertWithin,
53
+ PathSafetyError,
54
+ } from '../../../foundation/fs/path-safety.mjs';
55
+ import {createFileProtectionResolver} from '../../../foundation/fs/file-protection.mjs';
56
+
57
+ /**
58
+ * Collapse per-codemod protection hits into one stable row per file.
59
+ * @param {Array<{file: string, codemod: string, reason: string, declaration: string, generated: boolean, command?: string}>} rows
60
+ */
61
+ function summarizeProtectedFiles(rows) {
62
+ const byFile = new Map();
63
+ for (const row of rows) {
64
+ const current = byFile.get(row.file) ?? {
65
+ file: row.file,
66
+ codemods: new Set(),
67
+ reasons: new Set(),
68
+ declarations: new Set(),
69
+ commands: new Set(),
70
+ };
71
+ current.codemods.add(row.codemod);
72
+ current.reasons.add(row.reason);
73
+ current.declarations.add(row.declaration);
74
+ if (row.command) current.commands.add(row.command);
75
+ byFile.set(row.file, current);
76
+ }
77
+ return [...byFile.values()]
78
+ .map(item => ({
79
+ file: item.file,
80
+ codemods: [...item.codemods].sort(),
81
+ reasons: [...item.reasons].sort(),
82
+ declarations: [...item.declarations].sort(),
83
+ commands: [...item.commands].sort(),
84
+ }))
85
+ .sort((a, b) => a.file.localeCompare(b.file));
86
+ }
87
+
88
+ /** @param {string} cwd */
89
+ function loadFileProtection(cwd) {
90
+ try {
91
+ return createFileProtectionResolver(cwd);
92
+ } catch (err) {
93
+ const message = err instanceof Error ? err.message : String(err);
94
+ logger.error(message);
95
+ logger.log('Upgrade failed\n');
96
+ throw new AstryxError(
97
+ message,
98
+ undefined,
99
+ ERROR_CODES.ERR_CODEMOD_PROTECTION_SOURCE,
100
+ );
101
+ }
102
+ }
51
103
 
52
104
  /**
53
105
  * Run the upgrade pipeline for a validated, non-list invocation. Returns the
@@ -75,7 +127,11 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
75
127
  if (err instanceof PathSafetyError) {
76
128
  logger.error(err.message);
77
129
  logger.log('Aborted\n');
78
- throw new AstryxError(err.message, undefined, ERROR_CODES.ERR_PATH_TRAVERSAL);
130
+ throw new AstryxError(
131
+ err.message,
132
+ undefined,
133
+ ERROR_CODES.ERR_PATH_TRAVERSAL,
134
+ );
79
135
  }
80
136
  throw err;
81
137
  }
@@ -133,7 +189,10 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
133
189
  });
134
190
  }
135
191
 
136
- const versionManifests = await getCoreVersionManifests(currentVersion, targetVersion);
192
+ const versionManifests = await getCoreVersionManifests(
193
+ currentVersion,
194
+ targetVersion,
195
+ );
137
196
 
138
197
  const coreConfigCodemodNames = [];
139
198
  for (const {transforms} of versionManifests) {
@@ -157,6 +216,11 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
157
216
  }
158
217
  }
159
218
 
219
+ // Read and parse all working-tree protection declarations before any
220
+ // dependency installation or codemod write. Core and integration runners
221
+ // share this exact snapshot.
222
+ let protection = loadFileProtection(cwd);
223
+
160
224
  const ready = await ensureCodemodDeps({installDeps: options.installDeps});
161
225
  if (!ready) {
162
226
  const msg = 'jscodeshift is required but could not be installed.';
@@ -172,34 +236,81 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
172
236
  codemod: options.codemod,
173
237
  skipCodemods,
174
238
  root: cwd,
239
+ protection,
175
240
  });
176
- const coreResult = codemodResult && 'totalFilesChanged' in codemodResult ? codemodResult : null;
241
+ const coreResult =
242
+ codemodResult && 'totalFilesChanged' in codemodResult
243
+ ? codemodResult
244
+ : null;
177
245
 
178
246
  /** @type {Array<import('../../../foundation/integrations/integrations.mjs').LoadedIntegration>} */
179
247
  let integrations;
180
248
  /** @type {import('../../../authoring/config/type').PostCodemodHook[]} */
181
249
  let postCodemodHooks;
182
250
  try {
183
- const projectContext = await loadProjectContext(cwd, options.integration ?? []);
251
+ const projectContext = await loadProjectContext(
252
+ cwd,
253
+ options.integration ?? [],
254
+ );
184
255
  postCodemodHooks = projectContext.postCodemodHooks;
185
256
  integrations = projectContext.integrations;
186
257
  } catch (err) {
187
258
  const configErr = /** @type {Error} */ (err);
259
+ const allProtected = coreResult?.protectedFiles ?? [];
260
+ if (allProtected.length > 0) {
261
+ const protectedFiles = summarizeProtectedFiles(allProtected);
262
+ logger.error(
263
+ `${ERROR_CODES.ERR_CODEMOD_PROTECTED}: protected codemod changes remain while loading the Astryx config.`,
264
+ );
265
+ for (const item of protectedFiles) {
266
+ logger.error(` ${item.file} — ${item.declarations.join('; ')}`);
267
+ }
268
+ logger.log('Upgrade incomplete: protected changes remain\n');
269
+ return {
270
+ type: 'upgrade.run',
271
+ data: {
272
+ from: currentVersion,
273
+ to: targetVersion,
274
+ codemods: totalTransforms,
275
+ integrations: [],
276
+ agentDocsRefreshed: false,
277
+ agentDocs: {
278
+ status: 'current',
279
+ installedVersion: targetVersion,
280
+ fromVersions: [],
281
+ files: [],
282
+ refreshed: false,
283
+ action: 'none',
284
+ },
285
+ filesChanged: coreResult?.totalFilesChanged ?? 0,
286
+ transformsApplied: coreResult?.totalTransformsApplied ?? 0,
287
+ modifiedFiles: uniqueFiles(coreResult?.changedFiles).map(file =>
288
+ path.relative(cwd, file).split(path.sep).join('/'),
289
+ ),
290
+ protectedFiles,
291
+ declinedCandidates: [],
292
+ complete: false,
293
+ errorCode: 'ERR_CODEMOD_PROTECTED',
294
+ errors: coreResult?.errors ?? [],
295
+ },
296
+ };
297
+ }
188
298
  // Graceful dry-run catch: a config that fails strict validation is expected
189
299
  // & fixable ONLY when dry-run AND a pending core config codemod previewed a
190
300
  // change (the codemod that would repair it).
191
- const codemodWouldFixConfig = hasCoreConfigCodemod && (coreResult?.totalFilesChanged ?? 0) > 0;
301
+ const codemodWouldFixConfig =
302
+ hasCoreConfigCodemod && (coreResult?.totalFilesChanged ?? 0) > 0;
192
303
  if (!apply && codemodWouldFixConfig) {
193
304
  // Lightweight inspection — no config/Project load (config is still broken
194
305
  // in dry-run; the codemod previewed a fix but did not write it).
195
306
  const inspection = inspectAgentDocs(cwd, targetVersion);
196
- return statusConfigFixable(
197
- {
198
- from: currentVersion,
199
- to: targetVersion,
200
- configError: configErr.message,
201
- configCodemods: coreConfigCodemodNames,
202
- agentDocs: /** @type {import('../upgrade.type.mjs').AgentDocsSummary} */ ({
307
+ return statusConfigFixable({
308
+ from: currentVersion,
309
+ to: targetVersion,
310
+ configError: configErr.message,
311
+ configCodemods: coreConfigCodemodNames,
312
+ agentDocs:
313
+ /** @type {import('../upgrade.type.mjs').AgentDocsSummary} */ ({
203
314
  status: inspection.status,
204
315
  installedVersion: targetVersion,
205
316
  fromVersions: inspection.blockVersions,
@@ -207,17 +318,22 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
207
318
  refreshed: false,
208
319
  action: inspection.status === 'missing' ? 'nudge-init' : 'none',
209
320
  }),
210
- },
211
- );
321
+ });
212
322
  }
213
323
  // Genuine config error: abort.
214
324
  logger.error(configErr.message);
215
325
  logger.log('Aborted\n');
216
- throw new AstryxError(configErr.message, undefined, ERROR_CODES.ERR_INVALID_ARGUMENT);
326
+ throw new AstryxError(
327
+ configErr.message,
328
+ undefined,
329
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
330
+ );
217
331
  }
218
332
 
219
333
  if (integrations.length > 0) {
220
- logger.log(`Integrations: ${integrations.map(i => i.name ?? i.__spec).join(', ')}`);
334
+ logger.log(
335
+ `Integrations: ${integrations.map(i => i.name ?? i.__spec).join(', ')}`,
336
+ );
221
337
  }
222
338
 
223
339
  // Non-blocking nudge for integration validation issues (suppressed for
@@ -229,7 +345,9 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
229
345
  currentVersion,
230
346
  targetVersion,
231
347
  );
232
- const hasIntegrationCodemods = integrationVersionGroups.some(g => g.codemods.length > 0);
348
+ const hasIntegrationCodemods = integrationVersionGroups.some(
349
+ g => g.codemods.length > 0,
350
+ );
233
351
 
234
352
  for (const {codemods} of integrationVersionGroups) {
235
353
  for (const c of codemods) {
@@ -267,14 +385,14 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
267
385
  }
268
386
 
269
387
  if (totalTransforms > 0) {
270
- logger.log(`${totalTransforms} codemod${totalTransforms === 1 ? '' : 's'} to run${apply ? '' : ' (dry run)'}`);
388
+ logger.log(
389
+ `${totalTransforms} codemod${totalTransforms === 1 ? '' : 's'} to run${apply ? '' : ' (dry run)'}`,
390
+ );
271
391
  } else {
272
392
  logger.log('No automatic codemods to run for this version range.');
273
393
  }
274
394
 
275
- /**
276
- * @type {{from: string, to: string, codemods: number, integrations: string[], agentDocsRefreshed: boolean, agentDocs: import('../upgrade.type.mjs').AgentDocsSummary, registryCompositions?: import('../upgrade.type.mjs').RegistryCompositionSummary, filesChanged?: number, transformsApplied?: number, errors?: Array<{file: string, codemod: string, error: string}>}}
277
- */
395
+ /** @type {import('../upgrade.type.mjs').UpgradeRunResponse['data']} */
278
396
  const receipt = {
279
397
  from: currentVersion,
280
398
  to: targetVersion,
@@ -282,79 +400,257 @@ export async function run(options = {}, {cwd = process.cwd()} = {}) {
282
400
  integrations: integrations.map(i => i.name ?? i.__spec),
283
401
  agentDocsRefreshed: false,
284
402
  agentDocs: /** @type {import('../upgrade.type.mjs').AgentDocsSummary} */ ({
285
- status: 'current', installedVersion: targetVersion,
286
- fromVersions: [], files: [], refreshed: false, action: 'none',
403
+ status: 'current',
404
+ installedVersion: targetVersion,
405
+ fromVersions: [],
406
+ files: [],
407
+ refreshed: false,
408
+ action: 'none',
287
409
  }),
288
410
  };
289
411
 
290
412
  let integrationResult = null;
291
413
  if (hasIntegrationCodemods) {
414
+ const coreStagedContents = coreResult?.stagedContents;
415
+ if (
416
+ (coreResult?.writtenFiles.length ?? 0) > 0 ||
417
+ (coreStagedContents?.size ?? 0) > 0
418
+ ) {
419
+ protection = createFileProtectionResolver(cwd, {
420
+ overrides: coreStagedContents,
421
+ });
422
+ }
292
423
  logger.log('Applying integration codemods...');
293
- integrationResult = await runIntegrationCodemodsStep(integrationVersionGroups, {
294
- apply,
295
- path: path_,
296
- codemod: options.codemod,
297
- skipCodemods,
298
- });
424
+ integrationResult = await runIntegrationCodemodsStep(
425
+ integrationVersionGroups,
426
+ {
427
+ apply,
428
+ path: path_,
429
+ codemod: options.codemod,
430
+ skipCodemods,
431
+ root: cwd,
432
+ protection,
433
+ contents: coreStagedContents,
434
+ },
435
+ );
299
436
  }
300
437
 
301
438
  const registryResult = await reconcileCompositions();
302
439
 
303
- const mergedFilesChanged = (coreResult?.totalFilesChanged ?? 0) + (integrationResult?.totalFilesChanged ?? 0);
304
- const mergedTransformsApplied = (coreResult?.totalTransformsApplied ?? 0) + (integrationResult?.totalTransformsApplied ?? 0);
305
- const mergedWrittenFiles = [
306
- ...(coreResult?.writtenFiles ?? []),
307
- ...(integrationResult?.writtenFiles ?? []),
440
+ const mergedFilesChanged =
441
+ (coreResult?.totalFilesChanged ?? 0) +
442
+ (integrationResult?.totalFilesChanged ?? 0);
443
+ const mergedTransformsApplied =
444
+ (coreResult?.totalTransformsApplied ?? 0) +
445
+ (integrationResult?.totalTransformsApplied ?? 0);
446
+ const mergedChangedFiles = [
447
+ ...(coreResult?.changedFiles ?? []),
448
+ ...(integrationResult?.changedFiles ?? []),
308
449
  ...(registryResult?.writtenFiles ?? []),
309
450
  ];
310
- const mergedErrors = [...(coreResult?.errors ?? []), ...(integrationResult?.errors ?? [])];
451
+ const mergedErrors = [
452
+ ...(coreResult?.errors ?? []),
453
+ ...(integrationResult?.errors ?? []),
454
+ ];
455
+ let finalErrors = mergedErrors;
456
+ /** @type {string|undefined} */
457
+ let hookFailure;
458
+ const initialProtected = [
459
+ ...(coreResult?.protectedFiles ?? []),
460
+ ...(integrationResult?.protectedFiles ?? []),
461
+ ];
311
462
  const registryFilesChanged = registryResult?.writtenFiles.length ?? 0;
463
+ const generatedChangeBlocked = initialProtected.some(item => item.generated);
464
+ const shouldRunHooks =
465
+ postCodemodHooks.length > 0 &&
466
+ (mergedFilesChanged > 0 ||
467
+ registryFilesChanged > 0 ||
468
+ generatedChangeBlocked);
469
+ /** @type {Map<string, Buffer>} */
470
+ const protectedBeforeHooks = new Map();
471
+ if (apply && shouldRunHooks) {
472
+ for (const item of initialProtected) {
473
+ const absolute = path.resolve(cwd, item.file);
474
+ const relative = path.relative(cwd, absolute);
475
+ if (
476
+ relative.startsWith(`..${path.sep}`) ||
477
+ relative === '..' ||
478
+ path.isAbsolute(relative)
479
+ )
480
+ continue;
481
+ try {
482
+ if (fs.lstatSync(absolute).isFile()) {
483
+ protectedBeforeHooks.set(absolute, fs.readFileSync(absolute));
484
+ }
485
+ } catch {
486
+ // A candidate can disappear between planning and regeneration.
487
+ }
488
+ }
489
+ }
490
+ /** @type {string[]} */
491
+ const hookModifiedFiles = [];
312
492
 
313
- if (postCodemodHooks.length > 0 && (mergedFilesChanged > 0 || registryFilesChanged > 0)) {
314
- const files = uniqueFiles(mergedWrittenFiles).map(file => path.relative(cwd, file));
493
+ if (shouldRunHooks) {
494
+ const files = uniqueFiles(mergedChangedFiles).map(file =>
495
+ path.relative(cwd, file),
496
+ );
315
497
  try {
316
- await runPostCodemodHooks(postCodemodHooks, {packageDir: cwd, files, apply: apply || false});
498
+ await runPostCodemodHooks(postCodemodHooks, {
499
+ packageDir: cwd,
500
+ files,
501
+ apply: apply || false,
502
+ });
317
503
  } catch (err) {
318
504
  const hookErr = /** @type {Error} */ (err);
319
505
  const msg = `Post-codemod hook failed: ${hookErr.message}`;
320
506
  logger.error(msg);
321
- logger.log('Upgrade failed\n');
322
- throw new AstryxError(msg, undefined, ERROR_CODES.ERR_CODEMOD_FAILED);
507
+ if (initialProtected.length === 0) {
508
+ logger.log('Upgrade failed\n');
509
+ throw new AstryxError(msg, undefined, ERROR_CODES.ERR_CODEMOD_FAILED);
510
+ }
511
+ hookFailure = msg;
512
+ finalErrors = [
513
+ ...mergedErrors,
514
+ {file: '.', codemod: 'post-codemod-hook', error: msg},
515
+ ];
516
+ logger.log('Regeneration failed; protected changes remain\n');
517
+ }
518
+ if (apply) {
519
+ for (const [file, before] of protectedBeforeHooks) {
520
+ try {
521
+ if (!before.equals(fs.readFileSync(file)))
522
+ hookModifiedFiles.push(file);
523
+ } catch {
524
+ hookModifiedFiles.push(file);
525
+ }
526
+ }
323
527
  }
324
528
  }
325
529
 
530
+ // A successful apply hook may have regenerated protected outputs. Rerun the
531
+ // selected codemods in preview mode against fresh bytes and fresh declarations
532
+ // to distinguish resolved outputs from changes that remain blocked.
533
+ let remainingProtected = initialProtected;
534
+ if (apply && shouldRunHooks && !hookFailure) {
535
+ const refreshedProtection = loadFileProtection(cwd);
536
+ const coreCheck = await runCoreCodemods(versionManifests, {
537
+ apply: false,
538
+ path: path_,
539
+ codemod: options.codemod,
540
+ skipCodemods,
541
+ root: cwd,
542
+ protection: refreshedProtection,
543
+ silent: true,
544
+ });
545
+ const checkedCore =
546
+ coreCheck && 'totalFilesChanged' in coreCheck ? coreCheck : null;
547
+ const recheckProtection =
548
+ (checkedCore?.stagedContents.size ?? 0) > 0
549
+ ? createFileProtectionResolver(cwd, {
550
+ overrides: checkedCore?.stagedContents,
551
+ })
552
+ : refreshedProtection;
553
+ const integrationCheck = hasIntegrationCodemods
554
+ ? await runIntegrationCodemodsStep(integrationVersionGroups, {
555
+ apply: false,
556
+ path: path_,
557
+ codemod: options.codemod,
558
+ skipCodemods,
559
+ root: cwd,
560
+ protection: recheckProtection,
561
+ contents: checkedCore?.stagedContents,
562
+ silent: true,
563
+ })
564
+ : null;
565
+ const recheckedProtected = [
566
+ ...(checkedCore?.protectedFiles ?? []),
567
+ ...(integrationCheck?.protectedFiles ?? []),
568
+ ];
569
+ const recheckedChangedFiles = [
570
+ ...(checkedCore?.changedFiles ?? []),
571
+ ...(integrationCheck?.changedFiles ?? []),
572
+ ].map(file => path.relative(cwd, file).split(path.sep).join('/'));
573
+ const initialByFile = new Map();
574
+ for (const item of initialProtected) {
575
+ const rows = initialByFile.get(item.file) ?? [];
576
+ rows.push(item);
577
+ initialByFile.set(item.file, rows);
578
+ }
579
+ // A hook that removes a protection marker without regenerating the bytes
580
+ // does not make the required change disappear. Retain the original
581
+ // declaration for that still-pending file.
582
+ for (const file of recheckedChangedFiles) {
583
+ recheckedProtected.push(...(initialByFile.get(file) ?? []));
584
+ }
585
+ remainingProtected = recheckedProtected;
586
+ finalErrors = [
587
+ ...mergedErrors,
588
+ ...(checkedCore?.errors ?? []),
589
+ ...(integrationCheck?.errors ?? []),
590
+ ];
591
+ }
592
+
593
+ const protectedFiles = summarizeProtectedFiles(remainingProtected);
326
594
  receipt.filesChanged = mergedFilesChanged;
327
595
  receipt.transformsApplied = mergedTransformsApplied;
328
- receipt.errors = mergedErrors;
596
+ receipt.modifiedFiles = uniqueFiles([
597
+ ...mergedChangedFiles,
598
+ ...hookModifiedFiles,
599
+ ]).map(file => path.relative(cwd, file).split(path.sep).join('/'));
600
+ receipt.protectedFiles = protectedFiles;
601
+ receipt.declinedCandidates = [];
602
+ receipt.complete = protectedFiles.length === 0;
603
+ if (!receipt.complete) {
604
+ receipt.errorCode = 'ERR_CODEMOD_PROTECTED';
605
+ }
606
+ receipt.errors = finalErrors;
329
607
  if (registryResult) receipt.registryCompositions = registryResult.summary;
330
608
 
331
- if (receipt.errors?.length > 0) {
609
+ if (protectedFiles.length > 0) {
610
+ logger.error(
611
+ `${ERROR_CODES.ERR_CODEMOD_PROTECTED}: ${protectedFiles.length} protected file${protectedFiles.length === 1 ? '' : 's'} still require${protectedFiles.length === 1 ? 's' : ''} a codemod change:`,
612
+ );
613
+ for (const item of protectedFiles) {
614
+ logger.error(` ${item.file} — ${item.declarations.join('; ')}`);
615
+ for (const command of item.commands) {
616
+ logger.log(` Regenerate with: ${command}`);
617
+ }
618
+ }
619
+ }
620
+
621
+ if (receipt.errors?.length > 0 && protectedFiles.length === 0) {
332
622
  const msg = `Upgrade completed with ${receipt.errors.length} codemod error${receipt.errors.length === 1 ? '' : 's'}.`;
333
623
  logger.log('Upgrade failed\n');
334
624
  throw new AstryxError(msg, undefined, ERROR_CODES.ERR_CODEMOD_FAILED);
335
625
  }
336
626
 
337
- // All codemods + hooks succeeded — render from final post-upgrade state.
338
- const agentDocsPlan = await prepareAgentDocsRefresh({
339
- cwd,
340
- installedVersion: targetVersion,
341
- apply,
342
- fresh: true,
343
- });
344
- const completedAgentDocs = apply
345
- ? applyAgentDocsRefresh(agentDocsPlan)
346
- : agentDocsPlan.summary;
347
- receipt.agentDocs = completedAgentDocs;
348
- receipt.agentDocsRefreshed = completedAgentDocs.refreshed;
627
+ // Only refresh managed docs after all required codemod changes are complete.
628
+ if (protectedFiles.length === 0) {
629
+ const agentDocsPlan = await prepareAgentDocsRefresh({
630
+ cwd,
631
+ installedVersion: targetVersion,
632
+ apply,
633
+ fresh: true,
634
+ });
635
+ const completedAgentDocs = apply
636
+ ? applyAgentDocsRefresh(agentDocsPlan)
637
+ : agentDocsPlan.summary;
638
+ receipt.agentDocs = completedAgentDocs;
639
+ receipt.agentDocsRefreshed = completedAgentDocs.refreshed;
640
+ }
349
641
 
350
642
  const registryOk = receipt.registryCompositions?.ok ?? true;
351
643
  logger.log(
352
- registryOk
353
- ? (apply ? 'Upgrade complete' : 'Dry run complete') + '\n'
354
- : 'Upgrade finished with unresolved registry items\n',
644
+ protectedFiles.length > 0
645
+ ? 'Upgrade incomplete: protected changes remain\n'
646
+ : registryOk
647
+ ? (apply ? 'Upgrade complete' : 'Dry run complete') + '\n'
648
+ : 'Upgrade finished with unresolved registry items\n',
355
649
  );
356
650
  return {
357
651
  type: 'upgrade.run',
358
- data: /** @type {import('../upgrade.type.mjs').UpgradeRunResponse['data']} */ (/** @type {unknown} */ (receipt)),
652
+ data: /** @type {import('../upgrade.type.mjs').UpgradeRunResponse['data']} */ (
653
+ /** @type {unknown} */ (receipt)
654
+ ),
359
655
  };
360
656
  }
@@ -121,7 +121,7 @@ export const doc = {
121
121
  {
122
122
  type: 'upgrade.run',
123
123
  description:
124
- 'The terminal run receipt: from/to versions, the codemod count, integrations processed, the agent-docs summary, an optional copied-composition registry summary, and (apply mode) filesChanged, transformsApplied, and any per-codemod errors.',
124
+ 'The terminal run receipt: from/to versions, codemod count, integrations processed, agent-docs and registry summaries, modifiedFiles, protectedFiles, declinedCandidates, and completion state. A protected required change returns complete: false with ERR_CODEMOD_PROTECTED; the CLI exits nonzero while preserving the structured receipt.',
125
125
  },
126
126
  ],
127
127
  throws: [
@@ -147,6 +147,10 @@ export const doc = {
147
147
  code: 'ERR_CODEMOD_FAILED',
148
148
  when: 'one or more codemods failed, or a post-codemod hook failed',
149
149
  },
150
+ {
151
+ code: 'ERR_CODEMOD_PROTECTION_SOURCE',
152
+ when: 'a working-tree protection declaration cannot be read or parsed before writes begin',
153
+ },
150
154
  ],
151
155
  examples: [
152
156
  {
@@ -131,6 +131,31 @@ export type UpgradeRegistryResponse = {
131
131
  type: "upgrade.registry";
132
132
  data: RegistryCompositionSummary;
133
133
  };
134
+ /**
135
+ * A required codemod change blocked by effective working-tree protection.
136
+ */
137
+ export type ProtectedCodemodFile = {
138
+ /**
139
+ * Project-relative path.
140
+ */
141
+ file: string;
142
+ /**
143
+ * Codemods that still require a change.
144
+ */
145
+ codemods: string[];
146
+ /**
147
+ * Protection categories (generated, vendored, ignored, and hard boundaries).
148
+ */
149
+ reasons: string[];
150
+ /**
151
+ * Effective declarations that protect the file.
152
+ */
153
+ declarations: string[];
154
+ /**
155
+ * Exact regeneration commands found in generated headers, when present.
156
+ */
157
+ commands: string[];
158
+ };
134
159
  /**
135
160
  * astryx --json upgrade [--apply]
136
161
  */
@@ -144,7 +169,16 @@ export type UpgradeRunResponse = {
144
169
  agentDocsRefreshed: boolean;
145
170
  agentDocs: AgentDocsSummary;
146
171
  registryCompositions?: RegistryCompositionSummary | undefined;
172
+ complete?: boolean | undefined;
173
+ errorCode?: "ERR_CODEMOD_PROTECTED" | undefined;
147
174
  filesChanged?: number | undefined;
175
+ modifiedFiles?: string[] | undefined;
176
+ protectedFiles?: ProtectedCodemodFile[] | undefined;
177
+ declinedCandidates?: {
178
+ file: string;
179
+ location?: string;
180
+ reason: string;
181
+ }[] | undefined;
148
182
  transformsApplied?: number | undefined;
149
183
  errors?: {
150
184
  file: string;
@@ -95,6 +95,16 @@
95
95
  * @property {RegistryCompositionSummary} data
96
96
  */
97
97
 
98
+ /**
99
+ * A required codemod change blocked by effective working-tree protection.
100
+ * @typedef {object} ProtectedCodemodFile
101
+ * @property {string} file Project-relative path.
102
+ * @property {string[]} codemods Codemods that still require a change.
103
+ * @property {string[]} reasons Protection categories (generated, vendored, ignored, and hard boundaries).
104
+ * @property {string[]} declarations Effective declarations that protect the file.
105
+ * @property {string[]} commands Exact regeneration commands found in generated headers, when present.
106
+ */
107
+
98
108
  /**
99
109
  * astryx --json upgrade [--apply]
100
110
  * @typedef {object} UpgradeRunResponse
@@ -107,7 +117,12 @@
107
117
  * @property {boolean} data.agentDocsRefreshed
108
118
  * @property {AgentDocsSummary} data.agentDocs
109
119
  * @property {RegistryCompositionSummary} [data.registryCompositions]
120
+ * @property {boolean} [data.complete] False when protected required changes remain.
121
+ * @property {'ERR_CODEMOD_PROTECTED'} [data.errorCode] Stable incomplete-result code when complete is false.
110
122
  * @property {number} [data.filesChanged] Total files changed across core + integration codemods (apply mode).
123
+ * @property {string[]} [data.modifiedFiles] Project-relative files changed or previewed.
124
+ * @property {ProtectedCodemodFile[]} [data.protectedFiles] Protected files that still require a codemod change after regeneration.
125
+ * @property {Array<{file: string, location?: string, reason: string}>} [data.declinedCandidates] Candidates left unchanged because proof was insufficient.
111
126
  * @property {number} [data.transformsApplied] Total transforms that reported a change.
112
127
  * @property {Array<{file: string, codemod: string, error: string}>} [data.errors] Per-codemod errors, when any codemod failed.
113
128
  */