Files
magpie/internal/sessions/grok_workbuddy_test.go
T
yetone e927f027f3 sessions: Grok Build's and WorkBuddy's sessions are counted too (Aimer779: 用量→会话 增加 Grok Build 和 WorkBuddy 的本地会话统计)
Usage → Sessions and the stats now list Grok Build's and WorkBuddy's local sessions next to the other agents'. The GUI and CLI filters already name them from agent.Clients(), so nothing else needed changing.

Grok Build: the reader looks under $GROK_HOME/sessions (else ~/.grok/sessions), at <url-encoded cwd>/<id>/, reading summary.json and updates.jsonl.
- Tokens come from each turn_completed's usage.modelUsage, per model. Input is counted without the cache it read (inputTokens already includes cachedReadTokens), so the cache isn't counted twice.
- A subagent's session (session_kind "subagent…") counts once, inside the session it ran in.
- A fork's copied updates count only in the session they came from. The fork itself counts from its created_at on.
- The title is the first prompt typed, else Grok's generated_title.
- The hooks that fire on start and end don't count as time at work, so a session closed the next day doesn't run overnight.
- It resumes with `cd <cwd> && grok --resume <id>`.

WorkBuddy: the reader looks at $WORKBUDDY_CONFIG_DIR/projects/**/*.jsonl (else ~/.workbuddy).
- Tokens come from providerData.usage and count once per reply (providerData.messageId), however many of its lines carry it. Cached tokens (inputTokensDetails) are taken out of the input.
- The older snake_case input_tokens/output_tokens are read as well, with the cwd from <id>.meta.json for sessions migrated from its old history.
- The title is custom-title, then ai-title, then the first prompt (without WorkBuddy's <system-reminder> and @scene: blocks).
- There is no resume command, since it is a desktop app.

Tested:
- go test -tags nogui ./... passes. New sanitized fixtures cover both readers: a Grok main session with a subagent, a fork and a hooks-only session, plus a WorkBuddy session with repeated usage lines and a migrated one.
- go build -tags nogui passes for darwin, linux and windows (CGO_ENABLED=0).
- A read-only run against real ~/.grok and ~/.workbuddy: 7 Grok sessions (an 8th, hooks only, left out) and 16 WorkBuddy sessions. Token totals match a raw jq tally of the same files: Grok 293910 in / 4396 out / 610560 cache read, WorkBuddy 13548053 / 621856 / 32321668.
2026-09-28 23:25:18 +08:00

125 lines
3.9 KiB
Go

package sessions
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"testing"
)
const (
grokMain = "0190aaaa-0000-7000-8000-00000000000a"
grokFork = "0190aaaa-0000-7000-8000-00000000000c"
wbID = "22222222-aaaa-4bbb-8ccc-000000000001"
wbOldID = "0123456789abcdef0123456789abcdef"
)
func TestGrok(t *testing.T) {
inZone(t, 0)
home := setupAgents(t)
ss := List(0)
if n := count(ss, "grok"); n != 2 {
t.Fatalf("want 2 Grok Build sessions (the subagent in its session, the one of hooks only left out), got %d", n)
}
g := find(t, ss, "grok", grokMain)
// the first prompt typed, its chunks put together, before the title Grok made
if g.Cwd != "/work/grok" || g.Title != "Port the parser" {
t.Fatalf("grok: %+v", g)
}
// the cache out of the input, the subagent's turn in once
if m := model(g, "grok-4.7-build"); m.Tokens != (Tokens{700, 100, 2800, 0}) {
t.Fatalf("build: %+v", m)
}
if m := model(g, "grok-4.7-mini"); m.Tokens != (Tokens{300, 30, 100, 0}) {
t.Fatalf("mini, the subagent's in: %+v", m)
}
// the hook that ran as it was closed, a day on, isn't when it was at work
if !g.Start.Equal(at("2026-09-27T08:00:00Z")) || !g.Last.Equal(at("2026-09-27T08:02:10Z")) {
t.Fatalf("grok times %s %s", g.Start, g.Last)
}
if runtime.GOOS != "windows" {
if want := "cd '/work/grok' && grok --resume " + grokMain; g.Resume != want {
t.Fatalf("resume %q", g.Resume)
}
}
// the fork: what it copied counted in the session it came from
f := find(t, ss, "grok", grokFork)
if f.Tokens != (Tokens{100, 30, 300, 0}) || f.Title != "Try another way" {
t.Fatalf("fork: %+v", f)
}
if !f.Start.Equal(at("2026-09-27T09:00:00.25Z")) || !f.Last.Equal(at("2026-09-27T09:00:30Z")) {
t.Fatalf("fork times %s %s", f.Start, f.Last)
}
var active int64
for _, d := range StatsFor(0).Days {
for _, a := range d.Active {
if a.Agent == "grok" {
active += a.Seconds
}
}
}
if active < 150 || active > 170 {
t.Fatalf("active %ds, want the main session's 130 and the fork's 30", active)
}
// $GROK_HOME
moved := filepath.Join(filepath.Dir(home), "grok-home")
if err := os.Rename(filepath.Join(home, ".grok"), moved); err != nil {
t.Fatal(err)
}
t.Setenv("GROK_HOME", moved)
Reset()
find(t, List(0), "grok", grokMain)
}
func TestWorkBuddy(t *testing.T) {
inZone(t, 0)
home := setupAgents(t)
ss := List(0)
if n := count(ss, "workbuddy"); n != 2 {
t.Fatalf("want 2 WorkBuddy sessions, got %d", n)
}
w := find(t, ss, "workbuddy", wbID)
if w.Cwd != "/work/wb" || w.Title != "Launch notes draft" || w.Resume != "" {
t.Fatalf("workbuddy: %+v", w)
}
// a reply's usage counted once, however many of its lines carry it
if m := model(w, "hy3"); m.Tokens != (Tokens{400, 120, 2200, 0}) {
t.Fatalf("hy3: %+v", m)
}
if !w.Start.Equal(at("2026-09-27T10:00:00Z")) || !w.Last.Equal(at("2026-09-27T10:00:10Z")) {
t.Fatalf("workbuddy times %s %s", w.Start, w.Last)
}
// brought over from its older history: the cwd in its meta file
o := find(t, ss, "workbuddy", wbOldID)
if o.Cwd != "/work/wb-old" || o.Title != "Quarterly report" {
t.Fatalf("old: %+v", o)
}
if m := model(o, "auto"); m.Tokens != (Tokens{5000, 100, 0, 0}) {
t.Fatalf("auto: %+v", m)
}
// $WORKBUDDY_CONFIG_DIR
moved := filepath.Join(filepath.Dir(home), "wb")
if err := os.Rename(filepath.Join(home, ".workbuddy"), moved); err != nil {
t.Fatal(err)
}
t.Setenv("WORKBUDDY_CONFIG_DIR", moved)
Reset()
find(t, List(0), "workbuddy", wbID)
}
func TestWorkBuddyPrompt(t *testing.T) {
for in, want := range map[string]string{
`[{"type":"input_text","text":"<system-reminder>x</system-reminder>"},{"type":"input_text","text":"@scene:4"},{"type":"input_text","text":"Fix it"}]`: "Fix it",
`"plain words"`: "plain words",
`"<command>x</command>"`: "",
`[{"type":"input_image","image_url":"data:"}]`: "",
} {
if got := wbPrompt(json.RawMessage(in)); got != want {
t.Errorf("wbPrompt(%s) = %q, want %q", in, got, want)
}
}
}