diff --git a/internal/agent/connectinfo.go b/internal/agent/connectinfo.go index 7dd38d2f8..d313d7355 100644 --- a/internal/agent/connectinfo.go +++ b/internal/agent/connectinfo.go @@ -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 diff --git a/internal/agent/stale_since_test.go b/internal/agent/stale_since_test.go new file mode 100644 index 000000000..e70ce6c65 --- /dev/null +++ b/internal/agent/stale_since_test.go @@ -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) + } +} diff --git a/internal/gui/laststate.go b/internal/gui/laststate.go index 71c11d1ee..64c894ab0 100644 --- a/internal/gui/laststate.go +++ b/internal/gui/laststate.go @@ -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, diff --git a/internal/gui/laststate_gen_test.go b/internal/gui/laststate_gen_test.go new file mode 100644 index 000000000..4e7b5f906 --- /dev/null +++ b/internal/gui/laststate_gen_test.go @@ -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) + } +}