Round 2: replicate before start (f1fe437e)
The review of 8d5eaaa6 asked for rework: a failed repair lost its evidence, missing metadata disabled the check, a timeout let the process run unprotected, and HA and Checkpoint were exempt. Round 2 drops the gap repair and changes the order instead. Design note: docs/superpowers/specs/2026-11-19-replicate-before-start.md.
The invariant. A replica either continues, without a gap, the WAL its last clean close left, or restarts from a full image of the live file. A start does not serve until one of the two holds.
- Clean record. A close whose final sync and store close succeed writes
carlos-clean.jsonin the litestream meta directory. It records the newest local L0 TXID and the size and mtime of the database and its WAL. Every start of replication deletes it first, durably. A crash, a lost meta directory, a restore, an outside write, or a repair that failed or timed out all leave no matching record, so the next start takes a full image. The decision is never inferred from what local state happens to be left. - Start pin. The ordinary wake calls
ProtectStartafter restore and before the seed, any release command and the process. If the files match the record, it holds a SQLite read transaction. While that reader is held, the WAL can restart at most once and nothing written after it can be backfilled, so every boot write stays in the WAL.Replicatetakes the pin over and drops it only once litestream's own read lock is held. IfProtectStartcannot protect the database, nothing starts. - Replicate and Checkpoint, HA included. Both force a bounded first sync (30 s). When continuity is not proven, they need a full image before returning: litestream's own first file if it holds every page, otherwise a level-9 snapshot (bounded at 2 min). Any failure fails the start, and the process is stopped by the existing failed-wake cleanup.
- First start. The process starts first, and a full image is taken before the wake serves. A bootstrap file would fix page size, auto_vacuum and encoding before the app chooses them, and would break the "no file means fresh" rule that seeds rely on.
- Already-broken replicas. Releases before this one never wrote a record. So each database's first start after the upgrade takes a full image, and its newest restore heals.
- Claim wake. Claims are off on every box, so it is not reordered. With no pin,
Replicatedecides continuity itself and takes the image beforePublish.
Behaviour change. litestream's init reads the replica's position, so a replica that cannot be reached now fails a wake. Before, the wake served and replication lagged.
Tests. Each regression test below fails on main's order:
TestReplicateAfterUnreplicatedWALRestartRestoresWholeTestGapRepairFailureIsRetriedTestReplicateWithoutLocalMetadataRestoresWholeTestGapRepairTimeoutFailsReplicateAndIsRetriedTestCheckpointAcrossGapRestoresWholeTestHAReplicateAcrossGapRestoresWhole
New-behaviour tests: TestProtectStartKeepsBootWritesWithoutSnapshot, TestProtectStartRefusesContinuityAfterOutsideWrites, TestWakeProtectsTheDatabaseBeforeAnythingStarts and TestWakeThatCannotProtectStartsNothing.
Four existing tests were adjusted for the earlier first sync: two size-identical event lists gain "protect"; the HA wake-path filter ignores level 1-8 compaction PUTs; two fence rigs ship or break the replica after Replicate. The internal/lease package passes. In internal/activator, three tests fail identically without this change (escrow burst timing, two over-long socket paths). The internal/host move and claim tests pass, and the darwin and windows builds succeed.
Census. Overlay plan14-root-reads-replica-wake-gap.tsv, card docs/superpowers/notes/census-proposal-replica-wake-gap.md. It needs the reviewer's verdict. Not merged, not deployed.