Files
magpie/internal/gui/sessions_gateway_empty_test.go
T
Flandern1211 82f09e3248 sessions: Codex's leftover review threads are no rows, and its one-session folders are one group
Deleting a thread in Codex leaves its subagent rollouts behind: the
guardian's review threads, titled by the prompt it asks ("The following
is the Codex agent history whose request action you are assessing…"),
and the title threads it opens. magpie reads every rollout, so each was
a session of its own, and one whose thread was gone had nothing to hang
under. On this computer 642 Codex sessions held 422 subagent threads,
86 of them with their parent thread's file gone (and 2 more under one of
those): they filled folders of their own (Documents\Codex\2026-08-17\
shan, wang, wei…), and the bar counted 531 sessions made with another
provider, 371 of them review threads no one ever opened. Codex Desktop
also makes a folder for every chat started without a project
(~/Documents/Codex/<date>/<name>, its outputs/ and work/ in it, named
from the prompt): 36 of them here were 36 folders of one session each.

listAgentWith now drops a Codex subagent thread whose parent is not
listed, to a fixed point, so a chain of them goes too; the agent's tab
counts the rows it lists. The "Scratch sessions" group that commit
e373adddf gave ZCode's draft folders now holds Codex's own per-session
folders as well (that one, Documents\Codex\<date>, a probe in a temp
folder, worktrees, another app's hidden folder). The provider bar counts
Codex's own threads only (role main or unknown): 531 became 160 here.
And a gateway session no call of which was answered and which spent
nothing is no row either (usage.GatewaySession.SpentNothing): the three
empty rows under "Seen through the gateway" were Codex's failed
title calls (kind thread_title, 400/499, 0 tokens), their ids ones no
file of Codex's has.

Rows: 642 Codex sessions to 554 (88 review threads gone), 422 subagent
threads to 334; 36 folders (51 sessions) folded into Scratch sessions;
3 gateway rows to 0, so "Seen through the gateway" is empty here. Usage
still counts every one of those calls, and nothing was deleted.

Run:
- go test ./internal/sessions -run 'TestCodexSubagentWhoseParentIsGone|TestZCode' -count=1: ok.
- go test ./internal/gui -run 'Session|Gateway|Manage' -count=1: ok.
- go build ./...; go vet ./internal/gui ./internal/usage ./internal/sessions; gofmt -l; git diff --check; node --check client.js: clean.
- go test ./internal/sessions -count=1: the same failures as without this
  change (TestAgySessions, TestAgyRowOnly, TestAntigravityAppFolder,
  TestCCListingKeepsItsNames: path separators and allocation counts on
  this Windows box).
- go test ./internal/usage -count=1: TestDesktopSessionAccountMetadata
  fails without this change too.

Broken by hand:
- without keptSubagents the 86 rows come back
  (TestCodexSubagentWhoseParentIsGoneIsNoRow fails);
- hiding a gateway session on "spent nothing" alone breaks
  TestSessionsSameWithoutRows and TestGatewayConversationAPI: a call
  answered with no tokens is still a session, so SpentNothing also asks
  that none of its calls was answered;
- counting subagent threads in the provider bar says 531 here, 371 of
  them review threads.

Not changed: the review threads' calls are still counted by Usage and by
the Sessions page's stats; their rollouts stay on disk (Deleting the
parent does not delete them); the scratch grouping is worked out on the
page (client.js), so Usage's folder filter still lists those folders by
their paths.
2026-11-11 15:09:32 +08:00

66 lines
1.9 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 that spent nothing at all — Codex's failed title and
// review calls carry an id no file of its own has — has nothing to show and
// is no row on either list (usage.GatewaySession.SpentNothing; flandern:
// three empty rows under "Seen through the gateway").
func TestGatewaySessionThatSpentNothingIsNoRow(t *testing.T) {
sandboxHome(t)
now := time.Now().Add(-time.Hour).Truncate(time.Second)
if err := os.MkdirAll(filepath.Dir(usage.Path()), 0o700); err != nil {
t.Fatal(err)
}
f, err := os.Create(usage.Path())
if err != nil {
t.Fatal(err)
}
enc := json.NewEncoder(f)
enc.Encode(usage.Record{Time: now, Agent: "codex", Provider: "fake", Model: "m", Session: "spent", Input: 10, Status: 200})
enc.Encode(usage.Record{Time: now, Agent: "codex", Provider: "fake", Model: "m", Session: "empty", Status: 400})
f.Close()
sessions.Reset()
t.Cleanup(sessions.Reset)
mux := http.NewServeMux()
sessionRoutes(mux, folderOnly{})
sessionManageRoutes(mux, folderOnly{})
ids := func(path string) []string {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
if w.Code != 200 {
t.Fatalf("%s: %d %s", path, w.Code, w.Body)
}
var out struct {
Sessions []sessions.Session `json:"sessions"`
}
if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
var got []string
for _, s := range out.Sessions {
got = append(got, s.ID)
}
return got
}
for _, path := range []string{"/api/sessions", "/api/sessions/manage?agent=codex"} {
got := ids(path)
if len(got) != 1 || got[0] != "spent" {
t.Errorf("%s lists %v, want the session that spent something only", path, got)
}
}
}