Skip to content

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

sessions: a Sessions poll lists the session folders once, not twice

yetone51a2068b52ae
diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go
index b4031851d..2ccbdea51 100644
--- a/internal/gui/sessions.go
+++ b/internal/gui/sessions.go
@@ -80,6 +80,8 @@ func gatewaySession(g usage.GatewaySession) sessions.Session {
func sessionRoutes(mux *http.ServeMux, w Windows) {
warmSessions()
mux.HandleFunc("GET /api/sessions", func(rw http.ResponseWriter, r *http.Request) {
+ // the list and the Codex threads' effort each list every session file
+ defer sessions.OneListing()()
n, _ := strconv.Atoi(r.URL.Query().Get("limit"))
// The page draws a session count taken from every session
// (statsFor's Overview) beside this list, so the list is read as
diff --git a/internal/gui/sessions_manage.go b/internal/gui/sessions_manage.go
index 410a18b5c..6cb838e76 100644
--- a/internal/gui/sessions_manage.go
+++ b/internal/gui/sessions_manage.go
@@ -116,6 +116,7 @@ func sessionManageRoutes(mux *http.ServeMux, w Windows) {
// manage is the agents with sessions, and every session of ?agent=
// (the one with the most when none is named), and the trash.
mux.HandleFunc("GET /api/sessions/manage", func(rw http.ResponseWriter, r *http.Request) {
+ defer sessions.OneListing()()
looks := agentLooks()
out := struct {
Agents []manageAgentJSON `json:"agents"`
diff --git a/internal/sessions/one_listing.go b/internal/sessions/one_listing.go
new file mode 100644
index 000000000..fb7632060
--- /dev/null
+++ b/internal/sessions/one_listing.go
@@ -0,0 +1,68 @@
+package sessions
+
+import (
+ "slices"
+ "sync"
+ "time"
+)
+
+// held is the session folders' listings kept while a request that reads
+// them more than once is answered. A Sessions poll lists every Claude
+// Code and Codex file for its list (allFiles), then again for the Codex
+// threads' effort (the Requests state's CallSources): ~21k files, read
+// twice a poll.
+var held struct {
+ sync.Mutex
+ n int
+ since time.Time // when the latest hold began
+ lists map[string]heldList
+}
+
+type heldList struct {
+ at time.Time
+ files []file
+}
+
+// OneListing has the session folders listed once until done is called:
+// the reads between share each folder's listing, taken after the latest
+// hold began, so none sees the files as they were before its own request
+// came. done lets the listings go once no hold is left.
+func OneListing() (done func()) {
+ held.Lock()
+ held.n++
+ held.since = time.Now()
+ held.Unlock()
+ return sync.OnceFunc(func() {
+ held.Lock()
+ if held.n--; held.n == 0 {
+ held.lists = nil
+ }
+ held.Unlock()
+ })
+}
+
+// heldFiles is list(), or, under a hold, the listing of key taken since
+// the latest hold began. The folders are listed outside the lock.
+func heldFiles(key string, list func() []file) []file {
+ held.Lock()
+ if held.n == 0 {
+ held.Unlock()
+ return list()
+ }
+ if l, ok := held.lists[key]; ok && !l.at.Before(held.since) {
+ held.Unlock()
+ return slices.Clone(l.files)
+ }
+ held.Unlock()
+ at := time.Now()
+ files := list()
+ held.Lock()
+ if held.n > 0 && !at.Before(held.since) {
+ if held.lists == nil {
+ held.lists = map[string]heldList{}
+ }
+ held.lists[key] = heldList{at, slices.Clone(files)}
+ }
+ held.Unlock()
+ return files
+}
diff --git a/internal/sessions/one_listing_test.go b/internal/sessions/one_listing_test.go
new file mode 100644
index 000000000..428394f5e
--- /dev/null
+++ b/internal/sessions/one_listing_test.go
@@ -0,0 +1,48 @@
+package sessions
+
+import "testing"
+
+// Under a hold a folder is listed once; a hold begun after a listing has
+// it listed again, so no request reads files from before it came; with
+// no hold left every read lists.
+func TestOneListing(t *testing.T) {
+ n := 0
+ list := func() []file { n++; return []file{{path: "a"}} }
+ read := func() {
+ if fs := heldFiles("k", list); len(fs) != 1 || fs[0].path != "a" {
+ t.Fatalf("heldFiles = %v", fs)
+ }
+ }
+ read()
+ read()
+ if n != 2 {
+ t.Fatalf("listed %d times with no hold, want 2", n)
+ }
+ n = 0
+ done := OneListing()
+ read()
+ read()
+ if n != 1 {
+ t.Fatalf("listed %d times under one hold, want 1", n)
+ }
+ other := OneListing()
+ read()
+ if n != 2 {
+ t.Fatalf("a hold begun after the listing read it: listed %d times, want 2", n)
+ }
+ read()
+ done()
+ read()
+ if n != 2 {
+ t.Fatalf("listed %d times while a hold was left, want 2", n)
+ }
+ other()
+ other()
+ read()
+ if n != 3 {
+ t.Fatalf("listed %d times after the holds ended, want 3", n)
+ }
+ if held.n != 0 || held.lists != nil {
+ t.Fatalf("holds left: %d, lists %v", held.n, held.lists)
+ }
+}
diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go
index 71782de39..eec32444f 100644
--- a/internal/sessions/sessions.go
+++ b/internal/sessions/sessions.go
@@ -503,6 +503,10 @@ func parallel(n, per int, one func(i int)) {
// its workflows (ultracode) in <id>/subagents/workflows/<run>/agent-*.jsonl
// — beside each run's journal.jsonl, which isn't a transcript.
func ccFiles(agent, dir string) []file {
+ return heldFiles("cc\x00"+agent+"\x00"+dir, func() []file { return listCCFiles(agent, dir) })
+}
+
+func listCCFiles(agent, dir string) []file {
projects := filepath.Join(dir, "projects")
var out []file
for _, project := range readDirectory(projects) {
@@ -636,7 +640,7 @@ const zstSuffix = ".zst"
// calls (phantom3792 on X).
func codexFiles() []file {
if !quiet() {
- return listCodexFiles()
+ return heldFiles("codex", listCodexFiles)
}
warmListing.Lock()
defer warmListing.Unlock()