pageindex: log every simulated Clear and keep the store test on the API
A Clear that straddled a Reset was dropped from the model, so a render on the new epoch wrongly admitted past it could escape I1. Clear now reads its epoch under a test-side per-scope gate that Reset holds exclusively; publishes and lookups never take it. The store test no longer reads the index's records: after a Reset of every scope it checks the store holds no page objects and every lookup misses.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/pageindex/sim_test.go+38 −36
CI cancelled — superseded by a newer push — run details
Intent
<task-notification> <task-id>ad94244154716b3a4</task-id> <tool-use-id>toolu_01DV6wTZn6hqGnwp3SyExJ8P</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/d79ef2b4-4291-4945-bd45-b3f16796c4ba/tasks/ad94244154716b3a4.output</output-file> <status>completed</status> <summary>Agent "Implement Task 3: pageindex simulation" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE. Tests only; no production code changed and no test exposed a bug. Commit: `2bdbe94a pageindex: randomized invariants and the page-key guard` Tests: `go test ./internal/pageindex -count=3 -race` passes (148.5 s). The full simulation takes about 6 s, `-short` about 1.3 s. All four mutations failed their tests, and each was reverted. Concerns: - **Speed:** `Config.FS` only reports free space, so there's no fsync-free seam. I kept the full 2000 ops anyway because fsync is fast enough on this disk. - **Attempt-id mutation:** Task 2's race test catches it every time; the simulation catches it in about 6 of 10 full runs. - **Fence mutation:** caught 5 of 5 full runs, about 4 of 5 under `-short`. - **Busy fills:** about 37% of publishes hit `ErrBusy` because two scopes share one app's limit of 4 fills. They're counted and skipped. - **Additions:** I1 also checks resets. I added an extra test, `TestPageKeyGuardFixtures`, as my reading of "this guard's own fixtures". - **Sandbox:** the Go cache is read-only inside it, so I ran go commands with the sandbox off and GOCACHE at its default. - **Race time:** each `-race` pass takes about 47 s, mostly Task 2's `TestSweepBoundsRecords`. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/edge-page-cache-1/.superpowers/sdd/2026-11-08-edge-page-cache-plan-1-home/task-3-report.md</result> <usage><subagent_tokens>139993</subagent_tokens><tool_uses>50</tool_uses><duration_ms>1120548</duration_ms></usage> </task-notification>
<task-notification> <task-id>b33vzjyuc</task-id> <tool-use-id>toolu_01RzfeNyp565Eg8zcnVpMfuu</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/d79ef2b4-4291-4945-bd45-b3f16796c4ba/tasks/b33vzjyuc.output</output-file> <status>completed</status> <summary>Background command "Run Astra review of Task 3" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 24.2 g CO₂e (3,169,722 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on edge-page-cache-1, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/pageindex/sim_test.go b/internal/pageindex/sim_test.go| index ef0a4836d..6a6290425 100644 |
| --- a/internal/pageindex/sim_test.go |
| +++ b/internal/pageindex/sim_test.go |
| @@ -124,14 +124,14 @@ type simStats struct { |
| publishes, published, refused, busy, aborted atomic.Int64 |
| lookups, hits, readGone atomic.Int64 |
| missAbsent, missInputs, missCookies atomic.Int64 |
| - clears, ambiguous, resets, optins atomic.Int64 |
| + clears, resets, optins atomic.Int64 |
| } |
| func (s *simStats) String() string { |
| - return fmt.Sprintf("publishes %d (indexed %d, refused %d, busy %d, aborted %d); lookups %d (hits %d, body gone before read %d; misses absent %d, inputs %d, cookies %d); clears %d (epoch unknown %d); resets %d; opt-in changes %d", |
| + return fmt.Sprintf("publishes %d (indexed %d, refused %d, busy %d, aborted %d); lookups %d (hits %d, body gone before read %d; misses absent %d, inputs %d, cookies %d); clears %d; resets %d; opt-in changes %d", |
| s.publishes.Load(), s.published.Load(), s.refused.Load(), s.busy.Load(), s.aborted.Load(), |
| s.lookups.Load(), s.hits.Load(), s.readGone.Load(), s.missAbsent.Load(), s.missInputs.Load(), s.missCookies.Load(), |
| - s.clears.Load(), s.ambiguous.Load(), s.resets.Load(), s.optins.Load()) |
| + s.clears.Load(), s.resets.Load(), s.optins.Load()) |
| } |
| // sim is one simulation run over a rig. |
| @@ -145,6 +145,11 @@ type sim struct { |
| pauseRnd *rand.Rand // drives afterCommit's pauses |
| optMu [len(simScopes)]sync.Mutex // one opt-in change and its check at a time per scope |
| + |
| + // epochGate keeps each scope's epoch still while a Clear reads it: Clear |
| + // holds it shared, Reset exclusively. Publishes and lookups never take |
| + // it, and clears still run concurrently with one another. |
| + epochGate [len(simScopes)]sync.RWMutex |
| } |
| // simulationSeed is -seed, or the time. A failing test prints it. |
| @@ -444,39 +449,43 @@ func randomClear(rnd *rand.Rand) Clear { |
| return c |
| } |
| -// clear runs a random Clear and logs it when it returns. Its epoch is read |
| -// with a Begin on each side; when a Reset ran in between, the epoch it ran |
| -// on is unknown and it is not logged, which only weakens the check. |
| +// clear runs a random Clear and logs it when it returns, with the epoch it |
| +// ran on. The epoch gate keeps out Resets, so a Begin on each side sees |
| +// that epoch; every Clear is logged. |
| func (sm *sim) clear(si int, rnd *rand.Rand) { |
| t, x := sm.t, sm.r.x |
| s := simScopes[si].s |
| c := randomClear(rnd) |
| sm.stats.clears.Add(1) |
| + sm.epochGate[si].RLock() |
| before := x.Begin(s, "") |
| seq := x.Clear(s, c) |
| after := x.Begin(s, "") |
| + sm.model.add(modelEntry{scope: si, epoch: before.Epoch, seq: seq, c: c}) |
| + sm.epochGate[si].RUnlock() |
| if before.Epoch != after.Epoch { |
| - sm.stats.ambiguous.Add(1) |
| - return |
| + t.Errorf("Clear(%d): epoch moved from %x to %x with Resets held off", si, before.Epoch, after.Epoch) |
| } |
| if seq <= before.Seq || seq > after.Seq { |
| t.Errorf("Clear(%d) = seq %d, outside (%d, %d] on one epoch", si, seq, before.Seq, after.Seq) |
| } |
| - sm.model.add(modelEntry{scope: si, epoch: before.Epoch, seq: seq, c: c}) |
| } |
| -// reset runs Reset and logs that the epoch seen before it has ended: by the |
| -// time it returns, this Reset or an earlier one has ended that epoch. |
| +// reset runs Reset, holding the epoch gate so no Clear is between its |
| +// Begins, and logs that the epoch it ended is over. |
| func (sm *sim) reset(si int) { |
| t, x := sm.t, sm.r.x |
| s := simScopes[si].s |
| sm.stats.resets.Add(1) |
| + sm.epochGate[si].Lock() |
| before := x.Begin(s, "") |
| x.Reset(s) |
| - if after := x.Begin(s, ""); after.Epoch == before.Epoch { |
| + after := x.Begin(s, "") |
| + sm.model.add(modelEntry{scope: si, epoch: before.Epoch, reset: true}) |
| + sm.epochGate[si].Unlock() |
| + if after.Epoch == before.Epoch { |
| t.Errorf("Reset(%d) left epoch %x", si, before.Epoch) |
| } |
| - sm.model.add(modelEntry{scope: si, epoch: before.Epoch, reset: true}) |
| } |
| // optIn marks or clears the scope's opt-in and checks OptedIn agrees. |
| @@ -517,39 +526,32 @@ func TestSimulationInvariants(t *testing.T) { |
| runSim(t, simOps(), simulationSeed(t)) |
| } |
| -// TestStoreHoldsNoUnindexedPages: after a simulation the store holds exactly |
| -// the page objects the index names, and after a Reset of every scope none. |
| +// TestStoreHoldsNoUnindexedPages: after a simulation and a Reset of every |
| +// scope, the store holds no page objects, every lookup misses and nothing is |
| +// reserved. A refused or replaced page left behind would still be held. |
| func TestStoreHoldsNoUnindexedPages(t *testing.T) { |
| sm := runSim(t, simOps(), simulationSeed(t)) |
| x, store := sm.r.x, sm.r.store |
| - named := make(map[assetcache.Key]bool) |
| - for _, s := range x.Scopes() { |
| - st := x.lookupScope(s) |
| - st.mu.Lock() |
| - for _, rec := range st.records { |
| - named[rec.phys] = true |
| - } |
| - st.mu.Unlock() |
| - } |
| - held := pageKeys(store) |
| - for _, k := range held { |
| - if !named[k] { |
| - t.Errorf("store holds page object %+v that no record names", k) |
| - } |
| - } |
| - if len(held) != len(named) { |
| - t.Errorf("store holds %d page objects, index names %d", len(held), len(named)) |
| - } |
| - |
| for _, s := range x.Scopes() { |
| x.Reset(s) |
| } |
| if keys := pageKeys(store); len(keys) != 0 { |
| t.Fatalf("store holds %d page objects after a Reset of every scope, want none: %v", len(keys), keys) |
| } |
| - if n := recordCount(x); n != 0 { |
| - t.Fatalf("index holds %d records after a Reset of every scope, want none", n) |
| + for si, ss := range simScopes { |
| + for _, host := range simHosts { |
| + for _, path := range simPaths { |
| + for _, query := range simQueries { |
| + k := Key{Host: host, Path: path, RawQuery: query} |
| + for _, inputs := range simInputs { |
| + if _, miss := x.Lookup(ss.s, k, inputs, nil); miss != MissAbsent { |
| + t.Fatalf("Lookup(%d, %+v, %q) after a Reset of every scope = %v, want MissAbsent", si, k, inputs, miss) |
| + } |
| + } |
| + } |
| + } |
| + } |
| } |
| if st := store.Stats(); st.Reserved != 0 { |
| t.Fatalf("store has %d bytes reserved after the simulation, want none", st.Reserved) |