opencode-swarm-plugin 0.26.1 → 0.27.0

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 (77) hide show
  1. package/.turbo/turbo-build.log +4 -4
  2. package/CHANGELOG.md +23 -0
  3. package/README.md +43 -46
  4. package/bin/swarm.ts +8 -8
  5. package/dist/compaction-hook.d.ts +57 -0
  6. package/dist/compaction-hook.d.ts.map +1 -0
  7. package/dist/hive.d.ts +741 -0
  8. package/dist/hive.d.ts.map +1 -0
  9. package/dist/index.d.ts +139 -23
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +1353 -350
  12. package/dist/learning.d.ts +9 -9
  13. package/dist/plugin.js +1176 -350
  14. package/dist/schemas/cell-events.d.ts +1352 -0
  15. package/dist/schemas/{bead-events.d.ts.map → cell-events.d.ts.map} +1 -1
  16. package/dist/schemas/{bead.d.ts → cell.d.ts} +173 -29
  17. package/dist/schemas/cell.d.ts.map +1 -0
  18. package/dist/schemas/index.d.ts +11 -7
  19. package/dist/schemas/index.d.ts.map +1 -1
  20. package/dist/structured.d.ts +17 -7
  21. package/dist/structured.d.ts.map +1 -1
  22. package/dist/swarm-decompose.d.ts +5 -5
  23. package/dist/swarm-orchestrate.d.ts +16 -2
  24. package/dist/swarm-orchestrate.d.ts.map +1 -1
  25. package/dist/swarm-prompts.d.ts +9 -9
  26. package/dist/swarm-prompts.d.ts.map +1 -1
  27. package/dist/swarm-review.d.ts +210 -0
  28. package/dist/swarm-review.d.ts.map +1 -0
  29. package/dist/swarm-worktree.d.ts +185 -0
  30. package/dist/swarm-worktree.d.ts.map +1 -0
  31. package/dist/swarm.d.ts +7 -0
  32. package/dist/swarm.d.ts.map +1 -1
  33. package/dist/tool-availability.d.ts +3 -2
  34. package/dist/tool-availability.d.ts.map +1 -1
  35. package/docs/analysis-socratic-planner-pattern.md +1 -1
  36. package/docs/planning/ADR-007-swarm-enhancements-worktree-review.md +168 -0
  37. package/docs/testing/context-recovery-test.md +2 -2
  38. package/evals/README.md +2 -2
  39. package/evals/scorers/index.ts +7 -7
  40. package/examples/commands/swarm.md +21 -23
  41. package/examples/plugin-wrapper-template.ts +310 -44
  42. package/examples/skills/{beads-workflow → hive-workflow}/SKILL.md +40 -40
  43. package/examples/skills/swarm-coordination/SKILL.md +1 -1
  44. package/global-skills/swarm-coordination/SKILL.md +14 -14
  45. package/global-skills/swarm-coordination/references/coordinator-patterns.md +3 -3
  46. package/package.json +2 -2
  47. package/src/compaction-hook.ts +161 -0
  48. package/src/{beads.integration.test.ts → hive.integration.test.ts} +92 -80
  49. package/src/{beads.ts → hive.ts} +378 -219
  50. package/src/index.ts +57 -20
  51. package/src/learning.ts +9 -9
  52. package/src/output-guardrails.test.ts +4 -4
  53. package/src/output-guardrails.ts +9 -9
  54. package/src/planning-guardrails.test.ts +1 -1
  55. package/src/planning-guardrails.ts +1 -1
  56. package/src/schemas/{bead-events.test.ts → cell-events.test.ts} +83 -77
  57. package/src/schemas/cell-events.ts +807 -0
  58. package/src/schemas/{bead.ts → cell.ts} +95 -41
  59. package/src/schemas/evaluation.ts +1 -1
  60. package/src/schemas/index.ts +90 -18
  61. package/src/schemas/swarm-context.ts +2 -2
  62. package/src/structured.test.ts +15 -15
  63. package/src/structured.ts +18 -11
  64. package/src/swarm-decompose.ts +23 -23
  65. package/src/swarm-orchestrate.ts +135 -21
  66. package/src/swarm-prompts.ts +43 -43
  67. package/src/swarm-review.test.ts +702 -0
  68. package/src/swarm-review.ts +696 -0
  69. package/src/swarm-worktree.test.ts +501 -0
  70. package/src/swarm-worktree.ts +575 -0
  71. package/src/swarm.integration.test.ts +12 -12
  72. package/src/tool-availability.ts +36 -3
  73. package/dist/beads.d.ts +0 -386
  74. package/dist/beads.d.ts.map +0 -1
  75. package/dist/schemas/bead-events.d.ts +0 -698
  76. package/dist/schemas/bead.d.ts.map +0 -1
  77. package/src/schemas/bead-events.ts +0 -583
@@ -0,0 +1,702 @@
1
+ /**
2
+ * Swarm Structured Review Tests
3
+ *
4
+ * Tests for the coordinator-driven review of worker output.
5
+ * The review is epic-aware - it checks if work serves the overall goal
6
+ * and enables downstream tasks.
7
+ *
8
+ * Credit: Review patterns inspired by https://github.com/nexxeln/opencode-config
9
+ */
10
+ import { describe, it, expect, beforeEach, vi } from "vitest";
11
+ import {
12
+ generateReviewPrompt,
13
+ ReviewResultSchema,
14
+ markReviewApproved,
15
+ isReviewApproved,
16
+ getReviewStatus,
17
+ clearReviewStatus,
18
+ swarm_review,
19
+ swarm_review_feedback,
20
+ type ReviewPromptContext,
21
+ type ReviewResult,
22
+ type ReviewIssue,
23
+ } from "./swarm-review";
24
+
25
+ // Mock swarm-mail
26
+ vi.mock("swarm-mail", () => ({
27
+ sendSwarmMessage: vi.fn().mockResolvedValue({ success: true }),
28
+ }));
29
+
30
+ const mockContext = {
31
+ sessionID: `test-review-${Date.now()}`,
32
+ messageID: `test-message-${Date.now()}`,
33
+ agent: "test-agent",
34
+ abort: new AbortController().signal,
35
+ };
36
+
37
+ // ============================================================================
38
+ // Review Prompt Generation
39
+ // ============================================================================
40
+
41
+ describe("generateReviewPrompt", () => {
42
+ const baseContext: ReviewPromptContext = {
43
+ epic_id: "bd-test-123",
44
+ epic_title: "Add user authentication",
45
+ epic_description: "Implement OAuth2 with JWT tokens",
46
+ task_id: "bd-test-123.1",
47
+ task_title: "Create auth utilities",
48
+ task_description: "JWT sign/verify functions",
49
+ files_touched: ["src/lib/auth.ts"],
50
+ diff: "+export function signToken() {}",
51
+ };
52
+
53
+ it("includes epic goal for big-picture context", () => {
54
+ const prompt = generateReviewPrompt(baseContext);
55
+ expect(prompt).toContain("Add user authentication");
56
+ expect(prompt).toContain("OAuth2 with JWT tokens");
57
+ expect(prompt).toContain("## Epic Goal");
58
+ });
59
+
60
+ it("includes task requirements", () => {
61
+ const prompt = generateReviewPrompt(baseContext);
62
+ expect(prompt).toContain("Create auth utilities");
63
+ expect(prompt).toContain("JWT sign/verify functions");
64
+ expect(prompt).toContain("## Task Requirements");
65
+ });
66
+
67
+ it("includes dependency context (what this builds on)", () => {
68
+ const contextWithDeps: ReviewPromptContext = {
69
+ ...baseContext,
70
+ task_id: "bd-test-123.2",
71
+ task_title: "Create auth middleware",
72
+ completed_dependencies: [
73
+ {
74
+ id: "bd-test-123.1",
75
+ title: "Create auth utilities",
76
+ summary: "JWT sign/verify done",
77
+ },
78
+ ],
79
+ };
80
+ const prompt = generateReviewPrompt(contextWithDeps);
81
+ expect(prompt).toContain("This Task Builds On");
82
+ expect(prompt).toContain("Create auth utilities");
83
+ expect(prompt).toContain("JWT sign/verify done");
84
+ });
85
+
86
+ it("includes downstream context (what depends on this)", () => {
87
+ const contextWithDownstream: ReviewPromptContext = {
88
+ ...baseContext,
89
+ downstream_tasks: [
90
+ { id: "bd-test-123.2", title: "Create auth middleware" },
91
+ { id: "bd-test-123.3", title: "Add protected routes" },
92
+ ],
93
+ };
94
+ const prompt = generateReviewPrompt(contextWithDownstream);
95
+ expect(prompt).toContain("Downstream Tasks");
96
+ expect(prompt).toContain("Create auth middleware");
97
+ expect(prompt).toContain("Add protected routes");
98
+ });
99
+
100
+ it("includes the actual code diff", () => {
101
+ const diff = `+export function signToken(payload: TokenPayload): string {
102
+ + return jwt.sign(payload, SECRET, { expiresIn: '1h' });
103
+ +}`;
104
+ const contextWithDiff: ReviewPromptContext = {
105
+ ...baseContext,
106
+ diff,
107
+ };
108
+ const prompt = generateReviewPrompt(contextWithDiff);
109
+ expect(prompt).toContain("signToken");
110
+ expect(prompt).toContain("TokenPayload");
111
+ expect(prompt).toContain("```diff");
112
+ });
113
+
114
+ it("includes review criteria checklist", () => {
115
+ const prompt = generateReviewPrompt(baseContext);
116
+ expect(prompt).toContain("Fulfills Requirements");
117
+ expect(prompt).toContain("Serves Epic Goal");
118
+ expect(prompt).toContain("Enables Downstream");
119
+ expect(prompt).toContain("Type Safety");
120
+ expect(prompt).toContain("No Critical Bugs");
121
+ expect(prompt).toContain("Test Coverage");
122
+ });
123
+
124
+ it("includes files modified section", () => {
125
+ const prompt = generateReviewPrompt(baseContext);
126
+ expect(prompt).toContain("## Files Modified");
127
+ expect(prompt).toContain("`src/lib/auth.ts`");
128
+ });
129
+
130
+ it("includes response format instructions", () => {
131
+ const prompt = generateReviewPrompt(baseContext);
132
+ expect(prompt).toContain("## Response Format");
133
+ expect(prompt).toContain('"status"');
134
+ expect(prompt).toContain('"approved"');
135
+ expect(prompt).toContain('"needs_changes"');
136
+ });
137
+ });
138
+
139
+ // ============================================================================
140
+ // Review Result Schema
141
+ // ============================================================================
142
+
143
+ describe("ReviewResultSchema", () => {
144
+ it("accepts approved status with summary", () => {
145
+ const result: ReviewResult = {
146
+ status: "approved",
147
+ summary: "Clean implementation, exports are clear for downstream tasks",
148
+ };
149
+ expect(ReviewResultSchema.safeParse(result).success).toBe(true);
150
+ });
151
+
152
+ it("accepts needs_changes status with issues array", () => {
153
+ const result: ReviewResult = {
154
+ status: "needs_changes",
155
+ issues: [
156
+ {
157
+ file: "src/lib/auth.ts",
158
+ line: 42,
159
+ issue: "Missing error handling for expired tokens",
160
+ suggestion:
161
+ "Return { valid: false, error: 'expired' } instead of throwing",
162
+ },
163
+ ],
164
+ remaining_attempts: 2,
165
+ };
166
+ expect(ReviewResultSchema.safeParse(result).success).toBe(true);
167
+ });
168
+
169
+ it("requires issues array when status is needs_changes", () => {
170
+ const result = {
171
+ status: "needs_changes",
172
+ // missing issues array
173
+ };
174
+ const parsed = ReviewResultSchema.safeParse(result);
175
+ expect(parsed.success).toBe(false);
176
+ });
177
+
178
+ it("rejects needs_changes with empty issues array", () => {
179
+ const result = {
180
+ status: "needs_changes",
181
+ issues: [],
182
+ };
183
+ const parsed = ReviewResultSchema.safeParse(result);
184
+ expect(parsed.success).toBe(false);
185
+ });
186
+
187
+ it("tracks remaining review attempts", () => {
188
+ const result: ReviewResult = {
189
+ status: "needs_changes",
190
+ issues: [{ file: "x.ts", issue: "bug" }],
191
+ remaining_attempts: 1,
192
+ };
193
+ const parsed = ReviewResultSchema.safeParse(result);
194
+ expect(parsed.success).toBe(true);
195
+ if (parsed.success) {
196
+ expect(parsed.data.remaining_attempts).toBe(1);
197
+ }
198
+ });
199
+
200
+ it("accepts approved without summary", () => {
201
+ const result: ReviewResult = {
202
+ status: "approved",
203
+ };
204
+ expect(ReviewResultSchema.safeParse(result).success).toBe(true);
205
+ });
206
+
207
+ it("accepts issue without line number", () => {
208
+ const result: ReviewResult = {
209
+ status: "needs_changes",
210
+ issues: [{ file: "x.ts", issue: "general problem" }],
211
+ };
212
+ expect(ReviewResultSchema.safeParse(result).success).toBe(true);
213
+ });
214
+
215
+ it("accepts issue without suggestion", () => {
216
+ const result: ReviewResult = {
217
+ status: "needs_changes",
218
+ issues: [{ file: "x.ts", line: 10, issue: "problem here" }],
219
+ };
220
+ expect(ReviewResultSchema.safeParse(result).success).toBe(true);
221
+ });
222
+ });
223
+
224
+ // ============================================================================
225
+ // Review Status Tracking
226
+ // ============================================================================
227
+
228
+ describe("Review status tracking", () => {
229
+ beforeEach(() => {
230
+ clearReviewStatus("test-task-1");
231
+ clearReviewStatus("test-task-2");
232
+ });
233
+
234
+ it("starts with no review status", () => {
235
+ const status = getReviewStatus("test-task-1");
236
+ expect(status.reviewed).toBe(false);
237
+ expect(status.approved).toBe(false);
238
+ expect(status.attempt_count).toBe(0);
239
+ expect(status.remaining_attempts).toBe(3);
240
+ });
241
+
242
+ it("marks task as approved", () => {
243
+ markReviewApproved("test-task-1");
244
+ expect(isReviewApproved("test-task-1")).toBe(true);
245
+ const status = getReviewStatus("test-task-1");
246
+ expect(status.reviewed).toBe(true);
247
+ expect(status.approved).toBe(true);
248
+ });
249
+
250
+ it("tracks separate status per task", () => {
251
+ markReviewApproved("test-task-1");
252
+ expect(isReviewApproved("test-task-1")).toBe(true);
253
+ expect(isReviewApproved("test-task-2")).toBe(false);
254
+ });
255
+
256
+ it("clears review status", () => {
257
+ markReviewApproved("test-task-1");
258
+ expect(isReviewApproved("test-task-1")).toBe(true);
259
+ clearReviewStatus("test-task-1");
260
+ expect(isReviewApproved("test-task-1")).toBe(false);
261
+ });
262
+ });
263
+
264
+ // ============================================================================
265
+ // swarm_review tool
266
+ // ============================================================================
267
+
268
+ describe("swarm_review", () => {
269
+ it("has correct tool metadata", () => {
270
+ expect(swarm_review.description).toContain("review prompt");
271
+ expect(swarm_review.description).toContain("epic context");
272
+ });
273
+
274
+ it("returns JSON with review_prompt field", async () => {
275
+ // This test exercises the tool structure without needing real git/beads
276
+ // The tool will fail to get real data but should still return valid JSON
277
+ // Use /tmp which exists on all systems
278
+ const result = await swarm_review.execute(
279
+ {
280
+ project_key: "/tmp",
281
+ epic_id: "bd-test-123",
282
+ task_id: "bd-test-123.1",
283
+ files_touched: ["src/test.ts"],
284
+ },
285
+ mockContext
286
+ );
287
+
288
+ const parsed = JSON.parse(result);
289
+ expect(parsed).toHaveProperty("review_prompt");
290
+ expect(parsed).toHaveProperty("context");
291
+ expect(parsed.context.epic_id).toBe("bd-test-123");
292
+ expect(parsed.context.task_id).toBe("bd-test-123.1");
293
+ });
294
+
295
+ it("includes remaining attempts in context", async () => {
296
+ clearReviewStatus("bd-test-123.1");
297
+ const result = await swarm_review.execute(
298
+ {
299
+ project_key: "/tmp",
300
+ epic_id: "bd-test-123",
301
+ task_id: "bd-test-123.1",
302
+ },
303
+ mockContext
304
+ );
305
+
306
+ const parsed = JSON.parse(result);
307
+ expect(parsed.context.remaining_attempts).toBe(3);
308
+ });
309
+ });
310
+
311
+ // ============================================================================
312
+ // swarm_review_feedback tool
313
+ // ============================================================================
314
+
315
+ describe("swarm_review_feedback", () => {
316
+ beforeEach(() => {
317
+ clearReviewStatus("bd-feedback-test");
318
+ vi.clearAllMocks();
319
+ });
320
+
321
+ it("has correct tool metadata", () => {
322
+ expect(swarm_review_feedback.description).toContain("feedback");
323
+ expect(swarm_review_feedback.description).toContain("max 3");
324
+ });
325
+
326
+ it("sends approved feedback successfully", async () => {
327
+ const result = await swarm_review_feedback.execute(
328
+ {
329
+ project_key: "/tmp/test-project",
330
+ task_id: "bd-feedback-test",
331
+ worker_id: "worker-test",
332
+ status: "approved",
333
+ summary: "Looks good, clean implementation",
334
+ },
335
+ mockContext
336
+ );
337
+
338
+ const parsed = JSON.parse(result);
339
+ expect(parsed.success).toBe(true);
340
+ expect(parsed.status).toBe("approved");
341
+ });
342
+
343
+ it("sends needs_changes feedback with structured issues", async () => {
344
+ const issues: ReviewIssue[] = [
345
+ {
346
+ file: "src/auth.ts",
347
+ line: 42,
348
+ issue: "Missing null check",
349
+ suggestion: "Add if (!token) return null",
350
+ },
351
+ ];
352
+
353
+ const result = await swarm_review_feedback.execute(
354
+ {
355
+ project_key: "/tmp/test-project",
356
+ task_id: "bd-feedback-test",
357
+ worker_id: "worker-test",
358
+ status: "needs_changes",
359
+ issues: JSON.stringify(issues),
360
+ },
361
+ mockContext
362
+ );
363
+
364
+ const parsed = JSON.parse(result);
365
+ expect(parsed.success).toBe(true);
366
+ expect(parsed.status).toBe("needs_changes");
367
+ expect(parsed.remaining_attempts).toBe(2);
368
+ });
369
+
370
+ it("requires issues for needs_changes status", async () => {
371
+ const result = await swarm_review_feedback.execute(
372
+ {
373
+ project_key: "/tmp/test-project",
374
+ task_id: "bd-feedback-test",
375
+ worker_id: "worker-test",
376
+ status: "needs_changes",
377
+ // no issues provided
378
+ },
379
+ mockContext
380
+ );
381
+
382
+ const parsed = JSON.parse(result);
383
+ expect(parsed.success).toBe(false);
384
+ expect(parsed.error).toContain("requires at least one issue");
385
+ });
386
+
387
+ it("tracks review attempts (max 3)", async () => {
388
+ const issues = JSON.stringify([{ file: "x.ts", issue: "bug" }]);
389
+
390
+ // First attempt
391
+ let result = await swarm_review_feedback.execute(
392
+ {
393
+ project_key: "/tmp/test-project",
394
+ task_id: "bd-feedback-test",
395
+ worker_id: "worker-test",
396
+ status: "needs_changes",
397
+ issues,
398
+ },
399
+ mockContext
400
+ );
401
+ let parsed = JSON.parse(result);
402
+ expect(parsed.attempt).toBe(1);
403
+ expect(parsed.remaining_attempts).toBe(2);
404
+
405
+ // Second attempt
406
+ result = await swarm_review_feedback.execute(
407
+ {
408
+ project_key: "/tmp/test-project",
409
+ task_id: "bd-feedback-test",
410
+ worker_id: "worker-test",
411
+ status: "needs_changes",
412
+ issues,
413
+ },
414
+ mockContext
415
+ );
416
+ parsed = JSON.parse(result);
417
+ expect(parsed.attempt).toBe(2);
418
+ expect(parsed.remaining_attempts).toBe(1);
419
+ });
420
+
421
+ it("fails task after 3 rejected reviews", async () => {
422
+ const issues = JSON.stringify([{ file: "x.ts", issue: "still broken" }]);
423
+
424
+ // Exhaust all attempts
425
+ for (let i = 0; i < 3; i++) {
426
+ await swarm_review_feedback.execute(
427
+ {
428
+ project_key: "/tmp/test-project",
429
+ task_id: "bd-feedback-test",
430
+ worker_id: "worker-test",
431
+ status: "needs_changes",
432
+ issues,
433
+ },
434
+ mockContext
435
+ );
436
+ }
437
+
438
+ // Check final state
439
+ const status = getReviewStatus("bd-feedback-test");
440
+ expect(status.remaining_attempts).toBe(0);
441
+ });
442
+
443
+ it("clears attempts on approval", async () => {
444
+ const issues = JSON.stringify([{ file: "x.ts", issue: "bug" }]);
445
+
446
+ // Add some attempts
447
+ await swarm_review_feedback.execute(
448
+ {
449
+ project_key: "/tmp/test-project",
450
+ task_id: "bd-feedback-test",
451
+ worker_id: "worker-test",
452
+ status: "needs_changes",
453
+ issues,
454
+ },
455
+ mockContext
456
+ );
457
+
458
+ // Now approve
459
+ await swarm_review_feedback.execute(
460
+ {
461
+ project_key: "/tmp/test-project",
462
+ task_id: "bd-feedback-test",
463
+ worker_id: "worker-test",
464
+ status: "approved",
465
+ summary: "Fixed!",
466
+ },
467
+ mockContext
468
+ );
469
+
470
+ // Attempts should be cleared
471
+ const status = getReviewStatus("bd-feedback-test");
472
+ expect(status.attempt_count).toBe(0);
473
+ expect(status.remaining_attempts).toBe(3);
474
+ });
475
+
476
+ it("handles invalid issues JSON", async () => {
477
+ const result = await swarm_review_feedback.execute(
478
+ {
479
+ project_key: "/tmp/test-project",
480
+ task_id: "bd-feedback-test",
481
+ worker_id: "worker-test",
482
+ status: "needs_changes",
483
+ issues: "not valid json",
484
+ },
485
+ mockContext
486
+ );
487
+
488
+ const parsed = JSON.parse(result);
489
+ expect(parsed.success).toBe(false);
490
+ expect(parsed.error).toContain("parse");
491
+ });
492
+
493
+ it("extracts epic ID from task ID for thread", async () => {
494
+ // Task ID format: bd-epic.subtask
495
+ const result = await swarm_review_feedback.execute(
496
+ {
497
+ project_key: "/tmp/test-project",
498
+ task_id: "bd-epic-123.4",
499
+ worker_id: "worker-test",
500
+ status: "approved",
501
+ },
502
+ mockContext
503
+ );
504
+
505
+ const parsed = JSON.parse(result);
506
+ expect(parsed.success).toBe(true);
507
+ // The sendSwarmMessage mock was called with threadId = "bd-epic-123"
508
+ });
509
+ });
510
+
511
+ // ============================================================================
512
+ // Integration: swarm_complete with review gate
513
+ // ============================================================================
514
+
515
+ describe("swarm_complete with review gate", () => {
516
+ // These tests verify the review gate behavior that was added to swarm_complete
517
+ // The actual swarm_complete tests are in swarm.integration.test.ts
518
+ // Here we test the review status functions that gate completion
519
+
520
+ beforeEach(() => {
521
+ clearReviewStatus("bd-gate-test");
522
+ });
523
+
524
+ it("isReviewApproved returns false for unreviewed task", () => {
525
+ expect(isReviewApproved("bd-gate-test")).toBe(false);
526
+ });
527
+
528
+ it("isReviewApproved returns true after markReviewApproved", () => {
529
+ markReviewApproved("bd-gate-test");
530
+ expect(isReviewApproved("bd-gate-test")).toBe(true);
531
+ });
532
+
533
+ it("getReviewStatus provides complete status info", () => {
534
+ const status = getReviewStatus("bd-gate-test");
535
+ expect(status).toEqual({
536
+ reviewed: false,
537
+ approved: false,
538
+ attempt_count: 0,
539
+ remaining_attempts: 3,
540
+ });
541
+ });
542
+
543
+ it("approval clears attempt count", async () => {
544
+ // Simulate some failed attempts
545
+ const issues = JSON.stringify([{ file: "x.ts", issue: "bug" }]);
546
+ await swarm_review_feedback.execute(
547
+ {
548
+ project_key: "/tmp/test",
549
+ task_id: "bd-gate-test",
550
+ worker_id: "worker",
551
+ status: "needs_changes",
552
+ issues,
553
+ },
554
+ mockContext
555
+ );
556
+
557
+ let status = getReviewStatus("bd-gate-test");
558
+ expect(status.attempt_count).toBe(1);
559
+
560
+ // Approve
561
+ await swarm_review_feedback.execute(
562
+ {
563
+ project_key: "/tmp/test",
564
+ task_id: "bd-gate-test",
565
+ worker_id: "worker",
566
+ status: "approved",
567
+ },
568
+ mockContext
569
+ );
570
+
571
+ status = getReviewStatus("bd-gate-test");
572
+ expect(status.attempt_count).toBe(0);
573
+ expect(status.approved).toBe(true);
574
+ });
575
+ });
576
+
577
+ // ============================================================================
578
+ // Worker prompt updates for review flow
579
+ // ============================================================================
580
+
581
+ describe("worker prompt with review instructions", () => {
582
+ // These tests verify that the review prompt includes proper instructions
583
+ // The actual worker prompt generation is in swarm-prompts.ts
584
+
585
+ it("review prompt includes response format for workers", () => {
586
+ const prompt = generateReviewPrompt({
587
+ epic_id: "bd-test",
588
+ epic_title: "Test Epic",
589
+ task_id: "bd-test.1",
590
+ task_title: "Test Task",
591
+ files_touched: [],
592
+ diff: "",
593
+ });
594
+
595
+ // Workers need to know how to respond
596
+ expect(prompt).toContain("Response Format");
597
+ expect(prompt).toContain("approved");
598
+ expect(prompt).toContain("needs_changes");
599
+ });
600
+
601
+ it("review prompt explains issue structure", () => {
602
+ const prompt = generateReviewPrompt({
603
+ epic_id: "bd-test",
604
+ epic_title: "Test Epic",
605
+ task_id: "bd-test.1",
606
+ task_title: "Test Task",
607
+ files_touched: [],
608
+ diff: "",
609
+ });
610
+
611
+ expect(prompt).toContain("file");
612
+ expect(prompt).toContain("line");
613
+ expect(prompt).toContain("issue");
614
+ expect(prompt).toContain("suggestion");
615
+ });
616
+ });
617
+
618
+ // ============================================================================
619
+ // TDD ENFORCEMENT IN SWARM
620
+ // ============================================================================
621
+
622
+ describe("TDD enforcement in review criteria", () => {
623
+ it("review criteria includes test coverage check", () => {
624
+ const prompt = generateReviewPrompt({
625
+ epic_id: "bd-test",
626
+ epic_title: "Test Epic",
627
+ task_id: "bd-test.1",
628
+ task_title: "Test Task",
629
+ files_touched: ["src/foo.ts"],
630
+ diff: "+function foo() {}",
631
+ });
632
+
633
+ expect(prompt).toContain("Test Coverage");
634
+ });
635
+ });
636
+
637
+ // ============================================================================
638
+ // Edge Cases
639
+ // ============================================================================
640
+
641
+ describe("edge cases", () => {
642
+ it("handles empty files_touched", () => {
643
+ const prompt = generateReviewPrompt({
644
+ epic_id: "bd-test",
645
+ epic_title: "Test Epic",
646
+ task_id: "bd-test.1",
647
+ task_title: "Test Task",
648
+ files_touched: [],
649
+ diff: "",
650
+ });
651
+
652
+ expect(prompt).toContain("## Files Modified");
653
+ // Should not crash, just have empty list
654
+ });
655
+
656
+ it("handles missing optional fields", () => {
657
+ const prompt = generateReviewPrompt({
658
+ epic_id: "bd-test",
659
+ epic_title: "Test Epic",
660
+ task_id: "bd-test.1",
661
+ task_title: "Test Task",
662
+ files_touched: [],
663
+ diff: "",
664
+ // No epic_description, task_description, dependencies, downstream
665
+ });
666
+
667
+ expect(prompt).toContain("Test Epic");
668
+ expect(prompt).toContain("Test Task");
669
+ // Should not include dependency sections
670
+ expect(prompt).not.toContain("This Task Builds On");
671
+ expect(prompt).not.toContain("Downstream Tasks");
672
+ });
673
+
674
+ it("handles special characters in diff", () => {
675
+ const prompt = generateReviewPrompt({
676
+ epic_id: "bd-test",
677
+ epic_title: "Test Epic",
678
+ task_id: "bd-test.1",
679
+ task_title: "Test Task",
680
+ files_touched: ["src/test.ts"],
681
+ diff: '+const regex = /[a-z]+/g;\n+const template = `Hello ${name}`;',
682
+ });
683
+
684
+ expect(prompt).toContain("regex");
685
+ expect(prompt).toContain("template");
686
+ });
687
+
688
+ it("handles very long diffs", () => {
689
+ const longDiff = "+line\n".repeat(1000);
690
+ const prompt = generateReviewPrompt({
691
+ epic_id: "bd-test",
692
+ epic_title: "Test Epic",
693
+ task_id: "bd-test.1",
694
+ task_title: "Test Task",
695
+ files_touched: ["src/big.ts"],
696
+ diff: longDiff,
697
+ });
698
+
699
+ // Should include the diff without truncation (truncation is caller's responsibility)
700
+ expect(prompt).toContain(longDiff);
701
+ });
702
+ });