Skip to content

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

sessions: a path listed twice in one read is read once, so the start's warm-up no longer quits magpie (#2932)

yetone572a1757cc98
diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go
index 625c76078..915e46ce8 100644
--- a/internal/sessions/calls.go
+++ b/internal/sessions/calls.go
@@ -331,7 +331,7 @@ func callSources() []file {
out = append(out, piCallFiles()...)
out = append(out, codebuddyCallFiles()...)
out = append(out, agyCallFiles()...) // #2097
- return append(out, workbuddyCallFiles()...)
+ return distinctPaths(append(out, workbuddyCallFiles()...))
}
// desktopDataDirs are Claude Desktop's folders on this computer that
diff --git a/internal/sessions/calls_codebuddy_test.go b/internal/sessions/calls_codebuddy_test.go
index 182249ae2..47574fc1e 100644
--- a/internal/sessions/calls_codebuddy_test.go
+++ b/internal/sessions/calls_codebuddy_test.go
@@ -1,6 +1,8 @@
package sessions
import (
+ "os"
+ "path/filepath"
"testing"
"time"
)
@@ -53,3 +55,61 @@ func TestCodeBuddyGatewayIsItsOwn(t *testing.T) {
t.Fatal("CodeBuddy's models.json was read for WorkBuddy")
}
}
+
+// CodeBuddy Code told to keep its files in WorkBuddy's folder
+// (CODEBUDDY_CONFIG_DIR=~/.workbuddy, TimWongUp's .zshrc in #2932) leaves
+// one folder for both: its sessions are read once, as WorkBuddy's, on the
+// Sessions page and in usage alike, and the folder is named once. Read as
+// both, each was listed twice, its calls went to CodeBuddy Code, and a
+// refresh took each path twice and closed a nil channel.
+func TestCodeBuddyInWorkBuddysFolderIsReadOnce(t *testing.T) {
+ // "both": both variables on one folder, as WorkBuddy's installer and
+ // its IDE host set them on Windows (mxl2498, cunnak, Tsuki-hash)
+ for _, how := range []string{"named", "linked", "both"} {
+ t.Run(how, func(t *testing.T) {
+ inZone(t, 0)
+ home := setupAgents(t)
+ dir := filepath.Join(home, ".workbuddy")
+ switch how {
+ case "linked":
+ dir = filepath.Join(home, "buddy-link")
+ if err := os.Symlink(filepath.Join(home, ".workbuddy"), dir); err != nil {
+ t.Skip("no symlinks here:", err)
+ }
+ case "both":
+ t.Setenv("WORKBUDDY_CONFIG_DIR", dir)
+ }
+ t.Setenv("CODEBUDDY_CONFIG_DIR", dir)
+ // the start's warm-up, which quit magpie a few seconds in
+ if st, _ := StatsAndNative(0); len(st.Sessions) == 0 {
+ t.Fatal("the warm-up read no sessions")
+ }
+ ss := List(0)
+ if w, c := count(ss, "workbuddy"), count(ss, "codebuddy"); w != 3 || c != 0 {
+ t.Fatalf("listed %d WorkBuddy and %d CodeBuddy Code sessions, want 3 and 0", w, c)
+ }
+ n := map[string]int{}
+ for _, c := range Calls(time.Time{}) {
+ n[c.Agent]++
+ }
+ // TestWorkBuddyCalls' six, each once
+ if n["workbuddy"] != 6 || n["codebuddy"] != 0 {
+ t.Fatalf("calls %v, want WorkBuddy's 6 and no CodeBuddy Code's", n)
+ }
+ for _, s := range StatsFor(0).Sessions {
+ if s.Agent == "codebuddy" {
+ t.Fatalf("stats counted a WorkBuddy session as CodeBuddy Code's too: %s", s.Key)
+ }
+ }
+ named := 0
+ for _, d := range Dirs() {
+ if sameFolder(d, filepath.Join(home, ".workbuddy")) {
+ named++
+ }
+ }
+ if named != 1 {
+ t.Fatalf("WorkBuddy's folder named %d times in %v", named, Dirs())
+ }
+ })
+ }
+}
diff --git a/internal/sessions/calls_opencode_test.go b/internal/sessions/calls_opencode_test.go
index 83f8060e4..eb0db5e78 100644
--- a/internal/sessions/calls_opencode_test.go
+++ b/internal/sessions/calls_opencode_test.go
@@ -2,6 +2,7 @@ package sessions
import (
"path/filepath"
+ "strings"
"testing"
"time"
)
@@ -126,3 +127,39 @@ func TestOpenCodeCallsV2(t *testing.T) {
t.Fatalf("its compaction: %+v", c)
}
}
+
+// A session id stored once as TEXT and once as BLOB is two rows in
+// SQLite and one path here (#2932, as Hermes's state.db had it): every
+// list of the files to read has it once, so nothing reads or counts it
+// twice.
+func TestOpenCodeIDStoredAsTextAndBlobIsListedOnce(t *testing.T) {
+ data, _ := setupMore(t)
+ db := ocMakeDB(t, filepath.Join(data, "opencode", "opencode.db"))
+ if _, err := db.Exec("INSERT INTO session (id, project_id, parent_id, slug, directory, title, version, time_created, time_updated) VALUES (?, '0f3a', NULL, 's', '/work/oc', 'stray', '1.1.53', 1790416000000, 1790416000000)", []byte(ocMain)); err != nil {
+ t.Fatal(err)
+ }
+ listed := func(fs []file) (n int) {
+ for _, f := range fs {
+ if strings.HasSuffix(f.path, "#"+ocMain) {
+ n++
+ }
+ }
+ return n
+ }
+ // listed as List lists them, with mu held and the databases closed
+ // after: one left open can't be removed on Windows
+ mu.Lock()
+ release := useDBs()
+ own, all := openCodeFiles(), allFiles()
+ release()
+ mu.Unlock()
+ if n := listed(own); n != 2 {
+ t.Fatalf("OpenCode's own list has the session %d times, want the TEXT and the BLOB row", n)
+ }
+ for name, fs := range map[string][]file{"allFiles": all, "callSources": callSources()} {
+ if n := listed(fs); n != 1 {
+ t.Errorf("%s lists the session %d times", name, n)
+ }
+ }
+ checkOCCalls(t, ocCalls(t))
+}
diff --git a/internal/sessions/hermes.go b/internal/sessions/hermes.go
index 0cdbdae24..16185443c 100644
--- a/internal/sessions/hermes.go
+++ b/internal/sessions/hermes.go
@@ -319,12 +319,24 @@ func hermesFiles() []file {
if err != nil {
continue
}
+ // an id stored once as TEXT and once as BLOB is two rows under a
+ // TEXT primary key and one string here: it is one session, as
+ // late as the later of them (#2932)
+ at := map[string]int{}
for rows.Next() {
var sid string
var start, end, last, messageAt, usageAt float64
if rows.Scan(&sid, &start, &end, &last, &messageAt, &usageAt) != nil || sid == "" {
continue
}
+ mod := hermesTime(max(start, end, last, messageAt, usageAt))
+ if i, ok := at[sid]; ok {
+ if mod.After(out[i].mod) {
+ out[i].mod = mod
+ }
+ continue
+ }
+ at[sid] = len(out)
id := storeID + "-" + sid
out = append(out, file{
agent: "hermes",
@@ -332,7 +344,7 @@ func hermesFiles() []file {
path: path + "#" + url.QueryEscape(sid),
sid: sid,
main: true,
- mod: hermesTime(max(start, end, last, messageAt, usageAt)),
+ mod: mod,
hermes: store,
rev: prints[sid], // "" when unknown: parsed again
readOnly: true,
diff --git a/internal/sessions/hermes_test.go b/internal/sessions/hermes_test.go
index 01ed7dd7d..16cfbf9b4 100644
--- a/internal/sessions/hermes_test.go
+++ b/internal/sessions/hermes_test.go
@@ -318,3 +318,57 @@ func TestHermesOpensStateDatabaseReadOnly(t *testing.T) {
t.Fatalf("read-only probe changed fixture title to %q", title)
}
}
+
+// nativeas's state.db (#2932) held one session id twice, a TEXT row and a
+// BLOB row of the same bytes: SQLite keeps them apart under a TEXT primary
+// key, but both scan to one string, so the session was listed twice and its
+// usage counted twice (and, before refresh marked a path as it was taken,
+// the second read closed a nil channel at start).
+func TestHermesIDStoredAsTextAndBlobIsOneSession(t *testing.T) {
+ setup(t)
+ root := filepath.Join(t.TempDir(), "hermes")
+ t.Setenv("HERMES_HOME", root)
+ path := hermesFixture(t, root, "", "same-id", true)
+ db, err := sql.Open("sqlite", path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ base := time.Date(2026, 9, 28, 10, 0, 0, 0, time.UTC).Unix()
+ if _, err := db.Exec(`INSERT INTO sessions (id, title, started_at) VALUES (?, 'stray', ?)`, []byte("same-id"), float64(base)); err != nil {
+ db.Close()
+ t.Fatal(err)
+ }
+ var kinds string
+ if err := db.QueryRow(`SELECT group_concat(typeof(id)) FROM sessions`).Scan(&kinds); err != nil || kinds != "text,blob" {
+ db.Close()
+ t.Fatalf("fixture's ids are %q (%v), want a text and a blob", kinds, err)
+ }
+ db.Close()
+
+ var hs []Session
+ for _, s := range List(0) {
+ if s.Agent == "hermes" {
+ hs = append(hs, s)
+ }
+ }
+ if len(hs) != 1 {
+ t.Fatalf("listed %d Hermes sessions, want 1: %+v", len(hs), hs)
+ }
+ if hs[0].Title != "A Hermes task" {
+ t.Errorf("the session read is %q, want the one its messages belong to", hs[0].Title)
+ }
+ var total Tokens
+ for _, d := range StatsAt(0, time.Date(2026, 9, 28, 12, 0, 0, 0, time.UTC)).Days {
+ for _, u := range d.Usage {
+ if u.Agent == "hermes" {
+ total.add(u.Tokens)
+ }
+ }
+ }
+ if total != (Tokens{100, 50, 25, 15, 0}) {
+ t.Errorf("Hermes usage %+v, want the session's once", total)
+ }
+ if n := len(ListAgent("hermes")); n != 1 {
+ t.Errorf("the management page lists %d Hermes sessions, want 1", n)
+ }
+}
diff --git a/internal/sessions/refresh_shared_test.go b/internal/sessions/refresh_shared_test.go
index 57dd9d5ec..2db7fc196 100644
--- a/internal/sessions/refresh_shared_test.go
+++ b/internal/sessions/refresh_shared_test.go
@@ -169,3 +169,37 @@ func TestStoresLiveAsLongAsTheirLastUser(t *testing.T) {
t.Fatal("left open after the last read")
}
}
+
+// A path listed twice in one read is read once. Each listing was a job of
+// its own on one parsing entry, so the second publish closed a nil
+// channel and took magpie down a few seconds after every start (#2932).
+func TestRefreshReadsAPathListedTwiceOnce(t *testing.T) {
+ inZone(t, 0)
+ claude, _ := setup(t)
+ moreSessions(t, claude, 3)
+ var parsed atomic.Int32
+ parseHook = func() { parsed.Add(1) }
+ t.Cleanup(func() { parseHook = nil })
+
+ mu.Lock()
+ defer mu.Unlock()
+ loadCache()
+ defer useDBs()()
+ files := allFiles()
+ if len(files) < 3 {
+ t.Fatalf("listed %d files", len(files))
+ }
+ twice := append(append([]file(nil), files...), files[0], files[len(files)-1])
+ refresh(twice, files)
+ if n := int(parsed.Load()); n != len(files) {
+ t.Fatalf("parsed %d times for %d files", n, len(files))
+ }
+ for _, f := range files {
+ if cache[f.path] == nil {
+ t.Fatalf("%s has no parse", f.path)
+ }
+ if parsing[f.path] != nil {
+ t.Fatalf("%s is still being read", f.path)
+ }
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 9d2c031a2..ead018fc6 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -24,6 +24,7 @@ import (
"path/filepath"
"regexp"
"runtime"
+ "slices"
"sort"
"strconv"
"strings"
@@ -498,6 +499,22 @@ func allFiles() []file {
grokFiles(), workbuddyFiles(), codebuddyFiles(), droidFiles(), ompFiles(), cursorFiles(), hermesFiles(), almaFiles(), kimiFiles()} {
out = append(out, fs...)
}
+ return distinctPaths(out)
+}
+
+// distinctPaths is fs with each path kept once, the first time it is
+// listed. A path is one file to read and count: a session a database
+// holds twice under one id (a TEXT and a BLOB row, #2932), or a folder
+// two agents are told to share, would otherwise be counted twice.
+func distinctPaths(fs []file) []file {
+ seen := make(map[string]bool, len(fs))
+ out := fs[:0]
+ for _, f := range fs {
+ if !seen[f.path] {
+ seen[f.path] = true
+ out = append(out, f)
+ }
+ }
return out
}
@@ -505,6 +522,7 @@ func allFiles() []file {
// Codex's, and the other agents' where they keep sessions on this computer.
func Dirs() []string {
out := []string{ClaudeDir(), CodexDir()}
+ var named []os.FileInfo
for _, d := range []struct{ dir, sessions string }{
{OpenCodeDir(), OpenCodeDir()},
{PiDir(), PiDir()},
@@ -525,9 +543,18 @@ func Dirs() []string {
{MiniMaxDir(), minimaxSessions()},
{KimiCodeDir(), kimiSessions()},
} {
- if _, err := os.Stat(d.sessions); err == nil {
- out = append(out, d.dir)
+ if _, err := os.Stat(d.sessions); err != nil {
+ continue
+ }
+ // a folder two agents share is named once
+ // (CODEBUDDY_CONFIG_DIR set to ~/.workbuddy, #2932)
+ if fi, err := os.Stat(d.dir); err == nil {
+ if slices.ContainsFunc(named, func(o os.FileInfo) bool { return os.SameFile(o, fi) }) {
+ continue
+ }
+ named = append(named, fi)
}
+ out = append(out, d.dir)
}
out = append(out, HermesDirs()...)
out = append(out, wslDirs()...)
@@ -789,13 +816,18 @@ func refresh(want, all []file) {
}
if s := cache[f.path]; s == nil || s.Size != f.size || s.Mod != f.mod.UnixNano() || (f.agent == "hermes" && (f.hermes == nil || f.rev == "" || s.DBRevision != f.rev)) || ((f.agent == "alma" || f.agent == "cursor") && s.DBRevision != f.rev) {
if parsing[f.path] != nil {
- // another refresh is reading it: its last parse stands
- // until that one is in, and one never read is waited for
+ // a refresh is reading it, another or this one (a path
+ // listed twice): its last parse stands until that one is
+ // in, and one never read is waited for
if s == nil {
waits = append(waits, f)
}
continue
}
+ // marked as it is taken, so a second listing of the path is
+ // not a second job: two jobs shared one entry, and the second
+ // publish closed a nil channel (#2932)
+ parsing[f.path] = make(chan struct{})
todo = append(todo, f)
}
}
@@ -844,7 +876,6 @@ func refresh(want, all []file) {
jobs := make([]job, len(todo))
for i, f := range todo {
jobs[i] = job{f: f, old: cache[f.path]}
- parsing[f.path] = make(chan struct{})
}
generation := cacheGeneration
mu.Unlock()
diff --git a/internal/sessions/workbuddy.go b/internal/sessions/workbuddy.go
index 0858034b0..2cf6a4523 100644
--- a/internal/sessions/workbuddy.go
+++ b/internal/sessions/workbuddy.go
@@ -41,8 +41,32 @@ func WorkBuddyDir() string {
func workbuddyFiles() []file { return buddyFiles("workbuddy", WorkBuddyDir()) }
// codebuddyFiles are the same under CodeBuddy Code's projects/: the CLI keeps
-// them as its desktop sibling does, one agent's folder apart (#1779).
-func codebuddyFiles() []file { return buddyFiles("codebuddy", CodeBuddyDir()) }
+// them as its desktop sibling does, one agent's folder apart (#1779). None
+// when its folder is WorkBuddy's, which workbuddyFiles reads already.
+func codebuddyFiles() []file {
+ if codebuddyInWorkBuddy() {
+ return nil
+ }
+ return buddyFiles("codebuddy", CodeBuddyDir())
+}
+
+// codebuddyInWorkBuddy is whether CodeBuddy Code's folder is WorkBuddy's
+// ($CODEBUDDY_CONFIG_DIR set to ~/.workbuddy, or a link to it). Its sessions
+// are then read once, as WorkBuddy's: read as both, each was listed and
+// counted twice, and one refresh took each path twice (#2932).
+func codebuddyInWorkBuddy() bool { return sameFolder(CodeBuddyDir(), WorkBuddyDir()) }
+
+// sameFolder is whether a and b are one folder on disk, however each is
+// spelled (a link, a letter's case where the disk ignores it). A folder
+// that can't be read is no other.
+func sameFolder(a, b string) bool {
+ x, err := os.Stat(a)
+ if err != nil {
+ return false
+ }
+ y, err := os.Stat(b)
+ return err == nil && os.SameFile(x, y)
+}
// isBuddy is whether agent keeps its sessions the way WorkBuddy does: its
// own, or CodeBuddy Code's.