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.
- package/bin/swarm.serve.test.ts +6 -4
- package/bin/swarm.ts +16 -10
- package/dist/compaction-prompt-scoring.js +139 -0
- package/dist/eval-capture.js +12811 -0
- package/dist/hive.d.ts.map +1 -1
- package/dist/index.js +7644 -62599
- package/dist/plugin.js +23766 -78721
- package/dist/swarm-orchestrate.d.ts.map +1 -1
- package/dist/swarm-prompts.d.ts.map +1 -1
- package/dist/swarm-review.d.ts.map +1 -1
- package/package.json +17 -5
- package/.changeset/swarm-insights-data-layer.md +0 -63
- package/.hive/analysis/eval-failure-analysis-2025-12-25.md +0 -331
- package/.hive/analysis/session-data-quality-audit.md +0 -320
- package/.hive/eval-results.json +0 -483
- package/.hive/issues.jsonl +0 -138
- package/.hive/memories.jsonl +0 -729
- package/.opencode/eval-history.jsonl +0 -327
- package/.turbo/turbo-build.log +0 -9
- package/CHANGELOG.md +0 -2286
- package/SCORER-ANALYSIS.md +0 -598
- package/docs/analysis/subagent-coordination-patterns.md +0 -902
- package/docs/analysis-socratic-planner-pattern.md +0 -504
- package/docs/planning/ADR-001-monorepo-structure.md +0 -171
- package/docs/planning/ADR-002-package-extraction.md +0 -393
- package/docs/planning/ADR-003-performance-improvements.md +0 -451
- package/docs/planning/ADR-004-message-queue-features.md +0 -187
- package/docs/planning/ADR-005-devtools-observability.md +0 -202
- package/docs/planning/ADR-007-swarm-enhancements-worktree-review.md +0 -168
- package/docs/planning/ADR-008-worker-handoff-protocol.md +0 -293
- package/docs/planning/ADR-009-oh-my-opencode-patterns.md +0 -353
- package/docs/planning/ADR-010-cass-inhousing.md +0 -1215
- package/docs/planning/ROADMAP.md +0 -368
- package/docs/semantic-memory-cli-syntax.md +0 -123
- package/docs/swarm-mail-architecture.md +0 -1147
- package/docs/testing/context-recovery-test.md +0 -470
- package/evals/ARCHITECTURE.md +0 -1189
- package/evals/README.md +0 -768
- package/evals/compaction-prompt.eval.ts +0 -149
- package/evals/compaction-resumption.eval.ts +0 -289
- package/evals/coordinator-behavior.eval.ts +0 -307
- package/evals/coordinator-session.eval.ts +0 -154
- package/evals/evalite.config.ts.bak +0 -15
- package/evals/example.eval.ts +0 -31
- package/evals/fixtures/cass-baseline.ts +0 -217
- package/evals/fixtures/compaction-cases.ts +0 -350
- package/evals/fixtures/compaction-prompt-cases.ts +0 -311
- package/evals/fixtures/coordinator-sessions.ts +0 -328
- package/evals/fixtures/decomposition-cases.ts +0 -105
- package/evals/lib/compaction-loader.test.ts +0 -248
- package/evals/lib/compaction-loader.ts +0 -320
- package/evals/lib/data-loader.evalite-test.ts +0 -289
- package/evals/lib/data-loader.test.ts +0 -345
- package/evals/lib/data-loader.ts +0 -281
- package/evals/lib/llm.ts +0 -115
- package/evals/scorers/compaction-prompt-scorers.ts +0 -145
- package/evals/scorers/compaction-scorers.ts +0 -305
- package/evals/scorers/coordinator-discipline.evalite-test.ts +0 -539
- package/evals/scorers/coordinator-discipline.ts +0 -325
- package/evals/scorers/index.test.ts +0 -146
- package/evals/scorers/index.ts +0 -328
- package/evals/scorers/outcome-scorers.evalite-test.ts +0 -27
- package/evals/scorers/outcome-scorers.ts +0 -349
- package/evals/swarm-decomposition.eval.ts +0 -121
- package/examples/commands/swarm.md +0 -745
- package/examples/plugin-wrapper-template.ts +0 -2515
- package/examples/skills/hive-workflow/SKILL.md +0 -212
- package/examples/skills/skill-creator/SKILL.md +0 -223
- package/examples/skills/swarm-coordination/SKILL.md +0 -292
- package/global-skills/cli-builder/SKILL.md +0 -344
- package/global-skills/cli-builder/references/advanced-patterns.md +0 -244
- package/global-skills/learning-systems/SKILL.md +0 -644
- package/global-skills/skill-creator/LICENSE.txt +0 -202
- package/global-skills/skill-creator/SKILL.md +0 -352
- package/global-skills/skill-creator/references/output-patterns.md +0 -82
- package/global-skills/skill-creator/references/workflows.md +0 -28
- package/global-skills/swarm-coordination/SKILL.md +0 -995
- package/global-skills/swarm-coordination/references/coordinator-patterns.md +0 -235
- package/global-skills/swarm-coordination/references/strategies.md +0 -138
- package/global-skills/system-design/SKILL.md +0 -213
- package/global-skills/testing-patterns/SKILL.md +0 -430
- package/global-skills/testing-patterns/references/dependency-breaking-catalog.md +0 -586
- package/opencode-swarm-plugin-0.30.7.tgz +0 -0
- package/opencode-swarm-plugin-0.31.0.tgz +0 -0
- package/scripts/cleanup-test-memories.ts +0 -346
- package/scripts/init-skill.ts +0 -222
- package/scripts/migrate-unknown-sessions.ts +0 -349
- package/scripts/validate-skill.ts +0 -204
- package/src/agent-mail.ts +0 -1724
- package/src/anti-patterns.test.ts +0 -1167
- package/src/anti-patterns.ts +0 -448
- package/src/compaction-capture.integration.test.ts +0 -257
- package/src/compaction-hook.test.ts +0 -838
- package/src/compaction-hook.ts +0 -1204
- package/src/compaction-observability.integration.test.ts +0 -139
- package/src/compaction-observability.test.ts +0 -187
- package/src/compaction-observability.ts +0 -324
- package/src/compaction-prompt-scorers.test.ts +0 -475
- package/src/compaction-prompt-scoring.ts +0 -300
- package/src/contributor-tools.test.ts +0 -133
- package/src/contributor-tools.ts +0 -201
- package/src/dashboard.test.ts +0 -611
- package/src/dashboard.ts +0 -462
- package/src/error-enrichment.test.ts +0 -403
- package/src/error-enrichment.ts +0 -219
- package/src/eval-capture.test.ts +0 -1015
- package/src/eval-capture.ts +0 -929
- package/src/eval-gates.test.ts +0 -306
- package/src/eval-gates.ts +0 -218
- package/src/eval-history.test.ts +0 -508
- package/src/eval-history.ts +0 -214
- package/src/eval-learning.test.ts +0 -378
- package/src/eval-learning.ts +0 -360
- package/src/eval-runner.test.ts +0 -223
- package/src/eval-runner.ts +0 -402
- package/src/export-tools.test.ts +0 -476
- package/src/export-tools.ts +0 -257
- package/src/hive.integration.test.ts +0 -2241
- package/src/hive.ts +0 -1628
- package/src/index.ts +0 -940
- package/src/learning.integration.test.ts +0 -1815
- package/src/learning.ts +0 -1079
- package/src/logger.test.ts +0 -189
- package/src/logger.ts +0 -135
- package/src/mandate-promotion.test.ts +0 -473
- package/src/mandate-promotion.ts +0 -239
- package/src/mandate-storage.integration.test.ts +0 -601
- package/src/mandate-storage.test.ts +0 -578
- package/src/mandate-storage.ts +0 -794
- package/src/mandates.ts +0 -540
- package/src/memory-tools.test.ts +0 -195
- package/src/memory-tools.ts +0 -344
- package/src/memory.integration.test.ts +0 -334
- package/src/memory.test.ts +0 -158
- package/src/memory.ts +0 -527
- package/src/model-selection.test.ts +0 -188
- package/src/model-selection.ts +0 -68
- package/src/observability-tools.test.ts +0 -359
- package/src/observability-tools.ts +0 -871
- package/src/output-guardrails.test.ts +0 -438
- package/src/output-guardrails.ts +0 -381
- package/src/pattern-maturity.test.ts +0 -1160
- package/src/pattern-maturity.ts +0 -525
- package/src/planning-guardrails.test.ts +0 -491
- package/src/planning-guardrails.ts +0 -438
- package/src/plugin.ts +0 -23
- package/src/post-compaction-tracker.test.ts +0 -251
- package/src/post-compaction-tracker.ts +0 -237
- package/src/query-tools.test.ts +0 -636
- package/src/query-tools.ts +0 -324
- package/src/rate-limiter.integration.test.ts +0 -466
- package/src/rate-limiter.ts +0 -774
- package/src/replay-tools.test.ts +0 -496
- package/src/replay-tools.ts +0 -240
- package/src/repo-crawl.integration.test.ts +0 -441
- package/src/repo-crawl.ts +0 -610
- package/src/schemas/cell-events.test.ts +0 -347
- package/src/schemas/cell-events.ts +0 -807
- package/src/schemas/cell.ts +0 -257
- package/src/schemas/evaluation.ts +0 -166
- package/src/schemas/index.test.ts +0 -199
- package/src/schemas/index.ts +0 -286
- package/src/schemas/mandate.ts +0 -232
- package/src/schemas/swarm-context.ts +0 -115
- package/src/schemas/task.ts +0 -161
- package/src/schemas/worker-handoff.test.ts +0 -302
- package/src/schemas/worker-handoff.ts +0 -131
- package/src/sessions/agent-discovery.test.ts +0 -137
- package/src/sessions/agent-discovery.ts +0 -112
- package/src/sessions/index.ts +0 -15
- package/src/skills.integration.test.ts +0 -1192
- package/src/skills.test.ts +0 -643
- package/src/skills.ts +0 -1549
- package/src/storage.integration.test.ts +0 -341
- package/src/storage.ts +0 -884
- package/src/structured.integration.test.ts +0 -817
- package/src/structured.test.ts +0 -1046
- package/src/structured.ts +0 -762
- package/src/swarm-decompose.test.ts +0 -188
- package/src/swarm-decompose.ts +0 -1302
- package/src/swarm-deferred.integration.test.ts +0 -157
- package/src/swarm-deferred.test.ts +0 -38
- package/src/swarm-insights.test.ts +0 -214
- package/src/swarm-insights.ts +0 -459
- package/src/swarm-mail.integration.test.ts +0 -970
- package/src/swarm-mail.ts +0 -739
- package/src/swarm-orchestrate.integration.test.ts +0 -282
- package/src/swarm-orchestrate.test.ts +0 -548
- package/src/swarm-orchestrate.ts +0 -3084
- package/src/swarm-prompts.test.ts +0 -1270
- package/src/swarm-prompts.ts +0 -2077
- package/src/swarm-research.integration.test.ts +0 -701
- package/src/swarm-research.test.ts +0 -698
- package/src/swarm-research.ts +0 -472
- package/src/swarm-review.integration.test.ts +0 -285
- package/src/swarm-review.test.ts +0 -879
- package/src/swarm-review.ts +0 -709
- package/src/swarm-strategies.ts +0 -407
- package/src/swarm-worktree.test.ts +0 -501
- package/src/swarm-worktree.ts +0 -575
- package/src/swarm.integration.test.ts +0 -2377
- package/src/swarm.ts +0 -38
- package/src/tool-adapter.integration.test.ts +0 -1221
- package/src/tool-availability.ts +0 -461
- package/tsconfig.json +0 -28
|
@@ -1,1167 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for anti-pattern learning module
|
|
3
|
-
*/
|
|
4
|
-
import { describe, expect, it } from "vitest";
|
|
5
|
-
import {
|
|
6
|
-
DEFAULT_ANTI_PATTERN_CONFIG,
|
|
7
|
-
DecompositionPatternSchema,
|
|
8
|
-
InMemoryPatternStorage,
|
|
9
|
-
PatternInversionResultSchema,
|
|
10
|
-
PatternKindSchema,
|
|
11
|
-
createPattern,
|
|
12
|
-
extractPatternsFromDescription,
|
|
13
|
-
formatAntiPatternsForPrompt,
|
|
14
|
-
formatSuccessfulPatternsForPrompt,
|
|
15
|
-
invertToAntiPattern,
|
|
16
|
-
recordPatternObservation,
|
|
17
|
-
shouldInvertPattern,
|
|
18
|
-
type AntiPatternConfig,
|
|
19
|
-
type DecompositionPattern,
|
|
20
|
-
} from "./anti-patterns";
|
|
21
|
-
|
|
22
|
-
// ============================================================================
|
|
23
|
-
// Schema Validation Tests
|
|
24
|
-
// ============================================================================
|
|
25
|
-
|
|
26
|
-
describe("PatternKindSchema", () => {
|
|
27
|
-
it("validates 'pattern' kind", () => {
|
|
28
|
-
expect(() => PatternKindSchema.parse("pattern")).not.toThrow();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it("validates 'anti_pattern' kind", () => {
|
|
32
|
-
expect(() => PatternKindSchema.parse("anti_pattern")).not.toThrow();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it("rejects invalid kind", () => {
|
|
36
|
-
expect(() => PatternKindSchema.parse("invalid")).toThrow();
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
describe("DecompositionPatternSchema", () => {
|
|
41
|
-
it("validates a complete valid pattern", () => {
|
|
42
|
-
const pattern = {
|
|
43
|
-
id: "pattern-123",
|
|
44
|
-
content: "Split by file type",
|
|
45
|
-
kind: "pattern",
|
|
46
|
-
is_negative: false,
|
|
47
|
-
success_count: 5,
|
|
48
|
-
failure_count: 2,
|
|
49
|
-
created_at: new Date().toISOString(),
|
|
50
|
-
updated_at: new Date().toISOString(),
|
|
51
|
-
example_beads: ["bd-123", "bd-456"],
|
|
52
|
-
tags: ["file-splitting"],
|
|
53
|
-
reason: "Test pattern",
|
|
54
|
-
};
|
|
55
|
-
expect(() => DecompositionPatternSchema.parse(pattern)).not.toThrow();
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it("validates a valid anti-pattern", () => {
|
|
59
|
-
const antiPattern = {
|
|
60
|
-
id: "anti-pattern-123",
|
|
61
|
-
content: "AVOID: Split by file type",
|
|
62
|
-
kind: "anti_pattern",
|
|
63
|
-
is_negative: true,
|
|
64
|
-
success_count: 2,
|
|
65
|
-
failure_count: 8,
|
|
66
|
-
created_at: new Date().toISOString(),
|
|
67
|
-
updated_at: new Date().toISOString(),
|
|
68
|
-
example_beads: [],
|
|
69
|
-
tags: [],
|
|
70
|
-
};
|
|
71
|
-
expect(() => DecompositionPatternSchema.parse(antiPattern)).not.toThrow();
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("applies default values for optional fields", () => {
|
|
75
|
-
const minimal = {
|
|
76
|
-
id: "pattern-minimal",
|
|
77
|
-
content: "Test pattern",
|
|
78
|
-
kind: "pattern",
|
|
79
|
-
is_negative: false,
|
|
80
|
-
created_at: new Date().toISOString(),
|
|
81
|
-
updated_at: new Date().toISOString(),
|
|
82
|
-
};
|
|
83
|
-
const parsed = DecompositionPatternSchema.parse(minimal);
|
|
84
|
-
expect(parsed.success_count).toBe(0);
|
|
85
|
-
expect(parsed.failure_count).toBe(0);
|
|
86
|
-
expect(parsed.tags).toEqual([]);
|
|
87
|
-
expect(parsed.example_beads).toEqual([]);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it("rejects negative success_count", () => {
|
|
91
|
-
const pattern = {
|
|
92
|
-
id: "pattern-invalid",
|
|
93
|
-
content: "Test",
|
|
94
|
-
kind: "pattern",
|
|
95
|
-
is_negative: false,
|
|
96
|
-
success_count: -1,
|
|
97
|
-
failure_count: 0,
|
|
98
|
-
created_at: new Date().toISOString(),
|
|
99
|
-
updated_at: new Date().toISOString(),
|
|
100
|
-
};
|
|
101
|
-
expect(() => DecompositionPatternSchema.parse(pattern)).toThrow();
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
it("rejects negative failure_count", () => {
|
|
105
|
-
const pattern = {
|
|
106
|
-
id: "pattern-invalid",
|
|
107
|
-
content: "Test",
|
|
108
|
-
kind: "pattern",
|
|
109
|
-
is_negative: false,
|
|
110
|
-
success_count: 0,
|
|
111
|
-
failure_count: -1,
|
|
112
|
-
created_at: new Date().toISOString(),
|
|
113
|
-
updated_at: new Date().toISOString(),
|
|
114
|
-
};
|
|
115
|
-
expect(() => DecompositionPatternSchema.parse(pattern)).toThrow();
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it("rejects invalid kind", () => {
|
|
119
|
-
const pattern = {
|
|
120
|
-
id: "pattern-invalid",
|
|
121
|
-
content: "Test",
|
|
122
|
-
kind: "invalid_kind",
|
|
123
|
-
is_negative: false,
|
|
124
|
-
success_count: 0,
|
|
125
|
-
failure_count: 0,
|
|
126
|
-
created_at: new Date().toISOString(),
|
|
127
|
-
updated_at: new Date().toISOString(),
|
|
128
|
-
};
|
|
129
|
-
expect(() => DecompositionPatternSchema.parse(pattern)).toThrow();
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
describe("PatternInversionResultSchema", () => {
|
|
134
|
-
it("validates a complete inversion result", () => {
|
|
135
|
-
const now = new Date().toISOString();
|
|
136
|
-
const result = {
|
|
137
|
-
original: {
|
|
138
|
-
id: "pattern-123",
|
|
139
|
-
content: "Split by file type",
|
|
140
|
-
kind: "pattern",
|
|
141
|
-
is_negative: false,
|
|
142
|
-
success_count: 2,
|
|
143
|
-
failure_count: 8,
|
|
144
|
-
created_at: now,
|
|
145
|
-
updated_at: now,
|
|
146
|
-
tags: [],
|
|
147
|
-
example_beads: [],
|
|
148
|
-
},
|
|
149
|
-
inverted: {
|
|
150
|
-
id: "anti-pattern-123",
|
|
151
|
-
content: "AVOID: Split by file type",
|
|
152
|
-
kind: "anti_pattern",
|
|
153
|
-
is_negative: true,
|
|
154
|
-
success_count: 2,
|
|
155
|
-
failure_count: 8,
|
|
156
|
-
created_at: now,
|
|
157
|
-
updated_at: now,
|
|
158
|
-
tags: [],
|
|
159
|
-
example_beads: [],
|
|
160
|
-
},
|
|
161
|
-
reason: "Failed 8/10 times (80% failure rate)",
|
|
162
|
-
};
|
|
163
|
-
expect(() => PatternInversionResultSchema.parse(result)).not.toThrow();
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
// ============================================================================
|
|
168
|
-
// shouldInvertPattern Tests
|
|
169
|
-
// ============================================================================
|
|
170
|
-
|
|
171
|
-
describe("shouldInvertPattern", () => {
|
|
172
|
-
const basePattern: DecompositionPattern = {
|
|
173
|
-
id: "pattern-test",
|
|
174
|
-
content: "Test pattern",
|
|
175
|
-
kind: "pattern",
|
|
176
|
-
is_negative: false,
|
|
177
|
-
success_count: 0,
|
|
178
|
-
failure_count: 0,
|
|
179
|
-
created_at: new Date().toISOString(),
|
|
180
|
-
updated_at: new Date().toISOString(),
|
|
181
|
-
tags: [],
|
|
182
|
-
example_beads: [],
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
it("returns true when failure rate exceeds 60%", () => {
|
|
186
|
-
const pattern = {
|
|
187
|
-
...basePattern,
|
|
188
|
-
success_count: 3,
|
|
189
|
-
failure_count: 7, // 70% failure rate
|
|
190
|
-
};
|
|
191
|
-
expect(shouldInvertPattern(pattern)).toBe(true);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it("returns true when failure rate equals 60%", () => {
|
|
195
|
-
const pattern = {
|
|
196
|
-
...basePattern,
|
|
197
|
-
success_count: 4,
|
|
198
|
-
failure_count: 6, // Exactly 60% failure rate
|
|
199
|
-
};
|
|
200
|
-
expect(shouldInvertPattern(pattern)).toBe(true);
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
it("returns false when failure rate is below 60%", () => {
|
|
204
|
-
const pattern = {
|
|
205
|
-
...basePattern,
|
|
206
|
-
success_count: 6,
|
|
207
|
-
failure_count: 4, // 40% failure rate
|
|
208
|
-
};
|
|
209
|
-
expect(shouldInvertPattern(pattern)).toBe(false);
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
it("returns false when failure rate is just below threshold", () => {
|
|
213
|
-
const pattern = {
|
|
214
|
-
...basePattern,
|
|
215
|
-
success_count: 41,
|
|
216
|
-
failure_count: 59, // 59% failure rate (just below 60%)
|
|
217
|
-
};
|
|
218
|
-
expect(shouldInvertPattern(pattern)).toBe(false);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
it("returns false with insufficient observations (< minObservations)", () => {
|
|
222
|
-
const pattern = {
|
|
223
|
-
...basePattern,
|
|
224
|
-
success_count: 0,
|
|
225
|
-
failure_count: 2, // Only 2 observations, need 3
|
|
226
|
-
};
|
|
227
|
-
expect(shouldInvertPattern(pattern)).toBe(false);
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
it("returns false when exactly at minObservations but low failure rate", () => {
|
|
231
|
-
const pattern = {
|
|
232
|
-
...basePattern,
|
|
233
|
-
success_count: 2,
|
|
234
|
-
failure_count: 1, // Exactly 3 observations, 33% failure
|
|
235
|
-
};
|
|
236
|
-
expect(shouldInvertPattern(pattern)).toBe(false);
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
it("returns true when at minObservations with high failure rate", () => {
|
|
240
|
-
const pattern = {
|
|
241
|
-
...basePattern,
|
|
242
|
-
success_count: 1,
|
|
243
|
-
failure_count: 2, // Exactly 3 observations, 67% failure
|
|
244
|
-
};
|
|
245
|
-
expect(shouldInvertPattern(pattern)).toBe(true);
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
it("returns false when already an anti-pattern", () => {
|
|
249
|
-
const antiPattern = {
|
|
250
|
-
...basePattern,
|
|
251
|
-
kind: "anti_pattern" as const,
|
|
252
|
-
is_negative: true,
|
|
253
|
-
success_count: 0,
|
|
254
|
-
failure_count: 10, // 100% failure but already anti-pattern
|
|
255
|
-
};
|
|
256
|
-
expect(shouldInvertPattern(antiPattern)).toBe(false);
|
|
257
|
-
});
|
|
258
|
-
|
|
259
|
-
it("returns false with zero observations", () => {
|
|
260
|
-
const pattern = {
|
|
261
|
-
...basePattern,
|
|
262
|
-
success_count: 0,
|
|
263
|
-
failure_count: 0,
|
|
264
|
-
};
|
|
265
|
-
expect(shouldInvertPattern(pattern)).toBe(false);
|
|
266
|
-
});
|
|
267
|
-
|
|
268
|
-
it("respects custom config minObservations", () => {
|
|
269
|
-
const pattern = {
|
|
270
|
-
...basePattern,
|
|
271
|
-
success_count: 1,
|
|
272
|
-
failure_count: 4, // 80% failure
|
|
273
|
-
};
|
|
274
|
-
const config: AntiPatternConfig = {
|
|
275
|
-
...DEFAULT_ANTI_PATTERN_CONFIG,
|
|
276
|
-
minObservations: 5,
|
|
277
|
-
};
|
|
278
|
-
expect(shouldInvertPattern(pattern, config)).toBe(true);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it("respects custom config failureRatioThreshold", () => {
|
|
282
|
-
const pattern = {
|
|
283
|
-
...basePattern,
|
|
284
|
-
success_count: 3,
|
|
285
|
-
failure_count: 7, // 70% failure
|
|
286
|
-
};
|
|
287
|
-
const config: AntiPatternConfig = {
|
|
288
|
-
...DEFAULT_ANTI_PATTERN_CONFIG,
|
|
289
|
-
failureRatioThreshold: 0.8, // Need 80% failure
|
|
290
|
-
};
|
|
291
|
-
expect(shouldInvertPattern(pattern, config)).toBe(false);
|
|
292
|
-
});
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
// ============================================================================
|
|
296
|
-
// invertToAntiPattern Tests
|
|
297
|
-
// ============================================================================
|
|
298
|
-
|
|
299
|
-
describe("invertToAntiPattern", () => {
|
|
300
|
-
const basePattern: DecompositionPattern = {
|
|
301
|
-
id: "pattern-123",
|
|
302
|
-
content: "Split by file type",
|
|
303
|
-
kind: "pattern",
|
|
304
|
-
is_negative: false,
|
|
305
|
-
success_count: 2,
|
|
306
|
-
failure_count: 8,
|
|
307
|
-
created_at: new Date().toISOString(),
|
|
308
|
-
updated_at: new Date().toISOString(),
|
|
309
|
-
tags: ["file-splitting"],
|
|
310
|
-
example_beads: ["bd-123", "bd-456"],
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
it("converts pattern to anti-pattern with correct kind", () => {
|
|
314
|
-
const result = invertToAntiPattern(basePattern, "Test reason");
|
|
315
|
-
expect(result.inverted.kind).toBe("anti_pattern");
|
|
316
|
-
expect(result.inverted.is_negative).toBe(true);
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
it("prefixes content with AVOID:", () => {
|
|
320
|
-
const result = invertToAntiPattern(basePattern, "Test reason");
|
|
321
|
-
expect(result.inverted.content).toContain("AVOID:");
|
|
322
|
-
expect(result.inverted.content).toContain("Split by file type");
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
it("appends reason to content", () => {
|
|
326
|
-
const result = invertToAntiPattern(basePattern, "Failed too many times");
|
|
327
|
-
expect(result.inverted.content).toContain("Failed too many times");
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
it("preserves success and failure counts", () => {
|
|
331
|
-
const result = invertToAntiPattern(basePattern, "Test");
|
|
332
|
-
expect(result.inverted.success_count).toBe(basePattern.success_count);
|
|
333
|
-
expect(result.inverted.failure_count).toBe(basePattern.failure_count);
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
it("preserves example_beads", () => {
|
|
337
|
-
const result = invertToAntiPattern(basePattern, "Test");
|
|
338
|
-
expect(result.inverted.example_beads).toEqual(["bd-123", "bd-456"]);
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
it("preserves tags", () => {
|
|
342
|
-
const result = invertToAntiPattern(basePattern, "Test");
|
|
343
|
-
expect(result.inverted.tags).toEqual(["file-splitting"]);
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
it("generates new ID with 'anti-' prefix", () => {
|
|
347
|
-
const result = invertToAntiPattern(basePattern, "Test");
|
|
348
|
-
expect(result.inverted.id).toBe("anti-pattern-123");
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
it("stores reason in inverted pattern", () => {
|
|
352
|
-
const result = invertToAntiPattern(basePattern, "Custom reason");
|
|
353
|
-
expect(result.inverted.reason).toBe("Custom reason");
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
it("updates updated_at timestamp", () => {
|
|
357
|
-
const before = new Date();
|
|
358
|
-
const result = invertToAntiPattern(basePattern, "Test");
|
|
359
|
-
const after = new Date();
|
|
360
|
-
const updatedAt = new Date(result.inverted.updated_at);
|
|
361
|
-
expect(updatedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
362
|
-
expect(updatedAt.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
it("returns original pattern in result", () => {
|
|
366
|
-
const result = invertToAntiPattern(basePattern, "Test");
|
|
367
|
-
expect(result.original).toEqual(basePattern);
|
|
368
|
-
});
|
|
369
|
-
|
|
370
|
-
it("returns reason in result", () => {
|
|
371
|
-
const result = invertToAntiPattern(basePattern, "Test reason");
|
|
372
|
-
expect(result.reason).toBe("Test reason");
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
it("cleans existing AVOID: prefix", () => {
|
|
376
|
-
const pattern = {
|
|
377
|
-
...basePattern,
|
|
378
|
-
content: "AVOID: Split by file type",
|
|
379
|
-
};
|
|
380
|
-
const result = invertToAntiPattern(pattern, "Test");
|
|
381
|
-
// Should not have double prefix
|
|
382
|
-
expect(result.inverted.content).toMatch(/^AVOID: Split by file type\./);
|
|
383
|
-
expect(result.inverted.content).not.toMatch(/AVOID:.*AVOID:/);
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
it("cleans existing DO NOT: prefix", () => {
|
|
387
|
-
const pattern = {
|
|
388
|
-
...basePattern,
|
|
389
|
-
content: "DO NOT: Split by file type",
|
|
390
|
-
};
|
|
391
|
-
const result = invertToAntiPattern(pattern, "Test");
|
|
392
|
-
expect(result.inverted.content).toMatch(/^AVOID: Split by file type\./);
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
it("cleans existing NEVER: prefix", () => {
|
|
396
|
-
const pattern = {
|
|
397
|
-
...basePattern,
|
|
398
|
-
content: "NEVER: Split by file type",
|
|
399
|
-
};
|
|
400
|
-
const result = invertToAntiPattern(pattern, "Test");
|
|
401
|
-
expect(result.inverted.content).toMatch(/^AVOID: Split by file type\./);
|
|
402
|
-
});
|
|
403
|
-
|
|
404
|
-
it("respects custom antiPatternPrefix", () => {
|
|
405
|
-
const config: AntiPatternConfig = {
|
|
406
|
-
...DEFAULT_ANTI_PATTERN_CONFIG,
|
|
407
|
-
antiPatternPrefix: "DO NOT: ",
|
|
408
|
-
};
|
|
409
|
-
const result = invertToAntiPattern(basePattern, "Test", config);
|
|
410
|
-
expect(result.inverted.content).toContain("DO NOT:");
|
|
411
|
-
});
|
|
412
|
-
});
|
|
413
|
-
|
|
414
|
-
// ============================================================================
|
|
415
|
-
// recordPatternObservation Tests
|
|
416
|
-
// ============================================================================
|
|
417
|
-
|
|
418
|
-
describe("recordPatternObservation", () => {
|
|
419
|
-
const basePattern: DecompositionPattern = {
|
|
420
|
-
id: "pattern-test",
|
|
421
|
-
content: "Test pattern",
|
|
422
|
-
kind: "pattern",
|
|
423
|
-
is_negative: false,
|
|
424
|
-
success_count: 5,
|
|
425
|
-
failure_count: 2,
|
|
426
|
-
created_at: new Date().toISOString(),
|
|
427
|
-
updated_at: new Date().toISOString(),
|
|
428
|
-
tags: [],
|
|
429
|
-
example_beads: [],
|
|
430
|
-
};
|
|
431
|
-
|
|
432
|
-
it("increments success count on success", () => {
|
|
433
|
-
const result = recordPatternObservation(basePattern, true);
|
|
434
|
-
expect(result.pattern.success_count).toBe(6);
|
|
435
|
-
expect(result.pattern.failure_count).toBe(2);
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
it("increments failure count on failure", () => {
|
|
439
|
-
const result = recordPatternObservation(basePattern, false);
|
|
440
|
-
expect(result.pattern.success_count).toBe(5);
|
|
441
|
-
expect(result.pattern.failure_count).toBe(3);
|
|
442
|
-
});
|
|
443
|
-
|
|
444
|
-
it("adds bead to example_beads when provided", () => {
|
|
445
|
-
const result = recordPatternObservation(basePattern, true, "bd-789");
|
|
446
|
-
expect(result.pattern.example_beads).toContain("bd-789");
|
|
447
|
-
});
|
|
448
|
-
|
|
449
|
-
it("does not modify example_beads when beadId not provided", () => {
|
|
450
|
-
const result = recordPatternObservation(basePattern, true);
|
|
451
|
-
expect(result.pattern.example_beads).toEqual([]);
|
|
452
|
-
});
|
|
453
|
-
|
|
454
|
-
it("limits example_beads to MAX_EXAMPLE_BEADS (10)", () => {
|
|
455
|
-
const pattern = {
|
|
456
|
-
...basePattern,
|
|
457
|
-
example_beads: Array(10)
|
|
458
|
-
.fill(0)
|
|
459
|
-
.map((_, i) => `bd-${i}`),
|
|
460
|
-
};
|
|
461
|
-
const result = recordPatternObservation(pattern, true, "bd-new");
|
|
462
|
-
expect(result.pattern.example_beads.length).toBe(10);
|
|
463
|
-
expect(result.pattern.example_beads).toContain("bd-new");
|
|
464
|
-
expect(result.pattern.example_beads).not.toContain("bd-0"); // Oldest removed
|
|
465
|
-
});
|
|
466
|
-
|
|
467
|
-
it("keeps newest beads when trimming example_beads", () => {
|
|
468
|
-
const pattern = {
|
|
469
|
-
...basePattern,
|
|
470
|
-
example_beads: [
|
|
471
|
-
"bd-1",
|
|
472
|
-
"bd-2",
|
|
473
|
-
"bd-3",
|
|
474
|
-
"bd-4",
|
|
475
|
-
"bd-5",
|
|
476
|
-
"bd-6",
|
|
477
|
-
"bd-7",
|
|
478
|
-
"bd-8",
|
|
479
|
-
"bd-9",
|
|
480
|
-
"bd-10",
|
|
481
|
-
],
|
|
482
|
-
};
|
|
483
|
-
const result = recordPatternObservation(pattern, true, "bd-new");
|
|
484
|
-
expect(result.pattern.example_beads[0]).toBe("bd-2"); // First one removed
|
|
485
|
-
expect(result.pattern.example_beads[9]).toBe("bd-new"); // New one added
|
|
486
|
-
});
|
|
487
|
-
|
|
488
|
-
it("updates updated_at timestamp", () => {
|
|
489
|
-
const before = new Date();
|
|
490
|
-
const result = recordPatternObservation(basePattern, true);
|
|
491
|
-
const after = new Date();
|
|
492
|
-
const updatedAt = new Date(result.pattern.updated_at);
|
|
493
|
-
expect(updatedAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
494
|
-
expect(updatedAt.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
495
|
-
});
|
|
496
|
-
|
|
497
|
-
it("does not invert when below threshold", () => {
|
|
498
|
-
const result = recordPatternObservation(basePattern, false); // 5 success, 3 failure = 37.5%
|
|
499
|
-
expect(result.inversion).toBeUndefined();
|
|
500
|
-
});
|
|
501
|
-
|
|
502
|
-
it("inverts when crossing threshold", () => {
|
|
503
|
-
const pattern = {
|
|
504
|
-
...basePattern,
|
|
505
|
-
success_count: 2,
|
|
506
|
-
failure_count: 4, // Currently 66% failure
|
|
507
|
-
};
|
|
508
|
-
const result = recordPatternObservation(pattern, false); // Now 2/7 = 71% failure
|
|
509
|
-
expect(result.inversion).toBeDefined();
|
|
510
|
-
if (result.inversion) {
|
|
511
|
-
expect(result.inversion.inverted.kind).toBe("anti_pattern");
|
|
512
|
-
}
|
|
513
|
-
});
|
|
514
|
-
|
|
515
|
-
it("includes failure statistics in inversion reason", () => {
|
|
516
|
-
const pattern = {
|
|
517
|
-
...basePattern,
|
|
518
|
-
success_count: 3,
|
|
519
|
-
failure_count: 6, // 66% failure
|
|
520
|
-
};
|
|
521
|
-
const result = recordPatternObservation(pattern, false); // 70% failure
|
|
522
|
-
expect(result.inversion).toBeDefined();
|
|
523
|
-
if (result.inversion) {
|
|
524
|
-
expect(result.inversion.reason).toContain("7/10");
|
|
525
|
-
expect(result.inversion.reason).toContain("70%");
|
|
526
|
-
}
|
|
527
|
-
});
|
|
528
|
-
|
|
529
|
-
it("does not invert already-inverted anti-patterns", () => {
|
|
530
|
-
const antiPattern: DecompositionPattern = {
|
|
531
|
-
...basePattern,
|
|
532
|
-
kind: "anti_pattern",
|
|
533
|
-
is_negative: true,
|
|
534
|
-
success_count: 0,
|
|
535
|
-
failure_count: 10,
|
|
536
|
-
};
|
|
537
|
-
const result = recordPatternObservation(antiPattern, false);
|
|
538
|
-
expect(result.inversion).toBeUndefined();
|
|
539
|
-
});
|
|
540
|
-
|
|
541
|
-
it("respects custom config for inversion", () => {
|
|
542
|
-
const pattern = {
|
|
543
|
-
...basePattern,
|
|
544
|
-
success_count: 2,
|
|
545
|
-
failure_count: 3, // 60% failure
|
|
546
|
-
};
|
|
547
|
-
const config: AntiPatternConfig = {
|
|
548
|
-
...DEFAULT_ANTI_PATTERN_CONFIG,
|
|
549
|
-
failureRatioThreshold: 0.7, // Need 70%
|
|
550
|
-
};
|
|
551
|
-
const result = recordPatternObservation(pattern, false, undefined, config);
|
|
552
|
-
expect(result.inversion).toBeUndefined(); // 66% not enough
|
|
553
|
-
});
|
|
554
|
-
|
|
555
|
-
it("preserves original pattern fields", () => {
|
|
556
|
-
const result = recordPatternObservation(basePattern, true);
|
|
557
|
-
expect(result.pattern.id).toBe(basePattern.id);
|
|
558
|
-
expect(result.pattern.content).toBe(basePattern.content);
|
|
559
|
-
expect(result.pattern.kind).toBe(basePattern.kind);
|
|
560
|
-
expect(result.pattern.tags).toEqual(basePattern.tags);
|
|
561
|
-
});
|
|
562
|
-
});
|
|
563
|
-
|
|
564
|
-
// ============================================================================
|
|
565
|
-
// extractPatternsFromDescription Tests
|
|
566
|
-
// ============================================================================
|
|
567
|
-
|
|
568
|
-
describe("extractPatternsFromDescription", () => {
|
|
569
|
-
it("detects 'split by file type' pattern", () => {
|
|
570
|
-
const patterns = extractPatternsFromDescription("Split by file type");
|
|
571
|
-
expect(patterns).toContain("Split by file type");
|
|
572
|
-
});
|
|
573
|
-
|
|
574
|
-
it("detects 'splitting by file type' variant", () => {
|
|
575
|
-
const patterns = extractPatternsFromDescription("Splitting by file type");
|
|
576
|
-
expect(patterns).toContain("Split by file type");
|
|
577
|
-
});
|
|
578
|
-
|
|
579
|
-
it("detects 'split by component' pattern", () => {
|
|
580
|
-
const patterns = extractPatternsFromDescription("Split by component");
|
|
581
|
-
expect(patterns).toContain("Split by component");
|
|
582
|
-
});
|
|
583
|
-
|
|
584
|
-
it("detects 'split by layer' pattern", () => {
|
|
585
|
-
const patterns = extractPatternsFromDescription("Split by layer");
|
|
586
|
-
expect(patterns).toContain("Split by layer (UI/logic/data)");
|
|
587
|
-
});
|
|
588
|
-
|
|
589
|
-
it("detects 'split by feature' pattern", () => {
|
|
590
|
-
const patterns = extractPatternsFromDescription("Split by feature");
|
|
591
|
-
expect(patterns).toContain("Split by feature");
|
|
592
|
-
});
|
|
593
|
-
|
|
594
|
-
it("detects 'one file per task' pattern", () => {
|
|
595
|
-
const patterns = extractPatternsFromDescription("One file per task");
|
|
596
|
-
expect(patterns).toContain("One file per subtask");
|
|
597
|
-
});
|
|
598
|
-
|
|
599
|
-
it("detects 'shared types first' pattern", () => {
|
|
600
|
-
const patterns = extractPatternsFromDescription("shared types first");
|
|
601
|
-
expect(patterns).toContain("Handle shared types first");
|
|
602
|
-
});
|
|
603
|
-
|
|
604
|
-
it("detects 'API routes separate' pattern", () => {
|
|
605
|
-
const patterns = extractPatternsFromDescription("API routes separate");
|
|
606
|
-
expect(patterns).toContain("Separate API routes");
|
|
607
|
-
});
|
|
608
|
-
|
|
609
|
-
it("detects 'tests with code' pattern", () => {
|
|
610
|
-
const patterns = extractPatternsFromDescription("tests with code");
|
|
611
|
-
expect(patterns).toContain("Tests alongside implementation");
|
|
612
|
-
});
|
|
613
|
-
|
|
614
|
-
it("detects 'tests in separate subtask' pattern", () => {
|
|
615
|
-
const patterns = extractPatternsFromDescription(
|
|
616
|
-
"tests in separate subtask",
|
|
617
|
-
);
|
|
618
|
-
expect(patterns).toContain("Tests in separate subtask");
|
|
619
|
-
});
|
|
620
|
-
|
|
621
|
-
it("detects 'parallelize all' pattern", () => {
|
|
622
|
-
const patterns = extractPatternsFromDescription("Parallelize everything");
|
|
623
|
-
expect(patterns).toContain("Maximize parallelization");
|
|
624
|
-
});
|
|
625
|
-
|
|
626
|
-
it("detects 'sequential order' pattern", () => {
|
|
627
|
-
const patterns = extractPatternsFromDescription("Sequential execution");
|
|
628
|
-
expect(patterns).toContain("Sequential execution order");
|
|
629
|
-
});
|
|
630
|
-
|
|
631
|
-
it("detects 'dependency chain' pattern", () => {
|
|
632
|
-
const patterns = extractPatternsFromDescription("dependency chain");
|
|
633
|
-
expect(patterns).toContain("Respect dependency chain");
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
it("returns empty array for unrecognized descriptions", () => {
|
|
637
|
-
const patterns = extractPatternsFromDescription("random gibberish text");
|
|
638
|
-
expect(patterns).toEqual([]);
|
|
639
|
-
});
|
|
640
|
-
|
|
641
|
-
it("detects multiple patterns in one description", () => {
|
|
642
|
-
const patterns = extractPatternsFromDescription(
|
|
643
|
-
"Split by file type and handle shared types first",
|
|
644
|
-
);
|
|
645
|
-
expect(patterns).toContain("Split by file type");
|
|
646
|
-
expect(patterns).toContain("Handle shared types first");
|
|
647
|
-
expect(patterns.length).toBeGreaterThanOrEqual(2);
|
|
648
|
-
});
|
|
649
|
-
|
|
650
|
-
it("is case-insensitive", () => {
|
|
651
|
-
const patterns = extractPatternsFromDescription("SPLIT BY FILE TYPE");
|
|
652
|
-
expect(patterns).toContain("Split by file type");
|
|
653
|
-
});
|
|
654
|
-
|
|
655
|
-
it("handles partial matches in longer sentences", () => {
|
|
656
|
-
const patterns = extractPatternsFromDescription(
|
|
657
|
-
"We should split by component for this refactor",
|
|
658
|
-
);
|
|
659
|
-
expect(patterns).toContain("Split by component");
|
|
660
|
-
});
|
|
661
|
-
});
|
|
662
|
-
|
|
663
|
-
// ============================================================================
|
|
664
|
-
// createPattern Tests
|
|
665
|
-
// ============================================================================
|
|
666
|
-
|
|
667
|
-
describe("createPattern", () => {
|
|
668
|
-
it("creates pattern with provided content", () => {
|
|
669
|
-
const pattern = createPattern("Test pattern");
|
|
670
|
-
expect(pattern.content).toBe("Test pattern");
|
|
671
|
-
});
|
|
672
|
-
|
|
673
|
-
it("creates pattern with kind='pattern'", () => {
|
|
674
|
-
const pattern = createPattern("Test");
|
|
675
|
-
expect(pattern.kind).toBe("pattern");
|
|
676
|
-
});
|
|
677
|
-
|
|
678
|
-
it("creates pattern with is_negative=false", () => {
|
|
679
|
-
const pattern = createPattern("Test");
|
|
680
|
-
expect(pattern.is_negative).toBe(false);
|
|
681
|
-
});
|
|
682
|
-
|
|
683
|
-
it("initializes counts to zero", () => {
|
|
684
|
-
const pattern = createPattern("Test");
|
|
685
|
-
expect(pattern.success_count).toBe(0);
|
|
686
|
-
expect(pattern.failure_count).toBe(0);
|
|
687
|
-
});
|
|
688
|
-
|
|
689
|
-
it("includes provided tags", () => {
|
|
690
|
-
const pattern = createPattern("Test", ["tag1", "tag2"]);
|
|
691
|
-
expect(pattern.tags).toEqual(["tag1", "tag2"]);
|
|
692
|
-
});
|
|
693
|
-
|
|
694
|
-
it("defaults to empty tags array", () => {
|
|
695
|
-
const pattern = createPattern("Test");
|
|
696
|
-
expect(pattern.tags).toEqual([]);
|
|
697
|
-
});
|
|
698
|
-
|
|
699
|
-
it("generates unique ID", () => {
|
|
700
|
-
const p1 = createPattern("Test");
|
|
701
|
-
const p2 = createPattern("Test");
|
|
702
|
-
expect(p1.id).not.toBe(p2.id);
|
|
703
|
-
});
|
|
704
|
-
|
|
705
|
-
it("sets created_at timestamp", () => {
|
|
706
|
-
const before = new Date();
|
|
707
|
-
const pattern = createPattern("Test");
|
|
708
|
-
const after = new Date();
|
|
709
|
-
const createdAt = new Date(pattern.created_at);
|
|
710
|
-
expect(createdAt.getTime()).toBeGreaterThanOrEqual(before.getTime());
|
|
711
|
-
expect(createdAt.getTime()).toBeLessThanOrEqual(after.getTime());
|
|
712
|
-
});
|
|
713
|
-
|
|
714
|
-
it("sets updated_at equal to created_at", () => {
|
|
715
|
-
const pattern = createPattern("Test");
|
|
716
|
-
expect(pattern.updated_at).toBe(pattern.created_at);
|
|
717
|
-
});
|
|
718
|
-
|
|
719
|
-
it("initializes example_beads to empty array", () => {
|
|
720
|
-
const pattern = createPattern("Test");
|
|
721
|
-
expect(pattern.example_beads).toEqual([]);
|
|
722
|
-
});
|
|
723
|
-
});
|
|
724
|
-
|
|
725
|
-
// ============================================================================
|
|
726
|
-
// formatAntiPatternsForPrompt Tests
|
|
727
|
-
// ============================================================================
|
|
728
|
-
|
|
729
|
-
describe("formatAntiPatternsForPrompt", () => {
|
|
730
|
-
it("formats anti-patterns with header", () => {
|
|
731
|
-
const patterns: DecompositionPattern[] = [
|
|
732
|
-
{
|
|
733
|
-
id: "anti-1",
|
|
734
|
-
content: "AVOID: Split by file type",
|
|
735
|
-
kind: "anti_pattern",
|
|
736
|
-
is_negative: true,
|
|
737
|
-
success_count: 0,
|
|
738
|
-
failure_count: 10,
|
|
739
|
-
created_at: new Date().toISOString(),
|
|
740
|
-
updated_at: new Date().toISOString(),
|
|
741
|
-
tags: [],
|
|
742
|
-
example_beads: [],
|
|
743
|
-
},
|
|
744
|
-
];
|
|
745
|
-
const formatted = formatAntiPatternsForPrompt(patterns);
|
|
746
|
-
expect(formatted).toContain("## Anti-Patterns to Avoid");
|
|
747
|
-
expect(formatted).toContain("AVOID: Split by file type");
|
|
748
|
-
});
|
|
749
|
-
|
|
750
|
-
it("filters out non-anti-patterns", () => {
|
|
751
|
-
const patterns: DecompositionPattern[] = [
|
|
752
|
-
{
|
|
753
|
-
id: "pattern-1",
|
|
754
|
-
content: "Good pattern",
|
|
755
|
-
kind: "pattern",
|
|
756
|
-
is_negative: false,
|
|
757
|
-
success_count: 10,
|
|
758
|
-
failure_count: 0,
|
|
759
|
-
created_at: new Date().toISOString(),
|
|
760
|
-
updated_at: new Date().toISOString(),
|
|
761
|
-
tags: [],
|
|
762
|
-
example_beads: [],
|
|
763
|
-
},
|
|
764
|
-
{
|
|
765
|
-
id: "anti-1",
|
|
766
|
-
content: "AVOID: Bad pattern",
|
|
767
|
-
kind: "anti_pattern",
|
|
768
|
-
is_negative: true,
|
|
769
|
-
success_count: 0,
|
|
770
|
-
failure_count: 10,
|
|
771
|
-
created_at: new Date().toISOString(),
|
|
772
|
-
updated_at: new Date().toISOString(),
|
|
773
|
-
tags: [],
|
|
774
|
-
example_beads: [],
|
|
775
|
-
},
|
|
776
|
-
];
|
|
777
|
-
const formatted = formatAntiPatternsForPrompt(patterns);
|
|
778
|
-
expect(formatted).toContain("AVOID: Bad pattern");
|
|
779
|
-
expect(formatted).not.toContain("Good pattern");
|
|
780
|
-
});
|
|
781
|
-
|
|
782
|
-
it("returns empty string when no anti-patterns", () => {
|
|
783
|
-
const patterns: DecompositionPattern[] = [
|
|
784
|
-
{
|
|
785
|
-
id: "pattern-1",
|
|
786
|
-
content: "Good pattern",
|
|
787
|
-
kind: "pattern",
|
|
788
|
-
is_negative: false,
|
|
789
|
-
success_count: 10,
|
|
790
|
-
failure_count: 0,
|
|
791
|
-
created_at: new Date().toISOString(),
|
|
792
|
-
updated_at: new Date().toISOString(),
|
|
793
|
-
tags: [],
|
|
794
|
-
example_beads: [],
|
|
795
|
-
},
|
|
796
|
-
];
|
|
797
|
-
const formatted = formatAntiPatternsForPrompt(patterns);
|
|
798
|
-
expect(formatted).toBe("");
|
|
799
|
-
});
|
|
800
|
-
|
|
801
|
-
it("returns empty string for empty array", () => {
|
|
802
|
-
const formatted = formatAntiPatternsForPrompt([]);
|
|
803
|
-
expect(formatted).toBe("");
|
|
804
|
-
});
|
|
805
|
-
|
|
806
|
-
it("formats multiple anti-patterns", () => {
|
|
807
|
-
const patterns: DecompositionPattern[] = [
|
|
808
|
-
{
|
|
809
|
-
id: "anti-1",
|
|
810
|
-
content: "AVOID: Pattern 1",
|
|
811
|
-
kind: "anti_pattern",
|
|
812
|
-
is_negative: true,
|
|
813
|
-
success_count: 0,
|
|
814
|
-
failure_count: 5,
|
|
815
|
-
created_at: new Date().toISOString(),
|
|
816
|
-
updated_at: new Date().toISOString(),
|
|
817
|
-
tags: [],
|
|
818
|
-
example_beads: [],
|
|
819
|
-
},
|
|
820
|
-
{
|
|
821
|
-
id: "anti-2",
|
|
822
|
-
content: "AVOID: Pattern 2",
|
|
823
|
-
kind: "anti_pattern",
|
|
824
|
-
is_negative: true,
|
|
825
|
-
success_count: 1,
|
|
826
|
-
failure_count: 9,
|
|
827
|
-
created_at: new Date().toISOString(),
|
|
828
|
-
updated_at: new Date().toISOString(),
|
|
829
|
-
tags: [],
|
|
830
|
-
example_beads: [],
|
|
831
|
-
},
|
|
832
|
-
];
|
|
833
|
-
const formatted = formatAntiPatternsForPrompt(patterns);
|
|
834
|
-
expect(formatted).toContain("AVOID: Pattern 1");
|
|
835
|
-
expect(formatted).toContain("AVOID: Pattern 2");
|
|
836
|
-
});
|
|
837
|
-
});
|
|
838
|
-
|
|
839
|
-
// ============================================================================
|
|
840
|
-
// formatSuccessfulPatternsForPrompt Tests
|
|
841
|
-
// ============================================================================
|
|
842
|
-
|
|
843
|
-
describe("formatSuccessfulPatternsForPrompt", () => {
|
|
844
|
-
it("filters patterns below minSuccessRate", () => {
|
|
845
|
-
const patterns: DecompositionPattern[] = [
|
|
846
|
-
{
|
|
847
|
-
id: "pattern-1",
|
|
848
|
-
content: "Good pattern",
|
|
849
|
-
kind: "pattern",
|
|
850
|
-
is_negative: false,
|
|
851
|
-
success_count: 8,
|
|
852
|
-
failure_count: 2, // 80% success
|
|
853
|
-
created_at: new Date().toISOString(),
|
|
854
|
-
updated_at: new Date().toISOString(),
|
|
855
|
-
tags: [],
|
|
856
|
-
example_beads: [],
|
|
857
|
-
},
|
|
858
|
-
{
|
|
859
|
-
id: "pattern-2",
|
|
860
|
-
content: "Bad pattern",
|
|
861
|
-
kind: "pattern",
|
|
862
|
-
is_negative: false,
|
|
863
|
-
success_count: 5,
|
|
864
|
-
failure_count: 5, // 50% success
|
|
865
|
-
created_at: new Date().toISOString(),
|
|
866
|
-
updated_at: new Date().toISOString(),
|
|
867
|
-
tags: [],
|
|
868
|
-
example_beads: [],
|
|
869
|
-
},
|
|
870
|
-
];
|
|
871
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns, 0.7);
|
|
872
|
-
expect(formatted).toContain("Good pattern");
|
|
873
|
-
expect(formatted).not.toContain("Bad pattern");
|
|
874
|
-
});
|
|
875
|
-
|
|
876
|
-
it("includes success rate percentage in output", () => {
|
|
877
|
-
const patterns: DecompositionPattern[] = [
|
|
878
|
-
{
|
|
879
|
-
id: "pattern-1",
|
|
880
|
-
content: "Test pattern",
|
|
881
|
-
kind: "pattern",
|
|
882
|
-
is_negative: false,
|
|
883
|
-
success_count: 8,
|
|
884
|
-
failure_count: 2, // 80%
|
|
885
|
-
created_at: new Date().toISOString(),
|
|
886
|
-
updated_at: new Date().toISOString(),
|
|
887
|
-
tags: [],
|
|
888
|
-
example_beads: [],
|
|
889
|
-
},
|
|
890
|
-
];
|
|
891
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns);
|
|
892
|
-
expect(formatted).toContain("80% success rate");
|
|
893
|
-
});
|
|
894
|
-
|
|
895
|
-
it("filters out anti-patterns", () => {
|
|
896
|
-
const patterns: DecompositionPattern[] = [
|
|
897
|
-
{
|
|
898
|
-
id: "anti-1",
|
|
899
|
-
content: "AVOID: Bad",
|
|
900
|
-
kind: "anti_pattern",
|
|
901
|
-
is_negative: true,
|
|
902
|
-
success_count: 10,
|
|
903
|
-
failure_count: 0,
|
|
904
|
-
created_at: new Date().toISOString(),
|
|
905
|
-
updated_at: new Date().toISOString(),
|
|
906
|
-
tags: [],
|
|
907
|
-
example_beads: [],
|
|
908
|
-
},
|
|
909
|
-
];
|
|
910
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns);
|
|
911
|
-
expect(formatted).toBe("");
|
|
912
|
-
});
|
|
913
|
-
|
|
914
|
-
it("filters out patterns with < 2 total observations", () => {
|
|
915
|
-
const patterns: DecompositionPattern[] = [
|
|
916
|
-
{
|
|
917
|
-
id: "pattern-1",
|
|
918
|
-
content: "Not enough data",
|
|
919
|
-
kind: "pattern",
|
|
920
|
-
is_negative: false,
|
|
921
|
-
success_count: 1,
|
|
922
|
-
failure_count: 0, // Only 1 observation
|
|
923
|
-
created_at: new Date().toISOString(),
|
|
924
|
-
updated_at: new Date().toISOString(),
|
|
925
|
-
tags: [],
|
|
926
|
-
example_beads: [],
|
|
927
|
-
},
|
|
928
|
-
];
|
|
929
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns, 0.7);
|
|
930
|
-
expect(formatted).toBe("");
|
|
931
|
-
});
|
|
932
|
-
|
|
933
|
-
it("returns empty string when no qualifying patterns", () => {
|
|
934
|
-
const patterns: DecompositionPattern[] = [
|
|
935
|
-
{
|
|
936
|
-
id: "pattern-1",
|
|
937
|
-
content: "Low success",
|
|
938
|
-
kind: "pattern",
|
|
939
|
-
is_negative: false,
|
|
940
|
-
success_count: 1,
|
|
941
|
-
failure_count: 9, // 10% success
|
|
942
|
-
created_at: new Date().toISOString(),
|
|
943
|
-
updated_at: new Date().toISOString(),
|
|
944
|
-
tags: [],
|
|
945
|
-
example_beads: [],
|
|
946
|
-
},
|
|
947
|
-
];
|
|
948
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns, 0.7);
|
|
949
|
-
expect(formatted).toBe("");
|
|
950
|
-
});
|
|
951
|
-
|
|
952
|
-
it("returns empty string for empty array", () => {
|
|
953
|
-
const formatted = formatSuccessfulPatternsForPrompt([]);
|
|
954
|
-
expect(formatted).toBe("");
|
|
955
|
-
});
|
|
956
|
-
|
|
957
|
-
it("uses default minSuccessRate of 0.7", () => {
|
|
958
|
-
const patterns: DecompositionPattern[] = [
|
|
959
|
-
{
|
|
960
|
-
id: "pattern-1",
|
|
961
|
-
content: "69% success",
|
|
962
|
-
kind: "pattern",
|
|
963
|
-
is_negative: false,
|
|
964
|
-
success_count: 69,
|
|
965
|
-
failure_count: 31, // Just below 70%
|
|
966
|
-
created_at: new Date().toISOString(),
|
|
967
|
-
updated_at: new Date().toISOString(),
|
|
968
|
-
tags: [],
|
|
969
|
-
example_beads: [],
|
|
970
|
-
},
|
|
971
|
-
];
|
|
972
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns);
|
|
973
|
-
expect(formatted).toBe(""); // Should be filtered out
|
|
974
|
-
});
|
|
975
|
-
|
|
976
|
-
it("respects custom minSuccessRate", () => {
|
|
977
|
-
const patterns: DecompositionPattern[] = [
|
|
978
|
-
{
|
|
979
|
-
id: "pattern-1",
|
|
980
|
-
content: "60% success",
|
|
981
|
-
kind: "pattern",
|
|
982
|
-
is_negative: false,
|
|
983
|
-
success_count: 6,
|
|
984
|
-
failure_count: 4,
|
|
985
|
-
created_at: new Date().toISOString(),
|
|
986
|
-
updated_at: new Date().toISOString(),
|
|
987
|
-
tags: [],
|
|
988
|
-
example_beads: [],
|
|
989
|
-
},
|
|
990
|
-
];
|
|
991
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns, 0.5);
|
|
992
|
-
expect(formatted).toContain("60% success");
|
|
993
|
-
});
|
|
994
|
-
|
|
995
|
-
it("formats multiple successful patterns", () => {
|
|
996
|
-
const patterns: DecompositionPattern[] = [
|
|
997
|
-
{
|
|
998
|
-
id: "pattern-1",
|
|
999
|
-
content: "Pattern A",
|
|
1000
|
-
kind: "pattern",
|
|
1001
|
-
is_negative: false,
|
|
1002
|
-
success_count: 8,
|
|
1003
|
-
failure_count: 2,
|
|
1004
|
-
created_at: new Date().toISOString(),
|
|
1005
|
-
updated_at: new Date().toISOString(),
|
|
1006
|
-
tags: [],
|
|
1007
|
-
example_beads: [],
|
|
1008
|
-
},
|
|
1009
|
-
{
|
|
1010
|
-
id: "pattern-2",
|
|
1011
|
-
content: "Pattern B",
|
|
1012
|
-
kind: "pattern",
|
|
1013
|
-
is_negative: false,
|
|
1014
|
-
success_count: 7,
|
|
1015
|
-
failure_count: 3,
|
|
1016
|
-
created_at: new Date().toISOString(),
|
|
1017
|
-
updated_at: new Date().toISOString(),
|
|
1018
|
-
tags: [],
|
|
1019
|
-
example_beads: [],
|
|
1020
|
-
},
|
|
1021
|
-
];
|
|
1022
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns);
|
|
1023
|
-
expect(formatted).toContain("Pattern A");
|
|
1024
|
-
expect(formatted).toContain("Pattern B");
|
|
1025
|
-
});
|
|
1026
|
-
|
|
1027
|
-
it("includes header when patterns exist", () => {
|
|
1028
|
-
const patterns: DecompositionPattern[] = [
|
|
1029
|
-
{
|
|
1030
|
-
id: "pattern-1",
|
|
1031
|
-
content: "Test",
|
|
1032
|
-
kind: "pattern",
|
|
1033
|
-
is_negative: false,
|
|
1034
|
-
success_count: 8,
|
|
1035
|
-
failure_count: 2,
|
|
1036
|
-
created_at: new Date().toISOString(),
|
|
1037
|
-
updated_at: new Date().toISOString(),
|
|
1038
|
-
tags: [],
|
|
1039
|
-
example_beads: [],
|
|
1040
|
-
},
|
|
1041
|
-
];
|
|
1042
|
-
const formatted = formatSuccessfulPatternsForPrompt(patterns);
|
|
1043
|
-
expect(formatted).toContain("## Successful Patterns");
|
|
1044
|
-
});
|
|
1045
|
-
});
|
|
1046
|
-
|
|
1047
|
-
// ============================================================================
|
|
1048
|
-
// InMemoryPatternStorage Tests
|
|
1049
|
-
// ============================================================================
|
|
1050
|
-
|
|
1051
|
-
describe("InMemoryPatternStorage", () => {
|
|
1052
|
-
it("stores and retrieves a pattern", async () => {
|
|
1053
|
-
const storage = new InMemoryPatternStorage();
|
|
1054
|
-
const pattern = createPattern("Test pattern");
|
|
1055
|
-
await storage.store(pattern);
|
|
1056
|
-
const retrieved = await storage.get(pattern.id);
|
|
1057
|
-
expect(retrieved).toEqual(pattern);
|
|
1058
|
-
});
|
|
1059
|
-
|
|
1060
|
-
it("returns null for non-existent pattern", async () => {
|
|
1061
|
-
const storage = new InMemoryPatternStorage();
|
|
1062
|
-
const retrieved = await storage.get("non-existent");
|
|
1063
|
-
expect(retrieved).toBeNull();
|
|
1064
|
-
});
|
|
1065
|
-
|
|
1066
|
-
it("updates existing pattern on store", async () => {
|
|
1067
|
-
const storage = new InMemoryPatternStorage();
|
|
1068
|
-
const pattern = createPattern("Original");
|
|
1069
|
-
await storage.store(pattern);
|
|
1070
|
-
|
|
1071
|
-
const updated = { ...pattern, content: "Updated" };
|
|
1072
|
-
await storage.store(updated);
|
|
1073
|
-
|
|
1074
|
-
const retrieved = await storage.get(pattern.id);
|
|
1075
|
-
expect(retrieved?.content).toBe("Updated");
|
|
1076
|
-
});
|
|
1077
|
-
|
|
1078
|
-
it("getAll returns all stored patterns", async () => {
|
|
1079
|
-
const storage = new InMemoryPatternStorage();
|
|
1080
|
-
const p1 = createPattern("Pattern 1");
|
|
1081
|
-
const p2 = createPattern("Pattern 2");
|
|
1082
|
-
await storage.store(p1);
|
|
1083
|
-
await storage.store(p2);
|
|
1084
|
-
|
|
1085
|
-
const all = await storage.getAll();
|
|
1086
|
-
expect(all).toHaveLength(2);
|
|
1087
|
-
expect(all).toContainEqual(p1);
|
|
1088
|
-
expect(all).toContainEqual(p2);
|
|
1089
|
-
});
|
|
1090
|
-
|
|
1091
|
-
it("getAll returns empty array when no patterns", async () => {
|
|
1092
|
-
const storage = new InMemoryPatternStorage();
|
|
1093
|
-
const all = await storage.getAll();
|
|
1094
|
-
expect(all).toEqual([]);
|
|
1095
|
-
});
|
|
1096
|
-
|
|
1097
|
-
it("getAntiPatterns filters by kind", async () => {
|
|
1098
|
-
const storage = new InMemoryPatternStorage();
|
|
1099
|
-
const pattern = createPattern("Good pattern");
|
|
1100
|
-
const antiPattern: DecompositionPattern = {
|
|
1101
|
-
...createPattern("Bad pattern"),
|
|
1102
|
-
kind: "anti_pattern",
|
|
1103
|
-
is_negative: true,
|
|
1104
|
-
};
|
|
1105
|
-
await storage.store(pattern);
|
|
1106
|
-
await storage.store(antiPattern);
|
|
1107
|
-
|
|
1108
|
-
const antiPatterns = await storage.getAntiPatterns();
|
|
1109
|
-
expect(antiPatterns).toHaveLength(1);
|
|
1110
|
-
expect(antiPatterns[0].kind).toBe("anti_pattern");
|
|
1111
|
-
});
|
|
1112
|
-
|
|
1113
|
-
it("getByTag filters by tag", async () => {
|
|
1114
|
-
const storage = new InMemoryPatternStorage();
|
|
1115
|
-
const p1 = createPattern("Pattern 1", ["tag1", "tag2"]);
|
|
1116
|
-
const p2 = createPattern("Pattern 2", ["tag2", "tag3"]);
|
|
1117
|
-
const p3 = createPattern("Pattern 3", ["tag3"]);
|
|
1118
|
-
await storage.store(p1);
|
|
1119
|
-
await storage.store(p2);
|
|
1120
|
-
await storage.store(p3);
|
|
1121
|
-
|
|
1122
|
-
const tagged = await storage.getByTag("tag2");
|
|
1123
|
-
expect(tagged).toHaveLength(2);
|
|
1124
|
-
expect(tagged.map((p) => p.id)).toContain(p1.id);
|
|
1125
|
-
expect(tagged.map((p) => p.id)).toContain(p2.id);
|
|
1126
|
-
});
|
|
1127
|
-
|
|
1128
|
-
it("getByTag returns empty for non-existent tag", async () => {
|
|
1129
|
-
const storage = new InMemoryPatternStorage();
|
|
1130
|
-
const pattern = createPattern("Test", ["tag1"]);
|
|
1131
|
-
await storage.store(pattern);
|
|
1132
|
-
|
|
1133
|
-
const tagged = await storage.getByTag("tag2");
|
|
1134
|
-
expect(tagged).toEqual([]);
|
|
1135
|
-
});
|
|
1136
|
-
|
|
1137
|
-
it("findByContent finds patterns by substring", async () => {
|
|
1138
|
-
const storage = new InMemoryPatternStorage();
|
|
1139
|
-
const p1 = createPattern("Split by file type");
|
|
1140
|
-
const p2 = createPattern("Split by component");
|
|
1141
|
-
const p3 = createPattern("Maximize parallelization");
|
|
1142
|
-
await storage.store(p1);
|
|
1143
|
-
await storage.store(p2);
|
|
1144
|
-
await storage.store(p3);
|
|
1145
|
-
|
|
1146
|
-
const found = await storage.findByContent("split");
|
|
1147
|
-
expect(found).toHaveLength(2);
|
|
1148
|
-
});
|
|
1149
|
-
|
|
1150
|
-
it("findByContent is case-insensitive", async () => {
|
|
1151
|
-
const storage = new InMemoryPatternStorage();
|
|
1152
|
-
const pattern = createPattern("Split by file type");
|
|
1153
|
-
await storage.store(pattern);
|
|
1154
|
-
|
|
1155
|
-
const found = await storage.findByContent("SPLIT");
|
|
1156
|
-
expect(found).toHaveLength(1);
|
|
1157
|
-
});
|
|
1158
|
-
|
|
1159
|
-
it("findByContent returns empty for no matches", async () => {
|
|
1160
|
-
const storage = new InMemoryPatternStorage();
|
|
1161
|
-
const pattern = createPattern("Test pattern");
|
|
1162
|
-
await storage.store(pattern);
|
|
1163
|
-
|
|
1164
|
-
const found = await storage.findByContent("nonexistent");
|
|
1165
|
-
expect(found).toEqual([]);
|
|
1166
|
-
});
|
|
1167
|
-
});
|