Commit Graph
3 Commits
Author SHA1 Message Date
ggbdpq 24d4425944 gateway: TestRequestBuildsProvidersOnce judges the build's ask, not the
request's start (#2965)

Cause: the test judged the second request by when it began, letting it
want 0 rebuilds only within 900ms of the first, while what decides a
rebuild is the hold and the stamp, not the clock from a request's
start. Two ways that guess goes red: on a loaded machine a request can
begin within the second and reach the build after the hold has let what
was built go — "request 2 built the providers 1 times, want 0", seen in
full runs at load ~280–400; and since 6a050eeb keeps the catalog for
the next requests, a request that begins past the 900ms builds none at
all — "request 2 built the providers 0 times, want 1", which an idle
machine now reproduces with a plain 1.2s pause between the requests.

Fix: allProviders notes when the build it counts was asked for
(AllBuiltAt, beside AllBuilt), and the test judges a rebuild by that,
not by the request's start: a rebuild asked a second or more after the
first build's ask is the hold letting go, rightly, and is allowed only
when it is exactly one; a rebuild asked within the second, or more than
one build in a request (the #2502 bug class), stays red. Nothing rests
on how fast the machine gets from a request's start to its build.

Red first (macOS arm64, idle, this tree): with a 1.2s pause put before
the second request, the old test failed "request 2 built the providers
0 times, want 1"; the new test passed the same pause (-count=3); the
old form failed it again with the hook in place (ablation: the test is
what guards). The loaded-machine shape is the issue's own run at load
~280–400 and the maintainer's "request 2 built the providers 1 times,
want 0" at c8c0985df; the "request 1 built 2 times" red at dee5c9d75 is
not the guess — two builds inside one request stays red here by design,
per #2502.

Run (macOS arm64, go1.26.8, load ~4–7):
- go test -tags nogui -count=5 -run 'TestRequestBuildsProvidersOnce$'
  ./internal/gateway/: ok.
- go test -tags nogui -race -count=4 -run
  'TestRequestBuildsProvidersOnce$' ./internal/gateway/: ok.
- go test -tags nogui -count=1 ./internal/gateway/: ok (634.7s).
- go test -tags nogui -count=1 ./internal/provider/: ok (127.8s).
- gofmt -l internal/gateway/ internal/provider/: clean.
- go vet -tags nogui ./internal/gateway/ ./internal/provider/ for
  darwin, linux, windows: ok.
- CGO_ENABLED=0 go build -tags nogui ./..., darwin/linux/windows: ok.

Fixes #2965

Generated-by: GLM-5.3-Flash (ZCode)

Review (mote, go1.26.8): go test -tags nogui -count=3 -run
'TestRequestBuildsProvidersOnce$' ./internal/gateway/ ok. A rebuild
happens only when the second ask is a second or more after the build's
end (h.at is set after the build), so a second or more between the asks
is necessary and the new allowance excuses no wrong rebuild.
Broken by hand: heldFor at 1ns makes it red ("request 1 built the
providers 2 times, want 1"); a 1.2s pause before request 2 passes, which
is the case the old test misjudged.
2026-11-06 05:53:50 +08:00
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
yetone f54e76dbdd gateway: a request builds the providers once, and plugin-auth.json and the discovery setting are parsed once a version (#2502)
XcodeFish (#2502): with several agents at once the gateway sat at
100-350% CPU. Counting provider.AllBuilt around one streamed chat
request gave 3 full catalogue builds per request, and each build re-read
and re-parsed plugin-auth.json and settings.json (AccountDiscoveryOff
called Load() on every provider list).

- holdCatalog wraps the gateway's handler in provider.Hold() and
  filememo.Hold(), so one request builds the providers once and stats
  each file at most once a second. Hold already drops the cache on
  provider.Changed and on magpie's own writes; another program's write
  is seen within the second.
- plugin-auth.json is read through filememo (mtime, size, and the raw
  bytes while the file is under 2s old), so it is parsed once per
  version. A file that doesn't parse falls back to the last good .bak
  through lastgood, as settings does. Auths still hands each caller its
  own decoded map. The host looks at the file again before it reports a
  sign-in it just saved.
- AccountDiscoveryOff reads the setting through filememo with the same
  parse settings.Load uses (now settings.parse), under fileMu.

What was run:
- TestRequestBuildsProvidersOnce: two streamed requests, one build each
  (3 each before). Red with holdCatalog taken out.
- TestAuthParsedOncePerVersion: 100 rounds of Auths, SignedIn and the
  listing proxies parse plugin-auth.json zero more times; a caller's
  edit doesn't leak; a same-size rewrite is read; a zeroed file reads
  the .bak and stays signed in. Red with authRaw reading past filememo.
- TestAccountDiscoveryOffParsedOnce: 100 calls, no parse; a Save is
  read. Red with AccountDiscoveryOff back on Load().
- gofmt -l clean; go vet -tags nogui on darwin, linux and windows ok.
- Full suite, set -o pipefail; go test -p 4 -timeout 45m ./... at this
  pair rebased on 63be47717, machine load average 170-330 from other
  work: 63 packages ok; internal/agent and internal/gateway ran out of
  45m (#2544, #2191, #2108); internal/gui TestMainWindowCorners killed
  at its 40s deadline (#1936). Run alone afterwards, gateway and agent
  ran out of 45m again, and gateway's
  TestQuietCodexStreamKeptAliveThroughReplyWriters/a_middleware got its
  reply unchanged once, the middleware failing open on its time limit
  (filed #2565; 6/6 alone on 63be47717 and here).
- The same two packages on 63be47717 alone, side by side with this
  head under the same load: both ran out of 45m there too, in other
  tests, so the timeouts are not this change.

Not tried: a benchmark against a real many-agent load, or the
reporter's machine. The count of builds per request is the measure.
2026-10-16 20:05:11 +08:00