mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-04 06:21:59 +09:00
fix(agent): normalize agent_list status filter
Treat "all" and empty as no status filter, and match aliases and any letter case the way task_list does. Co-authored-by: yuzhou <1991039819@qq.com>
This commit is contained in:
2 files changed
+170
-7
No files matched your search
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* agent_list compared its status filter literally, so `all`, aliases such as
|
||||
* `active`, and any other letter case (`Idle`) matched nothing and returned
|
||||
* an empty list. Same reader as task_list: trim, case-fold, known aliases,
|
||||
* and `all` / empty means no status filter.
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { mkdtempSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { agentListStatusFilter, agentStatusOf, agentTools } from '../src/mcp-tools/agent-tools.js';
|
||||
import { agentCommand } from '../src/commands/agent.js';
|
||||
import type { CommandContext } from '../src/types.js';
|
||||
|
||||
const tool = (name: string) => {
|
||||
const t = agentTools.find(t => t.name === name);
|
||||
if (!t) throw new Error(`MCP tool not registered: ${name}`);
|
||||
return t;
|
||||
};
|
||||
|
||||
type ListResult = { agents: Array<{ agentId: string; status: string }>; total: number; error?: string };
|
||||
|
||||
describe('agent_list status filter aliases', () => {
|
||||
let dir: string;
|
||||
let prevCwd: string | undefined;
|
||||
let idle: string;
|
||||
let busy: string;
|
||||
let stopped: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
dir = mkdtempSync(join(tmpdir(), 'ruflo-agent-list-status-'));
|
||||
prevCwd = process.env.CLAUDE_FLOW_CWD;
|
||||
process.env.CLAUDE_FLOW_CWD = dir;
|
||||
|
||||
const spawn = tool('agent_spawn').handler;
|
||||
const update = tool('agent_update').handler;
|
||||
idle = (await spawn({ agentType: 'coder' }) as { agentId: string }).agentId;
|
||||
busy = (await spawn({ agentType: 'tester' }) as { agentId: string }).agentId;
|
||||
stopped = (await spawn({ agentType: 'reviewer' }) as { agentId: string }).agentId;
|
||||
await update({ agentId: busy, status: 'busy' });
|
||||
await tool('agent_terminate').handler({ agentId: stopped });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (prevCwd === undefined) delete process.env.CLAUDE_FLOW_CWD;
|
||||
else process.env.CLAUDE_FLOW_CWD = prevCwd;
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const list = async (status?: string, extra: Record<string, unknown> = {}) =>
|
||||
(await tool('agent_list').handler({ ...extra, ...(status === undefined ? {} : { status }) })) as ListResult;
|
||||
|
||||
const ids = (result: ListResult) => result.agents.map(a => a.agentId).sort();
|
||||
|
||||
it.each([
|
||||
['idle', 'idle'],
|
||||
['Idle', 'idle'],
|
||||
['IDLE', 'idle'],
|
||||
['ready', 'idle'],
|
||||
['busy', 'busy'],
|
||||
['active', 'busy'],
|
||||
['running', 'busy'],
|
||||
['Working', 'busy'],
|
||||
['terminated', 'terminated'],
|
||||
['Stopped', 'terminated'],
|
||||
['inactive', 'terminated'],
|
||||
])('status "%s" lists the %s agents', async (written, canonical) => {
|
||||
const result = await list(written);
|
||||
expect(result.error).toBeUndefined();
|
||||
expect(result.agents.map(a => a.status)).toEqual([canonical]);
|
||||
expect(result.total).toBe(1);
|
||||
});
|
||||
|
||||
it('resolves each entry of a comma-separated filter', async () => {
|
||||
const result = await list('active, Idle');
|
||||
expect(ids(result)).toEqual([busy, idle].sort());
|
||||
expect(result.total).toBe(2);
|
||||
});
|
||||
|
||||
it('treats all, empty, and whitespace as no status filter', async () => {
|
||||
const visible = [busy, idle].sort();
|
||||
expect(ids(await list('all'))).toEqual(visible);
|
||||
expect(ids(await list('ALL'))).toEqual(visible);
|
||||
expect(ids(await list(''))).toEqual(visible);
|
||||
expect(ids(await list(' '))).toEqual(visible);
|
||||
expect(ids(await list())).toEqual(visible);
|
||||
expect((await list('all')).total).toBe(2);
|
||||
});
|
||||
|
||||
it('all with includeTerminated returns every agent, including terminated', async () => {
|
||||
const result = await list('all', { includeTerminated: true });
|
||||
expect(ids(result)).toEqual([busy, idle, stopped].sort());
|
||||
});
|
||||
|
||||
it('keeps an unknown status and prototype keys from matching anything', async () => {
|
||||
expect((await list('bogus')).total).toBe(0);
|
||||
expect((await list('constructor')).total).toBe(0);
|
||||
expect(['constructor', '__proto__', 'toString', ' Idle ', 42, null].map(agentStatusOf))
|
||||
.toEqual([null, null, null, 'idle', null, null]);
|
||||
expect(agentListStatusFilter('all')).toBeNull();
|
||||
expect(agentListStatusFilter('')).toBeNull();
|
||||
expect(agentListStatusFilter('active, idle')).toEqual(['busy', 'idle']);
|
||||
});
|
||||
|
||||
it('agent list --status all lists the non-terminated agents', async () => {
|
||||
const listCommand = agentCommand.subcommands!.find(c => c.name === 'list')!;
|
||||
const ctx = {
|
||||
args: [],
|
||||
flags: { format: 'json', status: 'all' },
|
||||
cwd: process.cwd(),
|
||||
interactive: false,
|
||||
} as unknown as CommandContext;
|
||||
const result = await listCommand.action!(ctx);
|
||||
expect(result?.success).toBe(true);
|
||||
const agents = (result!.data as { agents: Array<{ agentId: string }> }).agents;
|
||||
expect(agents.map(a => a.agentId).sort()).toEqual([busy, idle].sort());
|
||||
});
|
||||
});
|
||||
@@ -116,6 +116,40 @@ function loadAllAgents(): Record<string, AgentRecord> {
|
||||
return { ...loadHiveAgents(), ...loadAgentStore().agents };
|
||||
}
|
||||
|
||||
export const AGENT_STATUSES = ['idle', 'busy', 'terminated'] as const;
|
||||
type AgentLifecycleStatus = (typeof AGENT_STATUSES)[number];
|
||||
|
||||
// Spellings callers write for the three stored states. v2 `agent/list` used
|
||||
// "active" for a working agent; hive liveness folds running/working into busy.
|
||||
const AGENT_STATUS_ALIASES: Readonly<Record<string, AgentLifecycleStatus>> = {
|
||||
active: 'busy', running: 'busy', working: 'busy',
|
||||
ready: 'idle', waiting: 'idle',
|
||||
stopped: 'terminated', inactive: 'terminated', offline: 'terminated',
|
||||
};
|
||||
|
||||
/** Canonical lifecycle status (trimmed, any case, known aliases), or null. */
|
||||
export function agentStatusOf(value: unknown): AgentLifecycleStatus | null {
|
||||
if (typeof value !== 'string') return null;
|
||||
const key = value.trim().toLowerCase();
|
||||
if ((AGENT_STATUSES as readonly string[]).includes(key)) return key as AgentLifecycleStatus;
|
||||
return Object.hasOwn(AGENT_STATUS_ALIASES, key) ? AGENT_STATUS_ALIASES[key] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Statuses `agent_list` should keep. `null` means no status filter:
|
||||
* omitted, empty, whitespace, or `all` (any case, including inside a comma list).
|
||||
* The caller still hides terminated agents unless includeTerminated is set.
|
||||
*/
|
||||
export function agentListStatusFilter(status: unknown): string[] | null {
|
||||
if (typeof status !== 'string' || status.trim() === '') return null;
|
||||
const canonical = status.split(',').map(part => part.trim()).filter(Boolean).map(part => {
|
||||
if (part.toLowerCase() === 'all') return 'all';
|
||||
return agentStatusOf(part) ?? part.toLowerCase();
|
||||
});
|
||||
if (canonical.length === 0 || canonical.includes('all')) return null;
|
||||
return canonical;
|
||||
}
|
||||
|
||||
// Default model mappings for agent types (can be overridden)
|
||||
const AGENT_TYPE_MODEL_DEFAULTS: Record<string, ClaudeModel> = {
|
||||
// Complex agents → opus
|
||||
@@ -664,16 +698,22 @@ export const agentTools: MCPTool[] = [
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
status: { type: 'string', description: 'Filter by status' },
|
||||
status: { type: 'string', description: 'Filter by status (idle, busy, terminated; case-insensitive, aliases such as active/running/stopped). "all" or empty applies no status filter. Comma-separated values match any of them.' },
|
||||
domain: { type: 'string', description: 'Filter by domain' },
|
||||
agentType: { type: 'string', description: 'Filter by agent type' },
|
||||
includeTerminated: { type: 'boolean', description: 'Include terminated agents' },
|
||||
},
|
||||
},
|
||||
handler: async (input) => {
|
||||
if (input.status) {
|
||||
const v = validateIdentifier(input.status, 'status');
|
||||
if (!v.valid) return { agents: [], total: 0, error: `Input validation failed: ${v.error}` };
|
||||
// Validate each status token. Commas separate values (`active,idle`);
|
||||
// the whole string is not one identifier.
|
||||
if (typeof input.status === 'string' && input.status.trim() !== '') {
|
||||
for (const part of input.status.split(',')) {
|
||||
const token = part.trim();
|
||||
if (!token || token.toLowerCase() === 'all') continue;
|
||||
const v = validateIdentifier(token, 'status');
|
||||
if (!v.valid) return { agents: [], total: 0, error: `Input validation failed: ${v.error}` };
|
||||
}
|
||||
}
|
||||
if (input.domain) {
|
||||
const v = validateIdentifier(input.domain, 'domain');
|
||||
@@ -686,9 +726,14 @@ export const agentTools: MCPTool[] = [
|
||||
|
||||
let agents = Object.values(loadAllAgents()); // #1916: includes hive-mind-spawned workers
|
||||
|
||||
// Filter by status
|
||||
if (input.status) {
|
||||
agents = agents.filter(a => a.status === input.status);
|
||||
// Filter by status. `all` and empty mean no status filter (#4732),
|
||||
// matching task_list: comma-separated, case-insensitive, known aliases.
|
||||
const statuses = agentListStatusFilter(input.status);
|
||||
if (statuses) {
|
||||
agents = agents.filter(a => {
|
||||
const stored = agentStatusOf(a.status) ?? String(a.status).trim().toLowerCase();
|
||||
return statuses.includes(stored);
|
||||
});
|
||||
} else if (!input.includeTerminated) {
|
||||
agents = agents.filter(a => a.status !== 'terminated');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user