Each Sessions or Usage poll listed every agent's files several times and copied the Claude Code and Codex lists twice: callFilesWith made a deduplicated slice and set, then callSourcesWith and allFiles appended to it and deduplicated it again. Now callFileLists hands out the lists, and distinctOf builds one slice and one set at their combined size. More cuts on the same poll path: - allowed (the read-consent gate,af8f3a939) gives back its list as it is when every file's agent may be read. Before, it copied every file on each of the poll's passes. - MergeExternal keys its set by an {agent, id} pair, not a string built for each of ~5000 sessions, and sizes the output once. It keeps the pairs GatewayKeys names, Raft's Pi id included. - codexSubagentTaskTitles skips the four parent maps for a thread that spawns no agent, which is most of them. - ccFamily holds each file by its index instead of copying ~200 bytes, and builds groups only for roots with two or more files. A singleton's uuid memo is still let go, as before. Allocation on mote's bench (ab19, 80s of polls over ~34k sessions): before this, callFilesWith was 250 MB, MergeExternal 200 MB, ccCopies 180 MB and codexSubagentTaskTitles 145 MB. A/B on idle mote (ab21; polls every 5s over 10 endpoints while the ledger and a Codex rollout are written; v0.1.1981 / the two commits before this / all three #2278 commits): - mean poll 0.35s / 0.25s / 0.25s - /api/sessions 0.87s / 0.73s / 0.47s - /api/sessions/stats 0.33s / 0.26s / 0.15s - /api/usage/heatmap 0.94s / 0.79s / 0.54s - CPU 27.9% / 26.9% / 24.3% ab20, run under load ~30 from other runs, showed the stats endpoints slower in the order g25-last. Its CPU profile had less sessions work, and run idle the gap goes the other way, so ab20's latency was load. RSS stays the same (1014 / 1036 / 1045 MiB average, footprint peak 871 / 884 / 869M). GOGC 25 still sets the GC share (~22-26%). Rebased onto origin/mainf982e46f2. calls.go, sessions.go, external.go and cc_copies.go conflicted with the read-consent gate (af8f3a939), Raft's sessions (9474eaf9c) and ccFamily's branches (f594b452e). Each cut was made again on main's code. Run at this head, on mote: - GOOS=linux, windows and darwin go vet -tags nogui ./internal/sessions/ ./internal/usage/ ./internal/gui/: ok. - go test -timeout 45m ./internal/sessions/ ./internal/usage/ ./internal/gui/: sessions and usage ok. gui red only on TestMainWindowCorners/macOS_26_SDK (#3949, open, red on macOS 27); this touches no GUI file.
221 lines
6.5 KiB
Go
221 lines
6.5 KiB
Go
package sessions
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Which agents' session files magpie may read (RustCats on X: a new
|
|
// install showed every agent's history before anyone agreed to it). Reads
|
|
// is the one gate: every lister asks it before it lists an agent's
|
|
// folder, the listings are filtered by it again, and so are the readers
|
|
// that take a session or a call already listed (a cache made before a
|
|
// switch was turned off). A fresh install reads nothing until the
|
|
// Sessions page's consent, or Settings, allows an agent; an install that
|
|
// already read sessions keeps reading them all.
|
|
|
|
// ErrNotRead: the session's agent is one magpie may not read.
|
|
var ErrNotRead = errors.New("magpie is not allowed to read this agent's sessions (Settings, Sessions)")
|
|
|
|
// settled is the decision SettleReads made this run, so a fresh install
|
|
// that couldn't save it (settings.json unreadable) still reads nothing,
|
|
// and the listings don't write settings.json again and again.
|
|
var settled struct {
|
|
sync.Mutex
|
|
r *settings.SessionReads
|
|
done bool
|
|
}
|
|
|
|
// readsAllInTests: in a test binary an install nothing settled reads every
|
|
// agent, as before, and SettleReads writes nothing to settings.json; the
|
|
// tests of the gate turn it off.
|
|
var readsAllInTests = testing.Testing()
|
|
|
|
// Reads is whether magpie may read agent's own session files.
|
|
func Reads(agent string) bool {
|
|
return currentReads().Reads(agent)
|
|
}
|
|
|
|
func currentReads() *settings.SessionReads {
|
|
if r := settings.Shared().SessionReads; r != nil {
|
|
return r
|
|
}
|
|
if readsAllInTests {
|
|
return &settings.SessionReads{Asked: true, All: true}
|
|
}
|
|
return SettleReads()
|
|
}
|
|
|
|
// SettleReads decides, once, what an install with no SessionReads reads:
|
|
// one that already read sessions (its sessions cache or call cache is
|
|
// there) keeps reading every agent; a fresh one reads none until asked.
|
|
// The decision is saved, so a cache written later can't turn a fresh
|
|
// install into one that reads everything. A value already saved is kept.
|
|
func SettleReads() *settings.SessionReads {
|
|
if r := settings.Shared().SessionReads; r != nil {
|
|
return r
|
|
}
|
|
if readsAllInTests {
|
|
return &settings.SessionReads{Asked: true, All: true}
|
|
}
|
|
settled.Lock()
|
|
defer settled.Unlock()
|
|
if settled.done {
|
|
return settled.r
|
|
}
|
|
r := &settings.SessionReads{}
|
|
if readBefore() {
|
|
r = &settings.SessionReads{Asked: true, All: true}
|
|
}
|
|
var saved *settings.SessionReads
|
|
err := settings.SetSessionReads(func(cur *settings.SessionReads) *settings.SessionReads {
|
|
if cur != nil {
|
|
saved = cur
|
|
return cur
|
|
}
|
|
return r
|
|
})
|
|
if saved != nil {
|
|
r = saved
|
|
}
|
|
if err != nil {
|
|
// not saved: decided for this run only; an unreadable
|
|
// settings.json is not a reason to read more
|
|
r = &settings.SessionReads{}
|
|
}
|
|
settled.r, settled.done = r, true
|
|
return r
|
|
}
|
|
|
|
// forgetSettled is for tests that settle again in a new HOME.
|
|
func forgetSettled() {
|
|
settled.Lock()
|
|
settled.r, settled.done = nil, false
|
|
settled.Unlock()
|
|
}
|
|
|
|
// readBefore is whether this install read sessions before SessionReads
|
|
// existed: it kept a sessions cache, or a call cache of any version.
|
|
func readBefore() bool {
|
|
if _, err := os.Stat(CachePath()); err == nil {
|
|
return true
|
|
}
|
|
m, _ := filepath.Glob(filepath.Join(filepath.Dir(CachePath()), "calls-v*"))
|
|
return len(m) > 0
|
|
}
|
|
|
|
// SetReads records the user's choice: all, when given, is whether an
|
|
// agent nobody named is read (allow all, Settings' "agents found later");
|
|
// each agent in agents is read or not as given; the others stay as they
|
|
// were. The lists are read again.
|
|
func SetReads(all *bool, agents map[string]bool) error {
|
|
err := settings.SetSessionReads(func(cur *settings.SessionReads) *settings.SessionReads {
|
|
r := &settings.SessionReads{Asked: true}
|
|
if cur != nil {
|
|
r.All = cur.All
|
|
for a, on := range cur.Agents {
|
|
if r.Agents == nil {
|
|
r.Agents = map[string]bool{}
|
|
}
|
|
r.Agents[a] = on
|
|
}
|
|
}
|
|
if all != nil {
|
|
r.All = *all
|
|
}
|
|
for a, on := range agents {
|
|
if r.Agents == nil {
|
|
r.Agents = map[string]bool{}
|
|
}
|
|
r.Agents[a] = on
|
|
}
|
|
return r
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
settled.Lock()
|
|
settled.r, settled.done = nil, false
|
|
settled.Unlock()
|
|
Reset()
|
|
return nil
|
|
}
|
|
|
|
// allowed is fs without the files of agents magpie may not read.
|
|
func allowed(fs []file) []file {
|
|
r := currentReads()
|
|
// most polls read every agent: fs as it is then, not a copy of each
|
|
// file listed (#2278)
|
|
k := slices.IndexFunc(fs, func(f file) bool { return !r.Reads(f.agent) })
|
|
if k < 0 {
|
|
return fs
|
|
}
|
|
out := slices.Clone(fs[:k])
|
|
for _, f := range fs[k+1:] {
|
|
if r.Reads(f.agent) {
|
|
out = append(out, f)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Found is the agents with a session store on this computer, by a look at
|
|
// their folders only (nothing in them is read): those the consent lists.
|
|
// WSL distros are not looked into; allow all covers them.
|
|
func Found() []string {
|
|
exists := func(p string) bool {
|
|
if p == "" {
|
|
return false
|
|
}
|
|
_, err := os.Stat(p)
|
|
return err == nil
|
|
}
|
|
var out []string
|
|
add := func(agent string, paths ...string) {
|
|
for _, p := range paths {
|
|
if exists(p) {
|
|
out = append(out, agent)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
add("claude", filepath.Join(ClaudeDir(), "projects"))
|
|
var desktop []string
|
|
for _, d := range callDesktopDirs() {
|
|
desktop = append(desktop, filepath.Join(d, "local-agent-mode-sessions"), filepath.Join(d, "claude-code-sessions"))
|
|
}
|
|
add("claude-desktop", desktop...)
|
|
var codex []string
|
|
for _, h := range CodexDirs() {
|
|
codex = append(codex, filepath.Join(h, "sessions"), filepath.Join(h, "archived_sessions"))
|
|
}
|
|
add("codex", codex...)
|
|
add("opencode", OpenCodeDir())
|
|
add("pi", PiDir())
|
|
add("raft", filepath.Join(SlockDir(), "agents"))
|
|
add("zcode", zcodeDB())
|
|
add("dsh", filepath.Join(DshDir(), "sessions"))
|
|
add("cline", ClineSessionDir())
|
|
add("qoder", filepath.Join(QoderDir("qoder"), "projects"))
|
|
add("qoder-cn", filepath.Join(QoderDir("qoder-cn"), "projects"))
|
|
add("grok", filepath.Join(GrokDir(), "sessions"))
|
|
add("workbuddy", filepath.Join(WorkBuddyDir(), "projects"))
|
|
add("codebuddy", filepath.Join(CodeBuddyDir(), "projects"))
|
|
add("omp", filepath.Join(OmpDir(), "sessions"))
|
|
add("droid", filepath.Join(FactoryDir(), "sessions"))
|
|
add("cursor", filepath.Join(CursorDir(), "chats"), filepath.Join(cursorDataDir(), "projects"), cursorEditorDB())
|
|
add("hermes", HermesDirs()...)
|
|
add("alma", almaDB())
|
|
add("minimax-code", minimaxSessions())
|
|
add("kimi", kimiSessions())
|
|
add("agy", filepath.Join(AgyDir(), "brain"))
|
|
add("gemini", geminiTmpDir())
|
|
return out
|
|
}
|