Files
magpie/internal/agent/dsh_rounds_test.go
T
yetone 489d816213 agent: dsh's wiring round does nothing when nothing it reads changed
A user's live heap profile showed 35.7 GB allocated in 48 minutes with
~157 MB live. 12.1 GB of that was one path: KeepDshWired -> keepDshWired
-> dshWiredOnce. It runs every 30 s, and each time it rebuilt the whole
catalog and every route, only to write nothing. Within it:
magpieModels("dsh") 9.5 GB, dshRouteAgain 4.8 GB, dshPutRoute 3.8 GB, and
Exposed/Catalog/heldOf/entriesOf ~7.7 GB under them. heldOf caches only
during a Hold.

What changed:
- provider.StampNow (hold.go) records what the catalog is built from,
  without building it:
  - held.gen, which Changed and a plugin's change move
  - filememo.Gen
  - the environment
  - the stat stamps of providers.json, settings.json and the catalog's
    cache file, which another process's writes move
- keepDshWired keeps a dshRounds between rounds.
  - It builds magpieModels again only when the stamp moved, or when its
    models are dshModelsFor (5 min) old. The 5-minute rebuild covers what
    no stamp sees: another app's account file, or a model retired by date.
  - It skips the round when these are all the same as at the last round,
    and that round wrote nothing and left the files as it found them:
    - the bytes of dsh's files: every profile's cordis.patch.yml, the
      home layer, settings.yaml, .env, .credentials.yaml, config.yaml
    - the gateway URL
    - the hash of the models
  - A read error other than "missing" hashes as unknown, so the round
    runs.
- dshWiredOnce (Sync, Connect, tests) still always does a full round.
- No singleflight and no new lock: the round already runs under dshWrites.
- docs/subsystems/agent-wiring.md says so.

Not done: caching Exposed/Catalog/entriesOf outside a Hold. Their output
depends on time (retiredNow, disabledNow) and on files other apps write,
and nothing signals a change in either, so nothing would bound how stale
the cache gets.

Measured with a throwaway test in a sandbox HOME, on top of 4f044c24c:
12 providers from a real models.json, 270 models, 3 dsh profiles.
- Before, per round: 132.61 MB, 900,624 allocations (~265 MB/min).
- After, nothing changed: 0.26 MB, 119 allocations.
- After, models older than dshModelsFor: 30.77 MB, 88,336 allocations.
  The dsh parse and write are skipped because the models' hash is the
  same.
- Averaged over time: ~6.7 MB/min, against ~265 MB/min before.

New tests:
- TestDshRoundWithNothingChangedBuildsNothing: AllocsPerRun must be at
  most 300; it is 77 now.
- TestDshRoundAfterAChangeWritesAgain covers each change:
  - a provider saved
  - a setting saved
  - the patch list written over
  - providers.json written by another magpie
  - a change no stamp sees, picked up after dshModelsFor

Broken by hand, each went red:
- skip always off: the allocation test, at 2099 allocations
- stamp check removed: the provider, setting and another-magpie cases
- roots left out of the stamp: the another-magpie case
- aging removed: the unseen-change case
- file bytes left out of the sum: the patch-list case and
  TestKeepDshWiredWritesTheRouteAgain

Run at this tree. It is built on origin/main dee5c9d75 plus a cherry-pick
of 4f044c24c, which is not part of this commit.
- gofmt -l internal/agent internal/provider: clean.
- GOOS=linux and GOOS=windows go vet -tags nogui ./...: clean.
- darwin go vet ./...: clean.
- set -o pipefail; go test -count=1 -timeout 45m ./internal/agent/...
  ./internal/provider/...: ok agent 2146.7s, ok provider 709.9s.
- An earlier run that also took ./internal/gateway/ and
  ./internal/catalog/:
  - catalog: ok, 6.5s.
  - gateway: hit the 45m timeout (FAIL 2701.3s). Other sessions' GUI
    suites had the machine at a load average of ~548. The output's tail
    was cut off, so which test was running isn't known. Gateway doesn't
    call StampNow or the dsh round. Not rerun at a normal load.
2026-10-20 20:32:05 +08:00

130 lines
4.5 KiB
Go

package agent
import (
"os"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// dshSettled is a profile with magpie's route, and keepDshWired's rounds
// once the route is what magpie writes: the first round writes it, the
// second finds nothing to write.
func dshSettled(t *testing.T) (r *dshRounds, web string) {
t.Helper()
_, dir, web := dshRouteHome(t)
dshKeyFixture(dir)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"+
dshRouteFixture("deepseek/pro")+
"- id: agent-default-model\n config:\n provider: magpie\n model: deepseek/flash\n"), 0o644)
r = &dshRounds{}
r.once()
r.once()
if !r.done {
t.Fatal("a round with nothing to write is not kept")
}
if !dshListed(web, "deepseek/pro") || !dshListed(web, "deepseek/flash") {
b, _ := os.ReadFile(web)
t.Fatalf("the route is not what magpie writes:\n%s", b)
}
return r, web
}
// dshListed reports whether the route in a patch list lists model.
func dshListed(web, model string) bool {
b, _ := os.ReadFile(web)
return strings.Contains(string(b), "- id: "+model+"\n")
}
// A user's magpie allocated 35.7 GB in 48 minutes with ~157 MB live, its
// heap peaking near 885 MB; 12 GB of it was keepDshWired's round, every 30
// seconds building magpie's whole catalog and every route again to write
// nothing. A round with nothing changed since does nothing of that.
func TestDshRoundWithNothingChangedBuildsNothing(t *testing.T) {
r, web := dshSettled(t)
before, _ := os.ReadFile(web)
if n := testing.AllocsPerRun(10, func() { r.once() }); n > 300 {
t.Fatalf("a round with nothing changed made %.0f allocations", n)
}
if after, _ := os.ReadFile(web); string(after) != string(before) {
t.Fatalf("a round with nothing changed wrote:\n%s", after)
}
}
// What a round goes by changing has the next round write the route again:
// a provider saved, a setting saved, the patch list written over by dsh,
// and the providers' file written by another magpie (the CLI).
func TestDshRoundAfterAChangeWritesAgain(t *testing.T) {
t.Run("provider", func(t *testing.T) {
r, web := dshSettled(t)
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash", "max"}}); err != nil {
t.Fatal(err)
}
r.once()
if !dshListed(web, "deepseek/max") {
t.Fatal("a model added to a provider is not in dsh's route after the next round")
}
})
t.Run("setting", func(t *testing.T) {
r, web := dshSettled(t)
s := settings.Load()
s.HiddenModels = map[string][]string{"dsh": {"deepseek/pro"}}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
r.once()
if dshListed(web, "deepseek/pro") {
t.Fatal("a model hidden from dsh is still in its route after the next round")
}
})
t.Run("patch list", func(t *testing.T) {
r, web := dshSettled(t)
os.WriteFile(web, []byte("# Your patch layer for this dsh profile.\n"+
dshRouteFixture("deepseek/pro")+
"- id: agent-default-model\n config:\n provider: magpie\n model: deepseek/flash\n"), 0o644)
r.once()
if !dshListed(web, "deepseek/flash") {
t.Fatal("a route dsh wrote over is not written again by the next round")
}
})
t.Run("another magpie", func(t *testing.T) {
r, web := dshSettled(t)
b, err := os.ReadFile(provider.Path())
if err != nil {
t.Fatal(err)
}
s := strings.Replace(string(b), `"flash"`, `"flash", "max"`, 1)
if s == string(b) {
t.Fatalf("no model list in the providers' file:\n%s", b)
}
if err := os.WriteFile(provider.Path(), []byte(s), 0o600); err != nil {
t.Fatal(err)
}
r.once()
if !dshListed(web, "deepseek/max") {
t.Fatal("a provider another magpie saved is not in dsh's route after the next round")
}
})
// what the stamp can't see (another app's account file, a date) is
// written by a round once the models are dshModelsFor old
t.Run("unseen", func(t *testing.T) {
r, web := dshSettled(t)
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash", "max"}}); err != nil {
t.Fatal(err)
}
r.stamp = provider.StampNow() // as if the stamp hadn't moved
r.once()
if dshListed(web, "deepseek/max") {
t.Fatal("a round with its stamp unmoved built the models again: the test sees no skip")
}
r.at = time.Now().Add(-dshModelsFor)
r.once()
if !dshListed(web, "deepseek/max") {
t.Fatal("models dshModelsFor old are not built again")
}
})
}