carlos / platform Public

Flagship replica stopped restoring (equipo-staging, 11-26) open

What happened. On the flagship (console.carloku.com), app equipo-staging (account oficina) has a replica that no longer restores, while the live app serves normally with no database errors in its logs.

  • carlos db query / db pull on the latest copy (2026-11-26 09:48-09:52Z, every attempt): decode database: decode header: EOF.
  • Point-in-time reads at 09:19:00Z or later fail the integrity check (out-of-order rows, pages referenced twice, btreeInitPage error 11).
  • Copies up to 09:18:10Z (transaction 193468) restore clean. The break happened between 09:18:10 and 09:19:00Z, during an Equipo staging suite run; an instance restart at 09:17:48Z left the copy clean.
  • Same app family, production equipo (account oficina), 2026-11-19 ~14:10Z: carlos db query failed with "restored copy failed its check: database disk image is malformed" (it later read clean). Production's copy restores clean today (09:54Z, transaction 201045).

Why it matters. The replica is the backup and the source for db query, db pull, --at reads and restores. A copy that silently stops restoring while the live database is fine means a tenant could lose the ability to restore without knowing. Two cases in a week on one account.

Ask. Root cause in the replication/restore path, a check that alerts when an app's latest copy stops restoring, and a way to start a fresh generation for an affected app. Investigation starting now (read-only on the flagship); findings will follow here.

Findings (read-only on the flagship; nothing written, restarted or snapshotted).

Root cause: the replica lost WAL frames because CARLOS kills litestream's read lock at every replication start. litestream opens its long-running read transaction (the lock that stops anyone else restarting the WAL over frames it has not read yet) in its first Sync, with BeginTx(ctx) on that call's context (litestream 0.5.14 db.go:1132). That first Sync is ours: internal/lease/continuity.go:402-406 (firstSync, reached from lease.go:476) runs it under context.WithTimeout and cancels it on return. database/sql rolls a transaction back when its context ends, so the lock dies about 60 ms into every wake, restart or deploy, while litestream still believes it holds it. Until litestream's next own checkpoint (here 3 minutes), the app's SQLite autocheckpoint (default 1000 pages; Rastrillo sets none) can backfill and restart the WAL. Frames committed after litestream's last read (it polls every 10 s, lease.go:108) are overwritten, and litestream's own check is fooled because the new WAL is shorter than its old position. The live database has those pages (they were checkpointed into it); the replica never will.

Evidence.

  • Store, carlos-data-ie-271376211898/instances/equipo-staging.bds.oncarlos.com/ (root layout, versioned, so the deleted L0s are still readable). L0s are contiguous, one version per key, none short. I decoded every LTX header from 09:00 to 10:02: every WAL restart is litestream's own (a 1-frame L0 at offset 32 within 20 ms of the previous file) except two. 2f3c1 (09:18:50.5Z, 94 pages, offset 32, new salt) follows 2f3c0, which ended at WAL byte 4,025,272 (977 frames, just under 1000), and the same thing happens again at 2f3c7 (09:19:50.5Z). litestream's first own checkpoint after the 09:17:50 wake is 09:20:50.
  • Rebuilt the replica locally as it stood at 09:52: restores to 2f3c0 (193472) pass the integrity check, 2f3c1 (193473) is the first that fails (Tree 249 page 12310: btreeInitPage() returns error code 11, the same errors you saw). Page 249 in 2f3c1 points at pages 12306-12323 and 12368, which are in no LTX after 2f3bc: they were written in the lost frames.
  • Box journal (SSM, read-only grep): 09:17:49 hibernated, 09:17:50 woke ("lease 100ms, restore 29ms, protect 9ms, spawn 1.037s, replicate 63ms"). No litestream errors. /etc/litestream.yml does not list the app, so there is no second replicator.
  • Production equipo, 11-19: platform log shows a wake at 14:10:31Z (new release), and the L0 headers show a foreign WAL restart at 14:10:41.4Z (216a0, prev end 1,602,712). Same mechanism, 10 s after a wake. It later read clean because the nightly level-9 snapshot is taken from the live database.
  • Reproduced with a plain SQLite app against litestream 0.5.14: the same writes restore clean when the first Sync's context stays open, and come back broken when it is cancelled.

"decode header: EOF" is a second bug that hid the first. ltx v0.5.1's Compactor drops an input's header error (compactor.go:84-87 returns nil), so any file open or read that fails mid-restore reads as decode header: EOF. The 09:48-09:52 latest plan was about 345 files. Locally the same plan gives the integrity failure, and gives exactly decode header: EOF if file opens fail (I forced it with ulimit -n 256). Your CLI's door download most likely hit a refusal that the ltx bug turned into EOF; the exact cause is lost. The latest copy now (about 110 files) reports the integrity failure instead.

Why nothing noticed. litestream writes LTX with no checksums (HeaderFlagNoChecksum; every header here has pre/post checksum 0), so nothing on the write side can see a gap. Its check treated the restart as normal and logged nothing. The live app is healthy. The 03:00Z drill (control/verify/annie-welding.json, equipo-staging passed today at 03:2x) restores only the latest copy, and the 00:00Z level-9 snapshot heals that first. The damage only survives in the --at history, which nothing checks.

Blast radius. Every instance replicated by the activator's embedded litestream, on every box and both consoles' regions, after any wake, restart, deploy or config change, if the app writes more than about 1000 pages before litestream's next checkpoint. Migrations at deploy and test suites are the likely triggers; quiet apps are not exposed. The latest copy heals at the next daily snapshot; --at points in between stay broken until the 7-day retention removes them. A cheap read-only check: for each instances/<host>/0000/, list object versions (7 days are kept) and fetch only the first 100 bytes of each L0 (the LTX header, no tenant data). Flag any salt change whose next file is not litestream's 1-frame checkpoint L0 at offset 32 within 1 s. Each flag is a point where frames may have been lost; confirm with carlos db query --at. Cost is one ranged GET per L0, about $0.02 per busy app per week. I have the script; tell me if you want it run across the flagship.

Fix (code). Branch replica-read-lock-1443, 0d9047c5, not merged. firstSync runs the first Sync under a context that ends only if the start fails, and keeps its 30 s timeout around it. TestFirstSyncKeepsTheWALPinned (internal/lease) fails on main and passes on the branch, and go test ./internal/lease passes. Census: the internal/lease envelope is re-pinned with no unit changed; it needs the other-family review on #280. Upstream fixed the same bug in litestream 0.5.18 (acquireReadLock uses context.WithoutCancel). ltx 0.5.2, which comes with it, returns the swallowed header error. That bump also covers the case this branch leaves open: litestream checkpointing inside one of our short-context SyncAndWait calls on an awake instance. It is a follow-up because three tests depend on 0.5.14 behaviour (TestGenerationRefusedSnapshotReleasesWAL, TestRestoreToRetriesTheWholeRestoreOnce, TestRestoreToHoldsOneStreamAtATime). Note that go get also pulls pierrec/lz4 v4.1.23, which is retracted for dropping data; pin v4.1.34.

Alert/check proposal. (1) Agent tripwire: after each sync, read the header of the newest local L0. If the salt changed at offset 32 and it was not litestream's own checkpoint, write a snapshot at once (writeSnapshot already exists in continuity.go) and raise replica_wal_restart@<host>. That heals the latest copy and the alert names the window. (2) Drill: also restore the chain the newest snapshot replaced, to that snapshot's TXID, so damage the 00:00Z snapshot hides still fails a drill.

Recovery for equipo-staging (for your go; I did nothing). No command forces a snapshot today: a clean carlos restart proves continuity and writes none. The safe step needs no write. The store's next level-9 snapshot is taken from the live database and is due 2026-11-27 00:00Z (or at the first wake after that if the app is asleep). It replaces the damaged chain for latest reads. After 00:01Z, check with carlos db query --app equipo-staging --account oficina --sql "select 1", which should pass its integrity check. Until then, do not move equipo-staging or restore it from its replica. Do not run the suite straight after a restart until the fix is rolled. --at reads from 09:18:50Z up to that snapshot will stay broken until 2026-12-03.

Paul's GO, 2026-11-26: (1) ship the narrow fix (replica-read-lock-1443, 0d9047c5) after its Astra/census review, in the next CARLOS release (dev first, flagship on Paul's GO); follow up with the litestream 0.5.18 + ltx 0.5.2 bump (lz4 pinned v4.1.34). (2) run the read-only blast-radius scan across the flagship (level-0 headers only, confirm with db query --at), results here. equipo-staging recovers with the 2026-11-27 00:00Z daily snapshot; no move/restore from its replica and no suite right after a restart until then.

Blast-radius scan, flagship (read-only: listings, 72-byte header reads, restores into private temp dirs; nothing written, nothing restarted).

Scope. All 420 instance prefixes in carlos-data-{ie,uk,us,au}-271376211898. Every level-0 version the buckets still keep: 7 days of noncurrent versions, 439,680 files. I read each file's LTX header and flagged every WAL salt change. 40,488 of the salt changes are litestream's own checkpoint (the next file is one WAL frame at offset 32). 41 come after a pause of more than 60 s (a wake or an idle box, which litestream checks on its first sync). 48 are mid-session restarts that litestream did not make, on 7 instances. For each one I restored the copy at the end of that hour (carlos db query --at, or a direct read-only restore where the door can't read the instance) and ran the integrity check.

Affected: equipo-staging only (and production equipo on 11-19, now older than the kept history).

| app | restart (UTC) | copy after it | latest copy | |---|---|---|---| | equipo-staging (bds) | 11-22 16:34, 11-23 11:24, 11-24 16:27, 11-26 09:18 | damaged | damaged (heals at the 11-27 00:00Z snapshot) | | equipo-staging | 11-22 12:38, 11-23 02:45, 11-24 09:11, 18:58, 20:05 | clean (no frames lost) | | | equipo (bds) | 11-19 14:10 | older than the kept history (oldest point 11-20 01:54Z) | clean | | equipo | 11-24 09:11 | clean | | | heldwitness held-1 / held-2 (bab, HA pool, US) | 14 each, 11-17 to 11-25 | clean wherever history is kept (11-19 14:00 onward); earlier is older than the kept history | clean | | amadan (amadan.net), bunkhouse (bdd) | 11-22, 11-23 / 11-25 | clean | clean | | todo-ocaml (bfj) | 10-23 | older than the kept history | |

Each equipo-staging break lasts until the next daily level-9 snapshot, so --at reads are broken from 11-22 16:34 to 11-23 00:00, 11-23 11:24 to 11-24 00:00, 11-24 16:27 to 11-25 00:00, and 11-26 09:18 to the next snapshot. These windows age out with the 7-day retention. Every other app's latest copy is clean. The pattern matches the cause: equipo-staging restarts and deploys during suite runs that write heavily, so a wake regularly lands on a burst.

Limits. Only the flagship; dev is not scanned. A restart by the app whose first file is exactly one frame looks like litestream's own and is not counted. Restarts after a pause of more than 60 s are not checked. Frames lost before 11-19 can't be seen, because older level-0 versions have expired.

Narrow fix merged: 62d4828c (squash of replica-read-lock-1443; CI green on the tip, b0e83c25). Astra R1 NOT READY with one P1 (an abandoned firstSync could still be taken as success), fixed; R2 READY. The census (internal/lease envelope) was approved in both rounds and recorded on #280. Not released or rolled: it needs an agent roll, dev first, flagship on Paul's GO.

Follow-up branch litestream-0518-1443 (778f3992), not for merge as it stands. It bumps litestream to 0.5.18 and ltx to 0.5.2, and pins lz4 at v4.1.34. It fixes the three tests named earlier plus one in ops, which 0.5.18 broke (the snapshot reader's type, 32 concurrent restore opens, litestream re-planning a vanished file itself). CI is red (TestOnlyASettledFirstWakeBecomesTheGoodBuild, TestServeMediaWiring; not yet examined).

Astra R1 is NOT READY, with four P1s, and two of them change the plan:

  1. Format: ltx 0.5.2 writes a new page format (PageHeaderFlagSize, block lz4) that ltx 0.5.1 rejects. Once one upgraded agent writes, every 0.5.14 reader (CLIs, other boxes, a rolled-back agent) can no longer restore that replica.
  2. Untrusted size: the new page decoder allocates whatever compressed length the replica's bytes declare, with no bound.
  3. A vanished file during an unpinned wake restore can now read as "nothing replicated", which means a fresh boot.
  4. The ops replan witness no longer tests a replan.

Recommended cut, for Paul: don't take the upstream bump yet. The narrow fix closes the observed failure. The remaining case (litestream checkpointing inside one of our short-context SyncAndWait calls on an awake instance) can be closed with a one-line patch of 0.5.14's acquireReadLock (context.WithoutCancel, as upstream did). The swallowed header error can be closed the same way in ltx 0.5.1 (return it), with both modules kept on a pinned fork via replace. The 0.5.18 format change then waits for its own reader-first rollout plan.

Paul, 2026-11-26: narrow fix 62d4828c ships in the next CARLOS release (dev first, flagship on his GO). Remaining gap (litestream checkpointing inside short-timeout SyncAndWait on a running instance): keep litestream 0.5.14 and apply upstream's one-line read-lock fix plus the ltx header-error fix on a pinned fork. The 0.5.18 upgrade waits for a readers-first rollout plan.

Pinned fork merged: 3085a0bf (squash of litestream-pin-1443, per Paul's ruling). third_party holds litestream v0.5.14 and ltx v0.5.1, byte for byte from upstream except two backports:

  • litestream's acquireReadLock detaches its context (context.WithoutCancel, as in 0.5.18), which closes the case where litestream checkpoints inside one of our short-timeout SyncAndWait calls;
  • ltx's Compactor returns the failed input's header error (as in 0.5.2), so a failed read no longer shows up as decode header: EOF.

go.mod uses replace directives, the same pattern as launchctx. There is no replica format change. TestCheckpointInsideSyncAndWaitKeepsTheWALPinned and TestRestoreSurfacesTheReplicaError fail on the upstream modules and pass on the forks. third_party/vulnscan keeps govulncheck checking the upstream versions. Astra R1 NOT READY (the AWS-client guard and vulnerability coverage; both fixed); R2 had no P1, and I applied its one P2. The census (68 vendored units, 7 envelopes) is approved on #280. Needs a release and an agent roll, dev first.

62d4828c is already in v0.43.10. litestream-0518-1443 stays parked for a readers-first plan, noted on the branch. One flake recurs, unrelated to this work: the race in TestAssetE2ECoalescedWaitersAndRefusals (internal/edge).

2026-11-27 00:17Z: equipo-staging's copy reads clean again after the 00:00Z daily snapshot (select 1 ok; PRAGMA integrity_check ok, up to transaction 199592). The narrow fix is in v0.43.10 (dev); the pinned fork 3085a0bf is merged, not released. Flagship rollout of both still needs Paul's GO.

New since the last update, from #1449 (now closed as a duplicate of this). Everything here was read-only: listings, header ranges, transient restores into private temp dirs. Downloads are deleted, and nothing about row contents was read or kept.

1. The flagship still runs v0.43.8. status/boxes/annie-welding.json reports metrics.release as v0.43.8. Neither 62d4828c (v0.43.10) nor 3085a0bf2 is on the box, so equipo-staging keeps breaking.

2. 11-27 re-broke equipo-staging twice, by this same mechanism. I read the L0 headers (versions, first 100 bytes) from 11-26 23:30 to 11-27 11:30: 4,988 files and 658 of Litestream's own checkpoints. There were 10 WAL restarts Litestream didn't make. Each started at offset 32 with 65–498 frames, 10 s after the previous file, which ended at WAL byte 1.4–3.9 MB (up to about 940 frames):

  • 00:57:52–01:00:43, 7 restarts (TXIDs 199879–199896). Restoring to 199891 fails integrity; a full-page L1 (199892-200305) healed the copy by 02:00.
  • 10:22:05–10:23:05, 3 restarts (TXIDs 203786–203792), right after the 10:21 release.

The latest copy (through L2 3134c-31d13) has failed integrity_check since then. It heals at the 11-28 00:00Z snapshot, unless the next restart lands on a write burst before a fixed release is rolled.

3. Production equipo.bds.oncarlos.com measurement. I restored each kept snapshot plus its incremental chain to the next snapshot's TXID, and compared page SHA-256s with that snapshot. Validated on staging first: the method reproduces the known damage, with 47, 74 and 352 differing pages and a failed quick_check at the 11-23, 11-24 and 11-27 boundaries.

| boundary (production) | pages | differing | chain quickcheck | | --- | --- | --- | --- | | 11-21 → 11-22 | 15927 | 9 (page 1, service, `runnersessions, 6 non-b-tree pages) | ok | | 11-22 00:03 → 11-23 | 15927 | 1 (service) | ok | | 11-23 → 11-24 | 15927 | 1 (runnersessions) | ok | | 11-24 → 11-25 | 15927 | 1 (runnersessions`) | ok | | 11-25 → 11-26 | 15927 | 0 | ok | | 11-26 → 11-27 | 15936 | 0 | ok |

The single-page differences are not lost writes. They are a Litestream snapshot-labelling race. At the 11-24 and 11-25 boundaries, the snapshot labelled T holds exactly the page version found in the L0 for T+1. At 11-23 it holds a version no L0 has, which is consistent with the snapshot reading the live file mid-way through the next 10 s sync window. So production shows no evidence of lost writes from 11-22 to 11-27, with every chain quickcheck ok. The 11-21 boundary's 9 pages are unexplained; quickcheck is ok there too.

The L0-header scan of production from 11-26 00:00 onward found 17,313 files, 2,429 of Litestream's own checkpoints, and one other restart, at 11-27 02:03:25. That one is benign by its shape: the previous WAL was 103 KB, so autocheckpoint could not have fired, and the gap was 0.1 s.

4. Proposal for the drill and alert, for this issue's owner and Paul to schedule. Nothing is built; nothing like it is on main.

  • Chain continuity in the nightly drill (internal/dbverify). When a new level-9 snapshot exists, restore the previous snapshot plus the chain to the new snapshot's TXID, and compare page hashes with the new snapshot. - Use page hashes, not integrity_check. integrity_check passes on silent loss, and the page comparison catches it. - Tolerate the labelling race. For any differing page, also accept the version in the L0 files for T+1 to T+n (n about 3), or the state after the next sync window. If that rule is wrong, production reports false positives every night. - Cost. About one extra snapshot download and a day of chain per host per day; a hash compare streams, with no extra disk. Hosts with no new snapshot are skipped. - The verdict goes into control/verify/<box>.json beside the current result, as chain: ok|lost <n> pages|race-only.
  • Alert. Raise replica_chain_gap@<host> on any lost verdict, naming the window (the previous snapshot's time to the new one's). Optionally add the cheaper tripwire proposed above: after each sync, a salt change at offset 32 that isn't Litestream's 1-frame checkpoint writes a snapshot and raises replica_wal_restart@<host>. The tripwire heals within seconds and the drill proves it overnight.
  • Limit. Loss healed within the same day by a full image (as at 11-24 16:27 on staging) leaves broken --at points that the snapshot-boundary check can't see. Only the tripwire, or an hourly L1-boundary check, covers those.

5. Equipo staging release timing. The daily snapshot runs at 00:00:00Z: Truncate(24h), so UTC midnight. It needs the instance awake (the store's snapshot monitor); if asleep, it runs at the first wake after midnight. A non-continuous wake also writes one before serving.

Paul, 2026-11-27 ~18:40Z: please roll the fixed CARLOS release to the flagship. He asked for this session to do it (relayed from the Base/Equipo session on Paul's MacBook Pro, which can't message DOCS-T1 directly).

  • v0.43.10 is the newest tag and has the narrow fix 62d4828c. The pinned fork 3085a0bf isn't in any tag yet. Cut a v0.43.11 with it if it's ready, or roll v0.43.10 now, whichever you judge right.
  • Why now: annie-welding is still on v0.43.8, and equipo-staging's replica broke again after the 10:21Z release. At 18:29Z, Equipo's staging release of main 50029aba (the Assistant's waiting-cards note) failed its carlos db check (Tree 249 / 251 / 918 integrity errors), so the Equipo production upgrade is blocked again. Production's replica quick_check was ok at 18:30Z.
  • After the rollout: equipo-staging needs a fresh replica generation, or it waits for the 11-28 00:00Z snapshot. Then I'll run the staged release from docs-t1 ~/amadan.net/oficina/equipo-release-50029aba. Artifacts are already built; nothing has been deployed.

Both fixes are live on dev and the flagship: v0.43.11, rolled 2026-11-27 (Paul's GO "Cut + roll now"). Tag v0.43.11 (42cc6a9b) at main 47b2f5d8 = v0.43.10 (narrow fix 62d4828c) + the pinned litestream 0.5.14 / ltx 0.5.1 fork 3085a0bf. Also in it: 3f3dbc1f (shared record listing, #964), billing catalog files, docs. Notes: https://github.com/carlosframework/releases/releases/tag/v0.43.11

  • Dev 19:14-20:15Z: every box, both IE members and the console. Witness on wakecheck: it woke at 20:11:32Z under a 3,000-request write burst, then took 4,000 more while running (7,000 x 200). Its WAL reached 3,959 frames, well past the app's 1,000-page autocheckpoint, and the only WAL restart in its level-0 headers is litestream's own: 0 restarts litestream did not make. carlos db query: integrity_check ok on the latest copy and at a mid-burst --at; the copy holds every row.
  • Flagship 20:15-21:18Z, from v0.43.8: every box (incl. kepler and both fleet box-1s), both US members, the console, bootstrap/carlos and apt. Every move passed; 0 non-2xx on the probes; no alarms. Full record on #421.
  • equipo-staging (read-only header reads): it woke on v0.43.11 at 20:51:04Z; from then to 21:18Z, 190 level-0 files, 25 litestream-own WAL restarts, 0 others.
  • Not repaired: the copy damaged at the 11-27 10:22Z restarts still fails integrity_check until the 11-28 00:00Z daily snapshot, as noted above. Worth a carlos db query --sql "PRAGMA integrity_check" on equipo-staging after 00:00Z before the next Equipo release reads it.
  • Rollback, if ever needed: v0.43.10 (no replica format change), which brings back the running-instance gap. litestream-0518-1443 stays parked for a readers-first plan.