Files
magpie/internal/sessions/manage_quick_test.go
T
yetone bc247a8cc4 sessions: the Sessions page answers before a first read of a long history
hey_boyce on X: "会话tab等的时间有点长", the page standing on its
skeleton rows on its first open. /api/sessions/manage read every file it
had no usable parse of whole before answering: the agent shown, and every
agent with a gateway session for its ids (Codex's 52 GB too while Claude
Code's page was shown). That is every file after a first start, a
cacheVersion bump, or a Claude Code or Codex file whose kept parse can't
be read on after a restart. 536a98316, 936bdccfa, 926ef769f, f193c3865
and efd1b697e made polls and warm reads cheap; this is the cold one.

Now Manage reads at once only what is cheap (a parse that has only grown).
Once more than 32 MB (quickBytes) would be read whole, those files are read
behind the answer, as Stats and List read them, and their rows stand on the
parse kept or on a glance: the file's first 256 KB with its agent's own line
parser (id, folder, first prompt, start) and, for Claude Code, the titles in
its last 64 KB, with the file's time as its last activity. Such a row has
pending: true and no counts. The Sessions page asks again every 1.5 s while
any row is pending, and redraws only the list, so the filter keeps its focus
and the folders shown stay. A glance is never saved in the index (savable),
and a refresh never takes it as a parse (parsed). A small store is read whole
as before, so its rows come complete the first time.

Measured on the Linux box (magpie-desktop), a sandbox HOME with 1,200 Claude
Code files and 600 Codex rollouts (1,985 MB, 2% 20 MB, 10% 2-8 MB) and one
Codex gateway record. Each mode ran in a fresh process, twice, origin/main
449231c19 against this change:
- no index, Claude Code's page, first answer: 1.94 s / 1.83 s before,
  282 ms / 343 ms after;
- no index, Codex's page after it: 8.93 s / 0.73 s before, 1.63 s /
  1.73 s after;
- no index, every row read whole: 10.9 s / 2.6 s before, 2.0 s / 2.1 s
  after;
- with the index: Claude Code 51-61 ms before, 49-51 ms after; Codex
  700-714 ms before, 718-800 ms after.
On the owner's real store (13 GB of Claude Code and 52 GB of Codex files,
counted read-only), a cold first answer read all 65 GB. A glance reads at
most 320 KB a file. The fixture was in the page cache, so these numbers
undersell a cold disk.

Tests:
- TestManageAnswersBeforeFirstReads holds every whole read on parseHook.
  On this change Manage answers with pending, glanced rows and Codex's ids,
  leaves no glance in the cache or a save, and answers a second ask while
  the read is under way. After the read, the rows are counted, no longer
  pending, and have the folder and title the glance showed. Broken by hand
  (Manage back on plain refresh): red at its 30 s fail-safe, "Manage
  waited for the whole read (2 files being read)".
- sessions-pending-fill.test.cjs: pending rows show at once, are asked
  for again on the page clock and filled in with the filter still focused
  and typed into, and then no more asks.

What was run:
- mote: go vet ./internal/sessions ./internal/gui; go test
  ./internal/sessions ok.
- go test ./internal/gui on mote at load 35-67, under another session's
  full GUI suite: TestCoreScriptBudget was red on origin/main too (#4133,
  fixed since in 3959192e9; this change adds no core bytes). Ten
  real-window AppKit tests were killed at their deadlines (#2645, #4125).
- Linux (wsl-run): gofmt, go vet -tags nogui, go test ./internal/sessions
  ok. go test -tags nogui -timeout 40m ./internal/gui (1021 s): red only
  in TestCoreScriptBudget (#4133, the base's) and
  TestRestartHandsTheGatewayOver, a request reaching the old pid after
  the handover (#4167, not on this path). Rebased on aad6546cd, both
  pass 3 of 3 with -count=3. GUI files sessions-pending-fill,
  sessions-manage, sessions-manage-paging, sessions-folder-paging,
  sessions-search and sessions-toolbar in Chromium and WebKit: 44 of 44
  pass.

Not run:
- The full go test ./... suite and the whole GUI suite: mote went
  unreachable mid-run, and WebKit hung there on existing tests too (#4125).
- No real account: the bench uses a generated store.
2026-10-29 12:12:30 +08:00

123 lines
3.5 KiB
Go

package sessions
import (
"sync/atomic"
"testing"
"time"
)
// The Sessions page answers before a first read of its files (hey_boyce on
// X: its placeholder rows stood for a long time on a first open). Its rows
// come from a glance at each file's first lines, say they are pending, and
// the whole read runs behind the answer; the next ask has their counts.
func TestManageAnswersBeforeFirstReads(t *testing.T) {
setup(t)
old := quickBytes
quickBytes = 0 // as a long history: every file over the bar
release := make(chan struct{})
var parses atomic.Int64
parseHook = func() { parses.Add(1); <-release }
opened := false
open := func() {
if !opened {
opened = true
close(release)
}
}
t.Cleanup(func() { open(); behindRuns.Wait(); parseHook, quickBytes = nil, old })
claude := func([]AgentCount, map[string]map[string]bool) string { return "claude" }
got := make(chan ManageView, 1)
go func() { got <- Manage(map[string]bool{"codex": true, "claude": true}, claude) }()
var v ManageView
select {
case v = <-got:
case <-time.After(30 * time.Second): // a fail-safe: the old code never answers
n := parses.Load()
open()
<-got // ended before the test's folder goes
t.Fatalf("Manage waited for the whole read (%d files being read)", n)
}
if len(v.Sessions) < 2 {
t.Fatalf("listed %d sessions before the read", len(v.Sessions))
}
for _, m := range v.Sessions {
if !m.Pending {
t.Fatalf("%s not pending, read whole before the answer", m.ID)
}
if m.Last.IsZero() {
t.Fatalf("glanced row %+v has no time", m.Session)
}
if m.Input+m.Output != 0 {
t.Fatalf("a glance counted tokens: %+v", m.Tokens)
}
}
glanced := map[string]Managed{}
for _, m := range v.Sessions {
glanced[m.ID] = m
}
named := 0
for _, m := range glanced {
if m.Cwd != "" && m.Title != "" {
named++
}
}
if named == 0 {
t.Fatalf("no glanced row has a folder and title: %+v", v.Sessions)
}
if !v.Native["codex"]["01a0bdc9-b5fd-7e63-9658-68bc8dce5ecd"] {
t.Fatalf("codex ids from the glance: %v", v.Native["codex"])
}
// no glance stays in the cache, nor goes into a save
mu.Lock()
for _, m := range v.Sessions {
if s := cache[m.Path]; s != nil {
mu.Unlock()
t.Fatalf("%s: a glance left in the cache", m.Path)
}
}
if len(savable()) != 0 {
mu.Unlock()
t.Fatal("a glance would be saved")
}
mu.Unlock()
// asked again while the read is under way: still answered, still pending
go func() { got <- Manage(map[string]bool{"codex": true}, claude) }()
select {
case v = <-got:
case <-time.After(30 * time.Second):
open()
<-got
t.Fatal("a second ask waited for the read under way")
}
if len(v.Sessions) == 0 || !v.Sessions[0].Pending {
t.Fatalf("second ask: %+v", v.Sessions)
}
open()
behindRuns.Wait()
v = Manage(map[string]bool{"codex": true}, claude)
spent := false
for _, m := range v.Sessions {
if m.Pending {
t.Fatalf("%s still pending after the read", m.ID)
}
spent = spent || m.Input+m.Output > 0 && m.Messages > 0
// the glance showed what the whole read shows
g, ok := glanced[m.ID]
if !ok || g.Cwd != m.Cwd || g.Title != m.Title || g.Path != m.Path {
t.Fatalf("glanced %q %q %q, read %q %q %q", g.ID, g.Cwd, g.Title, m.ID, m.Cwd, m.Title)
}
}
if len(v.Sessions) != len(glanced) {
t.Fatalf("%d glanced, %d read", len(glanced), len(v.Sessions))
}
if !spent {
t.Fatalf("read whole and nothing counted: %+v", v.Sessions)
}
if !v.Native["codex"]["01a0bdc9-b5fd-7e63-9658-68bc8dce5ecd"] {
t.Fatalf("codex ids after the read: %v", v.Native["codex"])
}
}