opencode-swarm-plugin 0.36.0 → 0.36.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 (49) hide show
  1. package/.hive/issues.jsonl +4 -4
  2. package/.hive/memories.jsonl +274 -1
  3. package/.turbo/turbo-build.log +4 -4
  4. package/.turbo/turbo-test.log +307 -307
  5. package/CHANGELOG.md +71 -0
  6. package/bin/swarm.ts +234 -179
  7. package/dist/compaction-hook.d.ts +54 -4
  8. package/dist/compaction-hook.d.ts.map +1 -1
  9. package/dist/eval-capture.d.ts +122 -17
  10. package/dist/eval-capture.d.ts.map +1 -1
  11. package/dist/index.d.ts +1 -7
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +1278 -619
  14. package/dist/planning-guardrails.d.ts +121 -0
  15. package/dist/planning-guardrails.d.ts.map +1 -1
  16. package/dist/plugin.d.ts +9 -9
  17. package/dist/plugin.d.ts.map +1 -1
  18. package/dist/plugin.js +1283 -329
  19. package/dist/schemas/task.d.ts +0 -1
  20. package/dist/schemas/task.d.ts.map +1 -1
  21. package/dist/swarm-decompose.d.ts +0 -8
  22. package/dist/swarm-decompose.d.ts.map +1 -1
  23. package/dist/swarm-orchestrate.d.ts.map +1 -1
  24. package/dist/swarm-prompts.d.ts +0 -4
  25. package/dist/swarm-prompts.d.ts.map +1 -1
  26. package/dist/swarm-review.d.ts.map +1 -1
  27. package/dist/swarm.d.ts +0 -6
  28. package/dist/swarm.d.ts.map +1 -1
  29. package/evals/README.md +38 -0
  30. package/evals/coordinator-session.eval.ts +154 -0
  31. package/evals/fixtures/coordinator-sessions.ts +328 -0
  32. package/evals/lib/data-loader.ts +69 -0
  33. package/evals/scorers/coordinator-discipline.evalite-test.ts +536 -0
  34. package/evals/scorers/coordinator-discipline.ts +315 -0
  35. package/evals/scorers/index.ts +12 -0
  36. package/examples/plugin-wrapper-template.ts +303 -4
  37. package/package.json +2 -2
  38. package/src/compaction-hook.test.ts +8 -1
  39. package/src/compaction-hook.ts +31 -21
  40. package/src/eval-capture.test.ts +390 -0
  41. package/src/eval-capture.ts +163 -4
  42. package/src/index.ts +68 -1
  43. package/src/planning-guardrails.test.ts +387 -2
  44. package/src/planning-guardrails.ts +289 -0
  45. package/src/plugin.ts +10 -10
  46. package/src/swarm-decompose.ts +20 -0
  47. package/src/swarm-orchestrate.ts +44 -0
  48. package/src/swarm-prompts.ts +20 -0
  49. package/src/swarm-review.ts +41 -0
@@ -7,6 +7,8 @@
7
7
  * @module planning-guardrails
8
8
  */
9
9
 
10
+ import { captureCoordinatorEvent } from "./eval-capture.js";
11
+
10
12
  /**
11
13
  * Patterns that suggest file modification work
12
14
  * These indicate the todo is about implementation, not tracking
@@ -147,3 +149,290 @@ Swarm workers can complete these ${fileModificationCount} tasks in parallel.
147
149
  export function shouldAnalyzeTool(toolName: string): boolean {
148
150
  return toolName === "todowrite" || toolName === "TodoWrite";
149
151
  }
152
+
153
+ /**
154
+ * Violation patterns for coordinator behavior detection
155
+ *
156
+ * These patterns identify when a coordinator is performing work
157
+ * that should be delegated to worker agents.
158
+ *
159
+ * @example
160
+ * ```ts
161
+ * // Bad: Coordinator editing files
162
+ * if (VIOLATION_PATTERNS.FILE_MODIFICATION_TOOLS.includes("edit")) { ... }
163
+ *
164
+ * // Good: Worker editing files
165
+ * // (no violation when agentContext === "worker")
166
+ * ```
167
+ */
168
+ export const VIOLATION_PATTERNS = {
169
+ /**
170
+ * Tool names that modify files
171
+ *
172
+ * Coordinators should NEVER call these tools directly.
173
+ * Workers reserve files and make modifications.
174
+ */
175
+ FILE_MODIFICATION_TOOLS: ["edit", "write"],
176
+
177
+ /**
178
+ * Tool names for file reservations
179
+ *
180
+ * Coordinators don't reserve files - workers do this
181
+ * before editing to prevent conflicts.
182
+ */
183
+ RESERVATION_TOOLS: ["swarmmail_reserve", "agentmail_reserve"],
184
+
185
+ /**
186
+ * Regex patterns that indicate test execution in bash commands
187
+ *
188
+ * Coordinators review test results, workers run tests.
189
+ * Matches common test runners and test file patterns.
190
+ */
191
+ TEST_EXECUTION_PATTERNS: [
192
+ /\bbun\s+test\b/i,
193
+ /\bnpm\s+(run\s+)?test/i,
194
+ /\byarn\s+(run\s+)?test/i,
195
+ /\bpnpm\s+(run\s+)?test/i,
196
+ /\bjest\b/i,
197
+ /\bvitest\b/i,
198
+ /\bmocha\b/i,
199
+ /\bava\b/i,
200
+ /\btape\b/i,
201
+ /\.test\.(ts|js|tsx|jsx)\b/i,
202
+ /\.spec\.(ts|js|tsx|jsx)\b/i,
203
+ ],
204
+ } as const;
205
+
206
+ /**
207
+ * Result of violation detection
208
+ */
209
+ export interface ViolationDetectionResult {
210
+ /** Whether a violation was detected */
211
+ isViolation: boolean;
212
+
213
+ /** Type of violation if detected */
214
+ violationType?:
215
+ | "coordinator_edited_file"
216
+ | "coordinator_ran_tests"
217
+ | "coordinator_reserved_files"
218
+ | "no_worker_spawned";
219
+
220
+ /** Human-readable message */
221
+ message?: string;
222
+
223
+ /** Payload data for the violation */
224
+ payload?: Record<string, unknown>;
225
+ }
226
+
227
+ /**
228
+ * Detect coordinator violations in real-time
229
+ *
230
+ * Checks for patterns that indicate a coordinator is doing work
231
+ * that should be delegated to workers:
232
+ * 1. Edit/Write tool calls (coordinators plan, workers implement)
233
+ * 2. Test execution (workers verify, coordinators review)
234
+ * 3. File reservations (workers reserve before editing)
235
+ * 4. No worker spawned after decomposition (coordinators must delegate)
236
+ *
237
+ * When a violation is detected, captures it via captureCoordinatorEvent().
238
+ *
239
+ * @param params - Detection parameters
240
+ * @returns Violation detection result
241
+ */
242
+ export function detectCoordinatorViolation(params: {
243
+ sessionId: string;
244
+ epicId: string;
245
+ toolName: string;
246
+ toolArgs: Record<string, unknown>;
247
+ agentContext: "coordinator" | "worker";
248
+ checkNoSpawn?: boolean;
249
+ }): ViolationDetectionResult {
250
+ const { sessionId, epicId, toolName, toolArgs, agentContext, checkNoSpawn = false } = params;
251
+
252
+ // Only check coordinator violations
253
+ if (agentContext !== "coordinator") {
254
+ return { isViolation: false };
255
+ }
256
+
257
+ // Check for file modification violation
258
+ if (VIOLATION_PATTERNS.FILE_MODIFICATION_TOOLS.includes(toolName as any)) {
259
+ const file = (toolArgs.filePath as string) || "";
260
+ const payload = { tool: toolName, file };
261
+
262
+ captureCoordinatorEvent({
263
+ session_id: sessionId,
264
+ epic_id: epicId,
265
+ timestamp: new Date().toISOString(),
266
+ event_type: "VIOLATION",
267
+ violation_type: "coordinator_edited_file",
268
+ payload,
269
+ });
270
+
271
+ return {
272
+ isViolation: true,
273
+ violationType: "coordinator_edited_file",
274
+ message: `⚠️ Coordinator should not edit files directly. Coordinators should spawn workers to implement changes.`,
275
+ payload,
276
+ };
277
+ }
278
+
279
+ // Check for test execution violation
280
+ if (toolName === "bash") {
281
+ const command = (toolArgs.command as string) || "";
282
+ const isTestCommand = VIOLATION_PATTERNS.TEST_EXECUTION_PATTERNS.some((pattern) =>
283
+ pattern.test(command),
284
+ );
285
+
286
+ if (isTestCommand) {
287
+ const payload = { tool: toolName, command };
288
+
289
+ captureCoordinatorEvent({
290
+ session_id: sessionId,
291
+ epic_id: epicId,
292
+ timestamp: new Date().toISOString(),
293
+ event_type: "VIOLATION",
294
+ violation_type: "coordinator_ran_tests",
295
+ payload,
296
+ });
297
+
298
+ return {
299
+ isViolation: true,
300
+ violationType: "coordinator_ran_tests",
301
+ message: `⚠️ Coordinator should not run tests directly. Workers run tests as part of their implementation verification.`,
302
+ payload,
303
+ };
304
+ }
305
+ }
306
+
307
+ // Check for file reservation violation
308
+ if (VIOLATION_PATTERNS.RESERVATION_TOOLS.includes(toolName as any)) {
309
+ const paths = (toolArgs.paths as string[]) || [];
310
+ const payload = { tool: toolName, paths };
311
+
312
+ captureCoordinatorEvent({
313
+ session_id: sessionId,
314
+ epic_id: epicId,
315
+ timestamp: new Date().toISOString(),
316
+ event_type: "VIOLATION",
317
+ violation_type: "coordinator_reserved_files",
318
+ payload,
319
+ });
320
+
321
+ return {
322
+ isViolation: true,
323
+ violationType: "coordinator_reserved_files",
324
+ message: `⚠️ Coordinator should not reserve files. Workers reserve files before editing to prevent conflicts.`,
325
+ payload,
326
+ };
327
+ }
328
+
329
+ // Check for no worker spawned after decomposition
330
+ if (toolName === "hive_create_epic" && checkNoSpawn) {
331
+ const epicTitle = (toolArgs.epic_title as string) || "";
332
+ const subtasks = (toolArgs.subtasks as unknown[]) || [];
333
+ const payload = { epic_title: epicTitle, subtask_count: subtasks.length };
334
+
335
+ captureCoordinatorEvent({
336
+ session_id: sessionId,
337
+ epic_id: epicId,
338
+ timestamp: new Date().toISOString(),
339
+ event_type: "VIOLATION",
340
+ violation_type: "no_worker_spawned",
341
+ payload,
342
+ });
343
+
344
+ return {
345
+ isViolation: true,
346
+ violationType: "no_worker_spawned",
347
+ message: `⚠️ Coordinator created decomposition without spawning workers. After hive_create_epic, use swarm_spawn_subtask for each task.`,
348
+ payload,
349
+ };
350
+ }
351
+
352
+ return { isViolation: false };
353
+ }
354
+
355
+ /**
356
+ * Coordinator context state
357
+ *
358
+ * Tracks whether the current session is acting as a swarm coordinator.
359
+ * Set when an epic is created or when swarm tools are used.
360
+ */
361
+ interface CoordinatorContext {
362
+ /** Whether we're in coordinator mode */
363
+ isCoordinator: boolean;
364
+ /** Active epic ID if any */
365
+ epicId?: string;
366
+ /** Session ID for event capture */
367
+ sessionId?: string;
368
+ /** When coordinator mode was activated */
369
+ activatedAt?: number;
370
+ }
371
+
372
+ /** Global coordinator context state */
373
+ let coordinatorContext: CoordinatorContext = {
374
+ isCoordinator: false,
375
+ };
376
+
377
+ /**
378
+ * Set coordinator context
379
+ *
380
+ * Called when swarm coordination begins (e.g., after hive_create_epic or swarm_decompose).
381
+ *
382
+ * @param ctx - Coordinator context to set
383
+ */
384
+ export function setCoordinatorContext(ctx: Partial<CoordinatorContext>): void {
385
+ coordinatorContext = {
386
+ ...coordinatorContext,
387
+ ...ctx,
388
+ activatedAt: ctx.isCoordinator ? Date.now() : coordinatorContext.activatedAt,
389
+ };
390
+ }
391
+
392
+ /**
393
+ * Get current coordinator context
394
+ *
395
+ * @returns Current coordinator context state
396
+ */
397
+ export function getCoordinatorContext(): CoordinatorContext {
398
+ return { ...coordinatorContext };
399
+ }
400
+
401
+ /**
402
+ * Clear coordinator context
403
+ *
404
+ * Called when swarm coordination ends (e.g., epic closed or session ends).
405
+ */
406
+ export function clearCoordinatorContext(): void {
407
+ coordinatorContext = {
408
+ isCoordinator: false,
409
+ };
410
+ }
411
+
412
+ /**
413
+ * Check if we're in coordinator context
414
+ *
415
+ * Returns true if:
416
+ * 1. Coordinator context was explicitly set
417
+ * 2. Context was set within the last 4 hours (session timeout)
418
+ *
419
+ * @returns Whether we're currently in coordinator mode
420
+ */
421
+ export function isInCoordinatorContext(): boolean {
422
+ if (!coordinatorContext.isCoordinator) {
423
+ return false;
424
+ }
425
+
426
+ // Check for session timeout (4 hours)
427
+ const COORDINATOR_TIMEOUT_MS = 4 * 60 * 60 * 1000;
428
+ if (coordinatorContext.activatedAt) {
429
+ const elapsed = Date.now() - coordinatorContext.activatedAt;
430
+ if (elapsed > COORDINATOR_TIMEOUT_MS) {
431
+ // Session timed out, clear context
432
+ clearCoordinatorContext();
433
+ return false;
434
+ }
435
+ }
436
+
437
+ return true;
438
+ }
package/src/plugin.ts CHANGED
@@ -1,23 +1,23 @@
1
1
  /**
2
2
  * OpenCode Plugin Entry Point
3
3
  *
4
- * CRITICAL: Only export the plugin function from this file.
4
+ * CRITICAL: Only export the plugin function as DEFAULT from this file.
5
5
  *
6
6
  * OpenCode's plugin loader calls ALL exports as functions during initialization.
7
- * Exporting classes, constants, or non-function values will cause the plugin
8
- * to fail to load with cryptic errors.
7
+ * If you export both named AND default pointing to the same function, the plugin
8
+ * gets registered TWICE, causing hooks to fire multiple times.
9
9
  *
10
10
  * If you need to export utilities for external use, add them to src/index.ts instead.
11
11
  *
12
12
  * @example
13
- * // ✅ CORRECT - only export the plugin function
13
+ * // ✅ CORRECT - only default export
14
14
  * export default SwarmPlugin;
15
15
  *
16
- * // ❌ WRONG - will break plugin loading
17
- * export const VERSION = "1.0.0";
18
- * export class Helper {}
16
+ * // ❌ WRONG - causes double registration
17
+ * export { SwarmPlugin };
18
+ * export default SwarmPlugin;
19
19
  */
20
- import { SwarmPlugin } from "./index";
20
+ import SwarmPlugin from "./index";
21
21
 
22
- // Only export the plugin function - nothing else!
23
- export { SwarmPlugin };
22
+ // Only default export - no named exports!
23
+ export default SwarmPlugin;
@@ -20,6 +20,7 @@ import {
20
20
  NEGATIVE_MARKERS,
21
21
  type DecompositionStrategy,
22
22
  } from "./swarm-strategies";
23
+ import { captureCoordinatorEvent } from "./eval-capture.js";
23
24
 
24
25
  // ============================================================================
25
26
  // Decomposition Prompt (temporary - will be moved to swarm-prompts.ts)
@@ -722,6 +723,25 @@ export const swarm_delegate_planning = tool({
722
723
  strategyReasoning = selection.reasoning;
723
724
  }
724
725
 
726
+ // Capture strategy selection decision
727
+ try {
728
+ captureCoordinatorEvent({
729
+ session_id: process.env.OPENCODE_SESSION_ID || "unknown",
730
+ epic_id: "planning", // No epic ID yet - this is pre-decomposition
731
+ timestamp: new Date().toISOString(),
732
+ event_type: "DECISION",
733
+ decision_type: "strategy_selected",
734
+ payload: {
735
+ strategy: selectedStrategy,
736
+ reasoning: strategyReasoning,
737
+ task_preview: args.task.slice(0, 100),
738
+ },
739
+ });
740
+ } catch (error) {
741
+ // Non-fatal - don't block planning if capture fails
742
+ console.warn("[swarm_delegate_planning] Failed to capture strategy_selected:", error);
743
+ }
744
+
725
745
  // Query CASS for similar past tasks
726
746
  let cassContext = "";
727
747
  let cassResultInfo: {
@@ -83,6 +83,7 @@ import {
83
83
  isReviewApproved,
84
84
  getReviewStatus,
85
85
  } from "./swarm-review";
86
+ import { captureCoordinatorEvent } from "./eval-capture.js";
86
87
 
87
88
  // ============================================================================
88
89
  // Helper Functions
@@ -1709,6 +1710,28 @@ Files touched: ${args.files_touched?.join(", ") || "none recorded"}`,
1709
1710
  },
1710
1711
  };
1711
1712
 
1713
+ // Capture subtask completion outcome
1714
+ try {
1715
+ const durationMs = args.start_time ? Date.now() - args.start_time : 0;
1716
+ captureCoordinatorEvent({
1717
+ session_id: process.env.OPENCODE_SESSION_ID || "unknown",
1718
+ epic_id: epicId,
1719
+ timestamp: new Date().toISOString(),
1720
+ event_type: "OUTCOME",
1721
+ outcome_type: "subtask_success",
1722
+ payload: {
1723
+ bead_id: args.bead_id,
1724
+ duration_ms: durationMs,
1725
+ files_touched: args.files_touched || [],
1726
+ verification_passed: verificationResult?.passed ?? false,
1727
+ verification_skipped: args.skip_verification ?? false,
1728
+ },
1729
+ });
1730
+ } catch (error) {
1731
+ // Non-fatal - don't block completion if capture fails
1732
+ console.warn("[swarm_complete] Failed to capture subtask_success:", error);
1733
+ }
1734
+
1712
1735
  return JSON.stringify(response, null, 2);
1713
1736
  } catch (error) {
1714
1737
  // CRITICAL: Notify coordinator of failure via swarm mail
@@ -1796,6 +1819,27 @@ Files touched: ${args.files_touched?.join(", ") || "none recorded"}`,
1796
1819
  console.error(`[swarm_complete] Original error:`, error);
1797
1820
  }
1798
1821
 
1822
+ // Capture subtask failure outcome
1823
+ try {
1824
+ const durationMs = args.start_time ? Date.now() - args.start_time : 0;
1825
+ captureCoordinatorEvent({
1826
+ session_id: process.env.OPENCODE_SESSION_ID || "unknown",
1827
+ epic_id: epicId,
1828
+ timestamp: new Date().toISOString(),
1829
+ event_type: "OUTCOME",
1830
+ outcome_type: "subtask_failed",
1831
+ payload: {
1832
+ bead_id: args.bead_id,
1833
+ duration_ms: durationMs,
1834
+ failed_step: failedStep,
1835
+ error_message: errorMessage.slice(0, 500),
1836
+ },
1837
+ });
1838
+ } catch (captureError) {
1839
+ // Non-fatal - don't block error return if capture fails
1840
+ console.warn("[swarm_complete] Failed to capture subtask_failed:", captureError);
1841
+ }
1842
+
1799
1843
  // Return structured error instead of throwing
1800
1844
  // This ensures the agent sees the actual error message
1801
1845
  return JSON.stringify(
@@ -14,6 +14,7 @@
14
14
 
15
15
  import { tool } from "@opencode-ai/plugin";
16
16
  import { generateWorkerHandoff } from "./swarm-orchestrate";
17
+ import { captureCoordinatorEvent } from "./eval-capture.js";
17
18
 
18
19
  // ============================================================================
19
20
  // Prompt Templates
@@ -1107,6 +1108,25 @@ export const swarm_spawn_subtask = tool({
1107
1108
  .replace(/{files_touched}/g, filesJoined)
1108
1109
  .replace(/{worker_id}/g, "worker"); // Will be filled by actual worker name
1109
1110
 
1111
+ // Capture worker spawn decision
1112
+ try {
1113
+ captureCoordinatorEvent({
1114
+ session_id: process.env.OPENCODE_SESSION_ID || "unknown",
1115
+ epic_id: args.epic_id,
1116
+ timestamp: new Date().toISOString(),
1117
+ event_type: "DECISION",
1118
+ decision_type: "worker_spawned",
1119
+ payload: {
1120
+ bead_id: args.bead_id,
1121
+ files: args.files,
1122
+ worker_model: selectedModel,
1123
+ },
1124
+ });
1125
+ } catch (error) {
1126
+ // Non-fatal - don't block spawn if capture fails
1127
+ console.warn("[swarm_spawn_subtask] Failed to capture worker_spawned:", error);
1128
+ }
1129
+
1110
1130
  return JSON.stringify(
1111
1131
  {
1112
1132
  prompt,
@@ -18,6 +18,7 @@ import { tool } from "@opencode-ai/plugin";
18
18
  import { z } from "zod";
19
19
  import { sendSwarmMessage, type HiveAdapter } from "swarm-mail";
20
20
  import { getHiveAdapter } from "./hive";
21
+ import { captureCoordinatorEvent } from "./eval-capture.js";
21
22
 
22
23
  // ============================================================================
23
24
  // Types & Schemas
@@ -508,6 +509,25 @@ export const swarm_review_feedback = tool({
508
509
  // Mark as approved and clear attempts
509
510
  markReviewApproved(args.task_id);
510
511
 
512
+ // Capture review approval decision
513
+ try {
514
+ captureCoordinatorEvent({
515
+ session_id: process.env.OPENCODE_SESSION_ID || "unknown",
516
+ epic_id: epicId,
517
+ timestamp: new Date().toISOString(),
518
+ event_type: "DECISION",
519
+ decision_type: "review_completed",
520
+ payload: {
521
+ task_id: args.task_id,
522
+ status: "approved",
523
+ retry_count: 0,
524
+ },
525
+ });
526
+ } catch (error) {
527
+ // Non-fatal - don't block approval if capture fails
528
+ console.warn("[swarm_review_feedback] Failed to capture review_completed:", error);
529
+ }
530
+
511
531
  // Send approval message
512
532
  await sendSwarmMessage({
513
533
  projectPath: args.project_key,
@@ -539,6 +559,27 @@ You may now complete the task with \`swarm_complete\`.`,
539
559
  const attemptNumber = incrementAttempt(args.task_id);
540
560
  const remaining = MAX_REVIEW_ATTEMPTS - attemptNumber;
541
561
 
562
+ // Capture review rejection decision
563
+ try {
564
+ captureCoordinatorEvent({
565
+ session_id: process.env.OPENCODE_SESSION_ID || "unknown",
566
+ epic_id: epicId,
567
+ timestamp: new Date().toISOString(),
568
+ event_type: "DECISION",
569
+ decision_type: "review_completed",
570
+ payload: {
571
+ task_id: args.task_id,
572
+ status: "needs_changes",
573
+ retry_count: attemptNumber,
574
+ remaining_attempts: remaining,
575
+ issues_count: parsedIssues.length,
576
+ },
577
+ });
578
+ } catch (error) {
579
+ // Non-fatal - don't block feedback if capture fails
580
+ console.warn("[swarm_review_feedback] Failed to capture review_completed:", error);
581
+ }
582
+
542
583
  // Check if task should fail
543
584
  if (remaining <= 0) {
544
585
  // Mark task as blocked using HiveAdapter