Files
magpie/internal/gui/gateway_top_session_test.go
T
yetone 6e17d58ebf usage: a top session only the gateway saw says "Gateway session", not "(no prompt)" with no folder (#2285)
The reporter's Usage › Sessions card listed seven Pi sessions on
deepseek-v4-pro as "(no prompt)" with no folder, and their routing group
has no DeepSeek. Those rows were gateway projections: Pi asked magpie for
deepseek/deepseek-v4-pro (every served model is in Pi's models.json), most
likely through a pi-subagents child, whose session file is in a temp folder
magpie never reads. The card gave no sign of that, so it read as Pi's own
sessions with an empty prompt, and our first reply took it that way.

The overview's summaries from MergeExternalStats now carry gateway: true,
as MergeExternal's list rows already did. The card shows "Gateway session"
where the folder would be, "(prompt not recorded)" as the title when no
text was kept, and a tooltip pointing to Record gateway conversations.
Strings in zh, zh-TW, ja and de. gateway-sessions.md says it.

Run in this worktree (a1f7b8c13 base):
- go test -tags nogui -p 1 -count=1 -run 'TestTopSessionsMarkWhatOnlyTheGatewaySaw|TestGatewaySession|Overview|Session' ./internal/gui/: ok
- go test -tags nogui -p 1 -count=1 -run 'External|Overview|Stats|Gateway' ./internal/sessions/: ok
- Gateway: true removed by hand: TestTopSessionsMarkWhatOnlyTheGatewaySaw red
  (the Pi record, provider deepseek, came back with cwd "" and title "",
  no gateway field), green with it back.
- sessions-overview.test.cjs: 28/28 in chromium and webkit; with the
  page.js change undone by hand, red in en and zh ("(no prompt)").
- i18n-keys, gui-zh-tw, gui-de, gui-ja (chromium): pass.
- gofmt -l clean; go vet -tags nogui for darwin, linux, windows on
  internal/sessions and internal/gui: ok.
Not run: the full Go suite and the full GUI suite.

Full suite: SUITE_LINE
2026-10-15 23:08:05 +08:00

74 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"time"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/usage"
)
// #2285: a Pi subagent's session file sits in a temp folder magpie never
// reads, so the sessions that spent the most (Usage › Sessions) knew its
// DeepSeek turns only from the gateway's records: no folder, no prompt,
// shown like a native session whose prompt was empty. The overview marks
// such a session as the gateway's, and a native one not, so the card can
// say which it is.
func TestTopSessionsMarkWhatOnlyTheGatewaySaw(t *testing.T) {
h := sandboxHome(t)
claude := filepath.Join(h, ".claude")
t.Setenv("CLAUDE_CONFIG_DIR", claude)
proj := filepath.Join(claude, "projects", "-work")
if err := os.MkdirAll(proj, 0o755); err != nil {
t.Fatal(err)
}
now := time.Now()
at := now.Add(-time.Hour).Format(time.RFC3339)
line := fmt.Sprintf(`{"type":"user","message":{"role":"user","content":"hello"},"uuid":"u","timestamp":%q,"cwd":"/work","sessionId":"native"}`+"\n"+
`{"type":"assistant","message":{"model":"claude-sonnet-5","id":"msg_1","role":"assistant","content":[{"type":"text","text":"done"}],"usage":{"input_tokens":10,"output_tokens":2}},"uuid":"a","timestamp":%q,"cwd":"/work","sessionId":"native"}`+"\n", at, at)
if err := os.WriteFile(filepath.Join(proj, "native.jsonl"), []byte(line), 0o600); err != nil {
t.Fatal(err)
}
sessions.Reset()
t.Cleanup(sessions.Reset)
t.Cleanup(func() { statsMemo.Lock(); statsMemo.m = nil; statsMemo.Unlock() })
statsMemo.Lock()
statsMemo.m = nil
statsMemo.Unlock()
// what the gateway recorded of Pi asking for magpie/deepseek/deepseek-v4-pro
usage.Append(usage.Record{Time: now.Add(-time.Minute), Agent: "pi", Session: "0199f1c2-subagent", Requested: "deepseek/deepseek-v4-pro", Provider: "deepseek", Model: "deepseek-v4-pro", Input: 2000, Output: 300, CacheRead: 60000, Status: 200})
mux := http.NewServeMux()
sessionRoutes(mux, folderOnly{})
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", "/api/sessions/overview?days=7", nil))
if w.Code != 200 {
t.Fatalf("overview: %d %s", w.Code, w.Body)
}
var ov struct {
Top map[string][]map[string]any `json:"top"`
}
if err := json.Unmarshal(w.Body.Bytes(), &ov); err != nil {
t.Fatal(err)
}
seen := map[string]any{}
for _, s := range ov.Top["tokens"] {
seen[fmt.Sprint(s["agent"], ":", s["id"])] = s["gateway"]
}
if len(seen) != 2 {
t.Fatalf("top sessions: %s", w.Body)
}
if g := seen["pi:0199f1c2-subagent"]; g != true {
t.Errorf("the gateway-only Pi session isn't marked the gateway's (gateway=%v): %s", g, w.Body)
}
if g, ok := seen["claude:native"]; !ok || g != nil {
t.Errorf("the native session is marked the gateway's (gateway=%v): %s", g, w.Body)
}
}