Files
magpie/internal/edit/yaml_held_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

137 lines
4.2 KiB
Go

package edit
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"gopkg.in/yaml.v3"
)
// GetYAML and its siblings are asked of an agent's YAML settings per
// field, every window poll (dsh's 80 KB settings.yaml); a look at a file
// that hasn't changed doesn't parse it again. It still sees a BOM in
// front, a nested map, a list and a block's text, and a write, its own or
// another process's.
func TestGetYAMLParsesTheFileOnce(t *testing.T) {
var b strings.Builder
b.WriteString("\xef\xbb\xbf# dsh settings\nagent:\n model: fake/m1\n levels:\n low: a\n high: b\n tags: [x, y]\nhistory:\n")
for i := range 3000 {
fmt.Fprintf(&b, " - {id: entry-%d, note: \"some words to make it longer\"}\n", i)
}
path := filepath.Join(t.TempDir(), "settings.yaml")
if err := os.WriteFile(path, []byte(b.String()), 0o600); err != nil {
t.Fatal(err)
}
old := time.Now().Add(-time.Hour)
if err := os.Chtimes(path, old, old); err != nil {
t.Fatal(err)
}
if v, ok := GetYAML(path, "agent.model"); !ok || v != "fake/m1" {
t.Fatalf("model: %q %v", v, ok)
}
if m := GetYAMLMap(path, "agent.levels"); m["low"] != "a" || m["high"] != "b" || len(m) != 2 {
t.Fatalf("levels: %v", m)
}
if l := GetYAMLList(path, "agent.tags"); strings.Join(l, ",") != "x,y" {
t.Fatalf("tags: %v", l)
}
if v, ok := GetYAMLText(path, "agent.tags"); !ok || v != "[x, y]" {
t.Fatalf("tags as text: %q %v", v, ok)
}
const looks = 20
var m0, m1 runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m0)
for range looks {
if v, _ := GetYAML(path, "agent.model"); v != "fake/m1" {
t.Fatalf("model: %q", v)
}
GetYAMLMap(path, "agent.levels")
}
runtime.ReadMemStats(&m1)
if per, limit := (m1.TotalAlloc-m0.TotalAlloc)/looks, uint64(b.Len()/10); per > limit {
t.Errorf("a look at a %d KB file allocates %d KB, over %d KB", b.Len()>>10, per>>10, limit>>10)
}
if err := SetYAML(path, KV{Path: "agent.model", Value: "fake/m2"}); err != nil {
t.Fatal(err)
}
if v, _ := GetYAML(path, "agent.model"); v != "fake/m2" {
t.Errorf("after a write: %q, want fake/m2", v)
}
// another process's write, a moment later
raw, _ := os.ReadFile(path)
if err := os.WriteFile(path, []byte(strings.Replace(string(raw), "fake/m2", "fake/m3", 1)), 0o600); err != nil {
t.Fatal(err)
}
later := time.Now().Add(time.Minute)
os.Chtimes(path, later, later)
if v, _ := GetYAML(path, "agent.model"); v != "fake/m3" {
t.Errorf("after another's write: %q, want fake/m3", v)
}
// gone, then not YAML: nothing read, as before
os.Remove(path)
if v, ok := GetYAML(path, "agent.model"); ok || v != "" {
t.Errorf("gone: %q %v", v, ok)
}
os.WriteFile(path, []byte("- a\n- b\n"), 0o600)
if v, ok := GetYAML(path, "agent.model"); ok || v != "" {
t.Errorf("a list: %q %v", v, ok)
}
}
// blockSequence, BlockList's shortcut, says a block list for exactly the
// texts whose first document a decode finds a block sequence in.
func TestBlockSequenceAgreesWithADecode(t *testing.T) {
for _, raw := range []string{
"- a\n- b\n",
"-\n",
"-\ta\n",
"# head\n\n- id: a\n x: 1\n",
"---\n- a\n",
"--- - a\n",
"--- # c\n- a\n",
"%YAML 1.2\n---\n- a\n",
" - a\n - b\n",
"- a\r\n- b\r\n",
"[]\n",
"[a, b]\n",
"# c\n[\n {id: a},\n]\n",
"--- [a]\n",
"---\n[a]\n",
"a: 1\n",
"-1\n",
"--x\n",
"---\n",
"",
"# only a comment\n",
"key: [- a]\n",
"!!seq [a]\n",
"&x [a]\n",
} {
var doc yaml.Node
dec := yaml.NewDecoder(strings.NewReader(raw))
ok := dec.Decode(&doc) == nil && len(doc.Content) == 1 && doc.Content[0].Kind == yaml.SequenceNode
block, flow := ok && doc.Content[0].Style&yaml.FlowStyle == 0, ok && doc.Content[0].Style&yaml.FlowStyle != 0
// a block list is told without a decode; a flow list never is
// (text that isn't YAML may go either way: BlockList leaves it)
if got := blockSequence(raw); block && !got || flow && got {
t.Errorf("%q: blockSequence %v, a decode: block %v, flow %v", raw, got, block, flow)
}
if blockSequence(raw) {
if _, ok := BlockList(raw); ok {
t.Errorf("%q: a block list rewritten", raw)
}
}
}
if got, ok := BlockList("[{id: a}, {id: b}]\n"); !ok || !strings.Contains(got, "- id: a") {
t.Errorf("a flow list: %q %v", got, ok)
}
}