opencode-swarm-plugin 0.32.0 → 0.34.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.
- package/.hive/issues.jsonl +12 -0
- package/.hive/memories.jsonl +255 -1
- package/.turbo/turbo-build.log +9 -10
- package/.turbo/turbo-test.log +343 -337
- package/CHANGELOG.md +358 -0
- package/README.md +152 -179
- package/bin/swarm.test.ts +303 -1
- package/bin/swarm.ts +473 -16
- package/dist/compaction-hook.d.ts +1 -1
- package/dist/compaction-hook.d.ts.map +1 -1
- package/dist/index.d.ts +112 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12380 -131
- package/dist/logger.d.ts +34 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/observability-tools.d.ts +116 -0
- package/dist/observability-tools.d.ts.map +1 -0
- package/dist/plugin.js +12254 -119
- package/dist/skills.d.ts.map +1 -1
- package/dist/swarm-orchestrate.d.ts +105 -0
- package/dist/swarm-orchestrate.d.ts.map +1 -1
- package/dist/swarm-prompts.d.ts +113 -2
- package/dist/swarm-prompts.d.ts.map +1 -1
- package/dist/swarm-research.d.ts +127 -0
- package/dist/swarm-research.d.ts.map +1 -0
- package/dist/swarm-review.d.ts.map +1 -1
- package/dist/swarm.d.ts +73 -1
- package/dist/swarm.d.ts.map +1 -1
- package/evals/compaction-resumption.eval.ts +289 -0
- package/evals/coordinator-behavior.eval.ts +307 -0
- package/evals/fixtures/compaction-cases.ts +350 -0
- package/evals/scorers/compaction-scorers.ts +305 -0
- package/evals/scorers/index.ts +12 -0
- package/examples/plugin-wrapper-template.ts +297 -8
- package/package.json +6 -2
- package/src/compaction-hook.test.ts +617 -1
- package/src/compaction-hook.ts +291 -18
- package/src/index.ts +54 -1
- package/src/logger.test.ts +189 -0
- package/src/logger.ts +135 -0
- package/src/observability-tools.test.ts +346 -0
- package/src/observability-tools.ts +594 -0
- package/src/skills.integration.test.ts +137 -1
- package/src/skills.test.ts +42 -1
- package/src/skills.ts +8 -4
- package/src/swarm-orchestrate.test.ts +123 -0
- package/src/swarm-orchestrate.ts +183 -0
- package/src/swarm-prompts.test.ts +553 -1
- package/src/swarm-prompts.ts +406 -4
- package/src/swarm-research.integration.test.ts +544 -0
- package/src/swarm-research.test.ts +698 -0
- package/src/swarm-research.ts +472 -0
- package/src/swarm-review.test.ts +177 -0
- package/src/swarm-review.ts +12 -47
- package/src/swarm.ts +6 -3
|
@@ -2,13 +2,32 @@
|
|
|
2
2
|
* Tests for Swarm-Aware Compaction Hook
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import { describe, expect, it, mock } from "bun:test";
|
|
5
|
+
import { describe, expect, it, mock, beforeEach } from "bun:test";
|
|
6
6
|
import {
|
|
7
7
|
SWARM_COMPACTION_CONTEXT,
|
|
8
8
|
SWARM_DETECTION_FALLBACK,
|
|
9
9
|
createCompactionHook,
|
|
10
10
|
} from "./compaction-hook";
|
|
11
11
|
|
|
12
|
+
// Track log calls for verification
|
|
13
|
+
let logCalls: Array<{ level: string; data: any; message?: string }> = [];
|
|
14
|
+
|
|
15
|
+
// Mock logger factory
|
|
16
|
+
const createMockLogger = () => ({
|
|
17
|
+
info: (data: any, message?: string) => {
|
|
18
|
+
logCalls.push({ level: "info", data, message });
|
|
19
|
+
},
|
|
20
|
+
debug: (data: any, message?: string) => {
|
|
21
|
+
logCalls.push({ level: "debug", data, message });
|
|
22
|
+
},
|
|
23
|
+
warn: (data: any, message?: string) => {
|
|
24
|
+
logCalls.push({ level: "warn", data, message });
|
|
25
|
+
},
|
|
26
|
+
error: (data: any, message?: string) => {
|
|
27
|
+
logCalls.push({ level: "error", data, message });
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
12
31
|
// Mock the dependencies
|
|
13
32
|
mock.module("./hive", () => ({
|
|
14
33
|
getHiveWorkingDirectory: () => "/test/project",
|
|
@@ -30,7 +49,16 @@ mock.module("swarm-mail", () => ({
|
|
|
30
49
|
}),
|
|
31
50
|
}));
|
|
32
51
|
|
|
52
|
+
// Mock logger module
|
|
53
|
+
mock.module("./logger", () => ({
|
|
54
|
+
createChildLogger: () => createMockLogger(),
|
|
55
|
+
}));
|
|
56
|
+
|
|
33
57
|
describe("Compaction Hook", () => {
|
|
58
|
+
beforeEach(() => {
|
|
59
|
+
// Reset log calls before each test
|
|
60
|
+
logCalls = [];
|
|
61
|
+
});
|
|
34
62
|
describe("SWARM_COMPACTION_CONTEXT", () => {
|
|
35
63
|
it("contains coordinator instructions", () => {
|
|
36
64
|
expect(SWARM_COMPACTION_CONTEXT).toContain("COORDINATOR");
|
|
@@ -107,4 +135,592 @@ describe("Compaction Hook", () => {
|
|
|
107
135
|
expect(SWARM_DETECTION_FALLBACK).toContain("Check Your Context");
|
|
108
136
|
});
|
|
109
137
|
});
|
|
138
|
+
|
|
139
|
+
describe("Specific swarm state injection (TDD red phase)", () => {
|
|
140
|
+
it("includes specific epic ID when in_progress epic exists", async () => {
|
|
141
|
+
// Mock hive with an in_progress epic
|
|
142
|
+
mock.module("./hive", () => ({
|
|
143
|
+
getHiveWorkingDirectory: () => "/test/project",
|
|
144
|
+
getHiveAdapter: async () => ({
|
|
145
|
+
queryCells: async () => [
|
|
146
|
+
{
|
|
147
|
+
id: "bd-epic-123",
|
|
148
|
+
title: "Add authentication system",
|
|
149
|
+
type: "epic",
|
|
150
|
+
status: "in_progress",
|
|
151
|
+
parent_id: null,
|
|
152
|
+
updated_at: Date.now(),
|
|
153
|
+
},
|
|
154
|
+
],
|
|
155
|
+
}),
|
|
156
|
+
}));
|
|
157
|
+
|
|
158
|
+
const hook = createCompactionHook();
|
|
159
|
+
const input = { sessionID: "test-session" };
|
|
160
|
+
const output = { context: [] as string[] };
|
|
161
|
+
|
|
162
|
+
await hook(input, output);
|
|
163
|
+
|
|
164
|
+
// Should inject context with the SPECIFIC epic ID, not a placeholder
|
|
165
|
+
expect(output.context.length).toBeGreaterThan(0);
|
|
166
|
+
const injectedContext = output.context[0];
|
|
167
|
+
expect(injectedContext).toContain("bd-epic-123");
|
|
168
|
+
expect(injectedContext).toContain("Add authentication system");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("includes subtask status summary in injected context", async () => {
|
|
172
|
+
// Mock hive with epic + subtasks in various states
|
|
173
|
+
mock.module("./hive", () => ({
|
|
174
|
+
getHiveWorkingDirectory: () => "/test/project",
|
|
175
|
+
getHiveAdapter: async () => ({
|
|
176
|
+
queryCells: async () => [
|
|
177
|
+
{
|
|
178
|
+
id: "bd-epic-456",
|
|
179
|
+
title: "Refactor auth flow",
|
|
180
|
+
type: "epic",
|
|
181
|
+
status: "in_progress",
|
|
182
|
+
parent_id: null,
|
|
183
|
+
updated_at: Date.now(),
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
id: "bd-epic-456.1",
|
|
187
|
+
title: "Update schema",
|
|
188
|
+
type: "task",
|
|
189
|
+
status: "closed",
|
|
190
|
+
parent_id: "bd-epic-456",
|
|
191
|
+
updated_at: Date.now(),
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
id: "bd-epic-456.2",
|
|
195
|
+
title: "Implement service layer",
|
|
196
|
+
type: "task",
|
|
197
|
+
status: "in_progress",
|
|
198
|
+
parent_id: "bd-epic-456",
|
|
199
|
+
updated_at: Date.now(),
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
id: "bd-epic-456.3",
|
|
203
|
+
title: "Add tests",
|
|
204
|
+
type: "task",
|
|
205
|
+
status: "open",
|
|
206
|
+
parent_id: "bd-epic-456",
|
|
207
|
+
updated_at: Date.now(),
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
}),
|
|
211
|
+
}));
|
|
212
|
+
|
|
213
|
+
const hook = createCompactionHook();
|
|
214
|
+
const input = { sessionID: "test-session" };
|
|
215
|
+
const output = { context: [] as string[] };
|
|
216
|
+
|
|
217
|
+
await hook(input, output);
|
|
218
|
+
|
|
219
|
+
expect(output.context.length).toBeGreaterThan(0);
|
|
220
|
+
const injectedContext = output.context[0];
|
|
221
|
+
|
|
222
|
+
// Should show subtask counts: 1 closed, 1 in_progress, 1 open
|
|
223
|
+
expect(injectedContext).toMatch(/1.*closed/i);
|
|
224
|
+
expect(injectedContext).toMatch(/1.*in_progress/i);
|
|
225
|
+
expect(injectedContext).toMatch(/1.*open/i);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it("includes project path in context", async () => {
|
|
229
|
+
mock.module("./hive", () => ({
|
|
230
|
+
getHiveWorkingDirectory: () => "/Users/joel/test-project",
|
|
231
|
+
getHiveAdapter: async () => ({
|
|
232
|
+
queryCells: async () => [
|
|
233
|
+
{
|
|
234
|
+
id: "bd-epic-789",
|
|
235
|
+
title: "Feature work",
|
|
236
|
+
type: "epic",
|
|
237
|
+
status: "in_progress",
|
|
238
|
+
parent_id: null,
|
|
239
|
+
updated_at: Date.now(),
|
|
240
|
+
},
|
|
241
|
+
],
|
|
242
|
+
}),
|
|
243
|
+
}));
|
|
244
|
+
|
|
245
|
+
const hook = createCompactionHook();
|
|
246
|
+
const input = { sessionID: "test-session" };
|
|
247
|
+
const output = { context: [] as string[] };
|
|
248
|
+
|
|
249
|
+
await hook(input, output);
|
|
250
|
+
|
|
251
|
+
expect(output.context.length).toBeGreaterThan(0);
|
|
252
|
+
const injectedContext = output.context[0];
|
|
253
|
+
|
|
254
|
+
// Should include the actual project path, not a placeholder
|
|
255
|
+
expect(injectedContext).toContain("/Users/joel/test-project");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("includes actionable swarm_status call with epic ID", async () => {
|
|
259
|
+
mock.module("./hive", () => ({
|
|
260
|
+
getHiveWorkingDirectory: () => "/test/project",
|
|
261
|
+
getHiveAdapter: async () => ({
|
|
262
|
+
queryCells: async () => [
|
|
263
|
+
{
|
|
264
|
+
id: "bd-epic-999",
|
|
265
|
+
title: "Critical fix",
|
|
266
|
+
type: "epic",
|
|
267
|
+
status: "in_progress",
|
|
268
|
+
parent_id: null,
|
|
269
|
+
updated_at: Date.now(),
|
|
270
|
+
},
|
|
271
|
+
],
|
|
272
|
+
}),
|
|
273
|
+
}));
|
|
274
|
+
|
|
275
|
+
const hook = createCompactionHook();
|
|
276
|
+
const input = { sessionID: "test-session" };
|
|
277
|
+
const output = { context: [] as string[] };
|
|
278
|
+
|
|
279
|
+
await hook(input, output);
|
|
280
|
+
|
|
281
|
+
expect(output.context.length).toBeGreaterThan(0);
|
|
282
|
+
const injectedContext = output.context[0];
|
|
283
|
+
|
|
284
|
+
// Should include actionable swarm_status call with the SPECIFIC epic ID
|
|
285
|
+
expect(injectedContext).toContain('swarm_status(epic_id="bd-epic-999"');
|
|
286
|
+
expect(injectedContext).toContain('project_key="/test/project"');
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it("includes coordinator role reminder - NOT worker commands", async () => {
|
|
290
|
+
// Mock an in-progress epic with subtasks
|
|
291
|
+
mock.module("./hive", () => ({
|
|
292
|
+
getHiveWorkingDirectory: () => "/test/project",
|
|
293
|
+
getHiveAdapter: async () => ({
|
|
294
|
+
queryCells: async () => [
|
|
295
|
+
{
|
|
296
|
+
id: "bd-epic-123",
|
|
297
|
+
title: "Test Epic",
|
|
298
|
+
type: "epic",
|
|
299
|
+
status: "in_progress",
|
|
300
|
+
parent_id: null,
|
|
301
|
+
updated_at: Date.now(),
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
id: "bd-task-1",
|
|
305
|
+
type: "task",
|
|
306
|
+
status: "open",
|
|
307
|
+
parent_id: "bd-epic-123",
|
|
308
|
+
updated_at: Date.now(),
|
|
309
|
+
},
|
|
310
|
+
],
|
|
311
|
+
}),
|
|
312
|
+
}));
|
|
313
|
+
|
|
314
|
+
const hook = createCompactionHook();
|
|
315
|
+
const input = { sessionID: "test-session" };
|
|
316
|
+
const output = { context: [] as string[] };
|
|
317
|
+
|
|
318
|
+
await hook(input, output);
|
|
319
|
+
|
|
320
|
+
expect(output.context.length).toBeGreaterThan(0);
|
|
321
|
+
const injectedContext = output.context[0];
|
|
322
|
+
|
|
323
|
+
// Should remind coordinator of their ROLE
|
|
324
|
+
expect(injectedContext).toContain("YOU ARE THE COORDINATOR");
|
|
325
|
+
expect(injectedContext).toContain("Spawn workers");
|
|
326
|
+
|
|
327
|
+
// Should include coordinator commands
|
|
328
|
+
expect(injectedContext).toContain("swarm_spawn_subtask");
|
|
329
|
+
expect(injectedContext).toContain("swarm_review");
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
describe("scanSessionMessages (SDK client integration)", () => {
|
|
334
|
+
/**
|
|
335
|
+
* These tests verify that we can extract swarm state from session messages
|
|
336
|
+
* using the OpenCode SDK client.
|
|
337
|
+
*
|
|
338
|
+
* Key types from @opencode-ai/sdk:
|
|
339
|
+
* - ToolPart: { type: "tool", tool: string, state: ToolState, ... }
|
|
340
|
+
* - ToolStateCompleted: { status: "completed", input: Record<string, unknown>, output: string, ... }
|
|
341
|
+
* - Part: union type including ToolPart
|
|
342
|
+
* - Message: { id, sessionID, role, ... }
|
|
343
|
+
*
|
|
344
|
+
* SDK API:
|
|
345
|
+
* - client.session.messages({ sessionID, limit }) → { info: Message, parts: Part[] }[]
|
|
346
|
+
*/
|
|
347
|
+
|
|
348
|
+
// Mock SDK client factory
|
|
349
|
+
const createMockClient = (messages: Array<{ info: any; parts: any[] }>) => ({
|
|
350
|
+
session: {
|
|
351
|
+
messages: async ({ sessionID, limit }: { sessionID: string; limit?: number }) => ({
|
|
352
|
+
data: messages,
|
|
353
|
+
error: undefined,
|
|
354
|
+
}),
|
|
355
|
+
},
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it("extracts epic ID from swarm_spawn_subtask tool calls", async () => {
|
|
359
|
+
const mockMessages = [
|
|
360
|
+
{
|
|
361
|
+
info: { id: "msg-1", sessionID: "sess-1", role: "assistant" },
|
|
362
|
+
parts: [
|
|
363
|
+
{
|
|
364
|
+
id: "part-1",
|
|
365
|
+
type: "tool",
|
|
366
|
+
tool: "swarm_spawn_subtask",
|
|
367
|
+
state: {
|
|
368
|
+
status: "completed",
|
|
369
|
+
input: {
|
|
370
|
+
epic_id: "bd-epic-auth-123",
|
|
371
|
+
subtask_title: "Implement auth service",
|
|
372
|
+
files: ["src/auth/service.ts"],
|
|
373
|
+
},
|
|
374
|
+
output: JSON.stringify({ success: true, agent_name: "BlueLake" }),
|
|
375
|
+
title: "Spawned subtask",
|
|
376
|
+
metadata: {},
|
|
377
|
+
time: { start: 1000, end: 2000 },
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
],
|
|
381
|
+
},
|
|
382
|
+
];
|
|
383
|
+
|
|
384
|
+
const client = createMockClient(mockMessages);
|
|
385
|
+
|
|
386
|
+
// This function doesn't exist yet - TDD Red phase
|
|
387
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
388
|
+
const result = await scanSessionMessages(client as any, "sess-1");
|
|
389
|
+
|
|
390
|
+
expect(result.epicId).toBe("bd-epic-auth-123");
|
|
391
|
+
expect(result.subtasks.size).toBeGreaterThan(0);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("extracts agent name from swarmmail_init tool calls", async () => {
|
|
395
|
+
const mockMessages = [
|
|
396
|
+
{
|
|
397
|
+
info: { id: "msg-1", sessionID: "sess-1", role: "assistant" },
|
|
398
|
+
parts: [
|
|
399
|
+
{
|
|
400
|
+
id: "part-1",
|
|
401
|
+
type: "tool",
|
|
402
|
+
tool: "swarmmail_init",
|
|
403
|
+
state: {
|
|
404
|
+
status: "completed",
|
|
405
|
+
input: {
|
|
406
|
+
project_path: "/Users/joel/project",
|
|
407
|
+
task_description: "Working on auth",
|
|
408
|
+
},
|
|
409
|
+
output: JSON.stringify({
|
|
410
|
+
agent_name: "DarkWind",
|
|
411
|
+
project_key: "/Users/joel/project",
|
|
412
|
+
}),
|
|
413
|
+
title: "Initialized swarm mail",
|
|
414
|
+
metadata: {},
|
|
415
|
+
time: { start: 1000, end: 2000 },
|
|
416
|
+
},
|
|
417
|
+
},
|
|
418
|
+
],
|
|
419
|
+
},
|
|
420
|
+
];
|
|
421
|
+
|
|
422
|
+
const client = createMockClient(mockMessages);
|
|
423
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
424
|
+
const result = await scanSessionMessages(client as any, "sess-1");
|
|
425
|
+
|
|
426
|
+
expect(result.agentName).toBe("DarkWind");
|
|
427
|
+
expect(result.projectPath).toBe("/Users/joel/project");
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it("extracts epic title from hive_create_epic tool calls", async () => {
|
|
431
|
+
const mockMessages = [
|
|
432
|
+
{
|
|
433
|
+
info: { id: "msg-1", sessionID: "sess-1", role: "assistant" },
|
|
434
|
+
parts: [
|
|
435
|
+
{
|
|
436
|
+
id: "part-1",
|
|
437
|
+
type: "tool",
|
|
438
|
+
tool: "hive_create_epic",
|
|
439
|
+
state: {
|
|
440
|
+
status: "completed",
|
|
441
|
+
input: {
|
|
442
|
+
epic_title: "Add OAuth authentication",
|
|
443
|
+
epic_description: "Implement OAuth2 flow",
|
|
444
|
+
subtasks: [
|
|
445
|
+
{ title: "Schema", files: ["src/schema.ts"] },
|
|
446
|
+
{ title: "Service", files: ["src/service.ts"] },
|
|
447
|
+
],
|
|
448
|
+
},
|
|
449
|
+
output: JSON.stringify({
|
|
450
|
+
epic_id: "bd-epic-oauth-456",
|
|
451
|
+
subtask_ids: ["bd-epic-oauth-456.1", "bd-epic-oauth-456.2"],
|
|
452
|
+
}),
|
|
453
|
+
title: "Created epic",
|
|
454
|
+
metadata: {},
|
|
455
|
+
time: { start: 1000, end: 2000 },
|
|
456
|
+
},
|
|
457
|
+
},
|
|
458
|
+
],
|
|
459
|
+
},
|
|
460
|
+
];
|
|
461
|
+
|
|
462
|
+
const client = createMockClient(mockMessages);
|
|
463
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
464
|
+
const result = await scanSessionMessages(client as any, "sess-1");
|
|
465
|
+
|
|
466
|
+
expect(result.epicId).toBe("bd-epic-oauth-456");
|
|
467
|
+
expect(result.epicTitle).toBe("Add OAuth authentication");
|
|
468
|
+
expect(result.subtasks.size).toBe(2);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it("tracks last action with timestamp", async () => {
|
|
472
|
+
const mockMessages = [
|
|
473
|
+
{
|
|
474
|
+
info: { id: "msg-1", sessionID: "sess-1", role: "assistant" },
|
|
475
|
+
parts: [
|
|
476
|
+
{
|
|
477
|
+
id: "part-1",
|
|
478
|
+
type: "tool",
|
|
479
|
+
tool: "swarm_status",
|
|
480
|
+
state: {
|
|
481
|
+
status: "completed",
|
|
482
|
+
input: { epic_id: "bd-epic-123", project_key: "/path" },
|
|
483
|
+
output: JSON.stringify({ status: "in_progress" }),
|
|
484
|
+
title: "Checked status",
|
|
485
|
+
metadata: {},
|
|
486
|
+
time: { start: 5000, end: 6000 },
|
|
487
|
+
},
|
|
488
|
+
},
|
|
489
|
+
],
|
|
490
|
+
},
|
|
491
|
+
];
|
|
492
|
+
|
|
493
|
+
const client = createMockClient(mockMessages);
|
|
494
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
495
|
+
const result = await scanSessionMessages(client as any, "sess-1");
|
|
496
|
+
|
|
497
|
+
expect(result.lastAction).toBeDefined();
|
|
498
|
+
expect(result.lastAction?.tool).toBe("swarm_status");
|
|
499
|
+
expect(result.lastAction?.timestamp).toBe(6000);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
it("ignores non-tool parts", async () => {
|
|
503
|
+
const mockMessages = [
|
|
504
|
+
{
|
|
505
|
+
info: { id: "msg-1", sessionID: "sess-1", role: "assistant" },
|
|
506
|
+
parts: [
|
|
507
|
+
{ id: "part-1", type: "text", text: "Hello" },
|
|
508
|
+
{ id: "part-2", type: "reasoning", text: "Thinking..." },
|
|
509
|
+
],
|
|
510
|
+
},
|
|
511
|
+
];
|
|
512
|
+
|
|
513
|
+
const client = createMockClient(mockMessages);
|
|
514
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
515
|
+
const result = await scanSessionMessages(client as any, "sess-1");
|
|
516
|
+
|
|
517
|
+
expect(result.epicId).toBeUndefined();
|
|
518
|
+
expect(result.subtasks.size).toBe(0);
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
it("ignores pending/running tool states", async () => {
|
|
522
|
+
const mockMessages = [
|
|
523
|
+
{
|
|
524
|
+
info: { id: "msg-1", sessionID: "sess-1", role: "assistant" },
|
|
525
|
+
parts: [
|
|
526
|
+
{
|
|
527
|
+
id: "part-1",
|
|
528
|
+
type: "tool",
|
|
529
|
+
tool: "swarm_spawn_subtask",
|
|
530
|
+
state: {
|
|
531
|
+
status: "pending",
|
|
532
|
+
input: { epic_id: "bd-epic-123" },
|
|
533
|
+
raw: "{}",
|
|
534
|
+
},
|
|
535
|
+
},
|
|
536
|
+
{
|
|
537
|
+
id: "part-2",
|
|
538
|
+
type: "tool",
|
|
539
|
+
tool: "swarm_status",
|
|
540
|
+
state: {
|
|
541
|
+
status: "running",
|
|
542
|
+
input: { epic_id: "bd-epic-456" },
|
|
543
|
+
time: { start: 1000 },
|
|
544
|
+
},
|
|
545
|
+
},
|
|
546
|
+
],
|
|
547
|
+
},
|
|
548
|
+
];
|
|
549
|
+
|
|
550
|
+
const client = createMockClient(mockMessages);
|
|
551
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
552
|
+
const result = await scanSessionMessages(client as any, "sess-1");
|
|
553
|
+
|
|
554
|
+
// Should not extract from pending/running states
|
|
555
|
+
expect(result.epicId).toBeUndefined();
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it("handles SDK client errors gracefully", async () => {
|
|
559
|
+
const errorClient = {
|
|
560
|
+
session: {
|
|
561
|
+
messages: async () => ({
|
|
562
|
+
data: undefined,
|
|
563
|
+
error: { message: "Network error" },
|
|
564
|
+
}),
|
|
565
|
+
},
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
569
|
+
const result = await scanSessionMessages(errorClient as any, "sess-1");
|
|
570
|
+
|
|
571
|
+
// Should return empty state, not throw
|
|
572
|
+
expect(result.subtasks.size).toBe(0);
|
|
573
|
+
expect(result.epicId).toBeUndefined();
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
it("returns empty state when client is undefined", async () => {
|
|
577
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
578
|
+
const result = await scanSessionMessages(undefined as any, "sess-1");
|
|
579
|
+
|
|
580
|
+
expect(result.subtasks.size).toBe(0);
|
|
581
|
+
});
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
describe("Logging instrumentation", () => {
|
|
585
|
+
it("logs compaction start with session_id", async () => {
|
|
586
|
+
const hook = createCompactionHook();
|
|
587
|
+
const input = { sessionID: "test-session-123" };
|
|
588
|
+
const output = { context: [] as string[] };
|
|
589
|
+
|
|
590
|
+
await hook(input, output);
|
|
591
|
+
|
|
592
|
+
const startLog = logCalls.find(
|
|
593
|
+
(log) => log.level === "info" && log.message === "compaction started",
|
|
594
|
+
);
|
|
595
|
+
expect(startLog).toBeDefined();
|
|
596
|
+
expect(startLog?.data).toHaveProperty("session_id", "test-session-123");
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
it("logs detection phase with confidence and reasons", async () => {
|
|
600
|
+
const hook = createCompactionHook();
|
|
601
|
+
const input = { sessionID: "test-session" };
|
|
602
|
+
const output = { context: [] as string[] };
|
|
603
|
+
|
|
604
|
+
await hook(input, output);
|
|
605
|
+
|
|
606
|
+
const detectionLog = logCalls.find(
|
|
607
|
+
(log) =>
|
|
608
|
+
log.level === "debug" && log.message === "swarm detection complete",
|
|
609
|
+
);
|
|
610
|
+
expect(detectionLog).toBeDefined();
|
|
611
|
+
expect(detectionLog?.data).toHaveProperty("confidence");
|
|
612
|
+
expect(detectionLog?.data).toHaveProperty("detected");
|
|
613
|
+
expect(detectionLog?.data).toHaveProperty("reason_count");
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
it("logs context injection when swarm detected", async () => {
|
|
617
|
+
const hook = createCompactionHook();
|
|
618
|
+
const input = { sessionID: "test-session" };
|
|
619
|
+
const output = { context: [] as string[] };
|
|
620
|
+
|
|
621
|
+
// Mock detection to return medium confidence
|
|
622
|
+
mock.module("./hive", () => ({
|
|
623
|
+
getHiveWorkingDirectory: () => "/test/project",
|
|
624
|
+
getHiveAdapter: async () => ({
|
|
625
|
+
queryCells: async () => [
|
|
626
|
+
{
|
|
627
|
+
id: "bd-123",
|
|
628
|
+
type: "epic",
|
|
629
|
+
status: "open",
|
|
630
|
+
parent_id: null,
|
|
631
|
+
updated_at: Date.now(),
|
|
632
|
+
},
|
|
633
|
+
],
|
|
634
|
+
}),
|
|
635
|
+
}));
|
|
636
|
+
|
|
637
|
+
await hook(input, output);
|
|
638
|
+
|
|
639
|
+
const injectionLog = logCalls.find(
|
|
640
|
+
(log) =>
|
|
641
|
+
log.level === "info" && log.message === "injected swarm context",
|
|
642
|
+
);
|
|
643
|
+
expect(injectionLog).toBeDefined();
|
|
644
|
+
expect(injectionLog?.data).toHaveProperty("confidence");
|
|
645
|
+
expect(injectionLog?.data).toHaveProperty("context_length");
|
|
646
|
+
});
|
|
647
|
+
|
|
648
|
+
it("logs completion with duration and success", async () => {
|
|
649
|
+
const hook = createCompactionHook();
|
|
650
|
+
const input = { sessionID: "test-session" };
|
|
651
|
+
const output = { context: [] as string[] };
|
|
652
|
+
|
|
653
|
+
await hook(input, output);
|
|
654
|
+
|
|
655
|
+
const completeLog = logCalls.find(
|
|
656
|
+
(log) => log.level === "info" && log.message === "compaction complete",
|
|
657
|
+
);
|
|
658
|
+
expect(completeLog).toBeDefined();
|
|
659
|
+
expect(completeLog?.data).toHaveProperty("duration_ms");
|
|
660
|
+
expect(completeLog?.data.duration_ms).toBeGreaterThanOrEqual(0);
|
|
661
|
+
expect(completeLog?.data).toHaveProperty("success", true);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
it("logs detailed detection sources (hive, swarm-mail)", async () => {
|
|
665
|
+
const hook = createCompactionHook();
|
|
666
|
+
const input = { sessionID: "test-session" };
|
|
667
|
+
const output = { context: [] as string[] };
|
|
668
|
+
|
|
669
|
+
await hook(input, output);
|
|
670
|
+
|
|
671
|
+
// Should log details about checking swarm-mail
|
|
672
|
+
const swarmMailLog = logCalls.find(
|
|
673
|
+
(log) => log.level === "debug" && log.message?.includes("swarm-mail"),
|
|
674
|
+
);
|
|
675
|
+
// Should log details about checking hive
|
|
676
|
+
const hiveLog = logCalls.find(
|
|
677
|
+
(log) => log.level === "debug" && log.message?.includes("hive"),
|
|
678
|
+
);
|
|
679
|
+
|
|
680
|
+
// At least one source should be checked
|
|
681
|
+
expect(logCalls.length).toBeGreaterThan(0);
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
it("logs errors without throwing when detection fails", async () => {
|
|
685
|
+
// Mock hive to throw
|
|
686
|
+
mock.module("./hive", () => ({
|
|
687
|
+
getHiveWorkingDirectory: () => {
|
|
688
|
+
throw new Error("Hive not available");
|
|
689
|
+
},
|
|
690
|
+
getHiveAdapter: async () => {
|
|
691
|
+
throw new Error("Hive not available");
|
|
692
|
+
},
|
|
693
|
+
}));
|
|
694
|
+
|
|
695
|
+
const hook = createCompactionHook();
|
|
696
|
+
const input = { sessionID: "test-session" };
|
|
697
|
+
const output = { context: [] as string[] };
|
|
698
|
+
|
|
699
|
+
// Should not throw
|
|
700
|
+
await expect(hook(input, output)).resolves.toBeUndefined();
|
|
701
|
+
|
|
702
|
+
// Should still complete successfully
|
|
703
|
+
const completeLog = logCalls.find(
|
|
704
|
+
(log) => log.level === "info" && log.message === "compaction complete",
|
|
705
|
+
);
|
|
706
|
+
expect(completeLog).toBeDefined();
|
|
707
|
+
});
|
|
708
|
+
|
|
709
|
+
it("includes context size when injecting", async () => {
|
|
710
|
+
const hook = createCompactionHook();
|
|
711
|
+
const input = { sessionID: "test-session" };
|
|
712
|
+
const output = { context: [] as string[] };
|
|
713
|
+
|
|
714
|
+
await hook(input, output);
|
|
715
|
+
|
|
716
|
+
// If context was injected, should log the size
|
|
717
|
+
if (output.context.length > 0) {
|
|
718
|
+
const injectionLog = logCalls.find(
|
|
719
|
+
(log) =>
|
|
720
|
+
log.level === "info" && log.message === "injected swarm context",
|
|
721
|
+
);
|
|
722
|
+
expect(injectionLog?.data.context_length).toBeGreaterThan(0);
|
|
723
|
+
}
|
|
724
|
+
});
|
|
725
|
+
});
|
|
110
726
|
});
|