@lunora/cli 1.0.0-alpha.33 → 1.0.0-alpha.330

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 (129) 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 +965 -372
  5. package/dist/index.d.ts +965 -372
  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 +2 -16
  10. package/dist/packem_chunks/handler11.mjs +1 -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 +3 -58
  15. package/dist/packem_chunks/handler16.mjs +1 -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 +1 -94
  22. package/dist/packem_chunks/handler22.mjs +1 -0
  23. package/dist/packem_chunks/handler23.mjs +3 -0
  24. package/dist/packem_chunks/handler24.mjs +3 -0
  25. package/dist/packem_chunks/handler25.mjs +1 -0
  26. package/dist/packem_chunks/handler26.mjs +2 -0
  27. package/dist/packem_chunks/handler27.mjs +99 -0
  28. package/dist/packem_chunks/handler28.mjs +10 -0
  29. package/dist/packem_chunks/handler3.mjs +1 -204
  30. package/dist/packem_chunks/handler4.mjs +1 -33
  31. package/dist/packem_chunks/handler5.mjs +1 -49
  32. package/dist/packem_chunks/handler6.mjs +3 -91
  33. package/dist/packem_chunks/handler7.mjs +1 -42
  34. package/dist/packem_chunks/handler8.mjs +1 -174
  35. package/dist/packem_chunks/handler9.mjs +1 -315
  36. package/dist/packem_chunks/runCodegenCommand.mjs +4 -52
  37. package/dist/packem_chunks/runDeployCommand.mjs +7 -594
  38. package/dist/packem_chunks/runDevCommand.mjs +3 -0
  39. package/dist/packem_chunks/runInitCommand.mjs +162 -1430
  40. package/dist/packem_chunks/runResetCommand.mjs +1 -41
  41. package/dist/packem_chunks/runRpcCommand.mjs +1 -68
  42. package/dist/packem_shared/COMMANDS-u8aPCKxf.mjs +1 -0
  43. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-CyIsI3S1.mjs +1 -0
  44. package/dist/packem_shared/EXIT_CODE-D_XCixv8.mjs +1 -0
  45. package/dist/packem_shared/admin-token-VdUnvnKW.mjs +1 -0
  46. package/dist/packem_shared/admin-url-DJB8P6Jh.mjs +1 -0
  47. package/dist/packem_shared/binding-manifest-file-CQ1eQYy1.mjs +2 -0
  48. package/dist/packem_shared/buildRegistryIndex-Dc8D8AR6.mjs +1 -0
  49. package/dist/packem_shared/catalog-CahzmDLV.mjs +1 -0
  50. package/dist/packem_shared/cli-BQ3AyV_K.mjs +2 -0
  51. package/dist/packem_shared/codegen-error-AmH54ofi.mjs +3 -0
  52. package/dist/packem_shared/command-CAXl2A1S.mjs +1 -0
  53. package/dist/packem_shared/commands-Cg06JNO2.mjs +13 -0
  54. package/dist/packem_shared/createLogger-Cl18I8AX.mjs +2 -0
  55. package/dist/packem_shared/createRecordingSpawner-sS7LEN7x.mjs +1 -0
  56. package/dist/packem_shared/deploy-target-DCWwiuAe.mjs +1 -0
  57. package/dist/packem_shared/diffSnapshots-WWwx-KvZ.mjs +5 -0
  58. package/dist/packem_shared/docker-DpVxvYpL.mjs +1 -0
  59. package/dist/packem_shared/health-probe-Bw9JFfIy.mjs +1 -0
  60. package/dist/packem_shared/import-BRM1hs7j.mjs +12 -0
  61. package/dist/packem_shared/insertSchemaExtension-DuiV6cba.mjs +8 -0
  62. package/dist/packem_shared/lint-ignore-report-DKZagpqk.mjs +2 -0
  63. package/dist/packem_shared/open-url-EnKy--w-.mjs +1 -0
  64. package/dist/packem_shared/output-format-B4eQHi5y.mjs +2 -0
  65. package/dist/packem_shared/parseManifest-CwPTKdtS.mjs +1 -0
  66. package/dist/packem_shared/path-containment-CgxYZggb.mjs +1 -0
  67. package/dist/packem_shared/plan-CtBB0rrT.mjs +5 -0
  68. package/dist/packem_shared/planDevCommand-tVTRd2QE.mjs +1 -0
  69. package/dist/packem_shared/platform-diagnostics-Cn2g6Jh-.mjs +4 -0
  70. package/dist/packem_shared/prompt-cancelled-BvsNxg_Q.mjs +1 -0
  71. package/dist/packem_shared/render-lunora-error-C3OJY5wk.mjs +2 -0
  72. package/dist/packem_shared/resolve-DUCSc7jQ.mjs +5 -0
  73. package/dist/packem_shared/resolve-target-C_ZloTvd.mjs +1 -0
  74. package/dist/packem_shared/runAddCommand-BsOrlyfD.mjs +1 -0
  75. package/dist/packem_shared/runExportCommand-slAq5BCF.mjs +5 -0
  76. package/dist/packem_shared/runMigrateGenerateCommand-Ii_WMZEA.mjs +11 -0
  77. package/dist/packem_shared/schemaIrToSnapshot-BjK-0IRo.mjs +1 -0
  78. package/dist/packem_shared/secret-key-DsMeUoEg.mjs +1 -0
  79. package/dist/packem_shared/shared-D-zCOmgY.mjs +1 -0
  80. package/dist/packem_shared/storage-iyNnV-2G.mjs +1 -0
  81. package/dist/packem_shared/tui-prompts-B3YwUhGw.mjs +4 -0
  82. package/dist/packem_shared/variables-acxcvVQa.mjs +1 -0
  83. package/dist/packem_shared/wrangler-name-Dsk5K1f-.mjs +1 -0
  84. package/dist/packem_shared/wrangler-secrets-CuUdFBaA.mjs +1 -0
  85. package/package.json +39 -19
  86. package/skills/README.md +35 -17
  87. package/skills/lunora/SKILL.md +123 -9
  88. package/skills/lunora-create-package/SKILL.md +4 -3
  89. package/skills/lunora-deploy/SKILL.md +42 -11
  90. package/skills/lunora-functions/SKILL.md +120 -15
  91. package/skills/lunora-migration-helper/SKILL.md +74 -21
  92. package/skills/lunora-performance-audit/SKILL.md +78 -9
  93. package/skills/lunora-quickstart/SKILL.md +95 -27
  94. package/skills/lunora-realtime/SKILL.md +73 -39
  95. package/skills/lunora-setup-auth/SKILL.md +33 -6
  96. package/skills/lunora-setup-hyperdrive/SKILL.md +33 -13
  97. package/skills/lunora-setup-hyperdrive-global/SKILL.md +5 -0
  98. package/skills/lunora-setup-mail/SKILL.md +34 -28
  99. package/skills/lunora-setup-scheduler/SKILL.md +18 -14
  100. package/skills/lunora-setup-storage/SKILL.md +187 -27
  101. package/dist/packem_chunks/planDevCommand.mjs +0 -541
  102. package/dist/packem_chunks/runMigrateGenerateCommand.mjs +0 -397
  103. package/dist/packem_shared/COMMANDS-B0ftFD_3.mjs +0 -948
  104. package/dist/packem_shared/DEFAULT_IMPORT_BATCH_SIZE-Ck-2bU08.mjs +0 -244
  105. package/dist/packem_shared/admin-url-4UzT-CI4.mjs +0 -19
  106. package/dist/packem_shared/api-spec-CtA6ilu4.mjs +0 -13
  107. package/dist/packem_shared/buildRegistryIndex-BcYe607_.mjs +0 -38
  108. package/dist/packem_shared/codegen-error-DJG-ghs_.mjs +0 -31
  109. package/dist/packem_shared/command-D3lB_4Az.mjs +0 -19
  110. package/dist/packem_shared/commands-B-gR09Z_.mjs +0 -845
  111. package/dist/packem_shared/createLogger-B40gPzQo.mjs +0 -78
  112. package/dist/packem_shared/createRecordingSpawner-DxI3mebw.mjs +0 -43
  113. package/dist/packem_shared/detect-package-manager-DYp7n3mJ.mjs +0 -61
  114. package/dist/packem_shared/diffSnapshots-BeDvvNiF.mjs +0 -161
  115. package/dist/packem_shared/docker-hMQ97KSQ.mjs +0 -21
  116. package/dist/packem_shared/insertSchemaExtension-DAqbfr9Z.mjs +0 -64
  117. package/dist/packem_shared/open-url-Dfq6fAyT.mjs +0 -41
  118. package/dist/packem_shared/output-format-wUvAN6AL.mjs +0 -17
  119. package/dist/packem_shared/parseArgs-YXFuKdEk.mjs +0 -56
  120. package/dist/packem_shared/parseManifest--vZf2FY1.mjs +0 -94
  121. package/dist/packem_shared/prompt-cancelled-APzX1Im-.mjs +0 -9
  122. package/dist/packem_shared/resolve-target-qbsJ_5sF.mjs +0 -16
  123. package/dist/packem_shared/runAddCommand-bnY6-HKb.mjs +0 -4
  124. package/dist/packem_shared/schema-drift-gate-BtBt0as0.mjs +0 -79
  125. package/dist/packem_shared/schemaIrToSnapshot-DdsljJT-.mjs +0 -43
  126. package/dist/packem_shared/storage-BIsph-Vk.mjs +0 -84
  127. package/dist/packem_shared/tui-prompts-BjEN8XgP.mjs +0 -658
  128. package/dist/packem_shared/wrangler-name-cy4yhm9j.mjs +0 -12
  129. package/dist/packem_shared/wrangler-secrets-P2_ZUR-k.mjs +0 -47
@@ -14,8 +14,9 @@ Lunora exposes a Convex-style functional API (`defineSchema`, `query`,
14
14
  `mutation`, `action`) on top of Cloudflare Workers and Durable Objects. State
15
15
  lives in a per-app `ShardDO` (SQLite, OCC, hibernated WebSocket subscriptions)
16
16
  by default; `.shardBy(key)` partitions it across many DOs and `.global()`
17
- replicates a table to D1 for low-latency cross-region reads. A Vite plugin
18
- drives codegen and end-to-end type sync.
17
+ replicates a table to D1 — or to Postgres/MySQL over Hyperdrive — for
18
+ low-latency cross-region reads. A Vite plugin drives codegen and end-to-end
19
+ type sync.
19
20
 
20
21
  If a more specific Lunora skill clearly matches the request, use that instead.
21
22
 
@@ -47,12 +48,13 @@ After codegen is green, use the most specific Lunora skill for the task:
47
48
  - Wiring live data into a client (hooks, optimistic updates): `lunora-realtime`
48
49
  - Authentication setup (email/password, OAuth, magic link, OTP):
49
50
  `lunora-setup-auth`
50
- - Wiring a prebuilt capability (mail, file storage, scheduled jobs, rate
51
- limiting, vectors, AI, containers, payments, MCP): install it with
52
- `lunora registry add <item>` (see `lunora registry list`). Capabilities with a
53
- dedicated skill: `lunora-setup-mail` (mail), `lunora-setup-storage` (R2 file
54
- storage), `lunora-setup-scheduler` (deferred `ctx.scheduler` + cron jobs). For
55
- the rest, read the item's README after installing.
51
+ - Transactional email: `lunora-setup-mail`
52
+ - R2 file storage (signed upload/download): `lunora-setup-storage`
53
+ - Deferred work (`ctx.scheduler`) and cron jobs: `lunora-setup-scheduler`
54
+ - Querying an **existing** Postgres/MySQL database from an action (`ctx.sql`,
55
+ non-reactive): `lunora-setup-hyperdrive`
56
+ - Using Postgres/MySQL as a **reactive `.global()` backend**, or migrating a D1
57
+ `.global()` dataset onto it: `lunora-setup-hyperdrive-global`
56
58
  - Building a reusable capability — a registry item or an `@lunora/*` package:
57
59
  `lunora-create-package`
58
60
  - Planning or running a schema/data migration: `lunora-migration-helper`
@@ -61,9 +63,119 @@ After codegen is green, use the most specific Lunora skill for the task:
61
63
  - Investigating performance, scan, or write-conflict issues:
62
64
  `lunora-performance-audit`
63
65
 
66
+ **Reviewing or auditing existing `lunora/` code** has no skill of its own,
67
+ because two commands do the work mechanically — run them before reading
68
+ anything:
69
+
70
+ ```bash
71
+ lunora verify # wrangler config + codegen dry-run + tsc --noEmit
72
+ lunora advisor # the static + runtime lint set, scored per procedure
73
+ ```
74
+
75
+ `lunora advisor` is the security/quality review: RLS coverage, ownership and
76
+ identity checks, fail-open guards, input validators, unindexed reads,
77
+ non-deterministic calls in queries, SQL interpolation, and leaked secrets. It
78
+ prints each finding's own lint id and category, so read its output rather than
79
+ grepping for rule names. Use `--entry <file>#<export>` to inspect one procedure
80
+ and `--min-score` / `--baseline` to gate CI. Only after it is clean is a by-hand
81
+ pass over `lunora-functions` worth the tokens.
82
+
64
83
  If one of those clearly matches the user's goal, switch to it instead of staying
65
84
  in this skill.
66
85
 
86
+ ## Capabilities Without a Dedicated Skill
87
+
88
+ Most other capabilities install as a **registry item** — `lunora registry add
89
+ <item>` scaffolds the `lunora/` glue, wrangler bindings, and env vars, then
90
+ prints post-install steps. Browse with `lunora registry list`; preview with
91
+ `lunora registry view <item>`. Read the installed item's README, and the
92
+ package's `docs/` for the API.
93
+
94
+ | Goal | Install / package |
95
+ | --------------------------------------------- | ------------------------------------------------------------ |
96
+ | Background jobs on Cloudflare Queues | `registry add queue` → `@lunora/queue` (`ctx.queues`) |
97
+ | Durable multi-step workflows | `registry add workflow` → `@lunora/workflow` (`ctx.runStep`) |
98
+ | Durable AI agents (tool loops, HITL, memory) | `@lunora/agent` (`defineAgent`) |
99
+ | Workers AI / RAG | `registry add ai` → `@lunora/ai` (`ctx.ai`, `defineRag`) |
100
+ | Feature flags (OpenFeature) | `registry add flags` → `@lunora/flags` (`ctx.flags`) |
101
+ | Payments (Stripe / Polar) | `registry add payment` → `@lunora/payment` |
102
+ | Rate limiting | `registry add ratelimit` → `@lunora/ratelimit` |
103
+ | Headless browser (action-only) | `registry add browser` → `@lunora/browser` (`ctx.browser`) |
104
+ | Cloudflare Containers | `@lunora/container` (`defineContainer`, `ctx.containers`) |
105
+ | Presence / who's-here | `registry add presence` |
106
+ | Cloudflare Access (Zero Trust) identity | `registry add cloudflare-access` |
107
+ | Backup / restore | `registry add backup` |
108
+ | Testing (in-memory harness, agent doubles) | `@lunora/testing` (`lunoraTest`) |
109
+ | Deterministic seed data | `@lunora/seed` + `lunora seed` |
110
+ | Local-first replica / offline mirror | `@lunora/replica` |
111
+ | Exposing the deployment to AI agents over MCP | `@lunora/mcp` |
112
+
113
+ Scaffolding inside this repo uses `vis generate lunora-<kind>` — `query`,
114
+ `mutation`, `action`, `http-route`, `table`, `cron`, `container`, `workflow`,
115
+ `queue`, `step`, `agent`, `flags`, `collections`, `package` (`vis generate
116
+ --list` for the full set). Most take a name: **always** pass it as
117
+ `--name=value`, since vis parses a space-separated `--name foo` as `--name=true`
118
+ plus a stray positional. `lunora-flags` and `lunora-collections` are singletons
119
+ and take no name.
120
+
121
+ ## Exposing a Deployment Over MCP
122
+
123
+ `@lunora/mcp` puts a **running deployment** behind an MCP server (`lunora-mcp`),
124
+ so an agent can introspect and call it. It needs `LUNORA_URL` and
125
+ `LUNORA_ADMIN_TOKEN` — the admin bearer, which cannot be scoped down, because
126
+ every tool reads admin-gated routes. The safety story is therefore the gates
127
+ below, never the credential.
128
+
129
+ Always advertised, no gate:
130
+
131
+ | Tool | What it does |
132
+ | ---------------------------- | ---------------------------------------------------------------------- |
133
+ | `lunora_list_functions` | The deployment's public functions and their kinds. |
134
+ | `lunora_list_tables` | `.global()` tables with row counts. |
135
+ | `lunora_get_function_schema` | One function's argument descriptors, so a caller can build valid args. |
136
+ | `lunora_run_query` | Runs a query. Read-only. |
137
+ | `lunora_explain_error` | Explains an error `code` or raw `message` from the static catalog. |
138
+
139
+ `lunora_explain_error` touches no deployment and needs no token — the catalog is
140
+ compiled into `@lunora/errors`. Reach for it before guessing at what a Lunora
141
+ error means.
142
+
143
+ **Three env gates hold back everything else.** Each one both omits the tool from
144
+ the advertised list _and_ refuses it at dispatch, so the guarantee does not
145
+ depend on the client behaving:
146
+
147
+ - `LUNORA_MCP_ALLOW_WRITES` → `lunora_run_mutation`, `lunora_run_action`.
148
+ - `LUNORA_MCP_ALLOW_OBSERVABILITY` → the five `lunora_get_*` tools (logs,
149
+ issues, advisories, query insights, migration status). Read-only, but they
150
+ return production log lines and error messages — user data that lands at the
151
+ model provider.
152
+ - `LUNORA_MCP_ALLOW_DATA_READS` → `lunora_find_related`. **It returns raw table
153
+ rows read through the deployment's admin writer, with RLS policies and column
154
+ masks bypassed**, plus everything reachable within `depth` hops. It is
155
+ deliberately not folded into the observability gate: enabling log reading for
156
+ debugging must not silently also hand over every row.
157
+
158
+ All three default to **off**. Set one only when the answer to "may a model see
159
+ this?" is yes.
160
+
161
+ ### Writes are a two-step handshake
162
+
163
+ Past `LUNORA_MCP_ALLOW_WRITES`, each individual write still needs its own
164
+ confirmation. The first `lunora_run_mutation` / `lunora_run_action` call does
165
+ **not** execute — it returns `status: "action_required"` carrying
166
+ `proposedAction`, an `actionDigest`, and the `expiresAt` it is valid until (ten
167
+ minutes). The client shows that to a human, then calls the same tool again with
168
+ the identical `functionPath`, `args` and `shardKey`, plus `confirmed: true` and
169
+ that digest.
170
+
171
+ Editing anything produces a different digest and needs a fresh proposal; an
172
+ expired digest is refused rather than quietly re-proposed. Be precise about what
173
+ this buys: the digest binds the **call**, proving the write that runs is exactly
174
+ the one proposed on this deployment inside the window. It does **not** prove a
175
+ human ever saw it — an MCP server has no channel to a person, and a client that
176
+ asks nobody can echo the digest straight back. Enabling writes is the operator
177
+ asserting that the client on the other end does the asking.
178
+
67
179
  ## Core Mental Model
68
180
 
69
181
  - **Functions** live in `lunora/*.ts` and are one of `query` (reactive read),
@@ -75,7 +187,9 @@ in this skill.
75
187
  - **Reads go through indexes.** Prefer `ctx.db.query("t").withIndex(...)` over
76
188
  `.filter(...)`; declare the index with `.index("by_x", ["x"])`.
77
189
  - **Clients** subscribe over WebSocket. `useQuery`/`useMutation` (React, Vue,
78
- Solid, Svelte) re-render the moment a mutation changes the queried rows.
190
+ Solid, Svelte, React Native; signals in Angular) re-render the moment a
191
+ mutation changes the queried rows. Subscriptions run under the socket's
192
+ verified identity, so `rls()` / `ctx.auth` apply to live updates too.
79
193
 
80
194
  ## When Not to Use
81
195
 
@@ -73,8 +73,8 @@ A registry item is a directory under `registry/<name>/` with three files:
73
73
  Add an entry to `registry/index.json`, then rebuild and check the index:
74
74
 
75
75
  ```bash
76
- lunora registry build # regenerate registry/index.json
77
- lunora registry build --check # verify the index is up to date (CI)
76
+ lunora registry build --from ./registry # regenerate registry/index.json
77
+ lunora registry build --from ./registry --check # verify the index is up to date (CI)
78
78
  lunora registry view <name> # preview what `add` would do
79
79
  lunora registry add <name> # install into the current project
80
80
  ```
@@ -122,7 +122,8 @@ declaration the user must add so codegen wires the typed surface.
122
122
  - [ ] Chose the right shape (registry item, workspace package, or both).
123
123
  - [ ] Registry item: `registry.json` + `index.ts` + `README.md` authored;
124
124
  `bindings`/`envVars`/`requires`/`files` correct.
125
- - [ ] `lunora registry build` run; `lunora registry build --check` passes.
125
+ - [ ] `lunora registry build --from ./registry` run; the same command with
126
+ `--check` passes.
126
127
  - [ ] Workspace package: scaffolded via `vis generate lunora-package`; no `.js`
127
128
  extensions, no mixed default+named exports, catalog versions used.
128
129
  - [ ] `lint:types` and `test` pass for the new package.
@@ -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.