opencode-swarm-plugin 0.1.0

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.
@@ -0,0 +1,1104 @@
1
+ /**
2
+ * Learning Module Integration Tests
3
+ *
4
+ * Tests for confidence decay, feedback scoring, outcome tracking,
5
+ * anti-patterns, pattern maturity, and swarm tool integrations.
6
+ *
7
+ * These tests don't require external services - they test the learning
8
+ * algorithms and their integration with swarm tools.
9
+ */
10
+ import { describe, it, expect, beforeEach } from "vitest";
11
+
12
+ // Learning module
13
+ import {
14
+ calculateDecayedValue,
15
+ calculateCriterionWeight,
16
+ scoreImplicitFeedback,
17
+ outcomeToFeedback,
18
+ applyWeights,
19
+ shouldDeprecateCriterion,
20
+ InMemoryFeedbackStorage,
21
+ DEFAULT_LEARNING_CONFIG,
22
+ type FeedbackEvent,
23
+ type OutcomeSignals,
24
+ type CriterionWeight,
25
+ } from "./learning";
26
+
27
+ // Anti-patterns module
28
+ import {
29
+ shouldInvertPattern,
30
+ invertToAntiPattern,
31
+ recordPatternObservation,
32
+ extractPatternsFromDescription,
33
+ createPattern,
34
+ formatAntiPatternsForPrompt,
35
+ formatSuccessfulPatternsForPrompt,
36
+ InMemoryPatternStorage,
37
+ DEFAULT_ANTI_PATTERN_CONFIG,
38
+ type DecompositionPattern,
39
+ } from "./anti-patterns";
40
+
41
+ // Pattern maturity module
42
+ import {
43
+ calculateMaturityState,
44
+ calculateDecayedCounts,
45
+ createPatternMaturity,
46
+ updatePatternMaturity,
47
+ promotePattern,
48
+ deprecatePattern,
49
+ formatMaturityForPrompt,
50
+ getMaturityMultiplier,
51
+ InMemoryMaturityStorage,
52
+ DEFAULT_MATURITY_CONFIG,
53
+ type PatternMaturity,
54
+ type MaturityFeedback,
55
+ } from "./pattern-maturity";
56
+
57
+ // Swarm tools
58
+ import {
59
+ swarm_decompose,
60
+ swarm_validate_decomposition,
61
+ swarm_record_outcome,
62
+ detectInstructionConflicts,
63
+ } from "./swarm";
64
+
65
+ // ============================================================================
66
+ // Test Helpers
67
+ // ============================================================================
68
+
69
+ const mockContext = {
70
+ sessionID: `test-learning-${Date.now()}`,
71
+ messageID: `test-message-${Date.now()}`,
72
+ agent: "test-agent",
73
+ abort: new AbortController().signal,
74
+ };
75
+
76
+ /**
77
+ * Create a feedback event for testing
78
+ */
79
+ function createFeedbackEvent(
80
+ criterion: string,
81
+ type: "helpful" | "harmful" | "neutral",
82
+ daysAgo: number = 0,
83
+ ): FeedbackEvent {
84
+ const timestamp = new Date(
85
+ Date.now() - daysAgo * 24 * 60 * 60 * 1000,
86
+ ).toISOString();
87
+ return {
88
+ id: `test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
89
+ criterion,
90
+ type,
91
+ timestamp,
92
+ raw_value: 1,
93
+ };
94
+ }
95
+
96
+ /**
97
+ * Create outcome signals for testing
98
+ */
99
+ function createOutcomeSignals(
100
+ overrides: Partial<OutcomeSignals> = {},
101
+ ): OutcomeSignals {
102
+ return {
103
+ bead_id: `test-bead-${Date.now()}`,
104
+ duration_ms: 60000, // 1 minute
105
+ error_count: 0,
106
+ retry_count: 0,
107
+ success: true,
108
+ files_touched: ["src/test.ts"],
109
+ timestamp: new Date().toISOString(),
110
+ ...overrides,
111
+ };
112
+ }
113
+
114
+ // ============================================================================
115
+ // Confidence Decay Tests
116
+ // ============================================================================
117
+
118
+ describe("Confidence Decay", () => {
119
+ describe("calculateDecayedValue", () => {
120
+ it("returns 1.0 for current timestamp", () => {
121
+ const now = new Date();
122
+ const value = calculateDecayedValue(now.toISOString(), now);
123
+ expect(value).toBeCloseTo(1.0, 5);
124
+ });
125
+
126
+ it("returns ~0.5 after one half-life", () => {
127
+ const now = new Date();
128
+ const halfLifeDays = 90;
129
+ const pastDate = new Date(
130
+ now.getTime() - halfLifeDays * 24 * 60 * 60 * 1000,
131
+ );
132
+ const value = calculateDecayedValue(
133
+ pastDate.toISOString(),
134
+ now,
135
+ halfLifeDays,
136
+ );
137
+ expect(value).toBeCloseTo(0.5, 1);
138
+ });
139
+
140
+ it("returns ~0.25 after two half-lives", () => {
141
+ const now = new Date();
142
+ const halfLifeDays = 90;
143
+ const pastDate = new Date(
144
+ now.getTime() - 2 * halfLifeDays * 24 * 60 * 60 * 1000,
145
+ );
146
+ const value = calculateDecayedValue(
147
+ pastDate.toISOString(),
148
+ now,
149
+ halfLifeDays,
150
+ );
151
+ expect(value).toBeCloseTo(0.25, 1);
152
+ });
153
+
154
+ it("handles future timestamps gracefully", () => {
155
+ const now = new Date();
156
+ const futureDate = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000);
157
+ const value = calculateDecayedValue(futureDate.toISOString(), now);
158
+ expect(value).toBe(1.0); // Max 0 age = no decay
159
+ });
160
+ });
161
+
162
+ describe("calculateCriterionWeight", () => {
163
+ it("returns weight 1.0 for no feedback", () => {
164
+ const weight = calculateCriterionWeight([]);
165
+ expect(weight.weight).toBe(1.0);
166
+ expect(weight.helpful_count).toBe(0);
167
+ expect(weight.harmful_count).toBe(0);
168
+ });
169
+
170
+ it("returns high weight for all helpful feedback", () => {
171
+ const events = [
172
+ createFeedbackEvent("type_safe", "helpful", 0),
173
+ createFeedbackEvent("type_safe", "helpful", 1),
174
+ createFeedbackEvent("type_safe", "helpful", 2),
175
+ ];
176
+ const weight = calculateCriterionWeight(events);
177
+ expect(weight.weight).toBeGreaterThan(0.9);
178
+ expect(weight.helpful_count).toBe(3);
179
+ expect(weight.harmful_count).toBe(0);
180
+ });
181
+
182
+ it("returns lower weight for mixed feedback", () => {
183
+ const events = [
184
+ createFeedbackEvent("type_safe", "helpful", 0),
185
+ createFeedbackEvent("type_safe", "harmful", 1),
186
+ createFeedbackEvent("type_safe", "helpful", 2),
187
+ ];
188
+ const weight = calculateCriterionWeight(events);
189
+ expect(weight.weight).toBeLessThan(0.9);
190
+ expect(weight.weight).toBeGreaterThan(0.5);
191
+ });
192
+
193
+ it("applies decay to older feedback", () => {
194
+ // Recent harmful feedback should have more impact than old helpful
195
+ const events = [
196
+ createFeedbackEvent("type_safe", "helpful", 180), // 180 days ago (2 half-lives)
197
+ createFeedbackEvent("type_safe", "harmful", 0), // today
198
+ ];
199
+ const weight = calculateCriterionWeight(events);
200
+ // Harmful is recent (weight ~1), helpful is old (weight ~0.25)
201
+ // So harmful dominates
202
+ expect(weight.weight).toBeLessThan(0.5);
203
+ });
204
+
205
+ it("tracks last_validated timestamp", () => {
206
+ const events = [
207
+ createFeedbackEvent("type_safe", "helpful", 10),
208
+ createFeedbackEvent("type_safe", "helpful", 5),
209
+ createFeedbackEvent("type_safe", "helpful", 0),
210
+ ];
211
+ const weight = calculateCriterionWeight(events);
212
+ expect(weight.last_validated).toBeDefined();
213
+ // Most recent helpful event should be last_validated
214
+ const lastValidated = new Date(weight.last_validated!);
215
+ const now = new Date();
216
+ const diffDays =
217
+ (now.getTime() - lastValidated.getTime()) / (24 * 60 * 60 * 1000);
218
+ expect(diffDays).toBeLessThan(1);
219
+ });
220
+ });
221
+
222
+ describe("shouldDeprecateCriterion", () => {
223
+ it("returns false for insufficient feedback", () => {
224
+ const weight: CriterionWeight = {
225
+ criterion: "type_safe",
226
+ weight: 0.3,
227
+ helpful_count: 1,
228
+ harmful_count: 1,
229
+ half_life_days: 90,
230
+ };
231
+ expect(shouldDeprecateCriterion(weight)).toBe(false);
232
+ });
233
+
234
+ it("returns true for high harmful ratio with enough feedback", () => {
235
+ const weight: CriterionWeight = {
236
+ criterion: "type_safe",
237
+ weight: 0.3,
238
+ helpful_count: 1,
239
+ harmful_count: 4, // 80% harmful
240
+ half_life_days: 90,
241
+ };
242
+ expect(shouldDeprecateCriterion(weight)).toBe(true);
243
+ });
244
+
245
+ it("returns false for acceptable harmful ratio", () => {
246
+ const weight: CriterionWeight = {
247
+ criterion: "type_safe",
248
+ weight: 0.8,
249
+ helpful_count: 8,
250
+ harmful_count: 2, // 20% harmful
251
+ half_life_days: 90,
252
+ };
253
+ expect(shouldDeprecateCriterion(weight)).toBe(false);
254
+ });
255
+ });
256
+ });
257
+
258
+ // ============================================================================
259
+ // Outcome Scoring Tests
260
+ // ============================================================================
261
+
262
+ describe("Outcome Scoring", () => {
263
+ describe("scoreImplicitFeedback", () => {
264
+ it("scores fast successful completion as helpful", () => {
265
+ const signals = createOutcomeSignals({
266
+ duration_ms: 60000, // 1 minute (fast)
267
+ error_count: 0,
268
+ retry_count: 0,
269
+ success: true,
270
+ });
271
+ const scored = scoreImplicitFeedback(signals);
272
+ expect(scored.type).toBe("helpful");
273
+ expect(scored.decayed_value).toBeGreaterThan(0.7);
274
+ });
275
+
276
+ it("scores slow failed completion as harmful", () => {
277
+ const signals = createOutcomeSignals({
278
+ duration_ms: 60 * 60 * 1000, // 1 hour (slow)
279
+ error_count: 5,
280
+ retry_count: 3,
281
+ success: false,
282
+ });
283
+ const scored = scoreImplicitFeedback(signals);
284
+ expect(scored.type).toBe("harmful");
285
+ expect(scored.decayed_value).toBeLessThan(0.4);
286
+ });
287
+
288
+ it("scores mixed signals as neutral", () => {
289
+ const signals = createOutcomeSignals({
290
+ duration_ms: 15 * 60 * 1000, // 15 minutes (medium)
291
+ error_count: 1,
292
+ retry_count: 1,
293
+ success: true,
294
+ });
295
+ const scored = scoreImplicitFeedback(signals);
296
+ // Could be helpful or neutral depending on exact thresholds
297
+ expect(["helpful", "neutral"]).toContain(scored.type);
298
+ });
299
+
300
+ it("includes reasoning in result", () => {
301
+ const signals = createOutcomeSignals();
302
+ const scored = scoreImplicitFeedback(signals);
303
+ expect(scored.reasoning).toBeDefined();
304
+ expect(scored.reasoning.length).toBeGreaterThan(0);
305
+ });
306
+ });
307
+
308
+ describe("outcomeToFeedback", () => {
309
+ it("converts scored outcome to feedback event", () => {
310
+ const signals = createOutcomeSignals({ bead_id: "test-bead-123" });
311
+ const scored = scoreImplicitFeedback(signals);
312
+ const feedback = outcomeToFeedback(scored, "type_safe");
313
+
314
+ expect(feedback.criterion).toBe("type_safe");
315
+ expect(feedback.type).toBe(scored.type);
316
+ expect(feedback.bead_id).toBe("test-bead-123");
317
+ expect(feedback.context).toBe(scored.reasoning);
318
+ });
319
+ });
320
+
321
+ describe("applyWeights", () => {
322
+ it("applies weights to raw scores", () => {
323
+ const criteria = {
324
+ type_safe: 0.8,
325
+ no_bugs: 0.9,
326
+ patterns: 0.7,
327
+ };
328
+ const weights: Record<string, CriterionWeight> = {
329
+ type_safe: {
330
+ criterion: "type_safe",
331
+ weight: 1.0,
332
+ helpful_count: 5,
333
+ harmful_count: 0,
334
+ half_life_days: 90,
335
+ },
336
+ no_bugs: {
337
+ criterion: "no_bugs",
338
+ weight: 0.5,
339
+ helpful_count: 2,
340
+ harmful_count: 2,
341
+ half_life_days: 90,
342
+ },
343
+ patterns: {
344
+ criterion: "patterns",
345
+ weight: 0.8,
346
+ helpful_count: 4,
347
+ harmful_count: 1,
348
+ half_life_days: 90,
349
+ },
350
+ };
351
+
352
+ const result = applyWeights(criteria, weights);
353
+
354
+ expect(result.type_safe.raw).toBe(0.8);
355
+ expect(result.type_safe.weighted).toBe(0.8); // 0.8 * 1.0
356
+ expect(result.no_bugs.weighted).toBe(0.45); // 0.9 * 0.5
357
+ expect(result.patterns.weighted).toBeCloseTo(0.56); // 0.7 * 0.8
358
+ });
359
+
360
+ it("uses default weight 1.0 for unknown criteria", () => {
361
+ const criteria = { unknown_criterion: 0.5 };
362
+ const weights: Record<string, CriterionWeight> = {};
363
+
364
+ const result = applyWeights(criteria, weights);
365
+
366
+ expect(result.unknown_criterion.weight).toBe(1.0);
367
+ expect(result.unknown_criterion.weighted).toBe(0.5);
368
+ });
369
+ });
370
+ });
371
+
372
+ // ============================================================================
373
+ // Feedback Storage Tests
374
+ // ============================================================================
375
+
376
+ describe("InMemoryFeedbackStorage", () => {
377
+ let storage: InMemoryFeedbackStorage;
378
+
379
+ beforeEach(() => {
380
+ storage = new InMemoryFeedbackStorage();
381
+ });
382
+
383
+ it("stores and retrieves feedback events", async () => {
384
+ const event = createFeedbackEvent("type_safe", "helpful");
385
+ await storage.store(event);
386
+
387
+ const all = await storage.getAll();
388
+ expect(all).toHaveLength(1);
389
+ expect(all[0].id).toBe(event.id);
390
+ });
391
+
392
+ it("retrieves events by criterion", async () => {
393
+ await storage.store(createFeedbackEvent("type_safe", "helpful"));
394
+ await storage.store(createFeedbackEvent("no_bugs", "harmful"));
395
+ await storage.store(createFeedbackEvent("type_safe", "helpful"));
396
+
397
+ const typeSafe = await storage.getByCriterion("type_safe");
398
+ expect(typeSafe).toHaveLength(2);
399
+
400
+ const noBugs = await storage.getByCriterion("no_bugs");
401
+ expect(noBugs).toHaveLength(1);
402
+ });
403
+
404
+ it("retrieves events by bead ID", async () => {
405
+ const event1 = {
406
+ ...createFeedbackEvent("type_safe", "helpful"),
407
+ bead_id: "bead-1",
408
+ };
409
+ const event2 = {
410
+ ...createFeedbackEvent("no_bugs", "harmful"),
411
+ bead_id: "bead-1",
412
+ };
413
+ const event3 = {
414
+ ...createFeedbackEvent("type_safe", "helpful"),
415
+ bead_id: "bead-2",
416
+ };
417
+
418
+ await storage.store(event1);
419
+ await storage.store(event2);
420
+ await storage.store(event3);
421
+
422
+ const bead1Events = await storage.getByBead("bead-1");
423
+ expect(bead1Events).toHaveLength(2);
424
+ });
425
+ });
426
+
427
+ // ============================================================================
428
+ // Anti-Pattern Tests
429
+ // ============================================================================
430
+
431
+ describe("Anti-Patterns", () => {
432
+ describe("shouldInvertPattern", () => {
433
+ it("returns false for patterns with insufficient observations", () => {
434
+ const pattern = createPattern("Split by file type");
435
+ pattern.success_count = 1;
436
+ pattern.failure_count = 1;
437
+
438
+ expect(shouldInvertPattern(pattern)).toBe(false);
439
+ });
440
+
441
+ it("returns true for patterns with high failure rate", () => {
442
+ const pattern = createPattern("Split by file type");
443
+ pattern.success_count = 1;
444
+ pattern.failure_count = 4; // 80% failure
445
+
446
+ expect(shouldInvertPattern(pattern)).toBe(true);
447
+ });
448
+
449
+ it("returns false for already inverted patterns", () => {
450
+ const pattern = createPattern("Split by file type");
451
+ pattern.kind = "anti_pattern";
452
+ pattern.success_count = 0;
453
+ pattern.failure_count = 10;
454
+
455
+ expect(shouldInvertPattern(pattern)).toBe(false);
456
+ });
457
+ });
458
+
459
+ describe("invertToAntiPattern", () => {
460
+ it("creates anti-pattern with AVOID prefix", () => {
461
+ const pattern = createPattern("Split by file type");
462
+ const result = invertToAntiPattern(pattern, "High failure rate");
463
+
464
+ expect(result.inverted.kind).toBe("anti_pattern");
465
+ expect(result.inverted.is_negative).toBe(true);
466
+ expect(result.inverted.content).toContain("AVOID:");
467
+ expect(result.inverted.content).toContain("Split by file type");
468
+ expect(result.inverted.reason).toBe("High failure rate");
469
+ });
470
+
471
+ it("removes existing prefixes before inverting", () => {
472
+ const pattern = createPattern("AVOID: something");
473
+ const result = invertToAntiPattern(pattern, "test");
474
+
475
+ // Should not have double AVOID
476
+ expect(result.inverted.content).not.toContain("AVOID: AVOID:");
477
+ });
478
+ });
479
+
480
+ describe("recordPatternObservation", () => {
481
+ it("increments success count on success", () => {
482
+ const pattern = createPattern("Test pattern");
483
+ const result = recordPatternObservation(pattern, true);
484
+
485
+ expect(result.pattern.success_count).toBe(1);
486
+ expect(result.pattern.failure_count).toBe(0);
487
+ expect(result.inversion).toBeUndefined();
488
+ });
489
+
490
+ it("increments failure count on failure", () => {
491
+ const pattern = createPattern("Test pattern");
492
+ const result = recordPatternObservation(pattern, false);
493
+
494
+ expect(result.pattern.success_count).toBe(0);
495
+ expect(result.pattern.failure_count).toBe(1);
496
+ });
497
+
498
+ it("triggers inversion when threshold reached", () => {
499
+ let pattern = createPattern("Bad pattern");
500
+ // Record enough failures to trigger inversion
501
+ for (let i = 0; i < 4; i++) {
502
+ const result = recordPatternObservation(pattern, false);
503
+ pattern = result.pattern;
504
+ if (result.inversion) {
505
+ expect(result.inversion.inverted.kind).toBe("anti_pattern");
506
+ return;
507
+ }
508
+ }
509
+ // Should have triggered by now
510
+ expect(pattern.failure_count).toBeGreaterThanOrEqual(3);
511
+ });
512
+
513
+ it("records bead ID in examples", () => {
514
+ const pattern = createPattern("Test pattern");
515
+ const result = recordPatternObservation(pattern, true, "bead-123");
516
+
517
+ expect(result.pattern.example_beads).toContain("bead-123");
518
+ });
519
+ });
520
+
521
+ describe("extractPatternsFromDescription", () => {
522
+ it("extracts file splitting patterns", () => {
523
+ const patterns = extractPatternsFromDescription(
524
+ "We should split by file type and handle shared types first",
525
+ );
526
+
527
+ expect(patterns).toContain("Split by file type");
528
+ expect(patterns).toContain("Handle shared types first");
529
+ });
530
+
531
+ it("extracts test organization patterns", () => {
532
+ const patterns = extractPatternsFromDescription(
533
+ "Tests alongside implementation code should be in the same subtask",
534
+ );
535
+
536
+ expect(patterns).toContain("Tests alongside implementation");
537
+ });
538
+
539
+ it("returns empty array for no matches", () => {
540
+ const patterns = extractPatternsFromDescription(
541
+ "Just a regular description with no patterns",
542
+ );
543
+
544
+ expect(patterns).toHaveLength(0);
545
+ });
546
+ });
547
+
548
+ describe("formatAntiPatternsForPrompt", () => {
549
+ it("formats anti-patterns as bullet list", () => {
550
+ const patterns: DecompositionPattern[] = [
551
+ {
552
+ ...createPattern("Bad pattern 1"),
553
+ kind: "anti_pattern",
554
+ is_negative: true,
555
+ },
556
+ {
557
+ ...createPattern("Bad pattern 2"),
558
+ kind: "anti_pattern",
559
+ is_negative: true,
560
+ },
561
+ ];
562
+
563
+ const formatted = formatAntiPatternsForPrompt(patterns);
564
+
565
+ expect(formatted).toContain("Anti-Patterns to Avoid");
566
+ expect(formatted).toContain("Bad pattern 1");
567
+ expect(formatted).toContain("Bad pattern 2");
568
+ });
569
+
570
+ it("returns empty string for no anti-patterns", () => {
571
+ const patterns: DecompositionPattern[] = [createPattern("Good pattern")];
572
+
573
+ const formatted = formatAntiPatternsForPrompt(patterns);
574
+
575
+ expect(formatted).toBe("");
576
+ });
577
+ });
578
+
579
+ describe("formatSuccessfulPatternsForPrompt", () => {
580
+ it("formats successful patterns with success rate", () => {
581
+ const pattern = createPattern("Good pattern");
582
+ pattern.success_count = 8;
583
+ pattern.failure_count = 2;
584
+
585
+ const formatted = formatSuccessfulPatternsForPrompt([pattern]);
586
+
587
+ expect(formatted).toContain("Successful Patterns");
588
+ expect(formatted).toContain("Good pattern");
589
+ expect(formatted).toContain("80%");
590
+ });
591
+
592
+ it("excludes patterns below success threshold", () => {
593
+ const pattern = createPattern("Mediocre pattern");
594
+ pattern.success_count = 5;
595
+ pattern.failure_count = 5; // 50% success
596
+
597
+ const formatted = formatSuccessfulPatternsForPrompt([pattern], 0.7);
598
+
599
+ expect(formatted).toBe("");
600
+ });
601
+ });
602
+ });
603
+
604
+ // ============================================================================
605
+ // Pattern Maturity Tests
606
+ // ============================================================================
607
+
608
+ /**
609
+ * Create maturity feedback events for testing
610
+ */
611
+ function createMaturityFeedback(
612
+ patternId: string,
613
+ type: "helpful" | "harmful",
614
+ daysAgo: number = 0,
615
+ ): MaturityFeedback {
616
+ return {
617
+ pattern_id: patternId,
618
+ type,
619
+ timestamp: new Date(
620
+ Date.now() - daysAgo * 24 * 60 * 60 * 1000,
621
+ ).toISOString(),
622
+ weight: 1,
623
+ };
624
+ }
625
+
626
+ describe("Pattern Maturity", () => {
627
+ describe("calculateMaturityState", () => {
628
+ it("returns candidate for insufficient feedback", () => {
629
+ const feedback: MaturityFeedback[] = [
630
+ createMaturityFeedback("test", "helpful"),
631
+ ];
632
+
633
+ const state = calculateMaturityState(feedback);
634
+ expect(state).toBe("candidate");
635
+ });
636
+
637
+ it("returns deprecated for high harmful ratio", () => {
638
+ const feedback: MaturityFeedback[] = [
639
+ createMaturityFeedback("test", "helpful"),
640
+ createMaturityFeedback("test", "helpful"),
641
+ createMaturityFeedback("test", "harmful"),
642
+ createMaturityFeedback("test", "harmful"),
643
+ createMaturityFeedback("test", "harmful"),
644
+ createMaturityFeedback("test", "harmful"),
645
+ createMaturityFeedback("test", "harmful"),
646
+ ];
647
+
648
+ const state = calculateMaturityState(feedback);
649
+ expect(state).toBe("deprecated");
650
+ });
651
+
652
+ it("returns proven for consistent success", () => {
653
+ const feedback: MaturityFeedback[] = [];
654
+ // Add 10 helpful, 1 harmful
655
+ for (let i = 0; i < 10; i++) {
656
+ feedback.push(createMaturityFeedback("test", "helpful"));
657
+ }
658
+ feedback.push(createMaturityFeedback("test", "harmful"));
659
+
660
+ const state = calculateMaturityState(feedback);
661
+ expect(state).toBe("proven");
662
+ });
663
+
664
+ it("returns established for moderate feedback", () => {
665
+ const feedback: MaturityFeedback[] = [
666
+ createMaturityFeedback("test", "helpful"),
667
+ createMaturityFeedback("test", "helpful"),
668
+ createMaturityFeedback("test", "helpful"),
669
+ createMaturityFeedback("test", "helpful"),
670
+ createMaturityFeedback("test", "harmful"),
671
+ ];
672
+
673
+ const state = calculateMaturityState(feedback);
674
+ expect(state).toBe("established");
675
+ });
676
+ });
677
+
678
+ describe("promotePattern", () => {
679
+ it("promotes to proven state", () => {
680
+ const maturity = createPatternMaturity("test");
681
+
682
+ const promoted = promotePattern(maturity);
683
+ expect(promoted.state).toBe("proven");
684
+ expect(promoted.promoted_at).toBeDefined();
685
+ });
686
+
687
+ it("keeps proven state if already proven", () => {
688
+ const maturity: PatternMaturity = {
689
+ pattern_id: "test",
690
+ state: "proven",
691
+ helpful_count: 20,
692
+ harmful_count: 0,
693
+ last_validated: new Date().toISOString(),
694
+ };
695
+
696
+ const promoted = promotePattern(maturity);
697
+ expect(promoted.state).toBe("proven");
698
+ });
699
+
700
+ it("throws when promoting deprecated pattern", () => {
701
+ const maturity: PatternMaturity = {
702
+ pattern_id: "test",
703
+ state: "deprecated",
704
+ helpful_count: 2,
705
+ harmful_count: 8,
706
+ last_validated: new Date().toISOString(),
707
+ };
708
+
709
+ expect(() => promotePattern(maturity)).toThrow();
710
+ });
711
+ });
712
+
713
+ describe("deprecatePattern", () => {
714
+ it("deprecates pattern", () => {
715
+ const maturity = createPatternMaturity("test");
716
+
717
+ const deprecated = deprecatePattern(maturity, "Too many failures");
718
+ expect(deprecated.state).toBe("deprecated");
719
+ expect(deprecated.deprecated_at).toBeDefined();
720
+ });
721
+
722
+ it("keeps deprecated state if already deprecated", () => {
723
+ const maturity: PatternMaturity = {
724
+ pattern_id: "test",
725
+ state: "deprecated",
726
+ helpful_count: 2,
727
+ harmful_count: 8,
728
+ last_validated: new Date().toISOString(),
729
+ deprecated_at: new Date().toISOString(),
730
+ };
731
+
732
+ const deprecated = deprecatePattern(maturity);
733
+ expect(deprecated.state).toBe("deprecated");
734
+ });
735
+ });
736
+
737
+ describe("getMaturityMultiplier", () => {
738
+ it("returns correct multipliers for each state", () => {
739
+ expect(getMaturityMultiplier("candidate")).toBe(0.5);
740
+ expect(getMaturityMultiplier("established")).toBe(1.0);
741
+ expect(getMaturityMultiplier("proven")).toBe(1.5);
742
+ expect(getMaturityMultiplier("deprecated")).toBe(0);
743
+ });
744
+ });
745
+
746
+ describe("formatMaturityForPrompt", () => {
747
+ it("formats proven maturity info", () => {
748
+ const maturity: PatternMaturity = {
749
+ pattern_id: "pattern-1",
750
+ state: "proven",
751
+ helpful_count: 10,
752
+ harmful_count: 1,
753
+ last_validated: new Date().toISOString(),
754
+ };
755
+
756
+ const formatted = formatMaturityForPrompt(maturity);
757
+
758
+ expect(formatted).toContain("PROVEN");
759
+ expect(formatted).toContain("helpful");
760
+ });
761
+
762
+ it("formats deprecated maturity info", () => {
763
+ const maturity: PatternMaturity = {
764
+ pattern_id: "pattern-2",
765
+ state: "deprecated",
766
+ helpful_count: 2,
767
+ harmful_count: 8,
768
+ last_validated: new Date().toISOString(),
769
+ };
770
+
771
+ const formatted = formatMaturityForPrompt(maturity);
772
+
773
+ expect(formatted).toContain("DEPRECATED");
774
+ expect(formatted).toContain("harmful");
775
+ });
776
+ });
777
+ });
778
+
779
+ // ============================================================================
780
+ // Swarm Tool Integration Tests
781
+ // ============================================================================
782
+
783
+ describe("Swarm Tool Integrations", () => {
784
+ describe("swarm_record_outcome", () => {
785
+ it("records successful outcome and generates feedback", async () => {
786
+ const result = await swarm_record_outcome.execute(
787
+ {
788
+ bead_id: "test-bead-123",
789
+ duration_ms: 60000,
790
+ error_count: 0,
791
+ retry_count: 0,
792
+ success: true,
793
+ files_touched: ["src/test.ts"],
794
+ },
795
+ mockContext,
796
+ );
797
+
798
+ const parsed = JSON.parse(result);
799
+
800
+ expect(parsed.success).toBe(true);
801
+ expect(parsed.outcome.scored.type).toBe("helpful");
802
+ expect(parsed.feedback_events).toHaveLength(4); // Default 4 criteria
803
+ expect(parsed.feedback_events[0].criterion).toBe("type_safe");
804
+ });
805
+
806
+ it("records failed outcome as harmful", async () => {
807
+ const result = await swarm_record_outcome.execute(
808
+ {
809
+ bead_id: "test-bead-456",
810
+ duration_ms: 3600000, // 1 hour
811
+ error_count: 10,
812
+ retry_count: 5,
813
+ success: false,
814
+ },
815
+ mockContext,
816
+ );
817
+
818
+ const parsed = JSON.parse(result);
819
+
820
+ expect(parsed.outcome.scored.type).toBe("harmful");
821
+ });
822
+
823
+ it("uses custom criteria when provided", async () => {
824
+ const result = await swarm_record_outcome.execute(
825
+ {
826
+ bead_id: "test-bead-789",
827
+ duration_ms: 60000,
828
+ error_count: 0,
829
+ retry_count: 0,
830
+ success: true,
831
+ criteria: ["custom_criterion"],
832
+ },
833
+ mockContext,
834
+ );
835
+
836
+ const parsed = JSON.parse(result);
837
+
838
+ expect(parsed.feedback_events).toHaveLength(1);
839
+ expect(parsed.feedback_events[0].criterion).toBe("custom_criterion");
840
+ });
841
+ });
842
+
843
+ describe("detectInstructionConflicts", () => {
844
+ it("detects positive/negative conflicts", () => {
845
+ const subtasks = [
846
+ {
847
+ title: "Use React Query for state management",
848
+ description: "Always use React Query",
849
+ },
850
+ {
851
+ title: "Avoid external state libraries",
852
+ description: "Never use external state libraries",
853
+ },
854
+ ];
855
+
856
+ const conflicts = detectInstructionConflicts(subtasks);
857
+
858
+ // Should detect potential conflict around "state" and "use/avoid"
859
+ expect(conflicts.length).toBeGreaterThanOrEqual(0); // Heuristic may or may not catch this
860
+ });
861
+
862
+ it("returns empty array for non-conflicting subtasks", () => {
863
+ const subtasks = [
864
+ {
865
+ title: "Add user authentication",
866
+ description: "Implement OAuth flow",
867
+ },
868
+ { title: "Add API routes", description: "Create REST endpoints" },
869
+ ];
870
+
871
+ const conflicts = detectInstructionConflicts(subtasks);
872
+
873
+ expect(conflicts).toHaveLength(0);
874
+ });
875
+ });
876
+
877
+ describe("swarm_validate_decomposition with conflicts", () => {
878
+ it("includes instruction conflicts as warnings", async () => {
879
+ const decomposition = {
880
+ epic: { title: "Test Epic" },
881
+ subtasks: [
882
+ {
883
+ title: "Always use TypeScript strict mode",
884
+ description: "Must enable strict mode",
885
+ files: ["tsconfig.json"],
886
+ dependencies: [],
887
+ estimated_complexity: 1,
888
+ },
889
+ {
890
+ title: "Avoid strict TypeScript settings",
891
+ description: "Never use strict mode",
892
+ files: ["src/index.ts"],
893
+ dependencies: [],
894
+ estimated_complexity: 1,
895
+ },
896
+ ],
897
+ };
898
+
899
+ const result = await swarm_validate_decomposition.execute(
900
+ { response: JSON.stringify(decomposition) },
901
+ mockContext,
902
+ );
903
+
904
+ const parsed = JSON.parse(result);
905
+
906
+ expect(parsed.valid).toBe(true);
907
+ // Warnings may or may not be present depending on heuristic
908
+ if (parsed.warnings) {
909
+ expect(parsed.warnings).toHaveProperty("instruction_conflicts");
910
+ }
911
+ });
912
+ });
913
+
914
+ describe("swarm_decompose with CASS integration", () => {
915
+ it("includes cass_history in response", async () => {
916
+ const result = await swarm_decompose.execute(
917
+ {
918
+ task: "Add user authentication",
919
+ max_subtasks: 3,
920
+ query_cass: true,
921
+ },
922
+ mockContext,
923
+ );
924
+
925
+ const parsed = JSON.parse(result);
926
+
927
+ expect(parsed).toHaveProperty("cass_history");
928
+ expect(parsed.cass_history).toHaveProperty("queried");
929
+ });
930
+
931
+ it("skips CASS when disabled", async () => {
932
+ const result = await swarm_decompose.execute(
933
+ {
934
+ task: "Add user authentication",
935
+ max_subtasks: 3,
936
+ query_cass: false,
937
+ },
938
+ mockContext,
939
+ );
940
+
941
+ const parsed = JSON.parse(result);
942
+
943
+ expect(parsed.cass_history.queried).toBe(false);
944
+ });
945
+ });
946
+ });
947
+
948
+ // ============================================================================
949
+ // Pattern Storage Tests
950
+ // ============================================================================
951
+
952
+ describe("InMemoryPatternStorage", () => {
953
+ let storage: InMemoryPatternStorage;
954
+
955
+ beforeEach(() => {
956
+ storage = new InMemoryPatternStorage();
957
+ });
958
+
959
+ it("stores and retrieves patterns", async () => {
960
+ const pattern = createPattern("Test pattern");
961
+ await storage.store(pattern);
962
+
963
+ const retrieved = await storage.get(pattern.id);
964
+ expect(retrieved).not.toBeNull();
965
+ expect(retrieved!.content).toBe("Test pattern");
966
+ });
967
+
968
+ it("lists all patterns", async () => {
969
+ await storage.store(createPattern("Pattern 1"));
970
+ await storage.store(createPattern("Pattern 2"));
971
+
972
+ const all = await storage.getAll();
973
+ expect(all).toHaveLength(2);
974
+ });
975
+
976
+ it("filters anti-patterns", async () => {
977
+ const pattern = createPattern("Good pattern");
978
+ const antiPattern = {
979
+ ...createPattern("Bad pattern"),
980
+ kind: "anti_pattern" as const,
981
+ is_negative: true,
982
+ };
983
+
984
+ await storage.store(pattern);
985
+ await storage.store(antiPattern);
986
+
987
+ const antiPatterns = await storage.getAntiPatterns();
988
+ expect(antiPatterns).toHaveLength(1);
989
+ expect(antiPatterns[0].content).toBe("Bad pattern");
990
+ });
991
+
992
+ it("filters by tag", async () => {
993
+ const pattern1 = { ...createPattern("Pattern 1"), tags: ["decomposition"] };
994
+ const pattern2 = { ...createPattern("Pattern 2"), tags: ["testing"] };
995
+
996
+ await storage.store(pattern1);
997
+ await storage.store(pattern2);
998
+
999
+ const decompositionPatterns = await storage.getByTag("decomposition");
1000
+ expect(decompositionPatterns).toHaveLength(1);
1001
+ });
1002
+
1003
+ it("finds patterns by content", async () => {
1004
+ await storage.store(createPattern("Split by file type"));
1005
+ await storage.store(createPattern("Split by component"));
1006
+ await storage.store(createPattern("Sequential execution"));
1007
+
1008
+ const splitPatterns = await storage.findByContent("split");
1009
+ expect(splitPatterns).toHaveLength(2);
1010
+ });
1011
+ });
1012
+
1013
+ // ============================================================================
1014
+ // Maturity Storage Tests
1015
+ // ============================================================================
1016
+
1017
+ describe("InMemoryMaturityStorage", () => {
1018
+ let storage: InMemoryMaturityStorage;
1019
+
1020
+ beforeEach(() => {
1021
+ storage = new InMemoryMaturityStorage();
1022
+ });
1023
+
1024
+ it("stores and retrieves maturity records", async () => {
1025
+ const maturity: PatternMaturity = {
1026
+ pattern_id: "pattern-1",
1027
+ state: "candidate",
1028
+ helpful_count: 0,
1029
+ harmful_count: 0,
1030
+ last_validated: new Date().toISOString(),
1031
+ };
1032
+
1033
+ await storage.store(maturity);
1034
+ const retrieved = await storage.get("pattern-1");
1035
+
1036
+ expect(retrieved).not.toBeNull();
1037
+ expect(retrieved!.state).toBe("candidate");
1038
+ });
1039
+
1040
+ it("stores and retrieves feedback events", async () => {
1041
+ const feedback: MaturityFeedback = {
1042
+ pattern_id: "pattern-1",
1043
+ type: "helpful",
1044
+ timestamp: new Date().toISOString(),
1045
+ weight: 1,
1046
+ };
1047
+
1048
+ await storage.storeFeedback(feedback);
1049
+ const retrieved = await storage.getFeedback("pattern-1");
1050
+
1051
+ expect(retrieved).toHaveLength(1);
1052
+ expect(retrieved[0].type).toBe("helpful");
1053
+ });
1054
+
1055
+ it("filters feedback by pattern ID", async () => {
1056
+ await storage.storeFeedback({
1057
+ pattern_id: "p1",
1058
+ type: "helpful",
1059
+ timestamp: new Date().toISOString(),
1060
+ weight: 1,
1061
+ });
1062
+ await storage.storeFeedback({
1063
+ pattern_id: "p2",
1064
+ type: "harmful",
1065
+ timestamp: new Date().toISOString(),
1066
+ weight: 1,
1067
+ });
1068
+ await storage.storeFeedback({
1069
+ pattern_id: "p1",
1070
+ type: "helpful",
1071
+ timestamp: new Date().toISOString(),
1072
+ weight: 1,
1073
+ });
1074
+
1075
+ const p1Feedback = await storage.getFeedback("p1");
1076
+ expect(p1Feedback).toHaveLength(2);
1077
+
1078
+ const p2Feedback = await storage.getFeedback("p2");
1079
+ expect(p2Feedback).toHaveLength(1);
1080
+ });
1081
+
1082
+ it("filters by state", async () => {
1083
+ await storage.store({
1084
+ pattern_id: "p1",
1085
+ state: "candidate",
1086
+ helpful_count: 1,
1087
+ harmful_count: 0,
1088
+ last_validated: new Date().toISOString(),
1089
+ });
1090
+ await storage.store({
1091
+ pattern_id: "p2",
1092
+ state: "proven",
1093
+ helpful_count: 10,
1094
+ harmful_count: 0,
1095
+ last_validated: new Date().toISOString(),
1096
+ });
1097
+
1098
+ const candidates = await storage.getByState("candidate");
1099
+ expect(candidates).toHaveLength(1);
1100
+
1101
+ const proven = await storage.getByState("proven");
1102
+ expect(proven).toHaveLength(1);
1103
+ });
1104
+ });