gui: a poll of the Agents page no longer drops every cached model list
/api/state keeps the state it built in agents-state.json, written only
when the bytes change. They changed on every poll: a stale copy's start
is now less how long it has run, two clock reads apart, so it moved by
microseconds each time (on a real config, only
/agents[1]/staleCopies[i]/since differed between two polls). The file
was written through edit.WriteAtomic, whose write calls
filememo.Forget(). That bumps provider.StampNow, so every 15s poll threw
away each agent's model list and picker (magpieModels, viaMagpie) and
every provider's kept catalog, and the next look built them anew.
- StaleCopies' since is truncated to the second (the check against
magpie's change still uses the exact time).
- keepState writes through steady.WriteFile (temp, flush, rename), which
leaves filememo alone: nothing a request reads is built from this file.
Not changed: on Linux and WSL the process list gives a run time in whole
seconds (ps etime), so a copy's since can still move by a second between
polls there; macOS reads the exact start from sysctl.
Run on mote at 7428f3229 + this change:
- go test ./internal/agent ./internal/gui (-count=1 -timeout 45m):
agent ok (280.8s). gui FAIL in two tests already red on main and
filed: TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners/macOS_26_SDK on macOS 27 (#3949).
- node --test agent-stale-copies.test.cjs: 0 failed.
- go vet (darwin), GOOS=windows go vet of both packages, GOOS=linux go
vet ./internal/agent, gofmt -l: clean. A Linux vet of internal/gui
needs gtk4 and webkitgtk-6.0, which neither mote's cross build nor the
WSL box has.
Broken by hand: without the truncation TestStaleCopySinceSameEachLook
is red (since 08:20:11.5 then 08:20:11.500001); with filememo.Forget()
after the write TestKeptStateLeavesFilememo is red.
This commit is contained in:
@@ -319,6 +319,10 @@ func (a *Agent) StaleCopies() []StaleCopy {
|
||||
if !since.Before(changed.Add(-time.Second)) {
|
||||
continue
|
||||
}
|
||||
// to the second: since is now less how long it has run, two
|
||||
// clock reads apart, and a time that moved by microseconds
|
||||
// each poll rewrote the last-known state each time
|
||||
since = since.Truncate(time.Second)
|
||||
c := StaleCopy{Since: since}
|
||||
if a.ID == "codex" {
|
||||
var app string
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A copy's start is now less how long it has run, two clock reads apart,
|
||||
// so it moved by microseconds on each look. The Agents page keeps the
|
||||
// state it was given, and a time that moved rewrote that file on every
|
||||
// poll. Asked again, a copy that hasn't changed says the same start.
|
||||
func TestStaleCopySinceSameEachLook(t *testing.T) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip(staleOnWindows)
|
||||
}
|
||||
home := t.TempDir()
|
||||
t.Setenv("HOME", home)
|
||||
t.Setenv("USERPROFILE", home)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
||||
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
||||
t.Setenv("CODEX_HOME", "")
|
||||
dir := filepath.Join(home, ".codex")
|
||||
os.MkdirAll(dir, 0o755)
|
||||
cfg := filepath.Join(dir, "config.toml")
|
||||
list := filepath.Join(dir, "magpie-models.json")
|
||||
conf := "model = \"magpie/gpt-6-luna\"\nmodel_provider = \"magpie\"\nmodel_catalog_json = \"" + tomlString(list) + "\"\n\n" +
|
||||
"[model_providers.magpie]\nname = \"magpie\"\nbase_url = \"http://127.0.0.1:3425/v1\"\n"
|
||||
os.WriteFile(cfg, []byte(conf), 0o600)
|
||||
os.WriteFile(list, []byte(`{"models":[{"slug":"magpie/gpt-6-luna"}]}`), 0o644)
|
||||
|
||||
// started two hours ago, half a second past a whole second, as the
|
||||
// process list tells it: how long it has run at the moment it is read
|
||||
start := time.Now().Add(-2 * time.Hour).Truncate(time.Second).Add(500 * time.Millisecond)
|
||||
was := running
|
||||
t.Cleanup(func() { running = was })
|
||||
running = func(string) []runningProc {
|
||||
return []runningProc{{time.Since(start), "/Applications/Codex.app/Contents/Resources/codex app-server --listen stdio://"}}
|
||||
}
|
||||
a := codex(home)
|
||||
first := a.StaleCopies()
|
||||
if len(first) != 1 {
|
||||
t.Fatalf("the copy from before magpie's list: %+v", first)
|
||||
}
|
||||
for range 20 {
|
||||
time.Sleep(time.Millisecond)
|
||||
got := a.StaleCopies()
|
||||
if len(got) != 1 || !got[0].Since.Equal(first[0].Since) {
|
||||
t.Fatalf("asked again: %+v, first %+v", got, first)
|
||||
}
|
||||
}
|
||||
if d := start.Sub(first[0].Since); d < 0 || d >= time.Second {
|
||||
t.Fatalf("since %v, the copy started %v", first[0].Since, start)
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/yetone/magpie/internal/appdir"
|
||||
"github.com/yetone/magpie/internal/edit"
|
||||
"github.com/yetone/magpie/internal/settings"
|
||||
"github.com/yetone/magpie/internal/steady"
|
||||
)
|
||||
|
||||
// The last-known state (#1519, fatkun). state() reads every agent's config
|
||||
@@ -41,7 +41,10 @@ type stateFile struct {
|
||||
func lastStatePath() string { return filepath.Join(appdir.Cache(), "agents-state.json") }
|
||||
|
||||
// keepState remembers s as the last-known state. The file is written only
|
||||
// when what is kept changed.
|
||||
// when what is kept changed, and as magpie's own cache: nothing a request
|
||||
// reads is built from it, so its write doesn't drop what filememo holds
|
||||
// (edit.WriteAtomic's would: every poll of the Agents page rebuilt each
|
||||
// agent's model list and picker, and every provider's catalog, anew).
|
||||
func keepState(s stateJSON) {
|
||||
s.Settings = settings.Settings{}
|
||||
// and what is told once: a notice, a file recovered after a crash
|
||||
@@ -60,7 +63,10 @@ func keepState(s stateJSON) {
|
||||
return
|
||||
}
|
||||
lastState.kept = b
|
||||
_ = edit.WriteAtomic(lastStatePath(), b)
|
||||
p := lastStatePath()
|
||||
if os.MkdirAll(filepath.Dir(p), 0o755) == nil {
|
||||
_ = steady.WriteFile(p, b, 0o644)
|
||||
}
|
||||
}
|
||||
|
||||
// lastKnown is the last state built, by this run or the one before it,
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package gui
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/filememo"
|
||||
)
|
||||
|
||||
// The last-known state is magpie's own cache, and nothing a request reads
|
||||
// is built from it. Its write dropped every file filememo held, so each
|
||||
// poll of the Agents page that wrote it had each agent's model list and
|
||||
// picker, and every provider's catalog, built anew on the next look.
|
||||
func TestKeptStateLeavesFilememo(t *testing.T) {
|
||||
sandboxHome(t)
|
||||
forgetLastState()
|
||||
t.Cleanup(forgetLastState)
|
||||
gen := filememo.Gen()
|
||||
keepState(stateJSON{Agents: []agentJSON{{ID: "a1"}}})
|
||||
keepState(stateJSON{Agents: []agentJSON{{ID: "a2"}}})
|
||||
if filememo.Gen() != gen {
|
||||
t.Fatal("writing the last-known state made filememo read every file again")
|
||||
}
|
||||
forgetLastState()
|
||||
if st, ok := lastKnown(); !ok || len(st.Agents) != 1 || st.Agents[0].ID != "a2" {
|
||||
t.Fatalf("the state kept last is not the one read back: %+v %v", st.Agents, ok)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user