/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.
58 lines
2.1 KiB
Go
58 lines
2.1 KiB
Go
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)
|
|
}
|
|
}
|