Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 41 additions & 134 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ make test # starts the test Postgres in Podman, runs every test
make lint # formatting, odoc, the release build, Hypha's rules by litany, and the dashboard's bundle rebuilt and compared
make fmt # format in place
make dashboard # the dashboard's page built into dashboard/assets, to commit
make dashboard-serve # the dashboard's example on the test Postgres, at localhost:4400/spool
make docs # the documentation site into _build/docs, as Pages publishes it, and
# agents/ from its pages; docs-serve serves it
make release VERSION=1.2.3 # tag and release
Expand All @@ -28,144 +29,50 @@ Pages from `main`.
## Layout

```
src/
spool the queue: the table's statements, kinds, enqueueing, the
runner, its leases, retries and pruning, and drain for tests
migrations/ the table's migrations, plain SQL, installed with the package
in share/spool/migrations; the tests make their table from
them
docs/ the documentation site, a Starlight site in Hypha's look, from
starlight-theme-hypha: its pages in src/content/docs/, every
example imported from examples/ with Vite's ?raw into
Starlight's <Code>, and
built into _build/docs, which dune clean removes
dashboard/ spool-dashboard, its own package over spool, spindle and
rowtype-postgres
spool_dashboard the routes, each asking the application's operator check
first
wire what they read and answer, as wiretype descriptions
live the live state: one reader a process, sending what changed
bundle dashboard/assets made a module by ocaml-crunch as it builds
assets/ the page as Vite built it, committed
web/ the page's source: OCaml on solidtype and its HeroUI, rhp's
chart, dressed in Hypha's theme by themes/hypha.css over
HeroUI's tokens, its fonts bundled; printer/ prints its
client from the routes, and Vite bundles what Melange emits
into assets/
test/ the routes in-process against a real Postgres: who may do
what, sensitive payloads, paging, actions, the live stream
agents/ the documentation for a user's agent, generated by
starlight-hypha-agents from the pages, `make docs`:
llms.txt, the catalogue; pages/, each page as Markdown;
installed by the spool package
examples/ the programs the site shows, each built, and run against a
Postgres by hand
test/
test_spool the queue against a real Postgres: claims, leases taken
mid-job, renewals that hang, listeners killed, payloads kept
out of every log line
db_target a database of a case's own, from SPOOL_TEST_PG
test_docs every example on a page
src/ the queue: spool.ml and its interface
migrations/ the table's migrations, plain SQL, installed with the package
dashboard/ spool-dashboard: its routes, their wire, the live stream;
web/ is the page's source, assets/ its committed build
docs/ the documentation site; agents/ is generated from its pages
examples/ the programs the site shows, each built
test/ the queue against a real Postgres, and every example a page shows
```

How to use it is the documentation site, every example imported from
`examples/` whole: a user-visible change updates its page there.
A user-visible change updates its page in `docs/`.

## Rules that must hold

Each an instruction, its reason in a clause, and the way you will find out
you broke it. Cite an entry by its bold phrase.
Each a design rule the code alone does not show, and the test that guards it.

- **A job is claimed, run and finished in three transactions, and the work
is in none of them.** A claim held open holds a connection and a row for
as long as the work. Symptom: the runner's pool exhausted by long jobs;
`test_spool` runs more jobs at once than the pool has
connections.
- **A finish counts only under its lease, and a lease is renewed or its job
stopped.** Finishing is guarded by runner and attempt, and a runner that
cannot renew stops the job a quarter of a lease before it ends -- from a
watchdog that asks the database nothing, since the renewal is what may
hang -- so a job runs in two places only when a runner stalls past its own
lease or two clocks disagree by more than that quarter. Symptom: a job
completed twice; the suite takes a lease from a runner mid-job, and locks
a job's row so its renewal hangs.
- **The table is the queue and memory is a cache.** What a runner holds --
its running set, its wake-ups -- is rebuilt from the table, a notification
is a hint polling makes good, and a runner killed loses nothing but its
leases' remaining time. Symptom: a job that runs only if a notification
arrived; the suite kills the listener's connection, and a runner without
`stop`.
- **A queue's global settings live in its row; a runner's concurrency in
its code.** One number every runner obeys has one place, where two
deploys cannot disagree on it, and what one machine carries is that
machine's. Symptom: a pause or a limit read from `Runner.create`'s
arguments; the suite pauses a queue and finds no runner claims from it,
one made after the pause among them.
- **A limit holds across runners, counted under the queue's lock.** A
limited queue's claim takes the queue's advisory lock and counts what
runs and what began in the rate's period before it claims, since a limit
two runners can each half-obey is no limit. Symptom: the suite runs two
runners on a queue limited to two and sees a third run.
- **A tick is enqueued once, by the transaction that moves the schedule
past it.** The update of `next_at` is guarded by the value the runner
read, so of every runner holding a schedule one enqueues each tick, with
no leader to elect or fail over. Symptom: two jobs of one schedule for
one tick; the suite holds one schedule in three runners and counts.
- **Every instant is the caller's `now`.** No statement reads the
database's clock and no wait reads the wall's. Symptom: a test that
sleeps for a backoff.
- **A job's payload has no log level.** No line, at any level, carries
it, and a raise's recorded reason is its constructor. Symptom: a secret
in a log at `debug`; the suite enqueues one and searches every line.
- **A sensitive kind's payload reaches no answer.** A kind made
`~sensitive` keeps it from every answer, the dashboard's among them: each
read returns NULL for it in SQL, so no caller of spool's can show it,
and the dashboard shows it hidden. Symptom: the secret in an answer;
`test_spool` enqueues one and searches every read, and `test_dashboard`
every route's answer, the live stream's included.
- **The dashboard writes only through spool's public functions.** It
cancels, retries, deletes, pauses and runs now by `Spool.cancel` and its
siblings, and reads by `Spool.jobs` and theirs, so whatever it can do,
code can, under the same checks. Symptom: `spool_jobs`, or any table or
SQL, named in `dashboard/`.
- **The dashboard has no route without the application's check.** Every
route of `Spool_dashboard.routes` asks the application's `may`, through
`Spindle.Access.require`, for its own action before it reads or does
anything, and there is no default, since the dashboard cancels and
deletes and an open default is how it ends up public; who the operator
is and what they may do stay the application's, so the dashboard keeps
no users of its own. Symptom: a route answering a request `may` denies,
or one `Spindle.Access.unguarded` lists; `test_dashboard` asks every
route the app lists as a stranger and as a viewer, and fails on one it
does not ask.
- **A live stream ends, and is asked for again.** It is one request, asked
once, so it lasts `stream_for` and the page opens the next, since access
the application takes away must reach a page left open. Symptom: a
stream still sending after its time; `test_dashboard` times one.
- **The committed bundle is the build of its source.** `dashboard/assets/`
is what `make dashboard` builds from `dashboard/web/`, committed so opam
installs the package with OCaml alone, and never edited by hand. Symptom:
`make lint`, which builds it again, finds a file that differs, or one
more or fewer.
- **`agents/` is generated, never edited.** starlight-hypha-agents writes
it from the documentation's pages and its options in
`docs/astro.config.mjs`: a page changed is `make docs` run in the same
commit. Why: a user's agent reads what the release installs, offline,
and a page it reads must be the release's. Test: the docs workflow builds
the pages on every pull request and fails if `agents/` then differs.
- **The queue names no driver.** It runs over `rowtype-postgres`, which
hands it a listener as it hands it a pool, so a second backend is a
second adapter. Symptom: `postgres-eio` named in `src/` or in its
`dune`.
- **`invalid_arg` is a constant's alone**: `Kind.make`'s, for a name, a
priority, attempts or a timeout out of range, or a payload that is not an
object; `Schedule.make`'s, for an expression or a zone that can never
run; and the dashboard page's entry, built without the element it draws
in -- each a constant written in source or a build. Symptom: `make
lint`'s litany reports `restricted-dependency` outside `src/spool.ml`
and `dashboard/web/src/main.ml`, by spool's own forbid among Hypha's.
- **The `litany` file's two `per-path`s are the page's client and its
printer**: `dashboard/web/printer/`, a build-time program that prints the
client and its errors, and the client, `dashboard/web/src/api.ml`, which
Spindle's printer writes as the build runs, so its code is Spindle's to
hold.
is in none of them**, so a long job holds no connection. `test_spool`
runs more jobs at once than the pool has connections.
- **A finish counts only under its lease, and a lease is renewed or its
job stopped**, by a watchdog that asks the database nothing, since the
renewal is what may hang. The suite takes a lease mid-job, and hangs a
renewal.
- **The table is the queue and memory is a cache**: a notification is a
hint the poll makes good, and a runner killed loses only its leases'
time. The suite kills the listener, and a runner without `stop`.
- **A queue's settings live in its row, a runner's concurrency in its
code.** The suite pauses a queue and no runner claims from it.
- **A limit holds across runners**, counted under the queue's advisory
lock. The suite runs two runners on a queue limited to two.
- **A tick is enqueued once**, by the update of `next_at` guarded by the
value read. The suite holds one schedule in three runners.
- **A job waited for is kept while anything waits for it, and read `for
share`**, so a wait never guesses how a job gone ended, nor misses one
ending as it is written. The suite ends a job as a wait on it is written.
- **A sensitive kind's payload reaches no answer**: every read returns
NULL for it in SQL. `test_spool` and `test_dashboard` search every
answer.
- **The dashboard acts only through `Spool`'s public functions, and every
route asks the application's `may` first**, with no default. Symptom:
SQL or a table named in `dashboard/`; `test_dashboard` asks every route
as a stranger and as a viewer.
- **Where Hypha's rules are relaxed**: `invalid_arg` only in
`src/spool.ml` (`Kind.make`, `Schedule.make`) and
`dashboard/web/src/main.ml`, each for a constant written in source; and
the `litany` file's two `per-path`s, `dashboard/web/printer/`, a
build-time program, and `dashboard/web/src/api.ml`, which Spindle's
printer writes.
31 changes: 31 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# Changes

## 0.8.0 (2026-12-04)

- Breaking: a job waited for is kept while anything waits for it, so a
wait never counts a job gone as completed. `enqueue_after` refuses an id
the table does not hold with `` `Unknown_jobs ``, where the waiting job
once ran at once; `delete` answers `false` for a job something waits for;
pruning and a kind that keeps none keep its row. A migration adds the
reference, dropping waits on jobs already gone.
- A job enqueued to wait for one that ended as it was enqueued learns how
it ended: before, it could stay blocked for good.
- `cancel` cancels a blocked job, and what waits on it; before, it did
nothing and answered `false`.
- `Spool.handle ~completed ~failed`: what the application records of how a
job ended, written in the transaction that ends it, so the job and the
record commit together or not at all (#6). `~completed` is given the
work's answer under the job's lease; `~failed` is given how it ended --
`Spool.Ending.t`, `Exhausted`, `Discarded`, `Expired` or `Cancelled` --
once, whichever way it failed and in whichever process.
- Breaking: a job that fails is `Failing` until a runner runs its kind's
`~failed` and settles it as `Discarded` or `Cancelled`; one whose
callback fails stays failing and is tried again. `info.ending` says how
it ended. `cancel` makes a job failing, not cancelled; `retry` takes a
settled job alone; `delete` takes a settled job alone, where it once
took a waiting one; `drain` answers each step, `Failing` among them; and
`counts ~finished:false` counts the failing. A migration adds the state
and the `ending` column, filling it in for jobs already ended.
- `enqueue_after ~until:All_ended`: a job that runs once every job it
waits for has ended, however -- a batch's last step -- after each failed
one's `~failed` has committed.
- The dashboard shows the failing state, its count, and how a job ended.

## 0.7.2 (2026-12-03)

- Every package installs the documentation for agents, `llms.txt` and every
Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ REFERENCE = spool spool-dashboard odoc.support
LOCAL = --ignore-constraints-on=deps-of-spool

.DEFAULT_GOAL := help
.PHONY: help setup build test lint fmt dashboard dashboard-check docs docs-serve db db-down release
.PHONY: help setup build test lint fmt dashboard dashboard-check dashboard-serve docs docs-serve db db-down release

help: ## list targets
@grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| awk 'BEGIN {FS = ":.*?## "}; {printf " %-10s %s\n", $$1, $$2}'
| awk 'BEGIN {FS = ":.*?## "}; {printf " %-16s %s\n", $$1, $$2}'

setup: ## create the switch and install dependencies
@podman compose version >/dev/null 2>&1 || echo "The tests need podman compose."
Expand Down Expand Up @@ -62,6 +62,12 @@ dashboard-check: dashboard
|| { git status --short -- dashboard/assets; \
echo "dashboard/assets is not the build of dashboard/web: run make dashboard and commit it."; exit 1; }

# The dashboard's example on the test database, spool's tables migrated
# into it first, as an application's deploy migrates them.
dashboard-serve: db ## serve the dashboard's example at localhost:4400/spool
$(OPAM) rowtype-migrate up --url $(PG_TEST_URL) --source spool=migrations --dir examples/migrations
DATABASE_URL=$(PG_TEST_URL) $(OPAM) dune exec examples/dashboard.exe

docs/node_modules: docs/package.json docs/bun.lock
cd docs && bun install --frozen-lockfile
@touch docs/node_modules
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ Transactional, reliable and testable. Eio-native and multicore-ready.
- **In your transaction**: a job is a row written with the work that made it owed, so a rollback leaves no job.
- **Several processes, one queue**: claims that never wait on each other, leases a heartbeat renews, and a lapsed one rescued.
- **Retries with backoff**: a handler retries, retries after what an upstream said, or discards.
- **Your record, with its end**: what you record of a job's completion or failure commits in the transaction that ends it, however it ended.
- **Asked twice, one job**: a unique key folds a second request into the job already waiting.
- **Scheduled and prioritised**: at an instant, after a span, until an expiry, at a priority from 0 to 9.
- **Tests that never sleep**: `Runner.drain` runs every job due at the test's own clock.
- **A live dashboard**: `spool-dashboard`, routes of your own Spindle server, open to whom you say and for what, to watch the queue, cancel, retry or delete a job or a selection, pause a queue and set its limits.
- **Live dashboard**: watch and steer the queue from your Spindle app.

## Install

Expand All @@ -28,8 +29,11 @@ opam repo add open-opam https://github.com/open-opam/opam-repository.git
opam install spool rowtype-migrate
```

spool reads no clock and opens no connection of its own: a program that
runs jobs links its pool, its event loop and its clock beside it.

```lisp
(libraries spool rowtype-postgres eio_main)
(libraries spool rowtype-postgres eio_main ptime.clock.os)
```

spool's table is made by its own migrations, applied beside yours before
Expand Down Expand Up @@ -82,6 +86,14 @@ The runner claims the job once the transaction commits, under a lease, and
runs its handler outside every transaction; `Spool.Runner.start` starts it
and `Spool.Runner.stop` stops it.

`spool-dashboard` shows the queue live, and steers it, from a page of your
own Spindle server: [the dashboard](https://hyphatech.github.io/spool/guide/dashboard/).

<picture>
<source media="(prefers-color-scheme: dark)" srcset="docs/src/assets/dashboard-dark.png">
<img src="docs/src/assets/dashboard-light.png" alt="The dashboard at /spool: jobs by state, queues, a schedule, runners, a chart of the last hour and the latest jobs">
</picture>

## With an agent

Every package of spool installs its documentation for an agent with it, at
Expand Down
2 changes: 1 addition & 1 deletion agents/llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [The runner](pages/guide/runner.md)
- [In a Spindle application](pages/guide/spindle.md)
- [Operating a queue](pages/guide/operating.md)
- [The dashboard](pages/guide/dashboard.md)
- [The dashboard](pages/guide/dashboard.md): Watch and steer the queue, live, from a page of your own application.
- [Limiting a queue](pages/guide/limiting.md)
- [Recurring jobs](pages/guide/schedules.md)
- [Workflows](pages/guide/workflows.md)
Expand Down
Loading