Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(claims): stop claims_rebalance from overfilling the first underloaded agent (#4547)

kevinaf091d5ffc5a
diff --git a/v3/@claude-flow/cli/__tests__/claims-rebalance-target-capacity.test.ts b/v3/@claude-flow/cli/__tests__/claims-rebalance-target-capacity.test.ts
new file mode 100644
index 0000000000..6fbc5f6db3
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/claims-rebalance-target-capacity.test.ts
@@ -0,0 +1,59 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { mkdtempSync, mkdirSync, rmSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { claimsTools } from '../src/mcp-tools/claims-tools.js';
+
+// claims_rebalance checked `target.claims.length < maxClaims` against the load
+// snapshot taken before any move, so every movable claim went to the first
+// underloaded agent and pushed it past maxClaims instead of spreading the work.
+const tool = (name: string) => claimsTools.find(t => t.name === name)!;
+let dir: string;
+
+beforeEach(() => {
+ dir = mkdtempSync(join(tmpdir(), 'ruflo-claims-rebalance-'));
+ mkdirSync(join(dir, '.claude-flow', 'claims'), { recursive: true });
+ vi.spyOn(process, 'cwd').mockReturnValue(dir);
+});
+
+afterEach(() => {
+ vi.restoreAllMocks();
+ rmSync(dir, { recursive: true, force: true });
+});
+
+type Load = { agentId: string; claimCount: number; maxClaims: number };
+
+describe('claims_rebalance', () => {
+ beforeEach(async () => {
+ // One overloaded coder with 10 claims, two idle coders with 1 claim each.
+ for (let i = 0; i < 10; i++) {
+ await tool('claims_claim').handler({ issueId: `busy-${i}`, claimant: 'agent:busy:coder' });
+ }
+ await tool('claims_claim').handler({ issueId: 'idle-a-0', claimant: 'agent:idle-a:coder' });
+ await tool('claims_claim').handler({ issueId: 'idle-b-0', claimant: 'agent:idle-b:coder' });
+ });
+
+ it('does not suggest moves that push a target past maxClaims', async () => {
+ const result = await tool('claims_rebalance').handler({ dryRun: true }) as {
+ suggestions: Array<{ issueId: string; to: string }>;
+ };
+ expect(result.suggestions.length).toBeGreaterThan(0);
+
+ const incoming = new Map<string, number>();
+ for (const s of result.suggestions) incoming.set(s.to, (incoming.get(s.to) ?? 0) + 1);
+ // Each idle agent starts with 1 claim, so it can take at most 4 more.
+ for (const [to, n] of incoming) {
+ expect(1 + n, `${to} receives ${n} claims`).toBeLessThanOrEqual(5);
+ }
+ expect(incoming.size).toBe(2);
+ });
+
+ it('leaves no agent over maxClaims after applying the moves', async () => {
+ await tool('claims_rebalance').handler({ dryRun: false });
+ const { loads } = await tool('claims_load').handler({}) as { loads: Load[] };
+ for (const l of loads) {
+ expect(l.claimCount, l.agentId).toBeLessThanOrEqual(l.maxClaims);
+ }
+ expect(loads.reduce((sum, l) => sum + l.claimCount, 0)).toBe(12);
+ });
+});
diff --git a/v3/@claude-flow/cli/src/mcp-tools/claims-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/claims-tools.ts
index e766d56b68..118758cf6b 100644
--- a/v3/@claude-flow/cli/src/mcp-tools/claims-tools.ts
+++ b/v3/@claude-flow/cli/src/mcp-tools/claims-tools.ts
@@ -867,6 +867,9 @@ export const claimsTools: MCPTool[] = [
const underloaded = loads.filter(l => l.claims.length < maxClaims * targetUtilization * 0.5);
const suggestions: Array<{ issueId: string; from: string; to: string; reason: string }> = [];
+ // Claim counts including moves suggested so far, so one target is not
+ // handed more claims than maxClaims allows.
+ const plannedCount = new Map(loads.map(l => [l.agentId, l.claims.length]));
for (const over of overloaded) {
// Find low-progress claims to redistribute
@@ -875,8 +878,9 @@ export const claimsTools: MCPTool[] = [
.slice(0, over.claims.length - Math.ceil(maxClaims * targetUtilization));
for (const claim of movable) {
- const target = underloaded.find(u => u.agentType === over.agentType && u.claims.length < maxClaims);
+ const target = underloaded.find(u => u.agentType === over.agentType && plannedCount.get(u.agentId)! < maxClaims);
if (target) {
+ plannedCount.set(target.agentId, plannedCount.get(target.agentId)! + 1);
suggestions.push({
issueId: claim.issueId,
from: `agent:${over.agentId}:${over.agentType}`,