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

dream(swarm): fix spawnAgent() auto-domain branch skipping pool.add()

Claude1f9d9db72773
diff --git a/v3/@claude-flow/swarm/__tests__/spawn-agent-domain-pool.test.ts b/v3/@claude-flow/swarm/__tests__/spawn-agent-domain-pool.test.ts
new file mode 100644
index 0000000000..851aba2574
--- /dev/null
+++ b/v3/@claude-flow/swarm/__tests__/spawn-agent-domain-pool.test.ts
@@ -0,0 +1,107 @@
+/**
+ * Discriminating regression test for the Dream Cycle 2026-09-29 (swarm) fix:
+ * UnifiedSwarmCoordinator.spawnAgent()'s auto-domain branch (no `domain`/
+ * `agentNumber` given — the plain agentic-flow-compatible call shape) never
+ * called `pool.add()`, unlike the other two spawnAgent() branches which both
+ * go through registerAgentWithDomain() (which does call pool.add()). The
+ * agent was registered in state.agents/agentDomainMap and reported idle by
+ * listAgents()/getAgent(), but stayed invisible to the domain pool that
+ * assignTaskToDomain() actually draws from via pool.acquire() — so a task
+ * routed to that domain either scale-up-creates a *different* agent (when
+ * the pool isn't at maxSize) or queues forever (when it is), while the
+ * originally spawned agent sits idle and unused forever.
+ */
+
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { UnifiedSwarmCoordinator, createUnifiedSwarmCoordinator } from '../src/unified-coordinator.js';
+
+describe('spawnAgent() auto-domain branch — domain pool visibility', () => {
+ let coordinator: UnifiedSwarmCoordinator;
+
+ beforeEach(async () => {
+ coordinator = 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,
+ },
+ });
+
+ await coordinator.initialize();
+ });
+
+ afterEach(async () => {
+ await coordinator.shutdown();
+ });
+
+ it('adds the spawned agent to its domain pool (visible in pool stats)', async () => {
+ const { agentId, domain } = await coordinator.spawnAgent({ type: 'coder' });
+ expect(domain).toBe('core');
+
+ const pool = coordinator.getDomainPool(domain);
+ expect(pool).toBeDefined();
+
+ const stats = pool!.getPoolStats();
+ // Baseline: total is 0 — the agent was never added to the pool.
+ expect(stats.total).toBe(1);
+ expect(stats.available).toBe(1);
+
+ // The agent itself is still visible/idle through the generic accessor.
+ const agent = coordinator.getAgent(agentId);
+ expect(agent?.status).toBe('idle');
+ });
+
+ it('routes a domain-assigned task to the agent spawnAgent() actually returned', async () => {
+ const { agentId, domain } = await coordinator.spawnAgent({ type: 'coder' });
+
+ const taskId = await coordinator.submitTask({
+ type: 'coding',
+ name: 'Domain-routed task',
+ description: 'Should be routed to the auto-domain-spawned agent',
+ priority: 'normal',
+ dependencies: [],
+ input: {},
+ timeoutMs: 5000,
+ retries: 0,
+ maxRetries: 3,
+ metadata: {},
+ });
+
+ const assignedAgentId = await coordinator.assignTaskToDomain(taskId, domain);
+
+ // Baseline: the domain pool is empty (agent never added), so acquire()
+ // scale-up-creates a brand new agent instead of finding the one
+ // spawnAgent() returned — assignedAgentId !== agentId, and a second,
+ // orphaned agent now exists in the pool that spawnAgent()'s caller
+ // never sees.
+ expect(assignedAgentId).toBe(agentId);
+
+ const task = coordinator.getTask(taskId);
+ expect(task?.assignedTo?.id).toBe(agentId);
+ });
+
+ it('still works correctly via the domain-aware branches (regression guard)', async () => {
+ // registerAgentWithDomain()'s own pool.add() call path must be
+ // unaffected by this fix.
+ const { agentId, domain } = await coordinator.spawnAgent({ type: 'coder', agentNumber: 5 });
+ expect(domain).toBe('core');
+
+ const pool = coordinator.getDomainPool(domain);
+ expect(pool!.getPoolStats().total).toBe(1);
+ expect(pool!.getPoolStats().available).toBe(1);
+
+ const agent = coordinator.getAgent(agentId);
+ expect(agent?.status).toBe('idle');
+ });
+});
diff --git a/v3/@claude-flow/swarm/src/unified-coordinator.ts b/v3/@claude-flow/swarm/src/unified-coordinator.ts
index 647ef89888..53ab8b62f5 100644
--- a/v3/@claude-flow/swarm/src/unified-coordinator.ts
+++ b/v3/@claude-flow/swarm/src/unified-coordinator.ts
@@ -1687,6 +1687,15 @@ export class UnifiedSwarmCoordinator extends EventEmitter implements IUnifiedSwa
domain = this.agentTypeToDomain(options.type);
agentId = await this.registerAgent(agentData);
this.agentDomainMap.set(agentId, domain);
+
+ // Add to domain pool so assignTaskToDomain() can route work to this
+ // agent (mirrors registerAgentWithDomain()'s pool.add() above; without
+ // this the agent is registered/idle but invisible to pool.acquire()).
+ const pool = this.domainPools.get(domain);
+ const agent = this.state.agents.get(agentId);
+ if (pool && agent) {
+ await pool.add(agent);
+ }
}
const duration = performance.now() - startTime;