The Usage page asks for its range every 10 s and the Sessions page for 7, 30 and every day every 15 s. Each read put every session of every range together again, though between two polls the files mostly haven't changed: statsReadAll allocated ~1.1 GB per 80 s of polling, a fifth of all magpie allocated. statsKept keeps each range's Stats (at most 8 ranges) and the native keys, with what they were read from: every listed file's path, key, agent, size, mod time, main/read-only, WSL distro and its parse pointer (a published parse is never written; a changed file gets a new one), today, and the cache generation. It also keeps the answers the read got from the prices, Codex's thread names and Claude Desktop's names, and asks them again before it hands anything out. When any of these differ, everything is read again. The listing and the refresh of changed files still run on every poll. MergeExternalStats wrote into the Stats it was given (a day's usage rows, an append into spare room in Sessions, sorts in place). It now copies whatever it changes, so a kept range is never written. What this doesn't help: while an agent is writing a session, every poll sees a changed file and reads everything again. Reading only the changed sessions is the next change. Run at the pushed head on mote: - GOOS=linux/windows/darwin go vet -tags nogui ./internal/sessions/ ./internal/gui/: ok. - go test -count=1 -timeout 45m ./internal/sessions/ ./internal/gui/ ./internal/usage/: sessions ok, usage ok, gui red only TestMainWindowCorners/macOS_26_SDK (#3949, open, same as main). - New tests: TestStatsKeptBetweenPolls (the same backing array on an unchanged poll; a grown file and a price change are read in), TestStatsKeptNewDay, TestStatsKeptRangesBounded, TestMergeExternalStatsWritesNoInput. TestStatsAllRangesOnePass now forgets the kept ranges before each single read. - Broken by hand, each red: keptFor always missing (TestStatsKeptBetweenPolls, "put together again"); the price check dropped (repriced cost unchanged); MergeExternalStats without its copies (TestMergeExternalStatsWritesNoInput). - Real data (a copy of the owner's history, ~34k sessions), v0.1.1978 against this: /api/sessions/stats?days=0/7/30 and both overviews asked 4 times 12 s apart, each pair byte-identical (1.97 MB for days=0). - A/B, 80 s of GUI polling, v0.1.1978 against this: allocated 5512 to 4480 MB, statsReadAll's share 1108 to 261 MB (the listing); GC 3.90 to 3.42 s, madvise 1.05 to 0.61 s, CPU 28.6 to 25.3%, avg RSS 1061 to 1019 MiB. mote's load was 20-40, so latencies aren't compared. The A/B binary was built before the 8-range bound was added. - Rebased ontob9f1fcddd(redact only), then again: vet on the three systems over sessions, gui and redact, and go test of sessions, usage and redact: ok. - Rebased again ontoa4e929212(GUI context map only), then at this head: the same vet and tests ok. TestCoreScriptBudget is red ata4e929212itself (#4133); this commit touches no GUI file.
156 lines
4.4 KiB
Go
156 lines
4.4 KiB
Go
package sessions
|
|
|
|
import (
|
|
"reflect"
|
|
"slices"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// keptStats are the ranges statsReadAll last read, and what they were read
|
|
// from. The Usage page asks for its range every 10 s and the Sessions page
|
|
// for 7, 30 and every day every 15 s; between two polls the files mostly
|
|
// haven't changed, and each read put every session of every range together
|
|
// again: ~1.1 GB a minute and a third, a fifth of all magpie allocated
|
|
// (#2278). Guarded by mu.
|
|
var keptStats struct {
|
|
files []keptFile
|
|
today time.Time
|
|
gen uint64
|
|
ranges map[int]Stats
|
|
// native is every listed session's agent|id, nil when no read asked
|
|
// for it; shared, so read only
|
|
native map[string]bool
|
|
// the answers the reads got from the prices, Codex's names and Claude
|
|
// Desktop's, asked again before the ranges are given out
|
|
prices map[string]*catalog.Price
|
|
titles map[string]string
|
|
desktop map[string]string
|
|
}
|
|
|
|
// keptFile is what of a listed file a range's stats are made from: its
|
|
// parse (a published parse is never written, a changed file gets a new
|
|
// one), and what the listing says of it.
|
|
type keptFile struct {
|
|
path, key, agent, wsl string
|
|
size int64
|
|
mod int64
|
|
main, readOnly bool
|
|
st *state
|
|
}
|
|
|
|
func keptFileOf(f file) keptFile {
|
|
return keptFile{path: f.path, key: f.key, agent: f.agent, wsl: f.m().wsl, size: f.size, mod: f.mod.UnixNano(),
|
|
main: f.main, readOnly: f.readOnly, st: cache[f.path]}
|
|
}
|
|
|
|
// keptFor reports whether the kept ranges were read from files as they
|
|
// are now, today, with the prices and names they got still the same. When
|
|
// they weren't, what was kept is let go.
|
|
func keptFor(files []file, today time.Time, price func(string) *catalog.Price, titles func(string) string) bool {
|
|
k := &keptStats
|
|
same := k.ranges != nil && k.gen == cacheGeneration && k.today.Equal(today) && len(k.files) == len(files)
|
|
for i := 0; same && i < len(files); i++ {
|
|
same = k.files[i] == keptFileOf(files[i])
|
|
}
|
|
for model, p := range k.prices {
|
|
if !same {
|
|
break
|
|
}
|
|
same = reflect.DeepEqual(price(model), p)
|
|
}
|
|
for id, t := range k.titles {
|
|
if !same {
|
|
break
|
|
}
|
|
same = titles(id) == t
|
|
}
|
|
if same && len(k.desktop) > 0 {
|
|
ids := make([]string, 0, len(k.desktop))
|
|
for id := range k.desktop {
|
|
ids = append(ids, id)
|
|
}
|
|
same = reflect.DeepEqual(DesktopTitles(ids), desktopKnown(k.desktop))
|
|
}
|
|
if !same {
|
|
forgetKeptStats()
|
|
k.files, k.today, k.gen = make([]keptFile, len(files)), today, cacheGeneration
|
|
for i, f := range files {
|
|
k.files[i] = keptFileOf(f)
|
|
}
|
|
k.ranges, k.prices, k.titles, k.desktop = map[int]Stats{}, map[string]*catalog.Price{}, map[string]string{}, map[string]string{}
|
|
}
|
|
return same
|
|
}
|
|
|
|
// keptRanges is how many ranges are kept: the page asks for three, and a
|
|
// days= of any other number is not kept beside them for ever.
|
|
const keptRanges = 8
|
|
|
|
// keepRange keeps st as days' Stats, letting go of a range asked is not
|
|
// among when there are keptRanges already.
|
|
func keepRange(days int, st Stats, asked []int) {
|
|
if len(keptStats.ranges) >= keptRanges {
|
|
for d := range keptStats.ranges {
|
|
if !slices.Contains(asked, d) {
|
|
delete(keptStats.ranges, d)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
keptStats.ranges[days] = st
|
|
}
|
|
|
|
// desktopKnown is names without the ids that have none, as DesktopTitles
|
|
// gives them.
|
|
func desktopKnown(names map[string]string) map[string]string {
|
|
out := map[string]string{}
|
|
for id, n := range names {
|
|
if n != "" {
|
|
out[id] = n
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// keptPrice and keptTitle answer as price and titles do, and note the
|
|
// answer for keptFor.
|
|
func keptPrice(price func(string) *catalog.Price) func(string) *catalog.Price {
|
|
return func(model string) *catalog.Price {
|
|
p := price(model)
|
|
keptStats.prices[model] = p
|
|
return p
|
|
}
|
|
}
|
|
|
|
func keptTitle(titles func(string) string) func(string) string {
|
|
return func(id string) string {
|
|
t := titles(id)
|
|
keptStats.titles[id] = t
|
|
return t
|
|
}
|
|
}
|
|
|
|
// keepDesktop notes the Claude Desktop names a range's sessions got.
|
|
func keepDesktop(st Stats) {
|
|
var ids []string
|
|
for _, s := range st.Sessions {
|
|
if s.Agent == "claude-desktop" {
|
|
ids = append(ids, s.ID)
|
|
}
|
|
}
|
|
if len(ids) == 0 {
|
|
return
|
|
}
|
|
names := DesktopTitles(ids)
|
|
for _, id := range ids {
|
|
keptStats.desktop[id] = names[id]
|
|
}
|
|
}
|
|
|
|
func forgetKeptStats() {
|
|
keptStats.files, keptStats.ranges, keptStats.native = nil, nil, nil
|
|
keptStats.prices, keptStats.titles, keptStats.desktop = nil, nil, nil
|
|
}
|