Files
magpie/internal/filememo
yetone ed3a46d02b provider: what a request built of the catalog stays for the next request within a second, not built again for each
Hold dropped everything held when the last request let go, so one
request after another built the catalog again from every provider's
files and accounts (All, entries, the groups' entries, the settings):
a fifth to a third of a request's CPU with 30 providers. What is built
now stays until heldFor (1s) is past, as it already did for requests
that overlapped. It is built again at once when anything it was built
from may have moved:
- Changed() and a plugin change (dropHeld), as before;
- a write that only tells filememo (settings.Save, writePrivate, an
  agent's account file): held values carry filememo.Gen(), new here, and
  are dropped when it moved;
- providers.json or settings.json written by another process (the CLI's
  "magpie group add"): the first Hold of a request stats both and drops
  what is held when either changed, so the next request sees it, not a
  second later;
- the groups served or gated (GateGroups, ServeGroups): holdGate drops
  what is held on both changes (TestGroupsOnAndOff was red without it);
- another environment (a test's HOME), by a hash of os.Environ().
Other files another process writes (an agent's own sign-in file) are
seen within heldFor, as filememo already takes a file found unchanged.

Tests:
- TestHoldKeptForTheNextRequest: five requests one after another build
  once. Broken by hand (held.built = nil on the last release put back):
  red, "five requests one after another: built 5 times, want 1".
- TestHoldKeptUntilAWrite: each of filememo.Forget, providers.json and
  settings.json written behind magpie's back, and GateGroups, builds
  again once. Broken by hand, one at a time: the Gen check off, red
  "filememo told: built 0 times, want 1"; the stat off, red
  "providers.json written by another process: built 0 times, want 1";
  holdGate's dropHeld off, red "the groups gated: built 0 times, want 1".
- TestRequestBuildsProvidersOnce now wants 1, 0, then 1 after
  Changed() (a request later than 0.9s after the first may rightly
  build again on a loaded machine; the test allows it).

BenchmarkGroupOverhead (internal/gateway, 200KB Anthropic body,
stream=false, 100x, two rounds alternating; load average ~450, so the
cpu-ms/op it reports is the measure, not wall ns/op). base is 8215e8275:

  12x20 p0/m1  base  25.66 / 24.36 cpu-ms/op  16.8MB  41723-41827 allocs
               this  22.13 / 20.67 cpu-ms/op  13.4MB  33771-33969 allocs
  12x20 g4     base  30.59 / 30.68 cpu-ms/op  22.5MB  53213-53409 allocs
               this  25.91 / 25.60 cpu-ms/op  19.0MB  45213-45613 allocs
  30x60 p0/m1  base  54.95 / 53.26 cpu-ms/op  42.5MB  89466-90023 allocs
               this  30.80 / 29.27 cpu-ms/op  23.0MB  45853-46928 allocs
  30x60 g4     base  66.67 / 73.07 cpu-ms/op  68-70MB  112283-116845 allocs
               this  45.59 / 48.38 cpu-ms/op  49-52MB  68540-74905 allocs

Run: go test ./internal/provider -run 'Hold|Group|Changed|Served'
-count 1: ok (91s). go test ./internal/gateway -run
'^(TestGroupsOnAndOff|TestRequestBuildsProvidersOnce)$' -count 1: ok.
The full suite at the head of this series is in its last commit.
2026-10-19 15:19:31 +08:00
..
windows: an account list read while another magpie writes it is waited for, not taken for no accounts (Packing1 on Discord, Windows 11: 早上更新重启后之前登录的Antigravity订阅供应商和账号都没了 … 整行都没了,供应商里面antigravity直接消失了) — on Windows a file being renamed over (every write magpie makes: a temp file renamed onto it) is, for a moment of up to ~2ms, "The system cannot find the file specified" to anyone else looking at it, with MoveFileEx and with POSIX-semantics renames alike. filememo.Read gave that back as an error, readLogins as no accounts, and whatever change came next (any account's read-modify-write) wrote the empty list back: every built-in account gone, its subscription with it. Measured on Windows 11 (10.0.0.132): one process replacing a 200 KB file, another stat-ing and reading it, saw the file "not there" in a few windows each 20 s. Now steady.Stat and steady.ReadThere wait out a moment of a file not there (15 tries a millisecond apart, Windows only), and filememo looks through them, remembering a file found not there so one that really isn't costs the wait once. A magpie started by an update now waits for the one it replaces before its start-up moves write any file (AwaitPredecessor at the top of run, not only in the GUI). TestLoginsKeptWhileAnotherMagpieWrites (Windows: two writers replacing logins.json, readLogins never without the Antigravity account) failed 1 in 3 and 2 in 8 runs on the old code ("read as no accounts 8 times"), passed 11 of 11 on this; filememo's TestReadWhileReplaced the same (1180 misses on the old code); TestStatWaitsOutAReplace everywhere. A real update in a sandbox home on the Windows box, magpie-cli 0.1.880 → 0.1.942 from the release, kept the account and logins.json byte for byte. go vet (darwin, and windows nogui), builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-05 10:57:45 +08:00