lease: one-shot checkpoint replicators start no compaction monitors
ReplicateWithLease (the moves checkpoint: sync, then Close) built its
litestream store like a wake's. Each compaction and snapshot monitor
runs one pass at store.Open, concurrently with the sync, and Close
cancels a pass still in flight. S3 (and the in-process test server) can
apply that cancelled request after Close returns. The late L1 or L9
write then lands while the same moves pass mirrors the prefix, which
fails the round ("final mirror round: 1 object(s) changed at the source
during the copy", or "the staged copy is not the replica"). It also
lands after the lease was released or tombstoned.
Seen on hosted CI in TestOfflineLandRefusesAnOlderLiveBuildOnTheTarget,
TestOfflineLandConfiguredInstanceKey and (no log) the nested seal test.
A fake that holds the first L9 PUT until the round's first copy fails
the seal 2 of 3 times; with the monitors off it passes 3 of 3.
The store now matches Held.Checkpoint's: no compaction or snapshot
monitor, no L0 retention monitor, retention off. A checkpoint writes
only its L0 sync and the lease; the next wake's replicator compacts at
its own Open. Wake replicators are unchanged (their late-write case is
under separate review).
TestCheckpointReplicatorWritesOnlyL0AndTheLease holds level listings
until the first L0 lands, so a monitor pass, if one ran, would upload;
without the change it records the 0001 and 0009 uploads.
Review: Astra (gpt-6-astra, high), one round, findings in
~/.cache/ha-flakes/narrow-review/FINDINGS.md. No P1. P2 (the upload half
of the test could pass without the fix) fixed in 5a8f9265. Astra also
notes, as existing paths outside this change: the replica monitor and the
final sync retry can still leave an L0 upload whose outcome is unknown at
Close. L0 growth: one L0 per generated TXID; unchanged checkpoints add
none; restore reads the uncovered L0 tail until a wake compacts.
Census: internal/lease envelope re-pinned (census-ruling-lease-checkpoint-no-compaction), approved by Astra on #280 at bbcaee3c; re-sealed after merging main.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Squashed from lease-checkpoint-no-compaction into main.
6 files changed,
+167
−1
docs/superpowers/notes/census-proposal-lease-checkpoint-no-compaction.md+11 −0internal/lease/checkpoint_monitors_test.go+120 −0internal/lease/export_test.go+7 −0internal/lease/lease.go+25 −1internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-lease-checkpoint-no-compaction.tsv+3 −0
CI passed — run details
diff --git a/docs/superpowers/notes/census-proposal-lease-checkpoint-no-compaction.md b/docs/superpowers/notes/census-proposal-lease-checkpoint-no-compaction.md| new file mode 100644 |
| index 000000000..2bc81e8bd |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-lease-checkpoint-no-compaction.md |
| @@ -0,0 +1,11 @@ |
| +# Census approval: lease-checkpoint-no-compaction |
| + |
| +Ruling `census-ruling-lease-checkpoint-no-compaction`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +Envelopes re-pinned: `internal/lease`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Seal: `b3d35a0bd7c0500c74efbe5e8f566adb623696d31bd9860164907e08a2e79369` → `b3d35a0bd7c0500c74efbe5e8f566adb623696d31bd9860164907e08a2e79369`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-lease-checkpoint-no-compaction.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/internal/lease/checkpoint_monitors_test.go b/internal/lease/checkpoint_monitors_test.go| new file mode 100644 |
| index 000000000..3df58da0e |
| --- /dev/null |
| +++ b/internal/lease/checkpoint_monitors_test.go |
| @@ -0,0 +1,120 @@ |
| +package lease_test |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| + "fmt" |
| + "net/http" |
| + "net/http/httptest" |
| + "path/filepath" |
| + "strings" |
| + "sync" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/lease" |
| + "github.com/carlosframework/platform/internal/lease/leasetest" |
| +) |
| + |
| +// A one-shot checkpoint (ReplicateWithLease: sync, then Close) writes its |
| +// L0 sync and the lease, and nothing else. Litestream's compaction and |
| +// snapshot monitors each run one pass at store.Open, and Close cancels a |
| +// pass still in flight; S3 can apply that request after Close returns, so |
| +// it landed while the moves pass mirrored the prefix ("final mirror round: |
| +// 1 object(s) changed at the source during the copy") and after the lease |
| +// was released. The fixture holds every level listing until the first L0 |
| +// upload has landed, so a monitor pass, if one runs, finds an L0 to |
| +// compact or snapshot and uploads it. |
| +func TestCheckpointReplicatorWritesOnlyL0AndTheLease(t *testing.T) { |
| + bucket := leasetest.NewVersionedBucket() |
| + bucket.AddBucket("data") |
| + inner, _ := leasetest.NewS3Server(bucket) |
| + var ( |
| + mu sync.Mutex |
| + puts []string |
| + l0Once sync.Once |
| + ) |
| + l0 := make(chan struct{}) |
| + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + key := strings.TrimPrefix(r.URL.Path, "/data/") |
| + if p := r.URL.Query().Get("prefix"); r.Method == http.MethodGet && strings.Contains(p, "/000") && !strings.Contains(p, "/0000/") { |
| + select { |
| + case <-l0: |
| + case <-time.After(2 * time.Second): |
| + } |
| + } |
| + inner.ServeHTTP(w, r) |
| + if r.Method == http.MethodPut { |
| + mu.Lock() |
| + puts = append(puts, key) |
| + mu.Unlock() |
| + if strings.Contains(key, "/0000/") { |
| + l0Once.Do(func() { close(l0) }) |
| + } |
| + } |
| + })) |
| + t.Cleanup(srv.Close) |
| + t.Setenv("AWS_ACCESS_KEY_ID", "test-key") |
| + t.Setenv("AWS_SECRET_ACCESS_KEY", "test-secret") |
| + t.Setenv("AWS_SESSION_TOKEN", "") |
| + t.Setenv("AWS_EC2_METADATA_DISABLED", "true") |
| + t.Setenv("AWS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) |
| + t.Setenv("AWS_SHARED_CREDENTIALS_FILE", filepath.Join(t.TempDir(), "absent")) |
| + t.Setenv("CARLOS_S3_ENDPOINT", srv.URL) |
| + |
| + dbPath := filepath.Join(t.TempDir(), "h.example.com.db") |
| + db, err := sql.Open("sqlite", dbPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, q := range []string{`PRAGMA journal_mode=WAL`, `CREATE TABLE t (id INTEGER PRIMARY KEY, body TEXT)`} { |
| + if _, err := db.Exec(q); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + for i := range 20 { |
| + if _, err := db.Exec(`INSERT INTO t (body) VALUES (?)`, fmt.Sprintf("row-%d", i)); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := db.Close(); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + ctx := context.Background() |
| + rep, err := lease.ReplicateWithLease(ctx, dbPath, "data", "instances/h.example.com", "test-region") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + // The store as built: no monitor at all. The uploads below are the |
| + // behaviour; this is the deterministic half, since a monitor's Open |
| + // pass can find the database not yet ready and retry only a second |
| + // later, after Close. |
| + if compaction, l0 := lease.StoreMonitorsForTest(rep); compaction || l0 != 0 { |
| + t.Errorf("a one-shot store starts monitors: compaction %v, L0 retention check every %s", compaction, l0) |
| + } |
| + if err := rep.SyncAndWait(ctx); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Leave the monitors' Open passes, which wait for the L0, time to run. |
| + time.Sleep(300 * time.Millisecond) |
| + if err := rep.Close(ctx); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + mu.Lock() |
| + defer mu.Unlock() |
| + sawL0 := false |
| + for _, k := range puts { |
| + switch rel := strings.TrimPrefix(k, "instances/h.example.com/"); { |
| + case rel == "lock.json": |
| + case strings.HasPrefix(rel, "0000/"): |
| + sawL0 = true |
| + default: |
| + t.Errorf("the checkpoint wrote %s; a one-shot replicator writes only L0 files and the lease", k) |
| + } |
| + } |
| + if !sawL0 { |
| + t.Fatalf("no L0 upload among %v", puts) |
| + } |
| +} |
diff --git a/internal/lease/export_test.go b/internal/lease/export_test.go| index 3afd92d03..383a9d033 100644 |
| --- a/internal/lease/export_test.go |
| +++ b/internal/lease/export_test.go |
| @@ -51,3 +51,10 @@ func SetFinalSyncHookForTest(hook func()) func() { |
| finalSyncHook = hook |
| return func() { finalSyncHook = prev } |
| } |
| + |
| +// StoreMonitorsForTest reports whether r's litestream store starts the |
| +// compaction and snapshot monitors, and its L0 retention check interval |
| +// (0: no L0 retention monitor). |
| +func StoreMonitorsForTest(r *Replicator) (compaction bool, l0RetentionCheck time.Duration) { |
| + return r.store.CompactionMonitorEnabled, r.store.L0RetentionCheckInterval |
| +} |
diff --git a/internal/lease/lease.go b/internal/lease/lease.go| index e3d9bb2bc..72e964aa2 100644 |
| --- a/internal/lease/lease.go |
| +++ b/internal/lease/lease.go |
| @@ -163,6 +163,10 @@ type Held struct { |
| // monitor is the DB monitor interval Replicate's store runs at; 0 |
| // keeps dbMonitorInterval. Guarded by mu. |
| monitor time.Duration |
| + // oneShot is a replicator that only syncs and closes (ReplicateWithLease, |
| + // the moves checkpoint): its store starts no compaction, snapshot or L0 |
| + // retention monitor. Guarded by mu. |
| + oneShot bool |
| } |
| // Acquire claims the single-writer lease at s3://bucket/prefix/lock.json |
| @@ -466,6 +470,22 @@ func (h *Held) newStore(dbPath string, syncInterval time.Duration) (*litestream. |
| store.SetRetentionEnabled(false) |
| } |
| store.L0RetentionCheckInterval = l0RetentionCheckInterval |
| + h.mu.Lock() |
| + oneShot := h.oneShot |
| + h.mu.Unlock() |
| + if oneShot { |
| + // A checkpoint writes its L0 sync and nothing else. Each monitor |
| + // runs one pass at store.Open, concurrently with the sync, and |
| + // Close cancels a pass still in flight; S3 can apply that request |
| + // after Close returns, so a late compaction or snapshot lands |
| + // while the moves pass mirrors the prefix ("final mirror round: |
| + // changed at the source"), after the lease is released. The next |
| + // wake's replicator compacts at its own Open. Held.Checkpoint |
| + // builds its store the same way. |
| + store.CompactionMonitorEnabled = false |
| + store.L0RetentionCheckInterval = 0 |
| + store.SetRetentionEnabled(false) |
| + } |
| return store, db, nil |
| } |
| @@ -504,7 +524,8 @@ type Replicator struct { |
| // ReplicateWithLease acquires the single-writer lease at |
| // s3://bucket/prefix/lock.json and, only if that succeeds, starts litestream |
| // replication of dbPath to s3://bucket/prefix at the default (stream-class) |
| -// sync interval. If another process holds the lease it fails LOUDLY with a |
| +// sync interval, for a one-shot checkpoint: sync, then Close. Its store |
| +// runs no compaction, snapshot or retention monitor (Held.oneShot). If another process holds the lease it fails LOUDLY with a |
| // *HeldError (check with errors.As) — this is the signal |
| // the activator reacts to. |
| func ReplicateWithLease(ctx context.Context, dbPath, bucket, prefix, region string) (*Replicator, error) { |
| @@ -512,6 +533,9 @@ func ReplicateWithLease(ctx context.Context, dbPath, bucket, prefix, region stri |
| if err != nil { |
| return nil, err |
| } |
| + held.mu.Lock() |
| + held.oneShot = true |
| + held.mu.Unlock() |
| r, err := held.Replicate(ctx, dbPath, 0) |
| if err != nil { |
| _ = held.Release(ctx) |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 65e5efdb8..13b555735 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -172,3 +172,4 @@ plan14-root-reads-linked-deployments-slice1.tsv 40c8459580c3e62a53637e9989f1616e |
| plan14-root-reads-stats-prune-400.tsv 75b32391a24d2c9269fc579aae68ac80b7b1d4ccf0be55f0373490e43a2a652a |
| plan14-root-reads-group-observation-carry.tsv 587f6e92c5e594b9bb6436a2d0066849454407202e311f43483bed3be4591940 |
| plan14-root-reads-activator-settle-wait.tsv b3d35a0bd7c0500c74efbe5e8f566adb623696d31bd9860164907e08a2e79369 |
| +plan14-root-reads-lease-checkpoint-no-compaction.tsv b3d35a0bd7c0500c74efbe5e8f566adb623696d31bd9860164907e08a2e79369 |
diff --git a/internal/provision/testdata/plan14-root-reads-lease-checkpoint-no-compaction.tsv b/internal/provision/testdata/plan14-root-reads-lease-checkpoint-no-compaction.tsv| new file mode 100644 |
| index 000000000..3bf56169b |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-lease-checkpoint-no-compaction.tsv |
| @@ -0,0 +1,3 @@ |
| +# Census overlay lease-checkpoint-no-compaction, written by census-propose (#369). census-ruling-lease-checkpoint-no-compaction, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-lease-checkpoint-no-compaction.md. |
| +# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +@coverage internal/lease f22bf6126662281466b81fbbaf98eaeb86c0e55796fef330e1537f9d2f5bc596 |