spec: replicate before start (design note for #1290)
Round 2 direction after review: protect the WAL before anything that can migrate starts, decide continuity from a clean record, and take a full snapshot whenever continuity is not proven.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+80
−0
docs/superpowers/specs/2026-11-19-replicate-before-start.md+80 −0
diff --git a/docs/superpowers/specs/2026-11-19-replicate-before-start.md b/docs/superpowers/specs/2026-11-19-replicate-before-start.md| new file mode 100644 |
| index 000000000..56a36c09f |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-11-19-replicate-before-start.md |
| @@ -0,0 +1,80 @@ |
| +# Replicate before start |
| + |
| +Discussion #1290. Round 2 of 3, after Astra's round-1 rework. |
| + |
| +## The invariant |
| + |
| +A replica either continues, without a gap, the WAL its last clean close |
| +left, or it starts from a full snapshot of the live file. A wake must not |
| +serve until one of the two is true. Every write must reach the replica, or |
| +be included in a snapshot taken before the wake serves. |
| + |
| +Two mechanisms carry it, both in `internal/lease`: |
| + |
| +1. **The clean record.** A close whose final sync and store close both |
| + succeed writes `carlos-clean.json` in the litestream meta directory. It |
| + holds the newest local L0 TXID and the size and mtime of the database |
| + and its WAL. Starting replication (`ProtectStart`, `Replicate` or |
| + `Checkpoint`) deletes the record first and fsyncs the directory. The |
| + files count as continuous only if the record is there and matches them |
| + exactly. A crash, a lost meta directory, a restore, a process that wrote |
| + while nothing replicated, or a repair that failed all leave no matching |
| + record, so the next start writes a full snapshot. That covers Astra's |
| + High 1 (a retry still repairs) and High 2 (missing local metadata). |
| +2. **The start pin.** `ProtectStart`, called before anything that can |
| + migrate runs, holds a SQLite read transaction on a continuous database. |
| + While that reader is held, SQLite can restart the WAL at most once and |
| + cannot backfill any frame written after the pin, so every write the |
| + process makes stays in the WAL until litestream reads it. `Replicate` |
| + takes the pin over. It releases the pin only after litestream's own init |
| + has taken its read lock (a forced, bounded local sync). If the files are |
| + not continuous, nothing is pinned and `Replicate` snapshots instead. |
| + |
| +## Wake paths |
| + |
| +- **Ordinary wake (`doWakeOnce`).** `ProtectStart` runs after restore and |
| + identity, before the seed (step 3) and the spawn (step 4). Step 4 |
| + includes the rootfs release commands (`spawnPlanned`, the hold's pinned |
| + start, `releaseInWake`), so they run pinned. A hold swap's start is a |
| + pinned wake. An overlap swap starts its successor while the incumbent's |
| + replicator runs. |
| +- **Restore.** A restored or replaced file never matches the record, so the |
| + next `Replicate` writes a full snapshot. |
| +- **HA leases.** Same `Replicate`, with no exemption. The HA finalization |
| + writes the record only when its sync and close succeed. |
| +- **Moves (`Held.Checkpoint`).** Same check. Not continuous means a full |
| + snapshot before the checkpoint's receipt. A successful checkpoint writes |
| + the record. |
| +- **Claim wake (`doClaimWake`).** Claims are shelved and host writes are off |
| + on every box, so it is not reordered. It still cannot serve a gap: with no |
| + pin, `Replicate` decides continuity itself, and a process that wrote first |
| + fails the match and gets a full snapshot before `Publish`. |
| +- **First start (no database file).** The process starts first, and |
| + `Replicate` writes a full snapshot before the wake serves. No bootstrap |
| + file: creating the database ahead of the app would fix `page_size`, |
| + `auto_vacuum` and encoding before the app chooses them, and break the |
| + "file absent means fresh" rule that seeds and frameworks rely on. A |
| + database that is minutes old is cheap to snapshot. With nothing earlier |
| + in the replica, there is no chain to continue wrongly. |
| + |
| +## Failure behaviour |
| + |
| +`ProtectStart` failing (pin or record I/O) fails the wake before anything |
| +starts. `Replicate`'s init is bounded (`firstSyncTimeout`, 30 s). If it |
| +fails or times out, `Replicate` fails, and the wake goes through |
| +`cleanupFailedWake` (process stopped, lease back). That is High 3. The |
| +person sees the wake fail with "replicate <host>: ..." in `carlos logs |
| +--stream platform`, and the edge answers as for any failed wake. A pinned |
| +wake adds one local sync (milliseconds, no S3 call). A snapshot wake also |
| +uploads one full snapshot, bounded by `gapSnapshotTimeout` (2 min). Only the |
| +first wake after an upgrade, a restore, a crash or a failed repair pays it. |
| +A slow or failing S3 upload in a continuous wake is the monitor's to retry, |
| +as before: the frames are already captured locally under litestream's lock. |
| + |
| +## A replica that is already broken |
| + |
| +No release before this one ever wrote a clean record. So the first |
| +`Replicate` of every database after the upgrade writes a full snapshot of |
| +the live file, and the newest restore then starts from it. Older poisoned |
| +L1-L3 files remain only on point-in-time paths before that snapshot, and |
| +age out with snapshot retention (7 days). |