@ultimat3/cli 31.26.0 → 31.28.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.
Files changed (206) hide show
  1. package/CLAUDE.md +47 -37
  2. package/README.md +19 -8
  3. package/package.json +29 -29
  4. package/src/app-manifest.ts +6 -1
  5. package/src/app-mcp.ts +17 -3
  6. package/src/cmd-cache-spec.ts +5 -2
  7. package/src/cmd-db-spec.ts +2 -3
  8. package/src/cmd-db.ts +0 -2
  9. package/src/cmd-deploy-verify-sample.ts +93 -0
  10. package/src/cmd-deploy-verify.ts +2 -41
  11. package/src/cmd-dev-spec.ts +15 -1
  12. package/src/cmd-dev.ts +31 -4
  13. package/src/cmd-doctor.ts +15 -4
  14. package/src/cmd-exercise-spec.ts +7 -1
  15. package/src/cmd-exercise.ts +22 -10
  16. package/src/cmd-generate.ts +2 -0
  17. package/src/cmd-jobs-spec.ts +3 -6
  18. package/src/cmd-jobs.ts +1 -7
  19. package/src/cmd-logs-spec.ts +2 -1
  20. package/src/cmd-logs.ts +9 -28
  21. package/src/cmd-planned.ts +11 -45
  22. package/src/cmd-profile-frames.ts +99 -0
  23. package/src/cmd-profile-spec.ts +24 -0
  24. package/src/cmd-profile.ts +164 -0
  25. package/src/cmd-repro-spec.ts +13 -1
  26. package/src/cmd-repro.ts +37 -5
  27. package/src/cmd-status-sample.ts +57 -0
  28. package/src/cmd-status-spec.ts +30 -0
  29. package/src/cmd-status.ts +108 -0
  30. package/src/cmd-token-spec.ts +5 -2
  31. package/src/cmd-token.ts +7 -3
  32. package/src/cmd-upgrade-codemods.ts +71 -0
  33. package/src/cmd-upgrade-spec.ts +19 -5
  34. package/src/cmd-upgrade.ts +91 -34
  35. package/src/codemods/agents-dev-mcp.ts +50 -0
  36. package/src/codemods/authenticator-session-refusal.ts +214 -0
  37. package/src/codemods/biome-no-default-export.ts +58 -0
  38. package/src/codemods/bunfig-island-coverage.ts +33 -0
  39. package/src/codemods/chart-dashboard.ts +73 -0
  40. package/src/codemods/ci-e2e-browser-stock.ts +75 -0
  41. package/src/codemods/ci-e2e-browser.ts +63 -0
  42. package/src/codemods/codemod.ts +58 -0
  43. package/src/codemods/define-error-leaf.ts +73 -0
  44. package/src/codemods/docs-ci-parts.ts +65 -0
  45. package/src/codemods/docs-gate-prose.ts +53 -0
  46. package/src/codemods/docs-island-rows.ts +31 -0
  47. package/src/codemods/docs-shipped-guards.ts +100 -0
  48. package/src/codemods/docs-test-services.ts +62 -0
  49. package/src/codemods/engine-diff.ts +104 -0
  50. package/src/codemods/engine-edits.ts +111 -0
  51. package/src/codemods/engine-fs.ts +95 -0
  52. package/src/codemods/engine.ts +191 -0
  53. package/src/codemods/format-edited-hook-3122.ts +28 -0
  54. package/src/codemods/format-edited-hook.ts +152 -0
  55. package/src/codemods/helm-alerts-objective.ts +105 -0
  56. package/src/codemods/helm-alerts-stock.ts +108 -0
  57. package/src/codemods/index.ts +56 -0
  58. package/src/codemods/metrics-probe-healthz.ts +47 -0
  59. package/src/codemods/pending.ts +36 -0
  60. package/src/codemods/repro-gitignore.ts +40 -0
  61. package/src/codemods/runtime-pin-lines.ts +109 -0
  62. package/src/codemods/runtime-pin.ts +116 -0
  63. package/src/codemods/solid-floor.ts +63 -0
  64. package/src/codemods/test-ports.ts +86 -0
  65. package/src/codemods/test-suites.ts +37 -0
  66. package/src/codemods/theme-toggle-mount-over.ts +70 -0
  67. package/src/codemods/vendored-guards.ts +186 -0
  68. package/src/codemods/verify-floor-skips.ts +88 -0
  69. package/src/codemods/verify-floor-steps.ts +90 -0
  70. package/src/coverage-island.ts +121 -0
  71. package/src/coverage-lcov.ts +49 -5
  72. package/src/coverage-remap.ts +146 -0
  73. package/src/db-replay-engine-prepared.ts +66 -0
  74. package/src/db-replay-engine-template.ts +156 -0
  75. package/src/db-replay-engine.ts +4 -0
  76. package/src/db-replay-scratch-reap.ts +54 -0
  77. package/src/deploy-verify-flags.ts +9 -2
  78. package/src/deploy-verify-grade.ts +10 -3
  79. package/src/dev-boot.ts +14 -1
  80. package/src/dev-dashboard-guard.ts +57 -10
  81. package/src/dev-host.ts +46 -0
  82. package/src/dev-tls-cache.ts +103 -0
  83. package/src/dev-tls-der.ts +69 -0
  84. package/src/dev-tls.ts +120 -0
  85. package/src/doctor-advisory-floors.ts +113 -0
  86. package/src/doctor-browser.ts +60 -0
  87. package/src/doctor-guards.ts +39 -86
  88. package/src/doctor-superseded.ts +67 -0
  89. package/src/error-codes-doctor.ts +12 -4
  90. package/src/error-codes-verify.ts +47 -0
  91. package/src/error-codes.ts +20 -23
  92. package/src/exercise-database.ts +47 -9
  93. package/src/exercise-push-device.ts +51 -0
  94. package/src/exercise-route-api.ts +74 -0
  95. package/src/exercise-routes.ts +14 -5
  96. package/src/exercise-services.ts +3 -1
  97. package/src/exercise-shard-flag.ts +47 -0
  98. package/src/fix-command.ts +2 -2
  99. package/src/generate-factories.ts +5 -11
  100. package/src/generate-feature-required.ts +9 -4
  101. package/src/generate-files.ts +26 -7
  102. package/src/generate-kinds.ts +5 -1
  103. package/src/guards-built-in-codes.ts +36 -0
  104. package/src/guards-built-in-refusal.ts +25 -0
  105. package/src/guards-built-in.ts +134 -0
  106. package/src/guards-materialize.ts +42 -0
  107. package/src/guards-paths.ts +37 -0
  108. package/src/guards-shipped-names.ts +38 -0
  109. package/src/guards-vendored.ts +73 -0
  110. package/src/guards.ts +105 -34
  111. package/src/index.ts +10 -0
  112. package/src/island-bundle-options.ts +48 -0
  113. package/src/island-bundle.ts +18 -46
  114. package/src/island-link.ts +5 -0
  115. package/src/island-outputs.ts +5 -0
  116. package/src/lint-findings.ts +3 -3
  117. package/src/mcp-errors-verify.ts +35 -0
  118. package/src/mcp-errors.ts +21 -20
  119. package/src/mcp-host-logs.ts +121 -0
  120. package/src/mcp-host.ts +12 -73
  121. package/src/measure-scope.ts +23 -3
  122. package/src/messages-floor.ts +19 -0
  123. package/src/messages-repro.ts +16 -2
  124. package/src/messages-status.ts +10 -0
  125. package/src/messages-upgrade.ts +15 -0
  126. package/src/messages.ts +22 -1
  127. package/src/registry.ts +4 -0
  128. package/src/repro-archive-collect.ts +161 -0
  129. package/src/repro-archive-write.ts +96 -0
  130. package/src/repro-archive.ts +161 -0
  131. package/src/repro-bundle.ts +8 -16
  132. package/src/repro-errors.ts +14 -2
  133. package/src/repro-framework.ts +53 -0
  134. package/src/repro-parse.ts +87 -0
  135. package/src/repro-redact.ts +218 -0
  136. package/src/repro-scrub.ts +257 -0
  137. package/src/role-start-types.ts +7 -0
  138. package/src/role-start.ts +4 -0
  139. package/src/serve-boot.ts +7 -1
  140. package/src/serve-env-tls.ts +57 -0
  141. package/src/solid-loader.ts +27 -10
  142. package/src/superseded-match.ts +87 -0
  143. package/src/superseded.ts +84 -0
  144. package/src/templates/bun-image.ts +48 -0
  145. package/src/templates/generate-export-rows.ts +136 -0
  146. package/src/templates/generate-import-rows.ts +186 -0
  147. package/src/templates/generate-rows-slice.ts +95 -0
  148. package/src/templates/github/ci.yml.ts +5 -1
  149. package/src/templates/guard-released-forms.ts +112 -0
  150. package/src/templates/guard-runtime-pin-test.ts +54 -0
  151. package/src/templates/guard-runtime-pin.ts +189 -0
  152. package/src/templates/scaffold-claude-agents.ts +6 -6
  153. package/src/templates/scaffold-claude-commands.ts +1 -1
  154. package/src/templates/scaffold-claude-hook.ts +18 -8
  155. package/src/templates/scaffold-claude.ts +2 -2
  156. package/src/templates/scaffold-db-package.ts +3 -0
  157. package/src/templates/scaffold-docs.ts +34 -30
  158. package/src/templates/scaffold-guards.ts +73 -39
  159. package/src/templates/scaffold-repo.ts +4 -8
  160. package/src/templates/scaffold-site.ts +4 -1
  161. package/src/templates/scaffold-suites.ts +72 -0
  162. package/src/templates/scaffold-test-services.ts +35 -10
  163. package/src/templates/scaffold-verify-floor.ts +53 -31
  164. package/src/templates/shapes/api.ts +4 -0
  165. package/src/templates/slice-foundation.ts +3 -3
  166. package/src/unthrown-codes.ts +3 -1
  167. package/src/upgrade-bun-floor.ts +4 -0
  168. package/src/upgrade-clean-tree.ts +54 -0
  169. package/src/upgrade-gate-codemods-install.ts +40 -0
  170. package/src/upgrade-gate-codemods.ts +151 -0
  171. package/src/upgrade-gate.ts +44 -7
  172. package/src/upgrade-guards-baseline.ts +43 -0
  173. package/src/upgrade-guards.ts +37 -2
  174. package/src/upgrade-lock.ts +118 -0
  175. package/src/upgrade-plan.ts +2 -1
  176. package/src/upgrade-report-codemods.ts +54 -0
  177. package/src/upgrade-report.ts +55 -27
  178. package/src/upgrade-signals.ts +91 -0
  179. package/src/upgrade-unfinished.ts +61 -0
  180. package/src/verify-coverage-run.ts +42 -11
  181. package/src/verify-e2e.ts +115 -4
  182. package/src/verify-floor.ts +27 -0
  183. package/src/verify-guard-baseline.ts +109 -0
  184. package/src/verify-live.ts +72 -0
  185. package/src/verify-merge-skips.ts +51 -0
  186. package/src/verify-merge.ts +36 -2
  187. package/src/verify-run.ts +48 -7
  188. package/src/verify-services.ts +287 -0
  189. package/src/verify-skip-reason.ts +53 -0
  190. package/src/verify-strict.ts +108 -0
  191. package/src/verify-test-command.ts +129 -0
  192. package/src/verify-tests.ts +39 -126
  193. package/types/babel-modules.d.ts +2 -0
  194. package/wiki/CLI-Reference.md +200 -27
  195. package/wiki/Caching-And-Invalidation.md +1 -1
  196. package/wiki/Entities-And-Migrations.md +1 -1
  197. package/wiki/Interface-Rules.md +17 -8
  198. package/wiki/Jobs-And-Workflows.md +3 -3
  199. package/wiki/Known-Gaps.md +5 -1
  200. package/wiki/MCP-And-AI.md +1 -1
  201. package/wiki/Observability.md +1 -0
  202. package/wiki/PWA-And-Offline.md +1 -1
  203. package/wiki/Project-Layout.md +4 -4
  204. package/wiki/Troubleshooting.md +3 -3
  205. package/wiki/Tutorial-05-Deploy-Free.md +2 -2
  206. package/wiki/Upgrading.md +8 -0
@@ -10,6 +10,7 @@ import { inspectJob } from '@ultimat3/jobs';
10
10
  import type { JobFailure } from '@ultimat3/testing';
11
11
  import { ReproCaptureOffError, ReproUnknownError } from './repro-errors';
12
12
  import { jobFailureOf } from './repro-job';
13
+ import { parseReproSubject } from './repro-parse';
13
14
 
14
15
  export type ReproSubject =
15
16
  | { readonly kind: 'job'; readonly failure: JobFailure }
@@ -18,27 +19,18 @@ export type ReproSubject =
18
19
  /** A uuid in any spelling `x_jobs.id` accepts — `cmd-jobs.ts`' rule, so no statement is wasted. */
19
20
  const JOB_ID = /^\{?[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}\}?$/i;
20
21
 
21
- const isBundle = (value: unknown): value is FailureBundle =>
22
- isJsonObject(value) &&
23
- typeof value['name'] === 'string' &&
24
- (value['primitive'] === 'action' || value['primitive'] === 'query') &&
25
- Array.isArray(value['reads']);
26
-
27
- const isJobFailure = (value: unknown): value is JobFailure =>
28
- isJsonObject(value) &&
29
- typeof value['jobId'] === 'string' &&
30
- typeof value['name'] === 'string' &&
31
- Array.isArray(value['steps']);
32
-
33
- /** `--from <file>`: a bundle `x repro <id> --json` printed (`data.bundle` or `data.failure`), or one bare. */
22
+ /** `--from <file>`: what `x repro <id> --json` printed (`data.bundle`/`data.failure`), a repro bundle, or bare. */
34
23
  export async function subjectFromFile(path: string): Promise<ReproSubject> {
35
24
  const file = Bun.file(path);
36
25
  if (!(await file.exists())) throw new ReproUnknownError({ id: path });
37
26
  const parsed: unknown = await file.json();
38
27
  const data = isJsonObject(parsed) && isJsonObject(parsed['data']) ? parsed['data'] : parsed;
39
- const inner = isJsonObject(data) ? (data['bundle'] ?? data['failure'] ?? data) : data;
40
- if (isBundle(inner)) return { kind: 'request', bundle: inner };
41
- if (isJobFailure(inner)) return { kind: 'job', failure: inner };
28
+ // `subject` is a repro bundle's (`x repro --bundle`): the redacted failure, read back as-is.
29
+ const inner = isJsonObject(data)
30
+ ? (data['bundle'] ?? data['failure'] ?? data['subject'] ?? data)
31
+ : data;
32
+ const subject = parseReproSubject(inner);
33
+ if (subject !== undefined) return subject;
42
34
  throw new ReproUnknownError({ id: path });
43
35
  }
44
36
 
@@ -1,5 +1,5 @@
1
- // The three ways `x repro` refuses: an id nothing holds, a capture that was never on, and a written
2
- // test that ran and did not fail the way the original did. Registered in `error-codes.ts`.
1
+ // The ways `x repro` refuses: an id nothing holds, a capture that was never on, a written test that
2
+ // did not fail the way the original did, and a file `create-ultimate repro` cannot open as a bundle.
3
3
 
4
4
  import { renderFixShellArg, UltimateError } from '@ultimat3/core';
5
5
 
@@ -45,3 +45,15 @@ export class ReproNotReproducedError extends UltimateError {
45
45
  });
46
46
  }
47
47
  }
48
+
49
+ /** `create-ultimate repro <file>` on a file this version cannot open: absent, another format, no test. */
50
+ export class ReproBundleInvalidError extends UltimateError {
51
+ constructor(input: { readonly path: string; readonly reason: string }) {
52
+ super({
53
+ code: 'X_REPRO_BUNDLE_INVALID',
54
+ cause: `${input.path} is not a repro bundle this version can open: ${input.reason}`,
55
+ fix: 'x repro <id> --bundle --json # on the failing app, then attach the file it writes',
56
+ meta: { path: input.path, reason: input.reason },
57
+ });
58
+ }
59
+ }
@@ -0,0 +1,53 @@
1
+ // Single responsibility: a framework checkout as a scratch app's `@ultimat3/*` source — which
2
+ // directory is one (its `packages/core` is `@ultimat3/core`), the one a cwd sits inside, and the
3
+ // `file:` override per package that points every install of the graph at that checkout.
4
+
5
+ // why: a cwd walk-up asks synchronously, and Bun has no directory listing.
6
+ import { existsSync, readdirSync, readFileSync } from 'node:fs';
7
+ // why: Bun exposes no path-join primitive.
8
+ import { join, resolve } from 'node:path';
9
+ import { isJsonObject } from '@ultimat3/core';
10
+
11
+ /** A checkout is the directory whose `packages/core/package.json` names `@ultimat3/core`. */
12
+ export function isFrameworkCheckout(dir: string): boolean {
13
+ const manifest = join(dir, 'packages', 'core', 'package.json');
14
+ if (!existsSync(manifest)) return false;
15
+ try {
16
+ const parsed: unknown = JSON.parse(readFileSync(manifest, 'utf8'));
17
+ return isJsonObject(parsed) && parsed['name'] === '@ultimat3/core';
18
+ } catch {
19
+ return false;
20
+ }
21
+ }
22
+
23
+ /** The checkout `cwd` is inside, walking up; `undefined` outside every one. */
24
+ export function frameworkCheckoutFrom(cwd: string): string | undefined {
25
+ let dir = resolve(cwd);
26
+ for (;;) {
27
+ if (isFrameworkCheckout(dir)) return dir;
28
+ const parent = resolve(dir, '..');
29
+ if (parent === dir) return undefined;
30
+ dir = parent;
31
+ }
32
+ }
33
+
34
+ /**
35
+ * One `file:` override per `@ultimat3/*` package under `<checkout>/packages`, names sorted.
36
+ * `overrides`, never `dependencies`: most of the graph is transitive, and only an override
37
+ * rewrites a range wherever it appears.
38
+ */
39
+ export async function frameworkOverrides(checkout: string): Promise<Record<string, string>> {
40
+ const out: Record<string, string> = {};
41
+ const packages = join(checkout, 'packages');
42
+ if (!existsSync(packages)) return out;
43
+ for (const dir of readdirSync(packages).sort()) {
44
+ const manifest = Bun.file(join(packages, dir, 'package.json'));
45
+ if (!(await manifest.exists())) continue;
46
+ const parsed: unknown = await manifest.json().catch(() => undefined);
47
+ const name = isJsonObject(parsed) ? parsed['name'] : undefined;
48
+ if (typeof name === 'string' && name.startsWith('@ultimat3/')) {
49
+ out[name] = `file:${join(packages, dir)}`;
50
+ }
51
+ }
52
+ return out;
53
+ }
@@ -0,0 +1,87 @@
1
+ // Single responsibility: a repro subject read from a file a stranger may have written (`--from`, a
2
+ // bundle `create-ultimate repro` opens). Every field the replay and the written test read is
3
+ // checked and the subject REBUILT from those alone — nothing unknown rides along — and a name,
4
+ // code, step or id holding a control character (a terminal escape, a line break) is refused.
5
+
6
+ import type { FailureBundle, FailureRead } from '@ultimat3/core';
7
+ import { isJsonObject } from '@ultimat3/core';
8
+ import type { JobFailure } from '@ultimat3/testing';
9
+ import type { ReproSubject } from './repro-bundle';
10
+
11
+ /** The actor a bundle carries; core's barrel names the bundle, not its parts. */
12
+ type FailureActor = NonNullable<FailureBundle['actor']>;
13
+
14
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: refusing them is the point.
15
+ const CONTROL = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/;
16
+
17
+ /** A string a terminal or a `//` comment can print as it stands. */
18
+ const label = (value: unknown): value is string =>
19
+ typeof value === 'string' && !CONTROL.test(value);
20
+ const labelOrNull = (value: unknown): value is string | null => value === null || label(value);
21
+ const labels = (value: unknown): value is string[] => Array.isArray(value) && value.every(label);
22
+
23
+ function actorOf(value: unknown): FailureActor | null | undefined {
24
+ if (value === null) return null;
25
+ if (!isJsonObject(value)) return undefined;
26
+ const { kind, id, orgId, roles, scopes, permissions } = value;
27
+ if (!label(kind) || !label(id) || !labelOrNull(orgId)) return undefined;
28
+ if (!labels(roles) || !labels(scopes) || !labels(permissions)) return undefined;
29
+ return { kind, id, orgId, roles, scopes, permissions };
30
+ }
31
+
32
+ function readsOf(value: unknown): FailureRead[] | undefined {
33
+ if (!Array.isArray(value)) return undefined;
34
+ const reads: FailureRead[] = [];
35
+ for (const read of value) {
36
+ if (!isJsonObject(read) || !label(read['entity']) || !isJsonObject(read['row']))
37
+ return undefined;
38
+ if (!labels(read['masked'])) return undefined;
39
+ reads.push({ entity: read['entity'], row: read['row'], masked: read['masked'] });
40
+ }
41
+ return reads;
42
+ }
43
+
44
+ function bundleOf(value: Record<string, unknown>): FailureBundle | undefined {
45
+ const { id, fingerprint, code, operation, primitive, name, at, requestId, traceId, build } =
46
+ value;
47
+ if (!label(id) || !label(fingerprint) || !label(code) || !labelOrNull(operation))
48
+ return undefined;
49
+ if ((primitive !== 'action' && primitive !== 'query') || !label(name)) return undefined;
50
+ if (typeof at !== 'number' || !Number.isFinite(at)) return undefined;
51
+ if (!labelOrNull(requestId) || !labelOrNull(traceId) || !labelOrNull(build)) return undefined;
52
+ const actor = actorOf(value['actor']);
53
+ const reads = readsOf(value['reads']);
54
+ const { masked, flags, truncated } = value;
55
+ if (actor === undefined || reads === undefined || !labels(masked) || !isJsonObject(flags)) {
56
+ return undefined;
57
+ }
58
+ if (typeof truncated !== 'boolean') return undefined;
59
+ const input = value['input'] ?? null;
60
+ return {
61
+ ...{ id, fingerprint, code, operation, primitive, name, at, requestId, traceId, build },
62
+ ...{ input, masked, actor, flags, reads, truncated },
63
+ };
64
+ }
65
+
66
+ function failureOf(value: Record<string, unknown>): JobFailure | undefined {
67
+ const { jobId, name, masked, steps, code, step } = value;
68
+ if (!label(jobId) || !label(name) || !labels(masked) || !Array.isArray(steps)) return undefined;
69
+ if (!labelOrNull(code) || !labelOrNull(step)) return undefined;
70
+ const kept: { name: string; output: unknown }[] = [];
71
+ for (const entry of steps) {
72
+ if (!isJsonObject(entry) || !label(entry['name'])) return undefined;
73
+ kept.push({ name: entry['name'], output: entry['output'] ?? null });
74
+ }
75
+ return { jobId, name, input: value['input'] ?? null, masked, steps: kept, code, step };
76
+ }
77
+
78
+ /** The subject `value` is, rebuilt from its checked fields; `undefined` for anything else. */
79
+ export function parseReproSubject(value: unknown): ReproSubject | undefined {
80
+ if (!isJsonObject(value)) return undefined;
81
+ if ('jobId' in value) {
82
+ const failure = failureOf(value);
83
+ return failure === undefined ? undefined : { kind: 'job', failure };
84
+ }
85
+ const bundle = bundleOf(value);
86
+ return bundle === undefined ? undefined : { kind: 'request', bundle };
87
+ }
@@ -0,0 +1,218 @@
1
+ // Single responsibility: a repro subject as it may leave the app. Always: input masked by its
2
+ // `.sensitive()` schema and core's `captureValue`, rows minus sealed columns, undeclared entities
3
+ // dropped. By default, every value that is not a key becomes a stand-in the replay fills
4
+ // (`@ultimat3/testing`'s `repro-stand-in.ts`) and the actor's org a placeholder; `includeRows` opts out.
5
+
6
+ import type { FailureBundle, FailureRead } from '@ultimat3/core';
7
+ import { captureValue, REDACTED } from '@ultimat3/core';
8
+ import type { EntityDescription } from '@ultimat3/entity';
9
+ import { maskSensitive } from '@ultimat3/schema';
10
+ import type { ReproSubject } from './repro-bundle';
11
+
12
+ /** The actor a bundle carries; core's barrel names the bundle, not its parts. */
13
+ type FailureActor = NonNullable<FailureBundle['actor']>;
14
+
15
+ /** What the app declared, asked by name: the registries in production, a table in a test. */
16
+ export interface ReproLookups {
17
+ /** The input schema of the job, action or query `name`; `undefined` when none is registered. */
18
+ inputSchema(kind: 'job' | 'action' | 'query', name: string): unknown;
19
+ /** The entity `name`, its description and its sealed properties; `undefined` when undeclared. */
20
+ entity(
21
+ name: string,
22
+ ): { readonly description: EntityDescription; readonly sealed: readonly string[] } | undefined;
23
+ }
24
+
25
+ export interface RedactOptions {
26
+ /** `x repro --bundle --include-rows`: real non-key values. Secrets stay masked either way. */
27
+ readonly includeRows: boolean;
28
+ }
29
+
30
+ export interface RedactedSubject {
31
+ readonly subject: ReproSubject;
32
+ /** The declarations of the entities whose rows the reproducer seeds, by name. */
33
+ readonly entities: readonly EntityDescription[];
34
+ /** Entities whose read rows were dropped: not declared by this app. Sorted, unique. */
35
+ readonly dropped: readonly string[];
36
+ /** Every masked path in the subject: input paths bare, `<entity>:<path>`, `step:<name>:<path>`. */
37
+ readonly masked: readonly string[];
38
+ }
39
+
40
+ /** The actor's org, everywhere it appears: a uuid so a uuid column still takes it. */
41
+ export const ORG_STAND_IN = '00000000-0000-4000-8000-000000000001';
42
+
43
+ /** A key by its name: `id`, `postId`, `post_id`, `tagIds`. What seeding links rows by. */
44
+ const KEY_NAME = /^id$|Ids?$|_ids?$/;
45
+
46
+ const sorted = (paths: Iterable<string>): string[] => [...new Set(paths)].sort();
47
+
48
+ /**
49
+ * Every leaf not under a key name, replaced — `REDACTED` (which the replay fills from the schema)
50
+ * or, where `typed` asks for a shape the replay cannot rebuild, `0` for a number and the boolean as
51
+ * it was. Each replaced path is listed.
52
+ */
53
+ function standIn(value: unknown, at: string, masked: string[], typed: boolean, key = ''): unknown {
54
+ if (value === REDACTED || value === null || value === undefined) return value;
55
+ if (Array.isArray(value))
56
+ return value.map((entry) => standIn(entry, `${at}[]`, masked, typed, key));
57
+ if (typeof value === 'object') {
58
+ const out: Record<string, unknown> = {};
59
+ for (const [name, entry] of Object.entries(value)) {
60
+ const path = at === '' ? name : `${at}.${name}`;
61
+ Object.defineProperty(out, name, {
62
+ value: standIn(entry, path, masked, typed, name),
63
+ enumerable: true,
64
+ writable: true,
65
+ configurable: true,
66
+ });
67
+ }
68
+ return out;
69
+ }
70
+ if (KEY_NAME.test(key)) return value;
71
+ if (typed && typeof value === 'boolean') return value;
72
+ masked.push(at);
73
+ return typed && typeof value === 'number' ? 0 : REDACTED;
74
+ }
75
+
76
+ /** The input, masked by its schema's `.sensitive()` flags, core's capture rules, then stood in. */
77
+ function maskedInput(schema: unknown, input: unknown, options: RedactOptions) {
78
+ const flagged = schema === undefined ? input : maskSensitive(schema, input, REDACTED);
79
+ const { value, masked } = captureValue(flagged);
80
+ const paths = [...masked];
81
+ const kept = options.includeRows ? value : standIn(value, '', paths, false);
82
+ return { value: kept, masked: paths };
83
+ }
84
+
85
+ /** Every string leaf equal to the actor's org, replaced by `ORG_STAND_IN`. */
86
+ function withOrg(value: unknown, org: string): unknown {
87
+ if (value === org) return ORG_STAND_IN;
88
+ if (Array.isArray(value)) return value.map((entry) => withOrg(entry, org));
89
+ if (value === null || typeof value !== 'object') return value;
90
+ const out: Record<string, unknown> = {};
91
+ for (const [name, entry] of Object.entries(value)) {
92
+ Object.defineProperty(out, name, {
93
+ value: withOrg(entry, org),
94
+ enumerable: true,
95
+ writable: true,
96
+ configurable: true,
97
+ });
98
+ }
99
+ return out;
100
+ }
101
+
102
+ function redactJob(
103
+ subject: Extract<ReproSubject, { kind: 'job' }>,
104
+ lookups: ReproLookups,
105
+ options: RedactOptions,
106
+ ): RedactedSubject {
107
+ const { failure } = subject;
108
+ const input = maskedInput(lookups.inputSchema('job', failure.name), failure.input, options);
109
+ const stepMasks: string[] = [];
110
+ const steps = failure.steps.map((step) => {
111
+ const { value, masked } = captureValue(step.output);
112
+ const paths = [...masked];
113
+ const output = options.includeRows ? value : standIn(value, '', paths, true);
114
+ stepMasks.push(...sorted(paths).map((path) => `step:${step.name}:${path}`));
115
+ return { name: step.name, output };
116
+ });
117
+ const inputMasks = sorted([...failure.masked, ...input.masked]);
118
+ return {
119
+ subject: {
120
+ kind: 'job',
121
+ failure: { ...failure, input: input.value, masked: inputMasks, steps },
122
+ },
123
+ entities: [],
124
+ dropped: [],
125
+ masked: [...inputMasks, ...stepMasks],
126
+ };
127
+ }
128
+
129
+ /** The properties a row keeps under stand-ins: its primary key, its references, and key names. */
130
+ const keysOf = (description: EntityDescription): ReadonlySet<string> =>
131
+ new Set(
132
+ description.columns
133
+ .filter((column) => column.primaryKey || column.references !== null)
134
+ .map((column) => column.property),
135
+ );
136
+
137
+ function redactRead(
138
+ read: FailureRead,
139
+ declared: { readonly description: EntityDescription; readonly sealed: readonly string[] },
140
+ options: RedactOptions,
141
+ ): FailureRead {
142
+ const keys = keysOf(declared.description);
143
+ const row = Object.fromEntries(
144
+ Object.entries(read.row).filter(([property]) => !declared.sealed.includes(property)),
145
+ );
146
+ const { value, masked } = captureValue(row);
147
+ const paths = [...read.masked, ...masked];
148
+ const kept = value as Record<string, unknown>;
149
+ if (!options.includeRows) {
150
+ for (const [property, entry] of Object.entries(kept)) {
151
+ if (keys.has(property) || KEY_NAME.test(property) || entry === null || entry === REDACTED)
152
+ continue;
153
+ kept[property] = REDACTED;
154
+ paths.push(property);
155
+ }
156
+ }
157
+ return { entity: read.entity, row: kept, masked: sorted(paths) };
158
+ }
159
+
160
+ function redactRequest(
161
+ subject: Extract<ReproSubject, { kind: 'request' }>,
162
+ lookups: ReproLookups,
163
+ options: RedactOptions,
164
+ ): RedactedSubject {
165
+ const { bundle } = subject;
166
+ const input = maskedInput(
167
+ lookups.inputSchema(bundle.primitive, bundle.name),
168
+ bundle.input,
169
+ options,
170
+ );
171
+ const dropped = new Set<string>();
172
+ const kept = new Map<string, EntityDescription>();
173
+ const reads: FailureRead[] = [];
174
+ for (const read of bundle.reads) {
175
+ const declared = lookups.entity(read.entity);
176
+ if (declared === undefined) {
177
+ dropped.add(read.entity);
178
+ continue;
179
+ }
180
+ kept.set(read.entity, declared.description);
181
+ reads.push(redactRead(read, declared, options));
182
+ }
183
+ const inputMasks = sorted([...bundle.masked, ...input.masked]);
184
+ const org = options.includeRows ? null : (bundle.actor?.orgId ?? null);
185
+ const swap = <T>(value: T): T => (org === null ? value : (withOrg(value, org) as T));
186
+ const actor: FailureActor | null =
187
+ bundle.actor === null || org === null ? bundle.actor : { ...bundle.actor, orgId: ORG_STAND_IN };
188
+ return {
189
+ subject: {
190
+ kind: 'request',
191
+ bundle: {
192
+ ...bundle,
193
+ input: swap(input.value),
194
+ masked: inputMasks,
195
+ actor,
196
+ flags: captureValue(bundle.flags).value as Record<string, unknown>,
197
+ reads: swap(reads),
198
+ truncated: bundle.truncated || dropped.size > 0,
199
+ },
200
+ },
201
+ entities: [...kept.keys()].sort().map((name) => kept.get(name) as EntityDescription),
202
+ dropped: [...dropped].sort(),
203
+ masked: [
204
+ ...inputMasks,
205
+ ...sorted(reads.flatMap((read) => read.masked.map((path) => `${read.entity}:${path}`))),
206
+ ],
207
+ };
208
+ }
209
+
210
+ export function redactSubject(
211
+ subject: ReproSubject,
212
+ lookups: ReproLookups,
213
+ options: RedactOptions,
214
+ ): RedactedSubject {
215
+ return subject.kind === 'job'
216
+ ? redactJob(subject, lookups, options)
217
+ : redactRequest(subject, lookups, options);
218
+ }