opencode-swarm-plugin 0.42.5 → 0.42.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,459 @@
1
+ /**
2
+ * Swarm Insights Data Layer
3
+ *
4
+ * Aggregates insights from swarm coordination for prompt injection.
5
+ * Provides concise, context-efficient summaries for coordinators and workers.
6
+ *
7
+ * Data sources:
8
+ * - Event store (subtask_outcome, eval_finalized)
9
+ * - Semantic memory (file-specific learnings)
10
+ * - Anti-pattern registry
11
+ */
12
+
13
+ import type { SwarmMailAdapter } from "swarm-mail";
14
+
15
+ // ============================================================================
16
+ // Types
17
+ // ============================================================================
18
+
19
+ export interface StrategyInsight {
20
+ strategy: string;
21
+ successRate: number;
22
+ totalAttempts: number;
23
+ recommendation: string;
24
+ }
25
+
26
+ export interface FileInsight {
27
+ file: string;
28
+ failureCount: number;
29
+ lastFailure: string | null;
30
+ gotchas: string[];
31
+ }
32
+
33
+ export interface PatternInsight {
34
+ pattern: string;
35
+ frequency: number;
36
+ recommendation: string;
37
+ }
38
+
39
+ export interface InsightsBundle {
40
+ strategies?: StrategyInsight[];
41
+ files?: FileInsight[];
42
+ patterns?: PatternInsight[];
43
+ }
44
+
45
+ export interface FormatOptions {
46
+ maxTokens?: number;
47
+ }
48
+
49
+ // ============================================================================
50
+ // Strategy Insights
51
+ // ============================================================================
52
+
53
+ /**
54
+ * Get strategy success rates and recommendations for a task.
55
+ *
56
+ * Queries the event store for subtask_outcome events and calculates
57
+ * success rates by strategy. Returns recommendations based on historical data.
58
+ *
59
+ * @param swarmMail - SwarmMail adapter for database access
60
+ * @param _task - Task description (currently unused, reserved for future filtering)
61
+ * @returns Promise resolving to array of strategy insights with success rates and recommendations
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * const insights = await getStrategyInsights(swarmMail, "Add authentication");
66
+ * // Returns: [
67
+ * // { strategy: "file-based", successRate: 85.5, totalAttempts: 12, recommendation: "..." },
68
+ * // { strategy: "feature-based", successRate: 65.0, totalAttempts: 8, recommendation: "..." }
69
+ * // ]
70
+ * ```
71
+ */
72
+ export async function getStrategyInsights(
73
+ swarmMail: SwarmMailAdapter,
74
+ _task: string,
75
+ ): Promise<StrategyInsight[]> {
76
+ const db = await swarmMail.getDatabase();
77
+
78
+ const query = `
79
+ SELECT
80
+ json_extract(data, '$.strategy') as strategy,
81
+ COUNT(*) as total_attempts,
82
+ SUM(CASE WHEN json_extract(data, '$.success') = 'true' THEN 1 ELSE 0 END) as successes
83
+ FROM events
84
+ WHERE type = 'subtask_outcome'
85
+ AND json_extract(data, '$.strategy') IS NOT NULL
86
+ GROUP BY json_extract(data, '$.strategy')
87
+ ORDER BY total_attempts DESC
88
+ `;
89
+
90
+ const result = await db.query(query, []);
91
+ const rows = result.rows as Array<{
92
+ strategy: string;
93
+ total_attempts: number;
94
+ successes: number;
95
+ }>;
96
+
97
+ return rows.map((row) => {
98
+ const successRate = (row.successes / row.total_attempts) * 100;
99
+ return {
100
+ strategy: row.strategy,
101
+ successRate: Math.round(successRate * 100) / 100,
102
+ totalAttempts: row.total_attempts,
103
+ recommendation: getStrategyRecommendation(row.strategy, successRate),
104
+ };
105
+ });
106
+ }
107
+
108
+ /**
109
+ * Generate recommendation based on strategy and success rate.
110
+ *
111
+ * @param strategy - Strategy name (e.g., "file-based", "feature-based")
112
+ * @param successRate - Success rate percentage (0-100)
113
+ * @returns Recommendation string based on performance thresholds
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * getStrategyRecommendation("file-based", 85);
118
+ * // Returns: "file-based is performing well (85% success)"
119
+ *
120
+ * getStrategyRecommendation("feature-based", 35);
121
+ * // Returns: "AVOID feature-based - high failure rate (35%)"
122
+ * ```
123
+ */
124
+ function getStrategyRecommendation(strategy: string, successRate: number): string {
125
+ if (successRate >= 80) {
126
+ return `${strategy} is performing well (${successRate.toFixed(0)}% success)`;
127
+ }
128
+ if (successRate >= 60) {
129
+ return `${strategy} is moderate - monitor for issues`;
130
+ }
131
+ if (successRate >= 40) {
132
+ return `${strategy} has low success - consider alternatives`;
133
+ }
134
+ return `AVOID ${strategy} - high failure rate (${successRate.toFixed(0)}%)`;
135
+ }
136
+
137
+ // ============================================================================
138
+ // File Insights
139
+ // ============================================================================
140
+
141
+ /**
142
+ * Get insights for specific files based on historical outcomes.
143
+ *
144
+ * Queries the event store for failures involving these files and
145
+ * semantic memory for file-specific gotchas.
146
+ *
147
+ * @param swarmMail - SwarmMail adapter for database access
148
+ * @param files - Array of file paths to analyze
149
+ * @returns Promise resolving to array of file-specific insights including failure counts and gotchas
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * const insights = await getFileInsights(swarmMail, ["src/auth.ts", "src/db.ts"]);
154
+ * // Returns: [
155
+ * // { file: "src/auth.ts", failureCount: 3, lastFailure: "2025-12-20T10:30:00Z", gotchas: [...] }
156
+ * // ]
157
+ * ```
158
+ */
159
+ export async function getFileInsights(
160
+ swarmMail: SwarmMailAdapter,
161
+ files: string[],
162
+ ): Promise<FileInsight[]> {
163
+ if (files.length === 0) return [];
164
+
165
+ const db = await swarmMail.getDatabase();
166
+ const insights: FileInsight[] = [];
167
+
168
+ for (const file of files) {
169
+ // Query for failures involving this file
170
+ const query = `
171
+ SELECT
172
+ COUNT(*) as failure_count,
173
+ MAX(timestamp) as last_failure
174
+ FROM events
175
+ WHERE type = 'subtask_outcome'
176
+ AND json_extract(data, '$.success') = 'false'
177
+ AND json_extract(data, '$.files_touched') LIKE ?
178
+ `;
179
+
180
+ const result = await db.query(query, [`%${file}%`]);
181
+ const row = result.rows[0] as {
182
+ failure_count: number;
183
+ last_failure: string | null;
184
+ };
185
+
186
+ if (row && row.failure_count > 0) {
187
+ // Query semantic memory for gotchas (simplified - would use actual memory search)
188
+ const gotchas = await getFileGotchas(swarmMail, file);
189
+
190
+ insights.push({
191
+ file,
192
+ failureCount: row.failure_count,
193
+ lastFailure: row.last_failure,
194
+ gotchas,
195
+ });
196
+ }
197
+ }
198
+
199
+ return insights;
200
+ }
201
+
202
+ /**
203
+ * Get gotchas for a file from semantic memory.
204
+ *
205
+ * In a full implementation, this would query the semantic memory
206
+ * for file-specific learnings. For now, returns empty array.
207
+ *
208
+ * @param _swarmMail - SwarmMail adapter (currently unused)
209
+ * @param _file - File path to query learnings for
210
+ * @returns Promise resolving to array of gotcha strings (currently empty, TODO)
211
+ *
212
+ * @example
213
+ * ```typescript
214
+ * const gotchas = await getFileGotchas(swarmMail, "src/auth.ts");
215
+ * // TODO: Will return semantic memory learnings like:
216
+ * // ["OAuth tokens need 5min buffer", "Always validate refresh token expiry"]
217
+ * ```
218
+ */
219
+ async function getFileGotchas(
220
+ _swarmMail: SwarmMailAdapter,
221
+ _file: string,
222
+ ): Promise<string[]> {
223
+ // TODO: Query semantic memory for file-specific learnings
224
+ // const memories = await semanticMemoryFind({ query: `file:${file}`, limit: 3 });
225
+ // return memories.map(m => m.summary);
226
+ return [];
227
+ }
228
+
229
+ // ============================================================================
230
+ // Pattern Insights
231
+ // ============================================================================
232
+
233
+ /**
234
+ * Get common failure patterns and anti-patterns.
235
+ *
236
+ * Analyzes event store for recurring failure patterns and
237
+ * queries the anti-pattern registry.
238
+ *
239
+ * @param swarmMail - SwarmMail adapter for database access
240
+ * @returns Promise resolving to array of pattern insights with frequency and recommendations
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * const patterns = await getPatternInsights(swarmMail);
245
+ * // Returns: [
246
+ * // { pattern: "type_error", frequency: 5, recommendation: "Add explicit type annotations and null checks" },
247
+ * // { pattern: "timeout", frequency: 3, recommendation: "Consider breaking into smaller tasks" }
248
+ * // ]
249
+ * ```
250
+ */
251
+ export async function getPatternInsights(
252
+ swarmMail: SwarmMailAdapter,
253
+ ): Promise<PatternInsight[]> {
254
+ const db = await swarmMail.getDatabase();
255
+ const patterns: PatternInsight[] = [];
256
+
257
+ // Query for common error patterns
258
+ const query = `
259
+ SELECT
260
+ json_extract(data, '$.error_type') as error_type,
261
+ COUNT(*) as frequency
262
+ FROM events
263
+ WHERE type = 'subtask_outcome'
264
+ AND json_extract(data, '$.success') = 'false'
265
+ AND json_extract(data, '$.error_type') IS NOT NULL
266
+ GROUP BY json_extract(data, '$.error_type')
267
+ HAVING COUNT(*) >= 2
268
+ ORDER BY frequency DESC
269
+ LIMIT 5
270
+ `;
271
+
272
+ const result = await db.query(query, []);
273
+ const rows = result.rows as Array<{
274
+ error_type: string;
275
+ frequency: number;
276
+ }>;
277
+
278
+ for (const row of rows) {
279
+ patterns.push({
280
+ pattern: row.error_type,
281
+ frequency: row.frequency,
282
+ recommendation: getPatternRecommendation(row.error_type),
283
+ });
284
+ }
285
+
286
+ return patterns;
287
+ }
288
+
289
+ /**
290
+ * Generate recommendation for a failure pattern.
291
+ *
292
+ * @param errorType - Type of error pattern (e.g., "type_error", "timeout", "conflict")
293
+ * @returns Recommendation string for addressing the pattern
294
+ *
295
+ * @example
296
+ * ```typescript
297
+ * getPatternRecommendation("type_error");
298
+ * // Returns: "Add explicit type annotations and null checks"
299
+ *
300
+ * getPatternRecommendation("unknown_error");
301
+ * // Returns: "Address unknown_error issues"
302
+ * ```
303
+ */
304
+ function getPatternRecommendation(errorType: string): string {
305
+ // Common patterns and their recommendations
306
+ const recommendations: Record<string, string> = {
307
+ type_error: "Add explicit type annotations and null checks",
308
+ timeout: "Consider breaking into smaller tasks",
309
+ conflict: "Check file reservations before editing",
310
+ test_failure: "Run tests incrementally during implementation",
311
+ };
312
+
313
+ return recommendations[errorType] || `Address ${errorType} issues`;
314
+ }
315
+
316
+ // ============================================================================
317
+ // Prompt Formatting
318
+ // ============================================================================
319
+
320
+ /**
321
+ * Format insights bundle for prompt injection.
322
+ *
323
+ * Produces a concise, context-efficient summary suitable for
324
+ * inclusion in coordinator or worker prompts.
325
+ *
326
+ * @param bundle - Insights bundle containing strategies, files, and patterns
327
+ * @param options - Formatting options (maxTokens defaults to 500)
328
+ * @returns Formatted markdown string for prompt injection, or empty string if no insights
329
+ *
330
+ * @example
331
+ * ```typescript
332
+ * const bundle = {
333
+ * strategies: [{ strategy: "file-based", successRate: 85.5, totalAttempts: 12, recommendation: "..." }],
334
+ * files: [{ file: "src/auth.ts", failureCount: 2, lastFailure: null, gotchas: [] }],
335
+ * patterns: [{ pattern: "type_error", frequency: 3, recommendation: "Add type checks" }]
336
+ * };
337
+ * const formatted = formatInsightsForPrompt(bundle, { maxTokens: 300 });
338
+ * // Returns formatted markdown with top 3 strategies, top 5 files, top 3 patterns
339
+ * ```
340
+ */
341
+ export function formatInsightsForPrompt(
342
+ bundle: InsightsBundle,
343
+ options: FormatOptions = {},
344
+ ): string {
345
+ const { maxTokens = 500 } = options;
346
+ const sections: string[] = [];
347
+
348
+ // Format strategy insights
349
+ if (bundle.strategies && bundle.strategies.length > 0) {
350
+ const strategyLines = bundle.strategies
351
+ .slice(0, 3) // Top 3 strategies
352
+ .map(
353
+ (s) =>
354
+ `- ${s.strategy}: ${s.successRate.toFixed(0)}% success (${s.totalAttempts} attempts)`,
355
+ );
356
+ sections.push(`**Strategy Performance:**\n${strategyLines.join("\n")}`);
357
+ }
358
+
359
+ // Format file insights
360
+ if (bundle.files && bundle.files.length > 0) {
361
+ const fileLines = bundle.files.slice(0, 5).map((f) => {
362
+ const gotchaStr =
363
+ f.gotchas.length > 0 ? ` - ${f.gotchas[0]}` : "";
364
+ return `- ${f.file}: ${f.failureCount} past failures${gotchaStr}`;
365
+ });
366
+ sections.push(`**File-Specific Gotchas:**\n${fileLines.join("\n")}`);
367
+ }
368
+
369
+ // Format pattern insights
370
+ if (bundle.patterns && bundle.patterns.length > 0) {
371
+ const patternLines = bundle.patterns
372
+ .slice(0, 3)
373
+ .map((p) => `- ${p.pattern} (${p.frequency}x): ${p.recommendation}`);
374
+ sections.push(`**Common Pitfalls:**\n${patternLines.join("\n")}`);
375
+ }
376
+
377
+ if (sections.length === 0) {
378
+ return "";
379
+ }
380
+
381
+ let result = sections.join("\n\n");
382
+
383
+ // Truncate to fit token budget (rough estimate: 4 chars per token)
384
+ const maxChars = maxTokens * 4;
385
+ if (result.length > maxChars) {
386
+ result = result.slice(0, maxChars - 3) + "...";
387
+ }
388
+
389
+ return result;
390
+ }
391
+
392
+ // ============================================================================
393
+ // Caching (for future optimization)
394
+ // ============================================================================
395
+
396
+ // Simple in-memory cache with TTL
397
+ const insightsCache = new Map<
398
+ string,
399
+ { data: InsightsBundle; expires: number }
400
+ >();
401
+ const CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes
402
+
403
+ /**
404
+ * Get cached insights or compute fresh ones.
405
+ *
406
+ * Simple in-memory cache with 5-minute TTL to avoid redundant database queries.
407
+ *
408
+ * @param _swarmMail - SwarmMail adapter (currently unused, reserved for future cache invalidation)
409
+ * @param cacheKey - Unique key for caching (e.g., "strategies:task-name" or "files:src/auth.ts")
410
+ * @param computeFn - Function to compute fresh insights if cache miss
411
+ * @returns Promise resolving to cached or freshly computed insights bundle
412
+ *
413
+ * @example
414
+ * ```typescript
415
+ * const insights = await getCachedInsights(
416
+ * swarmMail,
417
+ * "strategies:add-auth",
418
+ * async () => ({
419
+ * strategies: await getStrategyInsights(swarmMail, "add auth"),
420
+ * })
421
+ * );
422
+ * // First call: computes and caches. Subsequent calls within 5min: returns cached.
423
+ * ```
424
+ */
425
+ export async function getCachedInsights(
426
+ _swarmMail: SwarmMailAdapter,
427
+ cacheKey: string,
428
+ computeFn: () => Promise<InsightsBundle>,
429
+ ): Promise<InsightsBundle> {
430
+ const cached = insightsCache.get(cacheKey);
431
+ if (cached && cached.expires > Date.now()) {
432
+ return cached.data;
433
+ }
434
+
435
+ const data = await computeFn();
436
+ insightsCache.set(cacheKey, {
437
+ data,
438
+ expires: Date.now() + CACHE_TTL_MS,
439
+ });
440
+
441
+ return data;
442
+ }
443
+
444
+ /**
445
+ * Clear the insights cache.
446
+ *
447
+ * Useful for testing or forcing fresh insights computation.
448
+ *
449
+ * @returns void
450
+ *
451
+ * @example
452
+ * ```typescript
453
+ * clearInsightsCache();
454
+ * // All cached insights invalidated, next getCachedInsights() will recompute
455
+ * ```
456
+ */
457
+ export function clearInsightsCache(): void {
458
+ insightsCache.clear();
459
+ }
@@ -1102,4 +1102,169 @@ describe("getPromptInsights", () => {
1102
1102
  }
1103
1103
  });
1104
1104
  });
1105
+
1106
+ describe("getPromptInsights integration with swarm-insights", () => {
1107
+ test("coordinator role uses swarm-insights data layer", async () => {
1108
+ const { getPromptInsights } = await import("./swarm-prompts");
1109
+
1110
+ // Should call new data layer, not old swarm-mail analytics
1111
+ const result = await getPromptInsights({ role: "coordinator" });
1112
+
1113
+ // If we have data, it should be formatted by formatInsightsForPrompt
1114
+ if (result.length > 0) {
1115
+ // New format has "Historical Insights" section
1116
+ expect(result).toMatch(/Historical Insights|Strategy Performance|Common Pitfalls/i);
1117
+ }
1118
+ });
1119
+
1120
+ test("coordinator insights have expected structure when data exists", async () => {
1121
+ const { getPromptInsights } = await import("./swarm-prompts");
1122
+ const result = await getPromptInsights({ role: "coordinator" });
1123
+
1124
+ // Should have Historical Insights header
1125
+ if (result.length > 0) {
1126
+ expect(result).toContain("📊 Historical Insights");
1127
+ expect(result).toContain("Use these learnings when selecting decomposition strategies");
1128
+ }
1129
+ });
1130
+
1131
+ test("coordinator insights use formatInsightsForPrompt output", async () => {
1132
+ const { getPromptInsights } = await import("./swarm-prompts");
1133
+ const result = await getPromptInsights({ role: "coordinator" });
1134
+
1135
+ // formatInsightsForPrompt produces specific markdown patterns
1136
+ if (result.length > 0 && result.includes("Strategy")) {
1137
+ // Should have Strategy Performance or Common Pitfalls sections
1138
+ const hasExpectedSections =
1139
+ result.includes("Strategy Performance") ||
1140
+ result.includes("Common Pitfalls");
1141
+ expect(hasExpectedSections).toBe(true);
1142
+ }
1143
+ });
1144
+
1145
+ test("coordinator insights are concise (<500 tokens)", async () => {
1146
+ const { getPromptInsights } = await import("./swarm-prompts");
1147
+ const result = await getPromptInsights({ role: "coordinator" });
1148
+
1149
+ // formatInsightsForPrompt enforces maxTokens=500 by default
1150
+ // Rough estimate: 4 chars per token = 2000 chars max
1151
+ if (result.length > 0) {
1152
+ expect(result.length).toBeLessThan(2000);
1153
+ }
1154
+ });
1155
+
1156
+ test("gracefully handles missing data", async () => {
1157
+ const { getPromptInsights } = await import("./swarm-prompts");
1158
+
1159
+ // Should not throw if database is empty or missing
1160
+ const result = await getPromptInsights({ role: "coordinator" });
1161
+
1162
+ // Empty string is acceptable when no data
1163
+ expect(typeof result).toBe("string");
1164
+ });
1165
+
1166
+ test("imports from swarm-insights module", async () => {
1167
+ // Verify the imports exist
1168
+ const insights = await import("./swarm-insights");
1169
+
1170
+ expect(insights.getStrategyInsights).toBeDefined();
1171
+ expect(insights.getPatternInsights).toBeDefined();
1172
+ expect(insights.formatInsightsForPrompt).toBeDefined();
1173
+ });
1174
+ });
1175
+
1176
+ describe("worker insights integration with swarm-insights", () => {
1177
+ test("worker role uses getFileInsights from swarm-insights data layer", async () => {
1178
+ const { getPromptInsights } = await import("./swarm-prompts");
1179
+
1180
+ // Should call getFileInsights for file-specific insights
1181
+ const result = await getPromptInsights({
1182
+ role: "worker",
1183
+ files: ["src/auth.ts", "src/db.ts"],
1184
+ domain: "authentication"
1185
+ });
1186
+
1187
+ // If we have data, it should be formatted by formatInsightsForPrompt
1188
+ if (result.length > 0) {
1189
+ // New format has "File-Specific Gotchas" or semantic memory learnings
1190
+ expect(result).toMatch(/File-Specific Gotchas|Relevant Learnings/i);
1191
+ }
1192
+ });
1193
+
1194
+ test("worker insights include file-specific gotchas when available", async () => {
1195
+ const { getPromptInsights } = await import("./swarm-prompts");
1196
+ const result = await getPromptInsights({
1197
+ role: "worker",
1198
+ files: ["src/test-file.ts"],
1199
+ });
1200
+
1201
+ // Should contain either file gotchas or semantic memory results
1202
+ if (result.length > 0) {
1203
+ const hasFileInsights =
1204
+ result.includes("File-Specific Gotchas") ||
1205
+ result.includes("Relevant Learnings");
1206
+ expect(hasFileInsights).toBe(true);
1207
+ }
1208
+ });
1209
+
1210
+ test("worker insights combine event store failures + semantic memory", async () => {
1211
+ const { getPromptInsights } = await import("./swarm-prompts");
1212
+ const result = await getPromptInsights({
1213
+ role: "worker",
1214
+ files: ["src/complex.ts"],
1215
+ domain: "complex feature"
1216
+ });
1217
+
1218
+ // Should potentially have both sources of insight
1219
+ // At minimum, should return string (empty if no data)
1220
+ expect(typeof result).toBe("string");
1221
+ });
1222
+
1223
+ test("worker insights are concise (<300 tokens per file)", async () => {
1224
+ const { getPromptInsights } = await import("./swarm-prompts");
1225
+ const result = await getPromptInsights({
1226
+ role: "worker",
1227
+ files: ["src/file1.ts", "src/file2.ts", "src/file3.ts"],
1228
+ });
1229
+
1230
+ // <300 tokens per file = 900 tokens max for 3 files
1231
+ // Rough estimate: 4 chars per token = 3600 chars max
1232
+ if (result.length > 0) {
1233
+ expect(result.length).toBeLessThan(3600);
1234
+ }
1235
+ });
1236
+
1237
+ test("formatSubtaskPromptV2 includes file insights in shared_context", async () => {
1238
+ const result = await formatSubtaskPromptV2({
1239
+ bead_id: "test-123",
1240
+ epic_id: "epic-456",
1241
+ subtask_title: "Implement auth",
1242
+ subtask_description: "Add authentication flow",
1243
+ files: ["src/auth.ts", "src/user.ts"],
1244
+ shared_context: "Original context from coordinator",
1245
+ });
1246
+
1247
+ // shared_context should be replaced and insights potentially included
1248
+ // At minimum, the original context should be in the prompt
1249
+ expect(result).toContain("Original context from coordinator");
1250
+ });
1251
+
1252
+ test("worker insights gracefully handle missing files parameter", async () => {
1253
+ const { getPromptInsights } = await import("./swarm-prompts");
1254
+
1255
+ // Should not throw with no files or domain
1256
+ const result = await getPromptInsights({ role: "worker" });
1257
+
1258
+ // Empty string is acceptable when no context to query
1259
+ expect(typeof result).toBe("string");
1260
+ });
1261
+
1262
+ test("worker insights use swarm-insights getFileInsights", async () => {
1263
+ // Verify the function is imported
1264
+ const insights = await import("./swarm-insights");
1265
+
1266
+ expect(insights.getFileInsights).toBeDefined();
1267
+ expect(typeof insights.getFileInsights).toBe("function");
1268
+ });
1269
+ });
1105
1270
  });