Cake, my web harness for pi agents, keeps its conversations on pi-durable, Earendil's durable agent runtime. pi-durable ships SQLite storage, and Cake is a server with Postgres already open for its own tables, so I wanted the durable log in the same database. chord-postgres is the package that put it there: Postgres under chord's store, which gives pi-durable a Storage in one call.
If you just want pi-durable running on Postgres, here's all you need:
Quick start
Install it with your package manager of choice:
pnpm add chord-postgres @earendil-works/chord @earendil-works/pi-durable
Then hand pi-durable a Postgres storage:
import { postgresStorage } from 'chord-postgres/pi-durable'
import { Harness } from '@earendil-works/pi-durable'
import { BACKGROUND_CONTEXT as context } from '@earendil-works/chord/context'
const storage = await postgresStorage({ connection: process.env.DATABASE_URL, owner: 'my-app' }, context)
const harness = await Harness.open(storage, { registry, models }, context)
owner is the name pi-durable's tables live under, as <owner>:<table>. That's why it's capped at 38 characters: Postgres stops at 63 bytes of identifier and pi-durable's longest name is <owner>:entries:conversation. The schema option, "chord" by default, is the Postgres schema the store lives in, and it's created when it's missing.
harness.close(context) closes the storage, and the storage closes its pool. The rest of the setup is in getting started; the package is chord-postgres on npm.
For everyone who wants to know how it works, or why it sits under chord instead of inside pi-durable, here's how I got here.
The storage I wrote myself
My first version lived inside Cake: an implementation of pi-durable's Storage interface with its own tables, its own commit, scan and retry logic. It passed pi-durable's storage conformance suite, all 33 cases. It was also code nobody else could use, and every change to the interface landed on me. pi-durable 1.6.1 turned CommitResult into a tagged union, a result whose shape tells you which outcome it describes, and I had to follow it, because a Storage implementation is written against that exact contract. That's the shitty part of this approach: you don't own the contract, you just keep paying for it.
Someone had done that work before me. @netzlabor/pi-durable-postgres by Jens Köster was the first Postgres storage for pi-durable, derived from pi-durable's own SQLite storage under MIT. It showed early that pi-durable on Postgres works at all, and its structure is easy to follow. It was written against pi-durable 1.1. pi-durable 1.2.0 removed an error type it imports, StorageRejected, so on current pi-durable (1.6 and later) it doesn't load, and 1.4.0 added a sleeping task status it has never heard of.
Nothing wrong with the package. Working at the Storage layer just means following every change to pi-durable's storage contract by hand. Others kept it alive in forks: gscode1 ported it to pi-durable 1.6.0, where it passes all 33 cases, and added fail-closed and ownership tests. tri2820 and realreus319 have forks too. I took the fail-closed latch, the refusal of a newer schema and the connection-kill test from gscode1's fork, and credited them in the README. If you pin an old pi-durable, those packages work and are a fine choice.
One layer down
I wanted the thing that keeps working as pi-durable moves, so I went one layer down. pi-durable's SQLite storage is chord's store engine, and chord's sqliteBackend(db) takes any object with a handful of async SQL methods plus transaction and close. That's how chord's Cloudflare Durable Objects backend plugs in. Implement that interface over Postgres and pi-durable gets Postgres through its own StoreStorage, with none of pi-durable's code ported.
So what I have to follow instead is chord's SQL generator, which was byte-identical between chord 1.6.0 and 1.7.0. A daily Action opens an issue on every new chord or pi-durable release with the diff-and-test checklist. chord ships no Postgres backend of its own: its docs list one as an open item, and earendil-works/pi#12507 tracks it.
I wasn't sure an async network database could satisfy a contract written for a synchronous local one. It can: the interface is already async, and chord's transaction is one Postgres transaction. So the job turned out to be translation, not a redesign.
What it takes to be Postgres
chord emits SQLite SQL, so there's a dialect layer that translates the statements chord 1.7.0 actually generates. ? placeholders become $1. INSERT OR REPLACE becomes INSERT … ON CONFLICT (pk) DO UPDATE, with the primary key read once from pg_catalog. IN (SELECT value FROM json_each(?)) becomes = ANY($n::type[]). TEXT becomes text COLLATE "C", INTEGER becomes bigint, REAL becomes double precision.
COLLATE "C" isn't cosmetic. chord compares text by code point and turns a prefix() into a range query, so a locale collation would reorder the rows and the range would select the wrong ones.
Anything the translator doesn't recognise throws UnsupportedSqlError instead of being sent to Postgres. A mistranslated statement writes the wrong row and nobody notices. A refused one fails a test.
Then there's the concurrency side. chord's contract wants the adapter to queue unrelated work while a transaction runs, because the store advances its in-memory sequence only after COMMIT. SQLite gets that from a single connection. Here it's a queue plus an advisory lock per schema, which Postgres releases at the end of the transaction, because Postgres BEGIN gives no writer lock the way BEGIN IMMEDIATE does:
BEGIN; SELECT pg_advisory_xact_lock(hashtextextended('chord-postgres:<schema>', 0));
A lost connection latches: every later call rejects with ConnectionLostError until the host closes the database and opens a new one. A commit whose connection died may or may not have committed, and a transparent reconnect could hand out an id that the lost transaction already used. In Cake the supervisor throws the storage away and opens a fresh one. There's also a small chord_postgres_adapter table that records the adapter version, and a schema newer than the code is refused with SchemaTooNewError.
Proving it
pi-durable's conformance suite through the package: 33 of 33 on pi-durable 1.7.0. CI runs the suites on Postgres 16, 17, 18 and 19.
Then the statement counts, from the statement-budget test on Postgres 18 on localhost. A chord commit of one row is 4: BEGIN and the lock, INSERT … ON CONFLICT, the chord_meta update, COMMIT. Five rows is 8. A pi-durable commit of one entry is 7–8, of five entries 18. The overhead is flat: three statements per transaction plus one per row written.
Shipping it, and where it stands
Publishing was a detour. The CI token got a 403 from npm, because granular tokens that bypass 2FA are restricted for publishing. Fucking wonderful. I published 0.1.0 from my machine with --provenance=false to get it out, then moved the repository to npm Trusted Publishing, so the release now comes from GitHub Actions over OIDC with provenance. Along the way a stray 0.0.0-stage from my local attempts had shown up on npm as latest and had to be unpublished, which is a hell of a thing to find on a package page.
Cake's own Storage implementation is gone, with its commit, scan and retry code and their tests. The durable log sits in its own schema, cake_durable, in the same database, and Cake's app tables stay in public. Existing conversation data was thrown away rather than migrated, since there were no live users.
The package pins what it was tested against, ~1.7.0 for chord and pi-durable, and assumes one writer per schema. The SQL dialect is verified for chord 1.7.0, not guaranteed for whatever comes next. That's the trade I took on purpose: a chord release that emits a new construct makes the package throw instead of writing a wrong row.
Top comments (0)