carlos / platform Public

census, tests: clean-tree overlays; exact HA wake count (#1290)

The replica-wake-gap overlay was proposed with go.mod rewritten by
-mod=mod, so it re-pinned the root envelope; re-proposed from a clean
tree, both overlays keep main's "." pin, unit rows and seals unchanged.
TestHAWakeRequestCounts sets the store's background passes aside by
level and counts each wake's one image upload exactly (10/10 runs).
TestCloseProvenEmptyNeedsBothHalves regains its unproven-close half:
the bucket takes the first image, then refuses, and the close is
ErrCheckpointUnproven.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 06ddc062d330c6fbf950425f2af9716ecca33e1f parent 57e32c8
4 files changed, +86 −22
  • docs/superpowers/notes/census-proposal-replica-wake-gap.md +1 −1
  • internal/activator/harelease_test.go +59 −18
  • internal/lease/release_test.go +25 −1
  • internal/provision/testdata/plan14-root-reads-replica-wake-gap.tsv +1 −2

CI failed — run details

diff --git a/docs/superpowers/notes/census-proposal-replica-wake-gap.md b/docs/superpowers/notes/census-proposal-replica-wake-gap.md
index 2ccf144e1..8ccaa5e28 100644
--- a/docs/superpowers/notes/census-proposal-replica-wake-gap.md
+++ b/docs/superpowers/notes/census-proposal-replica-wake-gap.md
@@ -7,7 +7,7 @@ Ruling `census-ruling-replica-wake-gap`. The independent reviewer approves or re
| `internal/lease/checkpoint.go` `Held.Checkpoint` | body | unchanged (1) | Replicate before start (#1290): decides continuity (beginContinuity) before the store opens, runs startProtected (bounded first sync; a full image when continuity is unproven) before its sync, and writes the clean record after a successful close. Same database path the caller passed; no new path, input or root decision; the agent runs it as carlos. |
| `internal/activator/activator.go` `Service.doWakeOnce` | sites | +registry:DBPath | Step 2c: before the seed and the spawn, asks the lease to ProtectStart(rt.DBPath), the route's own database path already used by Restore and retainDatabase; a refusal gives the lease back and starts nothing. Agent code (user carlos); no root call, no new store or file read here. |
-Envelopes re-pinned: `.`, `internal/activator`, `internal/lease`.
+Envelopes re-pinned: `internal/activator`, `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.
diff --git a/internal/activator/harelease_test.go b/internal/activator/harelease_test.go
index f1364cf43..f126e2ed0 100644
--- a/internal/activator/harelease_test.go
+++ b/internal/activator/harelease_test.go
@@ -7,7 +7,6 @@ import (
"errors"
"os"
"path/filepath"
- "slices"
"strconv"
"strings"
"sync"
@@ -847,23 +846,37 @@ func TestHAWakeRequestCounts(t *testing.T) {
s := haService(t, "shop.example.com")
lock := s.lockKey()
manifest := hostfiles.ManifestKey(s.b.filesPrefix(s.rt.Host))
- wake := func() (*actEntry, []string) {
+ l0 := "PUT " + s.b.Cfg.hibernatePrefix(s.rt.Host) + "/0000/"
+ wake := func() (*actEntry, []string, []string) {
t.Helper()
n := len(s.fx.Requests())
e, err := s.wakeForTest(context.Background())
if err != nil {
t.Fatal(err)
}
- return e, s.wakePathRequests(s.fx.Requests()[n:])
- }
- e, reqs := wake()
- // litestream's monitor writes a fresh replica's first snapshot about a
- // second after it opens (#897), and a first wake now lasts until its
- // first image is uploaded (#1290), so that snapshot can land inside
- // it. It is the store's, not the wake path's.
- reqs = slices.DeleteFunc(reqs, func(r string) bool {
- return strings.HasPrefix(r, "PUT "+s.b.Cfg.hibernatePrefix(s.rt.Host)+"/0009/")
- })
+ // The wake's full image (replicate before start, #1290): an HA
+ // release truncates the WAL, so every wake starts the replica
+ // from an image and waits for its upload. Counted on its own.
+ // The store's own background passes (compactions into levels
+ // 1-8, snapshots at 9, retention's deletes) run whenever their
+ // timers fire and race the wake, so they are set aside by
+ // level; level 0 is the wake's alone.
+ var images, rest []string
+ for _, r := range s.wakeRequests(s.fx.Requests()[n:], true) {
+ switch {
+ case strings.HasPrefix(r, l0):
+ images = append(images, r)
+ case s.storeMonitorRequest(r):
+ default:
+ rest = append(rest, r)
+ }
+ }
+ return e, rest, images
+ }
+ e, reqs, images := wake()
+ if len(images) != 1 {
+ t.Fatalf("a first wake uploaded %v, want exactly one image", images)
+ }
// A first wake: the lock's three, then the launch's publish of its
// minted secrets (Task 11): the sealed env, the sealed cutover marker
// (durability 3a) and the wake state objects, and the first manifest,
@@ -875,13 +888,13 @@ func TestHAWakeRequestCounts(t *testing.T) {
}
writeFiles(t, s.b, s.rt, map[string]string{"a.txt": "app"})
s.stopForTest(e)
- e, reqs = wake()
- if want := []string{"GET " + lock, "PUT " + lock, "GET " + s.headKey()}; restoreCalls(s.b) != 0 || strings.Join(reqs, ",") != strings.Join(want, ",") {
- t.Fatalf("a kept wake made %v (%d restores), want %v", reqs, restoreCalls(s.b), want)
+ e, reqs, images = wake()
+ if want := []string{"GET " + lock, "PUT " + lock, "GET " + s.headKey()}; restoreCalls(s.b) != 0 || strings.Join(reqs, ",") != strings.Join(want, ",") || len(images) != 1 {
+ t.Fatalf("a kept wake made %v and uploaded %v (%d restores), want %v and exactly one image", reqs, images, restoreCalls(s.b), want)
}
s.stopForTest(e)
writeFiles(t, s.b, s.rt, map[string]string{"a.txt": "changed after the release"}) // the keep proof fails
- e, reqs = wake()
+ e, reqs, _ = wake()
manifestReads := 0
for _, r := range reqs {
if r == "GET "+manifest {
@@ -906,6 +919,12 @@ func TestHAWakeRequestCounts(t *testing.T) {
// 2026-10-27). They are replication's requests, not the wake's, and
// counting them made the wake's budget depend on the scheduler.
func (s *haSvc) wakePathRequests(reqs []string) []string {
+ return s.wakeRequests(reqs, false)
+}
+
+// wakeRequests is wakePathRequests, keeping litestream's level 0-8 PUTs
+// when data is set.
+func (s *haSvc) wakeRequests(reqs []string, data bool) []string {
prefix := s.b.Cfg.hibernatePrefix(s.rt.Host) + "/"
var out []string
for _, r := range reqs {
@@ -918,10 +937,10 @@ func (s *haSvc) wakePathRequests(reqs []string) []string {
// compactions the store, ready earlier, finishes inside the wake.
// They were the same requests a moment later. Snapshots (level 9)
// still count.
- if rel, ok := strings.CutPrefix(r, "PUT "+prefix); ok && replicaDataFile(rel) {
+ if rel, ok := strings.CutPrefix(r, "PUT "+prefix); ok && !data && replicaDataFile(rel) {
continue
}
- if rel, ok := strings.CutPrefix(r, "PUT "+prefix+"g-"); ok {
+ if rel, ok := strings.CutPrefix(r, "PUT "+prefix+"g-"); ok && !data {
if _, file, ok := strings.Cut(rel, "/"); ok && replicaDataFile(file) {
continue
}
@@ -938,6 +957,28 @@ func (s *haSvc) wakePathRequests(reqs []string) []string {
return out
}
+// storeMonitorRequest reports whether r is the replica store's own
+// background work on this host's root prefix: a read or write of a level
+// 1-9 file (compaction and snapshot passes), or any delete of a level file
+// (retention).
+func (s *haSvc) storeMonitorRequest(r string) bool {
+ method, key, ok := strings.Cut(r, " ")
+ if !ok {
+ return false
+ }
+ rel, ok := strings.CutPrefix(key, s.b.Cfg.hibernatePrefix(s.rt.Host)+"/")
+ if !ok || len(rel) <= 5 || !isReplicaLevelDir(rel[:5]) || !strings.HasSuffix(rel, ".ltx") {
+ return false
+ }
+ switch method {
+ case "DELETE":
+ return true
+ case "GET", "PUT":
+ return rel[:4] != "0000"
+ }
+ return false
+}
+
// replicaDataFile reports whether rel is a litestream file of levels 0-8
// ("NNNN/<min>-<max>.ltx"), not a snapshot.
func replicaDataFile(rel string) bool {
diff --git a/internal/lease/release_test.go b/internal/lease/release_test.go
index 95a508f81..6e4abab78 100644
--- a/internal/lease/release_test.go
+++ b/internal/lease/release_test.go
@@ -584,9 +584,33 @@ func TestCloseProvenReleasesANeverCreatedDatabase(t *testing.T) {
func TestCloseProvenEmptyNeedsBothHalves(t *testing.T) {
// The cleanup's abandonment tries its failing final sync once.
t.Cleanup(lease.SetShutdownSyncTimeoutForTest(0))
- t.Run("database exists, bucket empty", func(t *testing.T) {
+ t.Run("database exists, bucket refuses its writes", func(t *testing.T) {
fx := leasetest.NewLockFixture(t)
prefix := "instances/unsynced.example.com"
+ h, err := lease.Acquire(ctx, fx.Bucket, prefix, "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ path := tempDB(t)
+ // The bucket takes the start's first image (replicate before
+ // start, #1290), then refuses: the write after it cannot sync.
+ r, err := h.Replicate(ctx, path, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { r.Abandon(ctx) })
+ t.Cleanup(fx.FailReplicaPuts(prefix))
+ addRows(t, path, 3)
+ if err := r.CloseProven(ctx); !errors.Is(err, lease.ErrCheckpointUnproven) {
+ t.Fatalf("CloseProven over an existing database that cannot sync = %v, want ErrCheckpointUnproven", err)
+ }
+ if fx.LockBody(prefix) == nil {
+ t.Fatal("the lease was released over an unproven checkpoint")
+ }
+ })
+ t.Run("database exists, bucket refuses the first image", func(t *testing.T) {
+ fx := leasetest.NewLockFixture(t)
+ prefix := "instances/unstarted.example.com"
t.Cleanup(fx.FailReplicaPuts(prefix))
h, err := lease.Acquire(ctx, fx.Bucket, prefix, "")
if err != nil {
diff --git a/internal/provision/testdata/plan14-root-reads-replica-wake-gap.tsv b/internal/provision/testdata/plan14-root-reads-replica-wake-gap.tsv
index 99005fef7..34330e308 100644
--- a/internal/provision/testdata/plan14-root-reads-replica-wake-gap.tsv
+++ b/internal/provision/testdata/plan14-root-reads-replica-wake-gap.tsv
@@ -1,5 +1,5 @@
# Census overlay replica-wake-gap, written by census-propose (#369). census-ruling-replica-wake-gap, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-replica-wake-gap.md.
-# 1 body, 1 sites, 3 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
+# 1 body, 1 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
# 9 new dispositions: 1 reads re-frozen at their disposition with the claim, 8 new units frozen accepted-legacy, 0 quiet units retired while they read nothing, 0 other.
@legacy 88b9f7c7e9f19bc65301fb5b0a811710e34833e3e7bee18c41e405e7fc2dc2d1 internal/lease/checkpoint.go Held.Checkpoint legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-replica-wake-gap;basis=Replicate before start (#1290): decides continuity (beginContinuity) before the store opens, runs startProtected (bounded first sync; a full image when continuity is unproven) before its sync, and writes the clean record after a successful close. Same database path the caller passed; no new path, input or root decision; the agent runs it as carlos. file:Open@1016 337104180154a1e2f0f4d2126a4bb1b67862d8400431cc5e0b8920acced92b40
@legacy 54e3d9197838d727d894422f43a9ee9fc5c8a9a2b003150db13d466f6ae85308 internal/lease/continuity.go Protection.pin legacy accepted-legacy accepted-legacy;rev3=census-ruling-replica-wake-gap;basis=Opens a SQLite read transaction on the route's database (agent, user carlos) after stat-ing its -wal and -shm, reading PRAGMA journal_mode and sqlite_master; creates nothing, changes no mode. The path is the activator's own route database path. No root involvement. file:Open@187,file:QueryRow@343,file:QueryRow@594,file:Stat@122 c4c63fc921746b07fd3d32d6f50efc7c629b859f2501c0ebd0cf722d5c2c2461
@@ -23,6 +23,5 @@ internal/lease/continuity.go recordClean legacy accepted-legacy accepted-legacy;
internal/lease/continuity.go startProtected legacy accepted-legacy accepted-legacy;rev3=census-ruling-replica-wake-gap;basis=Stats the route database to tell a first start (no file) from an existing one; then litestream's own sync and, when continuity is unproven, a snapshot through the existing replica client. No new path or input. file:Stat@99 c56f5b768ffd6c8e7e03871fd0bb2901c13f3d575b50c4c06eed2c4a504aca46
internal/lease/lease.go Held.Replicate legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-region-ha-p1a;basis=the agent as carlos: the Replicator it returns also carries the handle's ha flag, marker base and region, so an HA replicator releases through the marker only; the store setup, its five steps and the transfer are unchanged; the one site unchanged, no read added;rev3=census-ruling-g6-fence;basis=G6 F-C Task 2, the agent as carlos: for an HA lease at sync interval 0 only, wraps db.Replica.Client in the write fence's gate (fenceGate: cancels and refuses replica calls after the event, except the finalization's) before store.Open, and starts the fence goroutine (runFence) on db.WALPath(), the instance's own -wal; the Replicator gains done and fenced channels. The one site unchanged; no root read added; store setup, its steps and the transfer unchanged.;rev3=census-ruling-replica-wake-gap;basis=Adds beginContinuity before the store opens, a Stat of dbPath (no file: reset local litestream state so the first sync is a full image), and startProtected after Open; a failure closes the store and returns the error. Same dbPath, same replica client; no root decision. file:Open@1243,file:Stat@1041 dee32cbbd3175437275b82e6940a909914ec151fd0386064b562323a2672cbd5
internal/activator/activator.go Service.doWakeOnce legacy accepted-legacy accepted-legacy;rev3=census-ruling-stateless-replicas-1;basis=Task 3a: The former doWake body, not deleted logic: refresh/admit/fence/layout checks remain. Select and verify the actual staged identity before serving Acquire; use canonical mode to call doStatelessWake. Keep SQLite acquire/restore/seed/marker/replicate only in the stateful arm, with the post-acquisition selection recheck and existing cleanup proofs. Fix 3a round 1 I1/I3a: admit config directly only for stateless and mark runtime ownership refusals. Stateful Acquire/Restore precede selection and pending publication; canonical mode inspection is separate, replan precedes settlement. Fix round 2 N2/N3: retain the original route/pin projection for post-lease declaration checks; reuse the unplanned capture so a live-link repoint cannot change the selected build. Stateful capture and changed-copy errors remain start failures after Acquire/Restore. A failed preliminary verification supplies no cached mode observation. No new file path or database authority. Final review C1/R26: unprojected stateful wakes capture and materialize the live link only after Acquire and Restore. Retain original route/generation/pin stateless evidence on the context across fallback, so missing stateless evidence cannot take stateful recovery inference. Known good stateful identity skips canonical declaration lookup. Existing pre-exec verification remains. R28 final fix 2: carry whether this wake has a plan in attempt-local context; no new read here; route, generation and pin stateless projection remains intact.;rev3=census-ruling-agent-start-demo-1;basis=Reads IsDemo and demo admission/capability state; chooses local lease retention, skips seed upload and sidecar selection. Existing stop and under-lease ordering retained.;rev3=census-ruling-region-ha-p3a;basis=P3a Task 3: refuse tenant starts under the existing service lock before main's demoWakeRefusal, and recheck before backend Acquire. Retain main's demo refusal and all lease, restore and serialization checks. Two added whole Route uses feed the in-memory admission predicate; no new filesystem or store read.;rev3=census-ruling-stateless-replicas-2;basis=P14: after verified storage-mode selection, before clearing the projected hint or acquiring a database lease, a set row still on the stateful branch returns ErrFenced. Reads the existing route FleetBoxes field only. Verified stateless wakes take their existing branch; legacy rows remain unchanged. No new file, registry query or bucket read. merge of origin/main 9b8b2c50: retain wake-slot and HA duty logic alongside the replica stateful-start refusal; no gate removed. merge of origin/main 6be08467: Keep main demo lease, local database and KeepLocal path; never equate replication-disabled demos with stateless generations. Merge-only body/read-site change. merge of origin/main 7920cccf: Retain main tenant admission and startOwnedLocked gates alongside the replica stateful-start refusal; whole Route passed to main admission checks, no new path or store read.;rev3=census-ruling-agent-start-demo-3;basis=Route claim seeds through seed restore/admission; do not fall back to empty boot or ordinary import behavior. Census read-site delta: +registry:SeedSource, +whole:Route.;rev3=census-ruling-region-ha-g2-fixes;basis=Bound recovery Acquire by its attempt deadline; existing acquire/restore/start ordering remains; no new read.;rev3=census-ruling-g6-fence;basis=G6 F-C Task 3, the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos): right after retainDatabase returns, attaches the write-fence watcher under a.mu (watchWriteFenceLocked, in memory). retainDatabase is called through a retainDB test seam that is nil in production. No read added; no root call; existing acquire, restore, publisher and cleanup order unchanged.;rev3=census-ruling-sr-followups;basis=#1061 Task 1, the agent (/opt/carlos/carlos agent, carlos-edge.service, user carlos): a refused plan now returns planRefusal(host, plan) instead of the inline fmt.Errorf, the text is unchanged, and when the planner marked the plan Installing the error also matches errBuildNotInstalled (installRefusal, in memory). Same refusal at the same point before the lease, read sites unchanged, no read, file, store, bucket or root call added;rev3=census-ruling-replica-wake-gap;basis=Step 2c: before the seed and the spawn, asks the lease to ProtectStart(rt.DBPath), the route's own database path already used by Restore and retainDatabase; a refusal gives the lease back and starts nothing. Agent code (user carlos); no root call, no new store or file read here. registry:DBPath@2071,registry:DBPath@2087,registry:DBPath@21869,registry:DBPath@2214,registry:DBPath@2225,registry:FleetBoxes@12030,registry:GoodVersion@18320,registry:Host@1900,registry:Host@1975,registry:Host@2053,registry:Host@2205,registry:Host@2539,registry:Host@4556,registry:Host@6813,registry:Host@949,registry:Replication@16947,registry:Replication@17076,registry:Replication@17095,registry:Replication@17176,registry:Replication@34070,registry:Seed@22754,registry:SeedSource@35530,registry:Socket@2100,registry:Socket@2116,registry:Socket@2239,registry:Socket@2250,registry:Stateless@10090,registry:Stateless@10376,registry:Stateless@11748,registry:Stateless@12078,registry:Stateless@20729,registry:Stateless@9633,registry:Stateless@9724,whole:Route@10162,whole:Route@10321,whole:Route@10700,whole:Route@10798,whole:Route@10889,whole:Route@11021,whole:Route@11144,whole:Route@11198,whole:Route@11470,whole:Route@11646,whole:Route@11650,whole:Route@12437,whole:Route@12712,whole:Route@13030,whole:Route@142,whole:Route@14491,whole:Route@15168,whole:Route@16049,whole:Route@16927,whole:Route@17178,whole:Route@18523,whole:Route@19114,whole:Route@19279,whole:Route@1998,whole:Route@20215,whole:Route@21011,whole:Route@21286,whole:Route@22855,whole:Route@22926,whole:Route@2434,whole:Route@2439,whole:Route@2493,whole:Route@2499,whole:Route@25420,whole:Route@2565,whole:Route@2573,whole:Route@2629,whole:Route@27278,whole:Route@27924,whole:Route@29231,whole:Route@29385,whole:Route@29884,whole:Route@29954,whole:Route@30541,whole:Route@31626,whole:Route@32057,whole:Route@32265,whole:Route@32319,whole:Route@33248,whole:Route@333,whole:Route@33433,whole:Route@34233,whole:Route@34304,whole:Route@35022,whole:Route@35071,whole:Route@35465,whole:Route@35647,whole:Route@3591,whole:Route@3685,whole:Route@36899,whole:Route@36994,whole:Route@3752,whole:Route@37835,whole:Route@4446,whole:Route@6182,whole:Route@6441,whole:Route@6703,whole:Route@6708,whole:Route@6765,whole:Route@6771,whole:Route@6840,whole:Route@6848,whole:Route@7164,whole:Route@7225,whole:Route@729,whole:Route@7323,whole:Route@8288,whole:Route@9237,whole:Route@934,whole:Route@9461,whole:Route@9525,whole:Route@9569 642bd5c0a4c68c943d51ed920045d7e29e9f968ebe93ae2d250cf616ab85a924
-@coverage . afe0b4d85c5b89b9566aa31cc05214d68466bfb68c6e7cfbec79a83b2c1fcba5
@coverage internal/activator 22a6e2f18728a2fe6d0e577d12638b742a5be22e75649b34c3ba11f1b2a3c31d
@coverage internal/lease c61536ca0105f191666955eaa21099c74afb87ccc3836078e55003ed2290b293