opencode-swarm-plugin 0.38.0 → 0.40.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 (91) hide show
  1. package/.env +2 -0
  2. package/.hive/eval-results.json +26 -0
  3. package/.hive/issues.jsonl +27 -0
  4. package/.hive/memories.jsonl +23 -1
  5. package/.opencode/eval-history.jsonl +12 -0
  6. package/CHANGELOG.md +182 -0
  7. package/README.md +29 -12
  8. package/bin/swarm.test.ts +881 -0
  9. package/bin/swarm.ts +686 -0
  10. package/dist/compaction-hook.d.ts +8 -1
  11. package/dist/compaction-hook.d.ts.map +1 -1
  12. package/dist/compaction-observability.d.ts +173 -0
  13. package/dist/compaction-observability.d.ts.map +1 -0
  14. package/dist/compaction-prompt-scoring.d.ts +124 -0
  15. package/dist/compaction-prompt-scoring.d.ts.map +1 -0
  16. package/dist/eval-capture.d.ts +174 -1
  17. package/dist/eval-capture.d.ts.map +1 -1
  18. package/dist/eval-gates.d.ts +84 -0
  19. package/dist/eval-gates.d.ts.map +1 -0
  20. package/dist/eval-history.d.ts +117 -0
  21. package/dist/eval-history.d.ts.map +1 -0
  22. package/dist/eval-learning.d.ts +216 -0
  23. package/dist/eval-learning.d.ts.map +1 -0
  24. package/dist/hive.d.ts.map +1 -1
  25. package/dist/index.d.ts +80 -1
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +16098 -651
  28. package/dist/plugin.js +16012 -756
  29. package/dist/post-compaction-tracker.d.ts +133 -0
  30. package/dist/post-compaction-tracker.d.ts.map +1 -0
  31. package/dist/schemas/task.d.ts +3 -3
  32. package/dist/swarm-orchestrate.d.ts +23 -0
  33. package/dist/swarm-orchestrate.d.ts.map +1 -1
  34. package/dist/swarm-prompts.d.ts +25 -1
  35. package/dist/swarm-prompts.d.ts.map +1 -1
  36. package/dist/swarm.d.ts +4 -0
  37. package/dist/swarm.d.ts.map +1 -1
  38. package/evals/README.md +702 -105
  39. package/evals/compaction-prompt.eval.ts +149 -0
  40. package/evals/coordinator-behavior.eval.ts +8 -8
  41. package/evals/fixtures/compaction-prompt-cases.ts +305 -0
  42. package/evals/lib/compaction-loader.test.ts +248 -0
  43. package/evals/lib/compaction-loader.ts +320 -0
  44. package/evals/lib/data-loader.test.ts +345 -0
  45. package/evals/lib/data-loader.ts +107 -6
  46. package/evals/scorers/compaction-prompt-scorers.ts +145 -0
  47. package/evals/scorers/compaction-scorers.ts +13 -13
  48. package/evals/scorers/coordinator-discipline.evalite-test.ts +166 -2
  49. package/evals/scorers/coordinator-discipline.ts +348 -15
  50. package/evals/scorers/index.test.ts +146 -0
  51. package/evals/scorers/index.ts +104 -0
  52. package/evals/swarm-decomposition.eval.ts +9 -2
  53. package/examples/commands/swarm.md +291 -21
  54. package/examples/plugin-wrapper-template.ts +117 -0
  55. package/package.json +7 -5
  56. package/scripts/migrate-unknown-sessions.ts +349 -0
  57. package/src/compaction-capture.integration.test.ts +257 -0
  58. package/src/compaction-hook.test.ts +42 -0
  59. package/src/compaction-hook.ts +315 -86
  60. package/src/compaction-observability.integration.test.ts +139 -0
  61. package/src/compaction-observability.test.ts +187 -0
  62. package/src/compaction-observability.ts +324 -0
  63. package/src/compaction-prompt-scorers.test.ts +299 -0
  64. package/src/compaction-prompt-scoring.ts +298 -0
  65. package/src/eval-capture.test.ts +626 -1
  66. package/src/eval-capture.ts +286 -2
  67. package/src/eval-gates.test.ts +306 -0
  68. package/src/eval-gates.ts +218 -0
  69. package/src/eval-history.test.ts +508 -0
  70. package/src/eval-history.ts +214 -0
  71. package/src/eval-learning.test.ts +378 -0
  72. package/src/eval-learning.ts +360 -0
  73. package/src/eval-runner.test.ts +96 -0
  74. package/src/eval-runner.ts +356 -0
  75. package/src/hive.ts +34 -0
  76. package/src/index.ts +115 -2
  77. package/src/memory.test.ts +110 -0
  78. package/src/memory.ts +34 -0
  79. package/src/post-compaction-tracker.test.ts +251 -0
  80. package/src/post-compaction-tracker.ts +237 -0
  81. package/src/swarm-decompose.ts +2 -2
  82. package/src/swarm-orchestrate.ts +2 -2
  83. package/src/swarm-prompts.ts +2 -2
  84. package/src/swarm-review.ts +3 -3
  85. package/dist/beads.d.ts +0 -386
  86. package/dist/beads.d.ts.map +0 -1
  87. package/dist/schemas/bead-events.d.ts +0 -698
  88. package/dist/schemas/bead-events.d.ts.map +0 -1
  89. package/dist/schemas/bead.d.ts +0 -255
  90. package/dist/schemas/bead.d.ts.map +0 -1
  91. /package/evals/{evalite.config.ts → evalite.config.ts.bak} +0 -0
@@ -11,8 +11,9 @@ import {
11
11
  type CoordinatorSession,
12
12
  CoordinatorSessionSchema,
13
13
  captureCoordinatorEvent,
14
+ captureCompactionEvent,
14
15
  saveSession,
15
- } from "./eval-capture.js";
16
+ } from "./eval-capture.ts";
16
17
 
17
18
  describe("CoordinatorEvent schemas", () => {
18
19
  describe("DECISION events", () => {
@@ -81,6 +82,110 @@ describe("CoordinatorEvent schemas", () => {
81
82
 
82
83
  expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
83
84
  });
85
+
86
+ test("validates researcher_spawned event", () => {
87
+ const event: CoordinatorEvent = {
88
+ session_id: "test-session",
89
+ epic_id: "bd-123",
90
+ timestamp: new Date().toISOString(),
91
+ event_type: "DECISION",
92
+ decision_type: "researcher_spawned",
93
+ payload: {
94
+ researcher_id: "BlueLake",
95
+ research_topic: "Next.js Cache Components",
96
+ tools_used: ["pdf-brain", "context7"],
97
+ },
98
+ };
99
+
100
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
101
+ });
102
+
103
+ test("validates skill_loaded event", () => {
104
+ const event: CoordinatorEvent = {
105
+ session_id: "test-session",
106
+ epic_id: "bd-123",
107
+ timestamp: new Date().toISOString(),
108
+ event_type: "DECISION",
109
+ decision_type: "skill_loaded",
110
+ payload: {
111
+ skill_name: "testing-patterns",
112
+ context: "Adding tests to legacy code",
113
+ },
114
+ };
115
+
116
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
117
+ });
118
+
119
+ test("validates inbox_checked event", () => {
120
+ const event: CoordinatorEvent = {
121
+ session_id: "test-session",
122
+ epic_id: "bd-123",
123
+ timestamp: new Date().toISOString(),
124
+ event_type: "DECISION",
125
+ decision_type: "inbox_checked",
126
+ payload: {
127
+ message_count: 3,
128
+ urgent_count: 1,
129
+ },
130
+ };
131
+
132
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
133
+ });
134
+
135
+ test("validates blocker_resolved event", () => {
136
+ const event: CoordinatorEvent = {
137
+ session_id: "test-session",
138
+ epic_id: "bd-123",
139
+ timestamp: new Date().toISOString(),
140
+ event_type: "DECISION",
141
+ decision_type: "blocker_resolved",
142
+ payload: {
143
+ worker_id: "GreenStorm",
144
+ subtask_id: "bd-123.2",
145
+ blocker_type: "dependency",
146
+ resolution: "Unblocked via coordinator action",
147
+ },
148
+ };
149
+
150
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
151
+ });
152
+
153
+ test("validates scope_change_approved event", () => {
154
+ const event: CoordinatorEvent = {
155
+ session_id: "test-session",
156
+ epic_id: "bd-123",
157
+ timestamp: new Date().toISOString(),
158
+ event_type: "DECISION",
159
+ decision_type: "scope_change_approved",
160
+ payload: {
161
+ worker_id: "BlueLake",
162
+ subtask_id: "bd-123.1",
163
+ original_scope: "Add auth service",
164
+ new_scope: "Add auth service + email validation",
165
+ estimated_time_add: 900000, // 15 min in ms
166
+ },
167
+ };
168
+
169
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
170
+ });
171
+
172
+ test("validates scope_change_rejected event", () => {
173
+ const event: CoordinatorEvent = {
174
+ session_id: "test-session",
175
+ epic_id: "bd-123",
176
+ timestamp: new Date().toISOString(),
177
+ event_type: "DECISION",
178
+ decision_type: "scope_change_rejected",
179
+ payload: {
180
+ worker_id: "BlueLake",
181
+ subtask_id: "bd-123.1",
182
+ requested_scope: "Add auth service + OAuth + SSO",
183
+ rejection_reason: "Too large for single subtask",
184
+ },
185
+ };
186
+
187
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
188
+ });
84
189
  });
85
190
 
86
191
  describe("VIOLATION events", () => {
@@ -214,6 +319,25 @@ describe("CoordinatorEvent schemas", () => {
214
319
 
215
320
  expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
216
321
  });
322
+
323
+ test("validates blocker_detected event", () => {
324
+ const event: CoordinatorEvent = {
325
+ session_id: "test-session",
326
+ epic_id: "bd-123",
327
+ timestamp: new Date().toISOString(),
328
+ event_type: "OUTCOME",
329
+ outcome_type: "blocker_detected",
330
+ payload: {
331
+ worker_id: "GreenStorm",
332
+ subtask_id: "bd-123.2",
333
+ blocker_type: "dependency",
334
+ blocker_description: "Waiting for database schema from bd-123.1",
335
+ reported_at: new Date().toISOString(),
336
+ },
337
+ };
338
+
339
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
340
+ });
217
341
  });
218
342
  });
219
343
 
@@ -336,6 +460,127 @@ describe("captureCoordinatorEvent", () => {
336
460
  });
337
461
  });
338
462
 
463
+ describe("COMPACTION events", () => {
464
+ test("validates detection_complete event", () => {
465
+ const event: CoordinatorEvent = {
466
+ session_id: "test-session",
467
+ epic_id: "bd-123",
468
+ timestamp: new Date().toISOString(),
469
+ event_type: "COMPACTION",
470
+ compaction_type: "detection_complete",
471
+ payload: {
472
+ confidence: "high",
473
+ context_type: "full",
474
+ epic_id: "bd-456",
475
+ },
476
+ };
477
+
478
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
479
+ });
480
+
481
+ test("validates prompt_generated event", () => {
482
+ const event: CoordinatorEvent = {
483
+ session_id: "test-session",
484
+ epic_id: "bd-123",
485
+ timestamp: new Date().toISOString(),
486
+ event_type: "COMPACTION",
487
+ compaction_type: "prompt_generated",
488
+ payload: {
489
+ prompt_length: 5000,
490
+ full_prompt: "You are a coordinator...", // Full prompt content captured
491
+ context_type: "full",
492
+ },
493
+ };
494
+
495
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
496
+ });
497
+
498
+ test("validates context_injected event", () => {
499
+ const event: CoordinatorEvent = {
500
+ session_id: "test-session",
501
+ epic_id: "bd-123",
502
+ timestamp: new Date().toISOString(),
503
+ event_type: "COMPACTION",
504
+ compaction_type: "context_injected",
505
+ payload: {
506
+ context_type: "fallback",
507
+ injected_sections: ["swarm_status", "mandatory_instructions"],
508
+ },
509
+ };
510
+
511
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
512
+ });
513
+
514
+ test("validates resumption_started event", () => {
515
+ const event: CoordinatorEvent = {
516
+ session_id: "test-session",
517
+ epic_id: "bd-123",
518
+ timestamp: new Date().toISOString(),
519
+ event_type: "COMPACTION",
520
+ compaction_type: "resumption_started",
521
+ payload: {
522
+ epic_id: "bd-456",
523
+ agent_role: "coordinator",
524
+ context_loaded: true,
525
+ },
526
+ };
527
+
528
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
529
+ });
530
+
531
+ test("validates tool_call_tracked event", () => {
532
+ const event: CoordinatorEvent = {
533
+ session_id: "test-session",
534
+ epic_id: "bd-123",
535
+ timestamp: new Date().toISOString(),
536
+ event_type: "COMPACTION",
537
+ compaction_type: "tool_call_tracked",
538
+ payload: {
539
+ tool_name: "hive_create_epic",
540
+ extracted_data: {
541
+ epic_id: "bd-789",
542
+ epic_title: "Add auth",
543
+ },
544
+ },
545
+ };
546
+
547
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
548
+ });
549
+
550
+ test("rejects invalid compaction_type", () => {
551
+ const event = {
552
+ session_id: "test-session",
553
+ epic_id: "bd-123",
554
+ timestamp: new Date().toISOString(),
555
+ event_type: "COMPACTION",
556
+ compaction_type: "invalid_type",
557
+ payload: {},
558
+ };
559
+
560
+ expect(() => CoordinatorEventSchema.parse(event)).toThrow();
561
+ });
562
+
563
+ test("captures full prompt content without truncation", () => {
564
+ const longPrompt = "A".repeat(10000); // 10k chars
565
+ const event: CoordinatorEvent = {
566
+ session_id: "test-session",
567
+ epic_id: "bd-123",
568
+ timestamp: new Date().toISOString(),
569
+ event_type: "COMPACTION",
570
+ compaction_type: "prompt_generated",
571
+ payload: {
572
+ prompt_length: longPrompt.length,
573
+ full_prompt: longPrompt,
574
+ context_type: "full",
575
+ },
576
+ };
577
+
578
+ expect(() => CoordinatorEventSchema.parse(event)).not.toThrow();
579
+ expect(event.payload.full_prompt).toBe(longPrompt);
580
+ expect(event.payload.full_prompt.length).toBe(10000);
581
+ });
582
+ });
583
+
339
584
  describe("saveSession", () => {
340
585
  let sessionDir: string;
341
586
  let sessionId: string;
@@ -388,3 +633,383 @@ describe("saveSession", () => {
388
633
  expect(session).toBeNull();
389
634
  });
390
635
  });
636
+
637
+ describe("session_id propagation from ctx.sessionID", () => {
638
+ let sessionDir: string;
639
+ let sessionId: string;
640
+
641
+ beforeEach(() => {
642
+ sessionDir = path.join(os.homedir(), ".config", "swarm-tools", "sessions");
643
+ sessionId = `test-ctx-${Date.now()}`;
644
+ });
645
+
646
+ afterEach(() => {
647
+ // Clean up test session file
648
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
649
+ if (fs.existsSync(sessionPath)) {
650
+ fs.unlinkSync(sessionPath);
651
+ }
652
+ });
653
+
654
+ test("session_id should come from ctx.sessionID, not process.env", () => {
655
+ // GIVEN: process.env.OPENCODE_SESSION_ID is empty (mimics real scenario)
656
+ const oldEnv = process.env.OPENCODE_SESSION_ID;
657
+ delete process.env.OPENCODE_SESSION_ID;
658
+
659
+ try {
660
+ // WHEN: captureCoordinatorEvent is called with session_id from ctx.sessionID
661
+ const event: CoordinatorEvent = {
662
+ session_id: sessionId, // This should come from ctx.sessionID in call sites
663
+ epic_id: "bd-123",
664
+ timestamp: new Date().toISOString(),
665
+ event_type: "DECISION",
666
+ decision_type: "strategy_selected",
667
+ payload: { strategy: "file-based" },
668
+ };
669
+
670
+ captureCoordinatorEvent(event);
671
+
672
+ // THEN: Event should be captured with correct session_id
673
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
674
+ expect(fs.existsSync(sessionPath)).toBe(true);
675
+
676
+ const content = fs.readFileSync(sessionPath, "utf-8");
677
+ const parsed = JSON.parse(content.trim());
678
+ expect(parsed.session_id).toBe(sessionId);
679
+ expect(parsed.session_id).not.toBe("unknown");
680
+ } finally {
681
+ // Restore env
682
+ if (oldEnv !== undefined) {
683
+ process.env.OPENCODE_SESSION_ID = oldEnv;
684
+ }
685
+ }
686
+ });
687
+
688
+ test("demonstrates call sites must pass ctx.sessionID not process.env", () => {
689
+ // GIVEN: This simulates what happens in real call sites
690
+ const oldEnv = process.env.OPENCODE_SESSION_ID;
691
+ delete process.env.OPENCODE_SESSION_ID; // Empty in real OpenCode environment
692
+
693
+ try {
694
+ // WHEN: Call site uses process.env (CURRENT BAD PATTERN)
695
+ const badSessionId = process.env.OPENCODE_SESSION_ID || "unknown";
696
+ const badEvent: CoordinatorEvent = {
697
+ session_id: badSessionId, // This evaluates to "unknown"
698
+ epic_id: "bd-123",
699
+ timestamp: new Date().toISOString(),
700
+ event_type: "DECISION",
701
+ decision_type: "strategy_selected",
702
+ payload: { strategy: "file-based" },
703
+ };
704
+
705
+ captureCoordinatorEvent(badEvent);
706
+
707
+ // THEN: Event goes to unknown.jsonl (BAD!)
708
+ const unknownPath = path.join(sessionDir, "unknown.jsonl");
709
+ expect(fs.existsSync(unknownPath)).toBe(true);
710
+
711
+ // WHEN: Call site uses ctx.sessionID (CORRECT PATTERN)
712
+ const goodEvent: CoordinatorEvent = {
713
+ session_id: sessionId, // From ctx.sessionID
714
+ epic_id: "bd-123",
715
+ timestamp: new Date().toISOString(),
716
+ event_type: "DECISION",
717
+ decision_type: "strategy_selected",
718
+ payload: { strategy: "file-based" },
719
+ };
720
+
721
+ captureCoordinatorEvent(goodEvent);
722
+
723
+ // THEN: Event goes to correct session file
724
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
725
+ expect(fs.existsSync(sessionPath)).toBe(true);
726
+ } finally {
727
+ if (oldEnv !== undefined) {
728
+ process.env.OPENCODE_SESSION_ID = oldEnv;
729
+ }
730
+ }
731
+ });
732
+
733
+ test("verifies all call sites now use ctx.sessionID", () => {
734
+ // This test documents that we've fixed all call sites to use ctx.sessionID
735
+ // instead of process.env.OPENCODE_SESSION_ID
736
+
737
+ // The fix was applied to:
738
+ // 1. src/swarm-orchestrate.ts:1743, 1852 - swarm_complete uses _ctx.sessionID
739
+ // 2. src/swarm-review.ts:515, 565 - swarm_review_feedback uses _ctx.sessionID
740
+ // 3. src/swarm-decompose.ts:780 - swarm_delegate_planning uses _ctx.sessionID
741
+ // 4. src/swarm-prompts.ts:1407 - swarm_spawn_subtask uses _ctx.sessionID
742
+ // 5. src/index.ts:216 - detectCoordinatorViolation uses input.sessionID
743
+
744
+ // With ctx.sessionID, events go to proper session files
745
+ const oldEnv = process.env.OPENCODE_SESSION_ID;
746
+ delete process.env.OPENCODE_SESSION_ID;
747
+
748
+ try {
749
+ // Simulate tool execution with ctx.sessionID
750
+ const mockCtx = { sessionID: sessionId };
751
+
752
+ const event: CoordinatorEvent = {
753
+ session_id: mockCtx.sessionID || "unknown",
754
+ epic_id: "bd-456",
755
+ timestamp: new Date().toISOString(),
756
+ event_type: "OUTCOME",
757
+ outcome_type: "subtask_success",
758
+ payload: { bead_id: "bd-456.1" },
759
+ };
760
+
761
+ captureCoordinatorEvent(event);
762
+
763
+ // Verify event captured with correct session_id
764
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
765
+ expect(fs.existsSync(sessionPath)).toBe(true);
766
+
767
+ const content = fs.readFileSync(sessionPath, "utf-8");
768
+ const parsed = JSON.parse(content.trim());
769
+ expect(parsed.session_id).toBe(sessionId);
770
+ } finally {
771
+ if (oldEnv !== undefined) {
772
+ process.env.OPENCODE_SESSION_ID = oldEnv;
773
+ }
774
+ }
775
+ });
776
+ });
777
+
778
+ describe("captureCompactionEvent", () => {
779
+ let sessionDir: string;
780
+ let sessionId: string;
781
+
782
+ beforeEach(() => {
783
+ sessionDir = path.join(os.homedir(), ".config", "swarm-tools", "sessions");
784
+ sessionId = `test-compaction-${Date.now()}`;
785
+ });
786
+
787
+ afterEach(() => {
788
+ // Clean up test session file
789
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
790
+ if (fs.existsSync(sessionPath)) {
791
+ fs.unlinkSync(sessionPath);
792
+ }
793
+ });
794
+
795
+ test("writes detection_complete event to session file", () => {
796
+ captureCompactionEvent({
797
+ session_id: sessionId,
798
+ epic_id: "bd-123",
799
+ compaction_type: "detection_complete",
800
+ payload: {
801
+ confidence: "high",
802
+ context_type: "full",
803
+ epic_id: "bd-456",
804
+ },
805
+ });
806
+
807
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
808
+ expect(fs.existsSync(sessionPath)).toBe(true);
809
+
810
+ const content = fs.readFileSync(sessionPath, "utf-8");
811
+ const lines = content.trim().split("\n");
812
+ expect(lines).toHaveLength(1);
813
+
814
+ const parsed = JSON.parse(lines[0]);
815
+ expect(parsed.event_type).toBe("COMPACTION");
816
+ expect(parsed.compaction_type).toBe("detection_complete");
817
+ expect(parsed.payload.confidence).toBe("high");
818
+ });
819
+
820
+ test("writes prompt_generated event with full prompt content", () => {
821
+ const fullPrompt = "You are a coordinator agent. ".repeat(200); // ~6k chars
822
+
823
+ captureCompactionEvent({
824
+ session_id: sessionId,
825
+ epic_id: "bd-123",
826
+ compaction_type: "prompt_generated",
827
+ payload: {
828
+ prompt_length: fullPrompt.length,
829
+ full_prompt: fullPrompt,
830
+ context_type: "full",
831
+ },
832
+ });
833
+
834
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
835
+ const content = fs.readFileSync(sessionPath, "utf-8");
836
+ const parsed = JSON.parse(content.trim());
837
+
838
+ expect(parsed.payload.full_prompt).toBe(fullPrompt);
839
+ expect(parsed.payload.full_prompt.length).toBe(fullPrompt.length);
840
+ });
841
+
842
+ test("appends multiple compaction events to same session", () => {
843
+ captureCompactionEvent({
844
+ session_id: sessionId,
845
+ epic_id: "bd-123",
846
+ compaction_type: "detection_complete",
847
+ payload: { confidence: "high" },
848
+ });
849
+
850
+ captureCompactionEvent({
851
+ session_id: sessionId,
852
+ epic_id: "bd-123",
853
+ compaction_type: "prompt_generated",
854
+ payload: { prompt_length: 1000, full_prompt: "test" },
855
+ });
856
+
857
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
858
+ const content = fs.readFileSync(sessionPath, "utf-8");
859
+ const lines = content.trim().split("\n");
860
+ expect(lines).toHaveLength(2);
861
+
862
+ const event1 = JSON.parse(lines[0]);
863
+ const event2 = JSON.parse(lines[1]);
864
+
865
+ expect(event1.compaction_type).toBe("detection_complete");
866
+ expect(event2.compaction_type).toBe("prompt_generated");
867
+ });
868
+
869
+ test("full compaction lifecycle tracking", () => {
870
+ // Simulate full compaction hook lifecycle
871
+ const lifecycleEvents = [
872
+ {
873
+ compaction_type: "detection_complete" as const,
874
+ payload: {
875
+ confidence: "high",
876
+ context_type: "full",
877
+ epic_id: "bd-789",
878
+ },
879
+ },
880
+ {
881
+ compaction_type: "prompt_generated" as const,
882
+ payload: {
883
+ prompt_length: 3500,
884
+ full_prompt: "You are a coordinator agent...",
885
+ context_type: "full",
886
+ },
887
+ },
888
+ {
889
+ compaction_type: "context_injected" as const,
890
+ payload: {
891
+ context_type: "full",
892
+ injected_sections: ["swarm_status", "mandatory_instructions"],
893
+ },
894
+ },
895
+ {
896
+ compaction_type: "resumption_started" as const,
897
+ payload: {
898
+ epic_id: "bd-789",
899
+ agent_role: "coordinator",
900
+ context_loaded: true,
901
+ },
902
+ },
903
+ {
904
+ compaction_type: "tool_call_tracked" as const,
905
+ payload: {
906
+ tool_name: "hive_create_epic",
907
+ extracted_data: { epic_id: "bd-789" },
908
+ },
909
+ },
910
+ ];
911
+
912
+ // Capture all lifecycle events
913
+ for (const event of lifecycleEvents) {
914
+ captureCompactionEvent({
915
+ session_id: sessionId,
916
+ epic_id: "bd-123",
917
+ ...event,
918
+ });
919
+ }
920
+
921
+ // Verify all events captured
922
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
923
+ const content = fs.readFileSync(sessionPath, "utf-8");
924
+ const lines = content.trim().split("\n");
925
+ expect(lines).toHaveLength(5);
926
+
927
+ // Verify lifecycle order
928
+ const capturedEvents = lines.map((line) => JSON.parse(line));
929
+ expect(capturedEvents[0].compaction_type).toBe("detection_complete");
930
+ expect(capturedEvents[1].compaction_type).toBe("prompt_generated");
931
+ expect(capturedEvents[2].compaction_type).toBe("context_injected");
932
+ expect(capturedEvents[3].compaction_type).toBe("resumption_started");
933
+ expect(capturedEvents[4].compaction_type).toBe("tool_call_tracked");
934
+ });
935
+ });
936
+
937
+ describe("hive_create_epic integration - decomposition_complete event", () => {
938
+ let sessionDir: string;
939
+ let sessionId: string;
940
+ const testProjectPath = "/tmp/test-epic-decomposition";
941
+
942
+ beforeEach(() => {
943
+ sessionDir = path.join(os.homedir(), ".config", "swarm-tools", "sessions");
944
+ sessionId = `test-epic-${Date.now()}`;
945
+ });
946
+
947
+ afterEach(() => {
948
+ // Clean up test session file
949
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
950
+ if (fs.existsSync(sessionPath)) {
951
+ fs.unlinkSync(sessionPath);
952
+ }
953
+ });
954
+
955
+ test("captures decomposition_complete event after hive_create_epic succeeds", async () => {
956
+ // Test the event capture by calling captureCoordinatorEvent directly
957
+ // Testing hive_create_epic directly would require full plugin infrastructure
958
+
959
+ // GIVEN: We simulate what hive_create_epic does after epic creation
960
+ const epicId = `test-epic-${Date.now()}`;
961
+ const subtasks = [
962
+ { title: "Subtask 1", files: ["src/a.ts"] },
963
+ { title: "Subtask 2", files: ["src/b.ts", "src/c.ts"] },
964
+ { title: "Subtask 3", files: ["src/d.ts"] },
965
+ ];
966
+
967
+ // Build files_per_subtask map (same logic as hive.ts)
968
+ const filesPerSubtask: Record<number, string[]> = {};
969
+ subtasks.forEach((subtask, index) => {
970
+ if (subtask.files && subtask.files.length > 0) {
971
+ filesPerSubtask[index] = subtask.files;
972
+ }
973
+ });
974
+
975
+ // WHEN: decomposition_complete event is captured
976
+ captureCoordinatorEvent({
977
+ session_id: sessionId,
978
+ epic_id: epicId,
979
+ timestamp: new Date().toISOString(),
980
+ event_type: "DECISION",
981
+ decision_type: "decomposition_complete",
982
+ payload: {
983
+ subtask_count: subtasks.length,
984
+ strategy_used: "file-based",
985
+ files_per_subtask: filesPerSubtask,
986
+ epic_title: "Test Epic for Event Capture",
987
+ task: "Original task description",
988
+ },
989
+ });
990
+
991
+ // THEN: Event should be written to session file
992
+ const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
993
+ expect(fs.existsSync(sessionPath)).toBe(true);
994
+
995
+ const content = fs.readFileSync(sessionPath, "utf-8");
996
+ const lines = content.trim().split("\n").filter(Boolean);
997
+ expect(lines.length).toBe(1);
998
+
999
+ // Verify event structure
1000
+ const event = JSON.parse(lines[0]);
1001
+ expect(event.session_id).toBe(sessionId);
1002
+ expect(event.epic_id).toBe(epicId);
1003
+ expect(event.event_type).toBe("DECISION");
1004
+ expect(event.decision_type).toBe("decomposition_complete");
1005
+ expect(event.payload.subtask_count).toBe(3);
1006
+ expect(event.payload.strategy_used).toBe("file-based");
1007
+ expect(event.payload.files_per_subtask).toEqual({
1008
+ 0: ["src/a.ts"],
1009
+ 1: ["src/b.ts", "src/c.ts"],
1010
+ 2: ["src/d.ts"],
1011
+ });
1012
+ expect(event.payload.epic_title).toBe("Test Epic for Event Capture");
1013
+ expect(event.payload.task).toBe("Original task description");
1014
+ });
1015
+ });