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).
This commit is contained in:
yetone
2026-11-10 06:28:13 +08:00
parent ce6ae593c5
commit 783cd0f840
2 changed files with 73 additions and 3 deletions
+27 -3
View File
@@ -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()
}
+46
View File
@@ -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)
}
}