@rehearsal-db/core 0.1.0-beta.7 → 0.1.0-beta.9

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 (64) hide show
  1. package/CHANGELOG.md +77 -1
  2. package/COMPATIBILITY.md +15 -3
  3. package/README.md +31 -13
  4. package/docs/README.md +29 -0
  5. package/docs/adapters.md +15 -6
  6. package/docs/architecture.md +58 -0
  7. package/docs/baselines.md +22 -1
  8. package/docs/commands.md +81 -14
  9. package/docs/configuration.md +144 -11
  10. package/docs/getting-started.md +12 -2
  11. package/docs/glossary.md +4 -0
  12. package/docs/production-source.md +176 -33
  13. package/docs/roadmap.md +30 -28
  14. package/docs/runtime-policies.md +191 -0
  15. package/docs/sanitization.md +24 -8
  16. package/docs/security-model.md +29 -8
  17. package/docs/standalone-workflow.md +107 -0
  18. package/docs/troubleshooting.md +8 -0
  19. package/package.json +25 -22
  20. package/scripts/runtime/manage_database.mjs +18 -0
  21. package/src/README.md +17 -0
  22. package/src/application/session.mjs +313 -0
  23. package/{scripts/lib/rehearsal/baseline_artifact.mjs → src/baseline/artifact.mjs} +110 -14
  24. package/{scripts/lib/rehearsal/baseline_builder.mjs → src/baseline/builder.mjs} +8 -4
  25. package/{scripts/lib/rehearsal/baseline_preparation.mjs → src/baseline/preparation.mjs} +11 -4
  26. package/src/baseline/privacy_engine.mjs +413 -0
  27. package/{scripts/lib/rehearsal → src/baseline}/sanitization_policy.mjs +27 -7
  28. package/{scripts/lib/rehearsal → src/baseline}/schema_snapshot.mjs +1 -1
  29. package/src/cli/arguments.mjs +120 -0
  30. package/src/cli/guided.mjs +807 -0
  31. package/src/cli/rehearsal.mjs +933 -0
  32. package/src/cli/renderers.mjs +584 -0
  33. package/src/cli/runtime_commands.mjs +553 -0
  34. package/src/cli/source_commands.mjs +326 -0
  35. package/src/cli/terminal.mjs +275 -0
  36. package/src/identity/claim.mjs +975 -0
  37. package/src/identity/storage.mjs +165 -0
  38. package/{scripts/lib/rehearsal → src/project}/configuration.d.mts +34 -0
  39. package/{scripts/lib/rehearsal → src/project}/configuration.mjs +353 -3
  40. package/{scripts/lib/rehearsal → src/project}/setup.mjs +1 -1
  41. package/{scripts/lib/rehearsal → src/project}/support_report.mjs +5 -2
  42. package/{scripts/lib/rehearsal → src/runtime}/cleanup.mjs +3 -3
  43. package/{scripts/lib/rehearsal → src/runtime}/plan.mjs +5 -5
  44. package/src/runtime/policy.mjs +438 -0
  45. package/{scripts/lib/rehearsal/runtime_restore.mjs → src/runtime/restore.mjs} +40 -25
  46. package/src/runtime/topology.mjs +177 -0
  47. package/{scripts/lib/rehearsal → src/shared}/diagnostics.mjs +1 -1
  48. package/src/shared/operation_guard.mjs +162 -0
  49. package/{scripts/lib/rehearsal → src/shared}/process_environment.mjs +5 -1
  50. package/src/source/access.mjs +578 -0
  51. package/src/source/asset_transfer.mjs +177 -0
  52. package/src/source/baseline.mjs +446 -0
  53. package/src/source/postgresql_access.mjs +480 -0
  54. package/{scripts/lib/runtime/postgresql_runtime.mjs → src/targets/postgresql.mjs} +49 -13
  55. package/{scripts/lib/runtime/supabase_runtime.mjs → src/targets/supabase.mjs} +54 -16
  56. package/{scripts/lib/environment/local_supabase.mjs → src/targets/supabase_environment.mjs} +52 -20
  57. package/{scripts/lib/runtime/runtime_target.mjs → src/targets/target.mjs} +27 -2
  58. package/scripts/operations/database/manage_rehearsal_database.mjs +0 -11
  59. package/scripts/operations/rehearsal/rehearsal_cli.mjs +0 -2166
  60. /package/{scripts/lib/rehearsal → src/baseline}/input_discovery.mjs +0 -0
  61. /package/{scripts/lib/rehearsal → src/baseline}/policy_review.mjs +0 -0
  62. /package/{scripts/lib/rehearsal → src/runtime}/migration_history.mjs +0 -0
  63. /package/{scripts/lib/rehearsal → src/runtime}/service_environment.mjs +0 -0
  64. /package/{scripts/lib/rehearsal → src/shared}/human_output.mjs +0 -0
@@ -0,0 +1,584 @@
1
+ /**
2
+ * Human-readable CLI output. These functions are intentionally pure so the
3
+ * command and guided workflow modules can focus on orchestration.
4
+ */
5
+
6
+ import { formatCount } from "../shared/human_output.mjs";
7
+ import { terminalStyle } from "./terminal.mjs";
8
+
9
+ export const renderPlan = (plan, verbosity) => {
10
+ if (plan.targets) {
11
+ return [
12
+ "REHEARSAL STACK PLAN",
13
+ "",
14
+ ...plan.targets.flatMap((target) => [
15
+ `${target.name} — ${target.plan.config.project}`,
16
+ ` ✓ ${target.plan.environment.kind}`,
17
+ ` ✓ baseline ${target.plan.baseline.generationId}`,
18
+ ` ✓ ${formatCount(target.plan.baseline.tableCount, "table")}; ${formatCount(target.plan.baseline.rowCount, "row")}`,
19
+ ...(target.plan.migrations.candidates.length
20
+ ? target.plan.migrations.candidates.map(
21
+ (migration) => ` → ${migration.filename}`,
22
+ )
23
+ : [" ✓ No candidate migrations"]),
24
+ "",
25
+ ]),
26
+ `Combined candidate digest: ${plan.migrations.candidateSha256}`,
27
+ "",
28
+ "Execution plan",
29
+ ...plan.execution.map((step, index) => ` ${index + 1}. ${step}`),
30
+ ...(verbosity === "normal"
31
+ ? []
32
+ : [
33
+ "",
34
+ ...plan.targets.flatMap((target) =>
35
+ target.plan.environment.barriers.map(
36
+ (barrier) => ` ✓ ${target.name}: ${barrier}`,
37
+ ),
38
+ ),
39
+ ]),
40
+ "",
41
+ plan.guarantee,
42
+ "",
43
+ plan.migrations.candidateCount
44
+ ? "Next: run rehearsal run to review and confirm this exact cross-target migration set."
45
+ : "Next: run rehearsal run to verify the runtime stack and project proofs.",
46
+ ].join("\n");
47
+ }
48
+ const candidates = plan.migrations.candidates.length
49
+ ? plan.migrations.candidates
50
+ .map((entry) => ` → ${entry.filename} (${entry.sha256.slice(0, 12)})`)
51
+ .join("\n")
52
+ : " ✓ No candidate migrations";
53
+ const verbose =
54
+ verbosity === "normal"
55
+ ? []
56
+ : [
57
+ "",
58
+ "Safety barriers",
59
+ ...plan.environment.barriers.map((barrier) => ` ✓ ${barrier}`),
60
+ ` ✓ Candidate digest: ${plan.migrations.candidateSha256}`,
61
+ ];
62
+ return [
63
+ "REHEARSAL PLAN",
64
+ "",
65
+ "Environment",
66
+ ` ✓ ${plan.environment.kind}`,
67
+ ` ✓ Hosted access ${plan.environment.hostedAccess}`,
68
+ ` ✓ Hosted target configuration ${plan.environment.outboundNetwork}`,
69
+ ...(plan.environment.authenticationProviders.length
70
+ ? [
71
+ ` ✓ External identity providers ${plan.environment.authenticationProviders.join(", ")}`,
72
+ ]
73
+ : []),
74
+ "",
75
+ "Baseline",
76
+ ` ✓ ${plan.baseline.generationId}`,
77
+ ` ✓ ${formatCount(plan.baseline.tableCount, "table")}; ${formatCount(plan.baseline.rowCount, "row")}`,
78
+ ` ✓ ${plan.baseline.verification}`,
79
+ "",
80
+ "Migrations",
81
+ ` ✓ ${plan.migrations.representedCount} represented by baseline`,
82
+ candidates,
83
+ "",
84
+ "Execution plan",
85
+ ...plan.execution.map((step, index) => ` ${index + 1}. ${step}`),
86
+ ...verbose,
87
+ "",
88
+ plan.guarantee,
89
+ "",
90
+ plan.migrations.candidateCount
91
+ ? "Next: run rehearsal run to review and confirm this exact candidate set."
92
+ : "Next: run rehearsal run to verify the baseline and application proof.",
93
+ ].join("\n");
94
+ };
95
+
96
+ export const renderDoctor = (doctor, verbosity) => {
97
+ const lines = doctor.checks.map((check) => {
98
+ const marker = check.status === "pass" ? "✓" : "✗";
99
+ const detail =
100
+ verbosity === "normal" && check.status === "pass"
101
+ ? ""
102
+ : ` — ${check.detail}`;
103
+ return `${marker} ${check.label}${detail}`;
104
+ });
105
+ for (const check of doctor.checks.filter(
106
+ (entry) => entry.status === "fail",
107
+ )) {
108
+ if (check.remediation) lines.push(` Try: ${check.remediation}`);
109
+ }
110
+ if (doctor.ambientHostedVariables?.presentButQuarantined.length) {
111
+ lines.push(
112
+ `! ${doctor.ambientHostedVariables.presentButQuarantined.length} ambient hosted credential variables detected and quarantined.`,
113
+ );
114
+ }
115
+ return [
116
+ ...lines,
117
+ "",
118
+ doctor.state,
119
+ "",
120
+ doctor.state === "READY"
121
+ ? "Next: run rehearsal explain to review the migration plan."
122
+ : "Next: fix the items above, then run rehearsal doctor again.",
123
+ ].join("\n");
124
+ };
125
+
126
+ export const renderBaseline = (baseline) =>
127
+ [
128
+ "REHEARSAL BASELINE",
129
+ `Rehearsal: ${baseline.rehearsalVersion}`,
130
+ `Generation: ${baseline.generationId}`,
131
+ `Created: ${baseline.createdAt ?? "unknown"}`,
132
+ `Format: ${baseline.formatVersion}`,
133
+ `Tables: ${baseline.tableCount}`,
134
+ `Rows: ${baseline.rowCount}`,
135
+ `Migrations: ${baseline.migrationCount} through ${baseline.migrationCutoff}`,
136
+ `Data SHA-256: ${baseline.dataSha256}`,
137
+ `Sanitization policy SHA-256: ${baseline.sanitizationPolicySha256}`,
138
+ `Verification: ${baseline.verification}`,
139
+ "Source rows and sensitive values are never printed by inspect.",
140
+ ].join("\n");
141
+
142
+ export const renderMigrations = (inspection, verbosity) => {
143
+ const entries = inspection.migrations.map((migration) => {
144
+ const digest = verbosity === "normal" ? "" : ` (${migration.sha256})`;
145
+ return `${migration.status.padEnd(28)} ${migration.filename}${digest}`;
146
+ });
147
+ return [
148
+ "REHEARSAL MIGRATIONS",
149
+ `Baseline: ${inspection.baselineGenerationId} through ${inspection.baselineCutoff}`,
150
+ `Candidate digest: ${inspection.candidateSha256}`,
151
+ "",
152
+ ...entries,
153
+ ].join("\n");
154
+ };
155
+
156
+ export const renderCandidates = (summary, verbosity) =>
157
+ [
158
+ "REHEARSAL CANDIDATES",
159
+ ...(summary.targets?.length > 1
160
+ ? summary.targets.flatMap((target) => [
161
+ "",
162
+ `${target.name} — baseline ${target.baselineGenerationId}`,
163
+ ...(target.candidates.length
164
+ ? target.candidates.map((migration) =>
165
+ verbosity === "normal"
166
+ ? ` → ${migration.filename}`
167
+ : ` → ${migration.filename} (${migration.sha256})`,
168
+ )
169
+ : [" ✓ No candidate migrations"]),
170
+ ])
171
+ : [
172
+ `Baseline: ${summary.baselineGenerationId}`,
173
+ ...(summary.candidates.length
174
+ ? summary.candidates.map((migration) =>
175
+ verbosity === "normal"
176
+ ? ` → ${migration.filename}`
177
+ : ` → ${migration.filename} (${migration.sha256})`,
178
+ )
179
+ : [" ✓ No candidate migrations"]),
180
+ ]),
181
+ "",
182
+ `${summary.targets?.length > 1 ? "Combined candidate digest" : "Candidate digest"}: ${summary.candidateSha256}`,
183
+ `${summary.targets?.length > 1 ? "Pending across all targets" : "Pending"}: ${summary.candidateCount}`,
184
+ "",
185
+ summary.candidateCount
186
+ ? "Next: run rehearsal run for a guided confirmation, or pass the full digest in automation."
187
+ : "Next: run rehearsal run to verify the baseline and application proof.",
188
+ ].join("\n");
189
+
190
+ export const renderInit = (result) =>
191
+ [
192
+ `REHEARSAL INIT — ${result.mode.toUpperCase()}`,
193
+ `Destination: ${result.destination}`,
194
+ `Detected package manager: ${result.detected.packageManager}`,
195
+ `Detected Supabase config: ${result.detected.hasSupabaseConfig ? "yes" : "no"}`,
196
+ `Detected migrations: ${result.detected.hasMigrations ? "yes" : "no"}`,
197
+ ...(result.source ? ["", result.source] : []),
198
+ result.nextAction,
199
+ ].join("\n");
200
+
201
+ const summarizeSetupReadinessChecks = (readiness) => {
202
+ const failures = readiness.checks.filter((check) => check.status === "fail");
203
+ const policyMissing = failures.some(
204
+ (check) =>
205
+ check.id === "sanitization-policy" &&
206
+ /ENOENT|no such file/iu.test(check.detail),
207
+ );
208
+ const baselineMissing = failures.some(
209
+ (check) =>
210
+ check.id === "baseline" && /ENOENT|no such file/iu.test(check.detail),
211
+ );
212
+ const hidden = new Set([
213
+ ...(policyMissing ? ["sanitization-policy"] : []),
214
+ ...(baselineMissing
215
+ ? ["baseline", "baseline-permissions", "migration-history"]
216
+ : []),
217
+ ]);
218
+ if (
219
+ policyMissing &&
220
+ failures.some(
221
+ (check) =>
222
+ check.id === "paths" && /sanitization-policy/iu.test(check.detail),
223
+ )
224
+ ) {
225
+ hidden.add("paths");
226
+ }
227
+ const passed = readiness.checks.filter(
228
+ (check) => check.status === "pass",
229
+ ).length;
230
+ return [
231
+ ...(passed ? [` ✓ ${passed} environment and safety checks passed`] : []),
232
+ ...failures
233
+ .filter((check) => !hidden.has(check.id))
234
+ .map((check) => ` ○ ${check.label}: ${check.detail}`),
235
+ ...(policyMissing
236
+ ? [" ○ Baseline policy: not created yet (next guided step)"]
237
+ : []),
238
+ ...(baselineMissing
239
+ ? [" ○ Verified baseline: not created yet (after policy review)"]
240
+ : []),
241
+ ];
242
+ };
243
+
244
+ export const renderSetup = (result) => {
245
+ const actionMarker = { create: "+", update: "~", unchanged: "=" };
246
+ return [
247
+ `REHEARSAL SETUP — ${result.mode.toUpperCase()}`,
248
+ `Project: ${result.project}`,
249
+ `Database: ${result.target === "postgresql" ? "PostgreSQL" : "Supabase"}`,
250
+ `Runtime ID: ${result.projectId}`,
251
+ `Application: ${result.applicationUrl}`,
252
+ result.target === "postgresql"
253
+ ? `Port: database ${result.ports.database}`
254
+ : `Ports: API ${result.ports.api}, database ${result.ports.database}, Studio ${result.ports.studio}`,
255
+ "",
256
+ "Files",
257
+ ...result.files.map(
258
+ (file) => ` ${actionMarker[file.action]} ${file.action} ${file.path}`,
259
+ ),
260
+ "",
261
+ "Safety boundaries",
262
+ ...result.safety.map((barrier) => ` ✓ ${barrier}`),
263
+ ...(result.readiness
264
+ ? [
265
+ "",
266
+ `Readiness — ${result.readiness.state}`,
267
+ ...summarizeSetupReadinessChecks(result.readiness),
268
+ ]
269
+ : []),
270
+ "",
271
+ result.nextAction,
272
+ ].join("\n");
273
+ };
274
+
275
+ export const renderBaselinePreparation = (result, flags = {}) =>
276
+ [
277
+ `BASELINE POLICY — ${result.mode.toUpperCase()}`,
278
+ `Destination: ${result.destination}`,
279
+ `Records: ${formatCount(result.rowCount, "row")} from ${result.recordsPath}`,
280
+ `Migrations: ${formatCount(result.migrationCount, "migration")} through ${result.migrationCutoff}`,
281
+ "",
282
+ "Detected shape (values are never printed)",
283
+ ...result.tables.map(
284
+ (table) =>
285
+ ` ${terminalStyle(flags, "36", "→")} ${table.name}: ${formatCount(table.rowCount, "row")}; ${table.columns.join(", ")}`,
286
+ ),
287
+ "",
288
+ "Every column is marked REVIEW REQUIRED for action, generated status, identity, and foreign key metadata.",
289
+ "The draft cannot be activated until those decisions are completed.",
290
+ "",
291
+ result.nextAction,
292
+ ].join("\n");
293
+
294
+ export const renderBaselineInputInspection = (result, flags = {}) =>
295
+ [
296
+ "BASELINE INPUTS — READY",
297
+ `Records: ${formatCount(result.rowCount, "row")} across ${formatCount(result.tables.length, "table")} from ${result.recordsPath}`,
298
+ `Migrations: ${formatCount(result.migrationCount, "migration")} through ${result.migrationCutoff} from ${result.ledgerPath}`,
299
+ result.assetsPath
300
+ ? `Storage: ${formatCount(result.assetCount, "asset")} from ${result.assetsPath}`
301
+ : "Storage: no asset manifest selected",
302
+ "",
303
+ "Detected tables (values are never printed)",
304
+ ...result.tables.map(
305
+ (table) =>
306
+ ` ${terminalStyle(flags, "36", "→")} ${table.name}: ${formatCount(table.rowCount, "row")}`,
307
+ ),
308
+ "",
309
+ "All inputs are project-local regular files and passed structural validation.",
310
+ ].join("\n");
311
+
312
+ export const renderSupportReport = (report) => {
313
+ const toolLine = (label, tool) =>
314
+ `${label}: ${tool.status === "available" ? tool.version : tool.status.replaceAll("_", " ")}`;
315
+ return [
316
+ "REHEARSAL SUPPORT REPORT",
317
+ "",
318
+ `Rehearsal: ${report.rehearsalVersion}`,
319
+ `System: ${report.system.platform} ${report.system.architecture} (${report.system.release})`,
320
+ `Node.js: ${report.system.node}`,
321
+ toolLine("npm", report.tools.npm),
322
+ toolLine("Supabase CLI", report.tools.supabase),
323
+ toolLine("Docker client", report.tools.dockerClient),
324
+ toolLine("Docker server", report.tools.dockerServer),
325
+ "",
326
+ `Readiness: ${report.readiness.state}`,
327
+ ...report.readiness.checks.map((check) =>
328
+ check.status === "pass"
329
+ ? ` ✓ ${check.label}`
330
+ : ` ✗ ${check.label}${check.remediation ? ` — ${check.remediation}` : ""}`,
331
+ ),
332
+ ...(report.readiness.quarantinedHostedVariableCount
333
+ ? [
334
+ ` ! ${formatCount(report.readiness.quarantinedHostedVariableCount, "ambient hosted variable")} detected and quarantined`,
335
+ ]
336
+ : []),
337
+ "",
338
+ "Privacy: no row values, credentials, project paths, migration SQL, or baseline identifiers are included.",
339
+ "Review this report before sharing it.",
340
+ `Report a reproducible issue: ${report.supportUrl}`,
341
+ ].join("\n");
342
+ };
343
+
344
+ export const formatBytes = (bytes) => {
345
+ if (!Number.isFinite(bytes) || bytes <= 0) return "0 B";
346
+ const units = ["B", "KiB", "MiB", "GiB", "TiB"];
347
+ const unit = Math.min(
348
+ Math.floor(Math.log(bytes) / Math.log(1024)),
349
+ units.length - 1,
350
+ );
351
+ const value = bytes / 1024 ** unit;
352
+ return `${value >= 10 || unit === 0 ? value.toFixed(0) : value.toFixed(1)} ${units[unit]}`;
353
+ };
354
+
355
+ export const renderCleanup = (result) => {
356
+ const plan = result.plan;
357
+ if (plan.targets) {
358
+ const selectedCount = plan.targets.reduce(
359
+ (total, target) =>
360
+ total +
361
+ target.plan.baselines.removed.length +
362
+ (target.plan.runtime.included && target.plan.runtime.detected ? 1 : 0) +
363
+ target.plan.images.candidates.length,
364
+ 0,
365
+ );
366
+ return [
367
+ `REHEARSAL STACK CLEANUP — ${result.mode.toUpperCase()}`,
368
+ ...plan.targets.flatMap((target) => [
369
+ "",
370
+ `${target.name} — ${target.plan.project}`,
371
+ target.plan.baselines.available
372
+ ? ` ${target.plan.baselines.removed.length ? "→" : "✓"} ${formatCount(target.plan.baselines.removed.length, "old baseline generation")} selected; keeping ${target.plan.baselines.retained.length}`
373
+ : " ○ No active baseline found",
374
+ !target.plan.runtime.detected
375
+ ? " ✓ No runtime found"
376
+ : target.plan.runtime.included
377
+ ? ` → Runtime ${target.plan.projectId} selected`
378
+ : ` ○ Runtime ${target.plan.projectId} preserved`,
379
+ ...(target.plan.images.requested
380
+ ? target.plan.images.inspected
381
+ ? target.plan.images.candidates.length
382
+ ? [
383
+ ` → ${formatCount(target.plan.images.candidates.length, "older unused Supabase image")} selected`,
384
+ ...target.plan.images.candidates.flatMap((image) =>
385
+ image.tags.map((tag) => ` ${tag}`),
386
+ ),
387
+ ]
388
+ : [" ✓ No older unused Supabase images found"]
389
+ : [" ! Docker is unavailable; shared images were not inspected"]
390
+ : []),
391
+ ]),
392
+ "",
393
+ ...(result.mode === "preview"
394
+ ? selectedCount
395
+ ? [
396
+ "Nothing has been removed.",
397
+ `Combined cleanup set: ${plan.digest.slice(0, 12)}`,
398
+ `Exact confirmation: --confirm-cleanup=${plan.digest}`,
399
+ "Review every target above, then rerun with --write and this exact confirmation.",
400
+ ]
401
+ : ["Nothing has been removed, and nothing is currently selected."]
402
+ : result.applied.targets.map(
403
+ (target) =>
404
+ `${target.name}: removed ${formatCount(target.applied.baselines.removed.length, "old baseline generation")}; runtime ${target.applied.runtimeRemoved ? "removed" : "preserved"}; removed ${formatCount(target.applied.imagesRemoved.length, "unused image tag")}.`,
405
+ )),
406
+ ].join("\n");
407
+ }
408
+ const imageBytes = plan.images.candidates.reduce(
409
+ (total, image) => total + image.bytes,
410
+ 0,
411
+ );
412
+ const dockerImages = plan.docker.resources.find(
413
+ (resource) => resource.Type === "Images",
414
+ );
415
+ const selectedCount =
416
+ plan.baselines.removed.length +
417
+ (plan.runtime.included && plan.runtime.detected ? 1 : 0) +
418
+ plan.images.candidates.length;
419
+ return [
420
+ `REHEARSAL CLEANUP — ${result.mode.toUpperCase()}`,
421
+ `Project: ${plan.project}`,
422
+ ...(dockerImages
423
+ ? [
424
+ `Docker images: ${dockerImages.Size}; ${dockerImages.Reclaimable} reclaimable across Docker`,
425
+ ]
426
+ : ["Docker usage: unavailable"]),
427
+ ...(plan.docker.disk
428
+ ? [
429
+ `Container disk: ${formatBytes(plan.docker.disk.usedBytes)} used of ${formatBytes(plan.docker.disk.totalBytes)}; ${formatBytes(plan.docker.disk.availableBytes)} free (${plan.docker.disk.usedPercent}% used)`,
430
+ ]
431
+ : []),
432
+ "",
433
+ "Project-owned files",
434
+ plan.baselines.available
435
+ ? ` ${plan.baselines.removed.length ? "→" : "✓"} ${formatCount(plan.baselines.removed.length, "old baseline generation")} selected; keeping ${plan.baselines.retained.length}`
436
+ : " ○ No active baseline found",
437
+ "",
438
+ "Disposable runtime",
439
+ !plan.runtime.detected
440
+ ? " ✓ No runtime found"
441
+ : plan.runtime.included
442
+ ? " → Current project runtime selected"
443
+ : " ○ Current project runtime preserved (add --include-runtime to select it)",
444
+ "",
445
+ "Shared Docker images",
446
+ !plan.images.requested
447
+ ? " ○ Not inspected (add --include-images to inspect older unused Supabase images)"
448
+ : !plan.images.inspected
449
+ ? " ! Docker is unavailable; images could not be inspected"
450
+ : plan.images.candidates.length
451
+ ? ` → ${formatCount(plan.images.candidates.length, "older unused Supabase image")} selected; approximately ${formatBytes(imageBytes)}`
452
+ : " ✓ No older unused Supabase images found",
453
+ ...(plan.images.candidates.length
454
+ ? plan.images.candidates.flatMap((image) =>
455
+ image.tags.map((tag) => ` ${tag}`),
456
+ )
457
+ : []),
458
+ "",
459
+ ...(result.mode === "preview"
460
+ ? selectedCount
461
+ ? [
462
+ "Nothing has been removed.",
463
+ `Cleanup set: ${plan.digest.slice(0, 12)}`,
464
+ `Exact confirmation: --confirm-cleanup=${plan.digest}`,
465
+ "Review this list, then rerun the same command with --write and the exact confirmation above.",
466
+ ]
467
+ : [
468
+ "Nothing has been removed, and nothing is currently selected.",
469
+ "Use --include-runtime or --include-images only when you want those shared resources considered.",
470
+ ]
471
+ : [
472
+ `Removed ${formatCount(result.applied.baselines.removed.length, "old baseline generation")}.`,
473
+ result.applied.runtimeRemoved
474
+ ? "Removed this project's disposable runtime and volumes."
475
+ : "Preserved this project's disposable runtime.",
476
+ `Removed ${formatCount(result.applied.imagesRemoved.length, "unused image tag")}.`,
477
+ ]),
478
+ ].join("\n");
479
+ };
480
+
481
+ export const renderSourceAccessPlan = (plan) =>
482
+ [
483
+ "SOURCE ACCESS — PREVIEW",
484
+ "",
485
+ `Target fingerprint: ${plan.review.targetFingerprint}`,
486
+ `Temporary reader: ${plan.review.reader.role} (${plan.review.reader.validForMinutes} minutes)`,
487
+ `Export schema: ${plan.review.exportSchema}`,
488
+ ...plan.review.relations.map(
489
+ (relation) =>
490
+ ` → ${relation.source.schema}.${relation.source.table} → ${relation.view}: ${relation.columns.join(", ")} (${relation.rowScope.kind})`,
491
+ ),
492
+ ...(plan.review.assets.length
493
+ ? plan.review.assets.map(
494
+ (asset) =>
495
+ ` → Storage ${asset.bucket}/${asset.prefix} (${asset.rights})`,
496
+ )
497
+ : [" ○ No source assets declared"]),
498
+ "",
499
+ "Deny checks",
500
+ ...plan.review.denyChecks.map((check) => ` ✓ ${check}`),
501
+ "",
502
+ "No source changes have been made.",
503
+ `Exact confirmation: --confirm-source-access=${plan.digest}`,
504
+ ].join("\n");
505
+
506
+ export const renderRefreshWorkflow = ({
507
+ mode,
508
+ plan,
509
+ refreshed,
510
+ runtime,
511
+ cleanup,
512
+ }) =>
513
+ [
514
+ `DATABASE COPY REFRESH — ${mode.toUpperCase()}`,
515
+ "",
516
+ `Current baseline: ${plan.review.currentBaseline}`,
517
+ `Source access expires: ${plan.review.source.readerExpiresAt}`,
518
+ `Runtime targets reset: ${plan.review.runtimeTargets.join(", ")}`,
519
+ `Baseline retention: ${formatCount(plan.review.retention.generations, "generation")}`,
520
+ ...(plan.review.retention.removeAfterReplacement.length
521
+ ? [
522
+ "Old generations removed after success:",
523
+ ...plan.review.retention.removeAfterReplacement.map(
524
+ (generation) => ` → ${generation}`,
525
+ ),
526
+ ]
527
+ : ["Old generations removed after success: none"]),
528
+ "",
529
+ ...plan.review.steps.map((step, index) => ` ${index + 1}. ${step}`),
530
+ "",
531
+ ...(mode === "preview"
532
+ ? [
533
+ "Nothing has changed. The current baseline and runtime remain intact.",
534
+ `Exact confirmation: --confirm-refresh=${plan.digest}`,
535
+ ]
536
+ : [
537
+ `Activated baseline: ${refreshed.generationId}`,
538
+ `Runtime reset: ${runtime.targets?.length ?? 1} target(s)`,
539
+ cleanup.state === "complete"
540
+ ? `Cleanup complete: ${formatCount(cleanup.removed.length, "old generation")} removed.`
541
+ : `Cleanup incomplete: ${formatCount(cleanup.remaining, "old generation")} still eligible. Run rehearsal cleanup.`,
542
+ "",
543
+ "Next: retire source access when no other refresh is needed, then run a rehearsal.",
544
+ ]),
545
+ ].join("\n");
546
+
547
+ export const renderSourceRetirement = ({ mode, plan }) =>
548
+ [
549
+ `SOURCE ACCESS RETIREMENT — ${mode.toUpperCase()}`,
550
+ `Target fingerprint: ${plan.review.targetFingerprint}`,
551
+ `Reader: ${plan.review.readerRole}`,
552
+ `Export schema: ${plan.review.exportSchema}`,
553
+ ...plan.review.views.map((view) => ` → remove view ${view}`),
554
+ "",
555
+ ...(mode === "preview"
556
+ ? [
557
+ "Nothing has been removed.",
558
+ `Exact confirmation: --confirm-source-retirement=${plan.digest}`,
559
+ ]
560
+ : [
561
+ "The exact reader, export views, and local credential files were retired.",
562
+ ]),
563
+ ].join("\n");
564
+
565
+ export const renderIdentityPlan = ({ mode, plan }) =>
566
+ [
567
+ `LOCAL IDENTITY — ${mode.toUpperCase()}`,
568
+ `Identity: ${plan.review.name}`,
569
+ `Provider: ${plan.review.provider}`,
570
+ `Approved email receipt: ${plan.review.approvedEmailSha256}`,
571
+ `Relational references: ${plan.review.references.length}`,
572
+ `Nested JSON references: ${plan.review.jsonReferences.length}`,
573
+ `Signup defaults: ${plan.review.signupDefaults.length}`,
574
+ `Storage path references: ${plan.review.pathReferences.length}`,
575
+ `Asset scopes: ${plan.review.assets.length}`,
576
+ `Token hook assertion: ${plan.review.tokenHook ? "configured" : "not configured"}`,
577
+ "",
578
+ ...(mode === "preview"
579
+ ? [
580
+ "No local account was changed.",
581
+ `Exact confirmation: --confirm-identity=${plan.digest}`,
582
+ ]
583
+ : ["The verified local identity now owns the declared copied graph."]),
584
+ ].join("\n");