@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
package/src/dev-policy.ts CHANGED
@@ -6,8 +6,9 @@
6
6
  import { listActions } from '@ultimat3/action';
7
7
  import type { PolicyFact } from '@ultimat3/admin/dev';
8
8
  import type { NamedActor, Policy } from '@ultimat3/policy';
9
- import { policyMatrix, roleDefinitions, testActor } from '@ultimat3/policy';
9
+ import { roleDefinitions, testActor } from '@ultimat3/policy';
10
10
  import { listQueries } from '@ultimat3/query';
11
+ import { rowWithoutInput } from './policy-row';
11
12
 
12
13
  /**
13
14
  * One capability and the policy that decides it. A primitive's `capability` IS its policy's own
@@ -56,21 +57,23 @@ export function devPolicyGates(): readonly PolicyGate[] {
56
57
  /**
57
58
  * The matrix, actor by actor and capability by capability.
58
59
  *
59
- * Evaluated with no row on purpose: `/_x` asks whether an actor may reach a capability at all, and
60
- * there is no row to hand a row-level rule outside a real request. A rule that needs one therefore
61
- * shows its no-row verdict, and the trace says so rather than letting the cell read as a flat deny.
60
+ * Evaluated with no row and no input on purpose: `/_x` asks whether an actor may reach a
61
+ * capability at all, and outside a real request there is neither. A row-level rule shows its
62
+ * no-row verdict and the trace says so; a predicate that reads the input is `needs input` — the
63
+ * row `x policy explain` draws (`policy-row.ts`), never the deny `input: {}` used to produce.
62
64
  */
63
65
  export function devPolicyMatrix(): readonly PolicyFact[] {
64
66
  const actors = devActors();
65
67
  return devPolicyGates().flatMap((gate) => {
66
- const matrix = policyMatrix(gate.policy, { actors, input: {} });
67
- return matrix.rows.map(
68
+ const rows = actors.map((actor) => rowWithoutInput(gate.policy, actor));
69
+ return rows.map(
68
70
  (row): PolicyFact => ({
69
71
  permission: gate.permission,
70
72
  actorId: row.actor,
71
- allowed: row.allowed,
73
+ allowed: row.allowed === true,
74
+ verdict: row.verdict,
72
75
  trace: [
73
- `${gate.policy.label}: ${row.allowed ? 'allow' : 'deny'}`,
76
+ `${gate.policy.label}: ${row.verdict === 'needs-input' ? 'needs input' : row.verdict}`,
74
77
  ...(row.deciding === null ? [] : [`deciding rule: ${row.deciding}`]),
75
78
  ...(row.reason === null ? [] : [`reason: ${row.reason}`]),
76
79
  `enforced in: ${gate.usedBy.join(', ')}`,
@@ -1,30 +1,133 @@
1
- // The schema half of an `x dev` generation: entities ahead of the migrations, said when the
2
- // generation is built — at the boot a restart makes, and at a save — instead of first as
3
- // `X_DB_SCHEMA_STALE` on the page that queries the new column.
1
+ // The schema half of an `x dev` generation: entities ahead of the migrations, and migrations ahead
2
+ // of the database's ledger, said when the generation is built — at the boot a restart makes, and
3
+ // at a save — instead of first as `X_DB_SCHEMA_STALE` on the page that queries the new column.
4
4
 
5
+ import type { Migration } from '@ultimat3/db';
6
+ import { db, dbClientInstalled, isLedgerMissing, readLedger } from '@ultimat3/db';
7
+ import { AppMigrationsUnappliedError } from './app-migrations-verdict';
8
+ import { type AppliedRow, refuseEdited } from './db-applied-edits';
9
+ import { devCanMigrate, MIGRATE_ALONE_FIX } from './dev-db-migrate';
5
10
  import { checkSourceDrift } from './drift';
11
+ import { readMigrations } from './migrations';
6
12
  import type { Finding } from './output';
7
13
  import { findingFrom } from './output';
14
+ import { resolveServices } from './runtime-bindings';
8
15
 
9
- /** The migrate half too: `x dev` applies no migration, so `x db gen` alone leaves the page red. */
16
+ /**
17
+ * The migrate half too: `x dev` applies no migration unasked, so `x db gen` alone leaves the page
18
+ * red. Beside this process `x db migrate` is applied BY it (`dev-db-migrate.ts`), embedded or not.
19
+ */
10
20
  export const SCHEMA_AHEAD_FIX = 'x db gen "describe the change" && x db migrate';
11
21
 
22
+ /** Where this process cannot apply the pass itself: the embedded database, to the command alone. */
23
+ export const SCHEMA_AHEAD_FIX_ALONE = `x db gen "describe the change" && ${MIGRATE_ALONE_FIX}`;
24
+
25
+ /** Whether `x db migrate` works while this `x dev` runs: any external database, or a pass it takes. */
26
+ export async function migratesBesideDev(root: string): Promise<boolean> {
27
+ return resolveServices(root, Bun.env).db.mode !== 'embedded' || (await devCanMigrate(root));
28
+ }
29
+
30
+ /** What `ledgerFindings` asks — the seam a test hands rows through. */
31
+ export interface LedgerReads {
32
+ readonly migrations: (root: string) => Promise<readonly Migration[]>;
33
+ /**
34
+ * Whether this process holds a database at all. `x dev`'s boot installs its client before the
35
+ * first generation; a caller that built one with none (a test, a tool) has nothing to compare
36
+ * against, and `db()` would dial a default for it.
37
+ */
38
+ readonly database: () => boolean;
39
+ /** `undefined` when the ledger table does not exist: nothing was ever applied. */
40
+ readonly ledger: () => Promise<readonly AppliedRow[] | undefined>;
41
+ }
42
+
43
+ const READS: LedgerReads = {
44
+ migrations: readMigrations,
45
+ database: dbClientInstalled,
46
+ ledger: async () => {
47
+ try {
48
+ return await readLedger(db());
49
+ } catch (error) {
50
+ if (!isLedgerMissing(error)) throw error;
51
+ return undefined;
52
+ }
53
+ },
54
+ };
55
+
56
+ /**
57
+ * The migrations on disk against the ledger of the database this process serves. `x db gen` beside
58
+ * a running `x dev` writes a file the database has not applied: every read of the new column is a
59
+ * 500, so the generation is not green until `x db migrate` — which this process applies itself
60
+ * (`dev-db-migrate.ts`) and then rebuilds for (`dev-db-settled.ts`). An applied migration edited
61
+ * since is the other half of the same read: `X_MIGRATION_CONFLICT`, as the migrator would refuse.
62
+ */
63
+ export async function ledgerFindings(
64
+ root: string,
65
+ beside: (root: string) => Promise<boolean> = migratesBesideDev,
66
+ reads: LedgerReads = READS,
67
+ ): Promise<readonly Finding[]> {
68
+ const migrations = await reads.migrations(root);
69
+ if (migrations.length === 0 || !reads.database()) return [];
70
+ const ledger = await reads.ledger();
71
+ const applied = new Set((ledger ?? []).map((row) => row.id));
72
+ const missing = migrations.map((migration) => migration.id).filter((id) => !applied.has(id));
73
+ const findings: Finding[] = [];
74
+ if (missing.length > 0) {
75
+ const behind = findingFrom(
76
+ new AppMigrationsUnappliedError({ missing, ledger: ledger !== undefined }),
77
+ );
78
+ findings.push((await beside(root)) ? behind : { ...behind, fix: MIGRATE_ALONE_FIX });
79
+ }
80
+ try {
81
+ refuseEdited(ledger ?? [], migrations);
82
+ } catch (error) {
83
+ findings.push(findingFrom(error));
84
+ }
85
+ return findings;
86
+ }
87
+
88
+ /** Every finding this module handed a generation — by identity, since any code may be one. */
89
+ const schemaFindings = new WeakSet<Finding>();
90
+
91
+ /**
92
+ * Whether `finding` is about the schema, not about what this process loaded or mounted. A schema
93
+ * finding appearing or clearing never needs a new process (`dev-rebuild.ts`): the modules and the
94
+ * route table are the same, and this process is the one that applies the migration.
95
+ */
96
+ export const isSchemaFinding = (finding: Finding): boolean => schemaFindings.has(finding);
97
+
98
+ function marked(findings: readonly Finding[]): readonly Finding[] {
99
+ for (const finding of findings) schemaFindings.add(finding);
100
+ return findings;
101
+ }
102
+
12
103
  /**
13
- * `checkSourceDrift`'s answer with the whole instruction. Asked only of a generation that loaded
14
- * clean: a module that would not import leaves the registry short, which hashes as drift that is not
15
- * there. Never rejects — a failed check is a finding, never a dev server that will not boot.
104
+ * `checkSourceDrift`'s answer with the whole instruction, then the ledger's. Asked only of a
105
+ * generation that loaded clean: a module that would not import leaves the registry short, which
106
+ * hashes as drift that is not there. Never rejects — a failed check is a finding, never a dev
107
+ * server that will not boot.
16
108
  */
17
109
  export async function schemaAheadFindings(
18
110
  root: string,
19
111
  check: (root: string) => Promise<readonly Finding[]> = checkSourceDrift,
112
+ beside: (root: string) => Promise<boolean> = migratesBesideDev,
113
+ ledger: (
114
+ root: string,
115
+ beside: (root: string) => Promise<boolean>,
116
+ ) => Promise<readonly Finding[]> = ledgerFindings,
20
117
  ): Promise<readonly Finding[]> {
21
118
  try {
22
- return (await check(root)).map((finding) => ({
23
- ...finding,
24
- cause: `entities are ahead of the migrations: ${finding.cause}`,
25
- fix: SCHEMA_AHEAD_FIX,
26
- }));
119
+ const drifted = await check(root);
120
+ const fix =
121
+ drifted.length === 0 || (await beside(root)) ? SCHEMA_AHEAD_FIX : SCHEMA_AHEAD_FIX_ALONE;
122
+ return marked([
123
+ ...drifted.map((finding) => ({
124
+ ...finding,
125
+ cause: `entities are ahead of the migrations: ${finding.cause}`,
126
+ fix,
127
+ })),
128
+ ...(await ledger(root, beside)),
129
+ ]);
27
130
  } catch (error) {
28
- return [findingFrom(error)];
131
+ return marked([findingFrom(error)]);
29
132
  }
30
133
  }
@@ -12,9 +12,10 @@ import type { AppManifest } from './app-manifest';
12
12
  import { appManifest } from './app-manifest';
13
13
  import type { StalePin } from './app-reload-graph';
14
14
  import { takeStalePins } from './app-reload-graph';
15
+ import { onLedgerMoved } from './dev-db-settled';
15
16
  import { clearIsrForRebuild } from './dev-rebuild-isr';
16
17
  import { remountFinding, remountLine, remountReason } from './dev-rebuild-remount';
17
- import { schemaAheadFindings } from './dev-rebuild-schema';
18
+ import { isSchemaFinding, schemaAheadFindings } from './dev-rebuild-schema';
18
19
  import type { ReloadTrigger } from './dev-reload';
19
20
  import { coalesceReloads } from './dev-reload';
20
21
  import { restartFinding, restartReason } from './dev-supervisor';
@@ -127,7 +128,12 @@ export function devRebuilder(
127
128
  ): ReloadTrigger {
128
129
  // What the route table was built from: the boot's generation, for the life of this process. Each
129
130
  // save is compared with IT, not with the save before — a route added two saves ago is still a 404.
130
- const mounted = { manifest: state.manifest, findings: state.appFindings };
131
+ // The LOAD findings only: a schema finding (entities ahead, the ledger behind) appears and clears
132
+ // with no module or route moving, and a restart for one is a dead port for the `x db migrate`
133
+ // that clears it — measured with two processes, a 30 s boot refused the pass it was waiting for.
134
+ const loaded = (findings: readonly Finding[]): readonly Finding[] =>
135
+ findings.filter((finding) => !isSchemaFinding(finding));
136
+ const mounted = { manifest: state.manifest, findings: loaded(state.appFindings) };
131
137
  // What this process still holds stale, for the life of it — the same rule as `mounted`. A pinned
132
138
  // module keeps the old instance until a new process, so a LATER rebuild that changed nothing
133
139
  // (the watcher's second event for one write, a no-op save) is still a restart: it reported
@@ -141,12 +147,12 @@ export function devRebuilder(
141
147
  };
142
148
  // One rebuild at a time, and the last save wins: a tick arriving mid-build coalesces into ONE
143
149
  // trailing rebuild instead of racing the one in flight for `state.manifest` and `state.islands`.
144
- return coalesceReloads(
150
+ const trigger = coalesceReloads(
145
151
  async (file) => {
146
152
  const started = performance.now();
147
153
  beginDeviceSocketGeneration();
148
154
  const { manifest, findings, islands } = await buildGeneration(options.root, build);
149
- const remount = remountReason(mounted, { manifest, findings });
155
+ const remount = remountReason(mounted, { manifest, findings: loaded(findings) });
150
156
  state.manifest = manifest;
151
157
  state.appFindings = findings;
152
158
  state.islands = islands;
@@ -189,4 +195,7 @@ export function devRebuilder(
189
195
  state.reloadFinding = { ...findingFrom(error), at: file };
190
196
  },
191
197
  );
198
+ // A migrate or rollback applied through `/_x/db` changes no file: it asks for its generation here.
199
+ onLedgerMoved(trigger);
200
+ return trigger;
192
201
  }
@@ -17,6 +17,7 @@ import { clientErrorsSurface } from './client-errors-routes';
17
17
  import { clientScriptHeads, clientVitalsSurface } from './client-vitals-route';
18
18
  import type { DevDashboardInput } from './dev-dashboard';
19
19
  import { devDashboardRoutes } from './dev-dashboard';
20
+ import { devMigrateRoutes } from './dev-db-migrate';
20
21
  import { errorPageStyleBodies } from './error-page-csp';
21
22
  import type { IslandBundle } from './island-bundle';
22
23
  import { islandHarnessRoutes } from './island-harness-route';
@@ -151,6 +152,12 @@ export async function devRouteTable(input: DevRouteTableInput): Promise<DevRoute
151
152
  const vitals = clientVitalsSurface(reporting?.vitals);
152
153
  const routes: readonly Route[] = [
153
154
  ...devDashboardRoutes(input.dashboard),
155
+ // `x db migrate` beside this process: applied here, by the one holder of the embedded database.
156
+ ...devMigrateRoutes({
157
+ root: input.root,
158
+ env: input.env,
159
+ devUrl: () => input.dashboard.status().url,
160
+ }),
154
161
  // The same API table the container serves: a read that answers here and 404s in production
155
162
  // is exactly the drift one composition exists to prevent.
156
163
  ...apiRoutes(),
package/src/dispatch.ts CHANGED
@@ -93,6 +93,23 @@ const errorResult = (command: string, error: unknown): CommandResult => ({
93
93
  * Returns the exit code instead of calling process.exit, so a test can drive the whole CLI end to
94
94
  * end without terminating the test runner.
95
95
  */
96
+ /**
97
+ * `argv` as a refusal's fix pastes it back: every word typed, less `--cwd` (the fix names the
98
+ * directory itself) and the JSON switch (it ends with one). Rebuilt from the command's name alone,
99
+ * `x db migrate` came back as `x db`, a second refusal. Cut at a bare `--`: the fix appends its
100
+ * `--json` last, and behind a passthrough tail that switch would be the other tool's.
101
+ */
102
+ export function typedWords(argv: readonly string[]): readonly string[] {
103
+ const words: string[] = [];
104
+ for (let index = 0; index < argv.length; index += 1) {
105
+ const word = argv[index] as string;
106
+ if (word === '--') break;
107
+ if (word === '--cwd') index += 1;
108
+ else if (word !== '--json' && word !== '-j' && !word.startsWith('--cwd=')) words.push(word);
109
+ }
110
+ return words;
111
+ }
112
+
96
113
  export async function dispatch(options: DispatchOptions): Promise<number> {
97
114
  const earlyReader = readerFor(options.cwd, options.invocation);
98
115
  // Its own branch, ahead of the parse: an unsupported Bun is a fact about the environment and
@@ -155,6 +172,7 @@ export async function dispatch(options: DispatchOptions): Promise<number> {
155
172
  env,
156
173
  bunVersion: options.bunVersion,
157
174
  ...(options.invocation === undefined ? {} : { invocation: options.invocation }),
175
+ reader,
158
176
  };
159
177
 
160
178
  // Put back in the `finally`: the stream is process-wide, so a dispatch that left `stderr` behind
@@ -179,7 +197,9 @@ export async function dispatch(options: DispatchOptions): Promise<number> {
179
197
  // (`env init`, `secrets set`) — but the DECLARATION is now what decides, ahead of any check a
180
198
  // command makes about its own arguments. `--help` is exempt because `target` is then the help
181
199
  // command, which declares nothing: usage for a command must be readable from anywhere.
182
- if (target.spec.requiresApp === true) requireAppRoot(target.spec.name, ctx.cwd);
200
+ if (target.spec.requiresApp === true) {
201
+ requireAppRoot(target.spec.name, ctx.cwd, typedWords(options.argv));
202
+ }
183
203
  const result = await target.run(ctx);
184
204
  sinkFor(result, options)(render(result, args.json, args.flags.get('verbose') === true, reader));
185
205
  // `x dev` and `x mcp serve --transport http` are still listening here: report first, so the
@@ -79,6 +79,35 @@ async function lockPackages(root: string): Promise<Readonly<Record<string, unkno
79
79
  }
80
80
  }
81
81
 
82
+ /** What `bun.lock` resolves for this row below its floor, lowest first. */
83
+ function versionsBelow(
84
+ packages: Readonly<Record<string, unknown>>,
85
+ row: AdvisoryFloor,
86
+ ): readonly string[] {
87
+ // A git, tarball or `file:` head is no release on npm's line, so no advisory range reads it:
88
+ // skipped rather than ordered, because `Bun.semver.order` throws on it and would take every
89
+ // other `x doctor` check down with this warning.
90
+ return resolvedVersions(packages, row.name)
91
+ .filter((version) => RELEASE.test(version))
92
+ .filter((version) => Bun.semver.order(version, row.floor) < 0)
93
+ .sort((a, b) => Bun.semver.order(a, b));
94
+ }
95
+
96
+ /**
97
+ * The transitive packages `bun.lock` holds under a floor: `bun install` honours the lockfile, so
98
+ * `x upgrade` names these to `bun update`, and an upgraded app is not left to `x doctor`'s warning.
99
+ */
100
+ export async function staleTransitiveFloors(
101
+ root: string,
102
+ floors: readonly AdvisoryFloor[] = ADVISORY_FLOORS,
103
+ ): Promise<readonly string[]> {
104
+ const packages = await lockPackages(root);
105
+ if (packages === undefined) return [];
106
+ return floors
107
+ .filter((row) => row.via === 'transitive' && versionsBelow(packages, row).length > 0)
108
+ .map((row) => row.name);
109
+ }
110
+
82
111
  /** One warning per floor the lockfile resolves under, lowest resolved version named. */
83
112
  export async function advisoryFloorFindings(
84
113
  root: string,
@@ -88,14 +117,7 @@ export async function advisoryFloorFindings(
88
117
  if (packages === undefined) return [];
89
118
  const transitive = floors.filter((f) => f.via === 'transitive').map((f) => quoteArg(f.name));
90
119
  return floors.flatMap((row) => {
91
- // A git, tarball or `file:` head is no release on npm's line, so no advisory range reads it:
92
- // skipped rather than ordered, because `Bun.semver.order` throws on it and would take every
93
- // other `x doctor` check down with this warning.
94
- const below = resolvedVersions(packages, row.name)
95
- .filter((version) => RELEASE.test(version))
96
- .filter((version) => Bun.semver.order(version, row.floor) < 0)
97
- .sort((a, b) => Bun.semver.order(a, b));
98
- const lowest = below[0];
120
+ const lowest = versionsBelow(packages, row)[0];
99
121
  if (lowest === undefined) return [];
100
122
  return [
101
123
  {
@@ -0,0 +1,114 @@
1
+ // `x doctor`'s warning for an action or query no `defineApi()` lists in an app whose `defineApi()`
2
+ // lists others: the scan still serves it (never refused), but the typed client's `Api` lacks it. An
3
+ // app that lists none registers by the scan alone, and is told nothing.
4
+
5
+ // why: Bun has no synchronous exists check and no path API; `posix` because the fix prints an
6
+ // import specifier on every OS.
7
+ import { existsSync } from 'node:fs';
8
+ import { join, posix } from 'node:path';
9
+ import { isAction } from '@ultimat3/action';
10
+ import { isQuery } from '@ultimat3/query';
11
+ import { loadApp } from './app-load';
12
+ import { API_INDEX } from './app-root';
13
+ import type { Finding } from './output';
14
+ import { camel } from './templates/naming';
15
+
16
+ /** The load and the modules it imported — the seam a test drives without a scan. */
17
+ type ListingLoader = (
18
+ root: string,
19
+ ) => Promise<{ readonly findings: readonly Finding[]; readonly files?: readonly string[] }>;
20
+
21
+ type ListKey = 'actions' | 'queries';
22
+
23
+ /** One primitive a module exports, under the name it serves by. */
24
+ interface Exported {
25
+ readonly key: ListKey;
26
+ readonly name: string;
27
+ readonly file: string;
28
+ }
29
+
30
+ /**
31
+ * What `defineApi()` returns: a frozen plain object, one name-keyed map per list. Read by shape,
32
+ * and only once it IS a plain object — an index also exports typed clients, whose property reads
33
+ * are a proxy's and build a transport (one threw X_ENV_MISSING for an unset APP_URL).
34
+ */
35
+ const listedIn = (value: unknown, key: ListKey): readonly string[] => {
36
+ if (typeof value !== 'object' || value === null || !Object.isFrozen(value)) return [];
37
+ if (Object.getPrototypeOf(value) !== Object.prototype) return [];
38
+ const api = value as Readonly<Record<string, unknown>>;
39
+ if (!['actions', 'queries', 'jobs', 'tasks'].every((one) => Object.hasOwn(api, one))) return [];
40
+ const list = api[key];
41
+ return typeof list === 'object' && list !== null ? Object.keys(list) : [];
42
+ };
43
+
44
+ /** `refund-ticket.ts` → `refundTicket`: the namespace the index binds, as `x g` names it. */
45
+ const aliasOf = (file: string): string => {
46
+ const alias = camel(posix.basename(file).replace(/\.tsx?$/, ''));
47
+ return /^[A-Za-z_]/.test(alias) ? alias : `module${alias}`;
48
+ };
49
+
50
+ /** The import the API index needs, relative to it and without the extension. */
51
+ const importLine = (file: string, alias: string): string => {
52
+ const path = posix.relative(posix.dirname(API_INDEX), file).replace(/\.tsx?$/, '');
53
+ return `import * as ${alias} from '${path.startsWith('.') ? path : `./${path}`}';`;
54
+ };
55
+
56
+ /** Pure: one warning per exported primitive whose name no list carries. */
57
+ export function unlistedFinding(primitive: Exported): Finding {
58
+ const alias = aliasOf(primitive.file);
59
+ const kind = primitive.key === 'actions' ? 'action' : 'query';
60
+ return {
61
+ code: primitive.key === 'actions' ? 'X_ACTION_UNREGISTERED' : 'X_QUERY_UNREGISTERED',
62
+ cause: `${primitive.name} declared in ${primitive.file} is an ${kind} no defineApi() lists: the module scan still serves it and the manifest carries it, but the typed client's Api does not, and a ${kind === 'action' ? 'job' : 'module'} left off the same list is refused`,
63
+ fix: `${importLine(primitive.file, alias)} // in ${API_INDEX}, and ${alias} added to ${primitive.key}: [...] there — then x manifest`,
64
+ at: primitive.file,
65
+ };
66
+ }
67
+
68
+ /** Every action and query the loaded modules export, once each, first declaring file kept. */
69
+ async function exportedPrimitives(root: string, files: readonly string[]): Promise<Exported[]> {
70
+ const seen = new Set<unknown>();
71
+ const found: Exported[] = [];
72
+ for (const file of files) {
73
+ const module = (await import(join(root, file))) as Readonly<Record<string, unknown>>;
74
+ for (const [exportName, value] of Object.entries(module)) {
75
+ const key: ListKey | undefined = isAction(value)
76
+ ? 'actions'
77
+ : isQuery(value)
78
+ ? 'queries'
79
+ : undefined;
80
+ if (key === undefined || seen.has(value)) continue;
81
+ seen.add(value);
82
+ // The scan names a primitive after its export; one it never reached keeps `''`.
83
+ const name = (value as { readonly name: string }).name || exportName;
84
+ found.push({ key, name, file });
85
+ }
86
+ }
87
+ return found;
88
+ }
89
+
90
+ /**
91
+ * The warnings. Loads the app as every gate step does and says nothing when the load failed (a short
92
+ * registry is the load's finding) or there is no index (nothing to list into). What the index
93
+ * exports from `defineApi()` is the list; a module it imports from a package is listed there too.
94
+ */
95
+ export async function unlistedPrimitiveFindings(
96
+ root: string,
97
+ load: ListingLoader = loadApp,
98
+ ): Promise<readonly Finding[]> {
99
+ const app = await load(root);
100
+ if (app.findings.length > 0 || !existsSync(join(root, API_INDEX))) return [];
101
+ const index = (await import(join(root, API_INDEX))) as Readonly<Record<string, unknown>>;
102
+ const listed = { actions: new Set<string>(), queries: new Set<string>() };
103
+ for (const value of Object.values(index)) {
104
+ for (const key of ['actions', 'queries'] as const) {
105
+ for (const name of listedIn(value, key)) listed[key].add(name);
106
+ }
107
+ }
108
+ // Inconsistency only: an app that lists nothing registers by the scan alone, a supported shape
109
+ // (dummy/social-media-clone), and every warning there would be noise about it.
110
+ if (listed.actions.size === 0 && listed.queries.size === 0) return [];
111
+ return (await exportedPrimitives(root, app.files ?? []))
112
+ .filter((primitive) => !listed[primitive.key].has(primitive.name))
113
+ .map(unlistedFinding);
114
+ }
@@ -0,0 +1,41 @@
1
+ // One `x doctor` check that throws costs that check, never the report. The whole command used to
2
+ // die as `X_CLI_UNEXPECTED`, whose fix is `x doctor --json`: the diagnostic told you to run itself.
3
+
4
+ import { ERROR_DOCS_URL, renderThrowable } from '@ultimat3/core';
5
+ import type { Finding } from './output';
6
+
7
+ export const probeFailedFinding = (check: string, error: unknown): Finding => ({
8
+ code: 'X_DOCTOR_PROBE_FAILED',
9
+ cause: `the ${check} check could not run: ${renderThrowable(error)}`,
10
+ // `x manifest` loads the app the same way and names the module that fails.
11
+ fix: 'bunx x manifest --json',
12
+ docs: ERROR_DOCS_URL,
13
+ });
14
+
15
+ /** Runs `check`, recording a throw on `findings` as that check's own finding. */
16
+ export async function guardedCheck(
17
+ name: string,
18
+ findings: Finding[],
19
+ check: () => Promise<void>,
20
+ ): Promise<void> {
21
+ try {
22
+ await check();
23
+ } catch (error) {
24
+ findings.push(probeFailedFinding(name, error));
25
+ }
26
+ }
27
+
28
+ /** A listing beside the verdict: its value, or `otherwise` with the failure on `warnings`. */
29
+ export async function guardedListing<T>(
30
+ name: string,
31
+ warnings: Finding[],
32
+ read: () => Promise<T>,
33
+ otherwise: T,
34
+ ): Promise<T> {
35
+ try {
36
+ return await read();
37
+ } catch (error) {
38
+ warnings.push(probeFailedFinding(name, error));
39
+ return otherwise;
40
+ }
41
+ }