FastATP

0003: Bring your own database

Problem

A repo is signed blocks plus one pointer to the newest commit, and a hosted repo also carries a firehose seq. The blocks already went through BlockStore, so an app could keep them anywhere. The pointer did not: Repo(..., head_cid=) took it from the caller, Identity wrote it to a repo_head file, the hosted example kept it in memory, and the seq was a counter in _HostedRepo. So "my repos live in my database" meant every app wrote its own head bookkeeping, nothing stopped two processes from committing on the same head (commits carry prev: null, so two writers do not fork a chain; they publish two competing current heads, and one repo has two truths until something picks one), and every restart sent every follower a FutureCursor. The first user wants its repos in its own Postgres beside the rest of its data, with one peer process now and several behind the same repos later.

Decision

The head is a compare-and-swap, not a held lock. HeadStore is three methods keyed by DID: load, swap(did, expected, new), delete. A commit reads the head, builds and signs the new commit, writes its blocks, then advances the head in one statement: from the head it built on to the new one, only if it still is that head. The database's atomicity for a single-row update decides who wins. The loser reloads the new head and retries on it (bounded); a write the winner already made byte for byte is a no-op; a create whose path now holds other bytes fails as it always did; contention beyond the retries is HeadMoved, carrying the current head. No row is held across the signature, and nothing is held through a crash.

The seq rides on the same row. A commit's seq is the one its swap assigned; every other frame takes the next through the same swap, so a number is durable before its frame goes out. The genesis is seq 0, the start of the stream. Gaps are allowed (a number allocated for a frame that never left the process); reuse never is. A restarted peer continues from the last number it allocated and followers' cursors stay valid.

Storage is one decision. Storage bundles blocks, heads and blobs; a deployment picks it once. Blocks and heads must share a database: the swap only protects a repo whose blocks every process can see. Blobs are referenced from records by CID and collected on their own, so they may live elsewhere (the built-in sqlite keeps them in a second file). A Storage serves one repo. SqliteStorage(directory) is the built-in and what Identity.bootstrap(state_dir=) uses unasked; bringing your own is a class with the same three attributes over the app's adapters, passed again on every boot the way a hardware signer is. The package ships no adapter and depends on no driver.

Garbage collection is per commit and per repo, never a sweep. A commit or import deletes only the blocks it orphaned (reachable from the head it replaced and not from the new one), so repos sharing a table and processes sharing a repo leave each other's blocks alone. collect_garbage() is the sweep, for a store the repo has to itself. A shared table keys blocks by (did, cid) so one block two repos hold by content is never one row.

The doorman's cache is for speed, never authority. TunnelIdentityResolver(lookup=) consults the builder's address table first; a hit names a caller only after the doorman confirms the named repo still holds a live capability for that DID whose key derives that address, and a miss or a failed confirmation falls back to the walk, which stays the behaviour with no lookup.

The head store is async; the block and blob stores stay synchronous. The MST walks blocks many times per commit; the head is one round-trip per commit, exactly where the commit path already awaits. A Postgres storage holds two handles to one database.

Why not the alternatives

Consequences and limits