Files
magpie/internal/sessions/save_gap_test.go
T
yetone 6f3dfc282f sessions: a scan's index is written at its end and on SIGTERM, not a minute later
A refresh's saves waited out saveGap (1 min) after the last one written,
the scan's checkpoints and its end too: the first checkpoint was written,
the rest waited. A magpie ended in that minute (quit, updated, `web` or
`serve` stopped by SIGTERM, whose handler died without sessions.Saved())
kept a few hundred of the files it read, and the next start read every
rollout again.

- A refresh that reads scanFiles (32) files or more is a scan: its
  checkpoints wait scanSaveGap (15 s) and its last save waits for nothing.
  A poll's few changed files still wait out saveGap. Saves folded into one
  take the shortest gap asked, and a shorter one wakes a waiting save.
- The signal handler writes the index before dying, bounded by
  saveOnSignal (5 s).

Real history in a sandbox HOME (11,994 Codex rollouts, 52.5 GB, plus
Claude projects; 17,140 files), index removed, `magpie web` killed by
SIGTERM ~44 s in (right after the first /api/sessions answered):
- v0.1.2521 tree: 1,200 entries kept (1,415 on a second try). Restart:
  /api/sessions 23.63 s, 1:38 of CPU.
- this change: 17,140 kept. Restart: /api/sessions 1.30 s, 3.4 s of CPU.

Tests, on mote: TestScanSavedWithoutTheGap and
TestSignalSavesTheSessionIndex are new. Broken by hand: with the scan's
gaps left at saveGap the first fails ("a scan's last save waited out
saveGap"); with the save call taken out of the handler the second fails
("died without the session index saved"). go test ./internal/sessions/
./internal/cli/ ok. gofmt -l clean; go vet (darwin), GOOS=windows go vet,
GOOS=linux go vet -tags nogui of both packages ok.

Not run: the full suite; the GUI app's own quit and update paths (they
return to main, which already called Saved); Windows and Linux runs.

Picked onto 0c3163804 (origin/main at push); not run again there.
2026-11-01 11:34:13 +08:00

92 lines
2.5 KiB
Go

package sessions
import (
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
)
// A save right after another waits out saveGap rather than writing the
// whole index again (each Sessions poll wrote it), and Saved writes it at
// once, not after the gap.
func TestSaveWaitsOutTheGap(t *testing.T) {
setup(t)
var writes atomic.Int32
old := saveGap
saveGap = time.Hour
saveHook = func() { writes.Add(1) }
t.Cleanup(func() { saveGap, saveHook = old, nil })
saved()
saving.Lock()
saving.wrote = time.Time{}
saving.Unlock()
saveCache()
saved()
if n := writes.Load(); n != 1 {
t.Fatalf("%d writes of the first save, want 1", n)
}
saveCache()
saveCache()
time.Sleep(50 * time.Millisecond) // room for a write the gap should hold back
if n := writes.Load(); n != 1 {
t.Fatalf("%d writes with saveGap not yet past, want the first only", n)
}
done := make(chan struct{})
go func() { saved(); close(done) }()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatal("Saved waited out the gap")
}
if n := writes.Load(); n != 2 {
t.Fatalf("%d writes once Saved, want the two saves in a row folded into one", n)
}
}
// A scan's last save is written at once, not saveGap after the save before
// it: a magpie quit or updated within that minute kept none of the scan,
// and each start read 22 GB of rollouts again. A poll's few files still
// wait out the gap.
func TestScanSavedWithoutTheGap(t *testing.T) {
inZone(t, 0)
claude, _ := setup(t)
var writes atomic.Int32
old, oldScan := saveGap, scanFiles
saveGap, scanFiles = time.Hour, 8
saveHook = func() { writes.Add(1) }
t.Cleanup(func() { saved(); saveGap, scanFiles, saveHook = old, oldScan, nil })
statsAt(0, statsNow)
saved()
gapFromNow := func() {
saving.Lock()
saving.wrote = time.Now()
saving.Unlock()
writes.Store(0)
}
gapFromNow()
moreSessions(t, claude, 12)
statsAt(0, statsNow)
for end := time.Now().Add(10 * time.Second); writes.Load() == 0; {
if time.Now().After(end) {
t.Fatal("a scan's last save waited out saveGap")
}
time.Sleep(10 * time.Millisecond)
}
saved()
gapFromNow()
p := filepath.Join(claude, "projects", "-work-many", "33333333-2222-3333-4444-000000000000.jsonl")
if err := os.WriteFile(p, []byte(ccMsg("m0", "2026-09-21T10:00:00.000Z", 200, 20)), 0o644); err != nil {
t.Fatal(err)
}
statsAt(0, statsNow)
time.Sleep(100 * time.Millisecond) // room for a write the gap should hold back
if n := writes.Load(); n != 0 {
t.Fatalf("a poll's save written %d times inside saveGap", n)
}
}