opencode-swarm-plugin 0.35.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.
- package/.hive/issues.jsonl +4 -4
- package/.hive/memories.jsonl +274 -1
- package/.turbo/turbo-build.log +4 -4
- package/.turbo/turbo-test.log +307 -307
- package/CHANGELOG.md +133 -0
- package/bin/swarm.ts +234 -179
- package/dist/compaction-hook.d.ts +54 -4
- package/dist/compaction-hook.d.ts.map +1 -1
- package/dist/eval-capture.d.ts +122 -17
- package/dist/eval-capture.d.ts.map +1 -1
- package/dist/index.d.ts +1 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1278 -619
- package/dist/planning-guardrails.d.ts +121 -0
- package/dist/planning-guardrails.d.ts.map +1 -1
- package/dist/plugin.d.ts +9 -9
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +1283 -329
- package/dist/schemas/task.d.ts +0 -1
- package/dist/schemas/task.d.ts.map +1 -1
- package/dist/swarm-decompose.d.ts +0 -8
- package/dist/swarm-decompose.d.ts.map +1 -1
- package/dist/swarm-orchestrate.d.ts.map +1 -1
- package/dist/swarm-prompts.d.ts +0 -4
- package/dist/swarm-prompts.d.ts.map +1 -1
- package/dist/swarm-review.d.ts.map +1 -1
- package/dist/swarm.d.ts +0 -6
- package/dist/swarm.d.ts.map +1 -1
- package/evals/README.md +38 -0
- package/evals/coordinator-session.eval.ts +154 -0
- package/evals/fixtures/coordinator-sessions.ts +328 -0
- package/evals/lib/data-loader.ts +69 -0
- package/evals/scorers/coordinator-discipline.evalite-test.ts +536 -0
- package/evals/scorers/coordinator-discipline.ts +315 -0
- package/evals/scorers/index.ts +12 -0
- package/examples/plugin-wrapper-template.ts +747 -34
- package/package.json +2 -2
- package/src/compaction-hook.test.ts +234 -281
- package/src/compaction-hook.ts +221 -63
- package/src/eval-capture.test.ts +390 -0
- package/src/eval-capture.ts +168 -10
- package/src/index.ts +89 -2
- package/src/learning.integration.test.ts +0 -2
- package/src/planning-guardrails.test.ts +387 -2
- package/src/planning-guardrails.ts +289 -0
- package/src/plugin.ts +10 -10
- package/src/schemas/task.ts +0 -1
- package/src/swarm-decompose.ts +21 -8
- package/src/swarm-orchestrate.ts +44 -0
- package/src/swarm-prompts.ts +20 -0
- package/src/swarm-review.ts +41 -0
- package/src/swarm.integration.test.ts +0 -40
|
@@ -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
|
-
*
|
|
8
|
-
*
|
|
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
|
|
13
|
+
* // ✅ CORRECT - only default export
|
|
14
14
|
* export default SwarmPlugin;
|
|
15
15
|
*
|
|
16
|
-
* // ❌ WRONG -
|
|
17
|
-
* export
|
|
18
|
-
* export
|
|
16
|
+
* // ❌ WRONG - causes double registration
|
|
17
|
+
* export { SwarmPlugin };
|
|
18
|
+
* export default SwarmPlugin;
|
|
19
19
|
*/
|
|
20
|
-
import
|
|
20
|
+
import SwarmPlugin from "./index";
|
|
21
21
|
|
|
22
|
-
// Only export
|
|
23
|
-
export
|
|
22
|
+
// Only default export - no named exports!
|
|
23
|
+
export default SwarmPlugin;
|
package/src/schemas/task.ts
CHANGED
|
@@ -87,7 +87,6 @@ export type TaskDecomposition = z.infer<typeof TaskDecompositionSchema>;
|
|
|
87
87
|
*/
|
|
88
88
|
export const DecomposeArgsSchema = z.object({
|
|
89
89
|
task: z.string().min(1),
|
|
90
|
-
max_subtasks: z.number().int().min(1).default(5),
|
|
91
90
|
context: z.string().optional(),
|
|
92
91
|
});
|
|
93
92
|
export type DecomposeArgs = z.infer<typeof DecomposeArgsSchema>;
|
package/src/swarm-decompose.ts
CHANGED
|
@@ -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)
|
|
@@ -690,12 +691,6 @@ export const swarm_delegate_planning = tool({
|
|
|
690
691
|
.string()
|
|
691
692
|
.optional()
|
|
692
693
|
.describe("Additional context to include"),
|
|
693
|
-
max_subtasks: tool.schema
|
|
694
|
-
.number()
|
|
695
|
-
.int()
|
|
696
|
-
.min(1)
|
|
697
|
-
.optional()
|
|
698
|
-
.describe("Suggested max subtasks (optional - LLM decides if not specified)"),
|
|
699
694
|
strategy: tool.schema
|
|
700
695
|
.enum(["auto", "file-based", "feature-based", "risk-based"])
|
|
701
696
|
.optional()
|
|
@@ -728,6 +723,25 @@ export const swarm_delegate_planning = tool({
|
|
|
728
723
|
strategyReasoning = selection.reasoning;
|
|
729
724
|
}
|
|
730
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
|
+
|
|
731
745
|
// Query CASS for similar past tasks
|
|
732
746
|
let cassContext = "";
|
|
733
747
|
let cassResultInfo: {
|
|
@@ -797,8 +811,7 @@ export const swarm_delegate_planning = tool({
|
|
|
797
811
|
.replace("{strategy_guidelines}", strategyGuidelines)
|
|
798
812
|
.replace("{context_section}", contextSection)
|
|
799
813
|
.replace("{cass_history}", cassContext || "")
|
|
800
|
-
.replace("{skills_context}", skillsContext || "")
|
|
801
|
-
.replace("{max_subtasks}", (args.max_subtasks ?? 5).toString());
|
|
814
|
+
.replace("{skills_context}", skillsContext || "");
|
|
802
815
|
|
|
803
816
|
// Add strict JSON-only instructions for the subagent
|
|
804
817
|
const subagentInstructions = `
|
package/src/swarm-orchestrate.ts
CHANGED
|
@@ -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(
|
package/src/swarm-prompts.ts
CHANGED
|
@@ -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,
|
package/src/swarm-review.ts
CHANGED
|
@@ -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
|
|
@@ -79,7 +79,6 @@ describe("swarm_decompose", () => {
|
|
|
79
79
|
const result = await swarm_decompose.execute(
|
|
80
80
|
{
|
|
81
81
|
task: "Add user authentication with OAuth",
|
|
82
|
-
max_subtasks: 3,
|
|
83
82
|
},
|
|
84
83
|
mockContext,
|
|
85
84
|
);
|
|
@@ -97,7 +96,6 @@ describe("swarm_decompose", () => {
|
|
|
97
96
|
const result = await swarm_decompose.execute(
|
|
98
97
|
{
|
|
99
98
|
task: "Refactor the API routes",
|
|
100
|
-
max_subtasks: 5,
|
|
101
99
|
context: "Using Next.js App Router with RSC",
|
|
102
100
|
},
|
|
103
101
|
mockContext,
|
|
@@ -109,20 +107,6 @@ describe("swarm_decompose", () => {
|
|
|
109
107
|
expect(parsed.prompt).toContain("Additional Context");
|
|
110
108
|
});
|
|
111
109
|
|
|
112
|
-
it("uses default max_subtasks when not provided", async () => {
|
|
113
|
-
const result = await swarm_decompose.execute(
|
|
114
|
-
{
|
|
115
|
-
task: "Simple task",
|
|
116
|
-
max_subtasks: 5, // Explicit default since schema requires it
|
|
117
|
-
},
|
|
118
|
-
mockContext,
|
|
119
|
-
);
|
|
120
|
-
|
|
121
|
-
const parsed = JSON.parse(result);
|
|
122
|
-
|
|
123
|
-
// Prompt should say "as many as needed" (max_subtasks no longer in template)
|
|
124
|
-
expect(parsed.prompt).toContain("as many as needed");
|
|
125
|
-
});
|
|
126
110
|
});
|
|
127
111
|
|
|
128
112
|
// ============================================================================
|
|
@@ -262,7 +246,6 @@ describe("swarm_plan_prompt", () => {
|
|
|
262
246
|
const result = await swarm_plan_prompt.execute(
|
|
263
247
|
{
|
|
264
248
|
task: "Add user settings page",
|
|
265
|
-
max_subtasks: 3,
|
|
266
249
|
query_cass: false, // Disable CASS to isolate test
|
|
267
250
|
},
|
|
268
251
|
mockContext,
|
|
@@ -281,7 +264,6 @@ describe("swarm_plan_prompt", () => {
|
|
|
281
264
|
{
|
|
282
265
|
task: "Do something",
|
|
283
266
|
strategy: "risk-based",
|
|
284
|
-
max_subtasks: 3,
|
|
285
267
|
query_cass: false,
|
|
286
268
|
},
|
|
287
269
|
mockContext,
|
|
@@ -296,7 +278,6 @@ describe("swarm_plan_prompt", () => {
|
|
|
296
278
|
const result = await swarm_plan_prompt.execute(
|
|
297
279
|
{
|
|
298
280
|
task: "Refactor the codebase",
|
|
299
|
-
max_subtasks: 4,
|
|
300
281
|
query_cass: false,
|
|
301
282
|
},
|
|
302
283
|
mockContext,
|
|
@@ -314,7 +295,6 @@ describe("swarm_plan_prompt", () => {
|
|
|
314
295
|
const result = await swarm_plan_prompt.execute(
|
|
315
296
|
{
|
|
316
297
|
task: "Build new feature",
|
|
317
|
-
max_subtasks: 3,
|
|
318
298
|
query_cass: false,
|
|
319
299
|
},
|
|
320
300
|
mockContext,
|
|
@@ -330,7 +310,6 @@ describe("swarm_plan_prompt", () => {
|
|
|
330
310
|
const result = await swarm_plan_prompt.execute(
|
|
331
311
|
{
|
|
332
312
|
task: "Some task",
|
|
333
|
-
max_subtasks: 5,
|
|
334
313
|
query_cass: false,
|
|
335
314
|
},
|
|
336
315
|
mockContext,
|
|
@@ -350,7 +329,6 @@ describe("swarm_plan_prompt", () => {
|
|
|
350
329
|
const result = await swarm_plan_prompt.execute(
|
|
351
330
|
{
|
|
352
331
|
task: "Add feature",
|
|
353
|
-
max_subtasks: 3,
|
|
354
332
|
},
|
|
355
333
|
mockContext,
|
|
356
334
|
);
|
|
@@ -375,7 +353,6 @@ describe("swarm_plan_prompt", () => {
|
|
|
375
353
|
const result = await swarm_plan_prompt.execute(
|
|
376
354
|
{
|
|
377
355
|
task: "Add user profile",
|
|
378
|
-
max_subtasks: 3,
|
|
379
356
|
context: "We use Next.js App Router with server components",
|
|
380
357
|
query_cass: false,
|
|
381
358
|
},
|
|
@@ -387,19 +364,6 @@ describe("swarm_plan_prompt", () => {
|
|
|
387
364
|
expect(parsed.prompt).toContain("server components");
|
|
388
365
|
});
|
|
389
366
|
|
|
390
|
-
it("includes max_subtasks in prompt", async () => {
|
|
391
|
-
const result = await swarm_plan_prompt.execute(
|
|
392
|
-
{
|
|
393
|
-
task: "Build something",
|
|
394
|
-
max_subtasks: 7,
|
|
395
|
-
query_cass: false,
|
|
396
|
-
},
|
|
397
|
-
mockContext,
|
|
398
|
-
);
|
|
399
|
-
const parsed = JSON.parse(result);
|
|
400
|
-
|
|
401
|
-
expect(parsed.prompt).toContain("as many as needed");
|
|
402
|
-
});
|
|
403
367
|
});
|
|
404
368
|
|
|
405
369
|
describe("swarm_validate_decomposition", () => {
|
|
@@ -920,7 +884,6 @@ describe("full swarm flow (integration)", () => {
|
|
|
920
884
|
const decomposeResult = await swarm_decompose.execute(
|
|
921
885
|
{
|
|
922
886
|
task: "Add unit tests for auth module",
|
|
923
|
-
max_subtasks: 2,
|
|
924
887
|
},
|
|
925
888
|
ctx,
|
|
926
889
|
);
|
|
@@ -1228,7 +1191,6 @@ describe("swarm_init", () => {
|
|
|
1228
1191
|
const result = await swarm_decompose.execute(
|
|
1229
1192
|
{
|
|
1230
1193
|
task: "Add user authentication",
|
|
1231
|
-
max_subtasks: 3,
|
|
1232
1194
|
query_cass: true, // Request CASS but it may not be available
|
|
1233
1195
|
},
|
|
1234
1196
|
mockContext,
|
|
@@ -1249,7 +1211,6 @@ describe("swarm_init", () => {
|
|
|
1249
1211
|
const result = await swarm_decompose.execute(
|
|
1250
1212
|
{
|
|
1251
1213
|
task: "Add user authentication",
|
|
1252
|
-
max_subtasks: 3,
|
|
1253
1214
|
query_cass: false, // Explicitly skip CASS
|
|
1254
1215
|
},
|
|
1255
1216
|
mockContext,
|
|
@@ -1264,7 +1225,6 @@ describe("swarm_init", () => {
|
|
|
1264
1225
|
const result = await swarm_decompose.execute(
|
|
1265
1226
|
{
|
|
1266
1227
|
task: "Build feature X",
|
|
1267
|
-
max_subtasks: 3,
|
|
1268
1228
|
},
|
|
1269
1229
|
mockContext,
|
|
1270
1230
|
);
|