Files
magpie/internal/agent/openclaw_look_test.go
T
yetone 783cd0f840 agent: OpenClaw's file is made plain JSON once a change for a look
The model's Get and Check are asked on every drift poll, and each one
copied openclaw.json (edit.Read clones it), copied it again, blanked its
comments, ran json.Valid and parsed it to see it is an object. They now
go through openClawLook: filememo.Read keeps the checked plain JSON
while the file is unchanged, shared and read only. A not-there file is
nil, as before. Wire, unwire, connect, Set and Sync still read the file
fresh (openClawRead), since they write from it.

Run (mote, at this tree):
- go test -count=1 ./internal/agent: ok (207s)
- go vet ./internal/agent for darwin, linux, windows: clean
- TestOpenClawLookReadsAnUnchangedFileOnce: green (0 allocations held,
  another program's write and a broken file seen, a gone file nil);
  with openClawLook put back on openClawRead by hand it is red
  (5 allocations), restored
- Real accounts (sandbox HOME copy), 40 /api/drift calls, paired runs:
  openClawRead 0.5/0.5 MB before, gone after; totals 17.4/21.4 MB
  before, 17.5/15.5 MB after (sampling noise is about +-15%).

Not tried: Windows and Linux runs of the package (vet only).
2026-11-10 06:28:13 +08:00

47 lines
1.3 KiB
Go

package agent
import (
"os"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
)
// The model's Get and Check are asked on every drift poll: an unchanged
// openclaw.json is made plain JSON and checked once, and a write, magpie's
// or another program's, is read at once.
func TestOpenClawLookReadsAnUnchangedFileOnce(t *testing.T) {
home, path := openClawHomeWith(t, openClawConfig)
old := time.Now().Add(-time.Minute)
os.Chtimes(path, old, old)
a := openClaw(home)
f := a.Field("model")
if got := f.Get(); got != "deepseek/deepseek-v4-pro" {
t.Fatalf("model = %q", got)
}
release := filememo.Hold()
defer release()
openClawLook(path)
if n := testing.AllocsPerRun(20, func() { openClawLook(path) }); n > 0 {
t.Fatalf("%v allocations for an unchanged file", n)
}
if err := os.WriteFile(path, []byte(`{"agents":{"defaults":{"model":"kimi/k2"}}}`), 0o600); err != nil {
t.Fatal(err)
}
filememo.Look(path)
if got := f.Get(); got != "kimi/k2" {
t.Fatalf("after another program's write, model = %q", got)
}
os.WriteFile(path, []byte(`{"agents":`), 0o600)
filememo.Look(path)
if a.Check() == "" {
t.Fatal("a file that isn't JSON is checked as fine")
}
os.Remove(path)
filememo.Look(path)
if s, err := openClawLook(path); s != nil || err != nil {
t.Fatalf("gone file: %q %v", s, err)
}
}