Skip to content

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

sessions: stats asked again unchanged are not encoded again; lists made at their size (#2278)

yetone7db0810b03dc
diff --git a/internal/gui/api.go b/internal/gui/api.go
index dddf14e2e..0152731c0 100644
--- a/internal/gui/api.go
+++ b/internal/gui/api.go
@@ -2703,11 +2703,12 @@ func writeJSON(rw http.ResponseWriter, v any) {
// history's ~8 MB list and ~0.7 MB of stats every 15 s, and parsed and
// compared them whole to find them the same. Not an ETag and a 304:
// WebView2 is sent no If-None-Match, and Wails turns its 304 into a 505.
-func writeJSONSame(rw http.ResponseWriter, r *http.Request, v any) {
+// It returns the tag, or "" when v could not be written.
+func writeJSONSame(rw http.ResponseWriter, r *http.Request, v any) string {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(v); err != nil {
fail(rw, err)
- return
+ return ""
}
sum := sha256.Sum256(b.Bytes())
tag := hex.EncodeToString(sum[:16])
@@ -2715,10 +2716,11 @@ func writeJSONSame(rw http.ResponseWriter, r *http.Request, v any) {
if r.Header.Get("X-Magpie-Had") == tag {
rw.Header().Set("X-Magpie-Same", "1")
rw.WriteHeader(http.StatusOK)
- return
+ return tag
}
rw.Header().Set("Content-Type", "application/json")
_, _ = rw.Write(b.Bytes())
+ return tag
}
// sentRows is each row's hash in the last few lists writeJSONRows sent, by
diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go
index bdce714c6..c1ae32b6a 100644
--- a/internal/gui/sessions.go
+++ b/internal/gui/sessions.go
@@ -5,14 +5,17 @@ import (
"cmp"
"encoding/json"
"errors"
+ "maps"
"net/http"
"os"
+ "reflect"
"runtime"
"runtime/debug"
"slices"
"strconv"
"strings"
"sync"
+ "sync/atomic"
"testing"
"time"
@@ -162,7 +165,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
mux.HandleFunc("GET /api/sessions/stats", func(rw http.ResponseWriter, r *http.Request) {
defer sessions.ShareListing()()
n, _ := strconv.Atoi(r.URL.Query().Get("days"))
- st := statsFor(n)
+ st, gen := statsForGen(n)
names := map[string]string{}
for _, a := range agent.Clients() {
names[a.ID] = a.SharedName()
@@ -176,10 +179,29 @@ func sessionRoutes(mux *http.ServeMux, w Windows) {
agents[a.Agent] = cmp.Or(names[a.Agent], a.Agent)
}
}
- writeJSONSame(rw, r, struct {
+ // the page has this very answer: told so without encoding and
+ // hashing a long history's ~6 MB of it again on every poll (#2278)
+ statsTags.Lock()
+ t, ok := statsTags.m[n]
+ statsTags.Unlock()
+ if ok && t.gen == gen && maps.Equal(t.agents, agents) && r.Header.Get("X-Magpie-Had") == t.tag {
+ rw.Header().Set("X-Magpie-Tag", t.tag)
+ rw.Header().Set("X-Magpie-Same", "1")
+ rw.WriteHeader(http.StatusOK)
+ return
+ }
+ tag := writeJSONSame(rw, r, struct {
sessions.Stats
Agents map[string]string `json:"agents"`
}{st, agents})
+ if tag != "" {
+ statsTags.Lock()
+ if statsTags.m == nil || len(statsTags.m) >= statsMemoMost {
+ statsTags.m = map[int]statsTag{}
+ }
+ statsTags.m[n] = statsTag{gen, agents, tag}
+ statsTags.Unlock()
+ }
})
// overview sums up the range's sessions under the page's filters:
// how many, what the middle one spent, how many each day, and the ones
@@ -462,6 +484,7 @@ var statsMemo struct {
type statsKept struct {
at time.Time
st sessions.Stats
+ gen uint64 // st's, new only when a read found st changed
busy bool
// used is the memo's ask count when this range was last asked: a count,
// not a clock, since Windows' clock can give two asks the same time.
@@ -473,7 +496,36 @@ type statsKept struct {
// least recently asked one is let go when a new value comes past it.
const statsMemoMost = 8
+// statsGen numbers each Stats the memo keeps, across memos: an answer's
+// tag is known by it (statsTags).
+var statsGen atomic.Uint64
+
+// statsTags is the tag of the stats answer last sent for each ?days, the
+// Stats it was made from known by its gen.
+var statsTags struct {
+ sync.Mutex
+ m map[int]statsTag
+}
+
+type statsTag struct {
+ gen uint64
+ agents map[string]string
+ tag string
+}
+
+// sameStats is whether a and b would be sent the same: a read again
+// behind finds the same stats most times, and keeping a's gen spares the
+// page's next ask the encoding.
+func sameStats(a, b sessions.Stats) bool {
+ return a.From == b.From && a.To == b.To && slices.Equal(a.Keys, b.Keys) && reflect.DeepEqual(a.Days, b.Days)
+}
+
func statsFor(days int) sessions.Stats {
+ st, _ := statsForGen(days)
+ return st
+}
+
+func statsForGen(days int) (sessions.Stats, uint64) {
days = max(0, days)
statsMemo.Lock()
if k := statsMemo.m[days]; k != nil {
@@ -500,17 +552,20 @@ func statsFor(days int) sessions.Stats {
sts := combinedSessionStatsAll(ranges)
statsMemo.Lock()
for i, o := range kept {
- o.at, o.st, o.busy = time.Now(), sts[i], false
+ if !sameStats(o.st, sts[i]) {
+ o.st, o.gen = sts[i], statsGen.Add(1)
+ }
+ o.at, o.busy = time.Now(), false
}
statsMemo.Unlock()
}()
}
- st := k.st
+ st, gen := k.st, k.gen
statsMemo.Unlock()
- return st
+ return st, gen
}
statsMemo.Unlock()
- st := combinedSessionStats(days)
+ st, gen := combinedSessionStats(days), statsGen.Add(1)
statsMemo.Lock()
if statsMemo.m == nil {
statsMemo.m = map[int]*statsKept{}
@@ -526,10 +581,10 @@ func statsFor(days int) sessions.Stats {
delete(statsMemo.m, oldest)
}
statsMemo.asks++
- statsMemo.m[days] = &statsKept{at: time.Now(), st: st, used: statsMemo.asks}
+ statsMemo.m[days] = &statsKept{at: time.Now(), st: st, gen: gen, used: statsMemo.asks}
}
statsMemo.Unlock()
- return st
+ return st, gen
}
func combinedSessionStats(days int) sessions.Stats {
diff --git a/internal/gui/sessions_stats_gen_test.go b/internal/gui/sessions_stats_gen_test.go
new file mode 100644
index 000000000..4ede0c679
--- /dev/null
+++ b/internal/gui/sessions_stats_gen_test.go
@@ -0,0 +1,99 @@
+package gui
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "github.com/yetone/magpie/internal/sessions"
+)
+
+// A stats read again behind that finds the same stats keeps their gen, so
+// the page's next ask with its tag is answered the same without the stats
+// encoded again; stats that changed are sent whole, whatever tag was kept
+// for the old ones (#2278).
+func TestStatsTagKeptWhileTheSame(t *testing.T) {
+ h := sandboxHome(t)
+ proj := filepath.Join(h, ".claude", "projects", "-work")
+ if err := os.MkdirAll(proj, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ write := func(id, at string) {
+ b := `{"parentUuid":null,"isSidechain":false,"type":"user","message":{"role":"user","content":"hi"},"uuid":"u","timestamp":"` + at + `","cwd":"/work","sessionId":"` + id + `"}` + "\n" +
+ `{"parentUuid":"u","isSidechain":false,"message":{"model":"claude-sonnet-5","id":"msg_` + id + `","type":"message","role":"assistant","content":[{"type":"text","text":"done"}],"usage":{"input_tokens":10,"output_tokens":2}},"requestId":"req_1","type":"assistant","uuid":"a","timestamp":"` + at + `","cwd":"/work","sessionId":"` + id + `"}` + "\n"
+ if err := os.WriteFile(filepath.Join(proj, id+".jsonl"), []byte(b), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ write("00000001-1111-2222-3333-444444444444", "2026-09-20T10:00:00.000Z")
+ t.Setenv("CLAUDE_CONFIG_DIR", filepath.Join(h, ".claude"))
+ sessions.Reset()
+ forgetStats()
+ t.Cleanup(func() { sessions.Reset(); forgetStats() })
+
+ mux := http.NewServeMux()
+ sessionRoutes(mux, folderOnly{})
+ get := func(had string) *httptest.ResponseRecorder {
+ r := httptest.NewRequest("GET", "/api/sessions/stats?days=0", nil)
+ if had != "" {
+ r.Header.Set("X-Magpie-Had", had)
+ }
+ w := httptest.NewRecorder()
+ mux.ServeHTTP(w, r)
+ if w.Code != 200 {
+ t.Fatalf("%d %s", w.Code, w.Body)
+ }
+ return w
+ }
+ kept := func() (uint64, bool) {
+ statsMemo.Lock()
+ defer statsMemo.Unlock()
+ k := statsMemo.m[0]
+ return k.gen, k.busy
+ }
+ // the kept stats made old, so the next ask reads them again behind
+ age := func() {
+ statsMemo.Lock()
+ statsMemo.m[0].at = time.Now().Add(-time.Minute)
+ statsMemo.Unlock()
+ }
+ settled := func() uint64 {
+ for range 500 {
+ if gen, busy := kept(); !busy {
+ return gen
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+ t.Fatal("the read behind never ended")
+ return 0
+ }
+
+ first := get("")
+ tag := first.Header().Get("X-Magpie-Tag")
+ gen := settled()
+ age()
+ if w := get(tag); w.Header().Get("X-Magpie-Same") != "1" {
+ t.Fatalf("asked with its own tag: same %q", w.Header().Get("X-Magpie-Same"))
+ }
+ if got := settled(); got != gen {
+ t.Errorf("read again behind with nothing changed, gen %d, want %d kept", got, gen)
+ }
+ if w := get(tag); w.Header().Get("X-Magpie-Same") != "1" || w.Header().Get("X-Magpie-Tag") != tag {
+ t.Errorf("after the read behind, asked with its own tag: same %q, tag %q", w.Header().Get("X-Magpie-Same"), w.Header().Get("X-Magpie-Tag"))
+ }
+
+ write("00000002-1111-2222-3333-444444444444", "2026-09-21T10:00:00.000Z")
+ sessions.Reset()
+ age()
+ get(tag) // starts the read behind; answered from the kept stats
+ if got := settled(); got == gen {
+ t.Fatalf("a new session read behind kept gen %d", got)
+ }
+ w := get(tag)
+ if w.Header().Get("X-Magpie-Same") != "" || w.Body.Len() == 0 || w.Header().Get("X-Magpie-Tag") == tag {
+ t.Errorf("after the stats changed, asked with the old tag: same %q, %d bytes, tag %q", w.Header().Get("X-Magpie-Same"), w.Body.Len(), w.Header().Get("X-Magpie-Tag"))
+ }
+}
diff --git a/internal/sessions/external.go b/internal/sessions/external.go
index 04650f846..f16b03d48 100644
--- a/internal/sessions/external.go
+++ b/internal/sessions/external.go
@@ -176,6 +176,18 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe
}
}
names := desktopExternalTitles(external)
+ // grown once for every external: a Clip and then an append a session
+ // copied the whole list again and again, ~110 MB a minute of polls on a
+ // full history (#2278)
+ n := 0
+ for _, e := range external {
+ if e.ID != "" && e.Agent != "" {
+ n++
+ }
+ }
+ if n > 0 {
+ st.Sessions, sessionsOwn = slices.Grow(slices.Clip(st.Sessions), n), true
+ }
for _, e := range external {
if e.ID == "" || e.Agent == "" {
continue
@@ -207,9 +219,6 @@ func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSe
s.Models = append(s.Models, m.Model)
s.Priced = s.Priced && m.Priced
}
- if !sessionsOwn {
- st.Sessions, sessionsOwn = slices.Clip(st.Sessions), true
- }
st.Sessions = append(st.Sessions, s)
}
sort.Slice(st.Days, func(i, j int) bool { return st.Days[i].Date < st.Days[j].Date })
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 906527336..a3f40e3a3 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -1455,7 +1455,7 @@ func listSessions(limit int) []Session {
defer useDBs()()
files := allFiles()
groups := byKey(files, nil)
- latest := map[string]time.Time{}
+ latest := make(map[string]time.Time, len(groups))
for _, f := range files {
if f.mod.After(latest[f.key]) {
latest[f.key] = f.mod
@@ -1485,7 +1485,8 @@ func listSessions(limit int) []Session {
copyIDs := copiesByKeeper(copies, groups)
price, titles := pricer(), codexTitleOf()
- out := []Session{}
+ // made for the page at once, not grown an append at a time (#2278)
+ out := make([]Session, 0, min(limit, len(keys)))
for _, k := range keys {
if copies[k] != "" {
continue