fix(coordination): read a weight of 0 as 0 in weighted load balancing (#5131) (#5132)

Co-authored-by: Lenin Mookiah <2639584+leninworld@users.noreply.github.com>
This commit is contained in:
Lenin MookiahandLenin Mookiah authored and GitHub committed 2026-11-27 08:17:34 -05:00
1 parent e3414cb2a0
commit 267b336739
2 files changed
+43 -1

No files matched your search

@@ -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<string, unknown>) {
const { coordinationTools } = await import('../src/mcp-tools/coordination-tools.js');
return coordinationTools.find((t) => t.name === name)!.handler(input) as Promise<Record<string, any>>;
}
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);
});
});
@@ -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).