Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

provider: retired.json is looked at on disk once a second, not on every request answered

yetone8fd74d15bfee
diff --git a/internal/provider/retired.go b/internal/provider/retired.go
index 7251aebb4..42cfe0619 100644
--- a/internal/provider/retired.go
+++ b/internal/provider/retired.go
@@ -33,8 +33,17 @@ var retired struct {
at map[string]time.Time // "provider/model" → when the vendor said it is retired
from string // the file read, and its time then
mod time.Time
+ // checked is when the file was last looked at: within retiredLook it
+ // isn't looked at again. Every answered request asks Unretire, and a
+ // look at the disk each time was a stat per request; this process's
+ // own writes keep retired.at current, and another magpie's is seen
+ // within retiredLook.
+ checked time.Time
+ looks int // looks at the disk, for tests
}
+var retiredLook = time.Second
+
func retiredPath() string { return filepath.Join(filepath.Dir(Path()), "retired.json") }
// readRetired brings retired.at up to the file, when the file changed since
@@ -42,6 +51,11 @@ func retiredPath() string { return filepath.Join(filepath.Dir(Path()), "retired.
// retired held.
func readRetired() {
path := retiredPath()
+ if path == retired.from && !retired.checked.IsZero() && time.Since(retired.checked) < retiredLook {
+ return
+ }
+ retired.looks++
+ retired.checked = time.Now()
fi, err := os.Stat(path)
if err != nil {
if path != retired.from {
@@ -81,7 +95,7 @@ func writeRetired() {
return
}
if fi, err := os.Stat(path); err == nil {
- retired.from, retired.mod = path, fi.ModTime()
+ retired.from, retired.mod, retired.checked = path, fi.ModTime(), time.Now()
}
}
diff --git a/internal/provider/retired_test.go b/internal/provider/retired_test.go
new file mode 100644
index 000000000..363272459
--- /dev/null
+++ b/internal/provider/retired_test.go
@@ -0,0 +1,85 @@
+package provider
+
+import (
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+func retiredLooks() int {
+ retired.Lock()
+ defer retired.Unlock()
+ return retired.looks
+}
+
+// Every answered request asks Unretire (the gateway's servedCandidate):
+// retired.json isn't looked at on disk for each, only once in retiredLook.
+// What this process retires or brings back is told at once, another
+// magpie's write within retiredLook, and a config path of its own (a new
+// HOME) is read at once.
+func TestRetiredLooksOnceInAWhile(t *testing.T) {
+ home := t.TempDir()
+ t.Setenv("HOME", home)
+ t.Setenv("USERPROFILE", home)
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ before := retiredLooks()
+ for range 200 {
+ Unretire("p", "m")
+ if Retired("p", "m") {
+ t.Fatal("retired out of nothing")
+ }
+ }
+ if n := retiredLooks() - before; n > 2 {
+ t.Fatalf("%d looks at retired.json for 200 requests", n)
+ }
+ Retire("p", "m")
+ if !Retired("p", "m") {
+ t.Fatal("this process's retirement isn't told at once")
+ }
+ Unretire("p", "m")
+ if Retired("p", "m") {
+ t.Fatal("this process's unretirement isn't told at once")
+ }
+
+ // another magpie's write
+ defer func(d time.Duration) { retiredLook = d }(retiredLook)
+ retiredLook = 20 * time.Millisecond
+ b, _ := json.Marshal(map[string]time.Time{"p/other": time.Now()})
+ if err := os.MkdirAll(filepath.Dir(retiredPath()), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(retiredPath(), b, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ later := time.Now().Add(time.Minute)
+ os.Chtimes(retiredPath(), later, later)
+ for deadline := time.Now().Add(10 * time.Second); !Retired("p", "other"); time.Sleep(5 * time.Millisecond) {
+ if time.Now().After(deadline) {
+ t.Fatal("another magpie's write isn't seen")
+ }
+ }
+
+ // a config of its own is read at once
+ home2 := t.TempDir()
+ t.Setenv("HOME", home2)
+ t.Setenv("USERPROFILE", home2)
+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())
+ retiredLook = time.Hour
+ if Retired("p", "other") {
+ t.Fatal("another config's retirement told")
+ }
+}
+
+// BenchmarkUnretire is what an answered request's Unretire costs it.
+func BenchmarkUnretire(b *testing.B) {
+ home := b.TempDir()
+ b.Setenv("HOME", home)
+ b.Setenv("USERPROFILE", home)
+ b.Setenv("XDG_CONFIG_HOME", b.TempDir())
+ Retire("p", "x")
+ for b.Loop() {
+ Unretire("p", "m")
+ }
+}