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): wire downgraded topology leaders only to the leader (#4483)

ordinary being31f554c7b290
diff --git a/v3/@claude-flow/swarm/__tests__/topology-role-resolution-4481.test.ts b/v3/@claude-flow/swarm/__tests__/topology-role-resolution-4481.test.ts
new file mode 100644
index 0000000000..6eb7d03506
--- /dev/null
+++ b/v3/@claude-flow/swarm/__tests__/topology-role-resolution-4481.test.ts
@@ -0,0 +1,55 @@
+/**
+ * #4481 hive-mind scan: addNode() resolved a requested role for the node's
+ * `role` field but passed the *raw* request to calculateInitialConnections().
+ * In centralized/hierarchical topologies a second node requesting
+ * coordinator/queen is downgraded to worker, yet was wired to every existing
+ * node as if it were the leader.
+ */
+
+import { describe, expect, it } from 'vitest';
+// Import the TS source explicitly: there is no generated .js for this module,
+// and this keeps the implementation under test unambiguous.
+import { createTopologyManager } from '../src/topology-manager.ts';
+
+const CASES = [
+ {
+ type: 'centralized' as const,
+ leaderRequest: 'coordinator' as const,
+ leaderRole: 'coordinator' as const,
+ },
+ {
+ type: 'hierarchical' as const,
+ leaderRequest: 'queen' as const,
+ leaderRole: 'queen' as const,
+ },
+];
+
+describe('TopologyManager resolved-role initial connections (#4481)', () => {
+ it.each(CASES)(
+ 'wires a downgraded $type leader only to the existing leader',
+ async ({ type, leaderRequest, leaderRole }) => {
+ const topology = createTopologyManager({
+ type,
+ maxAgents: 10,
+ autoRebalance: false,
+ });
+ await topology.initialize();
+
+ const leader = await topology.addNode('leader', leaderRequest);
+ expect(leader.role).toBe(leaderRole);
+
+ const worker = await topology.addNode('worker', 'worker');
+ expect(worker.role).toBe('worker');
+ expect(worker.connections).toEqual(['leader']);
+
+ // A second request for the leader role is resolved to worker. It must
+ // connect to the leader only, not to every pre-existing node.
+ const downgraded = await topology.addNode('late-leader', leaderRequest);
+ expect(downgraded.role).toBe('worker');
+ expect(downgraded.connections).toEqual(['leader']);
+ expect(topology.getNeighbors('late-leader')).toEqual(['leader']);
+ expect(topology.isConnected('late-leader', 'leader')).toBe(true);
+ expect(topology.isConnected('late-leader', 'worker')).toBe(false);
+ },
+ );
+});
diff --git a/v3/@claude-flow/swarm/src/topology-manager.ts b/v3/@claude-flow/swarm/src/topology-manager.ts
index c784631661..97f9058f29 100644
--- a/v3/@claude-flow/swarm/src/topology-manager.ts
+++ b/v3/@claude-flow/swarm/src/topology-manager.ts
@@ -73,13 +73,20 @@ export class TopologyManager extends EventEmitter implements ITopologyManager {
throw new Error(`Maximum agents (${this.config.maxAgents}) reached`);
}
+ // Resolve the role before wiring connections. In hierarchical and
+ // centralized topologies determineRole() may downgrade a requested
+ // queen/coordinator to worker once a leader exists; calculateInitialConnections()
+ // must see the resolved role, not the raw request, or a downgraded node is
+ // wired to every existing node as if it were the leader.
+ const resolvedRole = this.determineRole(role);
+
// Create node with connections based on topology type
- const connections = this.calculateInitialConnections(agentId, role);
+ const connections = this.calculateInitialConnections(agentId, resolvedRole);
const node: TopologyNode = {
id: `node_${agentId}`,
agentId,
- role: this.determineRole(role),
+ role: resolvedRole,
status: 'syncing',
connections,
metadata: {