@ultimat3/cli 31.47.1 → 31.49.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 (276) hide show
  1. package/CLAUDE.md +30 -23
  2. package/README.md +96 -4
  3. package/package.json +29 -29
  4. package/src/api-registration.ts +28 -14
  5. package/src/app-load.ts +6 -1
  6. package/src/app-root.ts +7 -1
  7. package/src/clean-services.ts +142 -0
  8. package/src/cmd-auth.ts +3 -0
  9. package/src/cmd-clean-spec.ts +10 -3
  10. package/src/cmd-clean.ts +23 -9
  11. package/src/cmd-db-backfill.ts +6 -0
  12. package/src/cmd-db-branch.ts +10 -4
  13. package/src/cmd-db-recount.ts +5 -12
  14. package/src/cmd-db-rollback.ts +77 -0
  15. package/src/cmd-db-spec.ts +18 -2
  16. package/src/cmd-db-status.ts +5 -10
  17. package/src/cmd-db.ts +105 -33
  18. package/src/cmd-deploy-verify.ts +14 -6
  19. package/src/cmd-docs-topic.ts +94 -0
  20. package/src/cmd-docs.ts +15 -1
  21. package/src/cmd-doctor.ts +109 -41
  22. package/src/cmd-drill.ts +100 -19
  23. package/src/cmd-env.ts +30 -8
  24. package/src/cmd-errors-app.ts +63 -0
  25. package/src/cmd-errors.ts +50 -14
  26. package/src/cmd-generate.ts +17 -4
  27. package/src/cmd-heavy-spec.ts +36 -0
  28. package/src/cmd-heavy.ts +244 -0
  29. package/src/cmd-jobs.ts +27 -1
  30. package/src/cmd-merge-driver-spec.ts +12 -0
  31. package/src/cmd-merge-driver.ts +95 -0
  32. package/src/cmd-new.ts +23 -3
  33. package/src/cmd-policy-spec.ts +9 -1
  34. package/src/cmd-policy.ts +72 -17
  35. package/src/cmd-stop.ts +18 -4
  36. package/src/cmd-tasks-run.ts +87 -0
  37. package/src/cmd-tasks-spec.ts +10 -3
  38. package/src/cmd-tasks.ts +8 -5
  39. package/src/cmd-test.ts +15 -4
  40. package/src/cmd-upgrade-spec.ts +8 -1
  41. package/src/cmd-upgrade.ts +32 -5
  42. package/src/cmd-verify.ts +9 -41
  43. package/src/codemods/agents-doc-row.ts +73 -0
  44. package/src/codemods/define-error-leaf.ts +37 -12
  45. package/src/codemods/docs-many-agents.ts +26 -0
  46. package/src/codemods/docs-worker-commands.ts +26 -0
  47. package/src/codemods/index.ts +6 -0
  48. package/src/codemods/merge-drivers.ts +66 -0
  49. package/src/command.ts +3 -0
  50. package/src/db-applied-edits.ts +117 -0
  51. package/src/db-dev-wire.ts +18 -0
  52. package/src/db-diagnostics.ts +5 -10
  53. package/src/db-generate.ts +82 -7
  54. package/src/db-migrate-pass.ts +51 -0
  55. package/src/db-migrate-through-dev.ts +209 -0
  56. package/src/db-rollback-consent.ts +128 -0
  57. package/src/db-rollback-pass.ts +76 -0
  58. package/src/db-seed-generate.ts +5 -1
  59. package/src/db-seed.ts +7 -1
  60. package/src/deploy-verify-findings.ts +3 -0
  61. package/src/deploy-verify-grade.ts +3 -0
  62. package/src/deploy-verify-processes.ts +32 -0
  63. package/src/dev-boot.ts +3 -0
  64. package/src/dev-db-migrate.ts +155 -0
  65. package/src/dev-db-rollback.ts +33 -0
  66. package/src/dev-db-settled.ts +22 -0
  67. package/src/dev-lock-errors.ts +78 -0
  68. package/src/dev-lock-holder-dir.ts +64 -0
  69. package/src/dev-lock-live.ts +44 -0
  70. package/src/dev-lock.ts +59 -84
  71. package/src/dev-policy.ts +11 -8
  72. package/src/dev-rebuild-schema.ts +116 -13
  73. package/src/dev-rebuild.ts +13 -4
  74. package/src/dev-route-table.ts +7 -0
  75. package/src/dispatch.ts +21 -1
  76. package/src/doctor-advisory-floors.ts +30 -8
  77. package/src/doctor-api-listing.ts +114 -0
  78. package/src/doctor-guarded.ts +41 -0
  79. package/src/doctor-port.ts +90 -17
  80. package/src/doctor-probe.ts +36 -5
  81. package/src/doctor-processes-probe.ts +127 -0
  82. package/src/doctor-processes.ts +136 -0
  83. package/src/doctor-warnings.ts +17 -0
  84. package/src/error-codes-agents.ts +35 -0
  85. package/src/error-codes-processes.ts +30 -0
  86. package/src/error-codes-tooling.ts +11 -3
  87. package/src/error-codes.ts +12 -13
  88. package/src/errors-suite.ts +27 -0
  89. package/src/errors.ts +18 -22
  90. package/src/exec.ts +11 -3
  91. package/src/exercise-registration.ts +4 -3
  92. package/src/framework-env.ts +7 -3
  93. package/src/generate-files.ts +1 -0
  94. package/src/generate-grant-findings.ts +7 -5
  95. package/src/generate-grants.ts +9 -8
  96. package/src/generate-kinds.ts +30 -6
  97. package/src/generate-roles-file.ts +30 -0
  98. package/src/generate-slice-policy.ts +3 -3
  99. package/src/guards.ts +2 -2
  100. package/src/heavy-cores.ts +51 -0
  101. package/src/heavy-env.ts +21 -0
  102. package/src/heavy-errors.ts +54 -0
  103. package/src/heavy-flock.ts +73 -0
  104. package/src/heavy-holders.ts +78 -0
  105. package/src/heavy-ledger.ts +400 -0
  106. package/src/heavy-memory.ts +67 -0
  107. package/src/heavy-pressure.ts +104 -0
  108. package/src/heavy-weights.ts +47 -0
  109. package/src/heavy.ts +308 -0
  110. package/src/i18n-registration.ts +22 -2
  111. package/src/island-harness.ts +2 -2
  112. package/src/job-budget.ts +4 -2
  113. package/src/job-registration.ts +64 -8
  114. package/src/jobs-queue-known.ts +43 -0
  115. package/src/local-cli.ts +6 -5
  116. package/src/mcp-errors-agents.ts +20 -0
  117. package/src/mcp-errors-doctor.ts +27 -0
  118. package/src/mcp-errors-processes.ts +23 -0
  119. package/src/mcp-errors.ts +18 -29
  120. package/src/mcp-host.ts +2 -2
  121. package/src/merge-driver-catalog.ts +58 -0
  122. package/src/merge-driver-config.ts +43 -0
  123. package/src/merge-driver-lines.ts +140 -0
  124. package/src/merge-state.ts +105 -0
  125. package/src/messages-db.ts +22 -1
  126. package/src/messages-processes.ts +13 -0
  127. package/src/messages-upgrade.ts +6 -0
  128. package/src/messages.ts +62 -1
  129. package/src/output-findings.ts +48 -21
  130. package/src/parse.ts +37 -6
  131. package/src/policy-facts.ts +38 -21
  132. package/src/policy-row.ts +99 -0
  133. package/src/port-pair-free.ts +28 -0
  134. package/src/registry-lines.ts +110 -0
  135. package/src/registry.ts +4 -0
  136. package/src/retain-sweep.ts +1 -1
  137. package/src/role-replicator.ts +9 -4
  138. package/src/role-select.ts +86 -0
  139. package/src/role-start.ts +13 -55
  140. package/src/role-sync.ts +10 -5
  141. package/src/runtime-cache-frame.ts +46 -0
  142. package/src/runtime-cache-ttl.ts +23 -0
  143. package/src/runtime-cache.ts +56 -18
  144. package/src/runtime-database-ssl.ts +84 -0
  145. package/src/runtime-idempotency-scope.ts +16 -2
  146. package/src/runtime-jobs-share.ts +52 -0
  147. package/src/runtime-jobs.ts +43 -12
  148. package/src/runtime-overrides.ts +4 -2
  149. package/src/runtime-queue.ts +18 -3
  150. package/src/runtime-services.ts +8 -1
  151. package/src/schema-drift.ts +19 -2
  152. package/src/schema-dump-drift.ts +4 -1
  153. package/src/serve-boot-probe.ts +30 -2
  154. package/src/serve-boot.ts +11 -1
  155. package/src/serve-env.ts +39 -3
  156. package/src/serve.ts +61 -10
  157. package/src/stop-run-start.ts +12 -0
  158. package/src/supervise-boot.ts +61 -0
  159. package/src/supervise-cgroup.ts +84 -0
  160. package/src/supervise-child.ts +206 -0
  161. package/src/supervise-count.ts +244 -0
  162. package/src/supervise-errors.ts +83 -0
  163. package/src/supervise-facts.ts +40 -0
  164. package/src/supervise-messages.ts +84 -0
  165. package/src/supervise-policy.ts +46 -0
  166. package/src/supervise-pool.ts +10 -0
  167. package/src/supervise-probe.ts +158 -0
  168. package/src/supervise-run.ts +54 -0
  169. package/src/supervise.ts +383 -0
  170. package/src/templates/generate-purge.ts +6 -6
  171. package/src/templates/guard-animated-layout-property.ts +2 -2
  172. package/src/templates/guard-bare-error.ts +1 -1
  173. package/src/templates/guard-error-declared.ts +2 -2
  174. package/src/templates/guard-focus-visible.ts +1 -1
  175. package/src/templates/guard-image-dimensions.ts +2 -2
  176. package/src/templates/guard-island-mount-clears.ts +1 -1
  177. package/src/templates/guard-native-confirm.ts +1 -1
  178. package/src/templates/guard-raw-breakpoint.ts +2 -2
  179. package/src/templates/guard-raw-colour.ts +2 -2
  180. package/src/templates/guard-raw-fetch.ts +1 -1
  181. package/src/templates/guard-raw-interval.ts +1 -1
  182. package/src/templates/guard-raw-length.ts +3 -3
  183. package/src/templates/guard-raw-motion.ts +1 -1
  184. package/src/templates/guard-raw-shadow.ts +1 -1
  185. package/src/templates/guard-raw-z-index.ts +1 -1
  186. package/src/templates/guard-released-forms.ts +59 -13
  187. package/src/templates/guard-repo-await-in-loop.ts +1 -1
  188. package/src/templates/guard-repo-raw-sql.ts +4 -4
  189. package/src/templates/guard-repo-unbounded.ts +1 -1
  190. package/src/templates/guard-runtime-pin.ts +1 -1
  191. package/src/templates/guard-semantic-interactive.ts +2 -2
  192. package/src/templates/guard-unbounded-fanout.ts +1 -1
  193. package/src/templates/guard-undeclared-custom-property.ts +3 -3
  194. package/src/templates/guard-undefined-style-class.ts +3 -3
  195. package/src/templates/guard-unzoned-date.ts +3 -3
  196. package/src/templates/query-slice.ts +82 -0
  197. package/src/templates/query.ts +76 -30
  198. package/src/templates/resource-page.ts +3 -2
  199. package/src/templates/scaffold-bin.ts +2 -0
  200. package/src/templates/scaffold-claude-agents.ts +9 -6
  201. package/src/templates/scaffold-claude-commands.ts +7 -1
  202. package/src/templates/scaffold-claude.ts +4 -4
  203. package/src/templates/scaffold-container-compose.ts +47 -7
  204. package/src/templates/scaffold-docs.ts +11 -6
  205. package/src/templates/scaffold-gitattributes.ts +18 -1
  206. package/src/templates/scaffold-helm-templates.ts +9 -0
  207. package/src/templates/scaffold-repo.ts +4 -1
  208. package/src/templates/scaffold-test-services.ts +33 -11
  209. package/src/templates/shapes/offline.ts +0 -7
  210. package/src/templates/shapes/realtime.ts +2 -6
  211. package/src/templates/sign-in-action-tests.ts +6 -4
  212. package/src/templates/sign-in-challenge.ts +62 -0
  213. package/src/templates/sign-in-oidc-mfa.ts +31 -0
  214. package/src/templates/sign-in-oidc.ts +11 -3
  215. package/src/templates/sign-in-password.ts +18 -56
  216. package/src/test-batches.ts +9 -3
  217. package/src/test-shards.ts +117 -37
  218. package/src/test-slots.ts +35 -161
  219. package/src/test-verdict.ts +79 -0
  220. package/src/test-workers.ts +15 -0
  221. package/src/upgrade-gate-codemods.ts +5 -3
  222. package/src/upgrade-gate.ts +19 -1
  223. package/src/upgrade-merge-drivers.ts +25 -0
  224. package/src/upgrade-report.ts +18 -4
  225. package/src/upgrade-scope.ts +62 -0
  226. package/src/verify-checks.ts +6 -11
  227. package/src/verify-coverage-run.ts +9 -5
  228. package/src/verify-deadline.ts +18 -2
  229. package/src/verify-heavy-steps.ts +26 -0
  230. package/src/verify-live.ts +38 -14
  231. package/src/verify-only.ts +67 -0
  232. package/src/verify-progress.ts +7 -3
  233. package/src/verify-run.ts +20 -2
  234. package/src/verify-services-stack.ts +165 -0
  235. package/src/verify-services.ts +84 -2
  236. package/src/verify-test-run.ts +9 -9
  237. package/src/verify-tests.ts +35 -22
  238. package/src/verify-typecheck.ts +25 -2
  239. package/wiki/Actions.md +2 -1
  240. package/wiki/Admin-Dashboard.md +2 -2
  241. package/wiki/Agents.md +5 -5
  242. package/wiki/Auth.md +15 -5
  243. package/wiki/Bare-VM.md +1 -1
  244. package/wiki/Building-Your-Own-Base.md +15 -2
  245. package/wiki/CI-Parallel-Gate.md +4 -4
  246. package/wiki/CLI-Reference.md +127 -48
  247. package/wiki/Caching-And-Invalidation.md +2 -2
  248. package/wiki/Client-Navigation.md +1 -1
  249. package/wiki/Configuration.md +32 -13
  250. package/wiki/Deployment.md +44 -4
  251. package/wiki/Entities-And-Migrations.md +5 -1
  252. package/wiki/FAQ.md +1 -1
  253. package/wiki/Getting-Started.md +2 -2
  254. package/wiki/Home.md +2 -1
  255. package/wiki/I18n.md +5 -3
  256. package/wiki/Installation.md +12 -11
  257. package/wiki/Interface-Rules.md +1 -1
  258. package/wiki/Jobs-And-Workflows.md +24 -4
  259. package/wiki/Known-Gaps.md +2 -2
  260. package/wiki/MCP-And-AI.md +8 -5
  261. package/wiki/Mail.md +1 -1
  262. package/wiki/Many-Agents.md +127 -0
  263. package/wiki/Migrations-And-Backfills.md +18 -0
  264. package/wiki/Notify.md +1 -1
  265. package/wiki/Observability.md +13 -0
  266. package/wiki/Queries-And-Live-Queries.md +2 -2
  267. package/wiki/Realtime.md +10 -1
  268. package/wiki/Routes-And-Render-Modes.md +1 -1
  269. package/wiki/Scheduled-Tasks.md +4 -1
  270. package/wiki/Testing.md +12 -9
  271. package/wiki/Timezones-And-Dates.md +5 -1
  272. package/wiki/Troubleshooting.md +1 -0
  273. package/wiki/Tutorial-01-First-App.md +5 -5
  274. package/wiki/Tutorial-02-First-Feature.md +1 -1
  275. package/wiki/Tutorial-03-Auth-And-Admin.md +7 -6
  276. package/wiki/Upgrading.md +37 -0
@@ -18,14 +18,19 @@ export const TEST_PORT_SPREAD = 500;
18
18
 
19
19
  type TestPorts = { readonly [K in keyof typeof TEST_PORTS]: number };
20
20
 
21
+ /** The app's name, and the checkout's root when the scaffold knows it: what keys its own stack. */
22
+ const stackKey = (app: NameSet, root: string | undefined): string =>
23
+ root === undefined ? app.kebab : `${app.kebab}\0${root}`;
24
+
21
25
  /**
22
26
  * This app's host ports, one place: the compose file publishes them and the env file names them.
23
27
  * One fixed set made two scaffolds on one machine share 55432, so the second app's live suites
24
- * dialled the first app's container; the app's name now picks its offset into each band.
28
+ * dialled the first app's container; the app's name — and its root, when the scaffold knows it, so
29
+ * two scaffolds of one name differ too — picks its offset into each band.
25
30
  */
26
- export const testPorts = (app: NameSet): TestPorts => {
27
- // FNV-1a over the app's name: stable across machines and runs.
28
- const offset = fnv1a(app.kebab) % TEST_PORT_SPREAD;
31
+ export const testPorts = (app: NameSet, root?: string): TestPorts => {
32
+ // FNV-1a: stable across machines and runs.
33
+ const offset = fnv1a(stackKey(app, root)) % TEST_PORT_SPREAD;
29
34
  return {
30
35
  postgres: TEST_PORTS.postgres + offset,
31
36
  redis: TEST_PORTS.redis + offset,
@@ -34,9 +39,21 @@ export const testPorts = (app: NameSet): TestPorts => {
34
39
  };
35
40
  };
36
41
 
42
+ /**
43
+ * The compose project, written into the file as its `name:` — so every reader (`x verify`,
44
+ * `x clean --services`, CI, a hand-typed `docker compose -f`) resolves the same one, and a name
45
+ * an app wrote itself is never second-guessed. `<app>-test` alone made every scaffold of one name
46
+ * on one machine one project; with the root, each scaffold's is its own (clones of it share it).
47
+ */
48
+ export const testProjectName = (app: NameSet, root?: string): string =>
49
+ root === undefined
50
+ ? `${app.kebab}-test`
51
+ : `${app.kebab}-test-${fnv1a(root).toString(16).padStart(8, '0')}`;
52
+
37
53
  const composeTest = (
38
54
  app: NameSet,
39
- ports: TestPorts = testPorts(app),
55
+ root: string | undefined,
56
+ ports: TestPorts = testPorts(app, root),
40
57
  ): string => `# The services the test suites run against — in RAM, and silent.
41
58
  #
42
59
  # docker compose -f ${TEST_COMPOSE_PATH} up -d --wait
@@ -58,7 +75,7 @@ const composeTest = (
58
75
  # watch one that will not start, run it attached: \`docker compose -f ${TEST_COMPOSE_PATH} up postgres\`.
59
76
  #
60
77
  # Every port binds 127.0.0.1: the credentials are in this file.
61
- name: ${app.kebab}-test
78
+ name: ${testProjectName(app, root)}
62
79
 
63
80
  x-silent: &silent
64
81
  logging: { driver: none }
@@ -151,7 +168,8 @@ services:
151
168
 
152
169
  const testEnv = (
153
170
  app: NameSet,
154
- ports: TestPorts = testPorts(app),
171
+ root: string | undefined,
172
+ ports: TestPorts = testPorts(app, root),
155
173
  ): string => `# Where the suites find the services ${TEST_COMPOSE_PATH} starts — the one place these URLs are
156
174
  # written. A laptop sources it (\`set -a; . ${TEST_ENV_PATH}; set +a\`); CI appends it to $GITHUB_ENV.
157
175
  # TEST_DATABASE_URL is what @ultimat3/testing clones one database per test worker from.
@@ -161,10 +179,14 @@ TEST_NATS_URL=nats://127.0.0.1:${ports.nats}
161
179
  TEST_S3_URL=http://${app.kebab}:${app.kebab}-test@127.0.0.1:${ports.s3}/${app.kebab}-test
162
180
  `;
163
181
 
164
- /** The test stack and its env file. */
165
- export function testServicesFiles(app: NameSet): readonly GeneratedFile[] {
182
+ /**
183
+ * The test stack and its env file. `root`, the absolute directory `x new` writes into, makes the
184
+ * project name and ports this scaffold's own; without it the bytes are the name-only ones every
185
+ * earlier scaffold wrote, which the codemods compare against.
186
+ */
187
+ export function testServicesFiles(app: NameSet, root?: string): readonly GeneratedFile[] {
166
188
  return [
167
- { path: TEST_COMPOSE_PATH, contents: composeTest(app) },
168
- { path: TEST_ENV_PATH, contents: testEnv(app) },
189
+ { path: TEST_COMPOSE_PATH, contents: composeTest(app, root) },
190
+ { path: TEST_ENV_PATH, contents: testEnv(app, root) },
169
191
  ];
170
192
  }
@@ -3,7 +3,6 @@
3
3
  // realtime's outbox, `createPost` safe to replay, both proven offline in a real browser, and the
4
4
  // README's recipe for a phone on the LAN (`x dev --host <address> --tls`).
5
5
 
6
- import { loadVersion } from '../../version-loader';
7
6
  import { catalogJson } from '../catalog-json';
8
7
  import { catalogPath } from '../locales';
9
8
  import type { GeneratedFile, NameSet } from '../naming';
@@ -86,12 +85,6 @@ const edits = (): readonly ShapeEdit[] => [
86
85
  replace:
87
86
  ' cache: { invalidates: [postTag] },\n // The outbox replays a write whose answer was lost under the SAME Idempotency-Key: this is what\n // makes that replay answer the first result instead of inserting a second post.\n idempotent: true,\n',
88
87
  },
89
- // The form imports `@ultimat3/realtime`; an isolated install resolves only what the app declares.
90
- {
91
- path: 'package.json',
92
- find: ' "@ultimat3/render": ',
93
- replace: ` "@ultimat3/realtime": "${loadVersion()}",\n "@ultimat3/render": `,
94
- },
95
88
  { path: 'README.md', find: README_LAYOUT, replace: `${lan()}${README_LAYOUT}` },
96
89
  ...shapeLines(
97
90
  'offline',
@@ -132,17 +132,13 @@ import * as recentPosts from '../app/post/queries/recent-posts';
132
132
  find: ' },\n});\n',
133
133
  replace: ` },\n${exerciseEntry('renamePost', 'post:write')}${exerciseEntry('recentPosts', 'post:read')}});\n`,
134
134
  },
135
- // The room and the notifier import both; an isolated install resolves only what the app declares.
135
+ // The notifier imports `@ultimat3/notify`; an isolated install resolves only what the app declares
136
+ // (`@ultimat3/realtime` is every scaffold's own dependency).
136
137
  {
137
138
  path: 'package.json',
138
139
  find: ' "@ultimat3/policy": ',
139
140
  replace: ` "@ultimat3/notify": "${loadVersion()}",\n "@ultimat3/policy": `,
140
141
  },
141
- {
142
- path: 'package.json',
143
- find: ' "@ultimat3/render": ',
144
- replace: ` "@ultimat3/realtime": "${loadVersion()}",\n "@ultimat3/render": `,
145
- },
146
142
  ...shapeLines(
147
143
  'realtime',
148
144
  "multi-user live state across nodes — a notifier tells the org's room (`apps/web/app/post/channels.ts`) of each new post over the bus; two `sync` nodes in `apps/web/app/post/channels.live.test.ts`",
@@ -77,9 +77,9 @@ export const createSessionTest =
77
77
  (): string => `// createSession, run inside a real request over the app's migrated database: a wrong password is
78
78
  // refused, a right one sets the session cookie, and a second factor still owed holds the challenge.
79
79
  import { toRoute } from '@ultimat3/action';
80
- import { generateTotpSecret, register, saveTotpSecret } from '@ultimat3/auth';
80
+ import { generateTotpSecret, MFA_CHALLENGE_COOKIE, register, saveTotpSecret } from '@ultimat3/auth';
81
81
  ${SHARED_IMPORTS}
82
- import { MFA_CHALLENGE_COOKIE, SECOND_FACTOR_PATH } from '../sign-in-challenge';
82
+ import { SECOND_FACTOR_PATH } from '../sign-in-challenge';
83
83
  import { AFTER_SIGN_IN } from '../sign-in-flow';
84
84
  import { createSession } from './create-session';
85
85
 
@@ -149,17 +149,19 @@ unitTest('a second factor still owed holds the challenge and sets no session', a
149
149
  export const confirmSessionTest =
150
150
  (): string => `// confirmSession, run inside a real request over the app's migrated database: with no challenge
151
151
  // held it refuses, a wrong code is refused, and the held challenge plus the right code signs in.
152
+ // The challenge and its cookie's name are auth's, whichever shape's first leg held it.
152
153
  import { toRoute } from '@ultimat3/action';
153
154
  import {
154
155
  generateTotpSecret,
155
156
  login,
157
+ MFA_CHALLENGE_COOKIE,
158
+ mfaChallengeOf,
156
159
  register,
157
160
  saveTotpSecret,
158
161
  totpCode,
159
162
  totpStep,
160
163
  } from '@ultimat3/auth';
161
164
  ${SHARED_IMPORTS}
162
- import { challengeOf, MFA_CHALLENGE_COOKIE } from '../sign-in-challenge';
163
165
  import { confirmSession } from './confirm-session';
164
166
 
165
167
  ${DATABASE}
@@ -184,7 +186,7 @@ const challenged = async (email: string) => {
184
186
  const secret = generateTotpSecret();
185
187
  await saveTotpSecret(appAuth(), user.id, secret);
186
188
  const first = await login(appAuth(), { email, password: PASSWORD }).catch((e: unknown) => e);
187
- const challenge = challengeOf(first);
189
+ const challenge = mfaChallengeOf(first);
188
190
  expect(challenge).not.toBeNull();
189
191
  return { secret, cookie: \`\${MFA_CHALLENGE_COOKIE}=\${challenge}\` };
190
192
  };
@@ -0,0 +1,62 @@
1
+ // `sign-in-challenge.ts` as both `x g sign-in` shapes write it: the second-factor challenge rides
2
+ // auth's ONE cookie (`__Host-x_mfa`), written, read and cleared through `@ultimat3/auth`. The
3
+ // password shape also holds it (its own first leg); OIDC's is held by `oauthLogin({ mfaPath })`.
4
+
5
+ import { SIGN_IN_PATH } from './sign-in-shared';
6
+
7
+ const HEADER: Readonly<Record<'password' | 'oidc', string>> = {
8
+ password: `// The second-factor challenge between the two legs: a sealed, five-minute credential \`login()\`
9
+ // hands back inside \`X_MFA_REQUIRED\`, carried in auth's own HttpOnly cookie — never a URL a log
10
+ // keeps — and read back by \`confirmSession\` through here.`,
11
+ oidc: `// The second-factor challenge between the provider's callback and the code. \`oauthLogin({ mfaPath })\`
12
+ // sends an enrolled user to SECOND_FACTOR_PATH with it sealed in auth's own HttpOnly cookie — never
13
+ // a URL a log keeps — and \`confirmSession\` reads it back through here.`,
14
+ };
15
+
16
+ const PATH_DOC: Readonly<Record<'password' | 'oidc', string>> = {
17
+ password:
18
+ '/** Where `createSession` sends a person who owes a code, and the page that posts it. */',
19
+ oidc: '/** The `mfaPath` `oidc.ts` hands `oauthLogin`, and the page that posts the code. */',
20
+ };
21
+
22
+ const HOLD = `
23
+ /** Auth's own line: the cookie a built-in callback sets, living as long as the challenge. */
24
+ export const holdChallenge = (challenge: string): void => {
25
+ useRequestContext('holdChallenge').headers.append('set-cookie', mfaChallengeCookie(challenge));
26
+ };
27
+ `;
28
+
29
+ export const challengeSource = (shape: 'password' | 'oidc'): string => {
30
+ const names = [
31
+ 'clearMfaChallengeCookie',
32
+ ...(shape === 'password' ? ['mfaChallengeCookie'] : []),
33
+ 'readMfaChallenge',
34
+ ];
35
+ return `${HEADER[shape]}
36
+ import { ${names.join(', ')} } from '@ultimat3/auth';
37
+ import { useRequestContext, useRequestHeaders } from '@ultimat3/http';
38
+
39
+ ${PATH_DOC[shape]}
40
+ export const SECOND_FACTOR_PATH = '${SIGN_IN_PATH}/second-factor';
41
+ ${shape === 'password' ? HOLD : ''}
42
+ export const heldChallenge = (): string | null => {
43
+ const value = readMfaChallenge({ headers: useRequestHeaders() });
44
+ return value === null || value === '' ? null : value;
45
+ };
46
+
47
+ /** Auth's own clearing line, relayed as it is: the cookie's name and attributes are auth's. */
48
+ export const dropChallenge = (): void => {
49
+ useRequestContext('dropChallenge').headers.append('set-cookie', clearMfaChallengeCookie());
50
+ };
51
+ `;
52
+ };
53
+
54
+ export const challengeTest =
55
+ (): string => `// The path the second leg lives at is the page this app serves the second-factor form at.
56
+ import { expect, unitTest } from '@ultimat3/testing';
57
+ import { SECOND_FACTOR_PATH } from './sign-in-challenge';
58
+
59
+ unitTest('the second leg lives under the sign-in page', () => {
60
+ expect(SECOND_FACTOR_PATH).toBe('${SIGN_IN_PATH}/second-factor');
61
+ });
62
+ `;
@@ -0,0 +1,31 @@
1
+ // `x g sign-in --oidc`'s second leg. An MFA-enrolled user's callback cannot finish inside
2
+ // `oauthLogin`: it 303s to `mfaPath` with the challenge sealed in auth's own HttpOnly cookie, so the
3
+ // scaffold passes that path and ships the page and the action behind it (auth README, MFA).
4
+
5
+ import type { GeneratedFile } from './naming';
6
+ import { confirmSessionTest } from './sign-in-action-tests';
7
+ import { challengeSource, challengeTest } from './sign-in-challenge';
8
+ import { secondFactorSource, signInPageTest } from './sign-in-pages';
9
+ import { confirmSessionSource, POSTS_AT } from './sign-in-password';
10
+ import { AUTH_DIR } from './sign-in-shared';
11
+
12
+ /** The `import` and the option `oidc.ts` gains: the callback's way to the second-factor page. */
13
+ export const OIDC_MFA_IMPORT = "import { SECOND_FACTOR_PATH } from './sign-in-challenge';";
14
+ export const OIDC_MFA_OPTION = 'mfaPath: SECOND_FACTOR_PATH,';
15
+
16
+ export function oidcSecondFactorFiles(module: string | undefined): readonly GeneratedFile[] {
17
+ return [
18
+ { path: `${AUTH_DIR}/sign-in-challenge.ts`, contents: challengeSource('oidc') },
19
+ { path: `${AUTH_DIR}/sign-in-challenge.test.ts`, contents: challengeTest() },
20
+ { path: `${AUTH_DIR}/actions/confirm-session.ts`, contents: confirmSessionSource() },
21
+ {
22
+ path: `${AUTH_DIR}/actions/confirm-session.test.ts`,
23
+ contents: confirmSessionTest(),
24
+ },
25
+ { path: `${AUTH_DIR}/sign-in/second-factor/page.tsx`, contents: secondFactorSource(module) },
26
+ {
27
+ path: `${AUTH_DIR}/sign-in/second-factor/page.test.ts`,
28
+ contents: signInPageTest(module, 'app.auth.secondFactor', POSTS_AT('confirmSession'), false),
29
+ },
30
+ ];
31
+ }
@@ -1,13 +1,14 @@
1
1
  // `x g sign-in --oidc <provider>`: an OpenID Connect issuer, discovered when the first sign-in
2
- // arrives (`discoverOAuthProvider`), its two legs (`oauthLogin`) mounted on `runtime.routes`, and
3
- // the page that links to the first one. PKCE, the sealed handshake, the state check and the account
4
- // link are the framework's; which IdP group means which role stays the app's (`resolveGrants`).
2
+ // arrives, its two legs (`oauthLogin`) mounted on `runtime.routes`, and the page linking the first.
3
+ // PKCE, the sealed handshake and the account link are the framework's; which IdP group means which
4
+ // role stays the app's (`resolveGrants`). The second factor's leg is `sign-in-oidc-mfa.ts`'s.
5
5
 
6
6
  // The literal the generated tests pin comes from auth itself, so the scaffold cannot drift from it.
7
7
  import { oauthStartPath } from '@ultimat3/auth';
8
8
  import { catalogJson } from './catalog-json';
9
9
  import { catalogPath } from './locales';
10
10
  import type { GeneratedFile } from './naming';
11
+ import { OIDC_MFA_IMPORT, OIDC_MFA_OPTION, oidcSecondFactorFiles } from './sign-in-oidc-mfa';
11
12
  import {
12
13
  SIGN_IN_CATALOG,
13
14
  type SignInForm,
@@ -15,6 +16,7 @@ import {
15
16
  signInPageSource,
16
17
  signInPageTest,
17
18
  } from './sign-in-pages';
19
+ import { SECOND_FACTOR_CATALOG } from './sign-in-password';
18
20
  import { OIDC_RUNTIME_ROUTES } from './sign-in-runtime';
19
21
  import { AUTH_DIR, sharedSignInFiles } from './sign-in-shared';
20
22
 
@@ -54,6 +56,7 @@ import {
54
56
  registerOAuthProvider,
55
57
  } from '@ultimat3/auth';
56
58
  import { appAuth } from './auth';
59
+ ${OIDC_MFA_IMPORT}
57
60
  import { AFTER_SIGN_IN } from './sign-in-flow';
58
61
 
59
62
  export const OIDC_PROVIDER = '${provider}';
@@ -89,6 +92,9 @@ export const oidcLegs = (env: Env = process.env): Promise<OAuthLoginRoutes> => {
89
92
  return oauthLogin(signInAuth(), {
90
93
  successPath: AFTER_SIGN_IN,
91
94
  callbackPath: OIDC_CALLBACK_PATH,
95
+ // An MFA-enrolled user finishes on this app's second-factor page; without it their
96
+ // callback is a 401 they can never get past.
97
+ ${OIDC_MFA_OPTION}
92
98
  });
93
99
  })().catch((error: unknown) => {
94
100
  legs = undefined;
@@ -192,11 +198,13 @@ export function oidcSignInFiles(
192
198
  path: `${AUTH_DIR}/sign-in/page.test.ts`,
193
199
  contents: signInPageTest(module, 'app.auth.signIn', `'href="${oauthStartPath(provider)}"'`),
194
200
  },
201
+ ...oidcSecondFactorFiles(module),
195
202
  ...options.locales.map((locale) => ({
196
203
  path: catalogPath(locale),
197
204
  contents: catalogJson({
198
205
  ...SIGN_IN_CATALOG,
199
206
  'app.auth.signIn.continue': `Continue with ${provider}`,
207
+ ...SECOND_FACTOR_CATALOG,
200
208
  }),
201
209
  merge: 'json' as const,
202
210
  })),
@@ -6,6 +6,7 @@ import { catalogJson } from './catalog-json';
6
6
  import { catalogPath } from './locales';
7
7
  import type { GeneratedFile } from './naming';
8
8
  import { confirmSessionTest, createSessionTest } from './sign-in-action-tests';
9
+ import { challengeSource, challengeTest } from './sign-in-challenge';
9
10
  import {
10
11
  SIGN_IN_CATALOG,
11
12
  type SignInForm,
@@ -14,66 +15,21 @@ import {
14
15
  signInPageSource,
15
16
  signInPageTest,
16
17
  } from './sign-in-pages';
17
- import { AUTH_DIR, SIGN_IN_PATH, sharedSignInFiles } from './sign-in-shared';
18
+ import { AUTH_DIR, sharedSignInFiles } from './sign-in-shared';
18
19
 
19
20
  /** The master key seals the second-factor challenge between the two legs. */
20
21
  export const PASSWORD_SIGN_IN_ENV: readonly string[] = ['ULTIMATE_SECRETS_KEY'];
21
22
 
22
- const challengeSource =
23
- (): string => `// The second-factor challenge between the two legs: a sealed, five-minute credential \`login()\`
24
- // hands back inside \`X_MFA_REQUIRED\`, carried in an HttpOnly cookie rather than a URL a log keeps.
25
- import { MFA_CHALLENGE_TTL_MS } from '@ultimat3/auth';
26
- import { isUltimateError } from '@ultimat3/core';
27
- import { deleteCookie, setCookie, useRequestCookie } from '@ultimat3/http';
28
-
29
- export const MFA_CHALLENGE_COOKIE = 'x_mfa_challenge';
30
- export const SECOND_FACTOR_PATH = '${SIGN_IN_PATH}/second-factor';
31
-
32
- /** The sealed challenge an \`X_MFA_REQUIRED\` carries, or \`null\` for any other throw. */
33
- export const challengeOf = (error: unknown): string | null => {
34
- if (!isUltimateError(error) || error.code !== 'X_MFA_REQUIRED') return null;
35
- const challenge = error.meta?.['challenge'];
36
- return typeof challenge === 'string' && challenge !== '' ? challenge : null;
37
- };
38
-
39
- export const holdChallenge = (challenge: string): void =>
40
- setCookie(MFA_CHALLENGE_COOKIE, challenge, { maxAge: Math.floor(MFA_CHALLENGE_TTL_MS / 1000) });
41
-
42
- export const heldChallenge = (): string | null => {
43
- const value = useRequestCookie(MFA_CHALLENGE_COOKIE);
44
- return value === null || value === '' ? null : value;
45
- };
46
-
47
- export const dropChallenge = (): void => deleteCookie(MFA_CHALLENGE_COOKIE);
48
- `;
49
-
50
- const challengeTest =
51
- (): string => `// Only an X_MFA_REQUIRED carrying a challenge is a second leg; every other throw is not.
52
- import { UltimateError } from '@ultimat3/core';
53
- import { expect, unitTest } from '@ultimat3/testing';
54
- import { challengeOf } from './sign-in-challenge';
55
-
56
- const coded = (code: string, meta?: Record<string, unknown>) =>
57
- new UltimateError({ code, cause: 'a test throw', fix: 'none', ...(meta ? { meta } : {}) });
58
-
59
- unitTest('a sealed challenge is read off X_MFA_REQUIRED, and nothing else is one', () => {
60
- expect(challengeOf(coded('X_MFA_REQUIRED', { challenge: 'sealed' }))).toBe('sealed');
61
- expect(challengeOf(coded('X_MFA_REQUIRED'))).toBeNull();
62
- expect(challengeOf(coded('X_UNAUTHENTICATED', { challenge: 'sealed' }))).toBeNull();
63
- expect(challengeOf('X_MFA_REQUIRED')).toBeNull();
64
- });
65
- `;
66
-
67
23
  const createSessionSource =
68
24
  (): string => `// Sign in with an address and a password. \`login()\` is the whole check — the hash, the lockout and
69
25
  // one indistinguishable \`X_UNAUTHENTICATED\` for every way a credential is wrong. A second factor
70
26
  // still owed stops here with the sealed challenge held for the next page.
71
27
  import { action, t } from '@ultimat3/action';
72
- import { login } from '@ultimat3/auth';
28
+ import { login, mfaChallengeOf } from '@ultimat3/auth';
73
29
  import { useRequestContext } from '@ultimat3/http';
74
30
  import { allow } from '@ultimat3/policy';
75
31
  import { appAuth } from '../auth';
76
- import { challengeOf, holdChallenge, SECOND_FACTOR_PATH } from '../sign-in-challenge';
32
+ import { holdChallenge, SECOND_FACTOR_PATH } from '../sign-in-challenge';
77
33
  import { NEXT_PARAM, nextPath } from '../sign-in-flow';
78
34
 
79
35
  export const createSession = action({
@@ -99,7 +55,7 @@ export const createSession = action({
99
55
  request.headers.append('set-cookie', signedIn.cookie);
100
56
  return { ok: true, next: nextPath(input[NEXT_PARAM]), secondFactor: false };
101
57
  } catch (error) {
102
- const challenge = challengeOf(error);
58
+ const challenge = mfaChallengeOf(error);
103
59
  if (challenge === null) throw error;
104
60
  holdChallenge(challenge);
105
61
  // The way on survives the second leg: its form posts \`next\` back to \`confirmSession\`.
@@ -114,7 +70,8 @@ export const createSession = action({
114
70
  });
115
71
  `;
116
72
 
117
- const confirmSessionSource =
73
+ /** Both shapes' second leg: it reads the challenge through `../sign-in-challenge`, whoever held it. */
74
+ export const confirmSessionSource =
118
75
  (): string => `// The second leg: the six digits (or one recovery code) against the challenge the first leg held.
119
76
  // \`completeMfa()\` meters the guess against the same lockout a password guess spends.
120
77
  import { action, t } from '@ultimat3/action';
@@ -168,19 +125,24 @@ const PASSWORD_FORM: SignInForm = {
168
125
  </form>`,
169
126
  };
170
127
 
128
+ /** The second-factor page's copy, for whichever shape reaches it. */
129
+ export const SECOND_FACTOR_CATALOG: Readonly<Record<string, string>> = {
130
+ 'app.auth.secondFactor.title': 'Second factor',
131
+ 'app.auth.secondFactor.description': 'Enter the code from your authenticator app.',
132
+ 'app.auth.secondFactor.code': 'Code',
133
+ 'app.auth.secondFactor.submit': 'Continue',
134
+ };
135
+
171
136
  const CATALOG: Readonly<Record<string, string>> = {
172
137
  ...SIGN_IN_CATALOG,
173
138
  'app.auth.signIn.email': 'Email',
174
139
  'app.auth.signIn.password': 'Password',
175
140
  'app.auth.signIn.submit': 'Sign in',
176
- 'app.auth.secondFactor.title': 'Second factor',
177
- 'app.auth.secondFactor.description': 'Enter the code from your authenticator app.',
178
- 'app.auth.secondFactor.code': 'Code',
179
- 'app.auth.secondFactor.submit': 'Continue',
141
+ ...SECOND_FACTOR_CATALOG,
180
142
  };
181
143
 
182
144
  /** The test's expectation that the page's form posts at `action`'s derived route. */
183
- const POSTS_AT = (action: string): string => `\`action="\${derivePath('${action}').path}"\``;
145
+ export const POSTS_AT = (action: string): string => `\`action="\${derivePath('${action}').path}"\``;
184
146
 
185
147
  export function passwordSignInFiles(options: SignInTemplateOptions): readonly GeneratedFile[] {
186
148
  const module = options.catalogModule;
@@ -189,7 +151,7 @@ export function passwordSignInFiles(options: SignInTemplateOptions): readonly Ge
189
151
  PASSWORD_SIGN_IN_ENV,
190
152
  'The master key seals the second-factor challenge between the two legs.',
191
153
  ),
192
- { path: `${AUTH_DIR}/sign-in-challenge.ts`, contents: challengeSource() },
154
+ { path: `${AUTH_DIR}/sign-in-challenge.ts`, contents: challengeSource('password') },
193
155
  { path: `${AUTH_DIR}/sign-in-challenge.test.ts`, contents: challengeTest() },
194
156
  { path: `${AUTH_DIR}/actions/create-session.ts`, contents: createSessionSource() },
195
157
  {
@@ -31,7 +31,7 @@
31
31
  import type { ExecResult, Runner } from './exec';
32
32
  import { execOutput } from './exec';
33
33
  import { msg } from './messages';
34
- import { SLOT_HELD_ENV } from './test-slots';
34
+ import type { MachineLease } from './test-slots';
35
35
  import { BATCH_FILES_PER_WORKER } from './test-workers';
36
36
 
37
37
  /**
@@ -89,7 +89,7 @@ export async function runBatches(input: {
89
89
  * Lease the batch's workers from the machine pool (`test-slots.ts`) before it starts, and give
90
90
  * them back when it exits — so two runs on one box share its budget batch by batch.
91
91
  */
92
- readonly lease?: (want: number) => Promise<{ readonly count: number; release(): void }>;
92
+ readonly lease?: MachineLease;
93
93
  readonly options: Parameters<Runner>[1];
94
94
  /**
95
95
  * Launch no further batch once one is red. Set for a caller-forwarded `--bail`: Bun stops a
@@ -109,7 +109,13 @@ export async function runBatches(input: {
109
109
  input.argsFor(files, width),
110
110
  lease === undefined
111
111
  ? input.options
112
- : { ...input.options, env: { ...input.options.env, [SLOT_HELD_ENV]: '1' } },
112
+ : {
113
+ ...input.options,
114
+ env: { ...input.options.env, ...lease.env },
115
+ ...(lease.fds === undefined || lease.fds.length === 0
116
+ ? {}
117
+ : { inheritFds: lease.fds }),
118
+ },
113
119
  );
114
120
  } finally {
115
121
  lease?.release();