From 12d3db7332db81e1a6edd8d0e35d0d71b29f4f0d Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 15 Oct 2026 15:47:17 +0800 Subject: [PATCH] provider: the Codex daemon check lists processes only when a pid file is behind, not a PowerShell run every 15s on Windows (#2341) KeepCodexDaemonCurrent runs every codexDaemonEvery (15s, internal/agent/ codex_daemon.go) for as long as magpie runs, and listed every process first. On Windows that is `powershell Get-CimInstance Win32_Process`, a cold start each time, which is the run the reporter saw over and over. A daemon is only ever found behind through a pid file written before the change (daemonStarted), so with no pid file, or only newer ones, the answer was always "nothing to do". pidFileBehind checks that first, with the same one-second slack, and processes are listed only when it can matter. Run: go test -p 1 -tags nogui -count=1 ./internal/provider -run 'TestKeepCodexDaemon|CodexDaemon' (ok, macOS, sandbox HOME). TestKeepCodexDaemonCurrentListsOnlyWhenBehind counts listProcesses calls: none with no pid file or a newer one, one with an older one. Broken by hand: codex_daemon.go put back to 03eb0633a -> the test fails ("processes listed 2 times with no daemon behind"). GOOS=windows and GOOS=linux go vet -tags nogui ./internal/provider ./internal/agent ./internal/wslrun: clean; gofmt clean. Not tried on Windows. Full suite: SUITE_LINE --- internal/provider/codex_daemon.go | 16 ++++++++++++- internal/provider/codex_daemon_test.go | 31 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/internal/provider/codex_daemon.go b/internal/provider/codex_daemon.go index 45f469758..7d5b70504 100644 --- a/internal/provider/codex_daemon.go +++ b/internal/provider/codex_daemon.go @@ -362,7 +362,7 @@ var codexRestarting sync.Mutex // be found or timed, or a restart already under way, is left as it is. func KeepCodexDaemonCurrent(ctx context.Context, home string, changed time.Time) CodexDaemonCheck { var c CodexDaemonCheck - if changed.IsZero() { + if changed.IsZero() || !pidFileBehind(home, changed) { return c } ps, err := listProcesses(ctx) @@ -390,6 +390,20 @@ func KeepCodexDaemonCurrent(ctx context.Context, home string, changed time.Time) return c } +// pidFileBehind reports whether a pid file of home's was written before +// changed: only a daemon one names can be found behind (daemonStarted), so +// with none the processes aren't listed at all. Listing them is a +// PowerShell run on Windows, which every look did every codexDaemonEvery +// for as long as magpie ran (#2341). +func pidFileBehind(home string, changed time.Time) bool { + for _, pid := range daemonPIDs(home) { + if since, ok := daemonStarted(home, pid); ok && since.Before(changed.Add(-time.Second)) { + return true + } + } + return false +} + // daemonStarted is when the daemon pid started: the time Codex wrote its // pid file, which it does once, as the daemon comes up, on every OS. ok is // false when no pid file of home's names pid. diff --git a/internal/provider/codex_daemon_test.go b/internal/provider/codex_daemon_test.go index 3be3da8bc..511dac3c4 100644 --- a/internal/provider/codex_daemon_test.go +++ b/internal/provider/codex_daemon_test.go @@ -385,6 +385,37 @@ func TestKeepCodexDaemonCurrent(t *testing.T) { } } +// The processes are listed only when a pid file says a daemon started +// before the change: listing them is a PowerShell run on Windows, which +// the look every 15s made for as long as magpie ran (#2341). +func TestKeepCodexDaemonCurrentListsOnlyWhenBehind(t *testing.T) { + pid := fakeDaemon(t) + fakeCodex(t, 0) + home, _ := os.UserHomeDir() + home = filepath.Join(home, ".codex") + lists := 0 + listProcesses = func(context.Context) ([]proc.Process, error) { + lists++ + return nil, nil + } + ctx := context.Background() + *pid = 50 + // no pid file: no daemon to be behind + KeepCodexDaemonCurrent(ctx, home, time.Now()) + // one written after the change: it has the list + started := time.Now().Add(-time.Hour).Truncate(time.Second) + writeDaemonPID(t, home, "50", started) + KeepCodexDaemonCurrent(ctx, home, started.Add(-time.Minute)) + if lists != 0 { + t.Fatalf("processes listed %d times with no daemon behind", lists) + } + // one written before the change: listed, to see it runs + KeepCodexDaemonCurrent(ctx, home, time.Now()) + if lists != 1 { + t.Fatalf("processes listed %d times for a daemon behind, want 1", lists) + } +} + // A failed restart is said, not taken for one done. func TestKeepCodexDaemonCurrentFails(t *testing.T) { pid := fakeDaemon(t)