Files
magpie/internal/gui/sessions_subagents_test.go
T
yetone c516841e46 sessions: a Claude Code, Qoder, OpenCode or Kimi Code session lists its subagents, and the transcript route reads one by sub= (#2261, infinitr0us)
What changes:

- A session's subagents are now on the session, as Session.Subagents (subagentsOf in the new internal/sessions/subagents.go). Each one has its id, title, kind, parent subagent, start and last.
  - Claude Code and Qoder: subagents/agent-<id>.jsonl. The type, description and parentAgentId come from agent-<id>.meta.json, through ccMeta, which SubagentParent also uses now. With no meta file, the title is the first user line. A compaction's own run (agent-acompact-*) is left out.
  - OpenCode: child sessions found through ocRoots, which now records each file's parent. The title is split from "<description> (@<agent> subagent)"; "Child session - <time>" counts as no title.
  - Kimi Code: agents/agent-<n>/wire.jsonl. The title is the first turn.prompt. Kimi 1.5's subagent.spawned record adds the description, type and parent agent.
- GET /api/sessions/transcript takes sub=. It is resolved only among the subagents the session lists (SubagentTranscript; ErrNoSubagent otherwise), never as a path. A subagent's file path is not sent (Path is json:"-").
- Codex is unchanged: its subagents are sessions of their own, with parent_session.

Fixtures:

- Claude Code: the meta and line shapes are copied from real files under ~/.claude/projects, with their words and ids replaced.
- OpenCode: titles of both the 1.x and 2.x forms.
- Kimi 1.4: the existing redacted reporter fixture.
- Kimi 1.5: the record is built from Kimi Code's schema (agent-core-v2 SubagentSpawned), not from a real file.

What was run:

- go test ./internal/sessions ./internal/gui. TestClaudeSubagents, TestQoderSubagents, TestOpenCodeSubagents (JSON and db), TestKimiSubagents and TestSessionSubagentTranscript are new.
- Broken by hand: with the subagentsOf call removed from assemble, 4 sessions tests and the gui test fail. Restored afterwards.
- gofmt -l is clean. go vet passes for darwin, linux (-tags nogui) and windows.
- The full suite (go test -p 4 ./...) under a temp HOME, at 5618ade1e with both commits: green (exit 0).
- Real use: a sandbox magpie (temp HOME, ports 3486/3487) against read-only copies of real Claude Code sessions, including a real nested pair (a subagent that started a subagent). The listing showed 5 subagents, and the nested subagent's transcript opened with 256 parts through sub=.

Not tried:

- No real OpenCode child sessions: this Mac's opencode.db has none.
- No real Kimi Code sessions.
- Qoder only with the fixture.
2026-10-15 08:23:54 +08:00

80 lines
3.4 KiB
Go

package gui
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/sessions"
)
// The Sessions page reads a subagent's conversation by its session and its
// id among the subagents that session lists (#2261), never by a path.
func TestSessionSubagentTranscript(t *testing.T) {
h := sandboxHome(t)
sessions.Reset()
t.Cleanup(sessions.Reset)
const id = "11111111-2222-3333-4444-555555555555"
proj := filepath.Join(h, ".claude", "projects", "-work-app")
write := func(p, s string) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
t.Fatal(err)
}
}
write(filepath.Join(proj, id+".jsonl"),
`{"parentUuid":null,"isSidechain":false,"type":"user","message":{"role":"user","content":"Find the callers"},"uuid":"u1","timestamp":"2026-09-20T10:00:00.000Z","cwd":"/work/app","sessionId":"`+id+`"}`+"\n")
write(filepath.Join(proj, id, "subagents", "agent-a1.jsonl"),
`{"parentUuid":null,"isSidechain":true,"agentId":"a1","type":"user","message":{"role":"user","content":"Search the repo"},"uuid":"s1","timestamp":"2026-09-20T10:01:00.000Z","cwd":"/work/app","sessionId":"`+id+`"}`+"\n"+
`{"parentUuid":"s1","isSidechain":true,"agentId":"a1","message":{"model":"claude-haiku-4-5-20251001","id":"msg_s1","type":"message","role":"assistant","content":[{"type":"text","text":"found three"}],"usage":{"input_tokens":10,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"output_tokens":5}},"type":"assistant","uuid":"s2","timestamp":"2026-09-20T10:02:00.000Z","cwd":"/work/app","sessionId":"`+id+`"}`+"\n")
write(filepath.Join(proj, id, "subagents", "agent-a1.meta.json"), `{"agentType":"Explore","description":"Search the repo","spawnDepth":1}`)
mux := http.NewServeMux()
sessionRoutes(mux, folderOnly{})
sessionManageRoutes(mux, folderOnly{})
get := func(path string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("GET", path, nil))
return w
}
var list struct {
Sessions []sessions.Session `json:"sessions"`
}
if err := json.Unmarshal(get("/api/sessions/manage?agent=claude").Body.Bytes(), &list); err != nil {
t.Fatal(err)
}
if len(list.Sessions) != 1 || len(list.Sessions[0].Subagents) != 1 || list.Sessions[0].Subagents[0].ID != "a1" || list.Sessions[0].Subagents[0].Kind != "Explore" {
t.Fatalf("listing %+v", list.Sessions)
}
if strings.Contains(get("/api/sessions/manage?agent=claude").Body.String(), "agent-a1.jsonl") {
t.Fatal("a subagent's path is listed")
}
var tr sessions.Transcript
w := get("/api/sessions/transcript?agent=claude&id=" + id + "&sub=a1")
if err := json.Unmarshal(w.Body.Bytes(), &tr); err != nil {
t.Fatal(err)
}
if len(tr.Parts) != 2 || tr.Parts[0].Text != "Search the repo" || tr.Parts[1].Text != "found three" {
t.Fatalf("subagent transcript: %s", w.Body)
}
// without sub, the session's own
w = get("/api/sessions/transcript?agent=claude&id=" + id)
if !strings.Contains(w.Body.String(), "Find the callers") || strings.Contains(w.Body.String(), "found three") {
t.Fatalf("session transcript: %s", w.Body)
}
for _, sub := range []string{"nope", "../" + id, "a1.meta"} {
if w := get("/api/sessions/transcript?agent=claude&id=" + id + "&sub=" + sub); w.Code == http.StatusOK || strings.Contains(w.Body.String(), "Find the callers") {
t.Fatalf("sub %q: %d %s", sub, w.Code, w.Body)
}
}
}