Skip to content

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

sessions: the index is written at most once a minute, not after every poll

yetonea87f051974d2
diff --git a/internal/sessions/main_test.go b/internal/sessions/main_test.go
index 20cc448aa..ed5d10482 100644
--- a/internal/sessions/main_test.go
+++ b/internal/sessions/main_test.go
@@ -11,5 +11,8 @@ import (
// a line past its scan fails here rather than for a user.
func TestMain(m *testing.M) {
poisonReleased = true
+ // a test's saves are written as they come; TestSaveWaitsOutTheGap
+ // sets the gap
+ saveGap = 0
testenv.Main(m)
}
diff --git a/internal/sessions/save_gap_test.go b/internal/sessions/save_gap_test.go
new file mode 100644
index 000000000..adcbef270
--- /dev/null
+++ b/internal/sessions/save_gap_test.go
@@ -0,0 +1,45 @@
+package sessions
+
+import (
+ "sync/atomic"
+ "testing"
+ "time"
+)
+
+// A save right after another waits out saveGap rather than writing the
+// whole index again (each Sessions poll wrote it), and Saved writes it at
+// once, not after the gap.
+func TestSaveWaitsOutTheGap(t *testing.T) {
+ setup(t)
+ var writes atomic.Int32
+ old := saveGap
+ saveGap = time.Hour
+ saveHook = func() { writes.Add(1) }
+ t.Cleanup(func() { saveGap, saveHook = old, nil })
+ saved()
+ saving.Lock()
+ saving.wrote = time.Time{}
+ saving.Unlock()
+
+ saveCache()
+ saved()
+ if n := writes.Load(); n != 1 {
+ t.Fatalf("%d writes of the first save, want 1", n)
+ }
+ saveCache()
+ saveCache()
+ time.Sleep(50 * time.Millisecond) // room for a write the gap should hold back
+ if n := writes.Load(); n != 1 {
+ t.Fatalf("%d writes with saveGap not yet past, want the first only", n)
+ }
+ done := make(chan struct{})
+ go func() { saved(); close(done) }()
+ select {
+ case <-done:
+ case <-time.After(10 * time.Second):
+ t.Fatal("Saved waited out the gap")
+ }
+ if n := writes.Load(); n != 2 {
+ t.Fatalf("%d writes once Saved, want the two saves in a row folded into one", n)
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 65fe779ff..652f7b2e1 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -936,7 +936,8 @@ func loadCache() {
// for a long history) takes a second or more, so it is done behind the
// request: the parses in the cache are never changed once put there (a new
// parse starts from a clone), so a copy of the map is a snapshot; saves in a
-// row are folded into the latest.
+// row are folded into the latest, and one is written saveGap after the
+// last at the soonest.
func saveCache() {
snap := save{cacheFile{Version: cacheVersion, Zone: zone(), Files: maps.Clone(cache)}, CachePath()}
saving.Lock()
@@ -948,26 +949,58 @@ func saveCache() {
saving.running = true
go func() {
for {
+ saving.Lock()
+ wait := saveGap - time.Since(saving.wrote)
+ if saving.hurry {
+ wait = 0
+ }
+ saving.Unlock()
+ if wait > 0 {
+ select {
+ case <-time.After(wait):
+ case <-saveNow:
+ }
+ }
saving.Lock()
f := saving.next
saving.next = nil
if f == nil {
- saving.running = false
+ saving.running, saving.hurry = false, false
+ select { // a Saved's wake-up, unused, cuts no later gap
+ case <-saveNow:
+ default:
+ }
saving.Broadcast()
saving.Unlock()
return
}
saving.Unlock()
writeCache(f)
+ saving.Lock()
+ saving.wrote = time.Now()
+ saving.Unlock()
}
}()
}
+// saveGap is how long a save waits after the last one written. A Sessions
+// or Usage page polls every 15 s, and a live session changes its file
+// between any two, so each poll wrote the whole index again: tens of MB
+// for a long history, ~20 MB allocated to encode it each time. The index
+// only spares the next start a read; what a save not yet written misses is
+// read again then. Saved does not wait it out.
+var saveGap = time.Minute
+
+// saveNow wakes a save waiting out saveGap once saving.hurry is set.
+var saveNow = make(chan struct{}, 1)
+
var saving struct {
sync.Mutex
*sync.Cond
running bool
next *save
+ wrote time.Time // when the last save was written
+ hurry bool // Saved is waiting: no save waits out saveGap
}
type save struct {
@@ -981,9 +1014,16 @@ func init() { saving.Cond = sync.NewCond(&saving.Mutex) }
// it on disk.
func Saved() { saved() }
-// saved waits for the saves under way.
+// saved waits for the saves under way, written without their saveGap.
func saved() {
saving.Lock()
+ if saving.running {
+ saving.hurry = true
+ select {
+ case saveNow <- struct{}{}:
+ default:
+ }
+ }
for saving.running {
saving.Wait()
}