From 65081ae7eb2440e82b60b682f1407b29cae34619 Mon Sep 17 00:00:00 2001 From: kevin9327 <5299031+kevin9327@users.noreply.github.com> Date: Thu, 5 Nov 2026 02:18:00 +0900 Subject: [PATCH] fix(claims): leave completed claims out of claims_load and claims_rebalance --- .../claims-load-skips-completed.test.ts | 69 +++++++++++++++++++ .../cli/src/mcp-tools/claims-tools.ts | 22 +++--- 2 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 v3/@claude-flow/cli/__tests__/claims-load-skips-completed.test.ts diff --git a/v3/@claude-flow/cli/__tests__/claims-load-skips-completed.test.ts b/v3/@claude-flow/cli/__tests__/claims-load-skips-completed.test.ts new file mode 100644 index 0000000000..0ab264065e --- /dev/null +++ b/v3/@claude-flow/cli/__tests__/claims-load-skips-completed.test.ts @@ -0,0 +1,69 @@ +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_status 'completed' keeps the claim in the store, and claims_load and +// claims_rebalance counted it as load. An agent that had finished its issues +// was reported as busy (claimCount and utilization included the finished +// work), and claims_rebalance treated it as overloaded and moved its one open +// claim to another agent. Completed claims are no longer counted as load. +const tool = (name: string) => claimsTools.find(t => t.name === name)!; +let dir: string; + +type Result = Record; +const call = (name: string, input: Record) => tool(name).handler(input) as Promise; + +beforeEach(() => { + dir = mkdtempSync(join(tmpdir(), 'ruflo-claims-load-completed-')); + mkdirSync(join(dir, '.claude-flow', 'claims'), { recursive: true }); + vi.spyOn(process, 'cwd').mockReturnValue(dir); +}); + +afterEach(() => { + vi.restoreAllMocks(); + rmSync(dir, { recursive: true, force: true }); +}); + +describe('completed claims and agent load', () => { + it('claims_load counts only claims that are not completed', async () => { + for (let i = 0; i < 3; i++) { + await call('claims_claim', { issueId: `issue-${i}`, claimant: 'agent:a1:coder' }); + } + await call('claims_status', { issueId: 'issue-0', status: 'completed' }); + await call('claims_status', { issueId: 'issue-1', status: 'completed' }); + + const result = await call('claims_load', {}); + expect(result.totalClaims).toBe(1); + const load = result.loads.find((l: Result) => l.agentId === 'a1'); + expect(load.claimCount).toBe(1); + expect(load.utilization).toBeCloseTo(0.2); + expect(load.claims.map((c: Result) => c.issueId)).toEqual(['issue-2']); + }); + + it('claims_load leaves out an agent whose claims are all completed', async () => { + await call('claims_claim', { issueId: 'done-1', claimant: 'agent:a1:coder' }); + await call('claims_status', { issueId: 'done-1', status: 'completed' }); + + const result = await call('claims_load', {}); + expect(result.totalAgents).toBe(0); + expect(result.totalClaims).toBe(0); + }); + + it('claims_rebalance does not treat finished work as overload', async () => { + // a1 finished 5 issues and has 1 open one; a2 has 1 open issue. + for (let i = 0; i < 5; i++) { + await call('claims_claim', { issueId: `done-${i}`, claimant: 'agent:a1:coder' }); + await call('claims_status', { issueId: `done-${i}`, status: 'completed' }); + } + await call('claims_claim', { issueId: 'open-a1', claimant: 'agent:a1:coder' }); + await call('claims_claim', { issueId: 'open-a2', claimant: 'agent:a2:coder' }); + + const result = await call('claims_rebalance', { dryRun: false }); + expect(result.suggestions).toEqual([]); + expect(result.metrics.overloadedCount).toBe(0); + const claim = (await call('claims_list', {})).claims.find((c: Result) => c.issueId === 'open-a1'); + expect(claim.claimant.agentId).toBe('a1'); + }); +}); 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 e0a4adcc78..63e4e19d5f 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/claims-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/claims-tools.ts @@ -77,6 +77,11 @@ function formatClaimant(claimant: Claimant): string { : `agent:${claimant.agentId}:${claimant.agentType}`; } +/** An agent's claim that is still work in hand (not completed). */ +function isOpenAgentClaim(claim: IssueClaim): boolean { + return claim.claimant.type === 'agent' && claim.status !== 'completed'; +} + function parseClaimant(str: string): Claimant | null { const parts = str.split(':'); if (parts[0] === 'human' && parts.length >= 3) { @@ -708,7 +713,9 @@ export const claimsTools: MCPTool[] = [ if (agentType) { const v = validateIdentifier(agentType, 'agentType'); if (!v.valid) return { success: false, error: v.error }; } const store = loadClaims(); - const claims = Object.values(store.claims); + // A completed claim stays in the store but is no longer work in hand, + // so it does not count toward an agent's load. + const openAgentClaims = Object.values(store.claims).filter(isOpenAgentClaim); // Group claims by agent const agentLoads = new Map(); - for (const claim of claims) { - if (claim.claimant.type !== 'agent') continue; - + for (const claim of openAgentClaims) { const key = claim.claimant.agentId!; if (!agentLoads.has(key)) { agentLoads.set(key, { @@ -766,7 +771,7 @@ export const claimsTools: MCPTool[] = [ success: true, loads: result, totalAgents: result.length, - totalClaims: claims.filter(c => c.claimant.type === 'agent').length, + totalClaims: openAgentClaims.length, avgUtilization: result.length > 0 ? result.reduce((sum, l) => sum + l.utilization, 0) / result.length : 0, @@ -852,14 +857,13 @@ export const claimsTools: MCPTool[] = [ const targetUtilization = (input.targetUtilization as number) || 0.7; const store = loadClaims(); - const claims = Object.values(store.claims); + // Completed claims are not load, as in claims_load. + const openAgentClaims = Object.values(store.claims).filter(isOpenAgentClaim); // Group by agent const agentLoads = new Map(); - for (const claim of claims) { - if (claim.claimant.type !== 'agent') continue; - + for (const claim of openAgentClaims) { const key = claim.claimant.agentId!; if (!agentLoads.has(key)) { agentLoads.set(key, { agentId: key, agentType: claim.claimant.agentType!, claims: [] });