diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index de2ba754b..812e620dc 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -287,7 +287,10 @@ Code's, Claude Desktop's and Cowork's, Codex's (and both in WSL), and, read a file at a time (`wholeCallReaders` in [`call_cache.go`](../../internal/sessions/call_cache.go)), OpenCode's (#680), ZCode's, DeepSeek Harness's (`dsh`), WorkBuddy's, CodeBuddy -Code's (#1779), MiniMax Code's (#1605), Kimi Code's (#1607, +Code's (#1779; its sessions are on the Sessions page too, from the same +`projects//.jsonl` files read as WorkBuddy's, resumed with +`codebuddy --resume ` and deletable, #2657: `TestCodeBuddy`, +`TestCodeBuddySessionsListedUnderTheirName`), MiniMax Code's (#1605), Kimi Code's (#1607, [`calls_kimi.go`](../../internal/sessions/calls_kimi.go)) and Pi's and omp's, here and in WSL (#1951, [`calls_pi.go`](../../internal/sessions/calls_pi.go)). Each skips the diff --git a/internal/gui/sessions_codebuddy_test.go b/internal/gui/sessions_codebuddy_test.go new file mode 100644 index 000000000..634ff0198 --- /dev/null +++ b/internal/gui/sessions_codebuddy_test.go @@ -0,0 +1,110 @@ +package gui + +import ( + "encoding/json" + "io/fs" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/yetone/magpie/internal/sessions" +) + +// CodeBuddy Code's own sessions (~/.codebuddy/projects, the real `codebuddy +// -p` runs in the sessions package's testdata) reach the Sessions page and +// Usage > Sessions under CodeBuddy Code's name and icon, with the command that +// resumes them, and the page offers them to the trash (#2657). +func TestCodeBuddySessionsListedUnderTheirName(t *testing.T) { + h := sandboxHome(t) + src := filepath.Join("..", "sessions", "testdata", "codebuddy") + dst := filepath.Join(h, ".codebuddy") + if err := filepath.WalkDir(src, func(p string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + rel, _ := filepath.Rel(src, p) + if d.IsDir() { + return os.MkdirAll(filepath.Join(dst, rel), 0o755) + } + b, err := os.ReadFile(p) + if err != nil { + return err + } + return os.WriteFile(filepath.Join(dst, rel), b, 0o644) + }); err != nil { + t.Fatal(err) + } + sessions.Reset() + t.Cleanup(sessions.Reset) + + mux := http.NewServeMux() + sessionRoutes(mux, folderOnly{}) + sessionManageRoutes(mux, folderOnly{}) + get := func(path string, v any) { + t.Helper() + w := httptest.NewRecorder() + mux.ServeHTTP(w, httptest.NewRequest("GET", path, nil)) + if w.Code != 200 { + t.Fatalf("%s: %d %s", path, w.Code, w.Body) + } + if err := json.Unmarshal(w.Body.Bytes(), v); err != nil { + t.Fatal(err) + } + } + type named struct { + Agent string `json:"agent"` + Name string `json:"name"` + Icon string `json:"icon"` + Resume string `json:"resume"` + } + var list struct{ Sessions []named } + get("/api/sessions", &list) + n := 0 + for _, s := range list.Sessions { + if s.Agent != "codebuddy" { + continue + } + n++ + if s.Name != "CodeBuddy Code" || s.Icon != "codebuddy-color" { + t.Errorf("listed as %q (%s), want CodeBuddy Code (codebuddy-color)", s.Name, s.Icon) + } + if !strings.Contains(s.Resume, "codebuddy --resume ") { + t.Errorf("resume %q", s.Resume) + } + } + if n != 2 { + t.Fatalf("want 2 CodeBuddy Code sessions, got %d: %+v", n, list.Sessions) + } + var manage struct { + Agents []struct { + named + Deletable bool `json:"deletable"` + } + } + get("/api/sessions/manage", &manage) + found := false + for _, a := range manage.Agents { + if a.Agent == "codebuddy" { + found = true + if a.Name != "CodeBuddy Code" { + t.Errorf("the Sessions page's agent is %q, want CodeBuddy Code", a.Name) + } + if !a.Deletable { + t.Error("the Sessions page offers no trash for CodeBuddy Code's sessions") + } + } + } + if !found { + t.Fatalf("no codebuddy among the Sessions page's agents: %+v", manage.Agents) + } + var stats struct { + Agents map[string]string `json:"agents"` + } + get("/api/sessions/stats?days=0", &stats) + if stats.Agents["codebuddy"] != "CodeBuddy Code" { + t.Errorf("Usage > Sessions' agent strip names it %q: %v", stats.Agents["codebuddy"], stats.Agents) + } +} diff --git a/internal/sessions/agents_test.go b/internal/sessions/agents_test.go index d30825a46..3d74572e5 100644 --- a/internal/sessions/agents_test.go +++ b/internal/sessions/agents_test.go @@ -18,8 +18,8 @@ const ( ) // setupAgents is setup with ZCode's database made and dsh's, Cline's, -// Qoder's, Grok Build's and WorkBuddy's sessions put in the home folder, -// where each keeps them. +// Qoder's, Grok Build's, WorkBuddy's and CodeBuddy Code's sessions put in the +// home folder, where each keeps them. func setupAgents(t *testing.T) (home string) { claude, _ := setup(t) home = filepath.Join(filepath.Dir(claude), "home") @@ -82,6 +82,49 @@ func count(ss []Session, agent string) int { return n } +// CodeBuddy Code's own sessions are listed as its own agent's, read from +// ~/.codebuddy/projects as WorkBuddy's are from ~/.workbuddy/projects +// (#1779): the two `codebuddy -p` runs in testdata/codebuddy, one on a model +// of the user's own, one on a model magpie wired. +func TestCodeBuddy(t *testing.T) { + inZone(t, 0) + setupAgents(t) + ss := List(0) + if n := count(ss, "codebuddy"); n != 2 { + t.Fatalf("want 2 CodeBuddy Code sessions, got %d", n) + } + for _, w := range []struct { + id string + cwd string + }{{"01a12fea-4cae-78ac-8b2b-a1c0f9da9b1c", "/Users/me/work"}, {"01a12fed-6117-7b70-934b-2d2c05250180", "/Users/me/wired"}} { + s := find(t, ss, "codebuddy", w.id) + // the only line that names a folder is the session's own + if s.Cwd != w.cwd || s.Title != "Read /etc/hosts and tell me what it names" { + t.Fatalf("%s: %+v", w.id, s) + } + // a run's two lines — the tool call's and the text after it — carry + // the same cumulative usage: 27017 in, of them 26496 read from the cache, + // and 115 out. Each is counted once, as WorkBuddy's summary does + want := Tokens{Input: 2 * (27017 - 26496), Output: 2 * 115, CacheRead: 2 * 26496} + if m := model(s, "fake-m1"); m.Tokens != want { + t.Fatalf("%s, fake-m1: %+v", w.id, m) + } + // picked up again by its CLI in its folder, as Claude Code's are + run := "codebuddy --resume " + w.id + wantResume := "cd '" + w.cwd + "' && " + run + if runtime.GOOS == "windows" { + wantResume = "Set-Location -LiteralPath '" + w.cwd + "'; " + run + } + if s.Resume != wantResume { + t.Fatalf("%s: resume %q, want %q", w.id, s.Resume, wantResume) + } + } + // its files are its own, so the Sessions page can move them to the trash + if !Deletable("codebuddy") { + t.Fatal("CodeBuddy Code's sessions are not deletable") + } +} + func TestZCode(t *testing.T) { inZone(t, 0) setupAgents(t) @@ -278,7 +321,7 @@ func TestDirsMore(t *testing.T) { home := setupAgents(t) d := Dirs() want := []string{filepath.Join(home, ".zcode"), filepath.Join(home, ".dsh"), filepath.Join(home, ".cline", "data", "sessions"), filepath.Join(home, ".qoder"), filepath.Join(home, ".qoder-cn"), - filepath.Join(home, ".grok"), filepath.Join(home, ".workbuddy"), filepath.Join(home, ".omp", "agent")} + filepath.Join(home, ".grok"), filepath.Join(home, ".workbuddy"), filepath.Join(home, ".codebuddy"), filepath.Join(home, ".omp", "agent")} if len(d) != 2+len(want) { t.Fatalf("dirs %v", d) } diff --git a/internal/sessions/calls_workbuddy.go b/internal/sessions/calls_workbuddy.go index e1b238f22..63ae13e27 100644 --- a/internal/sessions/calls_workbuddy.go +++ b/internal/sessions/calls_workbuddy.go @@ -44,7 +44,7 @@ func workbuddyCallFiles() []file { // message lines, and the same models.json, so its calls are read as // WorkBuddy's are, from its own folder. func codebuddyCallFiles() []file { - return buddyFiles("codebuddy", CodeBuddyDir()) + return codebuddyFiles() } // CodeBuddyDir is CodeBuddy Code's folder: $CODEBUDDY_CONFIG_DIR, else diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go index 06e2ce049..46c3bc6fc 100644 --- a/internal/sessions/manage.go +++ b/internal/sessions/manage.go @@ -58,7 +58,7 @@ type AgentCount struct { // those kept as files of their own, in a layout magpie knows whole. func Deletable(agent string) bool { switch agent { - case "claude", "qoder", "qoder-cn", "workbuddy", "codex", "pi", "omp", "cursor", "dsh": + case "claude", "qoder", "qoder-cn", "workbuddy", "codebuddy", "codex", "pi", "omp", "cursor", "dsh": return true } return false diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 122fe5892..153337647 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -2,7 +2,7 @@ // files — Claude Code's projects/*/.jsonl (and Qoder's, the same kind), // Codex's rollout files, OpenCode's database (or its older JSON files) and // ZCode's, Pi's session files and omp's, DeepSeek Harness's, Cline's, Grok -// Build's, WorkBuddy's, Droid's, Cursor CLI's and Alma's chat stores — with the tokens each spent, what that cost at the +// Build's, WorkBuddy's and CodeBuddy Code's, Droid's, Cursor CLI's and Alma's chat stores — with the tokens each spent, what that cost at the // effective price, and the command that resumes it. It only ever reads the // agents' folders. // @@ -118,7 +118,7 @@ type Session struct { // Gateway: seen only through magpie's gateway, with no file of the // agent's on this computer (external.go) Gateway bool `json:"gateway,omitempty"` - Agent string `json:"agent"` // magpie agent id: claude, codex, opencode, pi, omp, zcode, dsh, cline, qoder, qoder-cn, grok, workbuddy, droid, cursor, hermes, alma, kimi + Agent string `json:"agent"` // magpie agent id: claude, codex, opencode, pi, omp, zcode, dsh, cline, qoder, qoder-cn, grok, workbuddy, codebuddy, droid, cursor, hermes, alma, kimi ID string `json:"id"` Cwd string `json:"cwd"` Title string `json:"title"` // the name it was given, else the agent's own title, else the first prompt @@ -473,7 +473,7 @@ func allFiles() []file { var out []file for _, fs := range [][]file{callFiles(), openCodeFiles(), piFiles(), wslFiles("pi", "omp", "kimi"), zcodeFiles(), dshFiles(), clineFiles(), ccFiles("qoder", QoderDir("qoder")), ccFiles("qoder-cn", QoderDir("qoder-cn")), - grokFiles(), workbuddyFiles(), droidFiles(), ompFiles(), cursorFiles(), hermesFiles(), almaFiles(), kimiFiles()} { + grokFiles(), workbuddyFiles(), codebuddyFiles(), droidFiles(), ompFiles(), cursorFiles(), hermesFiles(), almaFiles(), kimiFiles()} { out = append(out, fs...) } return out @@ -493,6 +493,7 @@ func Dirs() []string { {QoderDir("qoder-cn"), filepath.Join(QoderDir("qoder-cn"), "projects")}, {GrokDir(), filepath.Join(GrokDir(), "sessions")}, {WorkBuddyDir(), filepath.Join(WorkBuddyDir(), "projects")}, + {CodeBuddyDir(), filepath.Join(CodeBuddyDir(), "projects")}, {OmpDir(), filepath.Join(OmpDir(), "sessions")}, {FactoryDir(), filepath.Join(FactoryDir(), "sessions")}, {CursorDir(), filepath.Join(CursorDir(), "chats")}, @@ -1175,7 +1176,7 @@ func parserFor(agent string) sessionParser { return sessionParser{line: codexBody} case "pi", "omp": return sessionParser{line: piParse} - case "workbuddy": + case "workbuddy", "codebuddy": return sessionParser{line: workbuddyLine} default: return sessionParser{line: claudeLine, claude: true} @@ -1219,7 +1220,7 @@ func parse(f file, old *state) *state { if err == nil { s.Off = off } - if f.agent == "workbuddy" && s.Cwd == "" { + if isBuddy(f.agent) && s.Cwd == "" { workbuddyMeta(s, f.path) } return s @@ -1477,6 +1478,10 @@ func resumeCommand(distro, agent, id, cwd, path string) string { run = "qoderclicn --resume " + id case "grok": run = "grok --resume " + id + case "codebuddy": + // its CLI's -r, --resume [sessionId], one bin of the package's + // three (codebuddy, cbc, codebuddy-code) + run = "codebuddy --resume " + id case "omp": run = "omp --resume " + id if profile := ompProfile(path); profile != "" { diff --git a/internal/sessions/workbuddy.go b/internal/sessions/workbuddy.go index 077bfcf5f..0858034b0 100644 --- a/internal/sessions/workbuddy.go +++ b/internal/sessions/workbuddy.go @@ -40,6 +40,14 @@ func WorkBuddyDir() string { // a session's own .jsonl, and a subagent's under /subagents/. func workbuddyFiles() []file { return buddyFiles("workbuddy", WorkBuddyDir()) } +// codebuddyFiles are the same under CodeBuddy Code's projects/: the CLI keeps +// them as its desktop sibling does, one agent's folder apart (#1779). +func codebuddyFiles() []file { return buddyFiles("codebuddy", CodeBuddyDir()) } + +// isBuddy is whether agent keeps its sessions the way WorkBuddy does: its +// own, or CodeBuddy Code's. +func isBuddy(agent string) bool { return agent == "workbuddy" || agent == "codebuddy" } + // buddyFiles are the sessions under projects/ in dir, as agent's: WorkBuddy's, // or CodeBuddy Code's, which keeps them the same way. func buddyFiles(agent, dir string) []file {