opencode-swarm-plugin 0.44.0 → 0.44.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 (205) hide show
  1. package/bin/swarm.serve.test.ts +6 -4
  2. package/bin/swarm.ts +16 -10
  3. package/dist/compaction-prompt-scoring.js +139 -0
  4. package/dist/eval-capture.js +12811 -0
  5. package/dist/hive.d.ts.map +1 -1
  6. package/dist/index.js +7644 -62599
  7. package/dist/plugin.js +23766 -78721
  8. package/dist/swarm-orchestrate.d.ts.map +1 -1
  9. package/dist/swarm-prompts.d.ts.map +1 -1
  10. package/dist/swarm-review.d.ts.map +1 -1
  11. package/package.json +17 -5
  12. package/.changeset/swarm-insights-data-layer.md +0 -63
  13. package/.hive/analysis/eval-failure-analysis-2025-12-25.md +0 -331
  14. package/.hive/analysis/session-data-quality-audit.md +0 -320
  15. package/.hive/eval-results.json +0 -483
  16. package/.hive/issues.jsonl +0 -138
  17. package/.hive/memories.jsonl +0 -729
  18. package/.opencode/eval-history.jsonl +0 -327
  19. package/.turbo/turbo-build.log +0 -9
  20. package/CHANGELOG.md +0 -2286
  21. package/SCORER-ANALYSIS.md +0 -598
  22. package/docs/analysis/subagent-coordination-patterns.md +0 -902
  23. package/docs/analysis-socratic-planner-pattern.md +0 -504
  24. package/docs/planning/ADR-001-monorepo-structure.md +0 -171
  25. package/docs/planning/ADR-002-package-extraction.md +0 -393
  26. package/docs/planning/ADR-003-performance-improvements.md +0 -451
  27. package/docs/planning/ADR-004-message-queue-features.md +0 -187
  28. package/docs/planning/ADR-005-devtools-observability.md +0 -202
  29. package/docs/planning/ADR-007-swarm-enhancements-worktree-review.md +0 -168
  30. package/docs/planning/ADR-008-worker-handoff-protocol.md +0 -293
  31. package/docs/planning/ADR-009-oh-my-opencode-patterns.md +0 -353
  32. package/docs/planning/ADR-010-cass-inhousing.md +0 -1215
  33. package/docs/planning/ROADMAP.md +0 -368
  34. package/docs/semantic-memory-cli-syntax.md +0 -123
  35. package/docs/swarm-mail-architecture.md +0 -1147
  36. package/docs/testing/context-recovery-test.md +0 -470
  37. package/evals/ARCHITECTURE.md +0 -1189
  38. package/evals/README.md +0 -768
  39. package/evals/compaction-prompt.eval.ts +0 -149
  40. package/evals/compaction-resumption.eval.ts +0 -289
  41. package/evals/coordinator-behavior.eval.ts +0 -307
  42. package/evals/coordinator-session.eval.ts +0 -154
  43. package/evals/evalite.config.ts.bak +0 -15
  44. package/evals/example.eval.ts +0 -31
  45. package/evals/fixtures/cass-baseline.ts +0 -217
  46. package/evals/fixtures/compaction-cases.ts +0 -350
  47. package/evals/fixtures/compaction-prompt-cases.ts +0 -311
  48. package/evals/fixtures/coordinator-sessions.ts +0 -328
  49. package/evals/fixtures/decomposition-cases.ts +0 -105
  50. package/evals/lib/compaction-loader.test.ts +0 -248
  51. package/evals/lib/compaction-loader.ts +0 -320
  52. package/evals/lib/data-loader.evalite-test.ts +0 -289
  53. package/evals/lib/data-loader.test.ts +0 -345
  54. package/evals/lib/data-loader.ts +0 -281
  55. package/evals/lib/llm.ts +0 -115
  56. package/evals/scorers/compaction-prompt-scorers.ts +0 -145
  57. package/evals/scorers/compaction-scorers.ts +0 -305
  58. package/evals/scorers/coordinator-discipline.evalite-test.ts +0 -539
  59. package/evals/scorers/coordinator-discipline.ts +0 -325
  60. package/evals/scorers/index.test.ts +0 -146
  61. package/evals/scorers/index.ts +0 -328
  62. package/evals/scorers/outcome-scorers.evalite-test.ts +0 -27
  63. package/evals/scorers/outcome-scorers.ts +0 -349
  64. package/evals/swarm-decomposition.eval.ts +0 -121
  65. package/examples/commands/swarm.md +0 -745
  66. package/examples/plugin-wrapper-template.ts +0 -2515
  67. package/examples/skills/hive-workflow/SKILL.md +0 -212
  68. package/examples/skills/skill-creator/SKILL.md +0 -223
  69. package/examples/skills/swarm-coordination/SKILL.md +0 -292
  70. package/global-skills/cli-builder/SKILL.md +0 -344
  71. package/global-skills/cli-builder/references/advanced-patterns.md +0 -244
  72. package/global-skills/learning-systems/SKILL.md +0 -644
  73. package/global-skills/skill-creator/LICENSE.txt +0 -202
  74. package/global-skills/skill-creator/SKILL.md +0 -352
  75. package/global-skills/skill-creator/references/output-patterns.md +0 -82
  76. package/global-skills/skill-creator/references/workflows.md +0 -28
  77. package/global-skills/swarm-coordination/SKILL.md +0 -995
  78. package/global-skills/swarm-coordination/references/coordinator-patterns.md +0 -235
  79. package/global-skills/swarm-coordination/references/strategies.md +0 -138
  80. package/global-skills/system-design/SKILL.md +0 -213
  81. package/global-skills/testing-patterns/SKILL.md +0 -430
  82. package/global-skills/testing-patterns/references/dependency-breaking-catalog.md +0 -586
  83. package/opencode-swarm-plugin-0.30.7.tgz +0 -0
  84. package/opencode-swarm-plugin-0.31.0.tgz +0 -0
  85. package/scripts/cleanup-test-memories.ts +0 -346
  86. package/scripts/init-skill.ts +0 -222
  87. package/scripts/migrate-unknown-sessions.ts +0 -349
  88. package/scripts/validate-skill.ts +0 -204
  89. package/src/agent-mail.ts +0 -1724
  90. package/src/anti-patterns.test.ts +0 -1167
  91. package/src/anti-patterns.ts +0 -448
  92. package/src/compaction-capture.integration.test.ts +0 -257
  93. package/src/compaction-hook.test.ts +0 -838
  94. package/src/compaction-hook.ts +0 -1204
  95. package/src/compaction-observability.integration.test.ts +0 -139
  96. package/src/compaction-observability.test.ts +0 -187
  97. package/src/compaction-observability.ts +0 -324
  98. package/src/compaction-prompt-scorers.test.ts +0 -475
  99. package/src/compaction-prompt-scoring.ts +0 -300
  100. package/src/contributor-tools.test.ts +0 -133
  101. package/src/contributor-tools.ts +0 -201
  102. package/src/dashboard.test.ts +0 -611
  103. package/src/dashboard.ts +0 -462
  104. package/src/error-enrichment.test.ts +0 -403
  105. package/src/error-enrichment.ts +0 -219
  106. package/src/eval-capture.test.ts +0 -1015
  107. package/src/eval-capture.ts +0 -929
  108. package/src/eval-gates.test.ts +0 -306
  109. package/src/eval-gates.ts +0 -218
  110. package/src/eval-history.test.ts +0 -508
  111. package/src/eval-history.ts +0 -214
  112. package/src/eval-learning.test.ts +0 -378
  113. package/src/eval-learning.ts +0 -360
  114. package/src/eval-runner.test.ts +0 -223
  115. package/src/eval-runner.ts +0 -402
  116. package/src/export-tools.test.ts +0 -476
  117. package/src/export-tools.ts +0 -257
  118. package/src/hive.integration.test.ts +0 -2241
  119. package/src/hive.ts +0 -1628
  120. package/src/index.ts +0 -940
  121. package/src/learning.integration.test.ts +0 -1815
  122. package/src/learning.ts +0 -1079
  123. package/src/logger.test.ts +0 -189
  124. package/src/logger.ts +0 -135
  125. package/src/mandate-promotion.test.ts +0 -473
  126. package/src/mandate-promotion.ts +0 -239
  127. package/src/mandate-storage.integration.test.ts +0 -601
  128. package/src/mandate-storage.test.ts +0 -578
  129. package/src/mandate-storage.ts +0 -794
  130. package/src/mandates.ts +0 -540
  131. package/src/memory-tools.test.ts +0 -195
  132. package/src/memory-tools.ts +0 -344
  133. package/src/memory.integration.test.ts +0 -334
  134. package/src/memory.test.ts +0 -158
  135. package/src/memory.ts +0 -527
  136. package/src/model-selection.test.ts +0 -188
  137. package/src/model-selection.ts +0 -68
  138. package/src/observability-tools.test.ts +0 -359
  139. package/src/observability-tools.ts +0 -871
  140. package/src/output-guardrails.test.ts +0 -438
  141. package/src/output-guardrails.ts +0 -381
  142. package/src/pattern-maturity.test.ts +0 -1160
  143. package/src/pattern-maturity.ts +0 -525
  144. package/src/planning-guardrails.test.ts +0 -491
  145. package/src/planning-guardrails.ts +0 -438
  146. package/src/plugin.ts +0 -23
  147. package/src/post-compaction-tracker.test.ts +0 -251
  148. package/src/post-compaction-tracker.ts +0 -237
  149. package/src/query-tools.test.ts +0 -636
  150. package/src/query-tools.ts +0 -324
  151. package/src/rate-limiter.integration.test.ts +0 -466
  152. package/src/rate-limiter.ts +0 -774
  153. package/src/replay-tools.test.ts +0 -496
  154. package/src/replay-tools.ts +0 -240
  155. package/src/repo-crawl.integration.test.ts +0 -441
  156. package/src/repo-crawl.ts +0 -610
  157. package/src/schemas/cell-events.test.ts +0 -347
  158. package/src/schemas/cell-events.ts +0 -807
  159. package/src/schemas/cell.ts +0 -257
  160. package/src/schemas/evaluation.ts +0 -166
  161. package/src/schemas/index.test.ts +0 -199
  162. package/src/schemas/index.ts +0 -286
  163. package/src/schemas/mandate.ts +0 -232
  164. package/src/schemas/swarm-context.ts +0 -115
  165. package/src/schemas/task.ts +0 -161
  166. package/src/schemas/worker-handoff.test.ts +0 -302
  167. package/src/schemas/worker-handoff.ts +0 -131
  168. package/src/sessions/agent-discovery.test.ts +0 -137
  169. package/src/sessions/agent-discovery.ts +0 -112
  170. package/src/sessions/index.ts +0 -15
  171. package/src/skills.integration.test.ts +0 -1192
  172. package/src/skills.test.ts +0 -643
  173. package/src/skills.ts +0 -1549
  174. package/src/storage.integration.test.ts +0 -341
  175. package/src/storage.ts +0 -884
  176. package/src/structured.integration.test.ts +0 -817
  177. package/src/structured.test.ts +0 -1046
  178. package/src/structured.ts +0 -762
  179. package/src/swarm-decompose.test.ts +0 -188
  180. package/src/swarm-decompose.ts +0 -1302
  181. package/src/swarm-deferred.integration.test.ts +0 -157
  182. package/src/swarm-deferred.test.ts +0 -38
  183. package/src/swarm-insights.test.ts +0 -214
  184. package/src/swarm-insights.ts +0 -459
  185. package/src/swarm-mail.integration.test.ts +0 -970
  186. package/src/swarm-mail.ts +0 -739
  187. package/src/swarm-orchestrate.integration.test.ts +0 -282
  188. package/src/swarm-orchestrate.test.ts +0 -548
  189. package/src/swarm-orchestrate.ts +0 -3084
  190. package/src/swarm-prompts.test.ts +0 -1270
  191. package/src/swarm-prompts.ts +0 -2077
  192. package/src/swarm-research.integration.test.ts +0 -701
  193. package/src/swarm-research.test.ts +0 -698
  194. package/src/swarm-research.ts +0 -472
  195. package/src/swarm-review.integration.test.ts +0 -285
  196. package/src/swarm-review.test.ts +0 -879
  197. package/src/swarm-review.ts +0 -709
  198. package/src/swarm-strategies.ts +0 -407
  199. package/src/swarm-worktree.test.ts +0 -501
  200. package/src/swarm-worktree.ts +0 -575
  201. package/src/swarm.integration.test.ts +0 -2377
  202. package/src/swarm.ts +0 -38
  203. package/src/tool-adapter.integration.test.ts +0 -1221
  204. package/src/tool-availability.ts +0 -461
  205. package/tsconfig.json +0 -28
@@ -1,105 +0,0 @@
1
- /**
2
- * Test cases for swarm task decomposition
3
- *
4
- * Each case includes:
5
- * - input: task description and optional context
6
- * - expected: validation criteria (min/max subtasks, required files)
7
- */
8
-
9
- export interface DecompositionTestCase {
10
- input: {
11
- task: string;
12
- context?: string;
13
- };
14
- expected: {
15
- minSubtasks: number;
16
- maxSubtasks: number;
17
- requiredFiles?: string[];
18
- };
19
- }
20
-
21
- export const decompositionCases: DecompositionTestCase[] = [
22
- {
23
- input: {
24
- task: "Add user authentication with OAuth",
25
- context: "Next.js App Router application with existing user model",
26
- },
27
- expected: {
28
- minSubtasks: 3,
29
- maxSubtasks: 6,
30
- requiredFiles: [
31
- "src/auth/oauth.ts",
32
- "src/auth/middleware.ts",
33
- "app/api/auth/[...nextauth]/route.ts",
34
- ],
35
- },
36
- },
37
- {
38
- input: {
39
- task: "Implement rate limiting for API endpoints",
40
- context: "Express.js API with Redis available",
41
- },
42
- expected: {
43
- minSubtasks: 2,
44
- maxSubtasks: 4,
45
- requiredFiles: [
46
- "src/middleware/rate-limit.ts",
47
- "src/utils/redis-client.ts",
48
- ],
49
- },
50
- },
51
- {
52
- input: {
53
- task: "Add TypeScript strict mode to legacy JavaScript project",
54
- context: "Large codebase with 50+ JS files, currently untyped",
55
- },
56
- expected: {
57
- minSubtasks: 4,
58
- maxSubtasks: 8,
59
- requiredFiles: ["tsconfig.json"],
60
- },
61
- },
62
- {
63
- input: {
64
- task: "Create admin dashboard for user management",
65
- context: "React app with existing component library and API client",
66
- },
67
- expected: {
68
- minSubtasks: 4,
69
- maxSubtasks: 7,
70
- requiredFiles: [
71
- "src/pages/admin/Dashboard.tsx",
72
- "src/components/admin/UserTable.tsx",
73
- "src/api/admin.ts",
74
- ],
75
- },
76
- },
77
- {
78
- input: {
79
- task: "Fix memory leak in long-running background job",
80
- context:
81
- "Node.js worker that processes queue messages, memory grows over time",
82
- },
83
- expected: {
84
- minSubtasks: 2,
85
- maxSubtasks: 4,
86
- requiredFiles: ["src/workers/queue-processor.ts"],
87
- },
88
- },
89
- {
90
- input: {
91
- task: "Implement feature flag system with remote config",
92
- context:
93
- "Microservices architecture, need runtime toggles without deploys",
94
- },
95
- expected: {
96
- minSubtasks: 3,
97
- maxSubtasks: 6,
98
- requiredFiles: [
99
- "src/feature-flags/client.ts",
100
- "src/feature-flags/middleware.ts",
101
- "src/feature-flags/types.ts",
102
- ],
103
- },
104
- },
105
- ];
@@ -1,248 +0,0 @@
1
- /**
2
- * Compaction Data Loader Tests
3
- *
4
- * Tests loading COMPACTION events from session JSONL files.
5
- */
6
- import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7
- import * as fs from "node:fs";
8
- import * as os from "node:os";
9
- import * as path from "node:path";
10
- import type { CoordinatorEvent } from "../../src/eval-capture.js";
11
- import {
12
- loadCompactionEvents,
13
- loadCompactionSessions,
14
- } from "./compaction-loader.js";
15
-
16
- // Test fixtures directory
17
- const TEST_SESSION_DIR = path.join(
18
- os.tmpdir(),
19
- `test-sessions-${Date.now()}`,
20
- );
21
-
22
- /**
23
- * Create a test session JSONL file
24
- */
25
- function createSessionFile(
26
- sessionId: string,
27
- events: CoordinatorEvent[],
28
- ): void {
29
- const sessionPath = path.join(TEST_SESSION_DIR, `${sessionId}.jsonl`);
30
- const lines = events.map((e) => JSON.stringify(e)).join("\n");
31
- fs.writeFileSync(sessionPath, `${lines}\n`, "utf-8");
32
- }
33
-
34
- beforeAll(() => {
35
- // Create test session directory
36
- fs.mkdirSync(TEST_SESSION_DIR, { recursive: true });
37
-
38
- // Create test session files with COMPACTION events
39
- createSessionFile("session-1", [
40
- {
41
- session_id: "session-1",
42
- epic_id: "epic-1",
43
- timestamp: "2025-01-01T10:00:00.000Z",
44
- event_type: "DECISION",
45
- decision_type: "decomposition_complete",
46
- payload: { subtask_count: 3 },
47
- },
48
- {
49
- session_id: "session-1",
50
- epic_id: "epic-1",
51
- timestamp: "2025-01-01T10:05:00.000Z",
52
- event_type: "COMPACTION",
53
- compaction_type: "detection_complete",
54
- payload: {
55
- confidence: "high",
56
- context_type: "full",
57
- epic_id: "epic-1",
58
- },
59
- },
60
- {
61
- session_id: "session-1",
62
- epic_id: "epic-1",
63
- timestamp: "2025-01-01T10:06:00.000Z",
64
- event_type: "COMPACTION",
65
- compaction_type: "prompt_generated",
66
- payload: {
67
- prompt_length: 5000,
68
- full_prompt: "You are a coordinator...",
69
- context_type: "full",
70
- },
71
- },
72
- ]);
73
-
74
- createSessionFile("session-2", [
75
- {
76
- session_id: "session-2",
77
- epic_id: "epic-2",
78
- timestamp: "2025-01-02T10:00:00.000Z",
79
- event_type: "COMPACTION",
80
- compaction_type: "context_injected",
81
- payload: {
82
- injection_point: "tool_call",
83
- context_length: 3000,
84
- },
85
- },
86
- {
87
- session_id: "session-2",
88
- epic_id: "epic-2",
89
- timestamp: "2025-01-02T10:01:00.000Z",
90
- event_type: "COMPACTION",
91
- compaction_type: "resumption_started",
92
- payload: {
93
- epic_id: "epic-2",
94
- resumption_type: "coordinator",
95
- },
96
- },
97
- ]);
98
-
99
- // Session with no COMPACTION events
100
- createSessionFile("session-3", [
101
- {
102
- session_id: "session-3",
103
- epic_id: "epic-3",
104
- timestamp: "2025-01-03T10:00:00.000Z",
105
- event_type: "DECISION",
106
- decision_type: "worker_spawned",
107
- payload: { worker: "BlueLake", bead_id: "epic-3.1" },
108
- },
109
- ]);
110
- });
111
-
112
- afterAll(() => {
113
- // Clean up test session directory
114
- if (fs.existsSync(TEST_SESSION_DIR)) {
115
- fs.rmSync(TEST_SESSION_DIR, { recursive: true });
116
- }
117
- });
118
-
119
- describe("loadCompactionEvents", () => {
120
- test("loads all COMPACTION events from session directory", async () => {
121
- const events = await loadCompactionEvents(TEST_SESSION_DIR);
122
-
123
- expect(events.length).toBe(4);
124
- expect(events.every((e) => e.event_type === "COMPACTION")).toBe(true);
125
- });
126
-
127
- test("filters by compaction_type", async () => {
128
- const events = await loadCompactionEvents(TEST_SESSION_DIR, {
129
- compaction_type: "detection_complete",
130
- });
131
-
132
- expect(events.length).toBe(1);
133
- expect(events[0].compaction_type).toBe("detection_complete");
134
- });
135
-
136
- test("filters by session_ids", async () => {
137
- const events = await loadCompactionEvents(TEST_SESSION_DIR, {
138
- sessionIds: ["session-1"],
139
- });
140
-
141
- expect(events.length).toBe(2);
142
- expect(events.every((e) => e.session_id === "session-1")).toBe(true);
143
- });
144
-
145
- test("applies limit", async () => {
146
- const events = await loadCompactionEvents(TEST_SESSION_DIR, {
147
- limit: 2,
148
- });
149
-
150
- expect(events.length).toBe(2);
151
- });
152
-
153
- test("combines filters", async () => {
154
- const events = await loadCompactionEvents(TEST_SESSION_DIR, {
155
- compaction_type: "prompt_generated",
156
- sessionIds: ["session-1"],
157
- limit: 1,
158
- });
159
-
160
- expect(events.length).toBe(1);
161
- expect(events[0].compaction_type).toBe("prompt_generated");
162
- expect(events[0].session_id).toBe("session-1");
163
- });
164
-
165
- test("returns empty array for non-existent directory", async () => {
166
- const events = await loadCompactionEvents("/non/existent/path");
167
-
168
- expect(events).toEqual([]);
169
- });
170
-
171
- test("skips invalid JSONL lines", async () => {
172
- // Create session with invalid JSON
173
- const invalidPath = path.join(TEST_SESSION_DIR, "session-invalid.jsonl");
174
- fs.writeFileSync(
175
- invalidPath,
176
- 'invalid json\n{"session_id": "session-valid", "event_type": "COMPACTION", "compaction_type": "detection_complete", "epic_id": "epic-4", "timestamp": "2025-01-04T10:00:00.000Z", "payload": {}}\n',
177
- "utf-8",
178
- );
179
-
180
- const events = await loadCompactionEvents(TEST_SESSION_DIR);
181
-
182
- // Should skip invalid line but include valid one
183
- expect(events.some((e) => e.session_id === "session-valid")).toBe(true);
184
-
185
- // Clean up
186
- fs.unlinkSync(invalidPath);
187
- });
188
- });
189
-
190
- describe("loadCompactionSessions", () => {
191
- test("groups events by session_id", async () => {
192
- const sessions = await loadCompactionSessions(TEST_SESSION_DIR);
193
-
194
- expect(sessions.length).toBe(2); // session-1 and session-2 (session-3 has no COMPACTION events)
195
- expect(sessions[0].session_id).toBeDefined();
196
- expect(sessions[0].events.length).toBeGreaterThan(0);
197
- });
198
-
199
- test("includes session metadata", async () => {
200
- const sessions = await loadCompactionSessions(TEST_SESSION_DIR);
201
-
202
- const session1 = sessions.find((s) => s.session_id === "session-1");
203
- expect(session1).toBeDefined();
204
- if (session1) {
205
- expect(session1.epic_id).toBe("epic-1");
206
- expect(session1.start_time).toBeDefined();
207
- expect(session1.end_time).toBeDefined();
208
- }
209
- });
210
-
211
- test("filters by compaction_type", async () => {
212
- const sessions = await loadCompactionSessions(TEST_SESSION_DIR, {
213
- compaction_type: "detection_complete",
214
- });
215
-
216
- expect(sessions.length).toBe(1);
217
- expect(sessions[0].session_id).toBe("session-1");
218
- });
219
-
220
- test("filters by session_ids", async () => {
221
- const sessions = await loadCompactionSessions(TEST_SESSION_DIR, {
222
- sessionIds: ["session-2"],
223
- });
224
-
225
- expect(sessions.length).toBe(1);
226
- expect(sessions[0].session_id).toBe("session-2");
227
- });
228
-
229
- test("applies limit", async () => {
230
- const sessions = await loadCompactionSessions(TEST_SESSION_DIR, {
231
- limit: 1,
232
- });
233
-
234
- expect(sessions.length).toBe(1);
235
- });
236
-
237
- test("returns empty array for non-existent directory", async () => {
238
- const sessions = await loadCompactionSessions("/non/existent/path");
239
-
240
- expect(sessions).toEqual([]);
241
- });
242
-
243
- test("excludes sessions with no COMPACTION events", async () => {
244
- const sessions = await loadCompactionSessions(TEST_SESSION_DIR);
245
-
246
- expect(sessions.every((s) => s.session_id !== "session-3")).toBe(true);
247
- });
248
- });
@@ -1,320 +0,0 @@
1
- /**
2
- * Compaction Data Loader
3
- *
4
- * Loads COMPACTION events from session JSONL files for use in evals.
5
- *
6
- * Features:
7
- * - Lazy loading with early termination for large datasets
8
- * - Filtering by compaction_type, sessionIds, and limit
9
- * - Graceful error handling (skips invalid lines)
10
- * - Type-safe with Zod validation
11
- *
12
- * @module compaction-loader
13
- */
14
- import * as fs from "node:fs";
15
- import { createInterface } from "node:readline";
16
- import * as path from "node:path";
17
- import type { CoordinatorEvent } from "../../src/eval-capture.js";
18
- import { CoordinatorEventSchema } from "../../src/eval-capture.js";
19
-
20
- /**
21
- * Compaction event - subset of CoordinatorEvent with event_type === "COMPACTION"
22
- */
23
- export type CompactionEvent = Extract<
24
- CoordinatorEvent,
25
- { event_type: "COMPACTION" }
26
- >;
27
-
28
- /**
29
- * Compaction session - session with only COMPACTION events
30
- */
31
- export interface CompactionSession {
32
- session_id: string;
33
- epic_id: string;
34
- start_time: string;
35
- end_time: string;
36
- events: CompactionEvent[];
37
- }
38
-
39
- /**
40
- * Load options
41
- */
42
- export interface LoadOptions {
43
- /** Filter by compaction_type */
44
- compaction_type?:
45
- | "detection_complete"
46
- | "prompt_generated"
47
- | "context_injected"
48
- | "resumption_started"
49
- | "tool_call_tracked";
50
- /** Filter by session IDs */
51
- sessionIds?: string[];
52
- /** Limit number of results */
53
- limit?: number;
54
- }
55
-
56
- /**
57
- * Load COMPACTION events from session JSONL files
58
- *
59
- * Reads all .jsonl files in the session directory, parses events,
60
- * and returns only COMPACTION events matching the filters.
61
- *
62
- * @param sessionDir - Path to session directory (default: ~/.config/swarm-tools/sessions)
63
- * @param options - Filter options
64
- * @returns Array of compaction events
65
- *
66
- * @example
67
- * // Load all COMPACTION events
68
- * const events = await loadCompactionEvents("/path/to/sessions");
69
- *
70
- * @example
71
- * // Load only detection_complete events
72
- * const events = await loadCompactionEvents("/path/to/sessions", {
73
- * compaction_type: "detection_complete",
74
- * });
75
- *
76
- * @example
77
- * // Load events from specific sessions
78
- * const events = await loadCompactionEvents("/path/to/sessions", {
79
- * sessionIds: ["session-1", "session-2"],
80
- * limit: 10,
81
- * });
82
- */
83
- export async function loadCompactionEvents(
84
- sessionDir: string,
85
- options?: LoadOptions,
86
- ): Promise<CompactionEvent[]> {
87
- const { compaction_type, sessionIds, limit } = options ?? {};
88
-
89
- // Check if directory exists
90
- if (!fs.existsSync(sessionDir)) {
91
- return [];
92
- }
93
-
94
- let files: string[];
95
- try {
96
- // Read all .jsonl files
97
- files = fs.readdirSync(sessionDir).filter((f) => f.endsWith(".jsonl"));
98
- } catch (error) {
99
- // Directory exists but can't be read - log and return empty
100
- console.warn(`Failed to read session directory ${sessionDir}:`, error);
101
- return [];
102
- }
103
-
104
- // Filter by sessionIds if provided
105
- const targetFiles = sessionIds
106
- ? files.filter((f) => sessionIds.includes(f.replace(".jsonl", "")))
107
- : files;
108
-
109
- const events: CompactionEvent[] = [];
110
-
111
- for (const file of targetFiles) {
112
- const filePath = path.join(sessionDir, file);
113
-
114
- try {
115
- // Stream large files line-by-line to avoid loading entire file into memory
116
- const shouldStream = limit && limit < 100; // For small limits, streaming is overkill
117
-
118
- if (shouldStream) {
119
- // Use streaming for better memory efficiency
120
- const found = await loadFromFileStream(filePath, {
121
- compaction_type,
122
- remainingLimit: limit - events.length,
123
- });
124
- events.push(...found);
125
- } else {
126
- // For small files or no limit, read entire file (faster)
127
- const content = fs.readFileSync(filePath, "utf-8");
128
- const lines = content.trim().split("\n").filter(Boolean);
129
-
130
- for (const line of lines) {
131
- const event = parseLine(line);
132
- if (event && event.event_type === "COMPACTION") {
133
- // Filter by compaction_type if provided
134
- if (!compaction_type || event.compaction_type === compaction_type) {
135
- events.push(event);
136
-
137
- // Apply limit early to avoid processing unnecessary files
138
- if (limit && events.length >= limit) {
139
- return events.slice(0, limit);
140
- }
141
- }
142
- }
143
- }
144
- }
145
-
146
- // Early termination if limit reached
147
- if (limit && events.length >= limit) {
148
- return events.slice(0, limit);
149
- }
150
- } catch (error) {
151
- // Log file read errors but continue processing other files
152
- console.warn(`Failed to read session file ${filePath}:`, error);
153
- }
154
- }
155
-
156
- return limit ? events.slice(0, limit) : events;
157
- }
158
-
159
- /**
160
- * Parse a JSONL line into a CoordinatorEvent
161
- *
162
- * @param line - JSONL line to parse
163
- * @returns Parsed and validated event, or null if invalid
164
- */
165
- function parseLine(line: string): CoordinatorEvent | null {
166
- try {
167
- const parsed = JSON.parse(line);
168
- return CoordinatorEventSchema.parse(parsed);
169
- } catch {
170
- // Invalid JSON or failed validation - skip silently
171
- return null;
172
- }
173
- }
174
-
175
- /**
176
- * Load COMPACTION events from a file using streaming (for large files)
177
- *
178
- * @param filePath - Path to session JSONL file
179
- * @param options - Filter options
180
- * @returns Array of matching compaction events
181
- */
182
- async function loadFromFileStream(
183
- filePath: string,
184
- options: {
185
- compaction_type?: LoadOptions["compaction_type"];
186
- remainingLimit?: number;
187
- },
188
- ): Promise<CompactionEvent[]> {
189
- const { compaction_type, remainingLimit } = options;
190
- const events: CompactionEvent[] = [];
191
-
192
- const fileStream = fs.createReadStream(filePath, { encoding: "utf-8" });
193
- const rl = createInterface({ input: fileStream, crlfDelay: Number.POSITIVE_INFINITY });
194
-
195
- for await (const line of rl) {
196
- const event = parseLine(line);
197
- if (event && event.event_type === "COMPACTION") {
198
- if (!compaction_type || event.compaction_type === compaction_type) {
199
- events.push(event);
200
-
201
- // Early termination for streaming
202
- if (remainingLimit && events.length >= remainingLimit) {
203
- rl.close();
204
- fileStream.close();
205
- break;
206
- }
207
- }
208
- }
209
- }
210
-
211
- return events;
212
- }
213
-
214
- /**
215
- * Load COMPACTION sessions grouped by session_id
216
- *
217
- * Groups COMPACTION events by session_id and returns session metadata.
218
- *
219
- * @param sessionDir - Path to session directory
220
- * @param options - Filter options
221
- * @returns Array of compaction sessions
222
- *
223
- * @example
224
- * // Load all sessions with COMPACTION events
225
- * const sessions = await loadCompactionSessions("/path/to/sessions");
226
- *
227
- * @example
228
- * // Load sessions with specific compaction_type
229
- * const sessions = await loadCompactionSessions("/path/to/sessions", {
230
- * compaction_type: "prompt_generated",
231
- * });
232
- */
233
- export async function loadCompactionSessions(
234
- sessionDir: string,
235
- options?: LoadOptions,
236
- ): Promise<CompactionSession[]> {
237
- const events = await loadCompactionEvents(sessionDir, options);
238
-
239
- if (events.length === 0) {
240
- return [];
241
- }
242
-
243
- // Group events by session_id
244
- const sessionMap = new Map<string, CompactionEvent[]>();
245
-
246
- for (const event of events) {
247
- const existing = sessionMap.get(event.session_id);
248
- if (existing) {
249
- existing.push(event);
250
- } else {
251
- sessionMap.set(event.session_id, [event]);
252
- }
253
- }
254
-
255
- // Build sessions with metadata
256
- const sessions: CompactionSession[] = [];
257
-
258
- for (const [sessionId, sessionEvents] of sessionMap.entries()) {
259
- if (sessionEvents.length === 0) {
260
- continue;
261
- }
262
-
263
- // Get epic_id from first event
264
- const epicId = sessionEvents[0].epic_id;
265
-
266
- // Get timestamps
267
- const timestamps = sessionEvents.map((e) => new Date(e.timestamp).getTime());
268
- const startTime = new Date(Math.min(...timestamps)).toISOString();
269
- const endTime = new Date(Math.max(...timestamps)).toISOString();
270
-
271
- sessions.push({
272
- session_id: sessionId,
273
- epic_id: epicId,
274
- start_time: startTime,
275
- end_time: endTime,
276
- events: sessionEvents,
277
- });
278
- }
279
-
280
- // Apply limit
281
- return options?.limit ? sessions.slice(0, options.limit) : sessions;
282
- }
283
-
284
- /**
285
- * Load COMPACTION events from default session directory
286
- *
287
- * Convenience wrapper that uses the default ~/.config/swarm-tools/sessions directory.
288
- *
289
- * @param options - Filter options
290
- * @returns Array of compaction events
291
- *
292
- * @example
293
- * // Load recent compaction events
294
- * const events = await loadDefaultCompactionEvents({ limit: 10 });
295
- */
296
- export async function loadDefaultCompactionEvents(
297
- options?: LoadOptions,
298
- ): Promise<CompactionEvent[]> {
299
- const { getSessionDir } = await import("../../src/eval-capture.js");
300
- return loadCompactionEvents(getSessionDir(), options);
301
- }
302
-
303
- /**
304
- * Load COMPACTION sessions from default session directory
305
- *
306
- * Convenience wrapper that uses the default ~/.config/swarm-tools/sessions directory.
307
- *
308
- * @param options - Filter options
309
- * @returns Array of compaction sessions
310
- *
311
- * @example
312
- * // Load all compaction sessions
313
- * const sessions = await loadDefaultCompactionSessions();
314
- */
315
- export async function loadDefaultCompactionSessions(
316
- options?: LoadOptions,
317
- ): Promise<CompactionSession[]> {
318
- const { getSessionDir } = await import("../../src/eval-capture.js");
319
- return loadCompactionSessions(getSessionDir(), options);
320
- }