Files
magpie/internal/agent/connect_stale_test.go
T
yetone 359831951f agent: 重开 Codex 后生效 goes once Codex is reopened, whatever Codex itself writes to config.toml after, and a Codex daemon of another home isn't counted (#729, Sun1090 on Codex 0.160.0: the Codex row stayed "已接入 · 重开 Codex 后生效" after ChatGPT and magpie were both restarted) — Agent.Stale counted every process matching (^|/)codex( |$) that started over a second before config.toml's or magpie-models.json's mtime, and Codex writes config.toml itself (each project it is opened in gets a [projects."…"] trust_level, a notice seen, the model picked in its app; this Mac's has 10919 project tables), so a Codex reopened after magpie's change was stale again at its first such write, as was one that never missed anything; and the pattern also took the app-server daemons Codex 0.160 leaves running with ppid 1 for another CODEX_HOME (<home>/.codex/packages/app-server-daemon/…/codex app-server --managed-daemon, days old on this Mac). Stale now compares a copy's start with when magpie last changed what the agent reads at start: a digest of it (Codex: model_provider, model_catalog_json, openai_base_url, the [model_providers.magpie] table and magpie-models.json; Claude Code: env and model in settings.json) is kept in the stash with the files' latest write at the time it was first seen, so neither the agent's own writes nor magpie starting again move it; a magpie change (a new list, connecting) still gives a new time and the notice. A process whose command runs from another home's .codex (or .claude) dir is skipped. pgrep/ps sit behind a var (running), ps now giving etime and command. TestCodexStaleOnlyForMagpiesChange (ChatGPT's Codex reopened an hour ago and another home's daemon: 0; Codex trusting a project: 0; magpie's list changed: 1, and 1 again from a fresh Agent) fails on the old logic at both the daemon and Codex's own write, and passes. go vet ./internal/..., builds (darwin -tags nogui, GOOS=linux, GOOS=windows internal/agent) and go test -tags nogui ./internal/agent pass (its TestMain sandboxes HOME; the HOME override itself was refused here). Not tried in the reporter's app: a Codex already flagged clears once it is reopened after this build has read the digest, one more reopen at most after updating.
2026-10-04 07:47:06 +08:00

67 lines
2.5 KiB
Go

package agent
import (
"os"
"path/filepath"
"testing"
"time"
)
// Codex writes config.toml itself (a project's trust, a notice seen), so a
// copy reopened after magpie's change and then writing there was still
// told to reopen; an app-server daemon left running for another
// CODEX_HOME was counted too (#729). Only magpie's own change counts, for
// copies of this home's Codex.
func TestCodexStaleOnlyForMagpiesChange(t *testing.T) {
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 = \"" + 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)
then := time.Now().Add(-2 * time.Hour)
os.Chtimes(cfg, then, then)
os.Chtimes(list, then, then)
was := running
t.Cleanup(func() { running = was })
running = func(string) []runningProc {
return []runningProc{
// the ChatGPT app's Codex, reopened an hour ago
{time.Hour, "/Applications/ChatGPT.app/Contents/Resources/codex app-server --listen stdio://"},
// a daemon another CODEX_HOME's Codex left running
{3 * time.Hour, "/tmp/sb/.codex/packages/app-server-daemon/releases/0.160.0/bin/codex app-server --listen unix:// --managed-daemon"},
}
}
a := codex(home)
if n := a.Stale(); n != 0 {
t.Fatalf("reopened after magpie's change, other home's daemon: %d stale, want 0", n)
}
// Codex trusts a project: config.toml written now, magpie's part as it was
f, _ := os.OpenFile(cfg, os.O_APPEND|os.O_WRONLY, 0)
f.WriteString("\n[projects.\"/Users/x/work\"]\ntrust_level = \"trusted\"\n")
f.Close()
if n := a.Stale(); n != 0 {
t.Fatalf("Codex's own write: %d stale, want 0", n)
}
// magpie's list changes: the copy running from before has the old one
os.WriteFile(list, []byte(`{"models":[{"slug":"magpie/gpt-6-luna"},{"slug":"magpie/new"}]}`), 0o644)
if n := a.Stale(); n != 1 {
t.Fatalf("magpie's list changed: %d stale, want 1", n)
}
// and stays so while nothing else changes, magpie restarted or not
if n := codex(home).Stale(); n != 1 {
t.Fatalf("asked again: %d stale, want 1", n)
}
}