/lunora:lunora-functionsfrom Lunora
Provides authoring rules for Lunora schema and server functions: defineSchema, defineTable, v.* validators, query/mutation/action builders, indexes, ctx.db API, pagination, and httpAction. Use when writing or reviewing files under lunora/.
Ships in Lunora. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
The core authoring rules for Lunora backend code. The verify loop after every edit:
lunora codegen — regenerates lunora/_generated/ and prints advisor
findings (it does not run tsc).lunora verify — codegen dry-run plus tsc --noEmit, no files written. The
type-check needs a tsconfig.json (without one it warns and skips) and is
off under --no-typecheck.lunora/ code for correctness and idiom.lunora-quickstart) or auth (lunora-setup-auth).lunora-performance-audit).lunora-migration-helper).defineSchema + defineTablelunora/schema.ts exports defineSchema as the default export. Every column is
a v.* validator. Declare an index for every access pattern you query by.
import { defineSchema, defineTable, v } from "lunorash/server";
export default defineSchema({
messages: defineTable({
channelId: v.id("channels"),
authorId: v.id("users"),
body: v.string(),
createdAt: v.number(),
}).index("by_channel", ["channelId", "createdAt"]),
channels: defineTable({
name: v.string(),
}),
});
_id and _creationTime on every row, so don't declare them.lunorash/* is the umbrella package templates install; @lunora/server is
the same module if the project depends on it directly..index("name", ["a", "b"]) — columns are ordered; put equality columns
first, then the range/sort column..shardBy("ownerId") partitions the table across Durable Objects by key;
.global() replicates it to D1 for cross-region reads. Default (neither) is a
single root-scoped ShardDO. They are not combined on one table — for choosing
between them, see the side-by-side comparison in the lunora-performance-audit
skill.v.*)string, number, boolean, id("table"), null, any, bigint, bytes,
literal(value), array(item), object({...}), record(key, value),
union(a, b, …), optional(inner), partial({...}), geoPoint(), the
convenience types date, timestamp, and storage (an R2 object key), and
v.from(schema) to adopt any Standard Schema v1 validator (Zod, Valibot, …).
v.optional(x) means the field may be absent;
x.nullable() means it may be null. They are different.v.string().min(1).max(4096), .email(), .url(), .pattern(re);
v.number().int().min(0), .positive(); v.array(x).max(n). Bound every
client-supplied string and array — templates cap text at .max(4096).Each function declares its inputs with .input(...) (a v.* map), optionally
adds .use(middleware) (rate limiting, RLS, masking) and .output(validator),
and ends with a terminal .query / .mutation / .action taking a
({ ctx, args }) => … handler. Export them as named consts from lunora/*.ts.
Codegen nests references by path: lunora/messages.ts → api.messages.<fn>,
lunora/billing/invoices.ts → api.billing.invoices.<fn>. Public functions
land in _generated/api.ts, internal* ones in _generated/internal.ts.
| Kind | Reads ctx.db | Writes ctx.db | Side effects / fetch | Reactive |
|---|---|---|---|---|
query | yes | no | no | yes |
mutation | yes | yes, transactional | no | — |
action | yes | yes, autocommitting | yes | — |
Import the builders from _generated/server, not from lunorash/server: codegen
binds them to your schema, which is what types ctx.db, v.id("channels"), and
index names. lunorash/server exports the schema/HTTP/validator surface and
LunoraError, never query / mutation / action.
// lunora/messages.ts
import { LunoraError } from "lunorash/server";
import { api } from "./_generated/api.js";
import type { Doc, Id } from "./_generated/server.js";
import { action, mutation, query, v } from "./_generated/server.js";
export const listByChannel = query.input({ channelId: v.id("channels") }).query(async ({ ctx, args: { channelId } }): Promise<Doc<"messages">[]> =>
ctx.db
.query("messages")
.withIndex("by_channel", (q) => q.eq("channelId", channelId))
.take(100),
);
export const send = mutation
.input({ channelId: v.id("channels"), body: v.string() })
.mutation(async ({ ctx, args: { channelId, body } }): Promise<Id<"messages">> => {
if (!ctx.auth.userId) {
throw new LunoraError("UNAUTHORIZED", "not signed in");
}
return ctx.db.insert("messages", {
channelId,
authorId: ctx.auth.userId as Id<"users">,
body,
createdAt: Date.now(),
});
});
export const notifySlack = action.input({ channelId: v.id("channels") }).action(async ({ ctx, args: { channelId } }) => {
// A public action is callable by anyone: gate it like a mutation (and check
// channel access too, if your app has per-channel membership).
if (!ctx.auth.userId) {
throw new LunoraError("UNAUTHORIZED", "not signed in");
}
const messages = await ctx.runQuery(api.messages.listByChannel, { channelId });
const response = await fetch(String(ctx.env?.SLACK_WEBHOOK_URL), { method: "POST", body: JSON.stringify(messages) });
if (!response.ok) {
throw new Error(`Slack webhook failed: ${String(response.status)}`); // fetch resolves on 4xx/5xx
}
});
Templates also map #lunora/* to ./lunora/* in package.json imports, so
#lunora/_generated/server.js works from anywhere in the project.
query. Transactional write →
mutation. External I/O (fetch, third-party SDKs, calling other functions)
→ action. An action has a ctx.db, but its own writes are not
transactional: each autocommits as it runs, so a later throw rolls nothing
back. Reach data through ctx.runQuery / ctx.runMutation — a mutation
called that way runs in the same all-or-nothing transaction a top-level one
gets, so put every write that has to land together in one mutation rather
than sequencing several from the action.internal* variants (internalQuery, internalMutation,
internalAction) are not exposed to clients — use them for server-only logic
called from actions, crons, or other functions.LunoraError(code, message?, options?) from lunorash/server for
expected failures ("UNAUTHORIZED", "NOT_FOUND", …); it serializes with its
code to the client, where isUnauthorizedError and friends branch on it.ctx.db APIReads:
await ctx.db.get("table", id); // one row by id (or null)
ctx.db.query("t").withIndex("by_x", (q) => q.eq("x", v)); // indexed query
.order("asc" | "desc") // by the active index (or _creationTime)
.filter((doc) => …) // JS predicate, applied after the index range
.collect(); // all matching rows
.take(n); // first n rows
.first(); // first row or null
.unique(); // the one match, null if none; throws if more than one
.paginate({ numItems, cursor }); // { page, isDone, continueCursor }
Writes (mutations, and autocommitting in actions):
await ctx.db.insert("t", { ...fields }); // returns the new Id
await ctx.db.patch(table, id, { field: next }); // shallow-merge update
await ctx.db.replace(table, id, { ...allFields }); // full overwrite
await ctx.db.delete(table, id);
Prefer withIndex over .filter: a .filter(...) without a covering index
scans the whole table (advisor: filter_without_index), and a bare .collect()
grows with the table and with every subscriber's payload (unbounded_collect).
Constrain with .withIndex, cap with .take(n), or paginate.
A paginated query takes a paginationOpts arg and returns .paginate(...)
as-is; the client hook (usePaginatedQuery, see lunora-realtime) supplies it:
// `v.any()` would type the arg `unknown`, which `.paginate()` rejects. The hook
// also sends `endCursor`; an undeclared key is stripped, so declare it. Bound
// `numItems` too: it is caller input and sets the read limit.
const cursor = v.optional(v.union(v.string(), v.null()));
export const page = query
.input({ channelId: v.id("channels"), paginationOpts: v.object({ cursor, endCursor: cursor, numItems: v.number().int().min(1).max(100) }) })
.query(async ({ ctx, args: { channelId, paginationOpts } }) =>
ctx.db
.query("messages")
.withIndex("by_channel", (q) => q.eq("channelId", channelId))
.order("desc")
.paginate(paginationOpts),
);
ctx.db.relatedEvery v.id("table") column is an edge in a graph your schema already declares.
ctx.db.related walks it, so "this customer's tickets, and those tickets'
messages" is one call instead of a hand-written chain of withIndex lookups.
const { continueCursor, isDone, nodes } = await ctx.db.related(
{ table: "customers", id: customerId }, // or a row you already loaded
{ depth: 2, direction: "in", edges: ["tickets.customerId", "messages.ticketId"], limit: 50 },
);
for (const node of nodes) {
node.table; // "messages"
node.document; // the row itself
node.depth; // 2
node.score; // 0.5 — 1 at depth 1, halving per hop
node.path; // ["tickets.customerId", "messages.ticketId"]
node.pathIds; // ids along the way, start included
}
"<table>.<column>". edges restricts the walk to the
named ones; a name the schema does not declare is refused, not ignored.direction — "out" follows the ids this row holds, "in" the rows that
point at it, "both" (the default) does both.depth defaults to 1, max 4. limit defaults to 50, max 200.
Both caps refuse rather than clamp, so a probe for the ceiling just errors.v.id(...), v.optional(v.id(...)) or
v.array(v.id(...)). An id nested in a v.object / v.union / v.record is
not. An array FK is followed outward only..global()
routing and reactivity all apply, because every hop goes back through
ctx.db. Under a .rls("required") schema each hop gets exactly the verdict a
direct read of that table would, so declare a read policy for every table the
walk can reach — or narrow it with edges.WHERE fk IN (…) read, and
unindexed it scans — see the lunora-performance-audit skill for the cost
model and the traversal caps.ctx capabilitiesAlways available:
ctx.auth — the resolved session (ctx.auth.userId, null when anonymous).ctx.secrets — Cloudflare Secrets Store.ctx.ip — the caller's CF-Connecting-IP; undefined off Cloudflare.ctx.runQuery (all kinds), ctx.runMutation (mutations, actions),
ctx.runAction (actions) — call another function by its api.* /
internal.* reference.ctx.log, ctx.metrics, ctx.span / ctx.trace — logging, metrics, the
current wide-event span and a scoped tracing helper.Wired by codegen on use. A dependency in package.json is not enough: codegen
scans the lunora/ source set and turns a capability on only when a file there
imports its @lunora/* package or reads its ctx.* helper. Write the call
first, then run lunora codegen to wire the binding and typed context:
ctx.* | Package |
|---|---|
ctx.scheduler (mutations + actions; see lunora-setup-scheduler) | @lunora/scheduler |
ctx.storage (R2; see lunora-setup-storage) | @lunora/storage |
ctx.ai | @lunora/ai (Workers AI) |
ctx.flags | @lunora/flags (OpenFeature) |
ctx.queues.<name> | @lunora/queue |
ctx.topics.<name> (pub/sub, see below) | @lunora/queue |
ctx.workflows / ctx.runStep | @lunora/workflow |
ctx.containers | @lunora/container |
ctx.browser (action-only) | @lunora/browser |
ctx.live (action-side only; see lunora-realtime) | @lunora/server |
ctx.sql (action-only) | @lunora/hyperdrive |
ctx.kv / ctx.images / ctx.analytics | @lunora/bindings subpaths |
ctx.pipelines / ctx.vectors / ctx.r2sql | @lunora/bindings subpaths |
One exception to the usage scan, and one extra requirement:
ctx.flags gates on a declaration file, not on usage — codegen wires it
only when lunora/flags.ts exists. ctx.notify / ctx.push work the same
way via lunora/notify.ts.ctx.sql also needs the real resource. Codegen types the field, but the
connection needs a HYPERDRIVE binding (wrangler hyperdrive create) and an
explicit createHyperdrive(ctx.env.HYPERDRIVE) + driver adapter in the
action — see lunora-setup-hyperdrive. Bindings codegen can provision on its
own (e.g. BROWSER for ctx.browser) need no manual wrangler step.ctx.browser and ctx.sql are action-only by design. They are external,
non-deterministic I/O: a query is re-run on every subscription re-evaluation, so
a non-deterministic read makes reactivity wrong, and a mutation's writes are
transactional — a rollback cannot un-send a network call.
A queue has one consumer. To fan one event out to several independent
consumers, declare a topic and subscribe to it — in lunora/queues.ts, next to
your queues. Each subscription is its own queue with its own retries and
dead-letter queue.
// lunora/queues.ts
import { defineSubscription, defineTopic } from "@lunora/queue";
import { internal } from "./_generated/internal.js";
export const signups = defineTopic<{ userId: string }>();
export const welcomeEmail = defineSubscription(signups, {
handler: async (_ctx, batch) => {
for (const message of batch.messages) {
await message.run(internal.email.welcome, { userId: message.body.userId });
message.ack();
}
},
maxRetries: 5,
deadLetterQueue: "welcome-email-dlq",
});
// in a mutation or action
await ctx.topics.signups.publish({ userId });
publish, and a retry re-delivers to the
subscriptions that already got it.ctx.queues.
The topic passed to defineSubscription must be a defineTopic export of
lunora/queues.ts or a module's queues.ts (import it from there).privileged_fanout_from_public_procedure flags it).lunora/ foldersA folder whose module.ts default-exports defineModule(...) is a module.
Modules are metadata — no api.* path or runtime change, still one Worker —
that drive the Studio Architecture view, OpenAPI tags, and table ownership:
// lunora/billing/module.ts
import { defineModule } from "lunorash/server";
export default defineModule({ description: "Invoices and payments", tables: ["invoices"] });
description and tables inline; codegen reads them without running
the file. Modules do not nest.lunora/<module>/queues.ts. Export names stay app-wide (ctx.queues.<name>),
so two queues files exporting the same name is a codegen error.cross_module_table_write warns when a function outside the owning module
writes an owned table (insert, patch/replace/delete, batch or facade),
including through a same-file helper. Move the write into the owning module:
an exported helper there taking the caller's ctx (openInvoice(ctx, …)), or
a registered owner mutation via ctx.runMutation(internal.<owner>.<fn>, …)..extend(...) schema extensions) count as modules that
own their prefixed tables: write them through the component's functions, not
ctx.db directly.Id<"table"> — a write through an untyped string id cannot
be attributed to a table.A Worker the app calls (a parser, an LLM gateway) is declared in
lunora.config.ts, not discovered. Each one becomes a typed ctx.services.<key>
on actions (not queries/mutations: a cross-Worker call cannot be replayed or
rolled back; HTTP actions reach it through ctx.runAction):
// lunora.config.ts
export default {
services: {
documentParser: { dir: "services/document-parser" }, // fetch service
llmGateway: { dir: "services/llm-gateway", entrypoint: "Gateway" }, // RPC
},
};
// in an action
const parsed = await ctx.services.documentParser.fetch("https://parser/parse", { body, method: "POST" });
const text = await ctx.services.llmGateway.complete(prompt); // typed from the Gateway class
<dir>/wrangler.jsonc; Lunora writes the
services[] binding (SERVICE_<KEY>), runs it in the same lunora dev /
vite dev session, and deploys it before the app.*_URL var and no HMAC: set
"workers_dev": false on it when it has no public route (lunora doctor warns
otherwise). Keep its HMAC check if it has a custom domain or route.For webhooks or non-RPC HTTP, use httpRouter / httpRoute + httpAction:
httpRouter() takes no arguments — it returns a Hono
app you mount routes on, and you export the app. Passing it a routes object is a
type error (Expected 0 arguments), and from untyped code the routes simply
never mount.
// lunora/http.ts
import { httpRouter } from "lunorash/server";
import { internal } from "./_generated/internal.js";
const app = httpRouter();
const encoder = new TextEncoder();
// Stripe's scheme: `stripe-signature: t=<unix>,v1=<hex HMAC-SHA256 of "<t>.<raw body>">`.
// The Stripe SDK's `stripe.webhooks.constructEventAsync` does the same check.
const verifyStripe = async (secret: string, header: string | undefined, payload: string): Promise<boolean> => {
const fields = header?.split(",").map((part) => part.split("=", 2)) ?? [];
const t = fields.find(([k]) => k === "t")?.[1];
// While a secret is being rolled Stripe sends one `v1` per secret; accept any.
const v1s = fields.filter(([k, value]) => k === "v1" && /^[\da-f]{64}$/.test(value ?? "")).map(([, value]) => value ?? "");
if (!t || v1s.length === 0 || Math.abs(Date.now() / 1000 - Number(t)) > 300) {
return false; // missing, malformed, or outside the 5-minute replay window
}
const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["verify"]);
const signed = encoder.encode(`${t}.${payload}`);
for (const v1 of v1s) {
const signature = Uint8Array.from(v1.match(/../g) ?? [], (byte) => Number.parseInt(byte, 16));
if (await crypto.subtle.verify("HMAC", key, signature, signed)) {
return true; // constant-time compare
}
}
return false;
};
// A plain Hono handler, not `httpAction`: the action ctx (`c.var.lunora`) has
// no env, while `c.env` is the Worker env.
app.post("/webhooks/stripe", async (c) => {
const secret = c.env.STRIPE_WEBHOOK_SECRET;
if (typeof secret !== "string") {
throw new Error("STRIPE_WEBHOOK_SECRET is not set");
}
const payload = await c.req.text(); // the raw bytes the signature covers
if (!(await verifyStripe(secret, c.req.header("stripe-signature"), payload))) {
return c.text("invalid signature", 401);
}
await c.var.lunora.runMutation(internal.billing.record, { event: JSON.parse(payload) as unknown });
return c.text("ok");
});
export default app;
A webhook route is public: verify the provider's signature over the raw body
before any write, or anyone can forge events into internal.* functions.
v.* validators; _id/_creationTime not declared.withIndex, not
.filter, and are bounded (.take(n) / .paginate)..max(...).query (reactive read) / mutation (write) /
action (side effects via runQuery/runMutation).internal*; expected failures throw LunoraError.Id<"table">.ctx.db.related walk is narrowed with edges / direction, and its
foreign keys are indexed.cross_module_table_write is clean).lunora codegen shows no new advisor findings; lunora verify is clean.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub anolilab/lunora --plugin lunora15 skills in this plugin
/lunoraRoutes general Lunora requests to the right Lunora skill and gives the shared mental model (codegen loop, generated `api`/`internal` references, review commands, add-on capabilities, the `@lunora/mcp` server). Use when the user asks which Lunora skill to use, gives an underspecified task in a project with a `lunora/` directory or `lunorash`/`@lunora/*` dependencies, asks to review or audit Lunora code, asks about an add-on without its own skill (queues, workflows, agents, AI, flags, payments, notifications), or wants to expose a deployment to an AI agent over MCP./lunora-cloudflare-auditAudits a Lunora app for Cloudflare cost and reliability risk before launch or after a surprise bill. Chains the read-only `lunora doctor`, `lunora advisor` and `lunora insights` signals, then traces what they cannot see — retry and fan-out loops, cron cadence, public routes that reach a metered product (Workers AI, Browser, Containers, Vectorize, R2, D1), preview environments with paid bindings, and missing usage alerts (`lunora cloudflare alerts`). Reports each finding with the billing meter it moves, the smallest fix, and how to verify. Use when the user asks to "audit before launch", "what could blow up our Cloudflare bill", "why did the bill spike", "is this safe to put on the public internet", or "review our cron / queue / AI / workflow cost". For a slow query or OCC conflict use `lunora-performance-audit`; for a failing deploy use `lunora-deploy`./lunora-create-packageBuilds a reusable Lunora capability, either as a registry item that `lunora add` / `lunora registry add` copies into an app's `lunora/`, or as a publishable `@lunora/*` workspace package in the Lunora monorepo. Use when the user wants to package schema + functions + wrangler bindings + env vars for other apps, add or edit an item under `registry/<name>/` (`registry.json`), run `vis generate lunora-package`, or wire a new `ctx.*` capability into codegen./lunora-deployDeploys a Lunora app to Cloudflare Workers + Durable Objects and gets wrangler.jsonc, remote resources and secrets to line up. Covers `lunora deploy` (and `--env`, `--dry-run`, `--migrate`), isolated per-PR previews (`lunora preview deploy | list | delete`), the `lunora doctor` / `lunora verify` / `lunora prepare` preflights, Durable Object bindings (SHARD, SHARD_REGISTRY, SCHEDULER), the `DB` D1 binding and its placeholder id, R2 buckets, production secrets (`lunora env push`, `wrangler secret put`) versus `.dev.vars`, the schema-drift gate, the architecture diff, and rollback with `lunora cloudflare deployments`. Use when the user asks to "deploy", "ship to production", "set up a staging environment", wire CI deploys, or when a deploy fails on a binding, a placeholder `database_id`, an unexported Durable Object class, a missing secret, or "schema drift gate blocked deploy"./lunora-migration-helperPlans and runs Lunora schema and data migrations with the widen → migrate → narrow pattern. Covers online data migrations (`defineMigration` in `lunora/migrations.ts`, `lunora migrate create | up | down | status`), what the pre-deploy schema-drift gate treats as breaking and how a migration covers it, `.global()` table DDL on D1 (`lunora migrate generate`) and Hyperdrive, moving a dataset with `lunora migrate d1-to-hyperdrive` (and back with `hyperdrive-to-d1`), and re-keying a `.shardBy()` table. Use when the user wants to make a field required, rename or drop a field or table, change a field's type, add a unique index to a populated table, backfill data, switch a table between root / `.shardBy()` / `.global()`, or when `lunora deploy` reports "schema drift gate blocked"./lunora-performance-auditDiagnoses and fixes Lunora performance problems — full-table scans and missing indexes, expensive `ctx.db.related` traversals, OCC write conflicts (409 `CONFLICT`), live queries that re-run too often, hot shards, and choosing `.shardBy(key)` or `.global()` to scale. Starts from measured signal (`lunora insights`) and static signal (`lunora advisor`, `@lunora/advisor` lints such as `filter_without_index`, `unbounded_collect`, `unindexed_foreign_key`, `hot_shard`). Use when the user says a query or page is slow, mutations fail with conflicts under load, subscriptions are chatty, asks to "audit performance", "add the right index", or "should I shard this table", or when the Studio Advisors tab or `lunora advisor` flags something.Other plugins' skills for the same job