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
172 changes: 171 additions & 1 deletion src/tui/components/Nav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
*/

import { describe, expect, mock, test } from 'bun:test';
import { buildWorkspaceTree } from '../session-tree.js';
import type { TreeNode } from '../types.js';
import { handleEnterAgent } from './Nav.js';
import { computeNavCounts, handleEnterAgent } from './Nav.js';

function makeAgentNode(overrides: Partial<TreeNode> = {}): TreeNode {
return {
Expand Down Expand Up @@ -91,3 +92,172 @@ describe('handleEnterAgent — Enter on stopped agent (G2)', () => {
expect(onTmuxSelect).not.toHaveBeenCalled();
});
});

// ─── computeNavCounts — sidebar header counter (regression coverage) ─────────
//
// The "Agents 0/0" report on .12 surfaced a coverage gap: nothing in the test
// suite exercised the counter. The shallow Array.filter over top-level nodes
// also undercounted sub-agents, so a workspace with `genie` + `genie/qa`
// surfaced as "Agents 0/1" no matter how many subs the user had.

const realisticAgentNames = [
'aegis',
'brain',
'felipe',
'felipe/notes',
'felipe/scout',
'genie',
'genie/qa',
'genie/devrel',
'genie/engineer',
'genie-docs',
'genie-docs/reviewer',
];

describe('computeNavCounts', () => {
test('workspace mode counts canonical AND sub-agent nodes', () => {
const tree = buildWorkspaceTree({
agentNames: realisticAgentNames,
sessions: [],
executors: [],
});
const { agentCount, runningCount } = computeNavCounts('/ws', tree, null);
expect(agentCount).toBe(realisticAgentNames.length);
expect(runningCount).toBe(0);
});

test('running sub-agent contributes to runningCount', () => {
const tree = buildWorkspaceTree({
agentNames: ['genie', 'genie/qa'],
sessions: [],
executors: [],
});
// Force the sub-agent to running for the test — exercises recursive walk.
tree[0].children[0].wsAgentState = 'running';
const { agentCount, runningCount } = computeNavCounts('/ws', tree, null);
expect(agentCount).toBe(2);
expect(runningCount).toBe(1);
});

test('workspace mode with empty tree falls back to live tmux count', () => {
// Reproduces the .12 "0/0" report: workspace mode active but scanAgents
// returned [] (stale workspaceRoot, transient FS error). Without the
// fallback the user saw "Agents 0/0" while tmux had live panes.
const diagnostics = {
sessions: [
{
name: 'felipe',
attached: false,
windowCount: 1,
created: 0,
windows: [
{
sessionName: 'felipe',
index: 0,
name: 'work',
active: true,
paneCount: 2,
panes: [
{
sessionName: 'felipe',
windowIndex: 0,
paneIndex: 0,
paneId: '%0',
pid: 1,
command: 'claude',
title: 'claude',
size: '80x24',
isDead: false,
},
{
sessionName: 'felipe',
windowIndex: 0,
paneIndex: 1,
paneId: '%1',
pid: 2,
command: 'bash',
title: 'bash',
size: '80x24',
isDead: false,
},
],
},
],
},
],
executors: [],
assignments: [],
gaps: {
deadPidExecutors: [],
orphanPanes: [],
linkedCount: 0,
totalExecutors: 0,
totalClaudePanes: 0,
deadPaneCount: 0,
},
workStates: new Map(),
alertCount: 0,
timestamp: 0,
} as Parameters<typeof computeNavCounts>[2];

const { agentCount, runningCount } = computeNavCounts('/stale-ws', [], diagnostics);
expect(agentCount).toBe(1);
expect(runningCount).toBe(2);
});

test('legacy mode (no workspaceRoot) reports session/pane totals', () => {
const diagnostics = {
sessions: [
{
name: 'a',
attached: false,
windowCount: 1,
created: 0,
windows: [
{
sessionName: 'a',
index: 0,
name: 'w',
active: true,
paneCount: 1,
panes: [
{
sessionName: 'a',
windowIndex: 0,
paneIndex: 0,
paneId: '%0',
pid: 1,
command: 'bash',
title: 'bash',
size: '80x24',
isDead: false,
},
],
},
],
},
],
executors: [],
assignments: [],
gaps: {
deadPidExecutors: [],
orphanPanes: [],
linkedCount: 0,
totalExecutors: 0,
totalClaudePanes: 0,
deadPaneCount: 0,
},
workStates: new Map(),
alertCount: 0,
timestamp: 0,
} as Parameters<typeof computeNavCounts>[2];

const { agentCount, runningCount } = computeNavCounts(undefined, [], diagnostics);
expect(agentCount).toBe(1);
expect(runningCount).toBe(1);
});

test('null diagnostics + empty tree returns 0/0 (initial render)', () => {
expect(computeNavCounts(undefined, [], null)).toEqual({ agentCount: 0, runningCount: 0 });
});
});
47 changes: 42 additions & 5 deletions src/tui/components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,53 @@ function renderAlertBadge(alertCount: number) {
);
}

function computeNavCounts(
/**
* Walk every agent node in the tree (top-level AND sub-agents) and count
* those matching `predicate`. The shallow `Array.filter` we used previously
* silently undercounted sub-agents — `genie/qa` lived as a child of `genie`
* and never reached the filter, so a workspace with 10 canonicals + 15 subs
* read as "Agents 0/10" instead of "Agents 0/25".
*/
function countAgents(nodes: TreeNode[], predicate: (n: TreeNode) => boolean): number {
let count = 0;
for (const n of nodes) {
if (n.type === 'agent' && predicate(n)) count++;
if (n.children.length > 0) count += countAgents(n.children, predicate);
}
Comment on lines +215 to +218

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This recursive function is a good solution for counting nested agents. However, it currently recurses into the children of all nodes, including non-agent nodes like orphan sessions. Since only nodes of type agent can contain sub-agents, you can make this more efficient and precise by only recursing into the children of agent nodes.

  for (const n of nodes) {
    if (n.type === 'agent') {
      if (predicate(n)) count++;
      if (n.children.length > 0) count += countAgents(n.children, predicate);
    }
  }

return count;
}

/**
* Compute the running/total counter shown in the sidebar header.
*
* Workspace mode counts every agent in the tree (canonical + sub-agent) so the
* displayed total matches what the user can see by expanding the tree. The
* `0/0` regression that motivated this rewrite hit when `scanAgents` returned
* `[]` (stale `workspaceRoot`, transient FS error) while no tmux sessions
* existed yet — `buildWorkspaceTree` produced an empty tree and a non-recursive
* filter masked any orphan sessions. The fallback below exposes those orphan
* sessions so the user sees something instead of `0/0` when the workspace
* scan comes up empty but tmux has live panes.
*
* Exported for unit-test coverage — the missing test on this helper is
* exactly why the regression slipped through PR #1447's review.
*/
export function computeNavCounts(
workspaceRoot: string | undefined,
sessionTree: TreeNode[],
diagnostics: DiagnosticSnapshot | null,
): { agentCount: number; runningCount: number } {
if (workspaceRoot) {
return {
agentCount: sessionTree.filter((n) => n.type === 'agent').length,
runningCount: sessionTree.filter((n) => n.wsAgentState === 'running').length,
};
const agentCount = countAgents(sessionTree, () => true);
if (agentCount > 0) {
return {
agentCount,
runningCount: countAgents(sessionTree, (n) => n.wsAgentState === 'running'),
};
}
Comment on lines +244 to +249

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude internal tmux session in workspace fallback counts

When workspaceRoot is set and agentCount is zero, this branch falls through to the legacy diagnostics counters, which include every tmux session. In workspace mode the tree intentionally filters out the internal genie-tui session (buildWorkspaceTree excludes it), so the header can report counts that don’t exist in the visible tree (for example, a stale workspace with only the TUI session can show 1/1). The fallback should be based on the filtered workspace tree/orphans or explicitly exclude the internal session before counting.

Useful? React with 👍 / 👎.

// Workspace mode but no agent nodes resolved (stale workspaceRoot, empty
// scan, or every node is an orphan session). Fall back to the live tmux
// inventory so the header still reflects the user's reality.
}
Comment on lines 242 to 253

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While this fallback logic correctly prevents the '0/0' display, it introduces a potentially confusing UI for users in workspace mode. When a fallback occurs, agentCount becomes the number of sessions and runningCount becomes the total number of panes. The Nav component will then display this as 'Agents <pane_count>/<session_count>', which can be misleading (e.g., 'Agents 2/1').

To improve clarity, consider signaling to the caller that a fallback occurred so a different display format can be used. A clean way to do this is to add a boolean flag to the return object (e.g., isWorkspace: boolean) and adjust the Nav component to use the appropriate label and format.

const paneSum =
diagnostics?.sessions.reduce((sum, s) => sum + s.windows.reduce((ws, w) => ws + w.panes.length, 0), 0) ?? 0;
Expand Down
Loading