Files
magpie/internal/agent/codex_daemon_wait_test.go
T
yetone c158b0439c agent: the Codex daemon is looked at on magpie's writes, not every 15s (#3185)
KeepCodexDaemonCurrent read Codex's config.toml (four TOML reads, a hash
and the stash) every 15s for as long as magpie ran, to see whether
magpie had changed what Codex reads at start since its daemon started.
Only a write can make the daemon behind, and every one magpie makes,
Codex's config among them (edit's file writes), closes filememo.Written.
The loop now waits on that, looks once a minute (OthersEvery) for another
process's write, and every 15s only while the daemon is behind, for the
codex sessions on it to end. codexDaemonBehind's 45s freshness still
holds, since a behind daemon is still looked at every 15s.

Run at this head (rebased on origin/main):
- go test -timeout 45m ./internal/agent ok (212s).
- TestCodexDaemonLooksOnWrites -count=3 ok; red with the loop not
  waiting on Written ("looked at 0 times in 5s").
- gofmt clean; GOOS=linux / windows go vet -tags nogui ./internal/agent ok.
- Real gateway, sandbox HOME, idle, per-second wakeups over 120s after a
  90s warm-up: v0.1.1564 1.6/s with a burst every ~15s, this 1.4/s with
  no 15s period left (the sandbox has no Codex config, so this is the
  timer alone; with Codex set up each look also read its files).
2026-10-26 11:48:48 +08:00

54 lines
1.9 KiB
Go

package agent
import (
"context"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/filememo"
"github.com/yetone/magpie/internal/provider"
)
// KeepCodexDaemonCurrent looks at the daemon when magpie writes a file
// (what Codex reads at start among them), every 15s only while the daemon
// is behind, and otherwise only now and then (#3185: a look every 15s woke
// an idle magpie).
func TestCodexDaemonLooksOnWrites(t *testing.T) {
codexHome(t, "", `model = "gpt-5"`)
was := daemonCheck
var mu sync.Mutex
looks := 0
looked := func() int { mu.Lock(); defer mu.Unlock(); return looks }
daemonCheck = func(string) provider.CodexDaemonCheck {
mu.Lock()
looks++
mu.Unlock()
return provider.CodexDaemonCheck{}
}
t.Cleanup(func() { daemonCheck = was })
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { KeepCodexDaemonCurrent(ctx); close(done) }()
defer func() { cancel(); <-done }()
// written again until it is seen: the loop may not be waiting yet, and
// a look made only on its 15s beat is what this guards against
for n := 1; n <= 2; n++ {
for end := time.Now().Add(5 * time.Second); looked() < n; time.Sleep(10 * time.Millisecond) {
if time.Now().After(end) {
t.Fatalf("magpie's writes had the daemon looked at %d times in 5s, want %d", looked(), n)
}
filememo.Forget()
}
}
if d := codexDaemonNext(provider.CodexDaemonCheck{}); d != filememo.OthersEvery() {
t.Errorf("with the daemon current the next look is in %v, want %v", d, filememo.OthersEvery())
}
if d := codexDaemonNext(provider.CodexDaemonCheck{Behind: true, Attached: 1}); d != codexDaemonEvery {
t.Errorf("with the daemon behind the next look is in %v, want %v", d, codexDaemonEvery)
}
if d := codexDaemonNext(provider.CodexDaemonCheck{Behind: true, Restarted: true}); d != filememo.OthersEvery() {
t.Errorf("with the daemon restarted the next look is in %v, want %v", d, filememo.OthersEvery())
}
}