Commit Graph
5 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 fdf5e2c65f cli: claude-tui-helper ends on a signal its own way, not of magpie's handler
magpie's process-wide handler (endProbesOnSignal) re-raises SIGTERM. In
claude-tui-helper it killed the helper (exit -15) before the shim's own
handler could stop its Claude Code and remove what it wrote, so a run the
gateway aborted left them behind. The helper's command now lets go of
those signals (leaveSignals) before claudetui.Run.

Run: go test ./internal/claudetui ./internal/cli on mote (macOS) and in
WSL magpie-desktop (-tags nogui), ok; go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./..., go build on mote, ok.
Broken by hand: without leaveSignals, SIGTERM to a real helper in the
sandbox gateway exited -15 and left its temp folder; with it, exit 1 and
the folder removed (termimg.py).
Not tried: SIGHUP from a closed terminal.
2026-10-31 05:00:39 +08:00
yetone a1118187b5 gateway: who answered a conversation is written to affinity.json behind the answer, once in a while, not on every answer
answered() (and unanswered()) called saveSticks on every request
answered: up to 512 conversations sorted, marshalled, written to a temp
file and renamed, one answer after another under sticksSaving. Now the
stick is in memory at once (stickOf reads the map first) and the file
follows within sticksFlushAfter (2s): what is answered meanwhile goes in
one write. The gateway's drain (ListenAndServe) and magpie ending on a
signal (cli/signals.go, beside FlushServed) write what is waiting, so a
restart or the magpie handed over to reads every answer. A crash loses
the last 2s, as served.json does since 4f9e8331a. In a test binary the
wait is 0 and the file is written at once, as it was (no timer outlives
a test; 8215e8275 did the same for served.json). A write waiting for
another HOME's file (a test's) is dropped, not written into this one.

Test: TestSticksWrittenBehind (2s set by the test): 200 answers write
no file, the last is known in memory, flushSticks writes all 200, and a
short wait writes by itself. Broken by hand (noteSticks calling
saveSticks always): red, "affinity.json written on a request answered".

BenchmarkAnswered (512 sticks kept, 200x, 2 runs; load average ~450):
  old (saveSticks inline)  21.3 / 25.0 ms/op  506KB/op  2140 allocs/op
  new                      2.5 / 5.6 us/op    392-434B/op  8 allocs/op

Run: go test ./internal/gateway -run '^TestSticksWrittenBehind$' -count 1:
ok. go test ./internal/gateway -run 'TestSticksWrittenBehind|Affinity|
Sealed|Stick|Strict' -count 1: ok (57.8s).
2026-10-19 15:19:31 +08:00
yetone a20d86900d provider: what answered a request is written to served.json behind it, once in a while, not on every answer
NoteServed ran on every answered request (servedCandidate): it read
served.json, merged, and wrote it through steady.WriteFile (a synced temp
file renamed over it), all under servedAt's lock, so concurrent answers
queued behind each other's fsync.

Now NoteServed only notes in memory and arms one write for
servedFlushAfter (2s) later; everything noted meanwhile goes in that one
write, merged with what the file holds (another gateway's) and pruned past
servedKeep as before. The file's format is unchanged. This process's
LastServed has a note at once (it merges memory over the file, as it
did); the CLI, a process of its own, sees it within 2s. FlushServed writes
what is pending: the gateway calls it as Serve ends, and the CLI's signal
handler before it dies of the signal. A crash loses at most the last 2s
of notes. A note made under one config path (a test's HOME) is never
written into another's: a path change drops the pending write.

Numbers (Apple M4 Pro, machine loaded by other sessions):
  BenchmarkNoteServed  before 5.10-5.33 ms/op  7.8 KB/op  82 allocs/op
                       after  0.64 us/op       352 B/op    5 allocs/op

Test: TestNoteServedWritesBehind notes 200 answers and checks the file is
untouched, LastServed has the latest at once, FlushServed writes it once
(a second flush writes nothing), merged with another gateway's entry, and
that the timer writes it by itself. Broken by hand (origin/main's
served.go with stubs for the new names): red, "written on a request
answered", and the 200 notes took 1.04s.

Run: go test ./internal/provider -run 'TestNoteServedWritesBehind|TestQuotasLastServed|TestCardsLastServed';
./internal/gateway -run TestQuotasLastServedAt; ./internal/cli -run
'TestQuotaJSONLastServedAt|Signal'; ./internal/gui -run
TestServedMovedWakesTheMenuBar: ok. The full suite run is in the last
commit of this series.
2026-10-19 15:19:31 +08:00
yetone 23e764d0e0 cli: magpie's commands move from the root into internal/cli; the root keeps only main.go
The root held 89 .go files (30 commands, 59 tests). They move as they are
to internal/cli (package cli), with git mv so history follows them. The
only code change is main() becoming cli.Main(version): the root's new
main.go keeps `var version` in package main, so -ldflags "-X
main.version=…" in the Makefile, the Dockerfile and magpie-releases, and
every workflow, work unchanged. go-winres's rsrc_windows_*.syso still land
next to package main in the root.

Tests that read testdata by a root-relative path now go up one level
(../plugin/testdata/fake, ../sessions/testdata). Five subsystem doc links
point at internal/cli/.

Run:
- go build ./..., gofmt -l, go vet on darwin, linux and windows (nogui)
- builds with tags production, dev, "production nogui"; windows
  -H windowsgui; linux CGO_ENABLED=0
- make release-cli TARGETS="darwin/arm64 linux/amd64 windows/amd64
  android/arm64" VERSION=9.9.9-tidy: all four built, binary prints
  "magpie 9.9.9-tidy"
- docker build --build-arg VERSION=7.7.7-tidy: container prints
  "magpie 7.7.7-tidy"
- go test ./internal/cli (default and nogui): 152 tests, the same count
  as the root on main
- go test ./...: all packages pass (agent, gateway, library, provider
  timed out at 10m under other sessions' load and passed rerun alone
  with -timeout 40m; none of them changed)
- the built binary in a sandbox HOME: version, help, ls, providers,
  claude, and an unknown command's error
2026-10-12 20:40:22 +08:00