diff --git a/v3/@claude-flow/cli/__tests__/coordination-load-balance-weighted-zero-5131.test.ts b/v3/@claude-flow/cli/__tests__/coordination-load-balance-weighted-zero-5131.test.ts new file mode 100644 index 0000000000..dc51f5ddb4 --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/coordination-load-balance-weighted-zero-5131.test.ts @@ -0,0 +1,40 @@ +/** + * coordination_load_balance distribute with the weighted algorithm treated a + * configured weight of zero as the default weight of one, so drained nodes + * could still receive tasks. + */ +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { mkdtempSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +const originalProjectCwd = process.env.CLAUDE_FLOW_CWD; + +async function tool(name: string, input: Record) { + const { coordinationTools } = await import('../src/mcp-tools/coordination-tools.js'); + return coordinationTools.find((t) => t.name === name)!.handler(input) as Promise>; +} + +beforeEach(() => { + process.env.CLAUDE_FLOW_CWD = mkdtempSync(join(tmpdir(), 'ruflo-weighted-zero-')); +}); +afterEach(() => { + if (originalProjectCwd === undefined) delete process.env.CLAUDE_FLOW_CWD; + else process.env.CLAUDE_FLOW_CWD = originalProjectCwd; +}); + +describe('coordination_load_balance weighted zero weights', () => { + it.each([ + { weights: { 'node-a': 0, 'node-b': 0.5 }, expected: 'node-b' }, + { weights: { 'node-a': 0 }, expected: 'node-b' }, + ])('assigns the task to $expected with weights $weights', async ({ weights, expected }) => { + for (const nodeId of ['node-a', 'node-b']) { + await tool('coordination_node', { action: 'add', nodeId }); + } + await tool('coordination_load_balance', { action: 'set', algorithm: 'weighted', weights }); + + const result = await tool('coordination_load_balance', { action: 'distribute', task: 'task-0' }); + expect(result.success).toBe(true); + expect(result.assignedTo).toBe(expected); + }); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/coordination-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/coordination-tools.ts index 6b6bb3bc09..b943283a4c 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/coordination-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/coordination-tools.ts @@ -268,7 +268,9 @@ export const coordinationTools: MCPTool[] = [ selectedNode = nodes.reduce((min, n) => n.load < min.load ? n : min); } else if (algorithm === 'weighted') { const weights = store.loadBalance.weights; - selectedNode = nodes.reduce((max, n) => (weights[n.id] || 1) > (weights[max.id] || 1) ? n : max); + // A weight of 0 is a real weight; any other missing or falsy weight counts as 1. + const weightOf = (id: string) => (weights[id] === 0 ? 0 : weights[id] || 1); + selectedNode = nodes.reduce((max, n) => weightOf(n.id) > weightOf(max.id) ? n : max); } else { // Round robin: the active node after the one the last task went to, // wrapping around (and starting over if that node is gone).