Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions internal/tui/sidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ var swarmSpawnRe = regexp.MustCompile(`as task (\S+) on team`)
type swarmAgent struct {
id string // e.g. "subagent-1" — the dedup key and fallback name
name string // the task briefing (argHint of the call row), or id when empty
state string // latest reported state (running/done/failed/…), "" until a report lands
finishing bool // done/failed but still lingering before removal (smooth exit)
finishedAt time.Time // when first seen finished (zero until the spinner tick stamps it)
}
Expand All @@ -200,14 +201,6 @@ type swarmAgent struct {
// opaque "subagent-N". Only spawns that produced a result row (success) are
// returned, and the list is deduped by id.
func (m model) swarmSpawnedAgents() []swarmAgent {
// If a swarm_collect has run on any team, the swarm is considered closed
// and we no longer surface its members in the AGENTS sidebar.
for _, row := range m.transcript {
if row.tool == "swarm_collect" && row.kind == rowToolResult {
return nil
}
}

seen := map[string]bool{}
var agents []swarmAgent
// pendingTask holds the task from the most recent unmatched swarm_spawn call
Expand Down Expand Up @@ -252,7 +245,8 @@ func (m model) swarmSpawnedAgents() []swarmAgent {
if status := m.swarmMemberStatus(); len(status) > 0 {
live := agents[:0:0]
for _, a := range agents {
switch status[a.id] {
a.state = status[a.id]
switch a.state {
case "done", "failed", "completed", "cancelled":
doneAt, stamped := m.swarmDoneAt[a.id]
if stamped && m.now().Sub(doneAt) >= sidebarAgentLinger {
Expand All @@ -274,12 +268,19 @@ func (m model) swarmSpawnedAgents() []swarmAgent {
// "– teammate-1 [done] (cyan) <task>" → captures the id and the status word.
var swarmStatusRe = regexp.MustCompile(`(?m)^\s*[-–—]?\s*(\S+)\s+\[([a-zA-Z]+)\]`)

// swarmMemberStatus parses the LATEST swarm_status result in the transcript into
// id → status (lowercased). Empty when no swarm_status has run yet.
// swarmMemberStatus parses the LATEST swarm roster report in the transcript into
// id → status (lowercased). Both swarm_status and swarm_collect results list
// every member with its "[state]", so either one refreshes the live roster; the
// last report in transcript order wins. Empty when no report has run yet. This
// is what lets a swarm_collect that runs while members are still working keep the
// AGENTS panel populated instead of clearing it.
func (m model) swarmMemberStatus() map[string]string {
status := map[string]string{}
for _, row := range m.transcript {
if row.tool != "swarm_status" || row.kind != rowToolResult {
if row.kind != rowToolResult {
continue
}
if row.tool != "swarm_status" && row.tool != "swarm_collect" {
continue
}
latest := map[string]string{}
Expand Down Expand Up @@ -389,7 +390,16 @@ func (m model) sidebarAgentLines(width int) []string {
lines = append(lines, " "+icon+" "+nameStyle.Render(truncateStep(a.name, room)))
continue
}
lines = append(lines, " "+zeroTheme.accent.Render("•")+" "+style.Render(truncateStep(a.name, room)))
// A non-running state (pending/handoff/…) is shown faintly after the task
// so the panel reports the member's actual status; running is left implied
// by the live pulse to keep the common case clean.
nameRoom := room
suffix := ""
if st := strings.TrimSpace(a.state); st != "" && st != "running" {
suffix = " " + zeroTheme.faint.Render(st)
nameRoom = maxInt(4, room-len(st)-1)
}
lines = append(lines, " "+zeroTheme.accent.Render("•")+" "+style.Render(truncateStep(a.name, nameRoom))+suffix)
}
return lines
}
Expand Down
34 changes: 34 additions & 0 deletions internal/tui/sidebar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,40 @@ func TestSwarmAgentsLingerThenDisappearWhenDone(t *testing.T) {
}
}

// Regression: a swarm_collect that runs WHILE members are still working must not
// clear the AGENTS panel. Previously any swarm_collect result wiped the roster,
// so the sidebar showed "no agents spawned" mid-run even with 4 subagents live.
func TestSwarmAgentsStayVisibleWhileCollectRunsMidFlight(t *testing.T) {
m := sidebarTestModel()
m.transcript = append(m.transcript,
transcriptRow{kind: rowToolCall, tool: "swarm_spawn", detail: "build the homepage"},
transcriptRow{kind: rowToolResult, tool: "swarm_spawn", detail: "Spawned subagent as task subagent-1 on team default."},
transcriptRow{kind: rowToolCall, tool: "swarm_spawn", detail: "build the stylesheet"},
transcriptRow{kind: rowToolResult, tool: "swarm_spawn", detail: "Spawned subagent as task subagent-2 on team default."},
// A collect mid-flight, both members still running.
transcriptRow{kind: rowToolResult, tool: "swarm_collect",
detail: "Results for team default: 2 task(s)\n- subagent-1 [running] build the homepage\n- subagent-2 [running] build the stylesheet"},
)

agents := m.swarmSpawnedAgents()
if len(agents) != 2 {
t.Fatalf("running members must survive a mid-flight swarm_collect, got %d: %+v", len(agents), agents)
}
for _, a := range agents {
if a.finishing {
t.Fatalf("a running member must not be marked finishing: %+v", a)
}
if a.state != "running" {
t.Fatalf("swarm_collect should set member state to running, got %q for %s", a.state, a.id)
}
}

plain := stripSidebar(m.sidebarAgentLines(sidebarWidth(m.width)))
if !strings.Contains(plain, "homepage") || !strings.Contains(plain, "stylesheet") {
t.Fatalf("sidebar should list the running members and their tasks:\n%s", plain)
}
}

func TestSidebarHidesNotFoundSpecialistMisroutes(t *testing.T) {
m := sidebarTestModel()
now := time.Now()
Expand Down
Loading