opencode-swarm-plugin 0.44.0 → 0.44.1

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 (205) hide show
  1. package/bin/swarm.serve.test.ts +6 -4
  2. package/bin/swarm.ts +16 -10
  3. package/dist/compaction-prompt-scoring.js +139 -0
  4. package/dist/eval-capture.js +12811 -0
  5. package/dist/hive.d.ts.map +1 -1
  6. package/dist/index.js +7644 -62599
  7. package/dist/plugin.js +23766 -78721
  8. package/dist/swarm-orchestrate.d.ts.map +1 -1
  9. package/dist/swarm-prompts.d.ts.map +1 -1
  10. package/dist/swarm-review.d.ts.map +1 -1
  11. package/package.json +17 -5
  12. package/.changeset/swarm-insights-data-layer.md +0 -63
  13. package/.hive/analysis/eval-failure-analysis-2025-12-25.md +0 -331
  14. package/.hive/analysis/session-data-quality-audit.md +0 -320
  15. package/.hive/eval-results.json +0 -483
  16. package/.hive/issues.jsonl +0 -138
  17. package/.hive/memories.jsonl +0 -729
  18. package/.opencode/eval-history.jsonl +0 -327
  19. package/.turbo/turbo-build.log +0 -9
  20. package/CHANGELOG.md +0 -2286
  21. package/SCORER-ANALYSIS.md +0 -598
  22. package/docs/analysis/subagent-coordination-patterns.md +0 -902
  23. package/docs/analysis-socratic-planner-pattern.md +0 -504
  24. package/docs/planning/ADR-001-monorepo-structure.md +0 -171
  25. package/docs/planning/ADR-002-package-extraction.md +0 -393
  26. package/docs/planning/ADR-003-performance-improvements.md +0 -451
  27. package/docs/planning/ADR-004-message-queue-features.md +0 -187
  28. package/docs/planning/ADR-005-devtools-observability.md +0 -202
  29. package/docs/planning/ADR-007-swarm-enhancements-worktree-review.md +0 -168
  30. package/docs/planning/ADR-008-worker-handoff-protocol.md +0 -293
  31. package/docs/planning/ADR-009-oh-my-opencode-patterns.md +0 -353
  32. package/docs/planning/ADR-010-cass-inhousing.md +0 -1215
  33. package/docs/planning/ROADMAP.md +0 -368
  34. package/docs/semantic-memory-cli-syntax.md +0 -123
  35. package/docs/swarm-mail-architecture.md +0 -1147
  36. package/docs/testing/context-recovery-test.md +0 -470
  37. package/evals/ARCHITECTURE.md +0 -1189
  38. package/evals/README.md +0 -768
  39. package/evals/compaction-prompt.eval.ts +0 -149
  40. package/evals/compaction-resumption.eval.ts +0 -289
  41. package/evals/coordinator-behavior.eval.ts +0 -307
  42. package/evals/coordinator-session.eval.ts +0 -154
  43. package/evals/evalite.config.ts.bak +0 -15
  44. package/evals/example.eval.ts +0 -31
  45. package/evals/fixtures/cass-baseline.ts +0 -217
  46. package/evals/fixtures/compaction-cases.ts +0 -350
  47. package/evals/fixtures/compaction-prompt-cases.ts +0 -311
  48. package/evals/fixtures/coordinator-sessions.ts +0 -328
  49. package/evals/fixtures/decomposition-cases.ts +0 -105
  50. package/evals/lib/compaction-loader.test.ts +0 -248
  51. package/evals/lib/compaction-loader.ts +0 -320
  52. package/evals/lib/data-loader.evalite-test.ts +0 -289
  53. package/evals/lib/data-loader.test.ts +0 -345
  54. package/evals/lib/data-loader.ts +0 -281
  55. package/evals/lib/llm.ts +0 -115
  56. package/evals/scorers/compaction-prompt-scorers.ts +0 -145
  57. package/evals/scorers/compaction-scorers.ts +0 -305
  58. package/evals/scorers/coordinator-discipline.evalite-test.ts +0 -539
  59. package/evals/scorers/coordinator-discipline.ts +0 -325
  60. package/evals/scorers/index.test.ts +0 -146
  61. package/evals/scorers/index.ts +0 -328
  62. package/evals/scorers/outcome-scorers.evalite-test.ts +0 -27
  63. package/evals/scorers/outcome-scorers.ts +0 -349
  64. package/evals/swarm-decomposition.eval.ts +0 -121
  65. package/examples/commands/swarm.md +0 -745
  66. package/examples/plugin-wrapper-template.ts +0 -2515
  67. package/examples/skills/hive-workflow/SKILL.md +0 -212
  68. package/examples/skills/skill-creator/SKILL.md +0 -223
  69. package/examples/skills/swarm-coordination/SKILL.md +0 -292
  70. package/global-skills/cli-builder/SKILL.md +0 -344
  71. package/global-skills/cli-builder/references/advanced-patterns.md +0 -244
  72. package/global-skills/learning-systems/SKILL.md +0 -644
  73. package/global-skills/skill-creator/LICENSE.txt +0 -202
  74. package/global-skills/skill-creator/SKILL.md +0 -352
  75. package/global-skills/skill-creator/references/output-patterns.md +0 -82
  76. package/global-skills/skill-creator/references/workflows.md +0 -28
  77. package/global-skills/swarm-coordination/SKILL.md +0 -995
  78. package/global-skills/swarm-coordination/references/coordinator-patterns.md +0 -235
  79. package/global-skills/swarm-coordination/references/strategies.md +0 -138
  80. package/global-skills/system-design/SKILL.md +0 -213
  81. package/global-skills/testing-patterns/SKILL.md +0 -430
  82. package/global-skills/testing-patterns/references/dependency-breaking-catalog.md +0 -586
  83. package/opencode-swarm-plugin-0.30.7.tgz +0 -0
  84. package/opencode-swarm-plugin-0.31.0.tgz +0 -0
  85. package/scripts/cleanup-test-memories.ts +0 -346
  86. package/scripts/init-skill.ts +0 -222
  87. package/scripts/migrate-unknown-sessions.ts +0 -349
  88. package/scripts/validate-skill.ts +0 -204
  89. package/src/agent-mail.ts +0 -1724
  90. package/src/anti-patterns.test.ts +0 -1167
  91. package/src/anti-patterns.ts +0 -448
  92. package/src/compaction-capture.integration.test.ts +0 -257
  93. package/src/compaction-hook.test.ts +0 -838
  94. package/src/compaction-hook.ts +0 -1204
  95. package/src/compaction-observability.integration.test.ts +0 -139
  96. package/src/compaction-observability.test.ts +0 -187
  97. package/src/compaction-observability.ts +0 -324
  98. package/src/compaction-prompt-scorers.test.ts +0 -475
  99. package/src/compaction-prompt-scoring.ts +0 -300
  100. package/src/contributor-tools.test.ts +0 -133
  101. package/src/contributor-tools.ts +0 -201
  102. package/src/dashboard.test.ts +0 -611
  103. package/src/dashboard.ts +0 -462
  104. package/src/error-enrichment.test.ts +0 -403
  105. package/src/error-enrichment.ts +0 -219
  106. package/src/eval-capture.test.ts +0 -1015
  107. package/src/eval-capture.ts +0 -929
  108. package/src/eval-gates.test.ts +0 -306
  109. package/src/eval-gates.ts +0 -218
  110. package/src/eval-history.test.ts +0 -508
  111. package/src/eval-history.ts +0 -214
  112. package/src/eval-learning.test.ts +0 -378
  113. package/src/eval-learning.ts +0 -360
  114. package/src/eval-runner.test.ts +0 -223
  115. package/src/eval-runner.ts +0 -402
  116. package/src/export-tools.test.ts +0 -476
  117. package/src/export-tools.ts +0 -257
  118. package/src/hive.integration.test.ts +0 -2241
  119. package/src/hive.ts +0 -1628
  120. package/src/index.ts +0 -940
  121. package/src/learning.integration.test.ts +0 -1815
  122. package/src/learning.ts +0 -1079
  123. package/src/logger.test.ts +0 -189
  124. package/src/logger.ts +0 -135
  125. package/src/mandate-promotion.test.ts +0 -473
  126. package/src/mandate-promotion.ts +0 -239
  127. package/src/mandate-storage.integration.test.ts +0 -601
  128. package/src/mandate-storage.test.ts +0 -578
  129. package/src/mandate-storage.ts +0 -794
  130. package/src/mandates.ts +0 -540
  131. package/src/memory-tools.test.ts +0 -195
  132. package/src/memory-tools.ts +0 -344
  133. package/src/memory.integration.test.ts +0 -334
  134. package/src/memory.test.ts +0 -158
  135. package/src/memory.ts +0 -527
  136. package/src/model-selection.test.ts +0 -188
  137. package/src/model-selection.ts +0 -68
  138. package/src/observability-tools.test.ts +0 -359
  139. package/src/observability-tools.ts +0 -871
  140. package/src/output-guardrails.test.ts +0 -438
  141. package/src/output-guardrails.ts +0 -381
  142. package/src/pattern-maturity.test.ts +0 -1160
  143. package/src/pattern-maturity.ts +0 -525
  144. package/src/planning-guardrails.test.ts +0 -491
  145. package/src/planning-guardrails.ts +0 -438
  146. package/src/plugin.ts +0 -23
  147. package/src/post-compaction-tracker.test.ts +0 -251
  148. package/src/post-compaction-tracker.ts +0 -237
  149. package/src/query-tools.test.ts +0 -636
  150. package/src/query-tools.ts +0 -324
  151. package/src/rate-limiter.integration.test.ts +0 -466
  152. package/src/rate-limiter.ts +0 -774
  153. package/src/replay-tools.test.ts +0 -496
  154. package/src/replay-tools.ts +0 -240
  155. package/src/repo-crawl.integration.test.ts +0 -441
  156. package/src/repo-crawl.ts +0 -610
  157. package/src/schemas/cell-events.test.ts +0 -347
  158. package/src/schemas/cell-events.ts +0 -807
  159. package/src/schemas/cell.ts +0 -257
  160. package/src/schemas/evaluation.ts +0 -166
  161. package/src/schemas/index.test.ts +0 -199
  162. package/src/schemas/index.ts +0 -286
  163. package/src/schemas/mandate.ts +0 -232
  164. package/src/schemas/swarm-context.ts +0 -115
  165. package/src/schemas/task.ts +0 -161
  166. package/src/schemas/worker-handoff.test.ts +0 -302
  167. package/src/schemas/worker-handoff.ts +0 -131
  168. package/src/sessions/agent-discovery.test.ts +0 -137
  169. package/src/sessions/agent-discovery.ts +0 -112
  170. package/src/sessions/index.ts +0 -15
  171. package/src/skills.integration.test.ts +0 -1192
  172. package/src/skills.test.ts +0 -643
  173. package/src/skills.ts +0 -1549
  174. package/src/storage.integration.test.ts +0 -341
  175. package/src/storage.ts +0 -884
  176. package/src/structured.integration.test.ts +0 -817
  177. package/src/structured.test.ts +0 -1046
  178. package/src/structured.ts +0 -762
  179. package/src/swarm-decompose.test.ts +0 -188
  180. package/src/swarm-decompose.ts +0 -1302
  181. package/src/swarm-deferred.integration.test.ts +0 -157
  182. package/src/swarm-deferred.test.ts +0 -38
  183. package/src/swarm-insights.test.ts +0 -214
  184. package/src/swarm-insights.ts +0 -459
  185. package/src/swarm-mail.integration.test.ts +0 -970
  186. package/src/swarm-mail.ts +0 -739
  187. package/src/swarm-orchestrate.integration.test.ts +0 -282
  188. package/src/swarm-orchestrate.test.ts +0 -548
  189. package/src/swarm-orchestrate.ts +0 -3084
  190. package/src/swarm-prompts.test.ts +0 -1270
  191. package/src/swarm-prompts.ts +0 -2077
  192. package/src/swarm-research.integration.test.ts +0 -701
  193. package/src/swarm-research.test.ts +0 -698
  194. package/src/swarm-research.ts +0 -472
  195. package/src/swarm-review.integration.test.ts +0 -285
  196. package/src/swarm-review.test.ts +0 -879
  197. package/src/swarm-review.ts +0 -709
  198. package/src/swarm-strategies.ts +0 -407
  199. package/src/swarm-worktree.test.ts +0 -501
  200. package/src/swarm-worktree.ts +0 -575
  201. package/src/swarm.integration.test.ts +0 -2377
  202. package/src/swarm.ts +0 -38
  203. package/src/tool-adapter.integration.test.ts +0 -1221
  204. package/src/tool-availability.ts +0 -461
  205. package/tsconfig.json +0 -28
@@ -1,1815 +0,0 @@
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
- * ## Test Isolation Pattern
8
- *
9
- * This file uses TEST_SEMANTIC_MEMORY_COLLECTION to isolate test data from
10
- * production semantic-memory collections. Each test run gets a unique suffix,
11
- * preventing pollution of the default collections.
12
- *
13
- * **Cleanup**: Test collections are NOT automatically deleted after test runs.
14
- * This is intentional - semantic-memory doesn't provide bulk delete APIs.
15
- * To clean up test artifacts, use:
16
- *
17
- * ```bash
18
- * # Manual cleanup (use scripts/cleanup-test-memories.ts for automation)
19
- * semantic-memory list --collection swarm-feedback-test-* --json | jq -r '.[].id' | xargs -I {} semantic-memory remove {}
20
- * ```
21
- *
22
- * The unique suffix prevents cross-test interference even without cleanup.
23
- */
24
- import {
25
- describe,
26
- it,
27
- expect,
28
- beforeEach,
29
- afterEach,
30
- beforeAll,
31
- afterAll,
32
- vi,
33
- } from "vitest";
34
- import { getTestCollectionName } from "./storage";
35
-
36
- // ============================================================================
37
- // Test Isolation Setup
38
- // ============================================================================
39
-
40
- /**
41
- * Set unique collection suffix for this test run
42
- *
43
- * CRITICAL: This MUST be set before any storage instances are created.
44
- * The env var is read during getCollectionNames() which happens at storage init.
45
- */
46
- process.env.TEST_SEMANTIC_MEMORY_COLLECTION = getTestCollectionName();
47
-
48
- // Learning module
49
- import {
50
- calculateDecayedValue,
51
- calculateCriterionWeight,
52
- scoreImplicitFeedback,
53
- outcomeToFeedback,
54
- applyWeights,
55
- shouldDeprecateCriterion,
56
- InMemoryFeedbackStorage,
57
- DEFAULT_LEARNING_CONFIG,
58
- type FeedbackEvent,
59
- type OutcomeSignals,
60
- type CriterionWeight,
61
- } from "./learning";
62
-
63
- // Anti-patterns module
64
- import {
65
- shouldInvertPattern,
66
- invertToAntiPattern,
67
- recordPatternObservation,
68
- extractPatternsFromDescription,
69
- createPattern,
70
- formatAntiPatternsForPrompt,
71
- formatSuccessfulPatternsForPrompt,
72
- InMemoryPatternStorage,
73
- DEFAULT_ANTI_PATTERN_CONFIG,
74
- type DecompositionPattern,
75
- } from "./anti-patterns";
76
-
77
- // Pattern maturity module
78
- import {
79
- calculateMaturityState,
80
- calculateDecayedCounts,
81
- createPatternMaturity,
82
- updatePatternMaturity,
83
- promotePattern,
84
- deprecatePattern,
85
- formatMaturityForPrompt,
86
- getMaturityMultiplier,
87
- InMemoryMaturityStorage,
88
- DEFAULT_MATURITY_CONFIG,
89
- type PatternMaturity,
90
- type MaturityFeedback,
91
- } from "./pattern-maturity";
92
-
93
- // Swarm tools
94
- import {
95
- swarm_decompose,
96
- swarm_validate_decomposition,
97
- swarm_record_outcome,
98
- detectInstructionConflicts,
99
- } from "./swarm";
100
-
101
- // ============================================================================
102
- // Test Helpers
103
- // ============================================================================
104
-
105
- const mockContext = {
106
- sessionID: `test-learning-${Date.now()}`,
107
- messageID: `test-message-${Date.now()}`,
108
- agent: "test-agent",
109
- abort: new AbortController().signal,
110
- };
111
-
112
- /**
113
- * Global test lifecycle hooks
114
- *
115
- * These document the test isolation pattern but don't actively clean up.
116
- * Cleanup is manual via scripts/cleanup-test-memories.ts
117
- */
118
- beforeAll(() => {
119
- console.log(
120
- `[test] TEST_SEMANTIC_MEMORY_COLLECTION = ${process.env.TEST_SEMANTIC_MEMORY_COLLECTION}`,
121
- );
122
- console.log(
123
- `[test] Test collections will be prefixed with: swarm-*-${process.env.TEST_SEMANTIC_MEMORY_COLLECTION}`,
124
- );
125
- });
126
-
127
- afterAll(() => {
128
- console.log(
129
- `[test] Test complete. Collections NOT auto-deleted (use scripts/cleanup-test-memories.ts for cleanup)`,
130
- );
131
- console.log(
132
- `[test] Test collection suffix was: ${process.env.TEST_SEMANTIC_MEMORY_COLLECTION}`,
133
- );
134
- });
135
-
136
- /**
137
- * Create a feedback event for testing
138
- */
139
- function createFeedbackEvent(
140
- criterion: string,
141
- type: "helpful" | "harmful" | "neutral",
142
- daysAgo: number = 0,
143
- ): FeedbackEvent {
144
- const timestamp = new Date(
145
- Date.now() - daysAgo * 24 * 60 * 60 * 1000,
146
- ).toISOString();
147
- return {
148
- id: `test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
149
- criterion,
150
- type,
151
- timestamp,
152
- raw_value: 1,
153
- };
154
- }
155
-
156
- /**
157
- * Create outcome signals for testing
158
- */
159
- function createOutcomeSignals(
160
- overrides: Partial<OutcomeSignals> = {},
161
- ): OutcomeSignals {
162
- return {
163
- bead_id: `test-bead-${Date.now()}`,
164
- duration_ms: 60000, // 1 minute
165
- error_count: 0,
166
- retry_count: 0,
167
- success: true,
168
- files_touched: ["src/test.ts"],
169
- timestamp: new Date().toISOString(),
170
- ...overrides,
171
- };
172
- }
173
-
174
- // ============================================================================
175
- // Confidence Decay Tests
176
- // ============================================================================
177
-
178
- describe("Confidence Decay", () => {
179
- describe("calculateDecayedValue", () => {
180
- it("returns 1.0 for current timestamp", () => {
181
- const now = new Date();
182
- const value = calculateDecayedValue(now.toISOString(), now);
183
- expect(value).toBeCloseTo(1.0, 5);
184
- });
185
-
186
- it("returns ~0.5 after one half-life", () => {
187
- const now = new Date();
188
- const halfLifeDays = 90;
189
- const pastDate = new Date(
190
- now.getTime() - halfLifeDays * 24 * 60 * 60 * 1000,
191
- );
192
- const value = calculateDecayedValue(
193
- pastDate.toISOString(),
194
- now,
195
- halfLifeDays,
196
- );
197
- expect(value).toBeCloseTo(0.5, 1);
198
- });
199
-
200
- it("returns ~0.25 after two half-lives", () => {
201
- const now = new Date();
202
- const halfLifeDays = 90;
203
- const pastDate = new Date(
204
- now.getTime() - 2 * halfLifeDays * 24 * 60 * 60 * 1000,
205
- );
206
- const value = calculateDecayedValue(
207
- pastDate.toISOString(),
208
- now,
209
- halfLifeDays,
210
- );
211
- expect(value).toBeCloseTo(0.25, 1);
212
- });
213
-
214
- it("handles future timestamps gracefully", () => {
215
- const now = new Date();
216
- const futureDate = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000);
217
- const value = calculateDecayedValue(futureDate.toISOString(), now);
218
- expect(value).toBe(1.0); // Max 0 age = no decay
219
- });
220
- });
221
-
222
- describe("calculateCriterionWeight", () => {
223
- it("returns weight 1.0 for no feedback", () => {
224
- const weight = calculateCriterionWeight([]);
225
- expect(weight.weight).toBe(1.0);
226
- expect(weight.helpful_count).toBe(0);
227
- expect(weight.harmful_count).toBe(0);
228
- });
229
-
230
- it("returns high weight for all helpful feedback", () => {
231
- const events = [
232
- createFeedbackEvent("type_safe", "helpful", 0),
233
- createFeedbackEvent("type_safe", "helpful", 1),
234
- createFeedbackEvent("type_safe", "helpful", 2),
235
- ];
236
- const weight = calculateCriterionWeight(events);
237
- expect(weight.weight).toBeGreaterThan(0.9);
238
- expect(weight.helpful_count).toBe(3);
239
- expect(weight.harmful_count).toBe(0);
240
- });
241
-
242
- it("returns lower weight for mixed feedback", () => {
243
- const events = [
244
- createFeedbackEvent("type_safe", "helpful", 0),
245
- createFeedbackEvent("type_safe", "harmful", 1),
246
- createFeedbackEvent("type_safe", "helpful", 2),
247
- ];
248
- const weight = calculateCriterionWeight(events);
249
- expect(weight.weight).toBeLessThan(0.9);
250
- expect(weight.weight).toBeGreaterThan(0.5);
251
- });
252
-
253
- it("applies decay to older feedback", () => {
254
- // Recent harmful feedback should have more impact than old helpful
255
- const events = [
256
- createFeedbackEvent("type_safe", "helpful", 180), // 180 days ago (2 half-lives)
257
- createFeedbackEvent("type_safe", "harmful", 0), // today
258
- ];
259
- const weight = calculateCriterionWeight(events);
260
- // Harmful is recent (weight ~1), helpful is old (weight ~0.25)
261
- // So harmful dominates
262
- expect(weight.weight).toBeLessThan(0.5);
263
- });
264
-
265
- it("tracks last_validated timestamp", () => {
266
- const events = [
267
- createFeedbackEvent("type_safe", "helpful", 10),
268
- createFeedbackEvent("type_safe", "helpful", 5),
269
- createFeedbackEvent("type_safe", "helpful", 0),
270
- ];
271
- const weight = calculateCriterionWeight(events);
272
- expect(weight.last_validated).toBeDefined();
273
- // Most recent helpful event should be last_validated
274
- const lastValidated = new Date(weight.last_validated!);
275
- const now = new Date();
276
- const diffDays =
277
- (now.getTime() - lastValidated.getTime()) / (24 * 60 * 60 * 1000);
278
- expect(diffDays).toBeLessThan(1);
279
- });
280
- });
281
-
282
- describe("shouldDeprecateCriterion", () => {
283
- it("returns false for insufficient feedback", () => {
284
- const weight: CriterionWeight = {
285
- criterion: "type_safe",
286
- weight: 0.3,
287
- helpful_count: 1,
288
- harmful_count: 1,
289
- half_life_days: 90,
290
- };
291
- expect(shouldDeprecateCriterion(weight)).toBe(false);
292
- });
293
-
294
- it("returns true for high harmful ratio with enough feedback", () => {
295
- const weight: CriterionWeight = {
296
- criterion: "type_safe",
297
- weight: 0.3,
298
- helpful_count: 1,
299
- harmful_count: 4, // 80% harmful
300
- half_life_days: 90,
301
- };
302
- expect(shouldDeprecateCriterion(weight)).toBe(true);
303
- });
304
-
305
- it("returns false for acceptable harmful ratio", () => {
306
- const weight: CriterionWeight = {
307
- criterion: "type_safe",
308
- weight: 0.8,
309
- helpful_count: 8,
310
- harmful_count: 2, // 20% harmful
311
- half_life_days: 90,
312
- };
313
- expect(shouldDeprecateCriterion(weight)).toBe(false);
314
- });
315
- });
316
- });
317
-
318
- // ============================================================================
319
- // Outcome Scoring Tests
320
- // ============================================================================
321
-
322
- describe("Outcome Scoring", () => {
323
- describe("scoreImplicitFeedback", () => {
324
- it("scores fast successful completion as helpful", () => {
325
- const signals = createOutcomeSignals({
326
- duration_ms: 60000, // 1 minute (fast)
327
- error_count: 0,
328
- retry_count: 0,
329
- success: true,
330
- });
331
- const scored = scoreImplicitFeedback(signals);
332
- expect(scored.type).toBe("helpful");
333
- expect(scored.decayed_value).toBeGreaterThan(0.7);
334
- });
335
-
336
- it("scores slow failed completion as harmful", () => {
337
- const signals = createOutcomeSignals({
338
- duration_ms: 60 * 60 * 1000, // 1 hour (slow)
339
- error_count: 5,
340
- retry_count: 3,
341
- success: false,
342
- });
343
- const scored = scoreImplicitFeedback(signals);
344
- expect(scored.type).toBe("harmful");
345
- expect(scored.decayed_value).toBeLessThan(0.4);
346
- });
347
-
348
- it("scores mixed signals as neutral", () => {
349
- const signals = createOutcomeSignals({
350
- duration_ms: 15 * 60 * 1000, // 15 minutes (medium)
351
- error_count: 1,
352
- retry_count: 1,
353
- success: true,
354
- });
355
- const scored = scoreImplicitFeedback(signals);
356
- // Could be helpful or neutral depending on exact thresholds
357
- expect(["helpful", "neutral"]).toContain(scored.type);
358
- });
359
-
360
- it("includes reasoning in result", () => {
361
- const signals = createOutcomeSignals();
362
- const scored = scoreImplicitFeedback(signals);
363
- expect(scored.reasoning).toBeDefined();
364
- expect(scored.reasoning.length).toBeGreaterThan(0);
365
- });
366
- });
367
-
368
- describe("outcomeToFeedback", () => {
369
- it("converts scored outcome to feedback event", () => {
370
- const signals = createOutcomeSignals({ bead_id: "test-bead-123" });
371
- const scored = scoreImplicitFeedback(signals);
372
- const feedback = outcomeToFeedback(scored, "type_safe");
373
-
374
- expect(feedback.criterion).toBe("type_safe");
375
- expect(feedback.type).toBe(scored.type);
376
- expect(feedback.bead_id).toBe("test-bead-123");
377
- expect(feedback.context).toBe(scored.reasoning);
378
- });
379
- });
380
-
381
- describe("applyWeights", () => {
382
- it("applies weights to raw scores", () => {
383
- const criteria = {
384
- type_safe: 0.8,
385
- no_bugs: 0.9,
386
- patterns: 0.7,
387
- };
388
- const weights: Record<string, CriterionWeight> = {
389
- type_safe: {
390
- criterion: "type_safe",
391
- weight: 1.0,
392
- helpful_count: 5,
393
- harmful_count: 0,
394
- half_life_days: 90,
395
- },
396
- no_bugs: {
397
- criterion: "no_bugs",
398
- weight: 0.5,
399
- helpful_count: 2,
400
- harmful_count: 2,
401
- half_life_days: 90,
402
- },
403
- patterns: {
404
- criterion: "patterns",
405
- weight: 0.8,
406
- helpful_count: 4,
407
- harmful_count: 1,
408
- half_life_days: 90,
409
- },
410
- };
411
-
412
- const result = applyWeights(criteria, weights);
413
-
414
- expect(result.type_safe.raw).toBe(0.8);
415
- expect(result.type_safe.weighted).toBe(0.8); // 0.8 * 1.0
416
- expect(result.no_bugs.weighted).toBe(0.45); // 0.9 * 0.5
417
- expect(result.patterns.weighted).toBeCloseTo(0.56); // 0.7 * 0.8
418
- });
419
-
420
- it("uses default weight 1.0 for unknown criteria", () => {
421
- const criteria = { unknown_criterion: 0.5 };
422
- const weights: Record<string, CriterionWeight> = {};
423
-
424
- const result = applyWeights(criteria, weights);
425
-
426
- expect(result.unknown_criterion.weight).toBe(1.0);
427
- expect(result.unknown_criterion.weighted).toBe(0.5);
428
- });
429
- });
430
- });
431
-
432
- // ============================================================================
433
- // Feedback Storage Tests
434
- // ============================================================================
435
-
436
- describe("InMemoryFeedbackStorage", () => {
437
- let storage: InMemoryFeedbackStorage;
438
-
439
- beforeEach(() => {
440
- storage = new InMemoryFeedbackStorage();
441
- });
442
-
443
- it("stores and retrieves feedback events", async () => {
444
- const event = createFeedbackEvent("type_safe", "helpful");
445
- await storage.store(event);
446
-
447
- const all = await storage.getAll();
448
- expect(all).toHaveLength(1);
449
- expect(all[0].id).toBe(event.id);
450
- });
451
-
452
- it("retrieves events by criterion", async () => {
453
- await storage.store(createFeedbackEvent("type_safe", "helpful"));
454
- await storage.store(createFeedbackEvent("no_bugs", "harmful"));
455
- await storage.store(createFeedbackEvent("type_safe", "helpful"));
456
-
457
- const typeSafe = await storage.getByCriterion("type_safe");
458
- expect(typeSafe).toHaveLength(2);
459
-
460
- const noBugs = await storage.getByCriterion("no_bugs");
461
- expect(noBugs).toHaveLength(1);
462
- });
463
-
464
- it("retrieves events by bead ID", async () => {
465
- const event1 = {
466
- ...createFeedbackEvent("type_safe", "helpful"),
467
- bead_id: "bead-1",
468
- };
469
- const event2 = {
470
- ...createFeedbackEvent("no_bugs", "harmful"),
471
- bead_id: "bead-1",
472
- };
473
- const event3 = {
474
- ...createFeedbackEvent("type_safe", "helpful"),
475
- bead_id: "bead-2",
476
- };
477
-
478
- await storage.store(event1);
479
- await storage.store(event2);
480
- await storage.store(event3);
481
-
482
- const bead1Events = await storage.getByBead("bead-1");
483
- expect(bead1Events).toHaveLength(2);
484
- });
485
- });
486
-
487
- // ============================================================================
488
- // Anti-Pattern Tests
489
- // ============================================================================
490
-
491
- describe("Anti-Patterns", () => {
492
- describe("shouldInvertPattern", () => {
493
- it("returns false for patterns with insufficient observations", () => {
494
- const pattern = createPattern("Split by file type");
495
- pattern.success_count = 1;
496
- pattern.failure_count = 1;
497
-
498
- expect(shouldInvertPattern(pattern)).toBe(false);
499
- });
500
-
501
- it("returns true for patterns with high failure rate", () => {
502
- const pattern = createPattern("Split by file type");
503
- pattern.success_count = 1;
504
- pattern.failure_count = 4; // 80% failure
505
-
506
- expect(shouldInvertPattern(pattern)).toBe(true);
507
- });
508
-
509
- it("returns false for already inverted patterns", () => {
510
- const pattern = createPattern("Split by file type");
511
- pattern.kind = "anti_pattern";
512
- pattern.success_count = 0;
513
- pattern.failure_count = 10;
514
-
515
- expect(shouldInvertPattern(pattern)).toBe(false);
516
- });
517
- });
518
-
519
- describe("invertToAntiPattern", () => {
520
- it("creates anti-pattern with AVOID prefix", () => {
521
- const pattern = createPattern("Split by file type");
522
- const result = invertToAntiPattern(pattern, "High failure rate");
523
-
524
- expect(result.inverted.kind).toBe("anti_pattern");
525
- expect(result.inverted.is_negative).toBe(true);
526
- expect(result.inverted.content).toContain("AVOID:");
527
- expect(result.inverted.content).toContain("Split by file type");
528
- expect(result.inverted.reason).toBe("High failure rate");
529
- });
530
-
531
- it("removes existing prefixes before inverting", () => {
532
- const pattern = createPattern("AVOID: something");
533
- const result = invertToAntiPattern(pattern, "test");
534
-
535
- // Should not have double AVOID
536
- expect(result.inverted.content).not.toContain("AVOID: AVOID:");
537
- });
538
- });
539
-
540
- describe("recordPatternObservation", () => {
541
- it("increments success count on success", () => {
542
- const pattern = createPattern("Test pattern");
543
- const result = recordPatternObservation(pattern, true);
544
-
545
- expect(result.pattern.success_count).toBe(1);
546
- expect(result.pattern.failure_count).toBe(0);
547
- expect(result.inversion).toBeUndefined();
548
- });
549
-
550
- it("increments failure count on failure", () => {
551
- const pattern = createPattern("Test pattern");
552
- const result = recordPatternObservation(pattern, false);
553
-
554
- expect(result.pattern.success_count).toBe(0);
555
- expect(result.pattern.failure_count).toBe(1);
556
- });
557
-
558
- it("triggers inversion when threshold reached", () => {
559
- let pattern = createPattern("Bad pattern");
560
- // Record enough failures to trigger inversion
561
- for (let i = 0; i < 4; i++) {
562
- const result = recordPatternObservation(pattern, false);
563
- pattern = result.pattern;
564
- if (result.inversion) {
565
- expect(result.inversion.inverted.kind).toBe("anti_pattern");
566
- return;
567
- }
568
- }
569
- // Should have triggered by now
570
- expect(pattern.failure_count).toBeGreaterThanOrEqual(3);
571
- });
572
-
573
- it("records bead ID in examples", () => {
574
- const pattern = createPattern("Test pattern");
575
- const result = recordPatternObservation(pattern, true, "bead-123");
576
-
577
- expect(result.pattern.example_beads).toContain("bead-123");
578
- });
579
- });
580
-
581
- describe("extractPatternsFromDescription", () => {
582
- it("extracts file splitting patterns", () => {
583
- const patterns = extractPatternsFromDescription(
584
- "We should split by file type and handle shared types first",
585
- );
586
-
587
- expect(patterns).toContain("Split by file type");
588
- expect(patterns).toContain("Handle shared types first");
589
- });
590
-
591
- it("extracts test organization patterns", () => {
592
- const patterns = extractPatternsFromDescription(
593
- "Tests alongside implementation code should be in the same subtask",
594
- );
595
-
596
- expect(patterns).toContain("Tests alongside implementation");
597
- });
598
-
599
- it("returns empty array for no matches", () => {
600
- const patterns = extractPatternsFromDescription(
601
- "Just a regular description with no patterns",
602
- );
603
-
604
- expect(patterns).toHaveLength(0);
605
- });
606
- });
607
-
608
- describe("formatAntiPatternsForPrompt", () => {
609
- it("formats anti-patterns as bullet list", () => {
610
- const patterns: DecompositionPattern[] = [
611
- {
612
- ...createPattern("Bad pattern 1"),
613
- kind: "anti_pattern",
614
- is_negative: true,
615
- },
616
- {
617
- ...createPattern("Bad pattern 2"),
618
- kind: "anti_pattern",
619
- is_negative: true,
620
- },
621
- ];
622
-
623
- const formatted = formatAntiPatternsForPrompt(patterns);
624
-
625
- expect(formatted).toContain("Anti-Patterns to Avoid");
626
- expect(formatted).toContain("Bad pattern 1");
627
- expect(formatted).toContain("Bad pattern 2");
628
- });
629
-
630
- it("returns empty string for no anti-patterns", () => {
631
- const patterns: DecompositionPattern[] = [createPattern("Good pattern")];
632
-
633
- const formatted = formatAntiPatternsForPrompt(patterns);
634
-
635
- expect(formatted).toBe("");
636
- });
637
- });
638
-
639
- describe("formatSuccessfulPatternsForPrompt", () => {
640
- it("formats successful patterns with success rate", () => {
641
- const pattern = createPattern("Good pattern");
642
- pattern.success_count = 8;
643
- pattern.failure_count = 2;
644
-
645
- const formatted = formatSuccessfulPatternsForPrompt([pattern]);
646
-
647
- expect(formatted).toContain("Successful Patterns");
648
- expect(formatted).toContain("Good pattern");
649
- expect(formatted).toContain("80%");
650
- });
651
-
652
- it("excludes patterns below success threshold", () => {
653
- const pattern = createPattern("Mediocre pattern");
654
- pattern.success_count = 5;
655
- pattern.failure_count = 5; // 50% success
656
-
657
- const formatted = formatSuccessfulPatternsForPrompt([pattern], 0.7);
658
-
659
- expect(formatted).toBe("");
660
- });
661
- });
662
- });
663
-
664
- // ============================================================================
665
- // Pattern Maturity Tests
666
- // ============================================================================
667
-
668
- /**
669
- * Create maturity feedback events for testing
670
- */
671
- function createMaturityFeedback(
672
- patternId: string,
673
- type: "helpful" | "harmful",
674
- daysAgo: number = 0,
675
- ): MaturityFeedback {
676
- return {
677
- pattern_id: patternId,
678
- type,
679
- timestamp: new Date(
680
- Date.now() - daysAgo * 24 * 60 * 60 * 1000,
681
- ).toISOString(),
682
- weight: 1,
683
- };
684
- }
685
-
686
- describe("Pattern Maturity", () => {
687
- describe("calculateMaturityState", () => {
688
- it("returns candidate for insufficient feedback", () => {
689
- const feedback: MaturityFeedback[] = [
690
- createMaturityFeedback("test", "helpful"),
691
- ];
692
-
693
- const state = calculateMaturityState(feedback);
694
- expect(state).toBe("candidate");
695
- });
696
-
697
- it("returns deprecated for high harmful ratio", () => {
698
- const feedback: MaturityFeedback[] = [
699
- createMaturityFeedback("test", "helpful"),
700
- createMaturityFeedback("test", "helpful"),
701
- createMaturityFeedback("test", "harmful"),
702
- createMaturityFeedback("test", "harmful"),
703
- createMaturityFeedback("test", "harmful"),
704
- createMaturityFeedback("test", "harmful"),
705
- createMaturityFeedback("test", "harmful"),
706
- ];
707
-
708
- const state = calculateMaturityState(feedback);
709
- expect(state).toBe("deprecated");
710
- });
711
-
712
- it("returns proven for consistent success", () => {
713
- const feedback: MaturityFeedback[] = [];
714
- // Add 10 helpful, 1 harmful
715
- for (let i = 0; i < 10; i++) {
716
- feedback.push(createMaturityFeedback("test", "helpful"));
717
- }
718
- feedback.push(createMaturityFeedback("test", "harmful"));
719
-
720
- const state = calculateMaturityState(feedback);
721
- expect(state).toBe("proven");
722
- });
723
-
724
- it("returns established for moderate feedback", () => {
725
- const feedback: MaturityFeedback[] = [
726
- createMaturityFeedback("test", "helpful"),
727
- createMaturityFeedback("test", "helpful"),
728
- createMaturityFeedback("test", "helpful"),
729
- createMaturityFeedback("test", "helpful"),
730
- createMaturityFeedback("test", "harmful"),
731
- ];
732
-
733
- const state = calculateMaturityState(feedback);
734
- expect(state).toBe("established");
735
- });
736
- });
737
-
738
- describe("promotePattern", () => {
739
- it("promotes to proven state", () => {
740
- const maturity = createPatternMaturity("test");
741
-
742
- const promoted = promotePattern(maturity);
743
- expect(promoted.state).toBe("proven");
744
- expect(promoted.promoted_at).toBeDefined();
745
- });
746
-
747
- it("keeps proven state if already proven", () => {
748
- const maturity: PatternMaturity = {
749
- pattern_id: "test",
750
- state: "proven",
751
- helpful_count: 20,
752
- harmful_count: 0,
753
- last_validated: new Date().toISOString(),
754
- };
755
-
756
- const promoted = promotePattern(maturity);
757
- expect(promoted.state).toBe("proven");
758
- });
759
-
760
- it("throws when promoting deprecated pattern", () => {
761
- const maturity: PatternMaturity = {
762
- pattern_id: "test",
763
- state: "deprecated",
764
- helpful_count: 2,
765
- harmful_count: 8,
766
- last_validated: new Date().toISOString(),
767
- };
768
-
769
- expect(() => promotePattern(maturity)).toThrow();
770
- });
771
- });
772
-
773
- describe("deprecatePattern", () => {
774
- it("deprecates pattern", () => {
775
- const maturity = createPatternMaturity("test");
776
-
777
- const deprecated = deprecatePattern(maturity, "Too many failures");
778
- expect(deprecated.state).toBe("deprecated");
779
- expect(deprecated.deprecated_at).toBeDefined();
780
- });
781
-
782
- it("keeps deprecated state if already deprecated", () => {
783
- const maturity: PatternMaturity = {
784
- pattern_id: "test",
785
- state: "deprecated",
786
- helpful_count: 2,
787
- harmful_count: 8,
788
- last_validated: new Date().toISOString(),
789
- deprecated_at: new Date().toISOString(),
790
- };
791
-
792
- const deprecated = deprecatePattern(maturity);
793
- expect(deprecated.state).toBe("deprecated");
794
- });
795
- });
796
-
797
- describe("getMaturityMultiplier", () => {
798
- it("returns correct multipliers for each state", () => {
799
- expect(getMaturityMultiplier("candidate")).toBe(0.5);
800
- expect(getMaturityMultiplier("established")).toBe(1.0);
801
- expect(getMaturityMultiplier("proven")).toBe(1.5);
802
- expect(getMaturityMultiplier("deprecated")).toBe(0);
803
- });
804
- });
805
-
806
- describe("formatMaturityForPrompt", () => {
807
- it("formats proven maturity info", () => {
808
- const maturity: PatternMaturity = {
809
- pattern_id: "pattern-1",
810
- state: "proven",
811
- helpful_count: 10,
812
- harmful_count: 1,
813
- last_validated: new Date().toISOString(),
814
- };
815
-
816
- const formatted = formatMaturityForPrompt(maturity);
817
-
818
- expect(formatted).toContain("PROVEN");
819
- expect(formatted).toContain("helpful");
820
- });
821
-
822
- it("formats deprecated maturity info", () => {
823
- const maturity: PatternMaturity = {
824
- pattern_id: "pattern-2",
825
- state: "deprecated",
826
- helpful_count: 2,
827
- harmful_count: 8,
828
- last_validated: new Date().toISOString(),
829
- };
830
-
831
- const formatted = formatMaturityForPrompt(maturity);
832
-
833
- expect(formatted).toContain("DEPRECATED");
834
- expect(formatted).toContain("harmful");
835
- });
836
- });
837
- });
838
-
839
- // ============================================================================
840
- // Swarm Tool Integration Tests
841
- // ============================================================================
842
-
843
- describe("Swarm Tool Integrations", () => {
844
- describe("swarm_record_outcome", () => {
845
- it("records successful outcome and generates feedback", async () => {
846
- const result = await swarm_record_outcome.execute(
847
- {
848
- bead_id: "test-bead-123",
849
- duration_ms: 60000,
850
- error_count: 0,
851
- retry_count: 0,
852
- success: true,
853
- files_touched: ["src/test.ts"],
854
- },
855
- mockContext,
856
- );
857
-
858
- const parsed = JSON.parse(result);
859
-
860
- expect(parsed.success).toBe(true);
861
- expect(parsed.outcome.scored.type).toBe("helpful");
862
- expect(parsed.feedback_events).toHaveLength(4); // Default 4 criteria
863
- expect(parsed.feedback_events[0].criterion).toBe("type_safe");
864
- });
865
-
866
- it("records failed outcome as harmful", async () => {
867
- const result = await swarm_record_outcome.execute(
868
- {
869
- bead_id: "test-bead-456",
870
- duration_ms: 3600000, // 1 hour
871
- error_count: 10,
872
- retry_count: 5,
873
- success: false,
874
- },
875
- mockContext,
876
- );
877
-
878
- const parsed = JSON.parse(result);
879
-
880
- expect(parsed.outcome.scored.type).toBe("harmful");
881
- });
882
-
883
- it("uses custom criteria when provided", async () => {
884
- const result = await swarm_record_outcome.execute(
885
- {
886
- bead_id: "test-bead-789",
887
- duration_ms: 60000,
888
- error_count: 0,
889
- retry_count: 0,
890
- success: true,
891
- criteria: ["custom_criterion"],
892
- },
893
- mockContext,
894
- );
895
-
896
- const parsed = JSON.parse(result);
897
-
898
- expect(parsed.feedback_events).toHaveLength(1);
899
- expect(parsed.feedback_events[0].criterion).toBe("custom_criterion");
900
- });
901
- });
902
-
903
- describe("detectInstructionConflicts", () => {
904
- it("detects positive/negative conflicts", () => {
905
- const subtasks = [
906
- {
907
- title: "Use React Query for state management",
908
- description: "Always use React Query",
909
- },
910
- {
911
- title: "Avoid external state libraries",
912
- description: "Never use external state libraries",
913
- },
914
- ];
915
-
916
- const conflicts = detectInstructionConflicts(subtasks);
917
-
918
- // Should detect potential conflict around "state" and "use/avoid"
919
- expect(conflicts.length).toBeGreaterThanOrEqual(0); // Heuristic may or may not catch this
920
- });
921
-
922
- it("returns empty array for non-conflicting subtasks", () => {
923
- const subtasks = [
924
- {
925
- title: "Add user authentication",
926
- description: "Implement OAuth flow",
927
- },
928
- { title: "Add API routes", description: "Create REST endpoints" },
929
- ];
930
-
931
- const conflicts = detectInstructionConflicts(subtasks);
932
-
933
- expect(conflicts).toHaveLength(0);
934
- });
935
- });
936
-
937
- describe("swarm_validate_decomposition with conflicts", () => {
938
- it("includes instruction conflicts as warnings", async () => {
939
- const decomposition = {
940
- epic: { title: "Test Epic" },
941
- subtasks: [
942
- {
943
- title: "Always use TypeScript strict mode",
944
- description: "Must enable strict mode",
945
- files: ["tsconfig.json"],
946
- dependencies: [],
947
- estimated_complexity: 1,
948
- },
949
- {
950
- title: "Avoid strict TypeScript settings",
951
- description: "Never use strict mode",
952
- files: ["src/index.ts"],
953
- dependencies: [],
954
- estimated_complexity: 1,
955
- },
956
- ],
957
- };
958
-
959
- const result = await swarm_validate_decomposition.execute(
960
- { response: JSON.stringify(decomposition) },
961
- mockContext,
962
- );
963
-
964
- const parsed = JSON.parse(result);
965
-
966
- expect(parsed.valid).toBe(true);
967
- // Warnings may or may not be present depending on heuristic
968
- if (parsed.warnings) {
969
- expect(parsed.warnings).toHaveProperty("instruction_conflicts");
970
- }
971
- });
972
- });
973
-
974
- describe("swarm_decompose with CASS integration", () => {
975
- it("includes cass_history in response", async () => {
976
- const result = await swarm_decompose.execute(
977
- {
978
- task: "Add user authentication",
979
- query_cass: true,
980
- },
981
- mockContext,
982
- );
983
-
984
- const parsed = JSON.parse(result);
985
-
986
- expect(parsed).toHaveProperty("cass_history");
987
- expect(parsed.cass_history).toHaveProperty("queried");
988
- });
989
-
990
- it("skips CASS when disabled", async () => {
991
- const result = await swarm_decompose.execute(
992
- {
993
- task: "Add user authentication",
994
- query_cass: false,
995
- },
996
- mockContext,
997
- );
998
-
999
- const parsed = JSON.parse(result);
1000
-
1001
- expect(parsed.cass_history.queried).toBe(false);
1002
- });
1003
- });
1004
- });
1005
-
1006
- // ============================================================================
1007
- // Pattern Storage Tests
1008
- // ============================================================================
1009
-
1010
- describe("InMemoryPatternStorage", () => {
1011
- let storage: InMemoryPatternStorage;
1012
-
1013
- beforeEach(() => {
1014
- storage = new InMemoryPatternStorage();
1015
- });
1016
-
1017
- it("stores and retrieves patterns", async () => {
1018
- const pattern = createPattern("Test pattern");
1019
- await storage.store(pattern);
1020
-
1021
- const retrieved = await storage.get(pattern.id);
1022
- expect(retrieved).not.toBeNull();
1023
- expect(retrieved!.content).toBe("Test pattern");
1024
- });
1025
-
1026
- it("lists all patterns", async () => {
1027
- await storage.store(createPattern("Pattern 1"));
1028
- await storage.store(createPattern("Pattern 2"));
1029
-
1030
- const all = await storage.getAll();
1031
- expect(all).toHaveLength(2);
1032
- });
1033
-
1034
- it("filters anti-patterns", async () => {
1035
- const pattern = createPattern("Good pattern");
1036
- const antiPattern = {
1037
- ...createPattern("Bad pattern"),
1038
- kind: "anti_pattern" as const,
1039
- is_negative: true,
1040
- };
1041
-
1042
- await storage.store(pattern);
1043
- await storage.store(antiPattern);
1044
-
1045
- const antiPatterns = await storage.getAntiPatterns();
1046
- expect(antiPatterns).toHaveLength(1);
1047
- expect(antiPatterns[0].content).toBe("Bad pattern");
1048
- });
1049
-
1050
- it("filters by tag", async () => {
1051
- const pattern1 = { ...createPattern("Pattern 1"), tags: ["decomposition"] };
1052
- const pattern2 = { ...createPattern("Pattern 2"), tags: ["testing"] };
1053
-
1054
- await storage.store(pattern1);
1055
- await storage.store(pattern2);
1056
-
1057
- const decompositionPatterns = await storage.getByTag("decomposition");
1058
- expect(decompositionPatterns).toHaveLength(1);
1059
- });
1060
-
1061
- it("finds patterns by content", async () => {
1062
- await storage.store(createPattern("Split by file type"));
1063
- await storage.store(createPattern("Split by component"));
1064
- await storage.store(createPattern("Sequential execution"));
1065
-
1066
- const splitPatterns = await storage.findByContent("split");
1067
- expect(splitPatterns).toHaveLength(2);
1068
- });
1069
- });
1070
-
1071
- // ============================================================================
1072
- // Maturity Storage Tests
1073
- // ============================================================================
1074
-
1075
- describe("InMemoryMaturityStorage", () => {
1076
- let storage: InMemoryMaturityStorage;
1077
-
1078
- beforeEach(() => {
1079
- storage = new InMemoryMaturityStorage();
1080
- });
1081
-
1082
- it("stores and retrieves maturity records", async () => {
1083
- const maturity: PatternMaturity = {
1084
- pattern_id: "pattern-1",
1085
- state: "candidate",
1086
- helpful_count: 0,
1087
- harmful_count: 0,
1088
- last_validated: new Date().toISOString(),
1089
- };
1090
-
1091
- await storage.store(maturity);
1092
- const retrieved = await storage.get("pattern-1");
1093
-
1094
- expect(retrieved).not.toBeNull();
1095
- expect(retrieved!.state).toBe("candidate");
1096
- });
1097
-
1098
- it("stores and retrieves feedback events", async () => {
1099
- const feedback: MaturityFeedback = {
1100
- pattern_id: "pattern-1",
1101
- type: "helpful",
1102
- timestamp: new Date().toISOString(),
1103
- weight: 1,
1104
- };
1105
-
1106
- await storage.storeFeedback(feedback);
1107
- const retrieved = await storage.getFeedback("pattern-1");
1108
-
1109
- expect(retrieved).toHaveLength(1);
1110
- expect(retrieved[0].type).toBe("helpful");
1111
- });
1112
-
1113
- it("filters feedback by pattern ID", async () => {
1114
- await storage.storeFeedback({
1115
- pattern_id: "p1",
1116
- type: "helpful",
1117
- timestamp: new Date().toISOString(),
1118
- weight: 1,
1119
- });
1120
- await storage.storeFeedback({
1121
- pattern_id: "p2",
1122
- type: "harmful",
1123
- timestamp: new Date().toISOString(),
1124
- weight: 1,
1125
- });
1126
- await storage.storeFeedback({
1127
- pattern_id: "p1",
1128
- type: "helpful",
1129
- timestamp: new Date().toISOString(),
1130
- weight: 1,
1131
- });
1132
-
1133
- const p1Feedback = await storage.getFeedback("p1");
1134
- expect(p1Feedback).toHaveLength(2);
1135
-
1136
- const p2Feedback = await storage.getFeedback("p2");
1137
- expect(p2Feedback).toHaveLength(1);
1138
- });
1139
-
1140
- it("filters by state", async () => {
1141
- await storage.store({
1142
- pattern_id: "p1",
1143
- state: "candidate",
1144
- helpful_count: 1,
1145
- harmful_count: 0,
1146
- last_validated: new Date().toISOString(),
1147
- });
1148
- await storage.store({
1149
- pattern_id: "p2",
1150
- state: "proven",
1151
- helpful_count: 10,
1152
- harmful_count: 0,
1153
- last_validated: new Date().toISOString(),
1154
- });
1155
-
1156
- const candidates = await storage.getByState("candidate");
1157
- expect(candidates).toHaveLength(1);
1158
-
1159
- const proven = await storage.getByState("proven");
1160
- expect(proven).toHaveLength(1);
1161
- });
1162
- });
1163
-
1164
- // ============================================================================
1165
- // Storage Module Tests
1166
- // ============================================================================
1167
-
1168
- import {
1169
- createStorage,
1170
- createStorageWithFallback,
1171
- getStorage,
1172
- setStorage,
1173
- resetStorage,
1174
- InMemoryStorage,
1175
- SemanticMemoryStorage,
1176
- isSemanticMemoryAvailable,
1177
- type LearningStorage,
1178
- } from "./storage";
1179
-
1180
- describe("Storage Module", () => {
1181
- describe("createStorage", () => {
1182
- it("creates InMemoryStorage when backend is memory", () => {
1183
- const storage = createStorage({ backend: "memory" });
1184
- expect(storage).toBeInstanceOf(InMemoryStorage);
1185
- });
1186
-
1187
- it("creates SemanticMemoryStorage when backend is semantic-memory", () => {
1188
- const storage = createStorage({ backend: "semantic-memory" });
1189
- expect(storage).toBeInstanceOf(SemanticMemoryStorage);
1190
- });
1191
-
1192
- it("uses semantic-memory as default backend", () => {
1193
- const storage = createStorage();
1194
- expect(storage).toBeInstanceOf(SemanticMemoryStorage);
1195
- });
1196
-
1197
- it("throws on unknown backend", () => {
1198
- expect(() => createStorage({ backend: "unknown" as any })).toThrow(
1199
- "Unknown storage backend",
1200
- );
1201
- });
1202
- });
1203
-
1204
- describe("InMemoryStorage", () => {
1205
- let storage: InMemoryStorage;
1206
-
1207
- beforeEach(() => {
1208
- storage = new InMemoryStorage();
1209
- });
1210
-
1211
- afterEach(async () => {
1212
- await storage.close();
1213
- });
1214
-
1215
- it("stores and retrieves feedback", async () => {
1216
- const event = createFeedbackEvent("type_safe", "helpful");
1217
- await storage.storeFeedback(event);
1218
-
1219
- const all = await storage.getAllFeedback();
1220
- expect(all).toHaveLength(1);
1221
- expect(all[0].id).toBe(event.id);
1222
- });
1223
-
1224
- it("retrieves feedback by criterion", async () => {
1225
- await storage.storeFeedback(createFeedbackEvent("type_safe", "helpful"));
1226
- await storage.storeFeedback(createFeedbackEvent("no_bugs", "harmful"));
1227
-
1228
- const typeSafe = await storage.getFeedbackByCriterion("type_safe");
1229
- expect(typeSafe).toHaveLength(1);
1230
- expect(typeSafe[0].criterion).toBe("type_safe");
1231
- });
1232
-
1233
- it("retrieves feedback by bead ID", async () => {
1234
- const event1 = {
1235
- ...createFeedbackEvent("type_safe", "helpful"),
1236
- bead_id: "bead-1",
1237
- };
1238
- const event2 = {
1239
- ...createFeedbackEvent("no_bugs", "harmful"),
1240
- bead_id: "bead-2",
1241
- };
1242
-
1243
- await storage.storeFeedback(event1);
1244
- await storage.storeFeedback(event2);
1245
-
1246
- const bead1Events = await storage.getFeedbackByBead("bead-1");
1247
- expect(bead1Events).toHaveLength(1);
1248
- expect(bead1Events[0].bead_id).toBe("bead-1");
1249
- });
1250
-
1251
- it("finds similar feedback (returns all in memory)", async () => {
1252
- await storage.storeFeedback(createFeedbackEvent("type_safe", "helpful"));
1253
- await storage.storeFeedback(createFeedbackEvent("no_bugs", "harmful"));
1254
-
1255
- const similar = await storage.findSimilarFeedback("type", 10);
1256
- expect(similar.length).toBeGreaterThan(0);
1257
- });
1258
-
1259
- it("stores and retrieves patterns", async () => {
1260
- const pattern = createPattern("Test pattern");
1261
- await storage.storePattern(pattern);
1262
-
1263
- const retrieved = await storage.getPattern(pattern.id);
1264
- expect(retrieved).not.toBeNull();
1265
- expect(retrieved!.content).toBe("Test pattern");
1266
- });
1267
-
1268
- it("retrieves all patterns", async () => {
1269
- await storage.storePattern(createPattern("Pattern 1"));
1270
- await storage.storePattern(createPattern("Pattern 2"));
1271
-
1272
- const all = await storage.getAllPatterns();
1273
- expect(all).toHaveLength(2);
1274
- });
1275
-
1276
- it("filters anti-patterns", async () => {
1277
- const pattern = createPattern("Good pattern");
1278
- const antiPattern = {
1279
- ...createPattern("Bad pattern"),
1280
- kind: "anti_pattern" as const,
1281
- is_negative: true,
1282
- };
1283
-
1284
- await storage.storePattern(pattern);
1285
- await storage.storePattern(antiPattern);
1286
-
1287
- const antiPatterns = await storage.getAntiPatterns();
1288
- expect(antiPatterns).toHaveLength(1);
1289
- expect(antiPatterns[0].kind).toBe("anti_pattern");
1290
- });
1291
-
1292
- it("retrieves patterns by tag", async () => {
1293
- const pattern1 = {
1294
- ...createPattern("Pattern 1"),
1295
- tags: ["decomposition"],
1296
- };
1297
- const pattern2 = { ...createPattern("Pattern 2"), tags: ["testing"] };
1298
-
1299
- await storage.storePattern(pattern1);
1300
- await storage.storePattern(pattern2);
1301
-
1302
- const decompositionPatterns =
1303
- await storage.getPatternsByTag("decomposition");
1304
- expect(decompositionPatterns).toHaveLength(1);
1305
- });
1306
-
1307
- it("finds similar patterns by content", async () => {
1308
- await storage.storePattern(createPattern("Split by file type"));
1309
- await storage.storePattern(createPattern("Split by component"));
1310
-
1311
- const similar = await storage.findSimilarPatterns("split");
1312
- expect(similar.length).toBeGreaterThan(0);
1313
- });
1314
-
1315
- it("stores and retrieves maturity", async () => {
1316
- const maturity = createPatternMaturity("pattern-1");
1317
- await storage.storeMaturity(maturity);
1318
-
1319
- const retrieved = await storage.getMaturity("pattern-1");
1320
- expect(retrieved).not.toBeNull();
1321
- expect(retrieved!.pattern_id).toBe("pattern-1");
1322
- });
1323
-
1324
- it("retrieves all maturity records", async () => {
1325
- await storage.storeMaturity(createPatternMaturity("p1"));
1326
- await storage.storeMaturity(createPatternMaturity("p2"));
1327
-
1328
- const all = await storage.getAllMaturity();
1329
- expect(all).toHaveLength(2);
1330
- });
1331
-
1332
- it("filters maturity by state", async () => {
1333
- const candidate = createPatternMaturity("p1");
1334
- const proven: PatternMaturity = {
1335
- pattern_id: "p2",
1336
- state: "proven",
1337
- helpful_count: 10,
1338
- harmful_count: 0,
1339
- last_validated: new Date().toISOString(),
1340
- };
1341
-
1342
- await storage.storeMaturity(candidate);
1343
- await storage.storeMaturity(proven);
1344
-
1345
- const candidates = await storage.getMaturityByState("candidate");
1346
- expect(candidates).toHaveLength(1);
1347
- });
1348
-
1349
- it("stores and retrieves maturity feedback", async () => {
1350
- const feedback = createMaturityFeedback("pattern-1", "helpful");
1351
- await storage.storeMaturityFeedback(feedback);
1352
-
1353
- const retrieved = await storage.getMaturityFeedback("pattern-1");
1354
- expect(retrieved).toHaveLength(1);
1355
- expect(retrieved[0].type).toBe("helpful");
1356
- });
1357
-
1358
- it("closes without error", async () => {
1359
- await expect(storage.close()).resolves.toBeUndefined();
1360
- });
1361
- });
1362
-
1363
- describe("SemanticMemoryStorage", () => {
1364
- let storage: SemanticMemoryStorage;
1365
- let isAvailable: boolean;
1366
-
1367
- beforeEach(async () => {
1368
- isAvailable = await isSemanticMemoryAvailable();
1369
- if (isAvailable) {
1370
- // Use default collections (which include TEST_SEMANTIC_MEMORY_COLLECTION suffix)
1371
- // This ensures all tests use the same isolated collections for this test run
1372
- storage = new SemanticMemoryStorage();
1373
- }
1374
- });
1375
-
1376
- afterEach(async () => {
1377
- if (storage) {
1378
- await storage.close();
1379
- }
1380
- });
1381
-
1382
- it("skips tests if semantic-memory not available", async () => {
1383
- if (!isAvailable) {
1384
- expect(isAvailable).toBe(false);
1385
- return;
1386
- }
1387
- expect(isAvailable).toBe(true);
1388
- });
1389
-
1390
- it("stores and retrieves feedback", async () => {
1391
- if (!isAvailable) return;
1392
-
1393
- const event = createFeedbackEvent("type_safe", "helpful");
1394
- await storage.storeFeedback(event);
1395
-
1396
- // Give semantic-memory time to index
1397
- await new Promise((resolve) => setTimeout(resolve, 100));
1398
-
1399
- const retrieved = await storage.getFeedbackByCriterion("type_safe");
1400
- expect(retrieved.length).toBeGreaterThan(0);
1401
- });
1402
-
1403
- it("stores and retrieves patterns", async () => {
1404
- if (!isAvailable) return;
1405
-
1406
- const pattern = createPattern("Test pattern for semantic search");
1407
- await storage.storePattern(pattern);
1408
-
1409
- // Give semantic-memory time to persist
1410
- await new Promise((resolve) => setTimeout(resolve, 100));
1411
-
1412
- const retrieved = await storage.getPattern(pattern.id);
1413
- expect(retrieved).not.toBeNull();
1414
- expect(retrieved?.id).toBe(pattern.id);
1415
- });
1416
-
1417
- it("closes without error", async () => {
1418
- if (!isAvailable) return;
1419
-
1420
- await expect(storage.close()).resolves.toBeUndefined();
1421
- });
1422
- });
1423
-
1424
- describe("createStorageWithFallback", () => {
1425
- it("returns InMemoryStorage when backend is memory", async () => {
1426
- const storage = await createStorageWithFallback({ backend: "memory" });
1427
- expect(storage).toBeInstanceOf(InMemoryStorage);
1428
- });
1429
-
1430
- it("returns appropriate backend based on availability", async () => {
1431
- const storage = await createStorageWithFallback();
1432
- const isAvailable = await isSemanticMemoryAvailable();
1433
-
1434
- if (isAvailable) {
1435
- expect(storage).toBeInstanceOf(SemanticMemoryStorage);
1436
- } else {
1437
- expect(storage).toBeInstanceOf(InMemoryStorage);
1438
- }
1439
- });
1440
- });
1441
-
1442
- describe("Global Storage Management", () => {
1443
- beforeEach(async () => {
1444
- await resetStorage();
1445
- });
1446
-
1447
- afterEach(async () => {
1448
- await resetStorage();
1449
- });
1450
-
1451
- it("getStorage returns a storage instance", async () => {
1452
- const storage = await getStorage();
1453
- expect(storage).toBeDefined();
1454
- expect(storage).toHaveProperty("storeFeedback");
1455
- expect(storage).toHaveProperty("storePattern");
1456
- expect(storage).toHaveProperty("storeMaturity");
1457
- });
1458
-
1459
- it("getStorage returns same instance on multiple calls", async () => {
1460
- const storage1 = await getStorage();
1461
- const storage2 = await getStorage();
1462
- expect(storage1).toBe(storage2);
1463
- });
1464
-
1465
- it("setStorage replaces global instance", async () => {
1466
- const customStorage = new InMemoryStorage();
1467
- setStorage(customStorage);
1468
-
1469
- const retrieved = await getStorage();
1470
- expect(retrieved).toBe(customStorage);
1471
- });
1472
-
1473
- it("resetStorage clears global instance", async () => {
1474
- const storage1 = await getStorage();
1475
- await resetStorage();
1476
- const storage2 = await getStorage();
1477
-
1478
- expect(storage1).not.toBe(storage2);
1479
- });
1480
-
1481
- it("resetStorage calls close on existing instance", async () => {
1482
- const storage = await getStorage();
1483
- const closeSpy = vi.spyOn(storage, "close");
1484
-
1485
- await resetStorage();
1486
-
1487
- expect(closeSpy).toHaveBeenCalled();
1488
- });
1489
- });
1490
-
1491
- describe("isSemanticMemoryAvailable", () => {
1492
- it("returns boolean", async () => {
1493
- const available = await isSemanticMemoryAvailable();
1494
- expect(typeof available).toBe("boolean");
1495
- });
1496
- });
1497
- });
1498
-
1499
- // ============================================================================
1500
- // 3-Strike Detection Tests
1501
- // ============================================================================
1502
-
1503
- import {
1504
- InMemoryStrikeStorage,
1505
- addStrike,
1506
- getStrikes,
1507
- isStrikedOut,
1508
- getArchitecturePrompt,
1509
- clearStrikes,
1510
- type StrikeStorage,
1511
- } from "./learning";
1512
-
1513
- describe("3-Strike Detection", () => {
1514
- let storage: StrikeStorage;
1515
-
1516
- beforeEach(() => {
1517
- storage = new InMemoryStrikeStorage();
1518
- });
1519
-
1520
- describe("addStrike", () => {
1521
- it("records first strike", async () => {
1522
- const record = await addStrike(
1523
- "test-bead-1",
1524
- "Attempted null check fix",
1525
- "Still getting undefined errors",
1526
- storage,
1527
- );
1528
-
1529
- expect(record.bead_id).toBe("test-bead-1");
1530
- expect(record.strike_count).toBe(1);
1531
- expect(record.failures).toHaveLength(1);
1532
- expect(record.failures[0].attempt).toBe("Attempted null check fix");
1533
- expect(record.failures[0].reason).toBe("Still getting undefined errors");
1534
- expect(record.first_strike_at).toBeDefined();
1535
- expect(record.last_strike_at).toBeDefined();
1536
- });
1537
-
1538
- it("increments strike count on subsequent strikes", async () => {
1539
- await addStrike("test-bead-2", "Fix 1", "Failed 1", storage);
1540
- const record2 = await addStrike(
1541
- "test-bead-2",
1542
- "Fix 2",
1543
- "Failed 2",
1544
- storage,
1545
- );
1546
-
1547
- expect(record2.strike_count).toBe(2);
1548
- expect(record2.failures).toHaveLength(2);
1549
- });
1550
-
1551
- it("caps strike count at 3", async () => {
1552
- await addStrike("test-bead-3", "Fix 1", "Failed 1", storage);
1553
- await addStrike("test-bead-3", "Fix 2", "Failed 2", storage);
1554
- await addStrike("test-bead-3", "Fix 3", "Failed 3", storage);
1555
- const record4 = await addStrike(
1556
- "test-bead-3",
1557
- "Fix 4",
1558
- "Failed 4",
1559
- storage,
1560
- );
1561
-
1562
- expect(record4.strike_count).toBe(3);
1563
- expect(record4.failures).toHaveLength(4); // Records all attempts
1564
- });
1565
-
1566
- it("preserves first_strike_at timestamp", async () => {
1567
- const record1 = await addStrike(
1568
- "test-bead-4",
1569
- "Fix 1",
1570
- "Failed 1",
1571
- storage,
1572
- );
1573
- // Capture the timestamps from first strike
1574
- const firstStrikeAt = record1.first_strike_at;
1575
- const firstLastStrikeAt = record1.last_strike_at;
1576
-
1577
- // Wait to ensure different timestamp
1578
- await new Promise((resolve) => setTimeout(resolve, 10));
1579
-
1580
- const record2 = await addStrike(
1581
- "test-bead-4",
1582
- "Fix 2",
1583
- "Failed 2",
1584
- storage,
1585
- );
1586
-
1587
- // first_strike_at should be preserved from first call
1588
- expect(record2.first_strike_at).toBe(firstStrikeAt);
1589
- // last_strike_at should be updated (different from first call's last_strike_at)
1590
- expect(record2.last_strike_at).not.toBe(firstLastStrikeAt);
1591
- });
1592
- });
1593
-
1594
- describe("getStrikes", () => {
1595
- it("returns 0 for bead with no strikes", async () => {
1596
- const count = await getStrikes("no-strikes-bead", storage);
1597
- expect(count).toBe(0);
1598
- });
1599
-
1600
- it("returns correct strike count", async () => {
1601
- await addStrike("bead-with-strikes", "Fix 1", "Failed 1", storage);
1602
- await addStrike("bead-with-strikes", "Fix 2", "Failed 2", storage);
1603
-
1604
- const count = await getStrikes("bead-with-strikes", storage);
1605
- expect(count).toBe(2);
1606
- });
1607
- });
1608
-
1609
- describe("isStrikedOut", () => {
1610
- it("returns false for bead with < 3 strikes", async () => {
1611
- await addStrike("bead-safe", "Fix 1", "Failed 1", storage);
1612
- await addStrike("bead-safe", "Fix 2", "Failed 2", storage);
1613
-
1614
- const strikedOut = await isStrikedOut("bead-safe", storage);
1615
- expect(strikedOut).toBe(false);
1616
- });
1617
-
1618
- it("returns true for bead with 3 strikes", async () => {
1619
- await addStrike("bead-danger", "Fix 1", "Failed 1", storage);
1620
- await addStrike("bead-danger", "Fix 2", "Failed 2", storage);
1621
- await addStrike("bead-danger", "Fix 3", "Failed 3", storage);
1622
-
1623
- const strikedOut = await isStrikedOut("bead-danger", storage);
1624
- expect(strikedOut).toBe(true);
1625
- });
1626
-
1627
- it("returns false for bead with no strikes", async () => {
1628
- const strikedOut = await isStrikedOut("no-record", storage);
1629
- expect(strikedOut).toBe(false);
1630
- });
1631
- });
1632
-
1633
- describe("getArchitecturePrompt", () => {
1634
- it("returns empty string for bead with < 3 strikes", async () => {
1635
- await addStrike("bead-prompt-1", "Fix 1", "Failed 1", storage);
1636
-
1637
- const prompt = await getArchitecturePrompt("bead-prompt-1", storage);
1638
- expect(prompt).toBe("");
1639
- });
1640
-
1641
- it("returns empty string for bead with no strikes", async () => {
1642
- const prompt = await getArchitecturePrompt("no-strikes", storage);
1643
- expect(prompt).toBe("");
1644
- });
1645
-
1646
- it("generates architecture review prompt for struck out bead", async () => {
1647
- await addStrike(
1648
- "bead-prompt-2",
1649
- "Added null checks",
1650
- "Still crashes on undefined",
1651
- storage,
1652
- );
1653
- await addStrike(
1654
- "bead-prompt-2",
1655
- "Used optional chaining",
1656
- "Runtime error persists",
1657
- storage,
1658
- );
1659
- await addStrike(
1660
- "bead-prompt-2",
1661
- "Wrapped in try-catch",
1662
- "Error still happening",
1663
- storage,
1664
- );
1665
-
1666
- const prompt = await getArchitecturePrompt("bead-prompt-2", storage);
1667
-
1668
- expect(prompt).toContain("Architecture Review Required");
1669
- expect(prompt).toContain("bead-prompt-2");
1670
- expect(prompt).toContain("Added null checks");
1671
- expect(prompt).toContain("Still crashes on undefined");
1672
- expect(prompt).toContain("Used optional chaining");
1673
- expect(prompt).toContain("Runtime error persists");
1674
- expect(prompt).toContain("Wrapped in try-catch");
1675
- expect(prompt).toContain("Error still happening");
1676
- expect(prompt).toContain("architectural problem");
1677
- expect(prompt).toContain("DO NOT attempt Fix #4");
1678
- expect(prompt).toContain("Refactor architecture");
1679
- expect(prompt).toContain("Continue with Fix #4");
1680
- expect(prompt).toContain("Abandon this approach");
1681
- });
1682
-
1683
- it("lists all failures in order", async () => {
1684
- await addStrike(
1685
- "bead-prompt-3",
1686
- "First attempt",
1687
- "First failure",
1688
- storage,
1689
- );
1690
- await addStrike(
1691
- "bead-prompt-3",
1692
- "Second attempt",
1693
- "Second failure",
1694
- storage,
1695
- );
1696
- await addStrike(
1697
- "bead-prompt-3",
1698
- "Third attempt",
1699
- "Third failure",
1700
- storage,
1701
- );
1702
-
1703
- const prompt = await getArchitecturePrompt("bead-prompt-3", storage);
1704
-
1705
- const lines = prompt.split("\n");
1706
- const failureLine1 = lines.find((l) => l.includes("First attempt"));
1707
- const failureLine2 = lines.find((l) => l.includes("Second attempt"));
1708
- const failureLine3 = lines.find((l) => l.includes("Third attempt"));
1709
-
1710
- expect(failureLine1).toBeDefined();
1711
- expect(failureLine2).toBeDefined();
1712
- expect(failureLine3).toBeDefined();
1713
-
1714
- // Check ordering
1715
- const idx1 = lines.indexOf(failureLine1!);
1716
- const idx2 = lines.indexOf(failureLine2!);
1717
- const idx3 = lines.indexOf(failureLine3!);
1718
-
1719
- expect(idx1).toBeLessThan(idx2);
1720
- expect(idx2).toBeLessThan(idx3);
1721
- });
1722
- });
1723
-
1724
- describe("clearStrikes", () => {
1725
- it("clears strikes for a bead", async () => {
1726
- await addStrike("bead-clear", "Fix 1", "Failed 1", storage);
1727
- await addStrike("bead-clear", "Fix 2", "Failed 2", storage);
1728
-
1729
- expect(await getStrikes("bead-clear", storage)).toBe(2);
1730
-
1731
- await clearStrikes("bead-clear", storage);
1732
-
1733
- expect(await getStrikes("bead-clear", storage)).toBe(0);
1734
- expect(await isStrikedOut("bead-clear", storage)).toBe(false);
1735
- });
1736
-
1737
- it("handles clearing non-existent bead gracefully", async () => {
1738
- await expect(clearStrikes("no-bead", storage)).resolves.toBeUndefined();
1739
- });
1740
- });
1741
-
1742
- describe("InMemoryStrikeStorage", () => {
1743
- it("stores and retrieves strike records", async () => {
1744
- const storage = new InMemoryStrikeStorage();
1745
- const record = await addStrike("bead-1", "Fix", "Failed", storage);
1746
-
1747
- const retrieved = await storage.get("bead-1");
1748
- expect(retrieved).not.toBeNull();
1749
- expect(retrieved!.bead_id).toBe("bead-1");
1750
- expect(retrieved!.strike_count).toBe(1);
1751
- });
1752
-
1753
- it("returns null for non-existent bead", async () => {
1754
- const storage = new InMemoryStrikeStorage();
1755
- const retrieved = await storage.get("non-existent");
1756
- expect(retrieved).toBeNull();
1757
- });
1758
-
1759
- it("lists all strike records", async () => {
1760
- const storage = new InMemoryStrikeStorage();
1761
- await addStrike("bead-1", "Fix", "Failed", storage);
1762
- await addStrike("bead-2", "Fix", "Failed", storage);
1763
-
1764
- const all = await storage.getAll();
1765
- expect(all).toHaveLength(2);
1766
- });
1767
-
1768
- it("clears specific bead strikes", async () => {
1769
- const storage = new InMemoryStrikeStorage();
1770
- await addStrike("bead-1", "Fix", "Failed", storage);
1771
- await addStrike("bead-2", "Fix", "Failed", storage);
1772
-
1773
- await storage.clear("bead-1");
1774
-
1775
- expect(await storage.get("bead-1")).toBeNull();
1776
- expect(await storage.get("bead-2")).not.toBeNull();
1777
- });
1778
- });
1779
-
1780
- describe("3-Strike Rule Integration", () => {
1781
- it("follows complete workflow from no strikes to architecture review", async () => {
1782
- const beadId = "integration-bead";
1783
-
1784
- // Start: No strikes
1785
- expect(await getStrikes(beadId, storage)).toBe(0);
1786
- expect(await isStrikedOut(beadId, storage)).toBe(false);
1787
- expect(await getArchitecturePrompt(beadId, storage)).toBe("");
1788
-
1789
- // Strike 1
1790
- await addStrike(beadId, "Tried approach A", "Didn't work", storage);
1791
- expect(await getStrikes(beadId, storage)).toBe(1);
1792
- expect(await isStrikedOut(beadId, storage)).toBe(false);
1793
-
1794
- // Strike 2
1795
- await addStrike(beadId, "Tried approach B", "Also failed", storage);
1796
- expect(await getStrikes(beadId, storage)).toBe(2);
1797
- expect(await isStrikedOut(beadId, storage)).toBe(false);
1798
-
1799
- // Strike 3 - STRUCK OUT
1800
- await addStrike(beadId, "Tried approach C", "Still broken", storage);
1801
- expect(await getStrikes(beadId, storage)).toBe(3);
1802
- expect(await isStrikedOut(beadId, storage)).toBe(true);
1803
-
1804
- // Architecture prompt should now be available
1805
- const prompt = await getArchitecturePrompt(beadId, storage);
1806
- expect(prompt).not.toBe("");
1807
- expect(prompt).toContain("Architecture Review Required");
1808
-
1809
- // Clear strikes (e.g., after human intervention)
1810
- await clearStrikes(beadId, storage);
1811
- expect(await getStrikes(beadId, storage)).toBe(0);
1812
- expect(await isStrikedOut(beadId, storage)).toBe(false);
1813
- });
1814
- });
1815
- });