@lunora/cli 1.0.0-alpha.32 → 1.0.0-alpha.320

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 (122) hide show
  1. package/LICENSE.md +33 -0
  2. package/README.md +1 -1
  3. package/dist/bin.mjs +2 -10
  4. package/dist/index.d.mts +960 -370
  5. package/dist/index.d.ts +960 -370
  6. package/dist/index.mjs +1 -19
  7. package/dist/packem_chunks/dispatch.mjs +1 -0
  8. package/dist/packem_chunks/handler.mjs +1 -160
  9. package/dist/packem_chunks/handler10.mjs +1 -16
  10. package/dist/packem_chunks/handler11.mjs +2 -22
  11. package/dist/packem_chunks/handler12.mjs +1 -192
  12. package/dist/packem_chunks/handler13.mjs +1 -131
  13. package/dist/packem_chunks/handler14.mjs +1 -65
  14. package/dist/packem_chunks/handler15.mjs +1 -58
  15. package/dist/packem_chunks/handler16.mjs +3 -80
  16. package/dist/packem_chunks/handler17.mjs +1 -43
  17. package/dist/packem_chunks/handler18.mjs +1 -105
  18. package/dist/packem_chunks/handler19.mjs +7 -170
  19. package/dist/packem_chunks/handler2.mjs +1 -114
  20. package/dist/packem_chunks/handler20.mjs +1 -94
  21. package/dist/packem_chunks/handler21.mjs +3 -94
  22. package/dist/packem_chunks/handler22.mjs +3 -0
  23. package/dist/packem_chunks/handler23.mjs +1 -0
  24. package/dist/packem_chunks/handler24.mjs +2 -0
  25. package/dist/packem_chunks/handler25.mjs +99 -0
  26. package/dist/packem_chunks/handler26.mjs +10 -0
  27. package/dist/packem_chunks/handler3.mjs +1 -204
  28. package/dist/packem_chunks/handler4.mjs +1 -33
  29. package/dist/packem_chunks/handler5.mjs +1 -49
  30. package/dist/packem_chunks/handler6.mjs +1 -91
  31. package/dist/packem_chunks/handler7.mjs +3 -42
  32. package/dist/packem_chunks/handler8.mjs +1 -174
  33. package/dist/packem_chunks/handler9.mjs +1 -315
  34. package/dist/packem_chunks/planDevCommand.mjs +7 -541
  35. package/dist/packem_chunks/runCodegenCommand.mjs +4 -52
  36. package/dist/packem_chunks/runDeployCommand.mjs +7 -594
  37. package/dist/packem_chunks/runInitCommand.mjs +162 -1430
  38. package/dist/packem_chunks/runResetCommand.mjs +1 -41
  39. package/dist/packem_chunks/runRpcCommand.mjs +1 -68
  40. package/dist/packem_shared/COMMANDS-DdaAWPtr.mjs +1 -0
  41. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-BgMPHEoe.mjs +1 -0
  42. package/dist/packem_shared/EXIT_CODE-08cwt3MK.mjs +1 -0
  43. package/dist/packem_shared/admin-token-VdUnvnKW.mjs +1 -0
  44. package/dist/packem_shared/admin-url-BhF5ufg1.mjs +1 -0
  45. package/dist/packem_shared/binding-manifest-file-CQ1eQYy1.mjs +2 -0
  46. package/dist/packem_shared/buildRegistryIndex-Dc8D8AR6.mjs +1 -0
  47. package/dist/packem_shared/catalog-CahzmDLV.mjs +1 -0
  48. package/dist/packem_shared/cli-DOpChqUe.mjs +2 -0
  49. package/dist/packem_shared/codegen-error-AmH54ofi.mjs +3 -0
  50. package/dist/packem_shared/command-Mmxzxyr5.mjs +1 -0
  51. package/dist/packem_shared/commands-CNqednoX.mjs +13 -0
  52. package/dist/packem_shared/createLogger-Cl18I8AX.mjs +2 -0
  53. package/dist/packem_shared/createRecordingSpawner-sS7LEN7x.mjs +1 -0
  54. package/dist/packem_shared/deploy-target-DCWwiuAe.mjs +1 -0
  55. package/dist/packem_shared/diffSnapshots-WWwx-KvZ.mjs +5 -0
  56. package/dist/packem_shared/docker-DpVxvYpL.mjs +1 -0
  57. package/dist/packem_shared/import-D7qdpSmJ.mjs +12 -0
  58. package/dist/packem_shared/insertSchemaExtension-DuiV6cba.mjs +8 -0
  59. package/dist/packem_shared/lint-ignore-report-DKZagpqk.mjs +2 -0
  60. package/dist/packem_shared/open-url-EnKy--w-.mjs +1 -0
  61. package/dist/packem_shared/parseManifest-CwPTKdtS.mjs +1 -0
  62. package/dist/packem_shared/path-containment-CgxYZggb.mjs +1 -0
  63. package/dist/packem_shared/platform-diagnostics-Cn2g6Jh-.mjs +4 -0
  64. package/dist/packem_shared/prompt-cancelled-BvsNxg_Q.mjs +1 -0
  65. package/dist/packem_shared/render-lunora-error--4tmM6mt.mjs +3 -0
  66. package/dist/packem_shared/resolve-DUCSc7jQ.mjs +5 -0
  67. package/dist/packem_shared/resolve-target-C_ZloTvd.mjs +1 -0
  68. package/dist/packem_shared/runAddCommand-Cf2q2y27.mjs +1 -0
  69. package/dist/packem_shared/runExportCommand-DxsJHYZt.mjs +5 -0
  70. package/dist/packem_shared/runMigrateGenerateCommand-CDHswpMU.mjs +11 -0
  71. package/dist/packem_shared/schema-drift-gate-BDCkQ1S6.mjs +1 -0
  72. package/dist/packem_shared/schemaIrToSnapshot-BjK-0IRo.mjs +1 -0
  73. package/dist/packem_shared/shared-D-zCOmgY.mjs +1 -0
  74. package/dist/packem_shared/storage-J4xhM4FU.mjs +1 -0
  75. package/dist/packem_shared/tui-prompts-B3YwUhGw.mjs +4 -0
  76. package/dist/packem_shared/vectorize-metadata-dXpl2_Ar.mjs +1 -0
  77. package/dist/packem_shared/wrangler-name-Dsk5K1f-.mjs +1 -0
  78. package/dist/packem_shared/wrangler-secrets-C9lJnd5N.mjs +1 -0
  79. package/package.json +39 -19
  80. package/skills/README.md +35 -17
  81. package/skills/lunora/SKILL.md +123 -9
  82. package/skills/lunora-create-package/SKILL.md +4 -3
  83. package/skills/lunora-deploy/SKILL.md +42 -11
  84. package/skills/lunora-functions/SKILL.md +120 -15
  85. package/skills/lunora-migration-helper/SKILL.md +74 -21
  86. package/skills/lunora-performance-audit/SKILL.md +78 -9
  87. package/skills/lunora-quickstart/SKILL.md +93 -27
  88. package/skills/lunora-realtime/SKILL.md +73 -39
  89. package/skills/lunora-setup-auth/SKILL.md +33 -6
  90. package/skills/lunora-setup-hyperdrive/SKILL.md +33 -13
  91. package/skills/lunora-setup-hyperdrive-global/SKILL.md +5 -0
  92. package/skills/lunora-setup-mail/SKILL.md +34 -28
  93. package/skills/lunora-setup-scheduler/SKILL.md +18 -14
  94. package/skills/lunora-setup-storage/SKILL.md +182 -25
  95. package/dist/packem_chunks/runMigrateGenerateCommand.mjs +0 -397
  96. package/dist/packem_shared/COMMANDS-B0ftFD_3.mjs +0 -948
  97. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +0 -244
  98. package/dist/packem_shared/admin-url-4UzT-CI4.mjs +0 -19
  99. package/dist/packem_shared/api-spec-CtA6ilu4.mjs +0 -13
  100. package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +0 -38
  101. package/dist/packem_shared/codegen-error-DJG-ghs_.mjs +0 -31
  102. package/dist/packem_shared/command-D3lB_4Az.mjs +0 -19
  103. package/dist/packem_shared/commands-B-gR09Z_.mjs +0 -845
  104. package/dist/packem_shared/createLogger-B40gPzQo.mjs +0 -78
  105. package/dist/packem_shared/createRecordingSpawner-DxI3mebw.mjs +0 -43
  106. package/dist/packem_shared/detect-package-manager-DYp7n3mJ.mjs +0 -61
  107. package/dist/packem_shared/diffSnapshots-BeDvvNiF.mjs +0 -161
  108. package/dist/packem_shared/docker-hMQ97KSQ.mjs +0 -21
  109. package/dist/packem_shared/insertSchemaExtension-DAqbfr9Z.mjs +0 -64
  110. package/dist/packem_shared/open-url-Dfq6fAyT.mjs +0 -41
  111. package/dist/packem_shared/output-format-wUvAN6AL.mjs +0 -17
  112. package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +0 -56
  113. package/dist/packem_shared/parseManifest--vZf2FY1.mjs +0 -94
  114. package/dist/packem_shared/prompt-cancelled-APzX1Im-.mjs +0 -9
  115. package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +0 -16
  116. package/dist/packem_shared/runAddCommand-bnY6-HKb.mjs +0 -4
  117. package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +0 -79
  118. package/dist/packem_shared/schemaIrToSnapshot-DdsljJT-.mjs +0 -43
  119. package/dist/packem_shared/storage-BIsph-Vk.mjs +0 -84
  120. package/dist/packem_shared/tui-prompts-BjEN8XgP.mjs +0 -658
  121. package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +0 -12
  122. package/dist/packem_shared/wrangler-secrets-P2_ZUR-k.mjs +0 -47
@@ -29,9 +29,16 @@ D1 database, R2 buckets, and secrets — so the work is mostly making
29
29
 
30
30
  `lunora deploy` runs a fixed pipeline:
31
31
 
32
- 1. **Codegen** — regenerates `lunora/_generated/` and typechecks.
33
- 2. **Validate `wrangler.jsonc`** — required `compatibility_date`, the
34
- `nodejs_compat` flag, and the `SHARD` Durable Object binding.
32
+ 1. **Codegen** — regenerates `lunora/_generated/`. It does **not** run
33
+ `tsc`: nothing in the deploy pipeline type-checks, and wrangler/esbuild
34
+ strips types, so a project with a TS error deploys. Run `lunora verify`
35
+ (which does run `tsc --noEmit`) as the CI type gate before this.
36
+ 2. **Validate `wrangler.jsonc`** — a `compatibility_date` at or past the
37
+ minimum the runtime needs, the `SHARD` Durable Object binding and its
38
+ SQLite migration tag, and the shape of every declared binding. It does
39
+ **not** check `compatibility_flags`: `nodejs_compat` is what every
40
+ template ships and what dependencies reaching for `node:` builtins need,
41
+ but no Lunora package requires it, so nothing fails when it is missing.
35
42
  3. **Schema-drift gate** — blocks if the committed baseline
36
43
  (`lunora/.lunora-schema.json`) drifted with a breaking change and no
37
44
  accompanying migration. The baseline is re-blessed only after the deploy
@@ -40,9 +47,23 @@ D1 database, R2 buckets, and secrets — so the work is mostly making
40
47
  images).
41
48
 
42
49
  Useful flags: `--env <name>` (Cloudflare environment), `--migrate` (run pending
43
- data migrations against the live worker after deploy, with `--migrate-token` /
44
- `--migrate-url`), `--allow-schema-drift` (override the gate — use sparingly), and
45
- `--update-schema-baseline` (re-bless the baseline with the current shape).
50
+ data migrations against the live worker after deploy), `--allow-schema-drift`
51
+ (override the gate — use sparingly), and `--update-schema-baseline` (re-bless
52
+ the baseline with the current shape).
53
+
54
+ `--migrate` has three hard requirements, checked **before** `wrangler deploy`,
55
+ so missing one aborts the whole deploy rather than skipping the migration:
56
+
57
+ - `--migrate-yes` — confirms running production data migrations. Always
58
+ required; there is no environment variable for it.
59
+ - `--migrate-url <https://…>` — the deploy target URL is only known after
60
+ wrangler runs, so it cannot be inferred. Omit it only when the checkout is
61
+ linked (`lunora link`) for this same `--env`.
62
+ - an admin token — `--migrate-token` or `LUNORA_ADMIN_TOKEN`.
63
+
64
+ ```bash
65
+ lunora deploy --migrate --migrate-yes --migrate-url https://app.example.com
66
+ ```
46
67
 
47
68
  ## Preflight: `lunora doctor`
48
69
 
@@ -119,10 +140,19 @@ before the first request hits production.
119
140
  - **Production:** `lunora deploy`. Separate D1 database / R2 buckets / secrets
120
141
  from dev. Never point a dev worker at prod resources.
121
142
 
122
- For `.global()` table DDL, generate and commit SQL migrations with `lunora
123
- migrate generate` before deploying; `@lunora/d1`'s runner applies them. For data
124
- backfills, deploy first, then `lunora deploy --migrate` (or `lunora migrate up
125
- --prod`). See `lunora-migration-helper`.
143
+ For `.global()` table DDL, `lunora migrate generate` emits a reviewable SQL
144
+ file — but **no command applies it**, `lunora deploy` included. What actually
145
+ provisions a global table is the runtime, from the schema rather than from the
146
+ file: on first use it runs `CREATE TABLE IF NOT EXISTS` plus additive
147
+ `ADD COLUMN`, so additive changes land whether or not the file exists.
148
+ Anything the generator cannot express — a backfilling `UPDATE`, a rename
149
+ written as add/copy/drop, an index the schema does not declare — must be
150
+ applied by hand:
151
+ `wrangler d1 execute <DB> --remote --file=lunora/migrations/<file>.sql`. The
152
+ file is multi-statement, so it is not a `@lunora/d1` `Migration` either.
153
+ For data backfills, deploy first, then `lunora deploy --migrate --migrate-yes
154
+ --migrate-url <url>` (or `lunora migrate up --prod`). See
155
+ `lunora-migration-helper`.
126
156
 
127
157
  ## Common Pitfalls
128
158
 
@@ -147,4 +177,5 @@ secret put` for every prod secret.
147
177
  - [ ] DO + container `class_name`s exported by the worker entry.
148
178
  - [ ] Production secrets set via `wrangler secret put`.
149
179
  - [ ] Schema changes migrated; the drift gate is green (no `--allow-schema-drift`).
150
- - [ ] `lunora deploy` succeeded; `--migrate` run if data backfills were pending.
180
+ - [ ] `lunora deploy` succeeded; `--migrate --migrate-yes --migrate-url <url>` run
181
+ if data backfills were pending.
@@ -70,15 +70,22 @@ Each function declares its inputs with `.input(...)` (a `v.*` map) and ends with
70
70
  terminal `.query` / `.mutation` / `.action` handler. Export them as named
71
71
  consts from `lunora/*.ts`; codegen surfaces them as `api.<file>.<name>`.
72
72
 
73
- | Kind | Reads `ctx.db` | Writes `ctx.db` | Side effects / `fetch` | Reactive |
74
- | ---------- | --------------------------------- | --------------- | ---------------------- | -------- |
75
- | `query` | yes | no | no | yes |
76
- | `mutation` | yes | yes | no | — |
77
- | `action` | no (use `runQuery`/`runMutation`) | no | yes | — |
73
+ | Kind | Reads `ctx.db` | Writes `ctx.db` | Side effects / `fetch` | Reactive |
74
+ | ---------- | -------------- | ------------------- | ---------------------- | -------- |
75
+ | `query` | yes | no | no | yes |
76
+ | `mutation` | yes | yes, transactional | no | — |
77
+ | `action` | yes | yes, autocommitting | yes | — |
78
+
79
+ The builders are **generated**, not imported from `@lunora/server`: codegen binds
80
+ them to your schema, so `ctx.db`, `v.id("channels")`, and index names are all
81
+ typed. `@lunora/server` exports the schema/HTTP/validator surface and
82
+ `LunoraError`, never `query` / `mutation` / `action`.
78
83
 
79
84
  ```ts
80
- import type { Id } from "@lunora/server";
81
- import { action, LunoraError, mutation, query, v } from "@lunora/server";
85
+ import { LunoraError } from "lunorash/server";
86
+
87
+ import type { Id } from "#lunora/_generated/server.js";
88
+ import { action, mutation, query, v } from "#lunora/_generated/server.js";
82
89
 
83
90
  // `api` / `internal` come from codegen:
84
91
  // import { api, internal } from "./_generated/api";
@@ -112,8 +119,12 @@ export const notifySlack = action.input({ messageId: v.id("messages") }).action(
112
119
 
113
120
  - **Pick the right kind.** Reactive read → `query`. Transactional write →
114
121
  `mutation`. External I/O (`fetch`, third-party SDKs, calling other functions)
115
- → `action`. An action has no `ctx.db`; it reaches data via `ctx.runQuery` /
116
- `ctx.runMutation`.
122
+ → `action`. An action has a `ctx.db`, but its own writes are not
123
+ transactional: each autocommits as it runs, so a later throw rolls nothing
124
+ back. Reach data through `ctx.runQuery` / `ctx.runMutation` — a mutation
125
+ called that way runs in the same all-or-nothing transaction a top-level one
126
+ gets, so put every write that has to land together in ONE mutation rather
127
+ than sequencing several from the action.
117
128
  - **`internal*` variants** (`internalQuery`, `internalMutation`,
118
129
  `internalAction`) are not exposed to clients — use them for server-only logic
119
130
  called from actions, crons, or other functions.
@@ -148,26 +159,118 @@ await ctx.db.delete(id);
148
159
  scans the whole table — `@lunora/advisor` flags it as `filter-without-index`.
149
160
  Declare the index and constrain with `.withIndex`.
150
161
 
162
+ ### Following foreign keys: `ctx.db.related`
163
+
164
+ Every `v.id("table")` column is an edge in a graph your schema already declares.
165
+ `ctx.db.related` walks it, so "this customer's tickets, and those tickets'
166
+ messages" is one call instead of a hand-written chain of `withIndex` lookups.
167
+
168
+ ```ts
169
+ const { continueCursor, isDone, nodes } = await ctx.db.related(
170
+ { table: "customers", id: customerId }, // or a row you already loaded
171
+ { depth: 2, direction: "in", edges: ["tickets.customerId", "messages.ticketId"], limit: 50 },
172
+ );
173
+
174
+ for (const node of nodes) {
175
+ node.table; // "messages"
176
+ node.document; // the row itself
177
+ node.depth; // 2
178
+ node.score; // 0.5 — 1 at depth 1, halving per hop
179
+ node.path; // ["tickets.customerId", "messages.ticketId"]
180
+ node.pathIds; // ids along the way, start included
181
+ }
182
+ ```
183
+
184
+ - **Edge names are `"<table>.<column>"`.** `edges` restricts the walk to the
185
+ named ones; a name the schema does not declare is **refused**, not ignored.
186
+ - **`direction`** — `"out"` follows the ids this row holds, `"in"` the rows that
187
+ point at it, `"both"` (the default) does both.
188
+ - **`depth`** defaults to `1`, max `4`. **`limit`** defaults to `50`, max `200`.
189
+ Both caps **refuse rather than clamp**, so do not probe for the ceiling.
190
+ - **Only a column is an edge**: a bare `v.id(...)`, `v.optional(v.id(...))` or
191
+ `v.array(v.id(...))`. An id nested in a `v.object` / `v.union` / `v.record` is
192
+ not. An array FK is followed **outward only**.
193
+ - **It is an ordinary read** — RLS, column masks, soft delete, `.global()`
194
+ routing and reactivity all apply, because every hop goes back through
195
+ `ctx.db`. Under a `.rls("required")` schema each hop gets exactly the verdict a
196
+ direct read of that table would, so declare a read policy for every table the
197
+ walk can reach — or narrow it with `edges`.
198
+ - Index the foreign keys. Each inward hop is a `WHERE fk IN (…)` read, and
199
+ unindexed it scans — see the `lunora-performance-audit` skill for the cost
200
+ model and the traversal caps.
201
+
151
202
  ## Other `ctx` capabilities
152
203
 
153
- `ctx.auth` (the resolved session: `ctx.auth.userId`), `ctx.scheduler`
154
- (`runAfter` / `runAt` for deferred work), `ctx.storage` (R2), `ctx.vectors`
155
- (Vectorize), and — when their packages are wired — `ctx.ai` and `ctx.containers`.
204
+ Always available:
205
+
206
+ - `ctx.auth` — the resolved session (`ctx.auth.userId`).
207
+ - `ctx.scheduler` — `runAfter` / `runAt` for deferred work.
208
+ - `ctx.secrets` — Cloudflare Secrets Store.
209
+ - `ctx.span` / `ctx.trace` — the current span and a scoped tracing helper for
210
+ wide events.
211
+
212
+ Added by their package when wired. **A dependency in `package.json` is not
213
+ enough** — codegen scans the `lunora/` source set and flips a capability on only
214
+ when a file there imports the `@lunora/*` package or reads its `ctx.*` helper.
215
+ So write the call first, then run `lunora codegen` to surface the typed context:
216
+
217
+ | `ctx.*` | Package |
218
+ | ----------------------------------------------------------------------------------------- | ----------------------------- |
219
+ | `ctx.storage` | `@lunora/storage` (R2) |
220
+ | `ctx.ai` | `@lunora/ai` (Workers AI) |
221
+ | `ctx.flags` | `@lunora/flags` (OpenFeature) |
222
+ | `ctx.queues.<name>` | `@lunora/queue` |
223
+ | `ctx.workflows` / `ctx.runStep` | `@lunora/workflow` |
224
+ | `ctx.containers` | `@lunora/container` |
225
+ | `ctx.browser` (action-only) | `@lunora/browser` |
226
+ | `ctx.sql` (action-only) | `@lunora/hyperdrive` |
227
+ | `ctx.kv` / `ctx.images` / `ctx.analytics` / `ctx.pipelines` / `ctx.vectors` / `ctx.r2sql` | `@lunora/bindings` subpaths |
228
+
229
+ Two exceptions to the usage scan, and one extra requirement:
230
+
231
+ - **`ctx.flags` gates on a declaration file**, not on usage — codegen wires it
232
+ only when `lunora/flags.ts` exists (`vis generate lunora-flags` creates it).
233
+ `ctx.notify` / `ctx.push` work the same way via `lunora/notify.ts`.
234
+ - **`ctx.sql` also needs the real resource.** Codegen types the field, but the
235
+ connection needs a `HYPERDRIVE` binding (`wrangler hyperdrive create`) and an
236
+ explicit `createHyperdrive(ctx.env.HYPERDRIVE)` + driver adapter in the
237
+ action — see `lunora-setup-hyperdrive`. Bindings codegen can provision on its
238
+ own (e.g. `BROWSER` for `ctx.browser`) need no manual wrangler step.
239
+
240
+ `ctx.browser` and `ctx.sql` are **action-only** by design. They are external,
241
+ non-deterministic I/O: a query is re-run on every subscription re-evaluation, so
242
+ a non-deterministic read makes reactivity wrong, and a mutation's writes are
243
+ transactional — a rollback cannot un-send a network call.
156
244
 
157
245
  ## HTTP endpoints
158
246
 
159
247
  For webhooks or non-RPC HTTP, use `httpRouter` / `httpRoute` + `httpAction`:
160
248
 
249
+ `httpRouter()` takes **no arguments** — it returns a [Hono](https://hono.dev)
250
+ app you mount routes on, and you export the app. Passing it a routes object is a
251
+ type error (`Expected 0 arguments`), and from untyped code the routes simply
252
+ never mount.
253
+
161
254
  ```ts
255
+ // lunora/http.ts
162
256
  import { httpAction, httpRouter } from "@lunora/server";
163
257
 
164
- export default httpRouter({
165
- "/webhooks/stripe": httpAction(async (ctx, request) => {
258
+ import { internal } from "./_generated/api";
259
+
260
+ const app = httpRouter();
261
+
262
+ app.post(
263
+ "/webhooks/stripe",
264
+ httpAction(async (ctx, request) => {
166
265
  const event = await request.json();
266
+
167
267
  await ctx.runMutation(internal.billing.record, { event });
268
+
168
269
  return new Response("ok");
169
270
  }),
170
- });
271
+ );
272
+
273
+ export default app;
171
274
  ```
172
275
 
173
276
  ## Checklist
@@ -179,4 +282,6 @@ export default httpRouter({
179
282
  `action` (side effects via `runQuery`/`runMutation`).
180
283
  - [ ] Server-only logic uses `internal*`; expected failures throw `LunoraError`.
181
284
  - [ ] `ctx.db` writes only inside mutations; ids typed with `Id<"table">`.
285
+ - [ ] Any `ctx.db.related` walk is narrowed with `edges` / `direction`, and its
286
+ foreign keys are indexed.
182
287
  - [ ] Ran `lunora codegen`; typecheck is clean.
@@ -2,7 +2,8 @@
2
2
  name: lunora-migration-helper
3
3
  description: Plans Lunora schema and data migrations with widen-migrate-narrow. Use for
4
4
  breaking schema changes, backfills, table reshaping, online data migrations
5
- (`defineMigration` + `lunora migrate up`), the `.global()` D1 SQL flow, and the
5
+ (`defineMigration` + `lunora migrate up`), the `.global()` D1 / Hyperdrive
6
+ structural flows, and the
6
7
  pre-deploy schema-drift gate.
7
8
  ---
8
9
 
@@ -15,7 +16,7 @@ Safely change a Lunora schema and migrate data when making breaking changes.
15
16
  - Adding required fields to existing tables.
16
17
  - Changing field types or structure.
17
18
  - Splitting/merging tables, renaming/removing fields.
18
- - Reshaping `.global()` (D1-backed) tables.
19
+ - Reshaping `.global()` tables (D1- or Hyperdrive-backed).
19
20
 
20
21
  ## When Not to Use
21
22
 
@@ -23,21 +24,35 @@ Safely change a Lunora schema and migrate data when making breaking changes.
23
24
  - Adding **optional** fields that need no backfill.
24
25
  - Adding new tables or indexes with no correctness concern.
25
26
 
26
- ## Two Storage Layers — Know Which You Are Migrating
27
+ ## Storage Layers — Know Which You Are Migrating
27
28
 
28
- Lunora tables live in one of two backends, and they migrate differently:
29
+ Lunora tables live in one of three backends, and they migrate differently:
29
30
 
30
31
  - **ShardDO SQLite (default `root`, and `.shardBy(key)` tables).** State lives in
31
32
  the per-app / per-shard Durable Object. Data is reshaped with **online data
32
33
  migrations** — `defineMigration` declarations run by `lunora migrate up`,
33
34
  resumable per shard.
34
- - **`.global()` tables (D1).** Replicated to D1 for cross-region reads. Their
35
- structural DDL gets versioned **SQL migrations** via `lunora migrate generate`,
36
- applied by `@lunora/d1`'s runner at deploy time.
37
-
38
- A breaking change to a `.global()` table needs a generated SQL migration; a data
39
- backfill (either layer) is an online `defineMigration`. Both follow the same
40
- **widen → migrate → narrow** discipline.
35
+ - **`.global()` on D1 (the default global backend).** Replicated to D1 for
36
+ cross-region reads. Structural DDL gets versioned **SQL migrations** via
37
+ `lunora migrate generate`, applied by `@lunora/d1`'s runner at deploy time.
38
+ - **`.global({ backend: "hyperdrive" })` on Postgres/MySQL.** The same reactive
39
+ `.global()` contract served over Cloudflare Hyperdrive. Structural DDL works
40
+ differently here: tables **auto-provision on first use** — the runtime applies
41
+ the DDL through the dialect — so there is no `lunora migrate generate` step
42
+ and no versioned SQL file to commit. See `lunora-setup-hyperdrive-global`.
43
+
44
+ So: a breaking structural change to a **D1-backed** `.global()` table needs a
45
+ generated SQL migration; the Hyperdrive-backed equivalent provisions itself. A
46
+ data backfill (any layer) is always an online `defineMigration`. All three
47
+ follow the same **widen → migrate → narrow** discipline — check the table's
48
+ backend before assuming which structural path applies.
49
+
50
+ > **Moving an existing dataset between global backends.** To move a `.global()`
51
+ > dataset from D1 onto Hyperdrive, use
52
+ > `lunora migrate d1-to-hyperdrive --from-url <d1-worker> --to-url <hd-worker>`
53
+ > (`--tables` scopes it; `--out` keeps the intermediate NDJSON dump). This is a
54
+ > backend move, not a schema change — the widen → migrate → narrow discipline
55
+ > below still governs any reshaping you do on either side of it.
41
56
 
42
57
  ## Key Principle: Widen, Migrate, Narrow
43
58
 
@@ -115,6 +130,40 @@ export default defineMigration({
115
130
  The transform must preserve row identity — the runner always keeps the original
116
131
  `_id` / `_creationTime`, so do not change them.
117
132
 
133
+ ### Reading another table
134
+
135
+ The transform's second argument is a **shard-scoped reader** (`ctx.db` with
136
+ `get` / `findFirst` / `findMany` / `count`), so the common backfill — read the
137
+ parent, copy a field down onto its children — is expressible. It may be `async`.
138
+
139
+ ```ts
140
+ up: async (doc, ctx) => {
141
+ const thread = await ctx.db.get(String(doc.threadId), "threads");
142
+
143
+ return thread ? { ...doc, userId: thread.userId } : undefined;
144
+ },
145
+ ```
146
+
147
+ It is a reader, not a writer: the runner accounts for exactly one rewrite per
148
+ row read, and a transform writing directly would make that count describe
149
+ something other than what happened. To touch a second table, run a second
150
+ migration over that table.
151
+
152
+ ### A shard key cannot be backfilled by a migration
153
+
154
+ If the field you are backfilling **is** the table's `.shardBy()` key, this is the
155
+ wrong tool, and no amount of reader access fixes it:
156
+
157
+ - a row whose shard key is unset does not belong to any shard, so a shard-scoped
158
+ query will never enumerate it; and
159
+ - writing the key would have to **move the row to a different Durable Object**,
160
+ which a per-shard runner has no way to do.
161
+
162
+ Re-keying is an export → transform → import (`lunora export`, rewrite the NDJSON,
163
+ `lunora import` — ids are preserved, so foreign keys survive), not a migration.
164
+ So the rule elsewhere in this skill that a data backfill is always an online
165
+ `defineMigration` has this one exception.
166
+
118
167
  ### Run it
119
168
 
120
169
  ```bash
@@ -129,7 +178,10 @@ lunora migrate down backfill-display-name # revert (if `down` defined)
129
178
  Useful flags: `--batch-size <n>`, `--steps <n>` (cap batches this run), and
130
179
  `--prod --url <worker> --yes` to target production (with `LUNORA_ADMIN_TOKEN`).
131
180
 
132
- ## `.global()` (D1) Structural Migration Flow
181
+ ## `.global()` on D1 — Structural Migration Flow
182
+
183
+ This flow is **D1-specific**. Hyperdrive-backed globals auto-provision their DDL
184
+ at runtime and skip it entirely.
133
185
 
134
186
  ```bash
135
187
  # 1. Edit lunora/schema.ts (widen: add the optional new field to the .global() table).
@@ -146,8 +198,8 @@ lunora deploy
146
198
  ```
147
199
 
148
200
  `lunora migrate generate` only considers `.global()` tables (root/sharded tables
149
- are not D1-backed). Run it after each schema edit in the widen and narrow steps;
150
- backfill data with an online migration between them.
201
+ live in ShardDO SQLite, not D1). Run it after each schema edit in the widen and
202
+ narrow steps; backfill data with an online migration between them.
151
203
 
152
204
  ## The Schema-Drift Gate
153
205
 
@@ -176,9 +228,10 @@ add the migration — not to bypass it.
176
228
  transform before it touches real rows.
177
229
  5. **Deleting a field prematurely.** Deprecate with `v.optional` + a comment;
178
230
  delete only once nothing references it.
179
- 6. **Migrating the wrong layer.** A `.global()` structural change needs `lunora
180
- migrate generate` (SQL); a data backfill needs a `defineMigration`. Check the
181
- table's `.global()` / `.shardBy()` modifier first.
231
+ 6. **Migrating the wrong layer.** A D1-backed `.global()` structural change needs
232
+ `lunora migrate generate` (SQL); a Hyperdrive-backed one auto-provisions; a
233
+ data backfill needs a `defineMigration`. Check the table's `.global()` /
234
+ `.shardBy()` modifier — and, for `.global()`, its `backend` — first.
182
235
 
183
236
  ## Checklist
184
237
 
@@ -186,9 +239,9 @@ migrate generate` (SQL); a data backfill needs a `defineMigration`. Check the
186
239
  - [ ] Widened the schema to accept both shapes; `lunora codegen` clean.
187
240
  - [ ] Updated reads to handle both shapes; started writing the new shape.
188
241
  - [ ] Deployed the widened schema.
189
- - [ ] Authored a `defineMigration`; previewed with `lunora migrate up --dry-run`.
190
- - [ ] Ran `lunora migrate up`; `lunora migrate generate` + deploy for `.global()`
191
- structural changes.
192
- - [ ] Verified completion with `lunora migrate status`.
242
+ - [ ] Authored a `defineMigration`; previewed with `lunora migrate up <id> --dry-run`.
243
+ - [ ] Ran `lunora migrate up <id>`; `lunora migrate generate` + deploy for
244
+ `.global()` structural changes.
245
+ - [ ] Verified completion with `lunora migrate status <id>`.
193
246
  - [ ] Narrowed the schema (required / drop old field); removed both-shapes code.
194
247
  - [ ] Deployed the final schema; schema-drift gate passed.
@@ -2,8 +2,8 @@
2
2
  name: lunora-performance-audit
3
3
  description: Diagnoses and fixes Lunora performance problems — full-table scans, missing
4
4
  indexes, OCC write conflicts, oversized subscriptions, and sharding/`.global()`
5
- scaling. Use when queries are slow, mutations conflict, or `@lunora/advisor`
6
- flags a table.
5
+ scaling. Use when queries are slow, mutations conflict, `lunora insights`
6
+ reports a hot-spot, or `@lunora/advisor` flags a table.
7
7
  ---
8
8
 
9
9
  # Lunora Performance Audit
@@ -14,7 +14,7 @@ apply it across sibling functions consistently.
14
14
  ## When to Use
15
15
 
16
16
  - Queries feel slow or read far more rows than they return.
17
- - Mutations retry or conflict under load (OCC).
17
+ - Mutations conflict (409 `CONFLICT`) under load (OCC).
18
18
  - Subscriptions fan out updates too broadly or re-run too often.
19
19
  - The Lunora Studio **Advisors** tab or `@lunora/advisor` flags a table.
20
20
 
@@ -34,12 +34,35 @@ apply it across sibling functions consistently.
34
34
 
35
35
  ## Signal Gathering
36
36
 
37
- Start with the static advisors — they need no traffic:
37
+ ### Runtime signal: `lunora insights`
38
+
39
+ When the worker is running and has served traffic, start here — it reports the
40
+ measured problem, not a suspected one:
41
+
42
+ ```bash
43
+ lunora insights # against the local dev worker
44
+ lunora insights --shard channel:demo # scope to one shard
45
+ lunora insights --limit 25 --format json # machine-readable, more rows
46
+ lunora insights --prod --url https://app.example.com --token $LUNORA_ADMIN_TOKEN
47
+ ```
48
+
49
+ It ranks per-function **write-conflict hot-spots** (OCC contention — the
50
+ sharding signal), **error rates**, and **latency outliers**. A function at the
51
+ top of the write-conflict list is the direct input to the OCC section below; a
52
+ latency outlier usually resolves to the read-amplification section.
53
+
54
+ The Studio **Issues** panel and `lunora logs` cover the error side in more
55
+ detail once `insights` tells you where to look.
56
+
57
+ ### Static signal: the advisors
58
+
59
+ These need no traffic, so they also work on a cold codebase:
38
60
 
39
61
  - **Lunora Studio → Advisors tab** surfaces `@lunora/advisor` findings live in
40
62
  dev.
41
- - `@lunora/advisor` runs static lints over `defineSchema` + discovered query
42
- reads / insert writes. Relevant performance/schema rules:
63
+ - `@lunora/advisor` runs ~90 static lints over `defineSchema` + discovered query
64
+ reads / insert writes (plus a few runtime lints). The performance/schema rules
65
+ most relevant here:
43
66
  - `filter-without-index` — a query filters a table with no covering index.
44
67
  - `unindexed-foreign-key` — a relation/FK column has no index.
45
68
  - `duplicate-index` / `empty-index` — wasted or malformed indexes.
@@ -73,11 +96,49 @@ const mine = await ctx.db
73
96
  Index columns are ordered: put equality columns first, then the range/sort
74
97
  column. Fix every sibling query on the table the same way.
75
98
 
99
+ ## Problem Class: Relation Traversal Cost
100
+
101
+ **Symptom:** a query calling `ctx.db.related` is slow, or one page of it reads
102
+ far more rows than it returns.
103
+
104
+ A traversal is not one read. It is one batched `findMany` **per edge, per hop**,
105
+ and the frontier grows multiplicatively — which is why every option is bounded,
106
+ and why the bounds **refuse rather than clamp**:
107
+
108
+ | Option | Default | Cap | What the cap is protecting |
109
+ | ------------- | ------- | -------- | ---------------------------------------------------------------------------- |
110
+ | `depth` | `1` | `4` | Past 4 hops a real schema is bounded by request time, not by `limit`. |
111
+ | `limit` | `50` | `200` | Nodes returned in one page. |
112
+ | cursor offset | `0` | `10 000` | The offset is re-walk work, not a skip: each hop reads `N + limit + 1` rows. |
113
+
114
+ A caller asking for `depth: 9` gets a `BAD_REQUEST`, not a silent depth 4 — so a
115
+ traversal that throws is a mis-sized request, not a bug to route around.
116
+
117
+ **Fixes, in order of preference:**
118
+
119
+ 1. **Narrow with `edges`.** Name only the `"<table>.<column>"` edges the feature
120
+ needs. Following every declared foreign key is the usual reason a walk is
121
+ wide — one unrelated `v.id(...)` column can double the frontier per hop.
122
+ 2. **Narrow with `direction`.** `"in"` or `"out"` instead of the default
123
+ `"both"` halves the edges expanded at each hop.
124
+ 3. **Drop `depth` before raising `limit`.** Each hop multiplies; a page is
125
+ linear.
126
+ 4. **Index the foreign keys.** Every inward hop is a `WHERE fk IN (…)` read, so
127
+ an unindexed FK makes each hop a scan. `@lunora/advisor` flags it as
128
+ `unindexed-foreign-key`.
129
+ 5. **Stop deep-paging.** An offset past 10 000 is refused outright. That is the
130
+ signal the traversal is the wrong tool for the job — narrow the walk instead
131
+ of paging through it.
132
+
76
133
  ## Problem Class: Write Conflicts (OCC)
77
134
 
78
- **Symptom:** mutations on hot rows retry or fail under concurrency. ShardDO uses
79
- optimistic concurrency control — concurrent writes to the same DO that touch
80
- overlapping state conflict and retry.
135
+ **Symptom:** mutations on hot rows fail under concurrency, or the function tops
136
+ the write-conflict section of `lunora insights`. ShardDO uses optimistic
137
+ concurrency control — concurrent writes to the same DO that touch overlapping
138
+ state conflict. There is **no server-side retry loop**: the loser throws
139
+ `ConflictError` (code `CONFLICT`, HTTP 409) and the caller decides, so a client
140
+ that never handles it (`isConflictError` from `@lunora/client`) just surfaces
141
+ 409s to the user.
81
142
 
82
143
  **Fixes, in order of preference:**
83
144
 
@@ -115,6 +176,11 @@ cross-region reads (with read-your-writes via the Sessions API). Reserve it for
115
176
  read-mostly tables — `.global()` adds the D1 migration flow (see the
116
177
  `lunora-migration-helper` skill) and write-path cost.
117
178
 
179
+ If the dataset outgrows D1, `.global({ backend: "hyperdrive" })` serves the same
180
+ reactive `.global()` contract from Postgres/MySQL over Cloudflare Hyperdrive —
181
+ see the `lunora-setup-hyperdrive-global` skill (and `lunora migrate
182
+ d1-to-hyperdrive` to move an existing dataset).
183
+
118
184
  ### `.shardBy(key)` vs `.global()` — choose one per table
119
185
 
120
186
  - `.shardBy(key)`: partitions a table across Durable Objects by key — scales
@@ -135,8 +201,11 @@ read-mostly tables — `.global()` adds the D1 migration flow (see the
135
201
  ## Checklist
136
202
 
137
203
  - [ ] Scoped one concrete flow; traced every `ctx.db` read/write.
204
+ - [ ] Ran `lunora insights` (if the worker has traffic) for the measured signal.
138
205
  - [ ] Checked the Studio Advisors tab / `@lunora/advisor` findings.
139
206
  - [ ] Read amplification: replaced `.filter()` with an indexed `.withIndex()`.
207
+ - [ ] Relation traversals: narrowed with `edges` / `direction` before `depth`;
208
+ foreign keys indexed.
140
209
  - [ ] Write conflicts: narrowed writes and/or partitioned with `.shardBy(key)`.
141
210
  - [ ] Subscription cost: scoped query args so live queries depend on few rows.
142
211
  - [ ] Cross-region: applied `.global()` only to read-mostly tables.