🌊 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(swarm): stop treating silent idle agents as heartbeat failures (#4320)
ordinary beingab40a99f3a0f
diff --git a/v3/@claude-flow/swarm/__tests__/idle-agent-heartbeat.test.ts b/v3/@claude-flow/swarm/__tests__/idle-agent-heartbeat.test.tsnew file mode 100644index 0000000000..b27368f3ac--- /dev/null+++ b/v3/@claude-flow/swarm/__tests__/idle-agent-heartbeat.test.ts@@ -0,0 +1,49 @@+import { describe, it, expect, beforeEach, afterEach } from 'vitest';+import { UnifiedSwarmCoordinator, createUnifiedSwarmCoordinator } from '../src/unified-coordinator.js';++function createCoordinator(): UnifiedSwarmCoordinator {+ return createUnifiedSwarmCoordinator({+ maxAgents: 20,+ maxTasks: 100,+ heartbeatIntervalMs: 60000,+ healthCheckIntervalMs: 60000,+ taskTimeoutMs: 10000,+ autoRecovery: true,+ 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: {},+ };+}++describe('idle agent heartbeat', () => {+ let c: UnifiedSwarmCoordinator;+ beforeEach(async () => { c = createCoordinator(); await c.initialize(); });+ afterEach(async () => { await c.shutdown(); });++ function check() { (c as unknown as { checkHeartbeats(): void }).checkHeartbeats(); }++ it('does not mark a silent idle agent as error', async () => {+ const { agentId } = await c.spawnAgent({ type: 'coder', domain: 'core' });+ const agent = c.getAgent(agentId)!;+ agent.lastHeartbeat = new Date(Date.now() - 200000);+ check();+ expect(agent.status).toBe('idle');+ expect(agent.health).toBe(1);+ });++ it('still marks a silent busy agent as error', async () => {+ const { agentId } = await c.spawnAgent({ type: 'coder', domain: 'core' });+ await c.submitTask(taskData('busy'));+ const agent = c.getAgent(agentId)!;+ expect(agent.status).toBe('busy');+ agent.lastHeartbeat = new Date(Date.now() - 200000);+ check();+ expect(agent.status).toBe('error');+ });+});diff --git a/v3/@claude-flow/swarm/src/unified-coordinator.ts b/v3/@claude-flow/swarm/src/unified-coordinator.tsindex 986fcdb5f5..a85d1ce189 100644--- a/v3/@claude-flow/swarm/src/unified-coordinator.ts+++ b/v3/@claude-flow/swarm/src/unified-coordinator.ts@@ -1036,9 +1036,18 @@ export class UnifiedSwarmCoordinator extends EventEmitter implements IUnifiedSwaconst timeout = this.config.heartbeatIntervalMs * 3;for (const [agentId, agent] of this.state.agents) {+ // An idle agent has no heartbeat producer and no in-flight work to+ // prove liveness with. Treating silence as failure made every idle+ // agent flip to 'error' after 3 intervals (and oscillate back through+ // auto-recovery), so `getAvailableAgents()` stopped offering healthy+ // workers. Only agents that are actually working need a heartbeat.+ if (agent.status === 'terminated' || (agent.status === 'idle' && !agent.currentTask)) {+ continue;+ }+const timeSinceHeartbeat = now - agent.lastHeartbeat.getTime();- if (timeSinceHeartbeat > timeout && agent.status !== 'terminated') {+ if (timeSinceHeartbeat > timeout) {agent.status = 'error';agent.health = Math.max(0, agent.health - 0.2);