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.