@gotza02/seq-thinking 1.1.4 → 1.1.6

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 (49) hide show
  1. package/README.md +31 -27
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +1 -1
  4. package/dist/mcp-server.js +1 -1
  5. package/package.json +8 -2
  6. package/agents_test.log +0 -15
  7. package/data/agents/1770106504306-dljh9ef.json +0 -68
  8. package/data/agents/1770106504310-4oarrst.json +0 -58
  9. package/data/agents/1770106540588-pvitt55.json +0 -68
  10. package/data/agents/1770106540595-z2ya871.json +0 -58
  11. package/data/agents/1770106710890-0e2naq1.json +0 -68
  12. package/data/agents/1770106710893-r076yxx.json +0 -58
  13. package/data/agents/1770109212161-4ybd0i7.json +0 -68
  14. package/data/agents/1770109212166-gkhya8h.json +0 -58
  15. package/data/agents/1770117726716-lrnm415.json +0 -68
  16. package/data/agents/1770117726719-w6hsf3v.json +0 -58
  17. package/data/sessions/1770100622009-5afiuyv.json +0 -499
  18. package/data/sessions/1770106504312-75zk750.json +0 -107
  19. package/data/sessions/1770106540597-z8e8soo.json +0 -150
  20. package/data/sessions/1770106710894-0kxgy5x.json +0 -150
  21. package/data/sessions/1770109212169-zpddeb9.json +0 -150
  22. package/data/sessions/1770117726720-frcwj99.json +0 -150
  23. package/real_world_test.log +0 -200
  24. package/real_world_test_dynamic.log +0 -184
  25. package/real_world_test_real.log +0 -184
  26. package/src/__tests__/agents.test.ts +0 -858
  27. package/src/__tests__/mcp-server.test.ts +0 -380
  28. package/src/__tests__/sequential-thinking.test.ts +0 -687
  29. package/src/__tests__/swarm-coordinator.test.ts +0 -903
  30. package/src/__tests__/types.test.ts +0 -839
  31. package/src/__tests__/utils.test.ts +0 -322
  32. package/src/agents/base-agent.ts +0 -288
  33. package/src/agents/critic-agent.ts +0 -582
  34. package/src/agents/index.ts +0 -11
  35. package/src/agents/meta-reasoning-agent.ts +0 -314
  36. package/src/agents/reasoner-agent.ts +0 -312
  37. package/src/agents/synthesizer-agent.ts +0 -641
  38. package/src/index.ts +0 -118
  39. package/src/mcp-server.ts +0 -391
  40. package/src/real_world_test.ts +0 -89
  41. package/src/sequential-thinking.ts +0 -614
  42. package/src/swarm-coordinator.ts +0 -772
  43. package/src/types/index.ts +0 -915
  44. package/src/utils/index.ts +0 -1004
  45. package/src/utils/llm-adapter.ts +0 -110
  46. package/src/utils/logger.ts +0 -56
  47. package/src/utils/persistence.ts +0 -109
  48. package/test_output.log +0 -0
  49. package/tsconfig.json +0 -21
@@ -1,903 +0,0 @@
1
- /**
2
- * Swarm Coordinator Test Suite
3
- * Tests for AgentRegistry, MessageBroker, TaskQueueManager, TaskAssigner,
4
- * ConsensusEngine, ConflictResolver, and SwarmCoordinator
5
- */
6
- import { test, describe, beforeEach } from 'node:test';
7
- import assert from 'node:assert';
8
- import {
9
- AgentRegistry,
10
- MessageBroker,
11
- TaskQueueManager,
12
- TaskAssigner,
13
- ConsensusEngine,
14
- ConflictResolver,
15
- SwarmCoordinator,
16
- } from '../swarm-coordinator.js';
17
- import {
18
- AgentType,
19
- MessageType,
20
- ConsensusAlgorithm,
21
- ConflictResolutionStrategy,
22
- AgentStatus,
23
- TaskStatus,
24
- ConflictStatus,
25
- type AgentConfig,
26
- type AgentCapability,
27
- type Task,
28
- type TaskResult,
29
- } from '../types/index.js';
30
- import { BaseAgent } from '../agents/base-agent.js';
31
-
32
- class MockAgent extends BaseAgent {
33
- async process(task: Task): Promise<TaskResult> {
34
- return this.createTaskResult(task.id, { processed: true }, 0.9, 100);
35
- }
36
-
37
- getType(): string {
38
- return this.config.type;
39
- }
40
-
41
- getCapabilities(): AgentCapability[] {
42
- return this.config.capabilities;
43
- }
44
- }
45
-
46
- describe('AgentRegistry', () => {
47
- let registry: AgentRegistry;
48
-
49
- beforeEach(() => {
50
- registry = new AgentRegistry();
51
- });
52
-
53
- test('should register an agent', async () => {
54
- const config = createMockAgentConfig();
55
- const agent = await registry.registerAgent(config);
56
-
57
- assert.ok(agent);
58
- assert.ok(agent.config.id);
59
- assert.strictEqual(agent.config.name, config.name);
60
- assert.strictEqual(agent.config.type, config.type);
61
- assert.strictEqual(agent.status, AgentStatus.IDLE);
62
- });
63
-
64
- test('should unregister an agent', async () => {
65
- const config = createMockAgentConfig();
66
- const agent = await registry.registerAgent(config);
67
-
68
- const result = await registry.unregisterAgent(agent.config.id);
69
-
70
- assert.strictEqual(result, true);
71
- assert.strictEqual(registry.getAgent(agent.config.id), undefined);
72
- });
73
-
74
- test('should return false when unregistering non-existent agent', async () => {
75
- const result = await registry.unregisterAgent('non-existent-id');
76
- assert.strictEqual(result, false);
77
- });
78
-
79
- test('should get agent by id', async () => {
80
- const config = createMockAgentConfig();
81
- const registered = await registry.registerAgent(config);
82
-
83
- const retrieved = registry.getAgent(registered.config.id);
84
-
85
- assert.ok(retrieved);
86
- assert.strictEqual(retrieved!.config.id, registered.config.id);
87
- });
88
-
89
- test('should get agents by type', async () => {
90
- const reasonerConfig1 = createMockAgentConfig({ type: AgentType.REASONER });
91
- const reasonerConfig2 = createMockAgentConfig({ type: AgentType.REASONER });
92
- const criticConfig = createMockAgentConfig({ type: AgentType.CRITIC });
93
-
94
- await registry.registerAgent(reasonerConfig1);
95
- await registry.registerAgent(criticConfig);
96
- await registry.registerAgent(reasonerConfig2);
97
-
98
- const reasoners = registry.getAgentsByType(AgentType.REASONER);
99
- const critics = registry.getAgentsByType(AgentType.CRITIC);
100
-
101
- assert.strictEqual(reasoners.length, 2);
102
- assert.strictEqual(critics.length, 1);
103
- });
104
-
105
- test('should get agents by capability', async () => {
106
- const config1 = createMockAgentConfig({
107
- capabilities: [createMockCapability('reasoning')],
108
- });
109
- const config2 = createMockAgentConfig({
110
- capabilities: [createMockCapability('analysis')],
111
- });
112
-
113
- const agent1 = await registry.registerAgent(config1);
114
- await registry.registerAgent(config2);
115
-
116
- const reasoningAgents = registry.getAgentsByCapability('reasoning');
117
-
118
- assert.strictEqual(reasoningAgents.length, 1);
119
- assert.strictEqual(reasoningAgents[0].config.id, agent1.config.id);
120
- });
121
-
122
- test('should get all agents', async () => {
123
- await registry.registerAgent(createMockAgentConfig());
124
- await registry.registerAgent(createMockAgentConfig());
125
- await registry.registerAgent(createMockAgentConfig());
126
-
127
- const allAgents = registry.getAllAgents();
128
-
129
- assert.strictEqual(allAgents.length, 3);
130
- });
131
-
132
- test('should get available agents', async () => {
133
- const config1 = createMockAgentConfig();
134
- const config2 = createMockAgentConfig();
135
-
136
- const agent1 = await registry.registerAgent(config1);
137
- const agent2 = await registry.registerAgent(config2);
138
-
139
- registry.updateAgentStatus(agent1.config.id, AgentStatus.BUSY);
140
-
141
- const available = registry.getAvailableAgents();
142
-
143
- assert.strictEqual(available.length, 1);
144
- assert.strictEqual(available[0].config.id, agent2.config.id);
145
- });
146
-
147
- test('should update agent status', async () => {
148
- const config = createMockAgentConfig();
149
- const agent = await registry.registerAgent(config);
150
-
151
- registry.updateAgentStatus(agent.config.id, AgentStatus.BUSY);
152
-
153
- const updated = registry.getAgent(agent.config.id);
154
- assert.strictEqual(updated!.status, 'busy');
155
- });
156
-
157
- test('should record agent performance', async () => {
158
- const config = createMockAgentConfig();
159
- const agent = await registry.registerAgent(config);
160
-
161
- registry.recordPerformance(agent.config.id, 'task-001', 0.9, 1000);
162
-
163
- const updated = registry.getAgent(agent.config.id);
164
- assert.strictEqual(updated!.performanceHistory.length, 1);
165
- assert.strictEqual(updated!.performanceHistory[0].taskId, 'task-001');
166
- assert.strictEqual(updated!.performanceHistory[0].quality, 0.9);
167
- assert.strictEqual(updated!.performanceHistory[0].timeMs, 1000);
168
- });
169
-
170
- test('should handle multiple performance records', async () => {
171
- const config = createMockAgentConfig();
172
- const agent = await registry.registerAgent(config);
173
-
174
- registry.recordPerformance(agent.config.id, 'task-001', 0.8, 500);
175
- registry.recordPerformance(agent.config.id, 'task-002', 0.9, 600);
176
- registry.recordPerformance(agent.config.id, 'task-003', 0.85, 550);
177
-
178
- const updated = registry.getAgent(agent.config.id);
179
- assert.strictEqual(updated!.performanceHistory.length, 3);
180
- });
181
- });
182
-
183
- describe('MessageBroker', () => {
184
- let broker: MessageBroker;
185
-
186
- beforeEach(() => {
187
- broker = new MessageBroker();
188
- });
189
-
190
- test('should send message', () => {
191
- const message = broker.sendMessage({
192
- type: MessageType.TASK_ASSIGNMENT,
193
- senderId: 'agent-001',
194
- recipientId: 'agent-002',
195
- payload: { taskId: 'task-001' },
196
- context: {},
197
- metadata: {
198
- priority: 1,
199
- ttl: 30000,
200
- retryCount: 0,
201
- },
202
- });
203
-
204
- assert.ok(message.id);
205
- assert.ok(message.timestamp);
206
- assert.strictEqual(message.type, MessageType.TASK_ASSIGNMENT);
207
- });
208
-
209
- test('should broadcast message', () => {
210
- const message = broker.sendMessage({
211
- type: MessageType.BROADCAST,
212
- senderId: 'agent-001',
213
- recipientId: null,
214
- payload: { announcement: 'Hello' },
215
- context: {},
216
- metadata: {
217
- priority: 0,
218
- ttl: 60000,
219
- retryCount: 0,
220
- },
221
- });
222
-
223
- // Should not throw
224
- broker.broadcast(message);
225
- assert.ok(message);
226
- });
227
-
228
- test('should deliver to recipient', () => {
229
- const message = broker.sendMessage({
230
- type: MessageType.DIRECT,
231
- senderId: 'agent-001',
232
- recipientId: 'agent-002',
233
- payload: { data: 'test' },
234
- context: {},
235
- metadata: {
236
- priority: 1,
237
- ttl: 30000,
238
- retryCount: 0,
239
- },
240
- });
241
-
242
- // Should not throw
243
- broker.deliverToRecipient(message);
244
- assert.ok(message);
245
- });
246
-
247
- test('should subscribe to messages', () => {
248
- const messages: unknown[] = [];
249
- const unsubscribe = broker.subscribe('agent-001', (msg) => {
250
- messages.push(msg);
251
- });
252
-
253
- assert.ok(typeof unsubscribe === 'function');
254
-
255
- broker.sendMessage({
256
- type: MessageType.BROADCAST,
257
- senderId: 'agent-002',
258
- recipientId: null,
259
- payload: {},
260
- context: {},
261
- metadata: { priority: 0, ttl: 30000, retryCount: 0 },
262
- });
263
-
264
- // Cleanup
265
- unsubscribe();
266
- });
267
-
268
- test('should unsubscribe from messages', () => {
269
- const messages: unknown[] = [];
270
- const unsubscribe = broker.subscribe('agent-001', (msg) => {
271
- messages.push(msg);
272
- });
273
-
274
- unsubscribe();
275
-
276
- // After unsubscribe, should not receive messages
277
- // This is implementation-dependent, but unsubscribe should work
278
- assert.ok(true);
279
- });
280
- });
281
-
282
- describe('TaskQueueManager', () => {
283
- let manager: TaskQueueManager;
284
-
285
- beforeEach(() => {
286
- manager = new TaskQueueManager();
287
- });
288
-
289
- test('should create task', () => {
290
- const task = manager.createTask(
291
- 'reasoning',
292
- 'Perform reasoning',
293
- { problem: 'Solve X' },
294
- { requiredCapabilities: ['reasoning'], minConfidence: 0.7, maxTimeMs: 5000 }
295
- );
296
-
297
- assert.ok(task.id);
298
- assert.strictEqual(task.type, 'reasoning');
299
- assert.strictEqual(task.status, TaskStatus.PENDING);
300
- assert.strictEqual(task.assignedAgentId, null);
301
- });
302
-
303
- test('should assign task to agent', () => {
304
- const task = manager.createTask(
305
- 'analysis',
306
- 'Analyze data',
307
- { data: [] },
308
- { requiredCapabilities: ['analysis'], minConfidence: 0.8, maxTimeMs: 10000 }
309
- );
310
-
311
- const assigned = manager.assignTask(task.id, 'agent-001');
312
-
313
- assert.ok(assigned);
314
- assert.strictEqual(assigned!.assignedAgentId, 'agent-001');
315
- assert.strictEqual(assigned!.status, TaskStatus.ASSIGNED);
316
- });
317
-
318
- test('should return null when assigning non-existent task', () => {
319
- const result = manager.assignTask('non-existent-id', 'agent-001');
320
- assert.strictEqual(result, null);
321
- });
322
-
323
- test('should start task', () => {
324
- const task = manager.createTask(
325
- 'processing',
326
- 'Process data',
327
- {},
328
- { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 }
329
- );
330
- manager.assignTask(task.id, 'agent-001');
331
-
332
- const started = manager.startTask(task.id);
333
-
334
- assert.ok(started);
335
- assert.strictEqual(started!.status, TaskStatus.IN_PROGRESS);
336
- });
337
-
338
- test('should complete task', () => {
339
- const task = manager.createTask(
340
- 'computation',
341
- 'Compute result',
342
- {},
343
- { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 }
344
- );
345
- manager.assignTask(task.id, 'agent-001');
346
- manager.startTask(task.id);
347
-
348
- const result: TaskResult = {
349
- taskId: task.id,
350
- agentId: 'agent-001',
351
- success: true,
352
- output: { result: 42 },
353
- confidence: 0.9,
354
- processingTimeMs: 1000,
355
- metadata: {
356
- tokensUsed: 50,
357
- reasoningSteps: 5,
358
- intermediateResults: [],
359
- },
360
- };
361
-
362
- manager.completeTask(task.id, result);
363
-
364
- const completed = manager.getTask(task.id);
365
- assert.strictEqual(completed!.status, 'completed');
366
- assert.strictEqual(completed!.result, result);
367
- });
368
-
369
- test('should fail task', () => {
370
- const task = manager.createTask(
371
- 'risky-task',
372
- 'Task that might fail',
373
- {},
374
- { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 }
375
- );
376
- manager.assignTask(task.id, 'agent-001');
377
- manager.startTask(task.id);
378
-
379
- manager.failTask(task.id, 'Timeout error');
380
-
381
- const failed = manager.getTask(task.id);
382
- assert.strictEqual(failed!.status, 'failed');
383
- });
384
-
385
- test('should get tasks by status', () => {
386
- const task1 = manager.createTask('type1', 'Task 1', {}, { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 });
387
- const task2 = manager.createTask('type2', 'Task 2', {}, { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 });
388
- const task3 = manager.createTask('type3', 'Task 3', {}, { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 });
389
-
390
- manager.assignTask(task2.id, 'agent-001');
391
- manager.assignTask(task3.id, 'agent-002');
392
- manager.startTask(task3.id);
393
-
394
- const pending = manager.getTasksByStatus(TaskStatus.PENDING);
395
- const assigned = manager.getTasksByStatus(TaskStatus.ASSIGNED);
396
- const inProgress = manager.getTasksByStatus(TaskStatus.IN_PROGRESS);
397
-
398
- assert.strictEqual(pending.length, 1);
399
- assert.strictEqual(assigned.length, 1);
400
- assert.strictEqual(inProgress.length, 1);
401
- });
402
-
403
- test('should get pending tasks', () => {
404
- manager.createTask('type1', 'Task 1', {}, { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 });
405
- manager.createTask('type2', 'Task 2', {}, { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 });
406
-
407
- const pending = manager.getPendingTasks();
408
- assert.strictEqual(pending.length, 2);
409
- });
410
-
411
- test('should get in progress tasks', () => {
412
- const task = manager.createTask('type', 'Task', {}, { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 });
413
- manager.assignTask(task.id, 'agent-001');
414
- manager.startTask(task.id);
415
-
416
- const inProgress = manager.getInProgressTasks();
417
- assert.strictEqual(inProgress.length, 1);
418
- });
419
-
420
- test('should get completed tasks', () => {
421
- const task = manager.createTask('type', 'Task', {}, { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 });
422
- manager.assignTask(task.id, 'agent-001');
423
- manager.startTask(task.id);
424
- manager.completeTask(task.id, createMockTaskResult(task.id, 'agent-001'));
425
-
426
- const completed = manager.getCompletedTasks();
427
- assert.strictEqual(completed.length, 1);
428
- });
429
- });
430
-
431
- describe('TaskAssigner', () => {
432
- let registry: AgentRegistry;
433
- let assigner: TaskAssigner;
434
-
435
- beforeEach(() => {
436
- registry = new AgentRegistry();
437
- assigner = new TaskAssigner(registry);
438
- });
439
-
440
- test('should assign task using round robin', async () => {
441
- await registry.registerAgent(createMockAgentConfig());
442
- await registry.registerAgent(createMockAgentConfig());
443
-
444
- const task = createMockTask();
445
-
446
- const agent1 = assigner.assignTask(task, 'round_robin');
447
- const agent2 = assigner.assignTask(task, 'round_robin');
448
-
449
- assert.ok(agent1);
450
- assert.ok(agent2);
451
- assert.notStrictEqual(agent1!.config.id, agent2!.config.id);
452
- });
453
-
454
- test('should assign task using capability based strategy', async () => {
455
- await registry.registerAgent(createMockAgentConfig({
456
- capabilities: [createMockCapability('required-skill')],
457
- }));
458
- await registry.registerAgent(createMockAgentConfig({
459
- capabilities: [createMockCapability('other-skill')],
460
- }));
461
-
462
- const task = createMockTask({ requiredCapabilities: ['required-skill'] });
463
-
464
- const agent = assigner.assignTask(task, 'capability_based');
465
-
466
- assert.ok(agent);
467
- assert.ok(agent!.config.capabilities.some((c: AgentCapability) => c.name === 'required-skill'));
468
- });
469
-
470
- test('should assign task using load balanced strategy', async () => {
471
- const agent1 = await registry.registerAgent(createMockAgentConfig());
472
- const agent2 = await registry.registerAgent(createMockAgentConfig());
473
-
474
- registry.updateAgentStatus(agent1.config.id, AgentStatus.BUSY);
475
-
476
- const task = createMockTask();
477
- const assigned = assigner.assignTask(task, 'load_balanced');
478
-
479
- assert.ok(assigned);
480
- assert.strictEqual(assigned!.config.id, agent2.config.id);
481
- });
482
-
483
- test('should return null when no suitable agent', () => {
484
- const task = createMockTask({ requiredCapabilities: ['non-existent-capability'] });
485
-
486
- const agent = assigner.assignTask(task, 'capability_based');
487
-
488
- assert.strictEqual(agent, null);
489
- });
490
-
491
- test('should handle empty registry', () => {
492
- const task = createMockTask();
493
- const agent = assigner.assignTask(task, 'round_robin');
494
- assert.strictEqual(agent, null);
495
- });
496
- });
497
-
498
- describe('ConsensusEngine', () => {
499
- let engine: ConsensusEngine;
500
-
501
- beforeEach(() => {
502
- engine = new ConsensusEngine();
503
- });
504
-
505
- test('should reach consensus with majority vote', async () => {
506
- const result = await engine.reachConsensus(
507
- 'prop-001',
508
- ConsensusAlgorithm.MAJORITY_VOTE,
509
- [
510
- { agentId: 'a1', proposalId: 'p1', vote: 'yes', confidence: 1.0, reasoning: '', timestamp: new Date() },
511
- { agentId: 'a2', proposalId: 'p1', vote: 'yes', confidence: 1.0, reasoning: '', timestamp: new Date() },
512
- { agentId: 'a3', proposalId: 'p1', vote: 'no', confidence: 1.0, reasoning: '', timestamp: new Date() }
513
- ],
514
- 0.5,
515
- 1
516
- );
517
-
518
- assert.ok(result);
519
- assert.ok(result.proposalId);
520
- assert.ok(result.winningOption);
521
- assert.ok(typeof result.confidence === 'number');
522
- assert.ok(Array.isArray(result.votes));
523
- assert.ok(typeof result.agreementRatio === 'number');
524
- });
525
-
526
- test('should reach consensus with weighted vote', async () => {
527
- const result = await engine.reachConsensus(
528
- 'prop-002',
529
- ConsensusAlgorithm.WEIGHTED_VOTE,
530
- [
531
- { agentId: 'a1', proposalId: 'p2', vote: 'yes', confidence: 0.9, reasoning: '', timestamp: new Date() },
532
- { agentId: 'a2', proposalId: 'p2', vote: 'no', confidence: 0.4, reasoning: '', timestamp: new Date() }
533
- ],
534
- 0.6,
535
- 1
536
- );
537
-
538
- assert.ok(result);
539
- assert.strictEqual(result.algorithm, ConsensusAlgorithm.WEIGHTED_VOTE);
540
- });
541
-
542
- test('should handle empty agent list', async () => {
543
- const result = await engine.reachConsensus(
544
- 'prop-003',
545
- ConsensusAlgorithm.MAJORITY_VOTE,
546
- [],
547
- 0.5,
548
- 1
549
- );
550
-
551
- assert.ok(result);
552
- });
553
-
554
- test('should handle single agent', async () => {
555
- const result = await engine.reachConsensus(
556
- 'prop-004',
557
- ConsensusAlgorithm.MAJORITY_VOTE,
558
- [{ agentId: 'a1', proposalId: 'p4', vote: 'a', confidence: 1.0, reasoning: '', timestamp: new Date() }],
559
- 0.5,
560
- 1
561
- );
562
-
563
- assert.ok(result);
564
- assert.strictEqual(result.agreementRatio, 1);
565
- });
566
-
567
- test('should include dissenting opinions', async () => {
568
- const result = await engine.reachConsensus(
569
- 'prop-005',
570
- ConsensusAlgorithm.MAJORITY_VOTE,
571
- [
572
- { agentId: 'a1', proposalId: 'p5', vote: 'a', confidence: 1.0, reasoning: '', timestamp: new Date() },
573
- { agentId: 'a2', proposalId: 'p5', vote: 'b', confidence: 1.0, reasoning: 'disagree', timestamp: new Date() }
574
- ],
575
- 0.5,
576
- 1
577
- );
578
-
579
- assert.ok(Array.isArray(result.dissentingOpinions));
580
- });
581
- });
582
-
583
- describe('ConflictResolver', () => {
584
- let resolver: ConflictResolver;
585
-
586
- beforeEach(() => {
587
- resolver = new ConflictResolver();
588
- });
589
-
590
- test('should identify conflict', () => {
591
- const conflict = resolver.identifyConflict(
592
- 'disagreement',
593
- 'Agents disagree on approach',
594
- [
595
- { agentId: 'agent-001', position: 'approach-a', evidence: ['evidence-1'], confidence: 0.8 },
596
- { agentId: 'agent-002', position: 'approach-b', evidence: ['evidence-2'], confidence: 0.75 },
597
- ]
598
- );
599
-
600
- assert.ok(conflict.id);
601
- assert.strictEqual(conflict.type, 'disagreement');
602
- assert.strictEqual(conflict.status, ConflictStatus.DETECTED);
603
- assert.strictEqual(conflict.involvedAgents.length, 2);
604
- });
605
-
606
- test('should resolve conflict with voting', () => {
607
- const conflict = resolver.identifyConflict(
608
- 'resource-contention',
609
- 'Resource dispute',
610
- [
611
- { agentId: 'agent-001', position: 'claim-a', evidence: [], confidence: 0.7 },
612
- { agentId: 'agent-002', position: 'claim-b', evidence: [], confidence: 0.6 },
613
- ]
614
- );
615
-
616
- const { conflict: resolved, resolution } = resolver.resolveConflict(
617
- conflict.id,
618
- ConflictResolutionStrategy.VOTING,
619
- 'test-resolver'
620
- );
621
-
622
- assert.strictEqual(resolved.status, ConflictStatus.RESOLVED);
623
- assert.ok(resolution);
624
- assert.ok(resolved.metadata.resolvedAt);
625
- assert.strictEqual(resolved.metadata.resolutionStrategy, ConflictResolutionStrategy.VOTING);
626
- });
627
-
628
- test('should resolve conflict with compromise', () => {
629
- const conflict = resolver.identifyConflict(
630
- 'priority-dispute',
631
- 'Task priority disagreement',
632
- [
633
- { agentId: 'agent-001', position: 'high-priority', evidence: [], confidence: 0.9 },
634
- { agentId: 'agent-002', position: 'low-priority', evidence: [], confidence: 0.8 },
635
- ]
636
- );
637
-
638
- const { conflict: resolved } = resolver.resolveConflict(
639
- conflict.id,
640
- ConflictResolutionStrategy.COMPROMISE,
641
- 'test-resolver'
642
- );
643
-
644
- assert.strictEqual(resolved.status, ConflictStatus.RESOLVED);
645
- });
646
-
647
- test('should get conflict by id', () => {
648
- const conflict = resolver.identifyConflict('test', 'Test conflict', []);
649
-
650
- const retrieved = resolver.getConflict(conflict.id);
651
-
652
- assert.ok(retrieved);
653
- assert.strictEqual(retrieved!.id, conflict.id);
654
- });
655
-
656
- test('should get all conflicts', () => {
657
- resolver.identifyConflict('type1', 'Conflict 1', []);
658
- resolver.identifyConflict('type2', 'Conflict 2', []);
659
- resolver.identifyConflict('type3', 'Conflict 3', []);
660
-
661
- const allConflicts = resolver.getAllConflicts();
662
-
663
- assert.strictEqual(allConflicts.length, 3);
664
- });
665
-
666
- test('should handle resolving non-existent conflict', () => {
667
- assert.throws(() => {
668
- resolver.resolveConflict('non-existent-id', ConflictResolutionStrategy.VOTING, 'test-resolver');
669
- });
670
- });
671
- });
672
-
673
- describe('SwarmCoordinator', () => {
674
- let coordinator: SwarmCoordinator;
675
-
676
- beforeEach(() => {
677
- coordinator = new SwarmCoordinator();
678
- });
679
-
680
- test('should register agent', async () => {
681
- const config = createMockAgentConfig();
682
- const agent = await coordinator.registerAgent(config);
683
-
684
- assert.ok(agent);
685
- assert.strictEqual(agent.config.name, config.name);
686
- });
687
-
688
- test('should get all agents', async () => {
689
- await coordinator.registerAgent(createMockAgentConfig());
690
- await coordinator.registerAgent(createMockAgentConfig());
691
-
692
- const agents = coordinator.getAllAgents();
693
-
694
- assert.strictEqual(agents.length, 2);
695
- });
696
-
697
- test('should unregister agent', async () => {
698
- const agent = await coordinator.registerAgent(createMockAgentConfig());
699
-
700
- const result = await coordinator.unregisterAgent(agent.config.id);
701
-
702
- assert.strictEqual(result, true);
703
- assert.strictEqual(coordinator.getAllAgents().length, 0);
704
- });
705
-
706
- test('should submit task', async () => {
707
- await coordinator.registerAgent(createMockAgentConfig());
708
-
709
- const { task, assignedAgent } = coordinator.submitTask(
710
- 'reasoning',
711
- 'Perform reasoning',
712
- { problem: 'Solve X' },
713
- { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 }
714
- );
715
-
716
- assert.ok(task);
717
- assert.ok(task.id);
718
- });
719
-
720
- test('should execute task', async () => {
721
- const config = createMockAgentConfig();
722
- const agent = new MockAgent(config);
723
- await coordinator.registerAgent(agent);
724
-
725
- const { task } = coordinator.submitTask(
726
- 'computation',
727
- 'Compute something',
728
- { input: 5 },
729
- { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 }
730
- );
731
-
732
- const result = await coordinator.executeTask(task.id);
733
-
734
- assert.ok(result);
735
- assert.strictEqual(result.taskId, task.id);
736
- assert.strictEqual(result.success, true);
737
- });
738
-
739
- test('should reach consensus', async () => {
740
- const config1 = createMockAgentConfig();
741
- const config2 = createMockAgentConfig();
742
- await coordinator.registerAgent(new MockAgent(config1));
743
- await coordinator.registerAgent(new MockAgent(config2));
744
-
745
- const result = await coordinator.reachConsensus(
746
- 'Decision?',
747
- ['option-a', 'option-b'],
748
- ConsensusAlgorithm.MAJORITY_VOTE
749
- );
750
-
751
- assert.ok(result);
752
- assert.ok(result.winningOption);
753
- });
754
-
755
- test('should identify conflict', () => {
756
- const conflict = coordinator.identifyConflict(
757
- 'disagreement',
758
- 'Test conflict',
759
- [
760
- { agentId: 'agent-001', position: 'a', evidence: [], confidence: 0.8 },
761
- { agentId: 'agent-002', position: 'b', evidence: [], confidence: 0.7 },
762
- ]
763
- );
764
-
765
- assert.ok(conflict);
766
- assert.strictEqual(conflict.type, 'disagreement');
767
- });
768
-
769
- test('should resolve conflict', () => {
770
- const conflict = coordinator.identifyConflict(
771
- 'test-conflict',
772
- 'Test',
773
- [{ agentId: 'agent-001', position: 'x', evidence: [], confidence: 0.5 }]
774
- );
775
-
776
- const { conflict: resolved, resolution } = coordinator.resolveConflict(
777
- conflict.id,
778
- ConflictResolutionStrategy.VOTING,
779
- 'test-resolver'
780
- );
781
-
782
- assert.strictEqual(resolved.status, ConflictStatus.RESOLVED);
783
- });
784
-
785
- test('should broadcast message', () => {
786
- const message = coordinator.broadcastMessage('agent-001', { announcement: 'Hello' });
787
-
788
- assert.ok(message);
789
- assert.strictEqual(message.type, MessageType.BROADCAST);
790
- });
791
-
792
- test('should send direct message', () => {
793
- const message = coordinator.sendDirectMessage('agent-001', 'agent-002', { data: 'test' });
794
-
795
- assert.ok(message);
796
- assert.strictEqual(message.type, MessageType.DIRECT);
797
- assert.strictEqual(message.recipientId, 'agent-002');
798
- });
799
-
800
- test('should get swarm stats', async () => {
801
- await coordinator.registerAgent(createMockAgentConfig());
802
- await coordinator.registerAgent(createMockAgentConfig());
803
-
804
- const stats = coordinator.getSwarmStats();
805
-
806
- assert.ok(typeof stats.totalAgents === 'number');
807
- assert.ok(typeof stats.availableAgents === 'number');
808
- assert.ok(typeof stats.busyAgents === 'number');
809
- assert.ok(typeof stats.pendingTasks === 'number');
810
- assert.ok(typeof stats.inProgressTasks === 'number');
811
- assert.ok(typeof stats.completedTasks === 'number');
812
- assert.ok(typeof stats.activeConflicts === 'number');
813
-
814
- assert.strictEqual(stats.totalAgents, 2);
815
- });
816
-
817
- test('should track task lifecycle', async () => {
818
- const config = createMockAgentConfig();
819
- await coordinator.registerAgent(new MockAgent(config));
820
-
821
- const { task } = coordinator.submitTask(
822
- 'test-task',
823
- 'Test task',
824
- {},
825
- { requiredCapabilities: [], minConfidence: 0.5, maxTimeMs: 5000 }
826
- );
827
-
828
- const initialStats = coordinator.getSwarmStats();
829
-
830
- await coordinator.executeTask(task.id);
831
-
832
- const finalStats = coordinator.getSwarmStats();
833
-
834
- assert.ok(finalStats.completedTasks > initialStats.completedTasks || finalStats.completedTasks === 1);
835
- });
836
- });
837
-
838
- // Helper functions
839
- function createMockAgentConfig(overrides: Partial<AgentConfig> = {}): AgentConfig {
840
- return {
841
- id: `agent-${Math.random().toString(36).substring(2, 9)}`,
842
- name: 'Test Agent',
843
- type: AgentType.REASONER,
844
- capabilities: [],
845
- maxConcurrentTasks: 3,
846
- confidenceThreshold: 0.7,
847
- metadata: {
848
- createdAt: new Date(),
849
- version: '1.0.0',
850
- config: {},
851
- },
852
- ...overrides,
853
- };
854
- }
855
-
856
- function createMockCapability(name: string): AgentCapability {
857
- return {
858
- name,
859
- description: `Capability: ${name}`,
860
- confidence: 0.8,
861
- performanceMetrics: {
862
- tasksCompleted: 10,
863
- averageQuality: 0.85,
864
- averageTimeMs: 1000,
865
- },
866
- };
867
- }
868
-
869
- function createMockTask(overrides: Partial<Task['requirements']> = {}): Task {
870
- return {
871
- id: `task-${Math.random().toString(36).substring(2, 9)}`,
872
- type: 'test',
873
- description: 'Test task',
874
- input: {},
875
- context: { dependencies: [] },
876
- requirements: {
877
- requiredCapabilities: [],
878
- minConfidence: 0.5,
879
- maxTimeMs: 5000,
880
- ...overrides,
881
- },
882
- assignedAgentId: null,
883
- status: TaskStatus.PENDING,
884
- result: null,
885
- metadata: { createdAt: new Date() },
886
- };
887
- }
888
-
889
- function createMockTaskResult(taskId: string, agentId: string): TaskResult {
890
- return {
891
- taskId,
892
- agentId,
893
- success: true,
894
- output: {},
895
- confidence: 0.9,
896
- processingTimeMs: 1000,
897
- metadata: {
898
- tokensUsed: 50,
899
- reasoningSteps: 5,
900
- intermediateResults: [],
901
- },
902
- };
903
- }