Architecture
Storage layer
Chat uses a two-layer storage model:
| Layer | Role | Technology |
|---|---|---|
| Durable Objects ring | Real-time source of truth | RocksDB WAL + in-memory state |
| Postgres | Search replica, API reads | Standard relational DB |
Postgres is never the primary write target. It is updated asynchronously by DurableSync after the WAL write succeeds.
JSON:API
Threads, participants, messages, bots, and reactions are also a REST resource
under /api/chat (media type application/vnd.api+json, auth X-App-Key +
X-App-Secret). This is the read/write surface for your backend — the
server SDK wraps it.
Thread — GET/POST /threads, GET/PATCH/DELETE /threads/:id,
plus PATCH /threads/:id/assign-bot, /handoff, /mark-pending,
/activate, /resolve, /reopen, /archive. Status changes go only through
these routes: the generic PATCH /threads/:id does not accept status or
archived_at, and a transition the thread's current state does not allow
(for example /activate on a resolved thread, or any change to an archived
thread) is rejected with a 4xx.
Message — GET/POST /threads/:tid/messages, GET/DELETE
/threads/:tid/messages/:id, PATCH .../messages/:id/edit and /delete,
GET .../messages/:id/replies.
Participant — GET/POST /threads/:tid/participants, PATCH
.../participants/:id/mark-read and /role, DELETE .../participants/:id.
Reaction — GET/POST /threads/:tid/messages/:mid/reactions, DELETE
.../reactions/:id.
Bot — GET/POST /bots, GET/PATCH/DELETE /bots/:id, PATCH
/bots/:id/rotate-secret.
Message IDs are client-minted strings — you pass id on POST. A bot's
webhook_secret is returned exactly once, in the create / rotate response's
meta.
A live OpenAPI spec is served at GET /api/chat/open_api.
WAL write path
Token streaming
LLM agents push tokens at up to 1,500/sec per run. A rollup buffer keeps this from saturating RocksDB:
Each flush is a transaction that sets stream:{runId} to the text accumulated so far, and only while the run is live (a flush after the run ended writes nothing). That is one log entry holding the run's text, not the thread; the full state snapshot is rewritten only once every 32 log ids, so a 25-flush/sec run does not serialise the whole thread on every flush. Subscribers receive a batch whose stream:{runId} change carries only the new span as append. On crash + rehydrate, the snapshot plus the log entries after it rebuild the stream slot exactly.
Run compaction
Every run ending (run:end, cancel, sweeper timeout) folds its stream into one message:
The message is written as an item-only append log entry — the transaction never rewrites the whole messages list, so its cost does not grow with the thread.
Generational archival
When load_older hits a stub it fetches the archive transparently — the client receives a normal page.
Supervision tree
TokenBuffer processes are :temporary — a crash during streaming is safe. flush_and_stop falls back to the last committed value from the Worker's in-memory state.