diff --git a/internal/agent/openclaw.go b/internal/agent/openclaw.go index 28483abd0..8f2428c80 100644 --- a/internal/agent/openclaw.go +++ b/internal/agent/openclaw.go @@ -41,8 +41,11 @@ package agent // and so does a config split with $include. import ( + "bytes" "encoding/json" + "errors" "fmt" + "io/fs" "os" "path/filepath" "slices" @@ -53,6 +56,7 @@ import ( "github.com/yetone/magpie/internal/capre" "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/filememo" ) // what OpenClaw says after a change (notice.go) @@ -123,7 +127,27 @@ func openClawRead(path string) ([]byte, error) { if err != nil { return nil, err } - if len(strings.TrimSpace(string(b))) == 0 { + return openClawPlain(path, b) +} + +// openClawLook is openClawRead for a look that writes nothing (the +// model's Get, Check), which every drift poll makes: the file is made +// plain JSON and checked once a change, and the bytes are shared, so read +// only. +func openClawLook(path string) ([]byte, error) { + s, err := filememo.Read("openclaw json", path, func(b []byte) ([]byte, error) { + return openClawPlain(path, edit.TrimBOM(b)) + }) + if errors.Is(err, fs.ErrNotExist) { + return nil, nil + } + return s, err +} + +// openClawPlain is b, OpenClaw's file, made plain JSON in a copy of its +// own, or nil for an empty file. +func openClawPlain(path string, b []byte) ([]byte, error) { + if len(bytes.TrimSpace(b)) == 0 { return nil, nil } s := jsonc.ToJSONInPlace(append([]byte(nil), b...)) @@ -220,7 +244,7 @@ func openClawAt(at place) *Agent { dir, path := openClawPaths(at) keyWas := at.key("openclaw:" + path + ":model") read := func() gjson.Result { - s, _ := openClawRead(path) + s, _ := openClawLook(path) return gjson.ParseBytes(s) } ours := func(c gjson.Result) bool { return prefixed(openClawPrimary(c.Get(openClawModel))) } @@ -434,7 +458,7 @@ func openClawAt(at place) *Agent { return patch(set, del) }, Check: func() string { - s, err := openClawRead(path) + s, err := openClawLook(path) if err != nil { return err.Error() } diff --git a/internal/agent/openclaw_look_test.go b/internal/agent/openclaw_look_test.go new file mode 100644 index 000000000..c976ad44c --- /dev/null +++ b/internal/agent/openclaw_look_test.go @@ -0,0 +1,46 @@ +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) + } +}