@fabrika/app 0.9.0-cloudflare.2 → 0.9.0-cloudflare.3

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 (53) hide show
  1. package/package.json +9 -9
  2. package/skill/SKILL.md +20 -18
  3. package/skill/references/app-setup.md +29 -16
  4. package/skill/references/endpoint.md +24 -22
  5. package/skill/references/errors.md +3 -2
  6. package/skill/references/jobs.md +13 -3
  7. package/skill/references/tests.md +7 -0
  8. package/src/bun.ts +159 -6
  9. package/src/cloudflare/application.ts +543 -73
  10. package/src/cloudflare/export.ts +621 -0
  11. package/src/cloudflare/import.ts +1097 -0
  12. package/src/cloudflare/maintenance.ts +518 -0
  13. package/src/cloudflare/ops.ts +147 -18
  14. package/src/cloudflare/restore.ts +167 -0
  15. package/src/cloudflare/tracker.ts +192 -0
  16. package/src/cloudflare.ts +3 -2
  17. package/src/contract/artifact-schema.ts +7 -1
  18. package/src/contract/build.ts +6 -2
  19. package/src/contract/context.ts +26 -9
  20. package/src/contract/defaults.ts +22 -2
  21. package/src/contract/define.ts +1 -1
  22. package/src/contract/group.ts +1 -1
  23. package/src/files/kinds.ts +54 -24
  24. package/src/implement/anonymous.ts +1 -10
  25. package/src/implement/boot.ts +21 -9
  26. package/src/implement/dispatch.ts +171 -33
  27. package/src/implement/egress.ts +34 -8
  28. package/src/implement/invocation.ts +50 -10
  29. package/src/implement/workload.ts +18 -3
  30. package/src/jobs/runner.ts +639 -96
  31. package/src/testing/kit.ts +60 -0
  32. package/types/src/bun.d.ts +34 -1
  33. package/types/src/cloudflare/application.d.ts +36 -5
  34. package/types/src/cloudflare/export.d.ts +16 -0
  35. package/types/src/cloudflare/import.d.ts +58 -0
  36. package/types/src/cloudflare/maintenance.d.ts +117 -0
  37. package/types/src/cloudflare/ops.d.ts +39 -4
  38. package/types/src/cloudflare/restore.d.ts +9 -0
  39. package/types/src/cloudflare/tracker.d.ts +29 -0
  40. package/types/src/cloudflare.d.ts +2 -1
  41. package/types/src/contract/artifact-schema.d.ts +2 -0
  42. package/types/src/contract/context.d.ts +24 -9
  43. package/types/src/contract/defaults.d.ts +19 -1
  44. package/types/src/contract/define.d.ts +1 -1
  45. package/types/src/contract/group.d.ts +1 -1
  46. package/types/src/files/kinds.d.ts +17 -6
  47. package/types/src/implement/anonymous.d.ts +0 -4
  48. package/types/src/implement/boot.d.ts +11 -0
  49. package/types/src/implement/dispatch.d.ts +31 -0
  50. package/types/src/implement/invocation.d.ts +17 -7
  51. package/types/src/implement/workload.d.ts +5 -0
  52. package/types/src/jobs/runner.d.ts +60 -2
  53. package/types/src/testing/kit.d.ts +16 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/app",
3
- "version": "0.9.0-cloudflare.2",
3
+ "version": "0.9.0-cloudflare.3",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -109,14 +109,14 @@
109
109
  "test": "bun test --path-ignore-patterns='src/react/**' && bun test --isolate src/react"
110
110
  },
111
111
  "dependencies": {
112
- "@fabrika/auth": "0.9.0-cloudflare.2",
113
- "@fabrika/auth-core": "0.9.0-cloudflare.2",
114
- "@fabrika/db": "0.9.0-cloudflare.2",
115
- "@fabrika/email": "0.9.0-cloudflare.2",
116
- "@fabrika/jwks": "0.9.0-cloudflare.2",
117
- "@fabrika/platform": "0.9.0-cloudflare.2",
118
- "@fabrika/platform-node": "0.9.0-cloudflare.2",
119
- "@fabrika/proxy-core": "0.9.0-cloudflare.2",
112
+ "@fabrika/auth": "0.9.0-cloudflare.3",
113
+ "@fabrika/auth-core": "0.9.0-cloudflare.3",
114
+ "@fabrika/db": "0.9.0-cloudflare.3",
115
+ "@fabrika/email": "0.9.0-cloudflare.3",
116
+ "@fabrika/jwks": "0.9.0-cloudflare.3",
117
+ "@fabrika/platform": "0.9.0-cloudflare.3",
118
+ "@fabrika/platform-node": "0.9.0-cloudflare.3",
119
+ "@fabrika/proxy-core": "0.9.0-cloudflare.3",
120
120
  "altcha-lib": "2.6.0",
121
121
  "jose": "^5.9.0"
122
122
  },
package/skill/SKILL.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: fabrika-app
3
- version: 0.9.0-cloudflare.2
3
+ version: 0.9.0-cloudflare.3
4
4
  description: >
5
5
  How to write and change a Fabrika application: src/vocabulary.ts, db/migrations/, src/db/tables.ts, the
6
6
  slices' endpoints.ts with their handlers, fabrika.contract.ts, jobs, files, webhooks, share links, connectors
@@ -234,14 +234,15 @@ the access check. `references/spa.md` shows the stack.
234
234
  | any public website (a job) | `egress: 'public'` | `fetch` to public hosts, never the installation |
235
235
  | a signed link (unsubscribe) | `signs: ['<purpose>']` | `ctx.tokens.sign` |
236
236
  | a webhook binding | `binds: '<webhook>'` | `ctx.webhooks('<webhook>').bind` and `.unbind` |
237
- | a job run after the commit | `kicks: ['<job>']` | `ctx.kick('<job>')` |
237
+ | a job run after the commit | `kicks: ['<job>']` | `ctx.kick('<job>')`, `ctx.kick('<job>', { at })` |
238
238
  | a trigger, function or view | its migration | — |
239
239
 
240
- Scope every scoped table a raw statement reads with `${ctx.sql.scope(<capability>, '<table>', '<alias>')}`, a conjunct
241
- of its WHERE or JOIN … ON (`where (<filter>) and ${…}`): `ctx.target` for `on` and `in`, `ctx.within` for `over`, a
242
- job's minted `Ref` or `Within` with `tx.sql.scope`. It binds the capability's whole predicate, chain included, as the
243
- handles apply it, and on an `update` or `delete` target it needs that operation's action; a reference table needs
244
- none. A deviation is fine: declare it, and do not contort the code to avoid it. The approver sees every declaration
240
+ Scope every scoped table a raw statement reads with `${ctx.sql.scope(<capability>, '<table>', { alias: '<alias>' })}`,
241
+ a conjunct of its WHERE or JOIN … ON (`where (<filter>) and ${…}`): `ctx.target` for `on` and `in`, `ctx.within` for
242
+ `over`, a job's minted `Ref` or `Within` with `tx.sql.scope`. It binds the capability's whole predicate, chain
243
+ included, as the handles apply it. The table an `update` or `delete` writes takes `{ use: 'update' }` or
244
+ `{ use: 'delete' }`, which needs that operation's action; a reference table needs none. The runtime does not check
245
+ where you put a fragment: the approver reads it. A deviation is fine: declare it, and do not contort the code to avoid it. The approver sees every declaration
245
246
  marked "needs closer review". Do not hide reach in a helper; the runtime builds `ctx.sql` and `ctx.db.unscoped` only
246
247
  where they are declared. A query or an export declares only `'read'`.
247
248
 
@@ -296,13 +297,15 @@ bunx tsc --noEmit # once, at the end
296
297
  bunx fabrika-check . # once, after tsc; refusals fail; hints print below them
297
298
  ```
298
299
 
299
- **On Cloudflare** (`fabrika.config.ts` imports `defineApp` from `@fabrika/provider-cloudflare`) the application runs in
300
- one Durable Object over SQLite. Its `db/migrations/*.sql` are SQLite (`references/table-and-migration.md`), and the
300
+ **On Cloudflare** (`fabrika.config.ts` imports `defineApp` from `@fabrika/provider-cloudflare`, as
301
+ `references/app-setup.md` shows) the application runs in one Durable Object over SQLite. Its `db/migrations/*.sql` are SQLite (`references/table-and-migration.md`), and the
301
302
  object applies them when it boots, so there is no `migrate` and no command needs a database URL or `FABRIKA_APP_ID`.
302
303
  The tests run on `@fabrika/db/testing-sqlite` (`references/tests.md`), in memory, and never skip. A handler holds the
303
- object's transaction, so its `limits.timeoutMs` is at most 25 s. No `sql` (`ctx.sql`) and no `scan: true`: the
304
- checker, the deploy and the boot refuse both. The deploy serves each SPA mount's built `dir` itself; declare no
305
- `assets` on the Worker (`references/app-setup.md`).
304
+ object's transaction, so its `limits.timeoutMs` is at most 25 s. A job's is at most 14 minutes, its default: longer
305
+ work claims a bounded batch per run, and the next run takes the rest (`references/jobs.md`). `ctx.sql` is SQLite there
306
+ (`references/endpoint.md`). No `scan: true`: the checker, the deploy and the boot refuse it. A migration names no
307
+ `fabrika_*` table but `REFERENCES fabrika_files (id)`. The deploy serves each SPA mount's
308
+ built `dir` itself; declare no `assets` on the Worker (`references/app-setup.md`).
306
309
 
307
310
  The object runs one transaction at a time, so every `await` in a handler that may write holds the whole environment:
308
311
  IAM, R2, a `fetch`, a `ctx.connect` call, a binding. One 2 s call caps the environment at half a request a second. A
@@ -322,9 +325,8 @@ bun test --isolate # no database URL; t
322
325
 
323
326
  A generated file changes only through its command (`codegen`, `test-scaffold`); running it is the loop, not an edit.
324
327
  Format and lint only the files you wrote, with the repository's own tools: in the fabrika-platform repository
325
- `bunx dprint fmt <files>` and `bunx biome lint <files>`, never `biome check` (its formatter fights dprint), and never a
326
- generated file. An application outside that repository has neither configuration: use the formatter its own
327
- repository configures, or none, and never add a configuration unasked; tsc and `fabrika-check` are its gates. `FABRIKA_APP_ID` is lowercase letters, digits and `-`. An applied file is immutable: while you still
328
+ `bunx dprint fmt <files>` and `bunx oxlint <files>`, and never a generated file. An application outside that
329
+ repository has neither configuration: use the formatter its own repository configures, or none, and never add a configuration unasked; tsc and `fabrika-check` are its gates. `FABRIKA_APP_ID` is lowercase letters, digits and `-`. An applied file is immutable: while you still
328
330
  change an uncommitted migration, migrate into a fresh schema with another `FABRIKA_APP_ID`. Read a refusal's
329
331
  "allowed form" and use it; fix hints too. `references/errors.md` explains the findings and the type errors.
330
332
 
@@ -352,9 +354,9 @@ e-mails them, a link holder's files, HR's review with its history.
352
354
  ## Not built yet
353
355
 
354
356
  - On Cloudflare, where the application runs in one Durable Object over its SQLite and its `db/migrations/*.sql` are
355
- SQLite, `sql` (`ctx.sql`) and a file kind with `scan: true` are refused. Everything else on the road runs there.
356
- - Jobs are cron only and app-wide: no `ctx.jobs.enqueue`, no job per scope. A `ctx.kick` starts a run of its job; it
357
- carries nothing, and the job finds its work in a queue table with `claim`. A list write is off the road (`sql`).
357
+ SQLite, a file kind with `scan: true` is refused. Everything else on the road runs there.
358
+ - Jobs are cron only and app-wide: no `ctx.jobs.enqueue`, no job per scope. A `ctx.kick` starts a run of its job, now
359
+ or at `{ at }`; it carries nothing, and the job finds its work in a queue table with `claim`. A list write is off the road (`sql`).
358
360
  - A read across tables (a join, a filter over a related table, an aggregate over several) is off the road (`sql`).
359
361
  - An MCP workspace has no shell, so `codegen` and `test-scaffold` cannot run there. Tell the human; never write a
360
362
  generated file by hand.
@@ -19,20 +19,20 @@ the SPA is added with it.
19
19
  "test": "bun test --isolate"
20
20
  },
21
21
  "dependencies": {
22
- "@fabrika/app": "0.9.0-cloudflare.2",
23
- "@fabrika/db": "0.9.0-cloudflare.2",
24
- "@fabrika/provider-zerops": "0.9.0-cloudflare.2",
25
- "@fabrika/proxy-core": "0.9.0-cloudflare.2",
26
- "@fabrika/ui": "0.9.0-cloudflare.2",
22
+ "@fabrika/app": "0.9.0-cloudflare.3",
23
+ "@fabrika/db": "0.9.0-cloudflare.3",
24
+ "@fabrika/provider-zerops": "0.9.0-cloudflare.3",
25
+ "@fabrika/proxy-core": "0.9.0-cloudflare.3",
26
+ "@fabrika/ui": "0.9.0-cloudflare.3",
27
27
  "kysely": "0.29.6",
28
28
  "react": "^19.0.0",
29
29
  "react-dom": "^19.0.0",
30
30
  "zod": "^3.25.76"
31
31
  },
32
32
  "devDependencies": {
33
- "@fabrika/check": "0.9.0-cloudflare.2",
34
- "@fabrika/cli": "0.9.0-cloudflare.2",
35
- "@fabrika/platform": "0.9.0-cloudflare.2",
33
+ "@fabrika/check": "0.9.0-cloudflare.3",
34
+ "@fabrika/cli": "0.9.0-cloudflare.3",
35
+ "@fabrika/platform": "0.9.0-cloudflare.3",
36
36
  "@happy-dom/global-registrator": "^20.11.2",
37
37
  "@types/bun": "^1.4.3",
38
38
  "@types/react": "^19.0.0",
@@ -251,16 +251,29 @@ version does not go live until every generated test passes. It writes every `FAB
251
251
 
252
252
  ## The deploy surface (Cloudflare)
253
253
 
254
- On Cloudflare `fabrika.config.ts` imports `defineApp`, `Worker` and `iamServiceReference` from
255
- `@fabrika/provider-cloudflare`, and `resources` returns the application Worker: `main: './fabrika/entry.generated.ts'`,
256
- `workers_dev: false`, `preview_urls: false`, `routes: []` and `bindings: { IAM: iamServiceReference() }`, with
257
- `pipeline: { workerDir: '.', build: 'bun run build:web' }` when the application has an SPA. The deploy adds the
258
- Durable Object, the files bucket, the minute tick, every `FABRIKA_*` variable and the proxy Worker.
254
+ ```ts fabrika.config.ts
255
+ import { defineApp } from '@fabrika/provider-cloudflare'
256
+
257
+ export default defineApp({ id: 'tasks' })
258
+ ```
259
259
 
260
- It also serves the SPA: when the Worker declares no `assets`, the deploy copies the build directory of each SPA and
260
+ That is the whole config. `defineApp` builds the application Worker: named after the id, running
261
+ `fabrika/entry.generated.ts`, with no route and no public URL, and IAM bound at its application entrypoint. The deploy
262
+ builds the proxy Worker from `fabrika.contract.ts` and writes the application's Durable Object, its files bucket, its
263
+ cron trigger and every `FABRIKA_*` variable; Control writes the IAM schema from the approved contract. Declare none of
264
+ them: a config that does is refused. Write no `resources`, `schema` or `pipeline.workerDir` (the config's directory is
265
+ the default). A binding of the application's own, or another `compatibility_date` or `compatibility_flags`, goes in
266
+ `worker: { bindings, compatibility_date, compatibility_flags }`; `IAM` stays the platform's. `package.json` names
267
+ `@fabrika/provider-cloudflare` where the Zerops one names `@fabrika/provider-zerops`, the entry is written by
268
+ `fabrika app build --provider=cloudflare` (the deploy runs it again from the pinned commit), and there is no
269
+ `zerops.yaml`.
270
+
271
+ It also serves the SPA: the deploy copies the build directory of each SPA and
261
272
  link mount (`dir`, a link's `spa`) into `fabrika/assets/` at the mount's prefix (`/admin` into `fabrika/assets/admin/`)
262
- after `pipeline.build`, and binds it as `ASSETS`. A deploy whose mount directory the build did not write fails and
263
- names the mount. Do not declare `assets` yourself. A Worker that declares them keeps them, and the object reads each
273
+ after `pipeline.build`, and binds it as `ASSETS`. A `.assetsignore` at the root of a mount's directory excludes files
274
+ as it does for wrangler (gitignore syntax, relative to that directory); `.git`, `.hg`, `.svn` and `.bzr` are never
275
+ copied. A deploy whose mount directory the build did not write fails and
276
+ names the mount. A config that composes its own Worker with `resources` and declares `assets` keeps them instead, and the object reads each
264
277
  request path from that directory as it is (`/admin/app.js` is `admin/app.js` there), so a hand-written `assets` works
265
278
  only for a root mount, or for mounts whose directories nest like their prefixes; it needs `binding: 'ASSETS'` and
266
279
  `run_worker_first: true`.
@@ -406,7 +406,7 @@ export const workloadRows = (
406
406
  count(*) filter (where t.status <> 'done')::int as open,
407
407
  count(*) filter (where t.status <> 'done' and t.due_on < current_date)::int as overdue
408
408
  from tasks t
409
- where ${sql.scope(org, 'tasks', 't')}
409
+ where ${sql.scope(org, 'tasks', { alias: 't' })}
410
410
  and (${status}::text is null or t.status::text = ${status})
411
411
  group by t.assignee
412
412
  order by t.assignee`
@@ -421,7 +421,7 @@ export const workloadRows = (
421
421
  ## Raw SQL: `sql: 'read'`
422
422
 
423
423
  Raw SQL is legitimate for a join, a report, an `OR` or `IS NULL` filter, or a bulk write. Declare it on the endpoint;
424
- `ctx.sql` exists only there. It runs on Postgres only: on Cloudflare a site that declares `sql` is refused.
424
+ `ctx.sql` exists only there. It runs on Postgres and, on Cloudflare, on SQLite (the rules of each are below).
425
425
 
426
426
  ```ts src/reports/endpoints.ts
427
427
  import { attachment } from '@fabrika/app/contract'
@@ -475,7 +475,7 @@ export const reports = app.group({ callers: ['human'] }, (r) => ({
475
475
  const rows = await ctx.sql(TaskExportRow)`
476
476
  select t.title, t.status, t.assignee, t.due_on::text as due_on
477
477
  from tasks t
478
- where ${ctx.sql.scope(ctx.target, 'tasks', 't')}
478
+ where ${ctx.sql.scope(ctx.target, 'tasks', { alias: 't' })}
479
479
  order by t.title
480
480
  limit 10000`
481
481
  await ctx.db.target.project_exports.insert({ row_count: rows.length })
@@ -487,7 +487,7 @@ export const reports = app.group({ callers: ['human'] }, (r) => ({
487
487
  - Every `${…}` of a value is a bound parameter, never SQL text. The one exception is `${ctx.sql.scope(…)}`: it is SQL
488
488
  (the predicate) around its own bound values, and it may have no value at all (`(true)`, `(false)`). Put no
489
489
  interpolation inside quotes, a quoted name, a dollar body or a comment: the tag refuses it.
490
- - **Scope every scoped table the statement reads with `${ctx.sql.scope(capability, '<table>', '<alias>')}`.**
490
+ - **Scope every scoped table the statement reads with `${ctx.sql.scope(capability, '<table>', { alias: '<alias>' })}`.**
491
491
  `ctx.sql` has no scope predicate of its own. The fragment is the capability's whole predicate on that table: for a
492
492
  `Ref` the object's column and every link of its chain, so a project moved to another org is out; for a `Within` its
493
493
  values, each container grant on its own column, every row for a global grant and none for an empty one. The
@@ -495,29 +495,31 @@ export const reports = app.group({ callers: ['human'] }, (r) => ({
495
495
  `Within` it minted to `tx.sql.scope`. A table the capability does not reach is a type error at the table name. A
496
496
  reference table is shared by every scope and needs no fragment, and no statement writes it. An application table
497
497
  needs none either (the intake job of `references/public-intake.md`).
498
- - **A fragment carries its action into the statement.** On the table an `update` or `delete` writes it needs the
499
- table's own update or delete action (`ctx.within('tasks.write', 'project')` for `update tasks`), so a read action
500
- never scopes a write. A column's own write action (`columns: { assignee: { write: 'tasks.assign' } }`) scopes an
501
- `update` too, when every column its `set` assigns declares that action: a table with only column writes is updated
502
- this way; anywhere else it needs an action the handles read the table with. `insert … on conflict do
503
- update … where ${…}` is an update of the inserted table. The fragment answers for its own query block: a CTE that
504
- updates and a statement that deletes take one capability each, and another occurrence of the written table (a
505
- subquery, `from tasks other`) reads with a read capability under its own alias. A refused fragment fails the call
506
- before anything is sent.
507
- - **A fragment is a conjunct.** It stands alone, joined by AND, at the top level of a WHERE or JOIN … ON clause whose
508
- top level holds no OR: `where (<the whole filter>) and ${…}`, `join projects p on p.id = t.project_id and ${…}`.
509
- The tag refuses `where a or ${…}`, `where a or b and ${…}`, `not ${…}`, a fragment in parentheses or outside such a
510
- clause. Never scope by hand with `ctx.target.value` alone: it drops the chain. The generated tests check that an `in`
511
- endpoint with `sql` leaks nothing of another scope.
498
+ - **A fragment states what it scopes.** `use` is `'read'` by default; the table an `update` or `delete` writes takes
499
+ `{ use: 'update' }` or `{ use: 'delete' }`, which needs the table's own update or delete action
500
+ (`ctx.within('tasks.write', 'project')` for `update tasks`) on a `'write'` site, so a read action never scopes a
501
+ write. A column's own write action (`columns: { assignee: { write: 'tasks.assign' } }`) is enough for `use: 'update'`
502
+ too; set only the columns it writes. `insert … on conflict do update … where ${…}` scopes the inserted table with
503
+ `use: 'update'`. Each block takes its own fragment: a CTE that updates and a statement that deletes take one
504
+ capability each, and another occurrence of the written table (a subquery, `from tasks other`) reads under its own
505
+ alias. A refused fragment fails the call before anything is sent.
506
+ - **A fragment is a conjunct.** Put it alone, joined by AND, at the top level of a WHERE or JOIN … ON clause whose top
507
+ level holds no OR: `where (<the whole filter>) and ${…}`, `join projects p on p.id = t.project_id and ${…}`. The tag
508
+ does not read where it stands, which relation it filters or which columns a `set` assigns: the approver does, and a
509
+ fragment under an `or` widens the statement to every row. Never scope by hand with `ctx.target.value` alone: it drops
510
+ the chain. The generated tests check that an `in` endpoint with `sql` leaks nothing of another scope.
512
511
  - Rows are parsed like `fromRow` (snake_case to camelCase), then by the schema. Postgres sends `bigint` and `numeric`
513
512
  as text: cast a count (`count(*)::int`), and a sum to the type its output declares (`::int` for an integer sum that
514
513
  fits, `::float8` for a fractional one), or keep it a string. A sum over no rows is `null`: `coalesce(sum(…), 0)`.
515
514
  - An optional filter is `(${value}::text is null or <column> = ${value})`.
516
515
  - One statement: `select`, `with`, `values`, plus `insert`, `update` and `delete` on a `'write'` site (a mutation, a
517
- job or a webhook). Refused before anything is sent: DDL, `SET`, `COPY`, transaction control, another schema, any
518
- `fabrika_*` or `pg_*` name, and calls outside the reviewed built-ins. A function your migrations declare is refused
519
- too: call it from a view. These name rules are defense in depth; the runtime role (DML only) is the boundary.
520
- - A query runs in a read-only transaction; a mutation's `ctx.sql` runs in the same transaction as its handles.
516
+ job or a webhook). Refused before anything is sent: DDL, `SET`, `COPY`, transaction control, and any `fabrika_*`
517
+ name. On Postgres also another schema, any `pg_*` name, and calls outside the reviewed built-ins; a function your migrations declare is refused too: call it from a view. On SQLite also any `sqlite_*`
518
+ name, `PRAGMA`, `ATTACH`, and a parameter you write yourself (`?`, `:x`, `@x`, `$x`): interpolate every value.
519
+ - On SQLite write SQLite: `count(*)` is already an integer, there is no `::` cast (`cast(x as text)`), and a list value
520
+ binds as JSON (`t.status in (select value from json_each(${list}))`).
521
+ - A query runs in a read-only transaction; a mutation's `ctx.sql` runs in the same transaction as its handles. A
522
+ `'read'` statement writes nothing, even in a mutation: one that tries fails the call.
521
523
 
522
524
  ## A file download: `attachment`
523
525
 
@@ -31,9 +31,10 @@ The refusals agents meet most, and their fix:
31
31
  | `on-conflict-index` | `insert(…, { onConflict: { columns } })` whose columns are no unique index the migrations create | a `UNIQUE` constraint or a `CREATE UNIQUE INDEX` without `WHERE` on exactly those columns |
32
32
  | `cross-app-fetch` | a `fetch` of an absolute URL under no origin a site declares in `egress`, or one a job's `egress: 'public'` refuses (an IP address, `localhost`) | `egress: ['https://…']` on the site that calls it (`references/endpoint.md`) |
33
33
  | `url-endpoint` | `ctx.url('<id>')` names no procedure of the contract | the endpoint id `<mount>.<group>.<procedure>` |
34
- | `cloudflare-sql` | on Cloudflare, a site that declares `sql` | the handles (`ctx.db`), without `sql` |
35
34
  | `cloudflare-scan` | on Cloudflare, a file kind with `scan: true` | the kind without `scan`, and no public procedure that takes its files |
35
+ | `cloudflare-job-segment` | on Cloudflare, a job whose `limits.timeoutMs` exceeds one 14-minute segment | at most 14 minutes: claim a bounded batch per run, and the next run takes the rest |
36
36
  | `cloudflare-current-timestamp` | on Cloudflare, `DEFAULT CURRENT_TIMESTAMP` in a migration: SQLite stores `YYYY-MM-DD HH:MM:SS`, which every read of the table refuses | `DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))`, in a new migration |
37
+ | `cloudflare-framework-name` | on Cloudflare, a `fabrika_*` name in a migration, quoted or not | your own tables, and `REFERENCES fabrika_files (id)` for a file column |
37
38
 
38
39
  The hints:
39
40
 
@@ -105,7 +106,7 @@ message. The codes you meet most:
105
106
 
106
107
  | Code | Means |
107
108
  | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
108
- | `query_refused` | the data layer refused a statement or an operation: `undeclared_reach`, `sql_read_only`, `unknown_column`, `read_only`, `sql_outside_transaction`, `sql_dialect` (`ctx.sql` on SQLite, Cloudflare) …; the reason's text names the rule |
109
+ | `query_refused` | the data layer refused a statement or an operation: `undeclared_reach`, `sql_read_only`, `unknown_column`, `read_only`, `sql_outside_transaction`, `sql_scope`, `sql_name` …; the reason's text names the rule |
109
110
  | `capability_refused` | a capability of another kind, dimension or action than the call takes (`wrong_capability`, `wrong_action`, `out_of_reach`), one kept past its request (`ended`, `foreign_work`), or a `fetch` its `egress` refuses (`undeclared_egress`, `refused_egress`, `oversized_egress`) |
110
111
  | `handler_failed` | your handler threw something else; the log has no more: the Operations issue has the stack, or test the handler |
111
112
 
@@ -4,8 +4,8 @@ Background work is a cron job: `app.cron(schedule, { actions })` with its `.hand
4
4
  `endpoints.ts` of the slice whose data it works on, mounted under `jobs` in the contract. There are no timers, loops,
5
5
  `setInterval` or self-scheduled work, and no `ctx.jobs.enqueue`: work for later is a row that a run picks up, usually
6
6
  from a queue table with `claim` (below). A mutation, a webhook or another job that declares `kicks: ['<job>']` and
7
- calls `ctx.kick('<job>')` starts a run within seconds of its commit (the outbox below); the schedule picks up whatever
8
- that run leaves.
7
+ calls `ctx.kick('<job>')` starts a run within seconds of its commit (the outbox below), and `ctx.kick('<job>', { at })`
8
+ starts one at `at`; the schedule picks up whatever that run leaves.
9
9
 
10
10
  ```ts src/overdue/endpoints.ts
11
11
  import { app } from '../app-kit'
@@ -118,7 +118,7 @@ export const contract = app.contract((c) => ({
118
118
  one, since no zone is a day further ahead.
119
119
  - **Reads run outside the transaction; every write runs inside `ctx.transaction((tx) => …)`**, through `tx.db`, fenced
120
120
  by the attempt's lease. A write outside it is a type error that names `tx.db`.
121
- - An attempt that fails or outlives `limits.timeoutMs` (default 15 minutes, at most a day) is retried up to `retries` times, so every write is
121
+ - An attempt that fails or outlives `limits.timeoutMs` (default 14 minutes, at most a day; at most 14 minutes on Cloudflare) is retried up to `retries` times, so every write is
122
122
  idempotent. **Re-read each row under its lock before you write it:** `tx.db.on(ref).getForUpdate()` inside the
123
123
  transaction, decide again on what it answers with the list's whole filter (`isOverdue`), and write with its
124
124
  `version`. The listed row is already old: someone
@@ -238,6 +238,8 @@ for (const row of await ctx.db.app('mail.send').outbox.claim({ limit: 50 })) {
238
238
  later.
239
239
  - A failure or a crash leaves the row as the claim left it, due again after the backoff; nothing retries at once.
240
240
  Attempts count claims, so claim only what the run will reach.
241
+ - A claim that leased rows runs the job again when the same claim would next find one (its `where`, its scope, below
242
+ `maxAttempts`), no sooner than a second later; a claim that took its full `limit` runs it again at once.
241
243
  - A row past `maxAttempts` stays in the table, no longer due, for the application to show or requeue.
242
244
  - One recipe covers an outbox, a per-record reminder (`<due>` is the reminder time) and asynchronous work: a mutation
243
245
  inserts a request row and kicks; the job claims it, renders, puts the result into a file column with
@@ -253,9 +255,17 @@ row, in its own transaction: a public procedure's row (`registrations`), an outb
253
255
  transaction committed, the job runs within seconds, under its own key, and the answer does not wait for it. A thrown
254
256
  handler, a refused answer and a failed commit kick nothing. Kicks coalesce: at most one run is active and one pending,
255
257
  however many calls arrive. A kick carries nothing; the job reads its rows, and its schedule stays the recovery.
258
+ The kick is written in the site's transaction, so a crash right after the commit still runs the job.
256
259
  `kicks` names cron jobs of the contract, as literals (`r.mutation({ …, kicks: ['testMails'] })`); a mutation, a
257
260
  webhook and a job declare it, and the kick edges never form a cycle.
258
261
 
262
+ **A kick for later.** `ctx.kick('<job>', { at })`, with `at` a `Date`, runs the job at that time instead of after the
263
+ commit: a reminder, a retry the application times itself. Kicks of one job in the same second are one; kicks at
264
+ different times stay apart, and a run takes only those already due. The second argument is the time and nothing else;
265
+ what to do stays in the job's rows (`where <due> <= now()`, or a queue table's `<due>`). On Cloudflare the object's
266
+ alarm runs it within seconds of `at`; on Bun the serving process that wrote it runs it at `at`, and another process
267
+ finds it within a minute.
268
+
259
269
  The rules of the job:
260
270
 
261
271
  - **Bounded batches.** Each run takes at most a fixed number of records (`order by received_at, id limit 50`), oldest
@@ -322,6 +322,13 @@ if (postgres !== null) {
322
322
  - A mutation that kicks (`kicks`, `ctx.kick`) starts its job in the background of the call. `kit.drain()` waits for
323
323
  those runs, and `kit.runJob` first waits for them; every generated access case drains before it answers. The kicked
324
324
  run may start at once, so arm a fault before the call, never between the call and the run.
325
+ - `await kit.runDueJobs({ within: 3 * 3600000 })` runs every job with a wake due now or within `within` milliseconds
326
+ (0 by default) once, as the Cloudflare object's alarm runs it: a cron job's next fire, a kick, a failed run's retry.
327
+ It answers `[{ job, result }]` for the runs it started, and first waits for the kicked runs. Without it the kit fires
328
+ no wake: a cron job runs only through `runJob` or `runDueJobs`. Only the wakes move, never the clock: a handler's
329
+ `new Date()` and a queue row's due time stay real, so a row due later is not claimed early. A second call does not run
330
+ a cron fire the first one ran early. A failed run of a job with `retries` is due again after its backoff, at most a
331
+ second for the first retry, so `runDueJobs({ within: 2000 })` runs it. It needs the application's data layer.
325
332
  - `kit.mail` is the stand-in for every job's mail transport (`MemoryEmailSender` of `@fabrika/app/testing`), so a test
326
333
  needs no mail variable. `kit.mail.accepted` is what reached the inbox, once per key, and `kit.mail.handoffs` every
327
334
  send with its `answer`. `refuse(code?)`, `unavailable()`, `unreachable()`, `loseAnswers(n)` and `during(hook)`
package/src/bun.ts CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  import { HttpIamMembership, HttpIamTerminals } from '@fabrika/auth'
9
9
  import type { Database } from '@fabrika/db'
10
- import { isScopedDatabase } from '@fabrika/db/framework'
10
+ import { isScopedDatabase, type Session } from '@fabrika/db/framework'
11
11
  import type { PostgresOptions } from '@fabrika/db/postgres'
12
12
  import type { EmailProviderRegistry } from '@fabrika/email'
13
13
  import { directoryEmailProvider } from '@fabrika/email/directory'
@@ -19,11 +19,12 @@ import { diskAssets } from './disk-assets.js'
19
19
  import type { FetchService, RequestExecutionContext } from './fetch.js'
20
20
  import { clamscanScanner } from './files/clamscan.js'
21
21
  import { boot, BootError, type IamKeySource } from './implement/boot.js'
22
+ import { REQUEST_BUDGET_MS } from './implement/bounded.js'
22
23
  import { type CrashEvents, environmentCrashReporter, type ProcessCrashKind, reportFatalCrashes, reportWithin } from './implement/crashes.js'
23
24
  import { type CapturedEnvironment, captureEnvironment } from './implement/environment.js'
24
25
  import { type ContractImplementation, isContractImplementation } from './implement/implement.js'
25
26
  import { issuerWorkloadTokens, type WorkloadTokenSource } from './implement/workload.js'
26
- import { JOB_CHECK_INTERVAL_MS, type JobRunOptions, type JobRunResult } from './jobs/runner.js'
27
+ import { type ContractJobs, JOB_CHECK_INTERVAL_MS, type JobRunOptions, type JobRunResult } from './jobs/runner.js'
27
28
  import { makeProcessUndumpable } from './undumpable.js'
28
29
 
29
30
  export { diskAssets } from './disk-assets.js'
@@ -135,6 +136,145 @@ async function loadDataLayer(): Promise<DataLayerOpeners> {
135
136
  /** How long a stopping process waits for the work its requests left running; a kicked run aborts at once. */
136
137
  const SHUTDOWN_DRAIN_MS = 10_000
137
138
 
139
+ /**
140
+ * How long the wake timer waits for a wake that is already due: such a wake's own kick is under way (the post-commit start,
141
+ * or the timer's last one), and looking at once would kick its job a second time before that kick took the lease.
142
+ */
143
+ export const WAKE_RETRY_MS = 1_000
144
+
145
+ /**
146
+ * How long after a write projected the timer a refresh that cannot see the projected wake looks again: the write may still
147
+ * be committing, and no transaction that writes a wake outlasts a request's budget.
148
+ */
149
+ export const WAKE_SETTLE_MS = REQUEST_BUDGET_MS + 5_000
150
+
151
+ /** A wake a write of this process projected and no refresh has seen yet: its time, and until when the timer looks for it. */
152
+ interface UnseenWake {
153
+ readonly at: number
154
+ readonly until: number
155
+ /** The next look's wait: it doubles from `WAKE_RETRY_MS`. */
156
+ readonly retry: number
157
+ }
158
+
159
+ /**
160
+ * The serving process's one timer to the jobs' next wake, Bun's side of the scheduler (ADR-0065 §5): it kicks each job
161
+ * whose wake came due through the kick path, so the lease admits one container. A write of this process lowers it in
162
+ * that write's transaction (`project`, `BootOptions.projectWakes`), by the delay on the database's clock; a wake another
163
+ * container wrote is found when the serving process refreshes it with its job check. Crontab still runs the schedule.
164
+ */
165
+ export class WakeTimer {
166
+ readonly #track: (kick: Promise<void>) => void
167
+ #jobs: ContractJobs | null = null
168
+ #timer: ReturnType<typeof setTimeout> | undefined
169
+ /** When the armed timer fires, in epoch milliseconds; `Infinity` while none is armed. */
170
+ #at = Number.POSITIVE_INFINITY
171
+ #stopped = false
172
+ /** The refresh under way: one at a time, and a refresh asked for meanwhile looks again after the retry wait. */
173
+ #refreshing: Promise<void> | null = null
174
+ #again = false
175
+ #unseen: UnseenWake | null = null
176
+
177
+ /** `track` holds each kick the timer starts until it ended, for the shutdown drain. */
178
+ constructor(track: (kick: Promise<void>) => void) {
179
+ this.#track = track
180
+ }
181
+
182
+ /** The booted application's jobs; until then nothing fires. */
183
+ attach(jobs: ContractJobs): void {
184
+ this.#jobs = jobs
185
+ }
186
+
187
+ /**
188
+ * `BootOptions.projectWakes`: never later than the next wake the write leaves, which a rollback only makes early. The
189
+ * timer may fire before the write commits, so the wake stays unseen until a refresh finds it.
190
+ */
191
+ readonly project = async (transaction: Session): Promise<void> => {
192
+ const delay = await this.#jobs?.scheduler?.untilNext(transaction)
193
+ if (delay === undefined || delay === null || this.#stopped) {
194
+ return
195
+ }
196
+ const now = Date.now()
197
+ this.#unseen = { at: Math.min(now + delay, this.#unseen?.at ?? Number.POSITIVE_INFINITY), until: now + WAKE_SETTLE_MS, retry: WAKE_RETRY_MS }
198
+ this.#lower(now + delay)
199
+ }
200
+
201
+ /** Kicks every job whose wake is due and arms the timer at the next one; it never rejects. */
202
+ refresh(): Promise<void> {
203
+ if (this.#stopped) {
204
+ return Promise.resolve()
205
+ }
206
+ if (this.#refreshing !== null) {
207
+ this.#again = true
208
+ return this.#refreshing
209
+ }
210
+ const refreshing = this.#refresh().finally(() => {
211
+ this.#refreshing = null
212
+ if (this.#again) {
213
+ this.#again = false
214
+ this.#lower(Date.now())
215
+ }
216
+ })
217
+ this.#refreshing = refreshing
218
+ return refreshing
219
+ }
220
+
221
+ async #refresh(): Promise<void> {
222
+ const jobs = this.#jobs
223
+ if (jobs === null) {
224
+ return
225
+ }
226
+ clearTimeout(this.#timer)
227
+ this.#at = Number.POSITIVE_INFINITY
228
+ const unseen = this.#unseen
229
+ this.#unseen = null
230
+ let delay: number | null
231
+ try {
232
+ delay = await jobs.wake(this.#track)
233
+ } catch {
234
+ // A driver error may quote the database URL.
235
+ console.error('fabrika: the wake timer could not read the jobs: driver_failed')
236
+ delay = 0
237
+ }
238
+ const now = Date.now()
239
+ // A projected wake this refresh did not find: its write may not have committed yet.
240
+ if (unseen !== null && (delay === null || now + delay > unseen.at + WAKE_RETRY_MS) && now < unseen.until) {
241
+ this.#keepUnseen(unseen)
242
+ this.#lower(now + unseen.retry)
243
+ }
244
+ if (delay !== null) {
245
+ this.#lower(now + delay)
246
+ }
247
+ }
248
+
249
+ /** Keeps looking for `unseen`, later next time, beside what writes projected during the refresh. */
250
+ #keepUnseen(unseen: UnseenWake): void {
251
+ const later = this.#unseen
252
+ this.#unseen = {
253
+ at: Math.min(unseen.at, later?.at ?? Number.POSITIVE_INFINITY),
254
+ until: Math.max(unseen.until, later?.until ?? 0),
255
+ retry: unseen.retry * 2,
256
+ }
257
+ }
258
+
259
+ /** Fires no more and refreshes no more; resolves once a refresh under way ended, so it starts no kick after it. */
260
+ async stop(): Promise<void> {
261
+ this.#stopped = true
262
+ clearTimeout(this.#timer)
263
+ await this.#refreshing
264
+ }
265
+
266
+ #lower(next: number): void {
267
+ const now = Date.now()
268
+ const at = next <= now ? now + WAKE_RETRY_MS : next
269
+ if (this.#stopped || at >= this.#at) {
270
+ return
271
+ }
272
+ clearTimeout(this.#timer)
273
+ this.#at = at
274
+ this.#timer = setTimeout(() => void this.refresh(), at - now)
275
+ }
276
+ }
277
+
138
278
  /** Waits for `drain`, at most `ms`; a drain the bound cuts short is logged. */
139
279
  async function drainWithin(drain: Promise<void>, ms: number): Promise<void> {
140
280
  const expired = Promise.withResolvers<void>()
@@ -397,6 +537,11 @@ async function startContractServer(
397
537
  // Zerops is where a Bun application runs, and a namespace there is one private network (ADR-0022 rule 5).
398
538
  const files = openFileStorage(environment.framework)
399
539
  const workloadTokens = workloadTokensOf(environment.framework)
540
+ const timerKicks = new Set<Promise<void>>()
541
+ const wakes = new WakeTimer((kick) => {
542
+ timerKicks.add(kick)
543
+ void kick.finally(() => timerKicks.delete(kick))
544
+ })
400
545
  const served = boot(app, {
401
546
  environment,
402
547
  iam,
@@ -410,24 +555,32 @@ async function startContractServer(
410
555
  requireProxySecret: true,
411
556
  localEmailProviders: LOCAL_EMAIL_PROVIDERS,
412
557
  connections: databaseConnections(environment.framework[DATABASE_CONNECTIONS]) ?? DEFAULT_DATABASE_CONNECTIONS,
558
+ projectWakes: wakes.project,
413
559
  ...(operationsFetch === undefined ? {} : { operationsFetch }),
414
560
  })
561
+ wakes.attach(served.jobs)
415
562
  // The work a request leaves running (a failure's report, a kicked run) is drained, within a bound, before the database closes.
416
563
  const handler = createBunHandler({ fetch: (request, _env: undefined, exec) => served.fetch(request, exec) }, undefined, {
417
564
  onBackgroundError: () => console.error('fabrika: a background task of a request failed'),
418
565
  })
419
566
  const server = Bun.serve({ port, hostname: '0.0.0.0', fetch: (request) => handler.fetch(request) })
420
- // The crontab may never fire; the serving process notices a job that stopped succeeding without being invoked.
421
- const check = () => void served.jobs.check()
567
+ // The crontab may never fire; the serving process notices a job that stopped succeeding without being invoked, and a
568
+ // wake another container wrote.
569
+ const check = () => {
570
+ void served.jobs.check()
571
+ void wakes.refresh()
572
+ }
422
573
  const checks = served.jobs.names.length === 0 ? undefined : setInterval(check, JOB_CHECK_INTERVAL_MS)
423
574
  if (checks !== undefined) {
424
575
  check()
425
576
  }
426
577
  const stop = async (): Promise<void> => {
427
578
  clearInterval(checks)
579
+ // Before the kicks stop and the drain takes the timer's kicks: a refresh under way may still start one.
580
+ await drainWithin(wakes.stop(), SHUTDOWN_DRAIN_MS)
428
581
  served.stopKicks()
429
582
  await server.stop(false)
430
- await drainWithin(handler.drain(), SHUTDOWN_DRAIN_MS)
583
+ await drainWithin(Promise.all([handler.drain(), ...timerKicks]).then(() => undefined), SHUTDOWN_DRAIN_MS)
431
584
  await database?.close()
432
585
  }
433
586
  process.once('SIGTERM', () => void stop().then(() => process.exit(0)))
@@ -436,7 +589,7 @@ async function startContractServer(
436
589
 
437
590
  /**
438
591
  * Every address `hostname` resolves to, as the operating system's resolver answers Bun's own `fetch`: a cron job's public
439
- * egress refuses a hop whose name resolves to any private one (ADR-0070).
592
+ * egress refuses a hop whose name resolves to any private one (ADR-0071).
440
593
  */
441
594
  const resolveHost = async (hostname: string): Promise<readonly string[]> =>
442
595
  (await lookup(hostname, { all: true, verbatim: true })).map((entry) => entry.address)