GET /api/sessions/manage went over every agent's session files three
times: sessions.Agents() for the counts, sessions.List(All) only to build
the set that keeps a gateway session its agent keeps too from being
counted twice, and sessions.ListAgent for the list. List(All) also read
every agent's changed files again and put every session together, prices
and titles included, to take its id. So a page showing Claude Code's
sessions parsed Hermes's and Codex's writes too.
sessions.Manage now does it in one pass over the files, under one hold
of the locks: the counts, the ids of the sessions of the agents that have
gateway sessions (nativeIDs: the id from the file's key, or from its
parse for the agents that write it inside the file, idInFile, which are
the only ones read), then the picked agent's list. The handler's pick
runs as a callback so the counts can include the gateway sessions. A
change to an agent not shown and with no gateway sessions waits for a
page that shows it. assemble's id rule moved into sessionID, which both
use.
That set was also wrong in two ways. List leaves out a session nothing
was said in and stops at 5000 (All), while the list beside it keeps
every one, so a gateway session whose agent's file was empty (or among
the oldest of more than 5000) was counted twice and listed once. The
set is now every native session, the same as the list's, and
POST /api/sessions/delete, which tells a listed gateway session from a
native one, uses it too (sessions.NativeIDs).
docs/subsystems/gateway-sessions.md says so.
Tests:
- TestManageReadsOnlyTheAgentsItShows: Codex's files changed, Claude
Code's page parses none of them; asked for Codex's ids it reads them;
the native ids equal the list's, the empty session included. Red with
Manage reading every file as List(All) did ("read 2 of them").
- TestManageCountsAGatewaySessionTheAgentKeepsOnce: a gateway session
whose Claude Code file has nothing said in it is counted once. Red on
the old handler: "one session listed, counted [{claude 2}]".
Measured on the synthetic sandbox HOME (3000 Claude Code, 1500 Codex,
2000 Hermes sessions; no gateway sessions), CPU time of the request
(load average ~250 from other sessions, so wall times are noisy), the
parent bbee24810 against this change, the same run of each:
first manage?agent=claude after a restart 2164 -> 627 ms CPU
(15697 -> 10554 ms wall)
warm manage?agent=claude 173-222 ms -> 64-88 ms CPU
warm manage?agent=hermes 176-243 ms -> 56-70 ms CPU
warm manage?agent=codex 237-269 ms -> 155-162 ms CPU (after its
first, which reads Codex's model/effort)
Run: go test ./internal/sessions -count=1, go test ./internal/gui -count=1;
gofmt -l clean; go vet. Full suite and cross-OS vet are recorded on the
last commit of this series.
62 lines
2.1 KiB
Go
62 lines
2.1 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// A gateway session whose agent keeps it too is one session, in the count
|
|
// as in the list, though nothing was said in the agent's own file: the
|
|
// count told them apart through List, which leaves such a file out (and
|
|
// stops at 5000), while the list kept every one (#2776).
|
|
func TestManageCountsAGatewaySessionTheAgentKeepsOnce(t *testing.T) {
|
|
h := sandboxHome(t)
|
|
sessions.Reset()
|
|
t.Cleanup(sessions.Reset)
|
|
const id = "3c9e1f2a-5b6d-4e7f-8a9b-0c1d2e3f4a5b"
|
|
dir := filepath.Join(h, ".claude", "projects", "-work-app")
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
line := `{"type":"file-history-snapshot","messageId":"m","snapshot":{"messageId":"m","trackedFileBackups":{},"timestamp":"2026-09-19T08:00:00.000Z"},"isSnapshotUpdate":false}` + "\n"
|
|
if err := os.WriteFile(filepath.Join(dir, id+".jsonl"), []byte(line), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
usage.Append(usage.Record{Time: time.Now().Add(-2 * time.Hour), Agent: "claude", Session: id, Provider: "deepseek", Model: "deepseek-flash", Input: 40, Output: 9, Status: 200})
|
|
mux := http.NewServeMux()
|
|
sessionManageRoutes(mux, folderOnly{})
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/sessions/manage?agent=claude", nil))
|
|
if w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
var m struct {
|
|
Agents []struct {
|
|
Agent string `json:"agent"`
|
|
Count int `json:"count"`
|
|
} `json:"agents"`
|
|
Agent string `json:"agent"`
|
|
Sessions []struct {
|
|
ID string `json:"id"`
|
|
Gateway bool `json:"gateway"`
|
|
} `json:"sessions"`
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if m.Agent != "claude" || len(m.Sessions) != 1 || m.Sessions[0].ID != id || m.Sessions[0].Gateway {
|
|
t.Fatalf("listed %q: %+v", m.Agent, m.Sessions)
|
|
}
|
|
if len(m.Agents) != 1 || m.Agents[0].Count != 1 {
|
|
t.Fatalf("one session listed, counted %+v", m.Agents)
|
|
}
|
|
}
|