Files
magpie/docs
yetone 38f6bee23e sessions: the manage page reads its agents' files once, and only the agents it shows (#2776)
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.
2026-10-19 03:57:52 +08:00
..