Why: a live heap profile of the app (yg6888 on X, ~1 GB held) had
36 GB allocated, and 5.3 GB of it was under seer -> autoVision ->
smallModelWhere. 4.8 GB of that came through agent.magpieModels
(provider.Described, asked for every agent's model list: dsh every 30s,
and the window's state). Each AutoVision did three things for every
provider that was on:
- built its Exposed list twice: once in smallReady, then again only to
read the picked model's price;
- asked spentModel (perKeyBarred, 1.4 GB) about every candidate model,
before picking one;
- copied the list three times in pickSmall.
The searcher's pick (searcherReady, Searchers, chosenSearcher) goes
through the same smallReady. For a Remote magpie it asked
p.Available() once per model (remoteSearch).
What changed:
- rankSmall puts the candidates in pickSmall's order once. pickSmall of
any subset is the first of that subset in this order, and
TestRankSmallIsPickSmall checks that against the old pickSmall over
2000 random lists and subsets. smallReadyOf then asks spentModel in
rank order and stops at the first model with allowance. The model it
picks, and whether it is ready, are the same as before. Usually only
one model is asked.
- The candidate carries its cost, so smallModelWhere no longer builds
Exposed a second time.
- keptSmallCands keeps each provider's ranked candidates in
stamped[smallKey, []smallCand]. The key is the provider id, the
signed-in account (agent, user, plan, home) and which filter ("sees",
"search"). Entries are dropped when provider.StampNow moves, or
after stampedFor (1 minute).
- provider.StampNow (hold.go, added with dsh's round) is made of:
- held.gen, which Changed moves: a provider saved, an account,
catalog.SaveLive/Touched/Reset, a plugin's change;
- filememo.Gen (settings.Save and every other write this process
makes);
- environSum;
- the time and size of the providers' file, the settings file and the
catalog's cache file, so a write by another magpie (the CLI) counts
too.
The one-minute backstop covers what nothing signals: a list another
magpie fetched, or another program changing an existing account's
model list. A new account is a new key, so it is seen at once (within
provider.Accounts' own 30s).
- Whether a model is out of its allowance (spentModel) is still asked
on every call, never kept. Two callers building one key at the same
time each build it. Nothing waits on another, and nothing is built
under the lock.
Measured on the real path: a sandbox HOME with the live
~/.cache/magpie/models.json (5.8 MB, 231 providers) copied in, 12 API
key providers (openrouter, anthropic, openai, google, deepseek,
nano-gpt, vercel, kilo, requesty, aihubmix, zenmux, poe), and the
catalog.Provider fix 4f044c24c applied to both sides. The benchmark is
go test -bench, -benchtime 2s, not committed. The pick is
requesty/mimo-v2.6-flash before and after.
| Benchmark | Before | After |
| --- | --- | --- |
| AutoVision | 769.7 ms, 22,075,218 B, 73,732 allocs/op | 7.3 ms, 238,201 B, 1,134 allocs/op |
| AutoVision under provider.Hold | 347.8 ms, 21,532,432 B, 69,061 allocs/op | 2.1 ms, 85,207 B, 244 allocs/op |
What is left is mostly provider.All, which reads the accounts.
Tests:
- TestAutoVisionDoesNotBuildListsAgain: AllocsPerRun of AutoVision and
searcherModel with 4 and with 400 listed models. It allows 50
allocations of difference: 521 against 527.
- Broken by hand (stamped.get always builds), it went red at 611
against 1055.
- TestAutoVisionSeesEachChange checks that each change is picked up:
- picks changed through provider.Save;
- a vendor's list re-fetched with new prices (catalog.SaveLive);
- providers.json rewritten directly, as another magpie would;
- settings.Save moving the stamp;
- a Codex sign-in written into the same HOME.
Broken by hand (the stamp ignored, only the minute kept), it went red
at "AutoVision = "" after Haiku and Gemini were enabled". Run with the
other AutoVision tests, TestAutoVisionPassesOverOneOut went red too.
- TestAutoVisionKeptListsAskAllowanceEachTime checks that a used-up
Codex account is seen on the very next call.
Run:
- gofmt -l internal/: clean.
- GOOS=linux and GOOS=windows go vet -tags nogui ./...: clean.
- set -o pipefail; go test -count=1 -timeout 45m ./internal/gateway/
./internal/provider/ ./internal/catalog/ ./internal/gui/, at load
~400 (other sessions' suites), with 4f044c24c applied:
- gateway ok 2259.7s, provider ok 821.8s, catalog ok 16.5s.
- gui FAIL in two tests that fail the same way without this change.
Each was run alone, with these files reverted, on 28bcd4f7e:
- TestMainWindowCorners: the real AppKit window was killed at its
40s deadline, alone too (#1936, #2645).
- TestSetAnswersWhileDevinAndProvidersHang: setting slow/slow-1
took 10.9s against its 10s bound. Alone it took 3.3 to 5.4s with
this change and 4.6 to 5.9s without it, passing 3 of 3 (#1375).
- The new tests were run again after the split into two commits:
go test -run 'TestInfoCounts|TestAutoVision|TestRankSmall|Searcher'
./internal/gateway/: ok.
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// stampedFor is how long what a stamped keeps is used while the stamp
|
|
// stays the same: a list another magpie fetched, or an account another
|
|
// program signed in to, moves no stamp, and is seen this long after.
|
|
const stampedFor = time.Minute
|
|
|
|
// stamped keeps what is built from the providers' model lists, by key,
|
|
// until provider.StampNow moves or stampedFor has gone by. Each
|
|
// AutoVision (every agent's model list asks it, provider.Described) built
|
|
// every provider's list again, twice, and a probe of the gateway (info)
|
|
// the whole catalog to count it: 5.3 GB and 5.4 GB of a day's 36 GB of
|
|
// allocations (yg6888 on X, ~1 GB held). Two callers building one key at
|
|
// once each build it; none waits on another.
|
|
type stamped[K comparable, V any] struct {
|
|
mu sync.Mutex
|
|
stamp provider.Stamp
|
|
at time.Time
|
|
of map[K]V
|
|
}
|
|
|
|
func (s *stamped[K, V]) get(k K, build func() V) V {
|
|
st := provider.StampNow()
|
|
s.mu.Lock()
|
|
if s.stamp != st || time.Since(s.at) >= stampedFor {
|
|
s.stamp, s.at, s.of = st, time.Now(), nil
|
|
}
|
|
if v, ok := s.of[k]; ok {
|
|
s.mu.Unlock()
|
|
return v
|
|
}
|
|
s.mu.Unlock()
|
|
// built from what is there after st was taken: never older than st
|
|
v := build()
|
|
s.mu.Lock()
|
|
if s.stamp == st {
|
|
if s.of == nil {
|
|
s.of = map[K]V{}
|
|
}
|
|
s.of[k] = v
|
|
}
|
|
s.mu.Unlock()
|
|
return v
|
|
}
|