Files
magpie/internal/sessions/external.go
T
yetone 1829cd52ff sessions: a poll while a session grows makes only the rows it shows
VeryBTC on X (tweet 2117104179454091396) reported RSS that rises and
stays up while the window is open and a long-context session is active.
In that case every Sessions poll sees a changed file and rebuilds the
list. That rebuild copied every gateway-only session three times
(GatewaySessions, listedGateway, gatewayListed) and made each one a
Session row before EachMerged stopped at its 5000-row limit. It also
made every Claude Code file's path and key again.

- usage.EachGatewaySession walks the projection without copying it.
  gui's listedExternal stops at the list's limit and skips hidden
  sessions and sessions without an agent or id, as listedGateway did.
- sessions.EachMerged sorts indexes and makes a row only when the merge
  reaches it. The order is unchanged: newest first, stable on ties.
- listCCFiles reuses the last listing's path and key strings while a
  folder lists the same names. One file new or gone is stepped over.
  This holds ~32 B per file (~480 KB at 15k files), and the strings are
  shared with the listing it already held.
- page_memory.go's ~330 MB comment now has current numbers.
  gateway-sessions.md describes the lazy rows.

Per poll, on mote (go test probe through the real /api/sessions
handler, TotalAlloc):
  15k sessions x 10 turns, 400k ledger rows
    after a session file grows  150.6 MB, 201 ms -> 109.9 MB, 170 ms
    unchanged (warm)             17.2 MB         ->  12.0 MB
  3k sessions x 40 turns, 150k rows (no gateway-only rows past the cap)
    after a session file grows   31.3 MB         ->  29.6 MB
    unchanged (warm)              4.5 MB         ->   3.4 MB
  /api/usage is unchanged at 0.4 MB.

Tests:
- TestSessionsListMakesOnlyTheGatewayRowsItShows (gui) uses 3 natives,
  6000 gateway sessions in tied pairs, one hidden and one carrying a
  native's id. At limits 5, 40 and 6010, and with no limit, the rows
  equal the old pipeline's (listedGateway + gatewayListed +
  MergeExternal) after the JSON round trip. A grown native comes first
  with its new Last. The poll's allocation stays under 6000 rows' size.
- TestEachMergedMakesOnlyTheRowsItReaches (sessions): 15000 externals
  and 20 rows give MergeExternal's first 20, with allocation under a
  tenth of the rows.
- TestCCListingKeepsItsNames (sessions): a second listing of 3000 files
  is equal to the first, in at most 3n mallocs. A file added, files
  removed, and a folder gone with another new each list as a fresh
  listing does. Another agent's keys keep their own prefix.

Broken by hand, each red:
- EachMerged building every row first: 8.8 MB, the sessions test fails.
- The GUI handler back on the listedGateway(GatewaySessions) loop, with
  the lazy merge kept: 13.7 MB, the gui test fails.
- The gui test on origin/main's code: 23 MB, fails.
- The ccNames reuse turned off: 12197 mallocs, the listing test fails.

Run on mote (mote-run) at 73a1b8769, rebased on origin/main 07f4d46c5:
gofmt -l internal (empty), go vet ./..., GOOS=linux go vet -tags nogui
./..., GOOS=windows go vet ./... all clean. go test -count=1 -timeout
45m ./internal/sessions ./internal/usage ./internal/gui: sessions ok,
usage ok. gui is ok except TestMainWindowCorners/macOS_26_SDK, which is
#3949 (red on mote's macOS 27, an AppKit title bar test this change
doesn't reach). It was red the same way in the run before the rebase.

Not tried:
- A real app window with RSS measured over time. The numbers are
  allocation per poll from a probe.
- The rest of a changed poll: statsKept re-sums every session (~26 MB at
  15k), listing (~24 MB) and byKey (~9 MB). None was a cheap, safe
  change.
- Windows and Linux test runs (vet only).
- A gateway title store in the fixture. GatewayTitler's ReadDirs now run
  only for the rows shown.
2026-11-02 17:19:23 +08:00

277 lines
9.0 KiB
Go

package sessions
import (
"cmp"
"math"
"slices"
"sort"
"time"
)
// ExternalModel and ExternalSession are read-only session projections from a
// request ledger such as the gateway. They intentionally carry no file or
// resume capability.
type ExternalModel struct {
Model string
Tokens
Cost float64
Priced bool
}
type ExternalSession struct {
Agent, ID string
Title string // what it was first asked (GatewayTitle); its id when ""
Start, Last time.Time
Models []ExternalModel
Daily []ExternalDayUsage
Tokens
Cost float64
Unpriced int
}
// MergeExternal adds external sessions to a file-backed listing. Native
// sessions win on the exact Agent+ID identity and external entries are kept
// read-only (no file of the agent's to resume or open) with a gateway transcript
// reader; magpie's own record of one is deleted by DeleteGateway.
func MergeExternal(native []Session, external []ExternalSession) []Session {
added := externalAdded(native, external)
out := make([]Session, len(native), len(native)+len(added))
copy(out, native)
out = append(out, added...)
sort.SliceStable(out, func(i, j int) bool { return out[i].Last.After(out[j].Last) })
return out
}
// EachMerged calls each with every session MergeExternal(native, external)
// lists, in its order, until each returns false, without making that list:
// the Sessions page's poll copied all ~17k sessions into it, then into its
// own rows. external holds none of native's sessions, as GatewaySessions
// gives them for native's GatewayKeys: the set of native's keys made again
// to look was ~2 MB a poll. native is written by neither; when it isn't
// newest first, as List gives it, it is sorted in a copy first.
func EachMerged(native []Session, external []ExternalSession, each func(*Session) bool) {
newest := func(a, b Session) int { return b.Last.Compare(a.Last) }
if !slices.IsSortedFunc(native, newest) {
native = slices.Clone(native)
slices.SortStableFunc(native, newest)
}
// external's sessions in the order externalSessions then a stable
// sort by Last give them, as indexes: a row is made only when it is
// reached. Every gateway session past what native lists was made into
// a row on each poll, ~22 MB of a poll over ~15k sessions while one
// grew, though the page stops at sessions.All rows.
added := make([]int, 0, len(external))
for k, e := range external {
if e.Agent != "" && e.ID != "" {
added = append(added, k)
}
}
slices.SortStableFunc(added, func(a, b int) int { return external[b].Last.Compare(external[a].Last) })
names := desktopExternalTitles(external)
// a merge of the two, natives first among equals, as SliceStable over
// native then added gives
i, j := 0, 0
for i < len(native) || j < len(added) {
var s *Session
if j < len(added) && (i == len(native) || external[added[j]].Last.After(native[i].Last)) {
row := externalSession(external[added[j]], names)
s, j = &row, j+1
} else {
s, i = &native[i], i+1
}
if !each(s) {
return
}
}
}
// externalAdded are the external sessions MergeExternal adds to native, in
// external's order.
func externalAdded(native []Session, external []ExternalSession) []Session {
// keyed by the pair, not "agent|id": a string made for each of ~5000
// sessions on each of a poll's listings, and the set and the list grown
// an append at a time, were ~200 MB in 80s of polls (#2278)
type agentID struct{ agent, id string }
seen := make(map[agentID]struct{}, len(native))
for _, s := range native {
// GatewayKeys, as pairs
seen[agentID{s.Agent, s.ID}] = struct{}{}
if s.Agent == "raft" {
seen[agentID{"pi", s.ID}] = struct{}{}
}
if s.App != "" {
seen[agentID{s.App, s.ID}] = struct{}{}
}
for _, id := range s.Copies {
seen[agentID{s.Agent, id}] = struct{}{}
}
for _, id := range s.Subs {
seen[agentID{s.Agent, id}] = struct{}{}
}
}
var not []ExternalSession
for _, e := range external {
if _, dup := seen[agentID{e.Agent, e.ID}]; !dup {
not = append(not, e)
}
}
return externalSessions(not)
}
// externalSessions are external's sessions as a list shows them, those
// with an agent and an id, in external's order.
func externalSessions(external []ExternalSession) []Session {
names := desktopExternalTitles(external)
var out []Session
for _, e := range external {
if e.Agent == "" || e.ID == "" {
continue
}
out = append(out, externalSession(e, names))
}
return out
}
// externalSession is e as a list shows it; names are
// desktopExternalTitles'.
func externalSession(e ExternalSession, names map[string]string) Session {
name := ""
if len(names) > 0 {
name = names[e.Agent+"|"+e.ID]
}
s := Session{ReadOnly: true, Gateway: true, Transcript: true, Agent: e.Agent, ID: e.ID, Title: cmp.Or(name, e.Title, e.ID), Start: e.Start, Last: e.Last, Models: []Model{}, Unpriced: e.Unpriced}
for _, m := range e.Models {
s.Models = append(s.Models, Model{Model: m.Model, Tokens: m.Tokens, Cost: m.Cost, Priced: m.Priced})
s.Tokens.add(m.Tokens)
}
s.Cost = e.Cost
return s
}
// desktopExternalTitles are the names an app saved for its sessions among
// external, by Agent|ID: Claude Desktop's (DesktopTitles), PI-Desktop's
// (PIDesktopTitles, #1967). They come before the gateway's recorded first
// prompt.
func desktopExternalTitles(external []ExternalSession) map[string]string {
var ids, pi []string
for _, e := range external {
if e.Agent == "claude-desktop" {
ids = append(ids, e.ID)
} else if isPIDesktop(e.Agent) {
pi = append(pi, e.ID)
}
}
out := map[string]string{}
for id, name := range DesktopTitles(ids) {
out["claude-desktop|"+id] = name
}
if len(pi) > 0 {
names := PIDesktopTitles(pi)
for _, e := range external {
if name := names[e.ID]; name != "" && isPIDesktop(e.Agent) {
out[e.Agent+"|"+e.ID] = name
}
}
}
return out
}
// MergeExternalStats appends external daily usage and session summaries to a
// Stats snapshot. It is deliberately data-only so callers can keep their own
// source and pricing policy.
type ExternalDayUsage struct {
Date, Agent, Cwd, Model string
Tokens
Cost float64
Priced bool
}
// st itself is not written: its Days and Sessions may be kept for the next
// read (keptStats), so what is changed is copied first.
func MergeExternalStats(st Stats, days []ExternalDayUsage, external []ExternalSession) Stats {
oldFirst, _ := time.ParseInLocation(time.DateOnly, st.From, time.Local)
for _, d := range days {
if d.Date <= st.To && (st.From == "" || d.Date < st.From) {
st.From = d.Date
}
}
first, _ := time.ParseInLocation(time.DateOnly, st.From, time.Local)
if !oldFirst.IsZero() && !first.Equal(oldFirst) {
// noted, not written into a copy of every session
st.shift += int(math.Round(oldFirst.Sub(first).Hours() / 24))
}
st.Days = slices.Clone(st.Days)
byDate, own := map[string]int{}, map[int]bool{}
for i := range st.Days {
byDate[st.Days[i].Date] = i
}
for _, u := range days {
if u.Date < st.From || u.Date > st.To {
continue
}
i, ok := byDate[u.Date]
if !ok {
st.Days = append(st.Days, Day{Date: u.Date, Usage: []Usage{}, Active: []Active{}})
i = len(st.Days) - 1
byDate[u.Date], own[i] = i, true
}
d := &st.Days[i]
if !own[i] {
// a day of st's: its rows are copied before one is added to
d.Usage, own[i] = slices.Clone(d.Usage), true
}
found := false
for i := range d.Usage {
if d.Usage[i].Agent == u.Agent && d.Usage[i].Cwd == u.Cwd && d.Usage[i].Model == u.Model {
d.Usage[i].Tokens.add(u.Tokens)
d.Usage[i].Cost += u.Cost
d.Usage[i].Priced = d.Usage[i].Priced && u.Priced
found = true
break
}
}
if !found {
d.Usage = append(d.Usage, Usage{Agent: u.Agent, Cwd: u.Cwd, Model: u.Model, Tokens: u.Tokens, Cost: u.Cost, Priced: u.Priced})
}
}
names := desktopExternalTitles(external)
// the gateway's go beside Sessions, not into a copy of them (#2278)
st.gateway = slices.Clip(st.gateway)
for _, e := range external {
if e.ID == "" || e.Agent == "" {
continue
}
s := Summary{Key: e.Agent + ":" + e.ID, Agent: e.Agent, ID: e.ID, Title: cmp.Or(names[e.Agent+"|"+e.ID], e.Title), Gateway: true, Last: e.Last, Tokens: e.Tokens, Cost: e.Cost, Priced: true, Models: []string{}, Days: []int{}}
perDay := map[int]*summaryDay{}
for _, u := range e.Daily {
day, err := time.ParseInLocation(time.DateOnly, u.Date, time.Local)
if err != nil {
continue
}
// in Sessions' days, st.shift after the range's
d := int(math.Round(day.Sub(first).Hours()/24)) - st.shift
if first.IsZero() || u.Date < st.From || u.Date > st.To {
continue
}
pd := perDay[d]
if pd == nil {
pd = &summaryDay{day: d}
perDay[d] = pd
s.Days = append(s.Days, d)
}
pd.output += u.Output
}
sort.Ints(s.Days)
for _, d := range s.Days {
s.perDay = append(s.perDay, *perDay[d])
}
for _, m := range e.Models {
s.Models = append(s.Models, m.Model)
s.Priced = s.Priced && m.Priced
}
st.gateway = append(st.gateway, s)
}
sort.Slice(st.Days, func(i, j int) bool { return st.Days[i].Date < st.Days[j].Date })
sort.SliceStable(st.gateway, func(i, j int) bool { return st.gateway[i].Last.After(st.gateway[j].Last) })
return st
}