opencode-swarm-plugin 0.33.0 → 0.35.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 +4 -4
- package/.turbo/turbo-test.log +289 -289
- package/CHANGELOG.md +133 -0
- package/README.md +29 -1
- package/bin/swarm.test.ts +342 -1
- package/bin/swarm.ts +351 -4
- package/dist/compaction-hook.d.ts +1 -1
- package/dist/compaction-hook.d.ts.map +1 -1
- package/dist/index.d.ts +95 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11848 -124
- package/dist/logger.d.ts +34 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/plugin.js +11722 -112
- package/dist/swarm-orchestrate.d.ts +105 -0
- package/dist/swarm-orchestrate.d.ts.map +1 -1
- package/dist/swarm-prompts.d.ts +54 -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 +56 -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/package.json +5 -2
- package/src/compaction-hook.test.ts +639 -1
- package/src/compaction-hook.ts +488 -18
- package/src/index.ts +29 -0
- package/src/logger.test.ts +189 -0
- package/src/logger.ts +135 -0
- package/src/swarm-decompose.ts +0 -7
- package/src/swarm-prompts.test.ts +164 -1
- package/src/swarm-prompts.ts +179 -12
- package/src/swarm-review.test.ts +177 -0
- package/src/swarm-review.ts +12 -47
|
@@ -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,614 @@ 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", () => {
|
|
334
|
+
it("returns empty state when client is undefined", async () => {
|
|
335
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
336
|
+
|
|
337
|
+
const result = await scanSessionMessages(undefined, "session-123");
|
|
338
|
+
|
|
339
|
+
expect(result.epicId).toBeUndefined();
|
|
340
|
+
expect(result.epicTitle).toBeUndefined();
|
|
341
|
+
expect(result.projectPath).toBeUndefined();
|
|
342
|
+
expect(result.agentName).toBeUndefined();
|
|
343
|
+
expect(result.subtasks.size).toBe(0);
|
|
344
|
+
expect(result.lastAction).toBeUndefined();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
it("extracts epic data from hive_create_epic tool call", async () => {
|
|
348
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
349
|
+
|
|
350
|
+
// Mock SDK client
|
|
351
|
+
const mockClient = {
|
|
352
|
+
session: {
|
|
353
|
+
messages: async ({ sessionID, limit }: { sessionID: string; limit?: number }) => {
|
|
354
|
+
return [
|
|
355
|
+
{
|
|
356
|
+
info: { id: "msg-1", sessionID: "session-123" },
|
|
357
|
+
parts: [
|
|
358
|
+
{
|
|
359
|
+
id: "part-1",
|
|
360
|
+
sessionID: "session-123",
|
|
361
|
+
messageID: "msg-1",
|
|
362
|
+
type: "tool" as const,
|
|
363
|
+
callID: "call-1",
|
|
364
|
+
tool: "hive_create_epic",
|
|
365
|
+
state: {
|
|
366
|
+
status: "completed" as const,
|
|
367
|
+
input: {
|
|
368
|
+
epic_title: "Add authentication system",
|
|
369
|
+
epic_description: "Implement OAuth flow",
|
|
370
|
+
},
|
|
371
|
+
output: JSON.stringify({
|
|
372
|
+
success: true,
|
|
373
|
+
epic: { id: "bd-epic-123" },
|
|
374
|
+
}),
|
|
375
|
+
title: "Create Epic",
|
|
376
|
+
metadata: {},
|
|
377
|
+
time: { start: Date.now(), end: Date.now() },
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
],
|
|
381
|
+
},
|
|
382
|
+
];
|
|
383
|
+
},
|
|
384
|
+
},
|
|
385
|
+
} as any;
|
|
386
|
+
|
|
387
|
+
const result = await scanSessionMessages(mockClient, "session-123");
|
|
388
|
+
|
|
389
|
+
expect(result.epicId).toBe("bd-epic-123");
|
|
390
|
+
expect(result.epicTitle).toBe("Add authentication system");
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("extracts agent name from swarmmail_init tool call", async () => {
|
|
394
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
395
|
+
|
|
396
|
+
const mockClient = {
|
|
397
|
+
session: {
|
|
398
|
+
messages: async () => [
|
|
399
|
+
{
|
|
400
|
+
info: { id: "msg-1", sessionID: "session-123" },
|
|
401
|
+
parts: [
|
|
402
|
+
{
|
|
403
|
+
id: "part-1",
|
|
404
|
+
sessionID: "session-123",
|
|
405
|
+
messageID: "msg-1",
|
|
406
|
+
type: "tool" as const,
|
|
407
|
+
callID: "call-1",
|
|
408
|
+
tool: "swarmmail_init",
|
|
409
|
+
state: {
|
|
410
|
+
status: "completed" as const,
|
|
411
|
+
input: {
|
|
412
|
+
project_path: "/test/project",
|
|
413
|
+
task_description: "Working on auth",
|
|
414
|
+
},
|
|
415
|
+
output: JSON.stringify({
|
|
416
|
+
agent_name: "BlueLake",
|
|
417
|
+
project_key: "/test/project",
|
|
418
|
+
}),
|
|
419
|
+
title: "Init Swarm Mail",
|
|
420
|
+
metadata: {},
|
|
421
|
+
time: { start: Date.now(), end: Date.now() },
|
|
422
|
+
},
|
|
423
|
+
},
|
|
424
|
+
],
|
|
425
|
+
},
|
|
426
|
+
],
|
|
427
|
+
},
|
|
428
|
+
} as any;
|
|
429
|
+
|
|
430
|
+
const result = await scanSessionMessages(mockClient, "session-123");
|
|
431
|
+
|
|
432
|
+
expect(result.agentName).toBe("BlueLake");
|
|
433
|
+
expect(result.projectPath).toBe("/test/project");
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
it("extracts subtask data from swarm_spawn_subtask tool call", async () => {
|
|
437
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
438
|
+
|
|
439
|
+
const mockClient = {
|
|
440
|
+
session: {
|
|
441
|
+
messages: async () => [
|
|
442
|
+
{
|
|
443
|
+
info: { id: "msg-1", sessionID: "session-123" },
|
|
444
|
+
parts: [
|
|
445
|
+
{
|
|
446
|
+
id: "part-1",
|
|
447
|
+
sessionID: "session-123",
|
|
448
|
+
messageID: "msg-1",
|
|
449
|
+
type: "tool" as const,
|
|
450
|
+
callID: "call-1",
|
|
451
|
+
tool: "swarm_spawn_subtask",
|
|
452
|
+
state: {
|
|
453
|
+
status: "completed" as const,
|
|
454
|
+
input: {
|
|
455
|
+
bead_id: "bd-task-1",
|
|
456
|
+
epic_id: "bd-epic-123",
|
|
457
|
+
subtask_title: "Implement OAuth service",
|
|
458
|
+
files: ["src/auth/oauth.ts"],
|
|
459
|
+
},
|
|
460
|
+
output: JSON.stringify({
|
|
461
|
+
worker: "RedMountain",
|
|
462
|
+
bead_id: "bd-task-1",
|
|
463
|
+
}),
|
|
464
|
+
title: "Spawn Subtask",
|
|
465
|
+
metadata: {},
|
|
466
|
+
time: { start: Date.now(), end: Date.now() },
|
|
467
|
+
},
|
|
468
|
+
},
|
|
469
|
+
],
|
|
470
|
+
},
|
|
471
|
+
],
|
|
472
|
+
},
|
|
473
|
+
} as any;
|
|
474
|
+
|
|
475
|
+
const result = await scanSessionMessages(mockClient, "session-123");
|
|
476
|
+
|
|
477
|
+
expect(result.subtasks.size).toBe(1);
|
|
478
|
+
expect(result.subtasks.get("bd-task-1")).toEqual({
|
|
479
|
+
title: "Implement OAuth service",
|
|
480
|
+
status: "spawned",
|
|
481
|
+
worker: "RedMountain",
|
|
482
|
+
files: ["src/auth/oauth.ts"],
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
it("marks subtask as completed from swarm_complete tool call", async () => {
|
|
487
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
488
|
+
|
|
489
|
+
const mockClient = {
|
|
490
|
+
session: {
|
|
491
|
+
messages: async () => [
|
|
492
|
+
{
|
|
493
|
+
info: { id: "msg-1", sessionID: "session-123" },
|
|
494
|
+
parts: [
|
|
495
|
+
{
|
|
496
|
+
id: "part-1",
|
|
497
|
+
sessionID: "session-123",
|
|
498
|
+
messageID: "msg-1",
|
|
499
|
+
type: "tool" as const,
|
|
500
|
+
callID: "call-1",
|
|
501
|
+
tool: "swarm_spawn_subtask",
|
|
502
|
+
state: {
|
|
503
|
+
status: "completed" as const,
|
|
504
|
+
input: {
|
|
505
|
+
bead_id: "bd-task-1",
|
|
506
|
+
epic_id: "bd-epic-123",
|
|
507
|
+
subtask_title: "Fix bug",
|
|
508
|
+
files: [],
|
|
509
|
+
},
|
|
510
|
+
output: "{}",
|
|
511
|
+
title: "Spawn",
|
|
512
|
+
metadata: {},
|
|
513
|
+
time: { start: 100, end: 200 },
|
|
514
|
+
},
|
|
515
|
+
},
|
|
516
|
+
{
|
|
517
|
+
id: "part-2",
|
|
518
|
+
sessionID: "session-123",
|
|
519
|
+
messageID: "msg-1",
|
|
520
|
+
type: "tool" as const,
|
|
521
|
+
callID: "call-2",
|
|
522
|
+
tool: "swarm_complete",
|
|
523
|
+
state: {
|
|
524
|
+
status: "completed" as const,
|
|
525
|
+
input: {
|
|
526
|
+
bead_id: "bd-task-1",
|
|
527
|
+
summary: "Fixed the bug",
|
|
528
|
+
},
|
|
529
|
+
output: JSON.stringify({ success: true, closed: true }),
|
|
530
|
+
title: "Complete",
|
|
531
|
+
metadata: {},
|
|
532
|
+
time: { start: 300, end: 400 },
|
|
533
|
+
},
|
|
534
|
+
},
|
|
535
|
+
],
|
|
536
|
+
},
|
|
537
|
+
],
|
|
538
|
+
},
|
|
539
|
+
} as any;
|
|
540
|
+
|
|
541
|
+
const result = await scanSessionMessages(mockClient, "session-123");
|
|
542
|
+
|
|
543
|
+
expect(result.subtasks.get("bd-task-1")?.status).toBe("completed");
|
|
544
|
+
});
|
|
545
|
+
|
|
546
|
+
it("captures last action timestamp", async () => {
|
|
547
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
548
|
+
|
|
549
|
+
const mockClient = {
|
|
550
|
+
session: {
|
|
551
|
+
messages: async () => [
|
|
552
|
+
{
|
|
553
|
+
info: { id: "msg-1", sessionID: "session-123" },
|
|
554
|
+
parts: [
|
|
555
|
+
{
|
|
556
|
+
id: "part-1",
|
|
557
|
+
sessionID: "session-123",
|
|
558
|
+
messageID: "msg-1",
|
|
559
|
+
type: "tool" as const,
|
|
560
|
+
callID: "call-1",
|
|
561
|
+
tool: "swarm_status",
|
|
562
|
+
state: {
|
|
563
|
+
status: "completed" as const,
|
|
564
|
+
input: {
|
|
565
|
+
epic_id: "bd-epic-123",
|
|
566
|
+
project_key: "/test",
|
|
567
|
+
},
|
|
568
|
+
output: "{}",
|
|
569
|
+
title: "Check Status",
|
|
570
|
+
metadata: {},
|
|
571
|
+
time: { start: 1000, end: 2000 },
|
|
572
|
+
},
|
|
573
|
+
},
|
|
574
|
+
],
|
|
575
|
+
},
|
|
576
|
+
],
|
|
577
|
+
},
|
|
578
|
+
} as any;
|
|
579
|
+
|
|
580
|
+
const result = await scanSessionMessages(mockClient, "session-123");
|
|
581
|
+
|
|
582
|
+
expect(result.lastAction).toBeDefined();
|
|
583
|
+
expect(result.lastAction?.tool).toBe("swarm_status");
|
|
584
|
+
expect(result.lastAction?.timestamp).toBe(2000);
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
it("respects limit parameter", async () => {
|
|
588
|
+
const { scanSessionMessages } = await import("./compaction-hook");
|
|
589
|
+
|
|
590
|
+
let capturedLimit: number | undefined;
|
|
591
|
+
const mockClient = {
|
|
592
|
+
session: {
|
|
593
|
+
messages: async ({ limit }: { limit?: number }) => {
|
|
594
|
+
capturedLimit = limit;
|
|
595
|
+
return [];
|
|
596
|
+
},
|
|
597
|
+
},
|
|
598
|
+
} as any;
|
|
599
|
+
|
|
600
|
+
await scanSessionMessages(mockClient, "session-123", 50);
|
|
601
|
+
|
|
602
|
+
expect(capturedLimit).toBe(50);
|
|
603
|
+
});
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
describe("Logging instrumentation", () => {
|
|
607
|
+
it("logs compaction start with session_id", async () => {
|
|
608
|
+
const hook = createCompactionHook();
|
|
609
|
+
const input = { sessionID: "test-session-123" };
|
|
610
|
+
const output = { context: [] as string[] };
|
|
611
|
+
|
|
612
|
+
await hook(input, output);
|
|
613
|
+
|
|
614
|
+
const startLog = logCalls.find(
|
|
615
|
+
(log) => log.level === "info" && log.message === "compaction started",
|
|
616
|
+
);
|
|
617
|
+
expect(startLog).toBeDefined();
|
|
618
|
+
expect(startLog?.data).toHaveProperty("session_id", "test-session-123");
|
|
619
|
+
});
|
|
620
|
+
|
|
621
|
+
it("logs detection phase with confidence and reasons", async () => {
|
|
622
|
+
const hook = createCompactionHook();
|
|
623
|
+
const input = { sessionID: "test-session" };
|
|
624
|
+
const output = { context: [] as string[] };
|
|
625
|
+
|
|
626
|
+
await hook(input, output);
|
|
627
|
+
|
|
628
|
+
const detectionLog = logCalls.find(
|
|
629
|
+
(log) =>
|
|
630
|
+
log.level === "debug" && log.message === "swarm detection complete",
|
|
631
|
+
);
|
|
632
|
+
expect(detectionLog).toBeDefined();
|
|
633
|
+
expect(detectionLog?.data).toHaveProperty("confidence");
|
|
634
|
+
expect(detectionLog?.data).toHaveProperty("detected");
|
|
635
|
+
expect(detectionLog?.data).toHaveProperty("reason_count");
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
it("logs context injection when swarm detected", async () => {
|
|
639
|
+
const hook = createCompactionHook();
|
|
640
|
+
const input = { sessionID: "test-session" };
|
|
641
|
+
const output = { context: [] as string[] };
|
|
642
|
+
|
|
643
|
+
// Mock detection to return medium confidence
|
|
644
|
+
mock.module("./hive", () => ({
|
|
645
|
+
getHiveWorkingDirectory: () => "/test/project",
|
|
646
|
+
getHiveAdapter: async () => ({
|
|
647
|
+
queryCells: async () => [
|
|
648
|
+
{
|
|
649
|
+
id: "bd-123",
|
|
650
|
+
type: "epic",
|
|
651
|
+
status: "open",
|
|
652
|
+
parent_id: null,
|
|
653
|
+
updated_at: Date.now(),
|
|
654
|
+
},
|
|
655
|
+
],
|
|
656
|
+
}),
|
|
657
|
+
}));
|
|
658
|
+
|
|
659
|
+
await hook(input, output);
|
|
660
|
+
|
|
661
|
+
const injectionLog = logCalls.find(
|
|
662
|
+
(log) =>
|
|
663
|
+
log.level === "info" && log.message === "injected swarm context",
|
|
664
|
+
);
|
|
665
|
+
expect(injectionLog).toBeDefined();
|
|
666
|
+
expect(injectionLog?.data).toHaveProperty("confidence");
|
|
667
|
+
expect(injectionLog?.data).toHaveProperty("context_length");
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
it("logs completion with duration and success", async () => {
|
|
671
|
+
const hook = createCompactionHook();
|
|
672
|
+
const input = { sessionID: "test-session" };
|
|
673
|
+
const output = { context: [] as string[] };
|
|
674
|
+
|
|
675
|
+
await hook(input, output);
|
|
676
|
+
|
|
677
|
+
const completeLog = logCalls.find(
|
|
678
|
+
(log) => log.level === "info" && log.message === "compaction complete",
|
|
679
|
+
);
|
|
680
|
+
expect(completeLog).toBeDefined();
|
|
681
|
+
expect(completeLog?.data).toHaveProperty("duration_ms");
|
|
682
|
+
expect(completeLog?.data.duration_ms).toBeGreaterThanOrEqual(0);
|
|
683
|
+
expect(completeLog?.data).toHaveProperty("success", true);
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
it("logs detailed detection sources (hive, swarm-mail)", async () => {
|
|
687
|
+
const hook = createCompactionHook();
|
|
688
|
+
const input = { sessionID: "test-session" };
|
|
689
|
+
const output = { context: [] as string[] };
|
|
690
|
+
|
|
691
|
+
await hook(input, output);
|
|
692
|
+
|
|
693
|
+
// Should log details about checking swarm-mail
|
|
694
|
+
const swarmMailLog = logCalls.find(
|
|
695
|
+
(log) => log.level === "debug" && log.message?.includes("swarm-mail"),
|
|
696
|
+
);
|
|
697
|
+
// Should log details about checking hive
|
|
698
|
+
const hiveLog = logCalls.find(
|
|
699
|
+
(log) => log.level === "debug" && log.message?.includes("hive"),
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
// At least one source should be checked
|
|
703
|
+
expect(logCalls.length).toBeGreaterThan(0);
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
it("logs errors without throwing when detection fails", async () => {
|
|
707
|
+
// Mock hive to throw
|
|
708
|
+
mock.module("./hive", () => ({
|
|
709
|
+
getHiveWorkingDirectory: () => {
|
|
710
|
+
throw new Error("Hive not available");
|
|
711
|
+
},
|
|
712
|
+
getHiveAdapter: async () => {
|
|
713
|
+
throw new Error("Hive not available");
|
|
714
|
+
},
|
|
715
|
+
}));
|
|
716
|
+
|
|
717
|
+
const hook = createCompactionHook();
|
|
718
|
+
const input = { sessionID: "test-session" };
|
|
719
|
+
const output = { context: [] as string[] };
|
|
720
|
+
|
|
721
|
+
// Should not throw
|
|
722
|
+
await expect(hook(input, output)).resolves.toBeUndefined();
|
|
723
|
+
|
|
724
|
+
// Should still complete successfully
|
|
725
|
+
const completeLog = logCalls.find(
|
|
726
|
+
(log) => log.level === "info" && log.message === "compaction complete",
|
|
727
|
+
);
|
|
728
|
+
expect(completeLog).toBeDefined();
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
it("includes context size when injecting", async () => {
|
|
732
|
+
const hook = createCompactionHook();
|
|
733
|
+
const input = { sessionID: "test-session" };
|
|
734
|
+
const output = { context: [] as string[] };
|
|
735
|
+
|
|
736
|
+
await hook(input, output);
|
|
737
|
+
|
|
738
|
+
// If context was injected, should log the size
|
|
739
|
+
if (output.context.length > 0) {
|
|
740
|
+
const injectionLog = logCalls.find(
|
|
741
|
+
(log) =>
|
|
742
|
+
log.level === "info" && log.message === "injected swarm context",
|
|
743
|
+
);
|
|
744
|
+
expect(injectionLog?.data.context_length).toBeGreaterThan(0);
|
|
745
|
+
}
|
|
746
|
+
});
|
|
747
|
+
});
|
|
110
748
|
});
|