From 2e8f419c22f8f37d51adbc5c1786998abfab6fc7 Mon Sep 17 00:00:00 2001 From: ruv Date: Wed, 30 Sep 2026 17:58:06 -0400 Subject: [PATCH] fix(swarm): status no longer invents 5% progress for a swarm with no tasks (#3572) getSwarmStatus() hard-coded progress to 5 whenever a swarm existed but had no tasks, so `swarm status` rendered [##----] 5.0% forever, regardless of how many agents were spawned. With no tasks there is nothing to be a fraction of: progress is now null with progressReason "no tasks" (JSON) and "Overall Progress: n/a (no tasks)" (text), matching the neighbouring "Tokens Used: unknown" / "Success Rate: no data" fields. With tasks, progress stays completed/total. Co-Authored-By: RuFlo --- .../swarm-status-progress-3572.test.ts | 98 +++++++++++++++++++ v3/@claude-flow/cli/src/commands/swarm.ts | 21 ++-- 2 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 v3/@claude-flow/cli/__tests__/swarm-status-progress-3572.test.ts diff --git a/v3/@claude-flow/cli/__tests__/swarm-status-progress-3572.test.ts b/v3/@claude-flow/cli/__tests__/swarm-status-progress-3572.test.ts new file mode 100644 index 0000000000..3245828cef --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/swarm-status-progress-3572.test.ts @@ -0,0 +1,98 @@ +/** + * #3572 — `swarm status` reported 5% progress for a swarm with no tasks: a + * hard-coded number shown as `[##----] 5.0%` that never changed as agents were + * spawned. With no tasks there is nothing to be a fraction of, so progress is + * `null` with a reason (JSON) and `n/a (no tasks)` (text), matching the + * neighbouring `Tokens Used: unknown` / `Success Rate: no data` fields. + * + * Exercised via real execFileSync against bin/cli.js in temp cwds, matching + * the harness in swarm-status-id-and-stop.test.ts. + */ + +import { describe, it, expect, afterEach } from 'vitest'; +import { execFileSync } from 'node:child_process'; +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +const CLI = join(__dirname, '..', 'bin', 'cli.js'); +const dirs: string[] = []; + +function run(args: string[], cwd: string): string { + try { + return execFileSync('node', [CLI, ...args], { + cwd, encoding: 'utf-8', stdio: ['ignore', 'pipe', 'pipe'], + }); + } catch (err) { + const e = err as { stdout?: Buffer; stderr?: Buffer }; + return (e.stdout?.toString() ?? '') + (e.stderr?.toString() ?? ''); + } +} + +function statusJson(cwd: string): Record { + const out = run(['swarm', 'status', '--format', 'json'], cwd); + return JSON.parse(out.slice(out.indexOf('{'))); +} + +/** A swarm as `swarm init` leaves it, with `agents` spawned and the given task statuses. */ +function fixture(agents: number, taskStatuses: string[]): string { + const cwd = mkdtempSync(join(tmpdir(), 'swarm-progress-3572-')); + dirs.push(cwd); + mkdirSync(join(cwd, '.swarm'), { recursive: true }); + writeFileSync(join(cwd, '.swarm', 'state.json'), JSON.stringify({ + id: 'swarm-3572', status: 'initialized', topology: 'hierarchical', + })); + if (agents > 0) { + mkdirSync(join(cwd, '.claude-flow', 'metrics'), { recursive: true }); + writeFileSync(join(cwd, '.claude-flow', 'metrics', 'swarm-activity.json'), JSON.stringify({ + swarm: { agent_count: agents, coordination_active: false }, + })); + } + if (taskStatuses.length > 0) { + mkdirSync(join(cwd, '.swarm', 'tasks'), { recursive: true }); + taskStatuses.forEach((status, i) => { + writeFileSync(join(cwd, '.swarm', 'tasks', `task-${i}.json`), JSON.stringify({ id: `task-${i}`, status })); + }); + } + return cwd; +} + +afterEach(() => { + while (dirs.length) rmSync(dirs.pop()!, { recursive: true, force: true }); +}); + +describe('#3572 swarm status progress is never invented', () => { + for (const agents of [0, 1, 2, 3]) { + it(`reports no percentage for a swarm with no tasks and ${agents} agent(s)`, () => { + const cwd = fixture(agents, []); + + const json = statusJson(cwd); + expect(json.progress).toBeNull(); + expect(json.progressReason).toBe('no tasks'); + + const text = run(['swarm', 'status'], cwd); + const line = text.split('\n').find(l => l.includes('Overall Progress')) ?? ''; + expect(line).toContain('n/a (no tasks)'); + expect(line).not.toMatch(/\d+(\.\d+)?%/); + expect(line).not.toContain('#'); + }); + } + + it('reports completed/total when tasks exist: 1 of 4 completed is 25%', () => { + const cwd = fixture(2, ['completed', 'in_progress', 'pending', 'pending']); + + const json = statusJson(cwd); + expect(json.progress).toBe(25); + expect(json.progressReason).toBeNull(); + + const text = run(['swarm', 'status'], cwd); + const line = text.split('\n').find(l => l.includes('Overall Progress')) ?? ''; + expect(line).toContain('25'); + expect(line).not.toContain('n/a'); + }); + + it('reports 100% when every task is completed', () => { + const cwd = fixture(1, ['completed', 'done']); + expect(statusJson(cwd).progress).toBe(100); + }); +}); diff --git a/v3/@claude-flow/cli/src/commands/swarm.ts b/v3/@claude-flow/cli/src/commands/swarm.ts index 82a13794cf..7852bdb3fb 100644 --- a/v3/@claude-flow/cli/src/commands/swarm.ts +++ b/v3/@claude-flow/cli/src/commands/swarm.ts @@ -198,16 +198,14 @@ function getSwarmStatus(swarmId?: string) { } } - // Calculate dynamic progress based on actual state - // If no swarm state, show 0%. Otherwise calculate from completed tasks + // Progress is completed / total tasks. With no tasks there is nothing to be + // a fraction of, so it is `null` with a reason — never an invented number + // (#3572: a hard-coded 5% was shown for a swarm with no tasks, forever). const totalTasks = completedTasks + inProgressTasks + pendingTasks; - let progress = 0; - if (totalTasks > 0) { - progress = Math.round((completedTasks / totalTasks) * 100); - } else if (swarmState) { - // Swarm initialized but no tasks yet - progress = 5; - } + const progress: number | null = totalTasks > 0 + ? Math.round((completedTasks / totalTasks) * 100) + : null; + const progressReason: string | null = totalTasks > 0 ? null : 'no tasks'; // Determine status let status = 'idle'; @@ -245,6 +243,7 @@ function getSwarmStatus(swarmId?: string) { completed: 0 }, progress, + progressReason, tasks: { total: totalTasks, completed: completedTasks, @@ -768,7 +767,9 @@ const statusCommand: Command = { output.writeln(); // Progress bar - output.writeln(`Overall Progress: ${output.progressBar(status.progress, 100, 40)}`); + output.writeln(status.progress === null + ? `Overall Progress: ${output.dim(`n/a (${status.progressReason})`)}` + : `Overall Progress: ${output.progressBar(status.progress, 100, 40)}`); output.writeln(); // Agent status