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.
This commit is contained in:
yetone
2026-11-10 05:13:34 +08:00
parent b27b660467
commit 8d9254729a
3 changed files with 78 additions and 10 deletions
+32 -9
View File
@@ -32,6 +32,7 @@ import (
"fmt"
"hash/maphash"
"io"
"io/fs"
"log"
"os"
"path/filepath"
@@ -48,6 +49,7 @@ import (
"github.com/yetone/magpie/internal/beat"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/provider"
)
@@ -453,7 +455,7 @@ func dshHomePatch(dir string) string { return filepath.Join(dir, "cordis.patch.y
// replaces the fields it supplies: config replaces the whole config, but
// disabled:false alone leaves the profile's config intact. For reading only.
func dshOver(dir string, items []dshItem) []dshItem {
_, home, err := dshRead(dshHomePatch(dir))
_, home, err := dshLook(dshHomePatch(dir))
if err != nil || len(home) == 0 {
return items
}
@@ -648,6 +650,27 @@ func dshRead(path string) ([]string, []dshItem, error) {
return dshParse(path, string(b))
}
// dshParsed is a patch list as dshParse gave it.
type dshParsed struct {
head []string
items []dshItem
}
// dshLook is dshRead for a look that writes nothing: parsed once each time
// the file changes and shared, so read only. The window's 15s drift poll
// read and parsed every profile's list again on each ask, a third of what
// forty asks allocated with real accounts.
func dshLook(path string) ([]string, []dshItem, error) {
d, err := filememo.Read("dsh list", path, func(b []byte) (dshParsed, error) {
head, items, err := dshParse(path, string(edit.TrimBOM(b)))
return dshParsed{head, items}, err
})
if errors.Is(err, fs.ErrNotExist) {
return nil, nil, nil // as dshRead, whose edit.Read gives no bytes
}
return d.head, d.items, err
}
// dshWrite puts a patch list back together.
func dshWrite(path string, head []string, items []dshItem) error {
out := append([]string{}, head...)
@@ -909,7 +932,7 @@ func dshGet(dir, lead string) string {
if f == "" {
return dshGetLegacy(filepath.Join(dir, "config.yaml"))
}
_, items, err := dshRead(f)
_, items, err := dshLook(f)
if err != nil {
return ""
}
@@ -950,7 +973,7 @@ func dshStartSel(dir string, items []dshItem) map[string]string {
// dshGetLegacy reads agent-loop's model from a config.yaml before 0.1.5.
func dshGetLegacy(path string) string {
_, items, err := dshRead(path)
_, items, err := dshLook(path)
if err != nil {
return ""
}
@@ -981,7 +1004,7 @@ func dshCheck(dir, gw string) string {
files := dshProfiles(dir)
if len(files) == 0 {
path := filepath.Join(dir, "config.yaml")
_, items, _ := dshRead(path)
_, items, _ := dshLook(path)
base := ""
if i := dshFind(items, "llm-deepseek"); i >= 0 {
for _, l := range items[i].lines {
@@ -993,7 +1016,7 @@ func dshCheck(dir, gw string) string {
get := func(string) (string, bool) { return base, base != "" }
return wiringOff("DeepSeek Harness", path, get, "baseURL", gw+"/v1")
}
_, items, _ := dshRead(files[0])
_, items, _ := dshLook(files[0])
routeFile := files[0]
if dshHomeHas(dir, dshPiRow) {
routeFile = dshHomePatch(dir)
@@ -1012,7 +1035,7 @@ func dshCheck(dir, gw string) string {
// wiring, which dshSync moves rather than this round writing beside.
canWriteBack := len(sharedModels(dshListFor(dir))) > 0
for _, f := range files[1:] {
_, items, err := dshRead(f)
_, items, err := dshLook(f)
if err != nil || dshWired(dshOver(dir, items)) {
continue
}
@@ -1064,7 +1087,7 @@ func dshCheck(dir, gw string) string {
// way. Every profile carries a route of its own, and dsh runs whichever
// one its session names, so every profile is asked.
for _, f := range files {
_, it, err := dshRead(f)
_, it, err := dshLook(f)
if err != nil {
continue
}
@@ -2784,13 +2807,13 @@ func DshPatchFiles(dir string) []string { return dshFiles(dir) }
func dshGetEffort(dir, lead string) string {
f := dshLead(dir, lead)
if f == "" {
_, items, err := dshRead(filepath.Join(dir, "config.yaml"))
_, items, err := dshLook(filepath.Join(dir, "config.yaml"))
if err != nil {
return ""
}
return dshEffortIn(items)
}
_, items, err := dshRead(f)
_, items, err := dshLook(f)
if err != nil {
return ""
}
+45
View File
@@ -0,0 +1,45 @@
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)
}
}
+1 -1
View File
@@ -125,7 +125,7 @@ func dshSearchesHere(dir string) bool {
if len(files) == 0 {
return false
}
_, items, err := dshRead(files[0])
_, items, err := dshLook(files[0])
if err != nil {
return false
}