diff --git a/v3/@claude-flow/cli/__tests__/hive-capacity-4727.test.ts b/v3/@claude-flow/cli/__tests__/hive-capacity-4727.test.ts index 5b8ee9ff98..f56ee9f60d 100644 --- a/v3/@claude-flow/cli/__tests__/hive-capacity-4727.test.ts +++ b/v3/@claude-flow/cli/__tests__/hive-capacity-4727.test.ts @@ -116,6 +116,15 @@ describe('#4727 hive capacity and registered membership', () => { expect(read('agents.json').agents[agentId].config).toEqual({ role: 'worker', hiveRole: 'worker' }); }); + it('rejects a role outside the schema enum on spawn and join without writing', async () => { + await run('hive-mind_init', { maxAgents: 4 }); + await register('agent-r'); + const before = snapshot(); + expect(await run('hive-mind_spawn', { count: 1, role: 'queen' })).toMatchObject({ success: false, error: expect.stringMatching(/role must be one of/) }); + expect(await run('hive-mind_join', { agentId: 'agent-r', role: 'queen' })).toMatchObject({ success: false, error: expect.stringMatching(/role must be one of/) }); + expect(snapshot()).toEqual(before); + }); + it('rejects unknown agents, including with a valid remote token, without writing', async () => { await run('hive-mind_init', { maxAgents: 2 }); const before = snapshot(); diff --git a/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts index 587987e06e..1d18b5cc22 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/hive-mind-tools.ts @@ -388,6 +388,8 @@ function findAgent(store: HiveAgentStore, agentId: string): HiveAgentRecord | un return Object.prototype.hasOwnProperty.call(store.agents, agentId) ? store.agents[agentId] : undefined; } +const HIVE_ROLES = new Set(['worker', 'specialist', 'scout']); + function capacityError(state: HiveState, additionalWorkers: number): Record | null { const maxAgents = state.maxAgents ?? 15; if (state.workers.length + additionalWorkers <= maxAgents) return null; @@ -445,6 +447,9 @@ export const hiveMindTools: MCPTool[] = [ const full = capacityError(state, count); if (full) return full; const role = (input.role as string) || 'worker'; + if (!HIVE_ROLES.has(role)) { + return { success: false, error: `role must be one of: ${[...HIVE_ROLES].join(', ')}` }; + } const agentType = (input.agentType as string) || 'worker'; const prefix = (input.prefix as string) || 'hive-worker'; const agentStore = loadAgentStore(); @@ -775,6 +780,9 @@ export const hiveMindTools: MCPTool[] = [ return { success: false, agentId, error: 'Agent not found. Register it with agent_spawn or hive-mind_spawn first.' }; } const role = (input.role as string) || 'worker'; + if (!HIVE_ROLES.has(role)) { + return { success: false, agentId, error: `role must be one of: ${[...HIVE_ROLES].join(', ')}` }; + } for (const { path, store, agent } of records) { agent.config = { ...(agent.config as Record), role, hiveRole: role }; saveAgentStore(store, path);