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

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 +186 -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,182 @@ 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.topics.<name>` (pub/sub, see below) | `@lunora/queue` |
224
+ | `ctx.workflows` / `ctx.runStep` | `@lunora/workflow` |
225
+ | `ctx.containers` | `@lunora/container` |
226
+ | `ctx.browser` (action-only) | `@lunora/browser` |
227
+ | `ctx.sql` (action-only) | `@lunora/hyperdrive` |
228
+ | `ctx.kv` / `ctx.images` / `ctx.analytics` / `ctx.pipelines` / `ctx.vectors` / `ctx.r2sql` | `@lunora/bindings` subpaths |
229
+
230
+ Two exceptions to the usage scan, and one extra requirement:
231
+
232
+ - **`ctx.flags` gates on a declaration file**, not on usage — codegen wires it
233
+ only when `lunora/flags.ts` exists (`vis generate lunora-flags` creates it).
234
+ `ctx.notify` / `ctx.push` work the same way via `lunora/notify.ts`.
235
+ - **`ctx.sql` also needs the real resource.** Codegen types the field, but the
236
+ connection needs a `HYPERDRIVE` binding (`wrangler hyperdrive create`) and an
237
+ explicit `createHyperdrive(ctx.env.HYPERDRIVE)` + driver adapter in the
238
+ action — see `lunora-setup-hyperdrive`. Bindings codegen can provision on its
239
+ own (e.g. `BROWSER` for `ctx.browser`) need no manual wrangler step.
240
+
241
+ `ctx.browser` and `ctx.sql` are **action-only** by design. They are external,
242
+ non-deterministic I/O: a query is re-run on every subscription re-evaluation, so
243
+ a non-deterministic read makes reactivity wrong, and a mutation's writes are
244
+ transactional — a rollback cannot un-send a network call.
245
+
246
+ ### Topics: one event, many consumers
247
+
248
+ A queue has one consumer. To fan one event out to several independent
249
+ consumers, declare a topic and subscribe to it — in `lunora/queues.ts`, next to
250
+ your queues. Each subscription is its own queue with its own retries and
251
+ dead-letter queue.
252
+
253
+ ```ts
254
+ // lunora/queues.ts
255
+ import { defineSubscription, defineTopic } from "@lunora/queue";
256
+
257
+ import { internal } from "./_generated/api";
258
+
259
+ export const signups = defineTopic<{ userId: string }>();
260
+
261
+ export const welcomeEmail = defineSubscription(signups, {
262
+ handler: async (_ctx, batch) => {
263
+ for (const message of batch.messages) {
264
+ await message.run(internal.email.welcome, { userId: message.body.userId });
265
+ message.ack();
266
+ }
267
+ },
268
+ maxRetries: 5,
269
+ deadLetterQueue: "welcome-email-dlq",
270
+ });
271
+
272
+ // in a mutation or action
273
+ await ctx.topics.signups.publish({ userId });
274
+ ```
275
+
276
+ - Delivery is **at-least-once per subscription** and unordered — make handlers
277
+ idempotent. A failed send rejects `publish`, and a retry re-delivers to the
278
+ subscriptions that already got it.
279
+ - A subscription is published to only through its topic, never `ctx.queues`.
280
+ The topic passed to `defineSubscription` must be a `defineTopic` export of the
281
+ same file.
282
+ - Rate-limit public procedures that publish: one publish is one send per
283
+ subscription (`privileged_fanout_from_public_procedure` flags it).
284
+
285
+ ## Modules: grouping `lunora/` folders
286
+
287
+ A folder whose `module.ts` default-exports `defineModule(...)` is a module.
288
+ Modules are metadata — no `api.*` path or runtime change, still one Worker —
289
+ that drive the Studio **Architecture** view, OpenAPI tags, and table ownership:
290
+
291
+ ```ts
292
+ // lunora/billing/module.ts
293
+ import { defineModule } from "@lunora/server";
294
+
295
+ export default defineModule({ description: "Invoices and payments", tables: ["invoices"] });
296
+ ```
297
+
298
+ - Write `description` and `tables` inline; codegen reads them without running
299
+ the file. Modules do not nest.
300
+ - `cross_module_table_write` warns when a function outside the owning module
301
+ writes an owned table (insert, `patch`/`replace`/`delete`, batch or facade).
302
+ Route the write through a function in the owning module instead.
303
+ - Installed components (`.extend(...)` schema extensions) count as modules that
304
+ own their prefixed tables: write them through the component's functions, not
305
+ `ctx.db` directly.
306
+ - Keep ids typed `Id<"table">` — a write through an untyped `string` id cannot
307
+ be attributed to a table.
156
308
 
157
309
  ## HTTP endpoints
158
310
 
159
311
  For webhooks or non-RPC HTTP, use `httpRouter` / `httpRoute` + `httpAction`:
160
312
 
313
+ `httpRouter()` takes **no arguments** — it returns a [Hono](https://hono.dev)
314
+ app you mount routes on, and you export the app. Passing it a routes object is a
315
+ type error (`Expected 0 arguments`), and from untyped code the routes simply
316
+ never mount.
317
+
161
318
  ```ts
319
+ // lunora/http.ts
162
320
  import { httpAction, httpRouter } from "@lunora/server";
163
321
 
164
- export default httpRouter({
165
- "/webhooks/stripe": httpAction(async (ctx, request) => {
322
+ import { internal } from "./_generated/api";
323
+
324
+ const app = httpRouter();
325
+
326
+ app.post(
327
+ "/webhooks/stripe",
328
+ httpAction(async (ctx, request) => {
166
329
  const event = await request.json();
330
+
167
331
  await ctx.runMutation(internal.billing.record, { event });
332
+
168
333
  return new Response("ok");
169
334
  }),
170
- });
335
+ );
336
+
337
+ export default app;
171
338
  ```
172
339
 
173
340
  ## Checklist
@@ -179,4 +346,8 @@ export default httpRouter({
179
346
  `action` (side effects via `runQuery`/`runMutation`).
180
347
  - [ ] Server-only logic uses `internal*`; expected failures throw `LunoraError`.
181
348
  - [ ] `ctx.db` writes only inside mutations; ids typed with `Id<"table">`.
349
+ - [ ] Any `ctx.db.related` walk is narrowed with `edges` / `direction`, and its
350
+ foreign keys are indexed.
351
+ - [ ] Writes to a module- or component-owned table go through the owner's
352
+ functions (`cross_module_table_write` is clean).
182
353
  - [ ] Ran `lunora codegen`; typecheck is clean.