Files
magpie/internal/agent/dsh_shared_test.go
T
yetone 35c47f762f agent: the window's drift poll parses dsh's YAML once per change, not per field and per look
The window asks /api/drift every 15s. On the owner's live magpie a heap
profile showed Drift and Values allocating ~3.3 GB in 37 minutes, most
of it DeepSeek Harness: settings.yaml (80 KB) parsed again for each
field GetYAML read (465 MB of it), and each profile entry's YAML parsed
again on each of dshCheck's looks (dshHomeHas, dshOver, dshConfigIndex,
dshRoutesIn, dshConfig).

- edit.loadYAML memoizes the parsed root through filememo, keyed by the
  file's stat, and shares it with GetYAML, GetYAMLMap, GetYAMLList and
  GetYAMLText, which only read it. Writes go through their own parse as
  before. BlockList skips the decode for a text that starts as a block
  list (blockSequence), which every dsh patch list does.
- dshNodeShared parses an entry's text once and shares the node with the
  read-only lookers (dshConfig, dshConfigIndex, dshRoutesIn, dshOver's
  patch side). The writers (dshOver's base, the route and start writes)
  keep dshNode's own copy. 512 entries kept at most.

Run on mote (macOS 27.2):
- Probe on copies of the owner's real ~/.dsh (settings.yaml, config.yaml,
  the desktop and web profiles; probe not committed, copies deleted):
  a.Values() + a.Drift() per poll, 20 polls: 16381 KB / 223788 allocs
  per poll with the edit change alone -> 4355 KB / 2588 allocs.
- TestGetYAMLParsesTheFileOnce (new): a look at a 3000-entry file
  allocates under a tenth of its size; sees SetYAML, another process's
  write, a removed file and a list-topped file.
  TestBlockSequenceAgreesWithADecode (new): ~24 texts against yaml's own
  decode.
- TestDshLookParsesAnEntryOnce (new): with dshConfig and dshConfigIndex
  put back on dshNode by hand: "27388 allocations, want a few", red.
  Restored: ok. It also checks a writer's change never shows in a look.
- go test ./internal/agent ./internal/edit: ok (164.2s, 5.9s).
  go vet both: ok. gofmt -l: clean.

Not tried: the full suite (only internal/agent and internal/edit
call these); a live window poll after the release.
2026-11-02 06:30:12 +08:00

40 lines
1.5 KiB
Go

package agent
import (
"fmt"
"strings"
"testing"
)
// The drift poll looks at each dsh entry's config several times a poll; an
// entry whose text hasn't changed is parsed once, and a writer's node is
// its own, so a change to it never shows in the next look.
func TestDshLookParsesAnEntryOnce(t *testing.T) {
lines := []string{"- id: " + dshPiRow, " name: pi", " config:", " model: \"m1\"", " providers:"}
for i := range 400 {
lines = append(lines, fmt.Sprintf(" p%d:", i), " baseURL: http://127.0.0.1:1/v1", " models: [a, b, c]")
}
it := dshItem{id: dshPiRow, lines: lines}
if got := dshConfig(it)["model"]; got != "m1" {
t.Fatalf("model: %q", got)
}
if n := testing.AllocsPerRun(20, func() { dshConfig(it); dshConfigIndex([]dshItem{it}, dshPiRow) }); n > 50 {
t.Errorf("a look at an unchanged entry: %.0f allocations, want a few", n)
}
w, err := dshNode(it)
if err != nil {
t.Fatal(err)
}
yamlSetKey(yamlKey(w, "config"), "model", yamlKey(w, "name"))
if got := dshConfig(it)["model"]; got != "m1" {
t.Errorf("a writer's change showed in a look: %q", got)
}
it.lines = append(append([]string{}, it.lines[:3]...), append([]string{" model: m2"}, it.lines[4:]...)...)
if got := dshConfig(it)["model"]; got != "m2" {
t.Errorf("changed text: %q, want m2", got)
}
if _, err := dshNodeShared(dshItem{id: "bad", lines: []string{"- id: bad", " : ["}}); err == nil || !strings.Contains(err.Error(), "bad") {
t.Errorf("bad entry: %v", err)
}
}