Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

Count running/idle subagents across the whole subtree in the bar (#2218)

Florian Brandfb2db8ee1b61
diff --git a/packages/coding-agent/.changes/subagents-bar-subtree-counts.md b/packages/coding-agent/.changes/subagents-bar-subtree-counts.md
new file mode 100644
index 0000000000..f7fb6ab91a
--- /dev/null
+++ b/packages/coding-agent/.changes/subagents-bar-subtree-counts.md
@@ -0,0 +1 @@
+- Changed the subagents bar to count running, idle, and inactive agents across the whole subagent subtree instead of only direct children.
diff --git a/packages/coding-agent/src/modes/agents-view/agents-view-state.ts b/packages/coding-agent/src/modes/agents-view/agents-view-state.ts
index a282b08745..6d8dcbbbbd 100644
--- a/packages/coding-agent/src/modes/agents-view/agents-view-state.ts
+++ b/packages/coding-agent/src/modes/agents-view/agents-view-state.ts
@@ -635,15 +635,48 @@ function getParentKeys(summary: SessionSummary): string[] {
].filter((key): key is string => key !== undefined);
}
-/** Direct-child linkage over getParentKeys, shared by the view tree and the chat subagents bar. */
-export function isDirectAgentChild(
- child: SessionSummary,
+// The parent side of getParentKeys: the keys by which a session is referenced as a parent.
+function parentIdentityKeys(summary: {
+ activeSessionId?: string | undefined;
+ sessionId?: string | undefined;
+ sessionFile?: string | undefined;
+}): string[] {
+ return [
+ summary.activeSessionId !== undefined ? `active:${summary.activeSessionId}` : undefined,
+ summary.sessionId !== undefined ? `session:${summary.sessionId}` : undefined,
+ summary.sessionFile !== undefined ? fileIdentity(summary.sessionFile) : undefined,
+ ].filter((key): key is string => key !== undefined);
+}
+
+/**
+ * Every subagent summary descending from the parent session, breadth-first over the shared parent
+ * linkage. Rows of any lifecycle link so live descendants stay reachable; callers decide what counts.
+ */
+export function collectSubagentDescendantSummaries(
+ summaries: Iterable<SessionSummary>,
parent: { activeSessionId?: string | undefined; sessionId?: string | undefined; sessionFile?: string | undefined },
-): boolean {
- const parentKeys = new Set(getParentKeys(child));
- if (parent.activeSessionId !== undefined && parentKeys.has(`active:${parent.activeSessionId}`)) return true;
- if (parent.sessionId !== undefined && parentKeys.has(`session:${parent.sessionId}`)) return true;
- return parent.sessionFile !== undefined && parentKeys.has(fileIdentity(parent.sessionFile));
+): SessionSummary[] {
+ const rowsByParentKey = new Map<string, SessionSummary[]>();
+ for (const summary of summaries) {
+ if (summary.runtimeKind !== "subagent") continue;
+ for (const key of getParentKeys(summary)) {
+ const siblings = rowsByParentKey.get(key) ?? [];
+ siblings.push(summary);
+ rowsByParentKey.set(key, siblings);
+ }
+ }
+ const descendants: SessionSummary[] = [];
+ const linked = new Set<SessionSummary>();
+ const keyQueue = [...parentIdentityKeys(parent)];
+ for (let index = 0; index < keyQueue.length; index++) {
+ for (const row of rowsByParentKey.get(keyQueue[index]!) ?? []) {
+ if (linked.has(row)) continue;
+ linked.add(row);
+ descendants.push(row);
+ keyQueue.push(...parentIdentityKeys(row));
+ }
+ }
+ return descendants;
}
export function getAgentsViewSummaryIdentity(summary: SessionSummary): string {
diff --git a/packages/coding-agent/src/modes/interactive/components/subagent-summary-line.ts b/packages/coding-agent/src/modes/interactive/components/subagent-summary-line.ts
index 1f228fa82f..600402a045 100644
--- a/packages/coding-agent/src/modes/interactive/components/subagent-summary-line.ts
+++ b/packages/coding-agent/src/modes/interactive/components/subagent-summary-line.ts
@@ -1,6 +1,6 @@
import { type Component, type Focusable, getKeybindings, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
import type { AgentConnectionRlmChildAgentSnapshot } from "../../agent-connection/index.js";
-import { isDirectAgentChild } from "../../agents-view/agents-view-state.js";
+import { collectSubagentDescendantSummaries } from "../../agents-view/agents-view-state.js";
import { type AgentRosterStatus, classifyAgentStatus } from "../../daemon/agent-roster.js";
import { classifySessionRosterStatus, type SessionSummary } from "../../daemon/daemon-session-list.js";
import { theme } from "../theme/theme.js";
@@ -24,15 +24,26 @@ export function classifySubagentSnapshotStatus(child: AgentConnectionRlmChildAge
});
}
-export function countDirectSubagentStatuses(
+/** Status counts over every snapshot descending from `parentId`; the recursive roster carries the whole subtree. */
+export function countSubtreeSubagentStatuses(
children: Iterable<AgentConnectionRlmChildAgentSnapshot>,
parentId: string | undefined,
): SubagentSummaryCounts {
const counts: SubagentSummaryCounts = { total: 0, running: 0, idle: 0, inactive: 0 };
- for (const child of children) {
- if (child.parentId !== parentId || child.status === "cancelled") continue;
- counts.total += 1;
- counts[classifySubagentSnapshotStatus(child)] += 1;
+ const knownNodes = new Set<string | undefined>([parentId]);
+ const pending = [...children].filter((child) => child.status !== "cancelled");
+ let matched = true;
+ while (matched) {
+ matched = false;
+ for (let index = pending.length - 1; index >= 0; index--) {
+ const child = pending[index]!;
+ if (!knownNodes.has(child.parentId)) continue;
+ pending.splice(index, 1);
+ matched = true;
+ counts.total += 1;
+ counts[classifySubagentSnapshotStatus(child)] += 1;
+ knownNodes.add(child.id);
+ }
}
return counts;
}
@@ -42,9 +53,9 @@ export function countRosterSubagentStatuses(
parent: { activeSessionId?: string | undefined; sessionId?: string | undefined; sessionFile?: string | undefined },
): SubagentSummaryCounts {
const counts: SubagentSummaryCounts = { total: 0, running: 0, idle: 0, inactive: 0 };
- for (const child of summaries) {
- if (child.runtimeKind !== "subagent" || child.lifecycle !== "live") continue;
- if (!isDirectAgentChild(child, parent)) continue;
+ // The daemon roster spans every tree: count live rows in this session's subtree, at any depth.
+ for (const child of collectSubagentDescendantSummaries(summaries, parent)) {
+ if (child.lifecycle !== "live") continue;
counts.total += 1;
counts[child.rosterStatus ?? classifySessionRosterStatus(child)] += 1;
}
diff --git a/packages/coding-agent/src/modes/interactive/interactive-mode.ts b/packages/coding-agent/src/modes/interactive/interactive-mode.ts
index e1927c6f6d..d37636663a 100644
--- a/packages/coding-agent/src/modes/interactive/interactive-mode.ts
+++ b/packages/coding-agent/src/modes/interactive/interactive-mode.ts
@@ -228,8 +228,8 @@ import {
} from "./components/slash-command-message.js";
import { SlashCommandResultMessageComponent } from "./components/slash-command-result-message.js";
import {
- countDirectSubagentStatuses,
countRosterSubagentStatuses,
+ countSubtreeSubagentStatuses,
SubagentSummaryLine,
} from "./components/subagent-summary-line.js";
import { ThinkingSelectorComponent } from "./components/thinking-selector.js";
@@ -6054,7 +6054,7 @@ export class InteractiveMode {
sessionId: this.connectionState?.sessionId,
sessionFile: this.connectionState?.sessionFile,
})
- : countDirectSubagentStatuses(this.subagentSnapshots.values(), this.rlmNodeId),
+ : countSubtreeSubagentStatuses(this.subagentSnapshots.values(), this.rlmNodeId),
);
if (!this.subagentSummaryLine.isSelectable() && this.subagentSummaryLine.focused) this.focusEditor();
}
diff --git a/packages/coding-agent/test/subagent-summary-line.test.ts b/packages/coding-agent/test/subagent-summary-line.test.ts
index d08c515064..903de7fde4 100644
--- a/packages/coding-agent/test/subagent-summary-line.test.ts
+++ b/packages/coding-agent/test/subagent-summary-line.test.ts
@@ -3,11 +3,10 @@ import stripAnsi from "strip-ansi";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { KeybindingsManager } from "../src/core/keybindings.js";
import type { AgentConnectionRlmChildAgentSnapshot } from "../src/modes/agent-connection/types.js";
-import { isDirectAgentChild } from "../src/modes/agents-view/agents-view-state.js";
import type { SessionSummary } from "../src/modes/daemon/daemon-session-list.js";
import {
- countDirectSubagentStatuses,
countRosterSubagentStatuses,
+ countSubtreeSubagentStatuses,
SubagentSummaryLine,
} from "../src/modes/interactive/components/subagent-summary-line.js";
import { InteractiveMode } from "../src/modes/interactive/interactive-mode.js";
@@ -79,7 +78,7 @@ describe("SubagentSummaryLine", () => {
}
});
- it("counts only direct children using running, idle, and inactive status projections", () => {
+ it("counts the whole snapshot subtree using running, idle, and inactive status projections", () => {
const children = [
child("running", "running"),
child("queued", "queued"),
@@ -91,12 +90,14 @@ describe("SubagentSummaryLine", () => {
child("inactive-error", "error"),
child("cancelled", "cancelled"),
child("grandchild", "running", { parentId: "running" }),
+ child("great-grandchild", "done", { activeSessionId: "gg-session", parentId: "grandchild" }),
+ child("foreign", "running", { parentId: "stranger" }),
];
- expect(countDirectSubagentStatuses(children, undefined)).toEqual({
- total: 8,
- running: 3,
- idle: 3,
+ expect(countSubtreeSubagentStatuses(children, undefined)).toEqual({
+ total: 10,
+ running: 4,
+ idle: 4,
inactive: 2,
});
});
@@ -269,7 +270,7 @@ describe("SubagentSummaryLine", () => {
expect(stripAnsi(line.render(100).join("\n"))).toContain("● 0 running ◐ 0 idle ○ 1 inactive");
});
- it("counts parentSessionId-only roster children exactly like the agents view", () => {
+ it("counts the whole roster subtree, not just direct children", () => {
const rosterChild = {
id: "c1",
sessionId: "c1",
@@ -279,10 +280,60 @@ describe("SubagentSummaryLine", () => {
parentSessionId: "root-session",
rosterStatus: "idle",
} as SessionSummary;
- expect(isDirectAgentChild(rosterChild, { sessionId: "root-session" })).toBe(true);
- expect(countRosterSubagentStatuses([rosterChild], { sessionId: "root-session" })).toEqual({
- total: 1,
- running: 0,
+ const grandchild = {
+ id: "gc1",
+ sessionId: "gc1",
+ activeSessionId: "gc1-active",
+ lifecycle: "live",
+ runtimeKind: "subagent",
+ rlmChildId: "gc1",
+ parentSessionId: "c1",
+ rosterStatus: "running",
+ } as SessionSummary;
+ const greatGrandchild = {
+ id: "gg1",
+ sessionId: "gg1",
+ lifecycle: "live",
+ runtimeKind: "subagent",
+ rlmChildId: "gg1",
+ parentActiveSessionId: "gc1-active",
+ rosterStatus: "running",
+ } as SessionSummary;
+ const archivedChild = {
+ id: "ac1",
+ sessionId: "ac1",
+ lifecycle: "archived",
+ runtimeKind: "subagent",
+ rlmChildId: "ac1",
+ parentSessionId: "root-session",
+ rosterStatus: "inactive",
+ } as SessionSummary;
+ const archivedGrandchild = {
+ id: "ag1",
+ sessionId: "ag1",
+ lifecycle: "live",
+ runtimeKind: "subagent",
+ rlmChildId: "ag1",
+ parentSessionId: "ac1",
+ rosterStatus: "running",
+ } as SessionSummary;
+ const foreign = {
+ id: "f1",
+ sessionId: "f1",
+ lifecycle: "live",
+ runtimeKind: "subagent",
+ rlmChildId: "f1",
+ parentSessionId: "other-root",
+ rosterStatus: "running",
+ } as SessionSummary;
+ expect(
+ countRosterSubagentStatuses(
+ [rosterChild, grandchild, greatGrandchild, archivedChild, archivedGrandchild, foreign],
+ { sessionId: "root-session" },
+ ),
+ ).toEqual({
+ total: 4,
+ running: 3,
idle: 1,
inactive: 0,
});