Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed the agents view undercounting running subagents: the "N subagents running" indicator now counts busy descendants at any depth, stays visible on collapsed groups, and idle sessions with busy subagents sort above plain idle sessions.
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,9 @@ export class AgentsViewMode implements Component, Focusable {
if (row.kind === "subagent-summary") {
const indent = " ".repeat(row.depth);
const hint = row.hasSpawnCode ? theme.fg("dim", ` · ${keyText("app.agents.program")} show program`) : "";
const label = `${theme.fg("dim", `${row.expanded ? "▾" : "▸"} ${row.title}`)}${hint}`;
// Busy descendants must be legible on a collapsed group, not dimmed away.
const titleColor = row.runningSubagentCount > 0 ? ("success" as const) : ("dim" as const);
const label = `${theme.fg(titleColor, `${row.expanded ? "▾" : "▸"} ${row.title}`)}${hint}`;
const line = padLine(truncateToWidth(`${indent}${label}`, width, ""), width);
return selected ? `${SELECTED_ROW_MARKER}${line}` : line;
}
Expand Down
22 changes: 19 additions & 3 deletions packages/coding-agent/src/modes/agents-view/agents-view-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,13 +698,25 @@ export function buildAgentsViewRows(
}
nestedRows.add(row);
parentByChild.set(row, parent);
if (row.section === "running") {
parent.runningSubagentCount += 1;
}
const siblings = childrenByParent.get(parent) ?? [];
siblings.push(row);
childrenByParent.set(parent, siblings);
}
// The parent's own hasRunningRlmChildren snapshot goes stale between its
// flushes; the live descendant rows are the truth the indicator shows. The
// tally runs before heartbeat propagation so it counts intrinsically busy
// rows only, never promotion-inflated ancestors.
const tallyRunningDescendants = (row: MutableAgentsViewRow): number => {
let count = 0;
for (const child of childrenByParent.get(row) ?? []) {
count += (child.section === "running" ? 1 : 0) + tallyRunningDescendants(child);
}
row.runningSubagentCount = count;
return count;
};
for (const row of baseRows) {
if (!nestedRows.has(row)) tallyRunningDescendants(row);
}
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
propagateHeartbeatStateToAncestors(baseRows, parentByChild);

const roots = baseRows.filter((row) => !nestedRows.has(row));
Expand Down Expand Up @@ -876,6 +888,10 @@ function compareAgentsViewRows(a: AgentsViewRow, b: AgentsViewRow): number {
return sectionDiff;
}
if (a.section !== "running") {
const busyDescendantsDiff = Number(b.runningSubagentCount > 0) - Number(a.runningSubagentCount > 0);
if (busyDescendantsDiff !== 0) {
return busyDescendantsDiff;
}
const activityDiff = getTimestamp(b.summary.lastActivityAt) - getTimestamp(a.summary.lastActivityAt);
if (activityDiff !== 0) {
return activityDiff;
Expand Down
34 changes: 33 additions & 1 deletion packages/coding-agent/test/agents-view-mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from "../src/modes/agents-view/agents-view-state.js";
import type { SessionSummary } from "../src/modes/daemon/daemon-session-list.js";
import type { InteractiveModeUiServices } from "../src/modes/interactive/interactive-mode-services.js";
import { stopThemeWatcher } from "../src/modes/interactive/theme/theme.js";
import { stopThemeWatcher, theme } from "../src/modes/interactive/theme/theme.js";

const modeMocks = vi.hoisted(() => ({
interactiveRun: vi.fn<() => Promise<never>>(),
Expand Down Expand Up @@ -685,6 +685,38 @@ describe("AgentsViewMode", () => {
stopThemeWatcher();
}
});

it("renders a collapsed group's busy-subagent badge legibly instead of dimmed", () => {
const parent = summary({ id: "parent", activeSessionId: "parent", sessionId: "parent-session" });
const busyChild = summary({
id: "busy-child",
activeSessionId: "busy-child",
sessionId: "busy-child-session",
sessionFile: "/tmp/busy-child.jsonl",
runtimeKind: "subagent",
parentActiveSessionId: "parent",
activity: "working",
isSessionActive: true,
isStreaming: true,
});
const idleChild = { ...busyChild, activity: "idle" as const, isSessionActive: false, isStreaming: false };
const view = new AgentsViewMode({ config: {}, uiServices: createUiServices() }, {});

try {
const busyRows = buildAgentsViewRows([parent, busyChild]);
const busySummaryRow = busyRows.find((row) => row.kind === "subagent-summary");
expect(busySummaryRow).toMatchObject({ section: "idle", title: "1 subagent running" });
Reflect.set(view, "rows", busyRows);
expect(invoke("renderRow", view, busySummaryRow, 160)).toContain(theme.fg("success", "▸ 1 subagent running"));

const idleRows = buildAgentsViewRows([parent, idleChild]);
const idleSummaryRow = idleRows.find((row) => row.kind === "subagent-summary");
Reflect.set(view, "rows", idleRows);
expect(invoke("renderRow", view, idleSummaryRow, 160)).toContain(theme.fg("dim", "▸ 1 subagent"));
} finally {
stopThemeWatcher();
}
});
});

function createUiServices(): InteractiveModeUiServices {
Expand Down
157 changes: 155 additions & 2 deletions packages/coding-agent/test/agents-view-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,159 @@ describe("agents view state", () => {
});
});

test("shows a busy settled child on its collapsed idle parent without promoting it", () => {
const summaries = [
makeSummary({
id: "parent-active",
activeSessionId: "parent-active",
sessionId: "parent-session",
sessionName: "Parent",
activity: "idle",
taskState: "completed",
messageCount: 2,
}),
makeSummary({
id: "busy-child",
activeSessionId: "busy-child",
sessionId: "busy-child-session",
sessionName: "Busy child",
runtimeKind: "subagent",
parentActiveSessionId: "parent-active",
activity: "working",
isSessionActive: true,
isStreaming: true,
}),
];

const collapsed = buildAgentsViewRows(summaries);
expect(collapsed[0]).toMatchObject({ kind: "agent", section: "idle", runningSubagentCount: 1 });
expect(collapsed[1]).toMatchObject({ kind: "subagent-summary", section: "idle", title: "1 subagent running" });
});

test("counts a busy grandchild on every idle ancestor without promoting them", () => {
const summaries = [
makeSummary({
id: "parent-active",
activeSessionId: "parent-active",
sessionId: "parent-session",
sessionName: "Parent",
activity: "idle",
taskState: "completed",
messageCount: 2,
}),
makeSummary({
id: "child-active",
activeSessionId: "child-active",
sessionId: "child-session",
sessionName: "Child",
runtimeKind: "subagent",
parentActiveSessionId: "parent-active",
activity: "idle",
taskState: "completed",
}),
makeSummary({
id: "grandchild-active",
activeSessionId: "grandchild-active",
sessionId: "grandchild-session",
sessionName: "Grandchild",
runtimeKind: "subagent",
parentActiveSessionId: "child-active",
activity: "working",
isSessionActive: true,
isStreaming: true,
}),
];

const collapsed = buildAgentsViewRows(summaries);
expect(collapsed[0]).toMatchObject({ kind: "agent", section: "idle", runningSubagentCount: 1 });
expect(collapsed[1]).toMatchObject({ kind: "subagent-summary", section: "idle", title: "1 subagent running" });

const expanded = buildAgentsViewRows(summaries, new Set([collapsed[0]?.identity ?? ""]));
const childRow = expanded.find((row) => row.title === "Child");
expect(childRow).toMatchObject({ kind: "subagent", section: "idle", runningSubagentCount: 1 });
});

test("counts a heartbeat-active grandchild once, not its promotion-inflated ancestors", () => {
const summaries = [
makeSummary({
id: "parent-active",
activeSessionId: "parent-active",
sessionId: "parent-session",
sessionName: "Parent",
activity: "idle",
taskState: "completed",
messageCount: 2,
}),
makeSummary({
id: "child-active",
activeSessionId: "child-active",
sessionId: "child-session",
sessionName: "Child",
runtimeKind: "subagent",
parentActiveSessionId: "parent-active",
activity: "idle",
taskState: "completed",
}),
makeSummary({
id: "grandchild-active",
activeSessionId: "grandchild-active",
sessionId: "grandchild-session",
sessionName: "Heartbeat grandchild",
runtimeKind: "subagent",
parentActiveSessionId: "child-active",
hasActiveHeartbeat: true,
activity: "idle",
}),
];

const collapsed = buildAgentsViewRows(summaries);
expect(collapsed[0]?.runningSubagentCount).toBe(1);
expect(collapsed[1]).toMatchObject({ kind: "subagent-summary", title: "1 subagent running" });
const expanded = buildAgentsViewRows(summaries, new Set([collapsed[0]?.identity ?? ""]));
expect(expanded.find((row) => row.title === "Child")?.runningSubagentCount).toBe(1);
});

test("ranks idle rows with busy descendants above plain idle rows", () => {
const rows = buildAgentsViewRows([
makeSummary({
id: "plain-idle",
activeSessionId: "plain-idle",
sessionId: "plain-session",
sessionName: "Plain idle",
activity: "idle",
taskState: "completed",
messageCount: 2,
lastActivityAt: "2026-09-02T00:00:00Z",
}),
makeSummary({
id: "parent-active",
activeSessionId: "parent-active",
sessionId: "parent-session",
sessionName: "Busy-subtree parent",
activity: "idle",
taskState: "completed",
messageCount: 2,
lastActivityAt: "2026-08-01T00:00:00Z",
}),
makeSummary({
id: "busy-child",
activeSessionId: "busy-child",
sessionId: "busy-child-session",
sessionName: "Busy child",
runtimeKind: "subagent",
parentActiveSessionId: "parent-active",
activity: "working",
isSessionActive: true,
isStreaming: true,
}),
]);

expect(rows.filter((row) => row.kind === "agent").map((row) => [row.title, row.section])).toEqual([
["Busy-subtree parent", "idle"],
["Plain idle", "idle"],
]);
});

test("counts every idle heartbeating subagent as running", () => {
const heartbeatChildren = Array.from({ length: 10 }, (_, index) =>
makeSummary({
Expand Down Expand Up @@ -577,7 +730,7 @@ describe("agents view state", () => {
const oneLevel = buildAgentsViewRows(summaries, new Set([rootIdentity]));
expect(oneLevel.map((row) => [row.title, row.kind])).toEqual([
["Root", "agent"],
["1 subagent running", "subagent-summary"],
["2 subagents running", "subagent-summary"],
["Child", "subagent"],
["1 subagent running", "subagent-summary"],
]);
Expand All @@ -586,7 +739,7 @@ describe("agents view state", () => {
const twoLevel = buildAgentsViewRows(summaries, new Set([rootIdentity, childIdentity]));
expect(twoLevel.map((row) => [row.title, row.kind, row.depth])).toEqual([
["Root", "agent", 0],
["1 subagent running", "subagent-summary", 1],
["2 subagents running", "subagent-summary", 1],
["Child", "subagent", 1],
["1 subagent running", "subagent-summary", 2],
["Grandchild", "subagent", 2],
Expand Down
Loading