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).
This commit is contained in:
yetone
2026-10-26 11:48:48 +08:00
parent 250adf5ec5
commit c158b0439c
2 changed files with 80 additions and 4 deletions
+27 -4
View File
@@ -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() {
+53
View File
@@ -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())
}
}