diff --git a/internal/agent/codex_daemon.go b/internal/agent/codex_daemon.go index 43cd8cff5..e99c360a8 100644 --- a/internal/agent/codex_daemon.go +++ b/internal/agent/codex_daemon.go @@ -9,6 +9,7 @@ import ( "time" "github.com/yetone/magpie/internal/beat" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/provider" ) @@ -73,32 +74,54 @@ func codexDaemonNotice(path string) (string, bool) { return "", false } -// codexDaemonEvery is how often KeepCodexDaemonCurrent looks. +// codexDaemonEvery is how often KeepCodexDaemonCurrent looks while the +// daemon is behind, for the codex sessions on it to end. const codexDaemonEvery = 15 * time.Second // KeepCodexDaemonCurrent restarts this machine's Codex daemon whenever it // is behind what Codex reads at start and no codex session is on it, -// looking every so often until ctx ends. +// until ctx ends. It looks again when magpie writes a file (what Codex +// reads is among them), every so often while the daemon is behind, and +// otherwise only now and then, for another process's write (#3185: a +// look every 15s woke an idle magpie). func KeepCodexDaemonCurrent(ctx context.Context) { home, err := os.UserHomeDir() if err != nil { return } path := filepath.Join(here(home).codexHome(), "config.toml") + every, written := codexDaemonEvery, filememo.Written() for { select { case <-ctx.Done(): return - case <-beat.Next(codexDaemonEvery): + case <-written: + case <-beat.Next(every): } + // taken before the look, so a write made during it isn't missed + written = filememo.Written() + var c provider.CodexDaemonCheck if _, err := os.Stat(path); err == nil { - if c := codexDaemonCheck(path); c.Restarted { + if c = daemonCheck(path); c.Restarted { log.Println("codex app-server: restarted, it had the model list from before", c.Since.Format(time.RFC3339)) } } + every = codexDaemonNext(c) } } +// daemonCheck is codexDaemonCheck; a test stands in for it. +var daemonCheck = codexDaemonCheck + +// codexDaemonNext is how long KeepCodexDaemonCurrent waits, with nothing +// written, after a look that found c. +func codexDaemonNext(c provider.CodexDaemonCheck) time.Duration { + if c.Behind && !c.Restarted { + return codexDaemonEvery + } + return filememo.OthersEvery() +} + // CodexDaemonRestarted forgets the daemon last found behind, once the user // has restarted it. func CodexDaemonRestarted() { diff --git a/internal/agent/codex_daemon_wait_test.go b/internal/agent/codex_daemon_wait_test.go new file mode 100644 index 000000000..d8e0678ef --- /dev/null +++ b/internal/agent/codex_daemon_wait_test.go @@ -0,0 +1,53 @@ +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()) + } +}