opencode-swarm-plugin 0.38.0 → 0.39.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 (69) hide show
  1. package/.env +2 -0
  2. package/.hive/eval-results.json +26 -0
  3. package/.hive/issues.jsonl +11 -0
  4. package/.hive/memories.jsonl +23 -1
  5. package/.opencode/eval-history.jsonl +12 -0
  6. package/CHANGELOG.md +130 -0
  7. package/README.md +29 -12
  8. package/bin/swarm.test.ts +475 -0
  9. package/bin/swarm.ts +383 -0
  10. package/dist/compaction-hook.d.ts +1 -1
  11. package/dist/compaction-hook.d.ts.map +1 -1
  12. package/dist/compaction-prompt-scoring.d.ts +124 -0
  13. package/dist/compaction-prompt-scoring.d.ts.map +1 -0
  14. package/dist/eval-capture.d.ts +81 -1
  15. package/dist/eval-capture.d.ts.map +1 -1
  16. package/dist/eval-gates.d.ts +84 -0
  17. package/dist/eval-gates.d.ts.map +1 -0
  18. package/dist/eval-history.d.ts +117 -0
  19. package/dist/eval-history.d.ts.map +1 -0
  20. package/dist/eval-learning.d.ts +216 -0
  21. package/dist/eval-learning.d.ts.map +1 -0
  22. package/dist/index.d.ts +44 -0
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +370 -13
  25. package/dist/plugin.js +203 -13
  26. package/dist/post-compaction-tracker.d.ts +133 -0
  27. package/dist/post-compaction-tracker.d.ts.map +1 -0
  28. package/dist/swarm-orchestrate.d.ts +23 -0
  29. package/dist/swarm-orchestrate.d.ts.map +1 -1
  30. package/dist/swarm-prompts.d.ts +25 -1
  31. package/dist/swarm-prompts.d.ts.map +1 -1
  32. package/dist/swarm.d.ts +4 -0
  33. package/dist/swarm.d.ts.map +1 -1
  34. package/evals/README.md +589 -105
  35. package/evals/compaction-prompt.eval.ts +149 -0
  36. package/evals/coordinator-behavior.eval.ts +8 -8
  37. package/evals/fixtures/compaction-prompt-cases.ts +305 -0
  38. package/evals/lib/compaction-loader.test.ts +248 -0
  39. package/evals/lib/compaction-loader.ts +320 -0
  40. package/evals/lib/data-loader.test.ts +345 -0
  41. package/evals/lib/data-loader.ts +107 -6
  42. package/evals/scorers/compaction-prompt-scorers.ts +145 -0
  43. package/evals/scorers/compaction-scorers.ts +13 -13
  44. package/evals/scorers/coordinator-discipline.evalite-test.ts +3 -2
  45. package/evals/scorers/coordinator-discipline.ts +13 -13
  46. package/examples/plugin-wrapper-template.ts +117 -0
  47. package/package.json +7 -5
  48. package/scripts/migrate-unknown-sessions.ts +349 -0
  49. package/src/compaction-capture.integration.test.ts +257 -0
  50. package/src/compaction-hook.test.ts +42 -0
  51. package/src/compaction-hook.ts +81 -0
  52. package/src/compaction-prompt-scorers.test.ts +299 -0
  53. package/src/compaction-prompt-scoring.ts +298 -0
  54. package/src/eval-capture.test.ts +422 -0
  55. package/src/eval-capture.ts +94 -2
  56. package/src/eval-gates.test.ts +306 -0
  57. package/src/eval-gates.ts +218 -0
  58. package/src/eval-history.test.ts +508 -0
  59. package/src/eval-history.ts +214 -0
  60. package/src/eval-learning.test.ts +378 -0
  61. package/src/eval-learning.ts +360 -0
  62. package/src/index.ts +61 -1
  63. package/src/post-compaction-tracker.test.ts +251 -0
  64. package/src/post-compaction-tracker.ts +237 -0
  65. package/src/swarm-decompose.ts +2 -2
  66. package/src/swarm-orchestrate.ts +2 -2
  67. package/src/swarm-prompts.ts +2 -2
  68. package/src/swarm-review.ts +3 -3
  69. /package/evals/{evalite.config.ts → evalite.config.ts.bak} +0 -0
package/dist/plugin.js CHANGED
@@ -22198,6 +22198,7 @@ __export(exports_eval_capture, {
22198
22198
  captureHumanFeedback: () => captureHumanFeedback,
22199
22199
  captureDecomposition: () => captureDecomposition,
22200
22200
  captureCoordinatorEvent: () => captureCoordinatorEvent,
22201
+ captureCompactionEvent: () => captureCompactionEvent,
22201
22202
  appendEvalRecord: () => appendEvalRecord,
22202
22203
  SubtaskOutcomeSchema: () => SubtaskOutcomeSchema,
22203
22204
  EvalRecordSchema: () => EvalRecordSchema,
@@ -22406,6 +22407,17 @@ function captureCoordinatorEvent(event) {
22406
22407
  `;
22407
22408
  fs.appendFileSync(sessionPath, line, "utf-8");
22408
22409
  }
22410
+ function captureCompactionEvent(params) {
22411
+ const event = {
22412
+ session_id: params.session_id,
22413
+ epic_id: params.epic_id,
22414
+ timestamp: new Date().toISOString(),
22415
+ event_type: "COMPACTION",
22416
+ compaction_type: params.compaction_type,
22417
+ payload: params.payload
22418
+ };
22419
+ captureCoordinatorEvent(event);
22420
+ }
22409
22421
  function readSessionEvents(sessionId) {
22410
22422
  const sessionPath = getSessionPath(sessionId);
22411
22423
  if (!fs.existsSync(sessionPath)) {
@@ -22517,6 +22529,20 @@ var init_eval_capture = __esm(() => {
22517
22529
  "epic_complete"
22518
22530
  ]),
22519
22531
  payload: exports_external.any()
22532
+ }),
22533
+ exports_external.object({
22534
+ session_id: exports_external.string(),
22535
+ epic_id: exports_external.string(),
22536
+ timestamp: exports_external.string(),
22537
+ event_type: exports_external.literal("COMPACTION"),
22538
+ compaction_type: exports_external.enum([
22539
+ "detection_complete",
22540
+ "prompt_generated",
22541
+ "context_injected",
22542
+ "resumption_started",
22543
+ "tool_call_tracked"
22544
+ ]),
22545
+ payload: exports_external.any()
22520
22546
  })
22521
22547
  ]);
22522
22548
  CoordinatorSessionSchema = exports_external.object({
@@ -42303,7 +42329,7 @@ var swarm_delegate_planning = tool({
42303
42329
  strategy: tool.schema.enum(["auto", "file-based", "feature-based", "risk-based"]).optional().default("auto").describe("Decomposition strategy (default: auto-detect)"),
42304
42330
  query_cass: tool.schema.boolean().optional().default(true).describe("Query CASS for similar past tasks (default: true)")
42305
42331
  },
42306
- async execute(args) {
42332
+ async execute(args, _ctx) {
42307
42333
  const { selectStrategy: selectStrategy2, formatStrategyGuidelines: formatStrategyGuidelines2 } = await Promise.resolve().then(() => (init_swarm_strategies(), exports_swarm_strategies));
42308
42334
  const { formatMemoryQueryForDecomposition: formatMemoryQueryForDecomposition2 } = await Promise.resolve().then(() => (init_learning(), exports_learning));
42309
42335
  const { listSkills: listSkills2, getSkillsContextForSwarm: getSkillsContextForSwarm2, findRelevantSkills: findRelevantSkills2 } = await Promise.resolve().then(() => (init_skills(), exports_skills));
@@ -42319,7 +42345,7 @@ var swarm_delegate_planning = tool({
42319
42345
  }
42320
42346
  try {
42321
42347
  captureCoordinatorEvent({
42322
- session_id: process.env.OPENCODE_SESSION_ID || "unknown",
42348
+ session_id: _ctx.sessionID || "unknown",
42323
42349
  epic_id: "planning",
42324
42350
  timestamp: new Date().toISOString(),
42325
42351
  event_type: "DECISION",
@@ -44635,7 +44661,7 @@ var swarm_review_feedback = tool({
44635
44661
  summary: exports_external.string().optional().describe("Review summary"),
44636
44662
  issues: exports_external.string().optional().describe("JSON array of ReviewIssue objects (for needs_changes)")
44637
44663
  },
44638
- async execute(args) {
44664
+ async execute(args, _ctx) {
44639
44665
  let parsedIssues = [];
44640
44666
  if (args.issues) {
44641
44667
  try {
@@ -44658,7 +44684,7 @@ var swarm_review_feedback = tool({
44658
44684
  markReviewApproved(args.task_id);
44659
44685
  try {
44660
44686
  captureCoordinatorEvent({
44661
- session_id: process.env.OPENCODE_SESSION_ID || "unknown",
44687
+ session_id: _ctx.sessionID || "unknown",
44662
44688
  epic_id: epicId,
44663
44689
  timestamp: new Date().toISOString(),
44664
44690
  event_type: "DECISION",
@@ -44696,7 +44722,7 @@ You may now complete the task with \`swarm_complete\`.`,
44696
44722
  const remaining = MAX_REVIEW_ATTEMPTS - attemptNumber;
44697
44723
  try {
44698
44724
  captureCoordinatorEvent({
44699
- session_id: process.env.OPENCODE_SESSION_ID || "unknown",
44725
+ session_id: _ctx.sessionID || "unknown",
44700
44726
  epic_id: epicId,
44701
44727
  timestamp: new Date().toISOString(),
44702
44728
  event_type: "DECISION",
@@ -45668,10 +45694,29 @@ Files touched: ${args.files_touched?.join(", ") || "none recorded"}`,
45668
45694
  reason: "No files_owned contract found (non-epic subtask or decomposition event missing)"
45669
45695
  }
45670
45696
  };
45697
+ try {
45698
+ const { captureSubtaskOutcome: captureSubtaskOutcome2 } = await Promise.resolve().then(() => (init_eval_capture(), exports_eval_capture));
45699
+ const durationMs2 = args.start_time ? Date.now() - args.start_time : 0;
45700
+ const evalEpicId = cell.parent_id || epicId2;
45701
+ captureSubtaskOutcome2({
45702
+ epicId: evalEpicId,
45703
+ projectPath: args.project_key,
45704
+ beadId: args.bead_id,
45705
+ title: cell.title,
45706
+ plannedFiles: args.planned_files || [],
45707
+ actualFiles: args.files_touched || [],
45708
+ durationMs: durationMs2,
45709
+ errorCount: args.error_count || 0,
45710
+ retryCount: args.retry_count || 0,
45711
+ success: true
45712
+ });
45713
+ } catch (error45) {
45714
+ console.warn("[swarm_complete] Failed to capture subtask outcome:", error45);
45715
+ }
45671
45716
  try {
45672
45717
  const durationMs2 = args.start_time ? Date.now() - args.start_time : 0;
45673
45718
  captureCoordinatorEvent({
45674
- session_id: process.env.OPENCODE_SESSION_ID || "unknown",
45719
+ session_id: _ctx.sessionID || "unknown",
45675
45720
  epic_id: epicId2,
45676
45721
  timestamp: new Date().toISOString(),
45677
45722
  event_type: "OUTCOME",
@@ -45753,7 +45798,7 @@ ${errorStack.slice(0, 1000)}
45753
45798
  try {
45754
45799
  const durationMs = args.start_time ? Date.now() - args.start_time : 0;
45755
45800
  captureCoordinatorEvent({
45756
- session_id: process.env.OPENCODE_SESSION_ID || "unknown",
45801
+ session_id: _ctx.sessionID || "unknown",
45757
45802
  epic_id: epicId,
45758
45803
  timestamp: new Date().toISOString(),
45759
45804
  event_type: "OUTCOME",
@@ -45820,7 +45865,9 @@ var swarm_record_outcome = tool({
45820
45865
  "user_cancelled",
45821
45866
  "unknown"
45822
45867
  ]).optional().describe("Failure classification (only when success=false). Auto-classified if not provided."),
45823
- failure_details: tool.schema.string().optional().describe("Detailed failure context (error message, stack trace, etc.)")
45868
+ failure_details: tool.schema.string().optional().describe("Detailed failure context (error message, stack trace, etc.)"),
45869
+ project_path: tool.schema.string().optional().describe("Project path (for finalizing eval records when all subtasks complete)"),
45870
+ epic_id: tool.schema.string().optional().describe("Epic ID (for finalizing eval records when all subtasks complete)")
45824
45871
  },
45825
45872
  async execute(args) {
45826
45873
  const signals = {
@@ -45842,6 +45889,18 @@ var swarm_record_outcome = tool({
45842
45889
  const validated = OutcomeSignalsSchema.parse(signals);
45843
45890
  const scored = scoreImplicitFeedback(validated, DEFAULT_LEARNING_CONFIG);
45844
45891
  const errorStats = await globalErrorAccumulator.getErrorStats(args.bead_id);
45892
+ let finalizedRecord = null;
45893
+ if (args.project_path && args.epic_id) {
45894
+ try {
45895
+ const { finalizeEvalRecord: finalizeEvalRecord2 } = await Promise.resolve().then(() => (init_eval_capture(), exports_eval_capture));
45896
+ finalizedRecord = finalizeEvalRecord2({
45897
+ epicId: args.epic_id,
45898
+ projectPath: args.project_path
45899
+ });
45900
+ } catch (error45) {
45901
+ console.warn("[swarm_record_outcome] Failed to finalize eval record:", error45);
45902
+ }
45903
+ }
45845
45904
  const criteriaToScore = args.criteria ?? [
45846
45905
  "type_safe",
45847
45906
  "no_bugs",
@@ -45883,6 +45942,7 @@ var swarm_record_outcome = tool({
45883
45942
  accumulated_errors: errorStats.total,
45884
45943
  unresolved_errors: errorStats.unresolved
45885
45944
  },
45945
+ finalized_eval_record: finalizedRecord || undefined,
45886
45946
  note: "Feedback events should be stored for criterion weight calculation. Use learning.ts functions to apply weights."
45887
45947
  }, null, 2);
45888
45948
  }
@@ -45914,12 +45974,31 @@ async function runResearchPhase(task, projectPath, options2) {
45914
45974
  if (techStack.length === 0) {
45915
45975
  return {
45916
45976
  tech_stack: [],
45977
+ spawn_instructions: [],
45917
45978
  summaries: {},
45918
45979
  memory_ids: []
45919
45980
  };
45920
45981
  }
45982
+ const spawnInstructions = [];
45983
+ for (const tech of techStack) {
45984
+ const researchId = `research-${tech}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
45985
+ const prompt = formatResearcherPrompt({
45986
+ research_id: researchId,
45987
+ epic_id: "standalone-research",
45988
+ tech_stack: [tech],
45989
+ project_path: projectPath,
45990
+ check_upgrades: options2?.checkUpgrades ?? false
45991
+ });
45992
+ spawnInstructions.push({
45993
+ research_id: researchId,
45994
+ tech,
45995
+ prompt,
45996
+ subagent_type: "swarm/researcher"
45997
+ });
45998
+ }
45921
45999
  return {
45922
46000
  tech_stack: techStack,
46001
+ spawn_instructions: spawnInstructions,
45923
46002
  summaries: {},
45924
46003
  memory_ids: []
45925
46004
  };
@@ -47153,7 +47232,7 @@ var swarm_spawn_subtask = tool({
47153
47232
  }).optional().describe("Recovery context from checkpoint compaction"),
47154
47233
  model: tool.schema.string().optional().describe("Optional explicit model override (auto-selected if not provided)")
47155
47234
  },
47156
- async execute(args) {
47235
+ async execute(args, _ctx) {
47157
47236
  const prompt = formatSubtaskPromptV2({
47158
47237
  bead_id: args.bead_id,
47159
47238
  epic_id: args.epic_id,
@@ -47182,7 +47261,7 @@ var swarm_spawn_subtask = tool({
47182
47261
  const postCompletionInstructions = COORDINATOR_POST_WORKER_CHECKLIST.replace(/{project_key}/g, args.project_path || "$PWD").replace(/{epic_id}/g, args.epic_id).replace(/{task_id}/g, args.bead_id).replace(/{files_touched}/g, filesJoined).replace(/{worker_id}/g, "worker");
47183
47262
  try {
47184
47263
  captureCoordinatorEvent({
47185
- session_id: process.env.OPENCODE_SESSION_ID || "unknown",
47264
+ session_id: _ctx.sessionID || "unknown",
47186
47265
  epic_id: args.epic_id,
47187
47266
  timestamp: new Date().toISOString(),
47188
47267
  event_type: "DECISION",
@@ -63331,9 +63410,21 @@ function getLog() {
63331
63410
  }
63332
63411
  return _logger;
63333
63412
  }
63334
- var SWARM_COMPACTION_CONTEXT = `## \uD83D\uDC1D SWARM ACTIVE - You Are The COORDINATOR
63413
+ var SWARM_COMPACTION_CONTEXT = `
63414
+ ┌─────────────────────────────────────────────────────────────┐
63415
+ │ │
63416
+ │ \uD83D\uDC1D YOU ARE THE COORDINATOR \uD83D\uDC1D │
63417
+ │ │
63418
+ │ NOT A WORKER. NOT AN IMPLEMENTER. │
63419
+ │ YOU ORCHESTRATE. │
63420
+ │ │
63421
+ └─────────────────────────────────────────────────────────────┘
63422
+
63423
+ ## \uD83C\uDFAF NON-NEGOTIABLE: YOU ARE THE COORDINATOR
63335
63424
 
63336
- Context was compacted but the swarm is still running. You are the **COORDINATOR**.
63425
+ Context was compacted but the swarm is still running. **YOU ARE THE COORDINATOR.**
63426
+
63427
+ Your role is ORCHESTRATION, not implementation. When you catch yourself about to do work directly, STOP.
63337
63428
 
63338
63429
  ### ⛔ NEVER DO THESE (Coordinator Anti-Patterns)
63339
63430
 
@@ -63344,9 +63435,27 @@ Context was compacted but the swarm is still running. You are the **COORDINATOR*
63344
63435
  - ❌ **NEVER** implement features yourself - SPAWN A WORKER
63345
63436
  - ❌ **NEVER** "just do it myself to save time" - NO. SPAWN A WORKER.
63346
63437
  - ❌ **NEVER** reserve files with \`swarmmail_reserve\` - Workers reserve files
63438
+ - ❌ **NEVER** fetch files/docs directly - SPAWN A RESEARCHER
63347
63439
 
63348
63440
  **If you catch yourself about to edit a file, STOP. Use \`swarm_spawn_subtask\` instead.**
63349
63441
 
63442
+ ### \uD83D\uDEAB FORBIDDEN TOOLS (Coordinators MUST delegate these)
63443
+
63444
+ **NEVER use these tools directly. ALWAYS spawn a researcher worker via \`swarm_spawn_researcher\`:**
63445
+
63446
+ **Repository fetching:**
63447
+ - \`repo-crawl_file\`, \`repo-crawl_readme\`, \`repo-crawl_search\`, \`repo-crawl_structure\`, \`repo-crawl_tree\`
63448
+ - \`repo-autopsy_*\` (all repo-autopsy tools)
63449
+
63450
+ **Web/documentation fetching:**
63451
+ - \`webfetch\`, \`fetch_fetch\`
63452
+ - \`context7_resolve-library-id\`, \`context7_get-library-docs\`
63453
+
63454
+ **Knowledge base:**
63455
+ - \`pdf-brain_search\`, \`pdf-brain_read\`
63456
+
63457
+ **If you need external data:** Use \`swarm_spawn_researcher\` with a clear research task. The researcher will fetch, summarize, and return findings.
63458
+
63350
63459
  ### ✅ ALWAYS DO THESE (Coordinator Checklist)
63351
63460
 
63352
63461
  On resume, execute this checklist IN ORDER:
@@ -63396,6 +63505,87 @@ Extract from session context:
63396
63505
  - **Review work** - Use \`swarm_review\` and \`swarm_review_feedback\` for completed work
63397
63506
  - **Close the loop** - When all subtasks done, verify and close the epic
63398
63507
 
63508
+ **You are the COORDINATOR. You orchestrate. You do NOT implement. Spawn workers.**
63509
+
63510
+ ---
63511
+
63512
+ ## \uD83D\uDCCB FULL COORDINATOR WORKFLOW (Reference)
63513
+
63514
+ You are ALWAYS swarming. Here is the complete workflow for any new work:
63515
+
63516
+ ### Phase 1.5: Research Phase (FOR COMPLEX TASKS)
63517
+
63518
+ **If the task requires understanding unfamiliar technologies, spawn a researcher FIRST:**
63519
+
63520
+ \`\`\`
63521
+ swarm_spawn_researcher(
63522
+ research_id="research-<topic>",
63523
+ epic_id="<epic-id>",
63524
+ tech_stack=["<technology>"],
63525
+ project_path="<path>"
63526
+ )
63527
+ // Then spawn with Task(subagent_type="swarm/researcher", prompt="<from above>")
63528
+ \`\`\`
63529
+
63530
+ ### Phase 2: Knowledge Gathering
63531
+
63532
+ \`\`\`
63533
+ semantic-memory_find(query="<task keywords>", limit=5) # Past learnings
63534
+ cass_search(query="<task description>", limit=5) # Similar past tasks
63535
+ skills_list() # Available skills
63536
+ \`\`\`
63537
+
63538
+ ### Phase 3: Decompose
63539
+
63540
+ \`\`\`
63541
+ swarm_select_strategy(task="<task>")
63542
+ swarm_plan_prompt(task="<task>", context="<synthesized knowledge>")
63543
+ swarm_validate_decomposition(response="<CellTree JSON>")
63544
+ \`\`\`
63545
+
63546
+ ### Phase 4: Create Cells
63547
+
63548
+ \`hive_create_epic(epic_title="<task>", subtasks=[...])\`
63549
+
63550
+ ### Phase 5: DO NOT Reserve Files
63551
+
63552
+ > **⚠️ Coordinator NEVER reserves files.** Workers reserve their own files.
63553
+
63554
+ ### Phase 6: Spawn Workers
63555
+
63556
+ \`\`\`
63557
+ swarm_spawn_subtask(bead_id, epic_id, title, files, shared_context, project_path)
63558
+ Task(subagent_type="swarm/worker", prompt="<from above>")
63559
+ \`\`\`
63560
+
63561
+ ### Phase 7: MANDATORY Review Loop
63562
+
63563
+ **AFTER EVERY Task() RETURNS:**
63564
+
63565
+ 1. \`swarmmail_inbox()\` - Check for messages
63566
+ 2. \`swarm_review(project_key, epic_id, task_id, files_touched)\` - Generate review
63567
+ 3. Evaluate against epic goals
63568
+ 4. \`swarm_review_feedback(project_key, task_id, worker_id, status, issues)\`
63569
+
63570
+ **If needs_changes:**
63571
+ \`\`\`
63572
+ swarm_spawn_retry(bead_id, epic_id, original_prompt, attempt, issues, diff, files, project_path)
63573
+ // Spawn NEW worker with Task() using retry prompt
63574
+ // Max 3 attempts before marking task blocked
63575
+ \`\`\`
63576
+
63577
+ ### Phase 8: Complete
63578
+
63579
+ \`hive_sync()\` - Sync all cells to git
63580
+
63581
+ ## Strategy Reference
63582
+
63583
+ | Strategy | Best For | Keywords |
63584
+ | -------------- | ------------------------ | -------------------------------------- |
63585
+ | file-based | Refactoring, migrations | refactor, migrate, rename, update all |
63586
+ | feature-based | New features | add, implement, build, create, feature |
63587
+ | risk-based | Bug fixes, security | fix, bug, security, critical, urgent |
63588
+
63399
63589
  **You are the COORDINATOR. You orchestrate. You do NOT implement. Spawn workers.**
63400
63590
  `;
63401
63591
  var SWARM_DETECTION_FALLBACK = `## \uD83D\uDC1D Swarm Detection - Check Your Context
@@ -64070,7 +64260,7 @@ var SwarmPlugin = async (input) => {
64070
64260
  if (isInCoordinatorContext()) {
64071
64261
  const ctx = getCoordinatorContext();
64072
64262
  const violation = detectCoordinatorViolation({
64073
- sessionId: ctx.sessionId || "unknown",
64263
+ sessionId: input2.sessionID || "unknown",
64074
64264
  epicId: ctx.epicId || "unknown",
64075
64265
  toolName,
64076
64266
  toolArgs: output.args,
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Post-Compaction Tool Call Tracker
3
+ *
4
+ * Tracks tool calls after compaction resumption to detect coordinator violations
5
+ * and provide learning signals for eval-driven development.
6
+ *
7
+ * ## Purpose
8
+ *
9
+ * When context is compacted, the continuation agent needs observation to learn
10
+ * if it's following coordinator discipline. This tracker:
11
+ *
12
+ * 1. Emits resumption_started on first tool call (marks compaction exit)
13
+ * 2. Tracks up to N tool calls (default 20) with violation detection
14
+ * 3. Stops tracking after limit to avoid noise in long sessions
15
+ *
16
+ * ## Coordinator Violations Detected
17
+ *
18
+ * - **Edit/Write**: Coordinators NEVER edit files - spawn worker instead
19
+ * - **swarmmail_reserve/agentmail_reserve**: Workers reserve, not coordinators
20
+ *
21
+ * ## Integration
22
+ *
23
+ * Used by compaction hook to wire tool.call events → eval capture.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const tracker = createPostCompactionTracker({
28
+ * sessionId: "session-123",
29
+ * epicId: "bd-epic-456",
30
+ * onEvent: captureCompactionEvent,
31
+ * });
32
+ *
33
+ * // Wire to OpenCode hook
34
+ * hooks["tool.call"] = (input) => {
35
+ * tracker.trackToolCall({
36
+ * tool: input.tool,
37
+ * args: input.args,
38
+ * timestamp: Date.now(),
39
+ * });
40
+ * };
41
+ * ```
42
+ */
43
+ /**
44
+ * Tool call event structure
45
+ */
46
+ export interface ToolCallEvent {
47
+ tool: string;
48
+ args: Record<string, unknown>;
49
+ timestamp: number;
50
+ }
51
+ /**
52
+ * Compaction event payload (matches eval-capture.ts structure)
53
+ */
54
+ export interface CompactionEvent {
55
+ session_id: string;
56
+ epic_id: string;
57
+ compaction_type: "detection_complete" | "prompt_generated" | "context_injected" | "resumption_started" | "tool_call_tracked";
58
+ payload: {
59
+ session_id?: string;
60
+ epic_id?: string;
61
+ tool?: string;
62
+ args?: Record<string, unknown>;
63
+ call_number?: number;
64
+ is_coordinator_violation?: boolean;
65
+ violation_reason?: string;
66
+ timestamp?: number;
67
+ };
68
+ }
69
+ /**
70
+ * Tracker configuration
71
+ */
72
+ export interface PostCompactionTrackerConfig {
73
+ sessionId: string;
74
+ epicId: string;
75
+ onEvent: (event: CompactionEvent) => void;
76
+ maxCalls?: number;
77
+ }
78
+ /**
79
+ * Post-compaction tracker instance
80
+ */
81
+ export interface PostCompactionTracker {
82
+ trackToolCall(event: ToolCallEvent): void;
83
+ isTracking(): boolean;
84
+ }
85
+ /**
86
+ * Default maximum number of tool calls to track
87
+ *
88
+ * Chosen to balance:
89
+ * - Enough data for pattern detection (20 calls is ~2-3 minutes of coordinator work)
90
+ * - Avoiding noise pollution in long sessions
91
+ */
92
+ export declare const DEFAULT_MAX_TRACKED_CALLS = 20;
93
+ /**
94
+ * Check if tool call is a coordinator violation
95
+ *
96
+ * @param tool - Tool name from OpenCode tool.call hook
97
+ * @returns Violation status with reason if forbidden
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const result = isCoordinatorViolation("edit");
102
+ * // { isViolation: true, reason: "Coordinators NEVER edit..." }
103
+ *
104
+ * const result = isCoordinatorViolation("read");
105
+ * // { isViolation: false }
106
+ * ```
107
+ */
108
+ export declare function isCoordinatorViolation(tool: string): {
109
+ isViolation: boolean;
110
+ reason?: string;
111
+ };
112
+ /**
113
+ * Create a post-compaction tool call tracker
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * const tracker = createPostCompactionTracker({
118
+ * sessionId: "session-123",
119
+ * epicId: "bd-epic-456",
120
+ * onEvent: (event) => captureCompactionEvent(event),
121
+ * maxCalls: 20
122
+ * });
123
+ *
124
+ * // Track tool calls
125
+ * tracker.trackToolCall({
126
+ * tool: "read",
127
+ * args: { filePath: "/test.ts" },
128
+ * timestamp: Date.now()
129
+ * });
130
+ * ```
131
+ */
132
+ export declare function createPostCompactionTracker(config: PostCompactionTrackerConfig): PostCompactionTracker;
133
+ //# sourceMappingURL=post-compaction-tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"post-compaction-tracker.d.ts","sourceRoot":"","sources":["../src/post-compaction-tracker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;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;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAMA;AAMD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,2BAA2B,GAClC,qBAAqB,CA2DvB"}
@@ -283,6 +283,8 @@ export declare const swarm_record_outcome: {
283
283
  user_cancelled: "user_cancelled";
284
284
  }>>;
285
285
  failure_details: z.ZodOptional<z.ZodString>;
286
+ project_path: z.ZodOptional<z.ZodString>;
287
+ epic_id: z.ZodOptional<z.ZodString>;
286
288
  };
287
289
  execute(args: {
288
290
  bead_id: string;
@@ -295,6 +297,8 @@ export declare const swarm_record_outcome: {
295
297
  strategy?: "file-based" | "feature-based" | "risk-based" | "research-based" | undefined;
296
298
  failure_mode?: "timeout" | "unknown" | "conflict" | "validation" | "tool_failure" | "context_overflow" | "dependency_blocked" | "user_cancelled" | undefined;
297
299
  failure_details?: string | undefined;
300
+ project_path?: string | undefined;
301
+ epic_id?: string | undefined;
298
302
  }, context: import("@opencode-ai/plugin").ToolContext): Promise<string>;
299
303
  };
300
304
  /**
@@ -313,12 +317,27 @@ export declare const swarm_record_outcome: {
313
317
  * ```
314
318
  */
315
319
  export declare function extractTechStack(task: string): string[];
320
+ /**
321
+ * Spawn instruction for a researcher worker
322
+ */
323
+ export interface ResearchSpawnInstruction {
324
+ /** Unique ID for this research task */
325
+ research_id: string;
326
+ /** Technology being researched */
327
+ tech: string;
328
+ /** Full prompt for the researcher agent */
329
+ prompt: string;
330
+ /** Agent type for the Task tool */
331
+ subagent_type: "swarm/researcher";
332
+ }
316
333
  /**
317
334
  * Research result from documentation discovery phase
318
335
  */
319
336
  export interface ResearchResult {
320
337
  /** Technologies identified and researched */
321
338
  tech_stack: string[];
339
+ /** Spawn instructions for researcher workers */
340
+ spawn_instructions: ResearchSpawnInstruction[];
322
341
  /** Summaries keyed by technology name */
323
342
  summaries: Record<string, string>;
324
343
  /** Semantic-memory IDs where research is stored */
@@ -731,6 +750,8 @@ export declare const orchestrateTools: {
731
750
  user_cancelled: "user_cancelled";
732
751
  }>>;
733
752
  failure_details: z.ZodOptional<z.ZodString>;
753
+ project_path: z.ZodOptional<z.ZodString>;
754
+ epic_id: z.ZodOptional<z.ZodString>;
734
755
  };
735
756
  execute(args: {
736
757
  bead_id: string;
@@ -743,6 +764,8 @@ export declare const orchestrateTools: {
743
764
  strategy?: "file-based" | "feature-based" | "risk-based" | "research-based" | undefined;
744
765
  failure_mode?: "timeout" | "unknown" | "conflict" | "validation" | "tool_failure" | "context_overflow" | "dependency_blocked" | "user_cancelled" | undefined;
745
766
  failure_details?: string | undefined;
767
+ project_path?: string | undefined;
768
+ epic_id?: string | undefined;
746
769
  }, context: import("@opencode-ai/plugin").ToolContext): Promise<string>;
747
770
  };
748
771
  swarm_research_phase: {
@@ -1 +1 @@
1
- {"version":3,"file":"swarm-orchestrate.d.ts","sourceRoot":"","sources":["../src/swarm-orchestrate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAGH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAaxB,OAAO,EACL,KAAK,aAAa,EAEnB,MAAM,0BAA0B,CAAC;AAqDlC;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE;IAC5C,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAC1B,sBAAsB,CAAC,EAAE,MAAM,EAAE,CAAC;IAClC,gBAAgB,CAAC,EAAE,MAAM,EAAE,CAAC;IAC5B,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,GAAG,aAAa,CA4BhB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,gBAAgB,CAC9B,aAAa,E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;;GAYG;AACH,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqG3B,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa;;;;;;;;;;CAuGxB,CAAC;AAEH;;;;;;;;GAQG;AACH,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgMtB,CAAC;AAMH,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAe5B,CAAC"}
@@ -39,7 +39,24 @@ export declare const SUBTASK_PROMPT = "You are a swarm agent working on a subtas
39
39
  */
40
40
  export declare const SUBTASK_PROMPT_V2 = "You are a swarm agent working on: **{subtask_title}**\n\n## [IDENTITY]\nAgent: (assigned at spawn)\nCell: {bead_id}\nEpic: {epic_id}\n\n## [TASK]\n{subtask_description}\n\n## [FILES]\nReserved (exclusive):\n{file_list}\n\nOnly modify these files. Need others? Message the coordinator.\n\n## [CONTEXT]\n{shared_context}\n\n{compressed_context}\n\n{error_context}\n\n## [MANDATORY SURVIVAL CHECKLIST]\n\n**CRITICAL: Follow this checklist IN ORDER. Each step builds on the previous.**\n\n### Step 1: Initialize Coordination (REQUIRED - DO THIS FIRST)\n```\nswarmmail_init(project_path=\"{project_path}\", task_description=\"{bead_id}: {subtask_title}\")\n```\n\n**This registers you with the coordination system and enables:**\n- File reservation tracking\n- Inter-agent communication\n- Progress monitoring\n- Conflict detection\n\n**If you skip this step, your work will not be tracked and swarm_complete will fail.**\n\n### Step 2: \uD83E\uDDE0 Query Past Learnings (MANDATORY - BEFORE starting work)\n\n**\u26A0\uFE0F CRITICAL: ALWAYS query semantic memory BEFORE writing ANY code.**\n\n```\nsemantic-memory_find(query=\"<keywords from your task>\", limit=5, expand=true)\n```\n\n**Why this is MANDATORY:**\n- Past agents may have already solved your exact problem\n- Avoids repeating mistakes that wasted 30+ minutes before\n- Discovers project-specific patterns and gotchas\n- Finds known workarounds for tool/library quirks\n\n**Search Query Examples by Task Type:**\n\n- **Bug fix**: Use exact error message or \"<symptom> <component>\"\n- **New feature**: Search \"<domain concept> implementation pattern\"\n- **Refactor**: Query \"<pattern name> migration approach\"\n- **Integration**: Look for \"<library name> gotchas configuration\"\n- **Testing**: Find \"testing <component type> characterization tests\"\n- **Performance**: Search \"<technology> performance optimization\"\n\n**BEFORE you start coding:**\n1. Run semantic-memory_find with keywords from your task\n2. Read the results with expand=true for full content\n3. Check if any memory solves your problem or warns of pitfalls\n4. Adjust your approach based on past learnings\n\n**If you skip this step, you WILL waste time solving already-solved problems.**\n\n### Step 3: Load Relevant Skills (if available)\n```\nskills_list() # See what skills exist\nskills_use(name=\"<relevant-skill>\", context=\"<your task>\") # Load skill\n```\n\n**Common skill triggers:**\n- Writing tests? \u2192 `skills_use(name=\"testing-patterns\")`\n- Breaking dependencies? \u2192 `skills_use(name=\"testing-patterns\")`\n- Multi-agent coordination? \u2192 `skills_use(name=\"swarm-coordination\")`\n- Building a CLI? \u2192 `skills_use(name=\"cli-builder\")`\n\n### Step 4: Reserve Your Files (YOU reserve, not coordinator)\n```\nswarmmail_reserve(\n paths=[{file_list}],\n reason=\"{bead_id}: {subtask_title}\",\n exclusive=true\n)\n```\n\n**Workers reserve their own files.** This prevents edit conflicts with other agents.\n\n### Step 5: Do the Work (TDD MANDATORY)\n\n**Follow RED \u2192 GREEN \u2192 REFACTOR. No exceptions.**\n\n1. **RED**: Write a failing test that describes the expected behavior\n - Test MUST fail before you write implementation\n - If test passes immediately, your test is wrong\n \n2. **GREEN**: Write minimal code to make the test pass\n - Don't over-engineer - just make it green\n - Hardcode if needed, refactor later\n \n3. **REFACTOR**: Clean up while tests stay green\n - Run tests after every change\n - If tests break, undo and try again\n\n```bash\n# Run tests continuously\nbun test <your-test-file> --watch\n```\n\n**Why TDD?**\n- Catches bugs before they exist\n- Documents expected behavior\n- Enables fearless refactoring\n- Proves your code works\n\n### Step 6: Report Progress at Milestones\n```\nswarm_progress(\n project_key=\"{project_path}\",\n agent_name=\"<your-agent-name>\",\n bead_id=\"{bead_id}\",\n status=\"in_progress\",\n progress_percent=25, # or 50, 75\n message=\"<what you just completed>\"\n)\n```\n\n**Report at 25%, 50%, 75% completion.** This:\n- Triggers auto-checkpoint (saves context)\n- Keeps coordinator informed\n- Prevents silent failures\n\n### Step 7: Manual Checkpoint BEFORE Risky Operations\n```\nswarm_checkpoint(\n project_key=\"{project_path}\",\n agent_name=\"<your-agent-name>\",\n bead_id=\"{bead_id}\"\n)\n```\n\n**Call BEFORE:**\n- Large refactors\n- File deletions\n- Breaking API changes\n- Anything that might fail catastrophically\n\n**Checkpoints preserve context so you can recover if things go wrong.**\n\n### Step 8: \uD83D\uDCBE STORE YOUR LEARNINGS (if you discovered something)\n\n**If you learned it the hard way, STORE IT so the next agent doesn't have to.**\n\n```\nsemantic-memory_store(\n information=\"<what you learned, WHY it matters, how to apply it>\",\n tags=\"<domain, tech-stack, pattern-type>\"\n)\n```\n\n**MANDATORY Storage Triggers - Store when you:**\n- \uD83D\uDC1B **Solved a tricky bug** (>15min debugging) - include root cause + solution\n- \uD83D\uDCA1 **Discovered a project-specific pattern** - domain rules, business logic quirks\n- \u26A0\uFE0F **Found a tool/library gotcha** - API quirks, version-specific bugs, workarounds\n- \uD83D\uDEAB **Tried an approach that failed** - anti-patterns to avoid, why it didn't work\n- \uD83C\uDFD7\uFE0F **Made an architectural decision** - reasoning, alternatives considered, tradeoffs\n\n**What Makes a GOOD Memory:**\n\n\u2705 **GOOD** (actionable, explains WHY):\n```\n\"OAuth refresh tokens need 5min buffer before expiry to avoid race conditions.\nWithout buffer, token refresh can fail mid-request if expiry happens between\ncheck and use. Implemented with: if (expiresAt - Date.now() < 300000) refresh()\"\n```\n\n\u274C **BAD** (generic, no context):\n```\n\"Fixed the auth bug by adding a null check\"\n```\n\n**What NOT to Store:**\n- Generic knowledge that's in official documentation\n- Implementation details that change frequently\n- Vague descriptions without context (\"fixed the thing\")\n\n**The WHY matters more than the WHAT.** Future agents need context to apply your learning.\n\n### Step 9: Complete (REQUIRED - releases reservations)\n```\nswarm_complete(\n project_key=\"{project_path}\",\n agent_name=\"<your-agent-name>\",\n bead_id=\"{bead_id}\",\n summary=\"<what you accomplished>\",\n files_touched=[\"list\", \"of\", \"files\"]\n)\n```\n\n**This automatically:**\n- Runs UBS bug scan\n- Releases file reservations\n- Records learning signals\n- Notifies coordinator\n\n**DO NOT manually close the cell with hive_close.** Use swarm_complete.\n\n## [ON-DEMAND RESEARCH]\n\nIf you encounter unknown API behavior or version-specific issues:\n\n1. **Check semantic-memory first:**\n `semantic-memory_find(query=\"<library> <version> <topic>\", limit=3, expand=true)`\n\n2. **If not found, spawn researcher:**\n `swarm_spawn_researcher(research_id=\"{bead_id}-research\", epic_id=\"{epic_id}\", tech_stack=[\"<library>\"], project_path=\"{project_path}\")`\n Then spawn with Task tool: `Task(subagent_type=\"swarm/researcher\", prompt=\"<from above>\")`\n\n3. **Wait for research, then continue**\n\n**Research triggers:**\n- \"I'm not sure how this API works in version X\"\n- \"This might have breaking changes\"\n- \"The docs I remember might be outdated\"\n\n**Don't research:**\n- Standard patterns you're confident about\n- Well-documented, stable APIs\n- Obvious implementations\n\n## [SWARM MAIL COMMUNICATION]\n\n### Check Inbox Regularly\n```\nswarmmail_inbox() # Check for coordinator messages\nswarmmail_read_message(message_id=N) # Read specific message\n```\n\n### When Blocked\n```\nswarmmail_send(\n to=[\"coordinator\"],\n subject=\"BLOCKED: {bead_id}\",\n body=\"<blocker description, what you need>\",\n importance=\"high\",\n thread_id=\"{epic_id}\"\n)\nhive_update(id=\"{bead_id}\", status=\"blocked\")\n```\n\n### Report Issues to Other Agents\n```\nswarmmail_send(\n to=[\"OtherAgent\", \"coordinator\"],\n subject=\"Issue in {bead_id}\",\n body=\"<describe problem, don't fix their code>\",\n thread_id=\"{epic_id}\"\n)\n```\n\n### Manual Release (if needed)\n```\nswarmmail_release() # Manually release reservations\n```\n\n**Note:** `swarm_complete` automatically releases reservations. Only use manual release if aborting work.\n\n## [OTHER TOOLS]\n### Hive - You Have Autonomy to File Issues\nYou can create new cells against this epic when you discover:\n- **Bugs**: Found a bug while working? File it.\n- **Tech debt**: Spotted something that needs cleanup? File it.\n- **Follow-up work**: Task needs more work than scoped? File a follow-up.\n- **Dependencies**: Need something from another agent? File and link it.\n\n```\nhive_create(\n title=\"<descriptive title>\",\n type=\"bug\", # or \"task\", \"chore\"\n priority=2,\n parent_id=\"{epic_id}\", # Links to this epic\n description=\"Found while working on {bead_id}: <details>\"\n)\n```\n\n**Don't silently ignore issues.** File them so they get tracked and addressed.\n\nOther cell operations:\n- hive_update(id, status) - Mark blocked if stuck\n- hive_query(status=\"open\") - See what else needs work\n\n### Skills\n- skills_list() - Discover available skills\n- skills_use(name) - Activate skill for specialized guidance\n- skills_create(name) - Create new skill (if you found a reusable pattern)\n\n## [CRITICAL REQUIREMENTS]\n\n**NON-NEGOTIABLE:**\n1. Step 1 (swarmmail_init) MUST be first - do it before anything else\n2. \uD83E\uDDE0 Step 2 (semantic-memory_find) MUST happen BEFORE starting work - query first, code second\n3. Step 4 (swarmmail_reserve) - YOU reserve files, not coordinator\n4. Step 6 (swarm_progress) - Report at milestones, don't work silently\n5. \uD83D\uDCBE Step 8 (semantic-memory_store) - If you learned something hard, STORE IT\n6. Step 9 (swarm_complete) - Use this to close, NOT hive_close\n\n**If you skip these steps:**\n- Your work won't be tracked (swarm_complete will fail)\n- \uD83D\uDD04 You'll waste time repeating already-solved problems (no semantic memory query)\n- Edit conflicts with other agents (no file reservation)\n- Lost work if you crash (no checkpoints)\n- \uD83D\uDD04 Future agents repeat YOUR mistakes (no learnings stored)\n\n**Memory is the swarm's collective intelligence. Query it. Feed it.**\n\nBegin now.";
41
41
  /**
42
- * Researcher prompt template for documentation discovery
42
+ * Coordinator Agent Prompt Template
43
+ *
44
+ * Used by the /swarm command to instruct coordinators on their role.
45
+ * Coordinators NEVER execute work directly - they clarify, decompose, spawn workers, and review.
46
+ *
47
+ * Key sections:
48
+ * - Role boundaries (what coordinators NEVER do)
49
+ * - Phase 1.5: Research Phase (spawn researchers, DON'T fetch docs directly)
50
+ * - Forbidden tools (repo-crawl, webfetch, context7, pdf-brain_search)
51
+ * - MANDATORY review loop after each worker completes
52
+ *
53
+ * Placeholders:
54
+ * - {task} - The task description from user
55
+ * - {project_path} - Absolute path to project root
56
+ */
57
+ export declare const COORDINATOR_PROMPT = "You are a swarm coordinator. Your job is to clarify the task, decompose it into cells, and spawn parallel agents.\n\n## Task\n\n{task}\n\n## CRITICAL: Coordinator Role Boundaries\n\n**\u26A0\uFE0F COORDINATORS NEVER EXECUTE WORK DIRECTLY**\n\nYour role is **ONLY** to:\n1. **Clarify** - Ask questions to understand scope\n2. **Decompose** - Break into subtasks with clear boundaries \n3. **Spawn** - Create worker agents for ALL subtasks\n4. **Monitor** - Check progress, unblock, mediate conflicts\n5. **Verify** - Confirm completion, run final checks\n\n**YOU DO NOT:**\n- Read implementation files (only metadata/structure for planning)\n- Edit code directly\n- Run tests yourself (workers run tests)\n- Implement features\n- Fix bugs inline\n- Make \"quick fixes\" yourself\n\n**ALWAYS spawn workers, even for sequential tasks.** Sequential just means spawn them in order and wait for each to complete before spawning the next.\n\n### Why This Matters\n\n| Coordinator Work | Worker Work | Consequence of Mixing |\n|-----------------|-------------|----------------------|\n| Sonnet context ($$$) | Disposable context | Expensive context waste |\n| Long-lived state | Task-scoped state | Context exhaustion |\n| Orchestration concerns | Implementation concerns | Mixed concerns |\n| No checkpoints | Checkpoints enabled | No recovery |\n| No learning signals | Outcomes tracked | No improvement |\n\n## CRITICAL: NEVER Fetch Documentation Directly\n\n**\u26A0\uFE0F COORDINATORS DO NOT CALL RESEARCH TOOLS DIRECTLY**\n\nThe following tools are **FORBIDDEN** for coordinators to call:\n\n- `repo-crawl_file`, `repo-crawl_readme`, `repo-crawl_search`, `repo-crawl_structure`, `repo-crawl_tree`\n- `repo-autopsy_*` (all variants)\n- `webfetch`, `fetch_fetch`\n- `context7_resolve-library-id`, `context7_get-library-docs`\n- `pdf-brain_search`, `pdf-brain_read`\n\n**WHY?** These tools dump massive context that exhausts your expensive Sonnet context. Your job is orchestration, not research.\n\n**INSTEAD:** Use `swarm_spawn_researcher` (see Phase 1.5 below) to spawn a researcher worker who:\n- Fetches documentation in disposable context\n- Stores full details in semantic-memory\n- Returns a condensed summary for shared_context\n\n## Workflow\n\n### Phase 0: Socratic Planning (INTERACTIVE - unless --fast)\n\n**Before decomposing, clarify the task with the user.**\n\nCheck for flags in the task:\n- `--fast` \u2192 Skip questions, use reasonable defaults\n- `--auto` \u2192 Zero interaction, heuristic decisions\n- `--confirm-only` \u2192 Show plan, get yes/no only\n\n**Default (no flags): Full Socratic Mode**\n\n1. **Analyze task for ambiguity:**\n - Scope unclear? (what's included/excluded)\n - Strategy unclear? (file-based vs feature-based)\n - Dependencies unclear? (what needs to exist first)\n - Success criteria unclear? (how do we know it's done)\n\n2. **If clarification needed, ask ONE question at a time:**\n ```\n The task \"<task>\" needs clarification before I can decompose it.\n\n **Question:** <specific question>\n\n Options:\n a) <option 1> - <tradeoff>\n b) <option 2> - <tradeoff>\n c) <option 3> - <tradeoff>\n\n I'd recommend (b) because <reason>. Which approach?\n ```\n\n3. **Wait for user response before proceeding**\n\n4. **Iterate if needed** (max 2-3 questions)\n\n**Rules:**\n- ONE question at a time - don't overwhelm\n- Offer concrete options - not open-ended\n- Lead with recommendation - save cognitive load\n- Wait for answer - don't assume\n\n### Phase 1: Initialize\n`swarmmail_init(project_path=\"{project_path}\", task_description=\"Swarm: {task}\")`\n\n### Phase 1.5: Research Phase (FOR COMPLEX TASKS)\n\n**\u26A0\uFE0F If the task requires understanding unfamiliar technologies, APIs, or libraries, spawn a researcher FIRST.**\n\n**DO NOT call documentation tools directly.** Instead:\n\n```\n// 1. Spawn researcher with explicit tech stack\nswarm_spawn_researcher(\n research_id=\"research-nextjs-cache-components\",\n epic_id=\"<epic-id>\",\n tech_stack=[\"Next.js 16 Cache Components\", \"React Server Components\"],\n project_path=\"{project_path}\"\n)\n\n// 2. Spawn researcher as Task subagent\nconst researchFindings = await Task(subagent_type=\"swarm/researcher\", prompt=\"<from above>\")\n\n// 3. Researcher returns condensed summary\n// Use this summary in shared_context for workers\n```\n\n**When to spawn a researcher:**\n- Task involves unfamiliar framework versions (e.g., Next.js 16 vs 14)\n- Need to compare installed vs latest library APIs\n- Working with experimental/preview features\n- Need architectural guidance from documentation\n\n**When NOT to spawn a researcher:**\n- Using well-known stable APIs (React hooks, Express middleware)\n- Task is purely refactoring existing code\n- You already have relevant findings from semantic-memory or CASS\n\n**Researcher output:**\n- Full findings stored in semantic-memory (searchable by future agents)\n- Condensed 3-5 bullet summary returned for shared_context\n\n### Phase 2: Knowledge Gathering (MANDATORY)\n\n**Before decomposing, query ALL knowledge sources:**\n\n```\nsemantic-memory_find(query=\"<task keywords>\", limit=5) # Past learnings\ncass_search(query=\"<task description>\", limit=5) # Similar past tasks \nskills_list() # Available skills\n```\n\nSynthesize findings into shared_context for workers.\n\n### Phase 3: Decompose\n```\nswarm_select_strategy(task=\"<task>\")\nswarm_plan_prompt(task=\"<task>\", context=\"<synthesized knowledge>\")\nswarm_validate_decomposition(response=\"<CellTree JSON>\")\n```\n\n### Phase 4: Create Cells\n`hive_create_epic(epic_title=\"<task>\", subtasks=[...])`\n\n### Phase 5: DO NOT Reserve Files\n\n> **\u26A0\uFE0F Coordinator NEVER reserves files.** Workers reserve their own files.\n> If coordinator reserves, workers get blocked and swarm stalls.\n\n### Phase 6: Spawn Workers for ALL Subtasks (MANDATORY)\n\n> **\u26A0\uFE0F ALWAYS spawn workers, even for sequential tasks.**\n> - Parallel tasks: Spawn ALL in a single message\n> - Sequential tasks: Spawn one, wait for completion, spawn next\n\n**For parallel work:**\n```\n// Single message with multiple Task calls\nswarm_spawn_subtask(bead_id_1, epic_id, title_1, files_1, shared_context, project_path=\"{project_path}\")\nTask(subagent_type=\"swarm/worker\", prompt=\"<from above>\")\nswarm_spawn_subtask(bead_id_2, epic_id, title_2, files_2, shared_context, project_path=\"{project_path}\")\nTask(subagent_type=\"swarm/worker\", prompt=\"<from above>\")\n```\n\n**For sequential work:**\n```\n// Spawn worker 1, wait for completion\nswarm_spawn_subtask(bead_id_1, ...)\nconst result1 = await Task(subagent_type=\"swarm/worker\", prompt=\"<from above>\")\n\n// THEN spawn worker 2 with context from worker 1\nswarm_spawn_subtask(bead_id_2, ..., shared_context=\"Worker 1 completed: \" + result1)\nconst result2 = await Task(subagent_type=\"swarm/worker\", prompt=\"<from above>\")\n```\n\n**NEVER do the work yourself.** Even if it seems faster, spawn a worker.\n\n**IMPORTANT:** Pass `project_path` to `swarm_spawn_subtask` so workers can call `swarmmail_init`.\n\n### Phase 7: MANDATORY Review Loop (NON-NEGOTIABLE)\n\n**\u26A0\uFE0F AFTER EVERY Task() RETURNS, YOU MUST:**\n\n1. **CHECK INBOX** - Worker may have sent messages\n `swarmmail_inbox()`\n `swarmmail_read_message(message_id=N)`\n\n2. **REVIEW WORK** - Generate review with diff\n `swarm_review(project_key, epic_id, task_id, files_touched)`\n\n3. **EVALUATE** - Does it meet epic goals?\n - Fulfills subtask requirements?\n - Serves overall epic goal?\n - Enables downstream tasks?\n - Type safety, no obvious bugs?\n\n4. **SEND FEEDBACK** - Approve or request changes\n `swarm_review_feedback(project_key, task_id, worker_id, status, issues)`\n \n **If approved:**\n - Close cell, spawn next worker\n \n **If needs_changes:**\n - `swarm_review_feedback` returns `retry_context` (NOT sends message - worker is dead)\n - Generate retry prompt: `swarm_spawn_retry(retry_context)`\n - Spawn NEW worker with Task() using retry prompt\n - Max 3 attempts before marking task blocked\n \n **If 3 failures:**\n - Mark task blocked, escalate to human\n\n5. **ONLY THEN** - Spawn next worker or complete\n\n**DO NOT skip this. DO NOT batch reviews. Review EACH worker IMMEDIATELY after return.**\n\n**Intervene if:**\n- Worker blocked >5min \u2192 unblock or reassign\n- File conflicts \u2192 mediate between workers\n- Scope creep \u2192 approve or reject expansion\n- Review fails 3x \u2192 mark task blocked, escalate to human\n\n### Phase 8: Complete\n```\n# After all workers complete and reviews pass:\nhive_sync() # Sync all cells to git\n# Coordinator does NOT call swarm_complete - workers do that\n```\n\n## Strategy Reference\n\n| Strategy | Best For | Keywords |\n| -------------- | ------------------------ | -------------------------------------- |\n| file-based | Refactoring, migrations | refactor, migrate, rename, update all |\n| feature-based | New features | add, implement, build, create, feature |\n| risk-based | Bug fixes, security | fix, bug, security, critical, urgent |\n| research-based | Investigation, discovery | research, investigate, explore, learn |\n\n## Flag Reference\n\n| Flag | Effect |\n|------|--------|\n| `--fast` | Skip Socratic questions, use defaults |\n| `--auto` | Zero interaction, heuristic decisions |\n| `--confirm-only` | Show plan, get yes/no only |\n\nBegin with Phase 0 (Socratic Planning) unless `--fast` or `--auto` flag is present.\n";
58
+ /**
59
+ * Researcher Agent Prompt Template
43
60
  *
44
61
  * Spawned BEFORE decomposition to gather technology documentation.
45
62
  * Researchers receive an EXPLICIT list of technologies to research from the coordinator.
@@ -70,6 +87,13 @@ export declare function formatResearcherPrompt(params: {
70
87
  project_path: string;
71
88
  check_upgrades: boolean;
72
89
  }): string;
90
+ /**
91
+ * Format the coordinator prompt with task and project path substitution
92
+ */
93
+ export declare function formatCoordinatorPrompt(params: {
94
+ task: string;
95
+ projectPath: string;
96
+ }): string;
73
97
  /**
74
98
  * Format the V2 subtask prompt for a specific agent
75
99
  */