@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.
- package/dist/agent-pool.d.ts.map +1 -1
- package/dist/agent-pool.js +10 -2
- package/dist/agent-pool.js.map +1 -1
- package/dist/attention-coordinator.d.ts +5 -4
- package/dist/attention-coordinator.d.ts.map +1 -1
- package/dist/attention-coordinator.js +17 -10
- package/dist/attention-coordinator.js.map +1 -1
- package/dist/consensus/byzantine.d.ts +2 -1
- package/dist/consensus/byzantine.d.ts.map +1 -1
- package/dist/consensus/byzantine.js +34 -8
- package/dist/consensus/byzantine.js.map +1 -1
- package/dist/consensus/gossip.d.ts +2 -1
- package/dist/consensus/gossip.d.ts.map +1 -1
- package/dist/consensus/gossip.js +27 -8
- package/dist/consensus/gossip.js.map +1 -1
- package/dist/consensus/index.d.ts +1 -1
- package/dist/consensus/index.d.ts.map +1 -1
- package/dist/consensus/index.js +2 -30
- package/dist/consensus/index.js.map +1 -1
- package/dist/consensus/raft.d.ts +2 -1
- package/dist/consensus/raft.d.ts.map +1 -1
- package/dist/consensus/raft.js +25 -9
- package/dist/consensus/raft.js.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/message-bus.d.ts.map +1 -1
- package/dist/message-bus.js +13 -4
- package/dist/message-bus.js.map +1 -1
- package/dist/queen-coordinator.d.ts +1 -1
- package/dist/queen-coordinator.d.ts.map +1 -1
- package/dist/queen-coordinator.js +10 -3
- package/dist/queen-coordinator.js.map +1 -1
- package/dist/topology-manager.d.ts.map +1 -1
- package/dist/topology-manager.js +26 -1
- package/dist/topology-manager.js.map +1 -1
- package/dist/types.d.ts +3 -12
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/unified-coordinator.d.ts +1 -1
- package/dist/unified-coordinator.d.ts.map +1 -1
- package/dist/unified-coordinator.js +85 -29
- package/dist/unified-coordinator.js.map +1 -1
- package/package.json +7 -2
- package/.claude-flow/data/pending-insights.jsonl +0 -15
- package/.claude-flow/hive-mind/state.json +0 -18
- package/__tests__/byzantine-transport.test.ts +0 -147
- package/__tests__/consensus-failure-injection.test.ts +0 -161
- package/__tests__/consensus-transport.test.ts +0 -200
- package/__tests__/consensus.test.ts +0 -577
- package/__tests__/coordinator.test.ts +0 -501
- package/__tests__/federation-transport.test.ts +0 -145
- package/__tests__/gossip-transport.test.ts +0 -81
- package/__tests__/queen-coordinator.test.ts +0 -1335
- package/__tests__/raft-transport.test.ts +0 -121
- package/__tests__/topology.test.ts +0 -621
- package/ruvector.db +0 -0
- package/src/agent-pool.ts +0 -476
- package/src/application/commands/create-task.command.ts +0 -124
- package/src/application/commands/spawn-agent.command.ts +0 -122
- package/src/application/index.ts +0 -30
- package/src/application/services/swarm-application-service.ts +0 -200
- package/src/attention-coordinator.ts +0 -1000
- package/src/consensus/byzantine.ts +0 -514
- package/src/consensus/federation-transport.ts +0 -185
- package/src/consensus/gossip.ts +0 -599
- package/src/consensus/index.ts +0 -326
- package/src/consensus/raft.ts +0 -561
- package/src/consensus/transport.ts +0 -284
- package/src/coordination/agent-registry.ts +0 -544
- package/src/coordination/index.ts +0 -23
- package/src/coordination/swarm-hub.ts +0 -776
- package/src/coordination/task-orchestrator.ts +0 -605
- package/src/domain/entities/agent.d.ts +0 -151
- package/src/domain/entities/agent.d.ts.map +0 -1
- package/src/domain/entities/agent.js +0 -280
- package/src/domain/entities/agent.js.map +0 -1
- package/src/domain/entities/agent.ts +0 -370
- package/src/domain/entities/task.d.ts +0 -133
- package/src/domain/entities/task.d.ts.map +0 -1
- package/src/domain/entities/task.js +0 -261
- package/src/domain/entities/task.js.map +0 -1
- package/src/domain/entities/task.ts +0 -319
- package/src/domain/index.ts +0 -41
- package/src/domain/repositories/agent-repository.interface.d.ts +0 -57
- package/src/domain/repositories/agent-repository.interface.d.ts.map +0 -1
- package/src/domain/repositories/agent-repository.interface.js +0 -9
- package/src/domain/repositories/agent-repository.interface.js.map +0 -1
- package/src/domain/repositories/agent-repository.interface.ts +0 -69
- package/src/domain/repositories/task-repository.interface.d.ts +0 -61
- package/src/domain/repositories/task-repository.interface.d.ts.map +0 -1
- package/src/domain/repositories/task-repository.interface.js +0 -9
- package/src/domain/repositories/task-repository.interface.js.map +0 -1
- package/src/domain/repositories/task-repository.interface.ts +0 -75
- package/src/domain/services/coordination-service.ts +0 -320
- package/src/federation-hub.d.ts +0 -284
- package/src/federation-hub.d.ts.map +0 -1
- package/src/federation-hub.js +0 -692
- package/src/federation-hub.js.map +0 -1
- package/src/federation-hub.ts +0 -979
- package/src/index.ts +0 -374
- package/src/message-bus.ts +0 -607
- package/src/queen-coordinator.ts +0 -2025
- package/src/shared/events.ts +0 -285
- package/src/shared/types.ts +0 -389
- package/src/topology-manager.ts +0 -656
- package/src/types.ts +0 -555
- package/src/unified-coordinator.ts +0 -1844
- package/src/workers/index.ts +0 -65
- package/src/workers/worker-dispatch.d.ts +0 -234
- package/src/workers/worker-dispatch.d.ts.map +0 -1
- package/src/workers/worker-dispatch.js +0 -842
- package/src/workers/worker-dispatch.js.map +0 -1
- package/src/workers/worker-dispatch.ts +0 -1076
- package/tmp.json +0 -0
- package/tsconfig.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -20
package/src/consensus/index.ts
DELETED
|
@@ -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
|
-
}
|