Files
magpie/internal/sessions/list_kept.go
T
yetone 344a59f9db gui: an unchanged Sessions poll answers Same without making the rows
The Sessions tab asks for every session each 15 s. When nothing had
changed, the handler still copied the kept listing, made a row of each
session, merged the gateway's sessions, read Codex's effort and the
vias, encoded every row and hashed them, only to answer X-Magpie-Same.
Over ~17k real sessions (69 GB of Claude Code and Codex history) that
was ~140 ms of CPU and ~24 MB of garbage per poll.

Now the handler keeps, per ?limit, what the last list it sent was made
from: the sessions listing (sessions.ListUnless's number, which changes
whenever the listing is made again, and at least once a minute), the
usage log's version (usage.LogVersion), the user's usage aliases, and
the agents' names and icons with the folders. An ask whose
X-Magpie-Had is that list's tag, while all of these stand, gets
X-Magpie-Same before any row is made or the listing copied. Anything
else a row shows (a Codex thread's effort, a gateway title) follows
within the minute the kept listing stands. writeJSONRows returns the
tag it sent.

Measured on the real history (a sandbox HOME with APFS clones of
~/.claude/projects and ~/.codex/sessions, 17,140 files), 30 polls of
/api/sessions + stats + overview, the same script for both builds:
  before (d614f2172): 4.19 s CPU, /api/sessions median 64 ms, 711 MB
    allocated
  after:              2.67 s CPU, median 47 ms, 298 MB allocated
What is left is the listing's own look at every file (getattrlistbulk,
~43% of the samples) and its GC.

Tests: TestSessionsSameWithoutRows: an unchanged poll is Same without
rows, and a gateway call, an alias and a new session file each make the
rows again. Broken by hand on mote: never taking the shortcut fails it
("rows made 1 times"), and leaving the log version out of the key fails
it ("after a gateway call: same true"). go vet and go test of
internal/gui, internal/sessions and internal/usage on mote (macOS):
sessions and usage ok; gui's reds were TestMainWindowCorners (red on
origin/main b646a9127 too, #3949) and the BusyLoad guard (#4739, fixed
by b646a9127; passes after the rebase onto 403323af7). GOOS=linux go
vet of sessions and usage, GOOS=windows of all three.

Not run: the full ./... suite, a Linux vet of internal/gui (cgo), the GUI suite (the page's api() and its
X-Magpie-Had handling are unchanged), Windows and Linux.
2026-11-01 15:27:49 +08:00

108 lines
3.6 KiB
Go

package sessions
import (
"slices"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// keptList is the listing listSessions last made, and what it was made
// from. The Sessions page lists every session every 15 s, and between two
// polls the files mostly haven't changed, while each listing grouped,
// sorted and assembled every one of them again: over ~17k real sessions
// ~25 ms and ~12 MB of a poll, before the page's own copies. Guarded by mu.
var keptList struct {
files []keptFile
gen uint64
limit int
at time.Time
out []Session
// the answers the listing got from the prices, Codex's names and
// Claude Desktop's, asked again before it is given out
prices map[string]*catalog.Price
titles map[string]string
desktop map[string]string
// the command an agy row resumes with (AgyStart), when one is listed:
// it changes when agy is wired to magpie's gateway or let go of
agy bool
agyStart string
made int // listings assembled: ListUnless's number for the one kept
}
// listKeptFor is how long a kept listing stands at most. What a row shows
// beside its files' parses, the prices and the names (whether an agent to
// carry it to is installed, a Claude Code subagent's meta, a Kimi wire
// file) is looked at again within it.
var listKeptFor = time.Minute
// keptListing is the kept listing when it was made of files as they are
// now, at limit, with the prices and names it got still the same. It is
// the kept slice itself: a caller copies it before writing rows of it
// (List puts the aliases in).
func keptListing(files []file, limit int, price func(string) *catalog.Price, titles func(string) string) ([]Session, bool) {
k := &keptList
if k.out == nil || k.gen != cacheGeneration || k.limit != limit || time.Since(k.at) >= listKeptFor ||
!keptStill(k.files, files, k.prices, k.titles, k.desktop, price, titles) || k.agy && agyStart() != k.agyStart {
forgetKeptList()
return nil, false
}
return k.out, true
}
// keepListing starts what keepList keeps: price and titles answer as they
// did, and note the answers.
func keepListing(files []file, limit int, price func(string) *catalog.Price, titles func(string) string) (func(string) *catalog.Price, func(string) string) {
k := &keptList
k.made++
k.files, k.gen, k.limit, k.at = make([]keptFile, len(files)), cacheGeneration, limit, time.Now()
for i, f := range files {
k.files[i] = keptFileOf(f)
}
k.prices, k.titles, k.desktop = map[string]*catalog.Price{}, map[string]string{}, map[string]string{}
return func(model string) *catalog.Price {
p := price(model)
k.prices[model] = p
return p
}, func(id string) string {
t := titles(id)
k.titles[id] = t
return t
}
}
// keepList keeps out, the listing keepListing was started for, with the
// Claude Desktop names its sessions got.
func keepList(out []Session) {
var ids []string
for _, s := range out {
if s.Agent == "claude-desktop" && s.WSL == "" {
ids = append(ids, s.ID)
}
}
if len(ids) > 0 {
names := DesktopTitles(ids)
for _, id := range ids {
keptList.desktop[id] = names[id]
}
}
keptList.agy, keptList.agyStart = false, ""
if i := slices.IndexFunc(out, func(s Session) bool { return s.Agent == "agy" && s.WSL == "" }); i >= 0 {
keptList.agy, keptList.agyStart = true, agyStart()
}
keptList.out = slices.Clone(out)
}
// agyStart is what resumeCommand starts an agy row with, "" for agy's own.
func agyStart() string {
if AgyStart == nil {
return ""
}
return AgyStart()
}
func forgetKeptList() {
keptList.files, keptList.out = nil, nil
keptList.prices, keptList.titles, keptList.desktop = nil, nil, nil
}