Files
magpie/internal/agent/dsh_look_test.go
T
yetone 8d9254729a agent: dsh's patch lists are parsed once a change for a look
The window asks /api/drift every 15s while it is up, and each ask read
and parsed every dsh profile's patch list again, several times over
(dshGet, dshGetEffort, dshOver for the home layer, dshCheck). With real
accounts, forty asks allocated 38 MB, 13 MB of it dshRead and its parse.

dshLook is dshRead through filememo.Read: parsed once each time the file
changes and shared, so only the paths that look and write nothing use it
(dshGet, dshGetEffort, dshOver, dshGetLegacy, dshCheck,
dshSearchesHere). Every write path keeps dshRead, whose items it edits.
A gone file reads as empty, as edit.Read made it for dshRead.

Run:
- TestDshLookParsesAnUnchangedListOnce: 0 allocations a look under a
  request's hold, dsh's own later save read, a gone list empty. Broken
  by hand (dshLook returning dshRead): red, 12 allocations a look.
- go test ./internal/agent ./internal/edit ./internal/filememo on mote;
  go vet ./internal/agent for darwin, windows and linux.
- Real accounts (sandbox HOME on mote, 40 /api/drift asks): see below.
  before (t23) 35.6 and 38.4 MB, 12.7 and 13.2 MB of it dshRead; after
  (t24) 26.0 and 15.7 MB, no dshRead or dshParse left in the profile.
2026-11-10 05:13:34 +08:00

46 lines
1.6 KiB
Go

package agent
import (
"os"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
)
// A look at a patch list (the window's drift poll asks each profile's
// several times) parses it once while it is unchanged, and a change
// another process makes — dsh's own save — is read, not the kept parse.
func TestDshLookParsesAnUnchangedListOnce(t *testing.T) {
_, dir, web := dshRouteHome(t)
list := "- id: agent-default-model\n config:\n provider: deepseek-official\n model: deepseek-v4-flash\n reasoningEffort: low\n"
if err := os.WriteFile(web, []byte(list), 0o644); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-time.Minute) // past the moment a write is told apart by its bytes
os.Chtimes(web, old, old)
if got := dshGet(dir, ""); got != "deepseek-v4-flash" {
t.Fatalf("model %q", got)
}
defer filememo.Hold()()
if n := testing.AllocsPerRun(20, func() { dshLook(web) }); n != 0 {
t.Fatalf("an unchanged list was parsed again: %v allocations a look", n)
}
// dsh saves another pick: the next look has it
if err := os.WriteFile(web, []byte(list+"- id: agent-default-model\n config:\n provider: deepseek-official\n model: deepseek-v4-pro\n"), 0o644); err != nil {
t.Fatal(err)
}
filememo.Look(web)
if got := dshGet(dir, ""); got != "deepseek-v4-pro" {
t.Fatalf("after dsh's save: model %q", got)
}
// a list that is gone reads as empty, as dshRead's
if err := os.Remove(web); err != nil {
t.Fatal(err)
}
filememo.Look(web)
if _, items, err := dshLook(web); err != nil || items != nil {
t.Fatalf("gone: %v %v", items, err)
}
}