Commit Graph
2 Commits
Author SHA1 Message Date
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
yetone a87f051974 sessions: the index is written at most once a minute, not after every poll
A Sessions or Usage page polls every 15s, and a live session changes its
file between any two polls, so each poll's refresh saved the whole index
again: tens of MB of JSON for a long history, and encodeCache allocated
~20-30 MB a write (170 MB over 8 polls). The index only spares the next
start a read, and a parse a save hasn't written yet is read again then,
so a save now waits saveGap (1 minute) after the last one written; saves
in a row are still folded into the latest. Saved (a CLI or the app
ending) sets hurry, so every save queued is written at once without the
gap, and an unused wake-up is drained when the saves end so it cuts no
later gap short. The package's tests write saves as they come
(saveGap = 0 in TestMain).

Run:
- TestSaveWaitsOutTheGap (new): a second and third save right after the
  first are not written while the gap stands, and Saved writes them, as
  one, at once. Red with the wait removed ("2 writes with saveGap not
  yet past") and red with Saved not hurrying ("Saved waited out the
  gap", 10s).
- go test -timeout 45m ./internal/sessions ./internal/gui ./internal/usage
  ./internal/usageapi ./internal/cli ./internal/tui: ok at this change on
  205e3b8ae. gofmt clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/sessions ok.
- A/B, 8 sequential polls of the Sessions and Usage pages under a
  sandbox HOME with the real ~/.codex and ~/.claude sessions (live, so
  totals vary run to run): writeCache 169.5 -> 95.9 MB, encodeCache
  163.7 -> 92.5 MB; the run spans a few minutes, so a write a minute
  still lands in it. At the GUI's 15s polls that is one write a minute
  instead of four.
Not tried: quitting the real app and checking the index on disk; the
CLI's exit path calls Saved, as before.

Rebased on 2badacaad (provider and plugin changes): go test -count=1
./internal/sessions ./internal/gui ok at 26ed62752 (15.6s, 162.0s).
2026-10-27 10:21:10 +08:00