Files
magpie/internal/sessions/omp_test.go
T
yetone 37527c7ad3 sessions: omp's subagents and advisor are their session's to the gateway, not counted again (Jumy on Discord)
omp's task subagent (Kid.jsonl) and advisor (__advisor.jsonl) each have
a header id of their own, and omp sends that id, not the main
session's, in x-session-affinity (seen in a real omp 18.9.1 run through
a fake upstream: the main session's requests carried 01a18ae4-555c-…,
the subagent's 01a18ae4-5671-…). magpie counts those files in the main
session, but the gateway's native keys held only omp|<main id>, so the
gateway's records of the subagent's and advisor's calls were added
again as sessions of their own: in Usage › Sessions' totals
(MergeExternalStats), in the Sessions list (MergeExternal) and in
NativeIDs.

Session.Subs (subIDs) now holds those ids, and GatewayKeys,
MergeExternal and nativeIDsWith take them as the session's own.
GatewayKeys takes Claude Code's Copies too, which the GUI list added
by hand beside it; the stats' native map already had each copy's own
group, so nothing counts differently there.

This is at most a second count of the subagent's and advisor's share,
not a ~1000x: it may not explain the whole ~900M Jumy saw. Not found
in the reader: omp's usage is per call (assistant messages only;
toolResult and compaction carry none), re-reads continue from the
offset only when the head and sampled prefix hashes match.

Tests: TestOmpSubagentsAreTheSessionsToTheGateway (sessions, the
existing omp fixture, whose sub headers match omp 18.9.1's) and
TestOmpSubagentCountedOnceWithTheGateway (gui, /api/sessions/overview
and /api/sessions, from omp 18.9.1's own lines). Broken by hand
(subIDs returning nil): both red, the overview showing the Kid id as a
second gateway session with 5000 input beside the native one.

Run on mote: go test ./internal/sessions -run 'TestOmp|TestRaft',
./internal/gui -run 'TestOmpSubagent|TestTopSessions'; gofmt -l;
go vet, GOOS=linux go vet -tags nogui, GOOS=windows go vet on the two
packages; go build for darwin, linux (nogui), windows.
go test -timeout 45m ./internal/sessions ./internal/gui ./internal/usage:
sessions and usage ok; gui red only in TestMainWindowCorners/macOS_26_SDK,
#3949 (mote's macOS 27, titlebar code this change doesn't reach). The
full ./... was not run before the push: mote had other sessions' full
suites running at load ~180.

Not tried: Jumy's own session files or ledger (not available), so the
rest of the 900M is unexplained; Kimi Code's subagents (it sends no
session header magpie knows, so its gateway rows aren't sessions);
the GUI Playwright suite (no asset changed).
2026-10-30 11:54:46 +08:00

145 lines
4.9 KiB
Go

package sessions
import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
)
const (
ompMain = "019a0000-0000-7000-8000-00000000000a"
ompFork = "019a0000-0000-7000-8000-00000000000c"
ompNamed = "019a0000-0000-7000-8000-00000000000d"
)
// omp's sessions (#303): Pi's kind, with its title line and title changes,
// model_usage entries, and its subagents' and advisor's sessions beside it.
func TestOmp(t *testing.T) {
inZone(t, 0)
home := setupAgents(t)
ss := List(0)
if n := count(ss, "omp"); n != 3 {
t.Fatalf("want 3 omp sessions (the subagents and advisor in theirs), got %d", n)
}
m := find(t, ss, "omp", ompMain)
if m.Cwd != "/work/omp" || m.Title != "Port the parser to omp" {
t.Fatalf("omp: %+v", m)
}
// the task tool's summed usage not counted again
for _, c := range []struct {
model string
want Tokens
}{
{"claude-opus-5-5", Tokens{110, 55, 1500, 200, 0}},
{"claude-haiku-5", Tokens{20, 5, 0, 0, 0}}, // a model_usage entry
{"claude-sonnet-5", Tokens{340, 34, 100, 0, 0}}, // the subagent and its own
{"gpt-6-astra", Tokens{50, 10, 0, 0, 0}}, // the advisor
} {
if got := model(m, c.model); got.Tokens != c.want {
t.Fatalf("%s: %+v, want %+v", c.model, got, c.want)
}
}
if len(m.Models) != 4 || !model(m, "claude-opus-5-5").Priced {
t.Fatalf("models %+v", m.Models)
}
if !m.Start.Equal(at("2026-09-28T08:00:00Z")) || !m.Last.Equal(at("2026-09-28T08:02:00Z")) {
t.Fatalf("omp times %s %s", m.Start, m.Last)
}
want := "cd '/work/omp' && omp --resume " + ompMain
if runtime.GOOS == "windows" {
want = "Set-Location -LiteralPath '/work/omp'; omp --resume " + ompMain
}
if m.Resume != want {
t.Fatalf("resume %q", m.Resume)
}
// the fork: what it copied counted in the session it came from
f := find(t, ss, "omp", ompFork)
if f.Tokens != (Tokens{7, 3, 0, 0, 0}) || f.Title != "Port the parser" {
t.Fatalf("fork: %+v", f)
}
if !f.Start.Equal(at("2026-09-29T09:00:00Z")) || !f.Last.Equal(at("2026-09-29T09:00:20Z")) {
t.Fatalf("fork times %s %s", f.Start, f.Last)
}
// begun from a template: the title it was last given
if n := find(t, ss, "omp", ompNamed); n.Title != "Tidy the tests" {
t.Fatalf("named: %+v", n)
}
// a profile's sessions, and those in $XDG_DATA_HOME/omp
from := filepath.Join(home, ".omp", "agent", "sessions", "--work-omp--")
moves := map[string]string{ompNamed: filepath.Join(home, ".omp", "profiles", "work", "agent", "sessions", "--work-omp--")}
if runtime.GOOS != "windows" {
moves[ompFork] = filepath.Join(os.Getenv("XDG_DATA_HOME"), "omp", "sessions", "--work-omp--")
}
for id, to := range moves {
names, _ := filepath.Glob(filepath.Join(from, "*_"+id+".jsonl"))
if len(names) != 1 {
t.Fatalf("fixture for %s: %v", id, names)
}
os.MkdirAll(to, 0o755)
if err := os.Rename(names[0], filepath.Join(to, filepath.Base(names[0]))); err != nil {
t.Fatal(err)
}
}
Reset()
ss = List(0)
for id := range moves {
m := find(t, ss, "omp", id)
if id == ompNamed && !strings.Contains(m.Resume, "omp --profile work --resume "+id) {
t.Fatalf("profile resume %q", m.Resume)
}
}
}
// Jumy (Discord, 10-30): omp's subagents and advisor each send their own
// session id to the gateway (x-session-affinity, the id in their file's
// header), not the main session's. Their files are counted in the main
// session, so the gateway's records under those ids are the same calls:
// they are the session's own to the gateway, not sessions of their own
// counted again.
func TestOmpSubagentsAreTheSessionsToTheGateway(t *testing.T) {
setupAgents(t)
subs := []string{
"019a0000-0000-7000-8000-00000000001a", // 0-Explore.jsonl
"019a0000-0000-7000-8000-00000000002a", // 0-Explore/0-Find.jsonl
"019a0000-0000-7000-8000-00000000003a", // __advisor.jsonl
}
ss := List(0)
m := find(t, ss, "omp", ompMain)
keys := GatewayKeys(m)
for _, id := range subs {
if !slices.Contains(keys, "omp|"+id) {
t.Errorf("GatewayKeys leave out the subagent %s: %v", id, keys)
}
}
merged := MergeExternal(ss, []ExternalSession{
{Agent: "omp", ID: subs[0], Title: "fail"},
{Agent: "omp", ID: subs[2], Title: "fail"},
{Agent: "omp", ID: "01a18ae4-0000-7000-8000-000000000000", Title: "omp's own"},
})
if n, want := count(merged, "omp"), count(ss, "omp")+1; n != want {
t.Fatalf("the gateway's records of omp's subagents listed as sessions: %d omp sessions, want %d", n, want)
}
ids := NativeIDs(map[string]bool{"omp": true})["omp"]
_, native := StatsAndNative(0)
for _, id := range append([]string{ompMain}, subs...) {
if !ids[id] {
t.Errorf("omp's native ids leave out %s: %v", id, ids)
}
if !native["omp|"+id] {
t.Errorf("the gateway's omp session %s is counted again: %v", id, native)
}
}
// the other sessions keep only their own
for _, id := range []string{ompFork, ompNamed} {
if s := find(t, ss, "omp", id); len(s.Subs) != 0 {
t.Errorf("%s: subagents %v", id, s.Subs)
}
}
}