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(swarm): release the old domain slot when a retry moves agents (#4499)

ordinary being118d3a0ab463
diff --git a/v3/@claude-flow/swarm/__tests__/retry-domain-release-4458.test.ts b/v3/@claude-flow/swarm/__tests__/retry-domain-release-4458.test.ts
new file mode 100644
index 0000000000..6508acae42
--- /dev/null
+++ b/v3/@claude-flow/swarm/__tests__/retry-domain-release-4458.test.ts
@@ -0,0 +1,93 @@
+/**
+ * #4458 swarm scan follow-up: handleTaskFail()'s retry branch cleared the
+ * failed agent's AgentState and called assignTask() without releasing the
+ * agent's domain-pool reservation. When the retry went to a different agent,
+ * the original slot stayed busy forever. The same-agent path must keep the
+ * reservation (the retried task now owns it).
+ */
+
+import { describe, expect, it } from 'vitest';
+import {
+ UnifiedSwarmCoordinator,
+ createUnifiedSwarmCoordinator,
+} from '../src/unified-coordinator.ts';
+
+function createCoordinator(): UnifiedSwarmCoordinator {
+ return createUnifiedSwarmCoordinator({
+ maxAgents: 20,
+ maxTasks: 100,
+ heartbeatIntervalMs: 1000,
+ healthCheckIntervalMs: 2000,
+ taskTimeoutMs: 10000,
+ topology: { type: 'hierarchical', maxAgents: 20 },
+ consensus: {
+ algorithm: 'raft',
+ threshold: 0.66,
+ timeoutMs: 5000,
+ maxRounds: 5,
+ requireQuorum: true,
+ },
+ });
+}
+
+function taskData(name: string) {
+ return {
+ type: 'coding' as const,
+ name,
+ description: name,
+ priority: 'normal' as const,
+ dependencies: [],
+ input: {},
+ timeoutMs: 5000,
+ retries: 0,
+ maxRetries: 3,
+ metadata: {},
+ };
+}
+
+async function flush(): Promise<void> {
+ await new Promise(resolve => setTimeout(resolve, 20));
+}
+
+describe('UnifiedSwarmCoordinator retry domain-slot release (#4458)', () => {
+ it('releases the failed domain agent when the retry is assigned elsewhere', async () => {
+ const coordinator = createCoordinator();
+ await coordinator.initialize();
+
+ try {
+ const taskId = await coordinator.submitTask(taskData('retryable'));
+
+ // The explicitly domain-routed agent has no coding type bonus; the
+ // generic coder should win scoreAgentForTask() when the retry is
+ // assigned after handleTaskFail() marks the first agent idle.
+ const { agentId: domainAgentId } = await coordinator.spawnAgent({
+ type: 'tester',
+ domain: 'core',
+ });
+
+ expect(await coordinator.assignTaskToDomain(taskId, 'core')).toBe(domainAgentId);
+
+ // Queue a second domain task while the only pool agent is busy. Spawn
+ // the generic coder only afterwards so submitTask() cannot auto-assign
+ // this task before it reaches the domain queue.
+ const queuedTaskId = await coordinator.submitTask(taskData('queued-behind-retry'));
+ expect(await coordinator.assignTaskToDomain(queuedTaskId, 'core')).toBeUndefined();
+ expect(coordinator.getTask(queuedTaskId)?.status).toBe('queued');
+
+ const { agentId: coderAgentId } = await coordinator.spawnAgent({ type: 'coder' });
+ expect(coordinator.getDomainPool('core')!.getState().busyAgents).toContain(domainAgentId);
+
+ (coordinator as unknown as {
+ handleTaskFail(agentId: string, data: { taskId: string; error: string }): void;
+ }).handleTaskFail(domainAgentId, { taskId, error: 'retryable failure' });
+ await flush();
+
+ expect(coordinator.getTask(taskId)?.assignedTo?.id).toBe(coderAgentId);
+ expect(coordinator.getTask(queuedTaskId)?.status).toBe('assigned');
+ expect(coordinator.getTask(queuedTaskId)?.assignedTo?.id).toBe(domainAgentId);
+ expect(coordinator.getAgent(domainAgentId)?.currentTask?.id).toBe(queuedTaskId);
+ } finally {
+ await coordinator.shutdown();
+ }
+ });
+});
diff --git a/v3/@claude-flow/swarm/src/unified-coordinator.ts b/v3/@claude-flow/swarm/src/unified-coordinator.ts
index a85d1ce189..fba841afa2 100644
--- a/v3/@claude-flow/swarm/src/unified-coordinator.ts
+++ b/v3/@claude-flow/swarm/src/unified-coordinator.ts
@@ -982,8 +982,17 @@ export class UnifiedSwarmCoordinator extends EventEmitter implements IUnifiedSwa
agent.currentTask = undefined;
agent.status = 'idle';
- // Re-assign
- this.assignTask(task);
+ // Re-assign. The failed agent is idle now, so assignTask() may hand
+ // the retry back to this same agent (keep its pool reservation) or to
+ // a different available agent. In the latter case the original domain
+ // reservation must be released, or that pool slot stays busy forever.
+ void this.assignTask(task).then(nextAgent => {
+ if (nextAgent?.id.id !== agentId) {
+ void this.releaseDomainAgent(agentId);
+ }
+ }).catch(() => {
+ void this.releaseDomainAgent(agentId);
+ });
} else {
task.status = 'failed';
agent.status = 'idle';