@claude-flow/swarm 3.0.0-alpha.8 → 3.0.0-alpha.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/dist/agent-pool.d.ts.map +1 -1
  2. package/dist/agent-pool.js +10 -2
  3. package/dist/agent-pool.js.map +1 -1
  4. package/dist/attention-coordinator.d.ts +5 -4
  5. package/dist/attention-coordinator.d.ts.map +1 -1
  6. package/dist/attention-coordinator.js +17 -10
  7. package/dist/attention-coordinator.js.map +1 -1
  8. package/dist/consensus/byzantine.d.ts +2 -1
  9. package/dist/consensus/byzantine.d.ts.map +1 -1
  10. package/dist/consensus/byzantine.js +34 -8
  11. package/dist/consensus/byzantine.js.map +1 -1
  12. package/dist/consensus/gossip.d.ts +2 -1
  13. package/dist/consensus/gossip.d.ts.map +1 -1
  14. package/dist/consensus/gossip.js +27 -8
  15. package/dist/consensus/gossip.js.map +1 -1
  16. package/dist/consensus/index.d.ts +1 -1
  17. package/dist/consensus/index.d.ts.map +1 -1
  18. package/dist/consensus/index.js +2 -30
  19. package/dist/consensus/index.js.map +1 -1
  20. package/dist/consensus/raft.d.ts +2 -1
  21. package/dist/consensus/raft.d.ts.map +1 -1
  22. package/dist/consensus/raft.js +25 -9
  23. package/dist/consensus/raft.js.map +1 -1
  24. package/dist/index.js +2 -2
  25. package/dist/index.js.map +1 -1
  26. package/dist/message-bus.d.ts.map +1 -1
  27. package/dist/message-bus.js +13 -4
  28. package/dist/message-bus.js.map +1 -1
  29. package/dist/queen-coordinator.d.ts +1 -1
  30. package/dist/queen-coordinator.d.ts.map +1 -1
  31. package/dist/queen-coordinator.js +10 -3
  32. package/dist/queen-coordinator.js.map +1 -1
  33. package/dist/topology-manager.d.ts.map +1 -1
  34. package/dist/topology-manager.js +26 -1
  35. package/dist/topology-manager.js.map +1 -1
  36. package/dist/types.d.ts +3 -12
  37. package/dist/types.d.ts.map +1 -1
  38. package/dist/types.js.map +1 -1
  39. package/dist/unified-coordinator.d.ts +1 -1
  40. package/dist/unified-coordinator.d.ts.map +1 -1
  41. package/dist/unified-coordinator.js +85 -29
  42. package/dist/unified-coordinator.js.map +1 -1
  43. package/package.json +7 -2
  44. package/.claude-flow/data/pending-insights.jsonl +0 -15
  45. package/.claude-flow/hive-mind/state.json +0 -18
  46. package/__tests__/byzantine-transport.test.ts +0 -147
  47. package/__tests__/consensus-failure-injection.test.ts +0 -161
  48. package/__tests__/consensus-transport.test.ts +0 -200
  49. package/__tests__/consensus.test.ts +0 -577
  50. package/__tests__/coordinator.test.ts +0 -501
  51. package/__tests__/federation-transport.test.ts +0 -145
  52. package/__tests__/gossip-transport.test.ts +0 -81
  53. package/__tests__/queen-coordinator.test.ts +0 -1335
  54. package/__tests__/raft-transport.test.ts +0 -121
  55. package/__tests__/topology.test.ts +0 -621
  56. package/ruvector.db +0 -0
  57. package/src/agent-pool.ts +0 -476
  58. package/src/application/commands/create-task.command.ts +0 -124
  59. package/src/application/commands/spawn-agent.command.ts +0 -122
  60. package/src/application/index.ts +0 -30
  61. package/src/application/services/swarm-application-service.ts +0 -200
  62. package/src/attention-coordinator.ts +0 -1000
  63. package/src/consensus/byzantine.ts +0 -514
  64. package/src/consensus/federation-transport.ts +0 -185
  65. package/src/consensus/gossip.ts +0 -599
  66. package/src/consensus/index.ts +0 -326
  67. package/src/consensus/raft.ts +0 -561
  68. package/src/consensus/transport.ts +0 -284
  69. package/src/coordination/agent-registry.ts +0 -544
  70. package/src/coordination/index.ts +0 -23
  71. package/src/coordination/swarm-hub.ts +0 -776
  72. package/src/coordination/task-orchestrator.ts +0 -605
  73. package/src/domain/entities/agent.d.ts +0 -151
  74. package/src/domain/entities/agent.d.ts.map +0 -1
  75. package/src/domain/entities/agent.js +0 -280
  76. package/src/domain/entities/agent.js.map +0 -1
  77. package/src/domain/entities/agent.ts +0 -370
  78. package/src/domain/entities/task.d.ts +0 -133
  79. package/src/domain/entities/task.d.ts.map +0 -1
  80. package/src/domain/entities/task.js +0 -261
  81. package/src/domain/entities/task.js.map +0 -1
  82. package/src/domain/entities/task.ts +0 -319
  83. package/src/domain/index.ts +0 -41
  84. package/src/domain/repositories/agent-repository.interface.d.ts +0 -57
  85. package/src/domain/repositories/agent-repository.interface.d.ts.map +0 -1
  86. package/src/domain/repositories/agent-repository.interface.js +0 -9
  87. package/src/domain/repositories/agent-repository.interface.js.map +0 -1
  88. package/src/domain/repositories/agent-repository.interface.ts +0 -69
  89. package/src/domain/repositories/task-repository.interface.d.ts +0 -61
  90. package/src/domain/repositories/task-repository.interface.d.ts.map +0 -1
  91. package/src/domain/repositories/task-repository.interface.js +0 -9
  92. package/src/domain/repositories/task-repository.interface.js.map +0 -1
  93. package/src/domain/repositories/task-repository.interface.ts +0 -75
  94. package/src/domain/services/coordination-service.ts +0 -320
  95. package/src/federation-hub.d.ts +0 -284
  96. package/src/federation-hub.d.ts.map +0 -1
  97. package/src/federation-hub.js +0 -692
  98. package/src/federation-hub.js.map +0 -1
  99. package/src/federation-hub.ts +0 -979
  100. package/src/index.ts +0 -374
  101. package/src/message-bus.ts +0 -607
  102. package/src/queen-coordinator.ts +0 -2025
  103. package/src/shared/events.ts +0 -285
  104. package/src/shared/types.ts +0 -389
  105. package/src/topology-manager.ts +0 -656
  106. package/src/types.ts +0 -555
  107. package/src/unified-coordinator.ts +0 -1844
  108. package/src/workers/index.ts +0 -65
  109. package/src/workers/worker-dispatch.d.ts +0 -234
  110. package/src/workers/worker-dispatch.d.ts.map +0 -1
  111. package/src/workers/worker-dispatch.js +0 -842
  112. package/src/workers/worker-dispatch.js.map +0 -1
  113. package/src/workers/worker-dispatch.ts +0 -1076
  114. package/tmp.json +0 -0
  115. package/tsconfig.json +0 -9
  116. package/tsconfig.tsbuildinfo +0 -1
  117. package/vitest.config.ts +0 -20
@@ -1,326 +0,0 @@
1
- /**
2
- * V3 Consensus Engine Factory
3
- * Unified interface for different consensus algorithms
4
- */
5
-
6
- import { EventEmitter } from 'events';
7
- import {
8
- ConsensusAlgorithm,
9
- ConsensusConfig,
10
- ConsensusProposal,
11
- ConsensusVote,
12
- ConsensusResult,
13
- IConsensusEngine,
14
- SWARM_CONSTANTS,
15
- } from '../types.js';
16
- import { RaftConsensus, createRaftConsensus, RaftConfig } from './raft.js';
17
- import { ByzantineConsensus, createByzantineConsensus, ByzantineConfig } from './byzantine.js';
18
- import { GossipConsensus, createGossipConsensus, GossipConfig } from './gossip.js';
19
- import type { ConsensusTransport } from './transport.js';
20
-
21
- export { RaftConsensus, ByzantineConsensus, GossipConsensus };
22
- export type { RaftConfig, ByzantineConfig, GossipConfig };
23
-
24
- // ADR-095 G2 — pluggable consensus transport. Replaces the implicit
25
- // single-process EventEmitter messaging in the consensus protocols.
26
- // LocalTransport is the default (matches current behavior); FederationTransport
27
- // (separate file, ADR-104 wire) is the multi-host one.
28
- export {
29
- LocalTransport,
30
- LocalTransportRegistry,
31
- defaultLocalRegistry,
32
- generateNodeKeyPair,
33
- signMessage,
34
- verifyMessage,
35
- canonicalizeForSigning,
36
- messageDigest,
37
- } from './transport.js';
38
- export type {
39
- ConsensusTransport,
40
- ConsensusMessage,
41
- ConsensusReply,
42
- ConsensusMessageHandler,
43
- NodeKeyPair,
44
- LocalTransportOptions,
45
- } from './transport.js';
46
-
47
- // ADR-095 G2 — FederationTransport: ConsensusTransport over the federation
48
- // plugin's ADR-104 WS wire (agentic-flow/transport/loader). Structural —
49
- // swarm doesn't import agentic-flow; the caller passes a transport instance.
50
- export { FederationTransport } from './federation-transport.js';
51
- export type { AgenticFlowTransportLike, FederationTransportOptions } from './federation-transport.js';
52
-
53
- type ConsensusImplementation = RaftConsensus | ByzantineConsensus | GossipConsensus;
54
-
55
- export class ConsensusEngine extends EventEmitter implements IConsensusEngine {
56
- private config: ConsensusConfig;
57
- private nodeId: string;
58
- private implementation?: ConsensusImplementation;
59
- private proposals: Map<string, ConsensusProposal> = new Map();
60
-
61
- constructor(nodeId: string, config: Partial<ConsensusConfig> = {}) {
62
- super();
63
- this.nodeId = nodeId;
64
- this.config = {
65
- algorithm: config.algorithm ?? 'raft',
66
- threshold: config.threshold ?? SWARM_CONSTANTS.DEFAULT_CONSENSUS_THRESHOLD,
67
- timeoutMs: config.timeoutMs ?? SWARM_CONSTANTS.DEFAULT_CONSENSUS_TIMEOUT_MS,
68
- maxRounds: config.maxRounds ?? 10,
69
- requireQuorum: config.requireQuorum ?? true,
70
- };
71
- }
72
-
73
- async initialize(config?: ConsensusConfig): Promise<void> {
74
- if (config) {
75
- this.config = { ...this.config, ...config };
76
- }
77
-
78
- // ADR-095 G2.2 — narrow the typed-as-unknown `transport` from
79
- // ConsensusConfig into the real ConsensusTransport so Raft / Byzantine /
80
- // Gossip get a working inter-node wire. Structural check, not
81
- // instanceof, so test mocks satisfy it without importing transport.ts.
82
- const transport = isConsensusTransport(this.config.transport)
83
- ? this.config.transport
84
- : undefined;
85
-
86
- // Create implementation based on algorithm
87
- switch (this.config.algorithm) {
88
- case 'raft':
89
- this.implementation = createRaftConsensus(this.nodeId, {
90
- threshold: this.config.threshold,
91
- timeoutMs: this.config.timeoutMs,
92
- maxRounds: this.config.maxRounds,
93
- requireQuorum: this.config.requireQuorum,
94
- transport,
95
- });
96
- break;
97
-
98
- case 'byzantine':
99
- this.implementation = createByzantineConsensus(this.nodeId, {
100
- threshold: this.config.threshold,
101
- timeoutMs: this.config.timeoutMs,
102
- maxRounds: this.config.maxRounds,
103
- requireQuorum: this.config.requireQuorum,
104
- transport,
105
- });
106
- break;
107
-
108
- case 'gossip':
109
- this.implementation = createGossipConsensus(this.nodeId, {
110
- threshold: this.config.threshold,
111
- timeoutMs: this.config.timeoutMs,
112
- maxRounds: this.config.maxRounds,
113
- requireQuorum: this.config.requireQuorum,
114
- transport,
115
- });
116
- break;
117
-
118
- case 'paxos':
119
- // Fall back to Raft for Paxos (similar guarantees)
120
- this.implementation = createRaftConsensus(this.nodeId, {
121
- threshold: this.config.threshold,
122
- timeoutMs: this.config.timeoutMs,
123
- maxRounds: this.config.maxRounds,
124
- requireQuorum: this.config.requireQuorum,
125
- transport,
126
- });
127
- break;
128
-
129
- default:
130
- throw new Error(`Unknown consensus algorithm: ${this.config.algorithm}`);
131
- }
132
-
133
- await this.implementation.initialize();
134
-
135
- // Forward events
136
- this.implementation.on('consensus.achieved', (data) => {
137
- this.emit('consensus.achieved', data);
138
- });
139
-
140
- this.implementation.on('leader.elected', (data) => {
141
- this.emit('leader.elected', data);
142
- });
143
-
144
- this.emit('initialized', {
145
- nodeId: this.nodeId,
146
- algorithm: this.config.algorithm
147
- });
148
- }
149
-
150
- async shutdown(): Promise<void> {
151
- if (this.implementation) {
152
- await this.implementation.shutdown();
153
- }
154
- this.emit('shutdown');
155
- }
156
-
157
- addNode(nodeId: string, options?: { isPrimary?: boolean }): void {
158
- if (!this.implementation) {
159
- throw new Error('Consensus engine not initialized');
160
- }
161
-
162
- if (this.implementation instanceof RaftConsensus) {
163
- this.implementation.addPeer(nodeId);
164
- } else if (this.implementation instanceof ByzantineConsensus) {
165
- this.implementation.addNode(nodeId, options?.isPrimary);
166
- } else if (this.implementation instanceof GossipConsensus) {
167
- this.implementation.addNode(nodeId);
168
- }
169
- }
170
-
171
- removeNode(nodeId: string): void {
172
- if (!this.implementation) {
173
- return;
174
- }
175
-
176
- if (this.implementation instanceof RaftConsensus) {
177
- this.implementation.removePeer(nodeId);
178
- } else if (this.implementation instanceof ByzantineConsensus) {
179
- this.implementation.removeNode(nodeId);
180
- } else if (this.implementation instanceof GossipConsensus) {
181
- this.implementation.removeNode(nodeId);
182
- }
183
- }
184
-
185
- async propose(value: unknown, proposerId?: string): Promise<ConsensusProposal> {
186
- if (!this.implementation) {
187
- throw new Error('Consensus engine not initialized');
188
- }
189
-
190
- const proposal = await this.implementation.propose(value);
191
- this.proposals.set(proposal.id, proposal);
192
- return proposal;
193
- }
194
-
195
- async vote(proposalId: string, vote: ConsensusVote): Promise<void> {
196
- if (!this.implementation) {
197
- throw new Error('Consensus engine not initialized');
198
- }
199
-
200
- await this.implementation.vote(proposalId, vote);
201
- }
202
-
203
- getProposal(proposalId: string): ConsensusProposal | undefined {
204
- return this.proposals.get(proposalId);
205
- }
206
-
207
- async awaitConsensus(proposalId: string): Promise<ConsensusResult> {
208
- if (!this.implementation) {
209
- throw new Error('Consensus engine not initialized');
210
- }
211
-
212
- return this.implementation.awaitConsensus(proposalId);
213
- }
214
-
215
- getActiveProposals(): ConsensusProposal[] {
216
- return Array.from(this.proposals.values()).filter(
217
- p => p.status === 'pending'
218
- );
219
- }
220
-
221
- // Algorithm-specific queries
222
- isLeader(): boolean {
223
- if (this.implementation instanceof RaftConsensus) {
224
- return this.implementation.isLeader();
225
- }
226
- if (this.implementation instanceof ByzantineConsensus) {
227
- return this.implementation.isPrimary();
228
- }
229
- return false; // Gossip has no leader
230
- }
231
-
232
- getLeaderId(): string | undefined {
233
- if (this.implementation instanceof RaftConsensus) {
234
- return this.implementation.getLeaderId();
235
- }
236
- return undefined;
237
- }
238
-
239
- getAlgorithm(): ConsensusAlgorithm {
240
- return this.config.algorithm;
241
- }
242
-
243
- getConfig(): ConsensusConfig {
244
- return { ...this.config };
245
- }
246
-
247
- // Metrics
248
- getStats(): {
249
- algorithm: ConsensusAlgorithm;
250
- totalProposals: number;
251
- pendingProposals: number;
252
- acceptedProposals: number;
253
- rejectedProposals: number;
254
- expiredProposals: number;
255
- } {
256
- const proposals = Array.from(this.proposals.values());
257
-
258
- return {
259
- algorithm: this.config.algorithm,
260
- totalProposals: proposals.length,
261
- pendingProposals: proposals.filter(p => p.status === 'pending').length,
262
- acceptedProposals: proposals.filter(p => p.status === 'accepted').length,
263
- rejectedProposals: proposals.filter(p => p.status === 'rejected').length,
264
- expiredProposals: proposals.filter(p => p.status === 'expired').length,
265
- };
266
- }
267
- }
268
-
269
- // Factory function
270
- export function createConsensusEngine(
271
- nodeId: string,
272
- algorithm: ConsensusAlgorithm = 'raft',
273
- config?: Partial<ConsensusConfig>
274
- ): ConsensusEngine {
275
- return new ConsensusEngine(nodeId, { ...config, algorithm });
276
- }
277
-
278
- /**
279
- * ADR-095 G2.2 — structural check that an opaque value implements the
280
- * ConsensusTransport interface. Used by the engine to safely narrow the
281
- * `transport` field on `ConsensusConfig` (typed as `unknown` to keep
282
- * `types.ts` free of cross-module imports).
283
- */
284
- function isConsensusTransport(value: unknown): value is ConsensusTransport {
285
- if (!value || typeof value !== 'object') return false;
286
- const t = value as Record<string, unknown>;
287
- return typeof t.nodeId === 'string'
288
- && typeof t.send === 'function'
289
- && typeof t.broadcast === 'function'
290
- && typeof t.onMessage === 'function'
291
- && typeof t.peers === 'function'
292
- && typeof t.close === 'function';
293
- }
294
-
295
- // Helper to select optimal algorithm based on requirements
296
- export function selectOptimalAlgorithm(requirements: {
297
- faultTolerance: 'crash' | 'byzantine';
298
- consistency: 'strong' | 'eventual';
299
- networkScale: 'small' | 'medium' | 'large';
300
- latencyPriority: 'low' | 'medium' | 'high';
301
- }): ConsensusAlgorithm {
302
- const { faultTolerance, consistency, networkScale, latencyPriority } = requirements;
303
-
304
- // Byzantine fault tolerance required
305
- if (faultTolerance === 'byzantine') {
306
- return 'byzantine';
307
- }
308
-
309
- // Eventual consistency acceptable and large scale
310
- if (consistency === 'eventual' && networkScale === 'large') {
311
- return 'gossip';
312
- }
313
-
314
- // Low latency priority with medium scale
315
- if (latencyPriority === 'high' && networkScale !== 'large') {
316
- return 'raft';
317
- }
318
-
319
- // Strong consistency with small/medium scale
320
- if (consistency === 'strong') {
321
- return 'raft';
322
- }
323
-
324
- // Default to Raft
325
- return 'raft';
326
- }