Files
magpie/internal/sessions/pi_subagents_test.go
T
yetone f5553d7103 sessions: a Pi subagent's calls are its lead session's, on its own model (nzvzbmz78932 on X)
nzvzbmz78932 on X: "在pi子代理中使用了deepseek 但是不显示用量". pi-subagents
(npm 0.88.1) runs each subagent as a Pi of its own, its session in
<lead file's folder>/<run id>/run-<n>/session.jsonl (async and parallel
runs a level deeper), or under extensions/subagent/config.json's
defaultSessionDir, its header's parentSession naming the lead's file.
magpie read only sessions/*/*.jsonl, so those files were never read. The
lead's `subagent` tool result carries the subagents' usage summed, and
piParse counted it on the lead's model and provider: a lead on magpie's
entry made it the gateway's (piGateway), so DeepSeek's calls were on no
page, Usage or Sessions.

- piFilesIn reads every session file under a lead's folder as the
  lead's (non-main, like omp's), and the defaultSessionDir ones through
  their headers' parentSession, up to 8 levels.
- piSubagentsRead: a tool result whose every sessionFile is one of those
  counts nothing, in piParse and piCall; one naming a file not read
  still counts as before.
- The scout's header id is in Session.Subs (pi is idInFile), so a
  subagent on magpie's entry is the lead's row to the gateway too.
- state.codexPath is parsePath, the file read, for piLeadID: a new field
  took state past its 416-byte class (TestStateFitsItsSizeClass).
- parseVersions and callParseVersions pi/raft/cindy raised.

The fixture is a real run: Pi 1.4.0 with pi-subagents 0.88.1 in a
sandbox PI_CODING_AGENT_DIR, a lead and one scout on a relay's
deepseek/deepseek-flash; system prompts dropped, paths rewritten.

Run (mica, at this head):
- go test ./internal/sessions: ok (119s)
- GOOS=linux and GOOS=windows go vet ./internal/sessions: ok; gofmt clean.
- Linux (wsl-run, magpie-desktop): TestPi*, TestOmp*, TestCodex*,
  TestStateFits*: ok
TestPiSubagentCalls: beside the lead and in defaultSessionDir, each with
the lead on the relay and on magpie's entry (the reporter's case).
Broken by hand: without the walk, 3 of 5 calls and 0 of 2 with the lead
on magpie (red); without the tool-result skip, 6 calls, the sum counted
again (red); the Subs check was red while it named the run id.
Not tried: the reporter's own files; a subagent nesting subagents; a
subagent run on magpie's entry against a live gateway.

With f0fb22c6b and 5bc816c1a below it, at this head (mica): go test
./internal/sessions ./internal/agent ./internal/claudecode ok,
./internal/gateway -run TestClaude ok, GOOS=linux and windows go vet of
the four ok, gofmt clean.
2026-11-13 19:07:34 +08:00

152 lines
5.2 KiB
Go

package sessions
import (
"os"
"path/filepath"
"slices"
"sort"
"strings"
"testing"
"time"
)
// nzvzbmz78932 on X: a Pi subagent's DeepSeek calls were on no page. The
// session is Pi 1.4.0's with pi-subagents 0.88.1, run in a sandbox against
// a provider of its own (relay, deepseek/deepseek-flash): the lead called
// the subagent tool once, and pi-subagents ran its scout as a Pi of its own
// with its session in the folder named for the lead's file
// (testdata/pi-subagents; the system prompts left out, the sandbox's
// folders written /work/sub and /home/u/.pi/agent). The lead's tool result
// carries the scout's usage summed. The scout's two calls are the lead
// session's, once, on the scout's model and provider, on Usage and on
// Sessions, whatever the lead runs on: magpie's own entry (the reporter's
// case, its calls the gateway's to count) included.
func TestPiSubagentCalls(t *testing.T) {
const lead = "01a1d480-175f-7297-b482-4707b3e6e44c"
leadFile := filepath.Join("sessions", "--work-sub--", "2026-11-13T10-42-06-815Z_"+lead+".jsonl")
type call struct {
at string
tokens Tokens
}
scout := []call{
{"2026-11-13T10:42:13.418Z", Tokens{Input: 2592, Output: 515}},
{"2026-11-13T10:42:15.393Z", Tokens{Input: 261, Output: 320, CacheRead: 2944}},
}
leads := []call{
{"2026-11-13T10:42:09.509Z", Tokens{Input: 6574, Output: 92}},
{"2026-11-13T10:42:16.967Z", Tokens{Input: 405, Output: 201, CacheRead: 6528}},
{"2026-11-13T10:42:18.859Z", Tokens{Input: 215, Output: 184, CacheRead: 7040}},
}
for _, own := range []bool{false, true} {
for _, gateway := range []bool{false, true} {
name := "beside its lead"
if own {
name = "in defaultSessionDir"
}
if gateway {
name += ", the lead on magpie"
}
t.Run(name, func(t *testing.T) {
claude, _ := setup(t)
resetCalls()
pi := filepath.Join(filepath.Dir(claude), "pi")
copyTree(t, "testdata/pi-subagents", pi)
if gateway {
// the lead on magpie's entry, the scout on a provider
// of its own
p := filepath.Join(pi, leadFile)
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
s := strings.ReplaceAll(string(b), `"provider":"relay","model":"deepseek/deepseek-flash"`, `"provider":"magpie","model":"gpt-6-astra"`)
s = strings.ReplaceAll(s, `"provider":"relay","modelId":"deepseek/deepseek-flash"`, `"provider":"magpie","modelId":"gpt-6-astra"`)
if strings.Contains(s, `"relay"`) {
t.Fatal("the lead's file still names relay")
}
if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
t.Fatal(err)
}
}
if own {
// pi-subagents told to keep every subagent's session
// in a folder of its own: <run id>/run-0/session.jsonl
// there, its header naming the lead's file
runs := filepath.Join(filepath.Dir(claude), "subs", "x")
if err := os.MkdirAll(filepath.Dir(runs), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Rename(filepath.Join(pi, strings.TrimSuffix(leadFile, ".jsonl")), runs); err != nil {
t.Fatal(err)
}
cfg := filepath.Join(pi, "extensions", "subagent", "config.json")
os.MkdirAll(filepath.Dir(cfg), 0o755)
if err := os.WriteFile(cfg, []byte(`{"defaultSessionDir": "`+filepath.ToSlash(runs)+`"}`), 0o644); err != nil {
t.Fatal(err)
}
}
want := append([]call{}, scout...)
if !gateway {
want = append(want, leads...)
}
sort.Slice(want, func(i, j int) bool { return want[i].at < want[j].at })
var got []Call
for _, c := range Calls(time.Time{}) {
if c.Agent == "pi" {
got = append(got, c)
}
}
sort.Slice(got, func(i, j int) bool { return got[i].Time.Before(got[j].Time) })
if len(got) != len(want) {
t.Fatalf("%d calls, want %d: %+v", len(got), len(want), got)
}
for i, w := range want {
c := got[i]
at, _ := time.Parse(time.RFC3339Nano, w.at)
if c.Session != lead || !c.Time.Equal(at) || c.Model != "deepseek/deepseek-flash" || c.Upstream != "relay" ||
c.Cwd != "/work/sub" || c.Tokens != w.tokens {
t.Errorf("call %d: %+v, want %s %+v", i, c, w.at, w.tokens)
}
}
var deepseek Tokens
for _, c := range want {
deepseek.add(c.tokens)
}
var n int
for _, s := range List(0) {
if s.Agent != "pi" {
continue
}
n++
if s.ID != lead || s.Cwd != "/work/sub" {
t.Errorf("session %s %q", s.ID, s.Cwd)
}
var seen bool
for _, m := range s.Models {
if m.Model == "deepseek/deepseek-flash" {
seen = true
if m.Tokens != deepseek {
t.Errorf("deepseek on Sessions: %+v, want %+v", m.Tokens, deepseek)
}
}
}
if !seen {
t.Errorf("no deepseek on Sessions: %+v", s.Models)
}
// a scout on magpie's entry sends its header's id; the gateway's
// pi|<that id> is the lead's row (GatewayKeys)
if !slices.Contains(s.Subs, "01a1d480-2447-77e7-b4d4-1747dc3bd17c") {
t.Errorf("Subs %v, want the scout's session id", s.Subs)
}
if !gateway && s.Tokens != deepseek {
t.Errorf("session tokens %+v, want %+v", s.Tokens, deepseek)
}
}
if n != 1 {
t.Errorf("%d Pi sessions, want the lead's alone", n)
}
})
}
}
}