opencode-swarm-plugin 0.36.0 → 0.37.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 +16 -4
- package/.hive/memories.jsonl +274 -1
- package/.turbo/turbo-build.log +4 -4
- package/.turbo/turbo-test.log +318 -318
- package/CHANGELOG.md +113 -0
- package/bin/swarm.test.ts +106 -0
- package/bin/swarm.ts +413 -179
- package/dist/compaction-hook.d.ts +54 -4
- package/dist/compaction-hook.d.ts.map +1 -1
- package/dist/eval-capture.d.ts +122 -17
- package/dist/eval-capture.d.ts.map +1 -1
- package/dist/index.d.ts +1 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1278 -619
- package/dist/planning-guardrails.d.ts +121 -0
- package/dist/planning-guardrails.d.ts.map +1 -1
- package/dist/plugin.d.ts +9 -9
- package/dist/plugin.d.ts.map +1 -1
- package/dist/plugin.js +1283 -329
- package/dist/schemas/task.d.ts +0 -1
- package/dist/schemas/task.d.ts.map +1 -1
- package/dist/swarm-decompose.d.ts +0 -8
- package/dist/swarm-decompose.d.ts.map +1 -1
- package/dist/swarm-orchestrate.d.ts.map +1 -1
- package/dist/swarm-prompts.d.ts +0 -4
- package/dist/swarm-prompts.d.ts.map +1 -1
- package/dist/swarm-review.d.ts.map +1 -1
- package/dist/swarm.d.ts +0 -6
- package/dist/swarm.d.ts.map +1 -1
- package/evals/README.md +38 -0
- package/evals/coordinator-session.eval.ts +154 -0
- package/evals/fixtures/coordinator-sessions.ts +328 -0
- package/evals/lib/data-loader.ts +69 -0
- package/evals/scorers/coordinator-discipline.evalite-test.ts +536 -0
- package/evals/scorers/coordinator-discipline.ts +315 -0
- package/evals/scorers/index.ts +12 -0
- package/examples/plugin-wrapper-template.ts +303 -4
- package/package.json +2 -2
- package/src/compaction-hook.test.ts +8 -1
- package/src/compaction-hook.ts +31 -21
- package/src/eval-capture.test.ts +390 -0
- package/src/eval-capture.ts +163 -4
- package/src/hive.integration.test.ts +148 -0
- package/src/hive.ts +89 -0
- package/src/index.ts +68 -1
- package/src/planning-guardrails.test.ts +387 -2
- package/src/planning-guardrails.ts +289 -0
- package/src/plugin.ts +10 -10
- package/src/swarm-decompose.test.ts +195 -0
- package/src/swarm-decompose.ts +72 -1
- package/src/swarm-orchestrate.ts +44 -0
- package/src/swarm-prompts.ts +20 -0
- package/src/swarm-review.integration.test.ts +24 -29
- package/src/swarm-review.ts +41 -0
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
import { describe, it, expect } from "bun:test";
|
|
2
|
-
import {
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
analyzeTodoWrite,
|
|
4
|
+
shouldAnalyzeTool,
|
|
5
|
+
detectCoordinatorViolation,
|
|
6
|
+
setCoordinatorContext,
|
|
7
|
+
getCoordinatorContext,
|
|
8
|
+
clearCoordinatorContext,
|
|
9
|
+
isInCoordinatorContext,
|
|
10
|
+
type ViolationDetectionResult,
|
|
11
|
+
} from "./planning-guardrails";
|
|
12
|
+
import * as fs from "node:fs";
|
|
13
|
+
import * as path from "node:path";
|
|
14
|
+
import * as os from "os";
|
|
3
15
|
|
|
4
16
|
describe("planning-guardrails", () => {
|
|
5
17
|
describe("shouldAnalyzeTool", () => {
|
|
@@ -103,4 +115,377 @@ describe("planning-guardrails", () => {
|
|
|
103
115
|
expect(result.totalCount).toBe(6);
|
|
104
116
|
});
|
|
105
117
|
});
|
|
118
|
+
|
|
119
|
+
describe("detectCoordinatorViolation", () => {
|
|
120
|
+
const sessionId = "test-session-123";
|
|
121
|
+
const epicId = "test-epic-456";
|
|
122
|
+
|
|
123
|
+
// Clean up session files after tests
|
|
124
|
+
afterEach(() => {
|
|
125
|
+
const sessionDir = path.join(os.homedir(), ".config", "swarm-tools", "sessions");
|
|
126
|
+
const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
|
|
127
|
+
if (fs.existsSync(sessionPath)) {
|
|
128
|
+
fs.unlinkSync(sessionPath);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe("coordinator_edited_file violation", () => {
|
|
133
|
+
it("detects Edit tool call from coordinator", () => {
|
|
134
|
+
const result = detectCoordinatorViolation({
|
|
135
|
+
sessionId,
|
|
136
|
+
epicId,
|
|
137
|
+
toolName: "edit",
|
|
138
|
+
toolArgs: { filePath: "/path/to/file.ts", oldString: "old", newString: "new" },
|
|
139
|
+
agentContext: "coordinator",
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
expect(result.isViolation).toBe(true);
|
|
143
|
+
expect(result.violationType).toBe("coordinator_edited_file");
|
|
144
|
+
expect(result.message).toContain("Coordinators should spawn workers");
|
|
145
|
+
expect(result.payload.tool).toBe("edit");
|
|
146
|
+
expect(result.payload.file).toBe("/path/to/file.ts");
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("detects Write tool call from coordinator", () => {
|
|
150
|
+
const result = detectCoordinatorViolation({
|
|
151
|
+
sessionId,
|
|
152
|
+
epicId,
|
|
153
|
+
toolName: "write",
|
|
154
|
+
toolArgs: { filePath: "/path/to/new-file.ts", content: "export const foo = 1;" },
|
|
155
|
+
agentContext: "coordinator",
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
expect(result.isViolation).toBe(true);
|
|
159
|
+
expect(result.violationType).toBe("coordinator_edited_file");
|
|
160
|
+
expect(result.message).toContain("Coordinators should spawn workers");
|
|
161
|
+
expect(result.payload.tool).toBe("write");
|
|
162
|
+
expect(result.payload.file).toBe("/path/to/new-file.ts");
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("does not detect edit from worker agent", () => {
|
|
166
|
+
const result = detectCoordinatorViolation({
|
|
167
|
+
sessionId,
|
|
168
|
+
epicId,
|
|
169
|
+
toolName: "edit",
|
|
170
|
+
toolArgs: { filePath: "/path/to/file.ts", oldString: "old", newString: "new" },
|
|
171
|
+
agentContext: "worker",
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(result.isViolation).toBe(false);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("does not detect Read tool (read-only)", () => {
|
|
178
|
+
const result = detectCoordinatorViolation({
|
|
179
|
+
sessionId,
|
|
180
|
+
epicId,
|
|
181
|
+
toolName: "read",
|
|
182
|
+
toolArgs: { filePath: "/path/to/file.ts" },
|
|
183
|
+
agentContext: "coordinator",
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
expect(result.isViolation).toBe(false);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
describe("coordinator_ran_tests violation", () => {
|
|
191
|
+
it("detects bash test execution from coordinator", () => {
|
|
192
|
+
const result = detectCoordinatorViolation({
|
|
193
|
+
sessionId,
|
|
194
|
+
epicId,
|
|
195
|
+
toolName: "bash",
|
|
196
|
+
toolArgs: { command: "bun test src/module.test.ts" },
|
|
197
|
+
agentContext: "coordinator",
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
expect(result.isViolation).toBe(true);
|
|
201
|
+
expect(result.violationType).toBe("coordinator_ran_tests");
|
|
202
|
+
expect(result.message).toContain("Workers run tests");
|
|
203
|
+
expect(result.payload.command).toContain("bun test");
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("detects npm test from coordinator", () => {
|
|
207
|
+
const result = detectCoordinatorViolation({
|
|
208
|
+
sessionId,
|
|
209
|
+
epicId,
|
|
210
|
+
toolName: "bash",
|
|
211
|
+
toolArgs: { command: "npm run test:unit" },
|
|
212
|
+
agentContext: "coordinator",
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
expect(result.isViolation).toBe(true);
|
|
216
|
+
expect(result.violationType).toBe("coordinator_ran_tests");
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it("detects jest from coordinator", () => {
|
|
220
|
+
const result = detectCoordinatorViolation({
|
|
221
|
+
sessionId,
|
|
222
|
+
epicId,
|
|
223
|
+
toolName: "bash",
|
|
224
|
+
toolArgs: { command: "jest --coverage" },
|
|
225
|
+
agentContext: "coordinator",
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
expect(result.isViolation).toBe(true);
|
|
229
|
+
expect(result.violationType).toBe("coordinator_ran_tests");
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it("does not detect non-test bash commands", () => {
|
|
233
|
+
const result = detectCoordinatorViolation({
|
|
234
|
+
sessionId,
|
|
235
|
+
epicId,
|
|
236
|
+
toolName: "bash",
|
|
237
|
+
toolArgs: { command: "git status" },
|
|
238
|
+
agentContext: "coordinator",
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
expect(result.isViolation).toBe(false);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("does not detect test execution from worker", () => {
|
|
245
|
+
const result = detectCoordinatorViolation({
|
|
246
|
+
sessionId,
|
|
247
|
+
epicId,
|
|
248
|
+
toolName: "bash",
|
|
249
|
+
toolArgs: { command: "bun test" },
|
|
250
|
+
agentContext: "worker",
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
expect(result.isViolation).toBe(false);
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
describe("coordinator_reserved_files violation", () => {
|
|
258
|
+
it("detects swarmmail_reserve from coordinator", () => {
|
|
259
|
+
const result = detectCoordinatorViolation({
|
|
260
|
+
sessionId,
|
|
261
|
+
epicId,
|
|
262
|
+
toolName: "swarmmail_reserve",
|
|
263
|
+
toolArgs: { paths: ["src/auth/**"], reason: "Working on auth" },
|
|
264
|
+
agentContext: "coordinator",
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
expect(result.isViolation).toBe(true);
|
|
268
|
+
expect(result.violationType).toBe("coordinator_reserved_files");
|
|
269
|
+
expect(result.message).toContain("Workers reserve files");
|
|
270
|
+
expect(result.payload.paths).toEqual(["src/auth/**"]);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it("detects agentmail_reserve from coordinator", () => {
|
|
274
|
+
const result = detectCoordinatorViolation({
|
|
275
|
+
sessionId,
|
|
276
|
+
epicId,
|
|
277
|
+
toolName: "agentmail_reserve",
|
|
278
|
+
toolArgs: { paths: ["src/lib/**"], reason: "Refactoring" },
|
|
279
|
+
agentContext: "coordinator",
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
expect(result.isViolation).toBe(true);
|
|
283
|
+
expect(result.violationType).toBe("coordinator_reserved_files");
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it("does not detect reserve from worker", () => {
|
|
287
|
+
const result = detectCoordinatorViolation({
|
|
288
|
+
sessionId,
|
|
289
|
+
epicId,
|
|
290
|
+
toolName: "swarmmail_reserve",
|
|
291
|
+
toolArgs: { paths: ["src/auth/**"], reason: "Working on auth" },
|
|
292
|
+
agentContext: "worker",
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
expect(result.isViolation).toBe(false);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
describe("no_worker_spawned violation", () => {
|
|
300
|
+
it("detects no spawn after decomposition", () => {
|
|
301
|
+
const result = detectCoordinatorViolation({
|
|
302
|
+
sessionId,
|
|
303
|
+
epicId,
|
|
304
|
+
toolName: "hive_create_epic",
|
|
305
|
+
toolArgs: {
|
|
306
|
+
epic_title: "Add feature",
|
|
307
|
+
subtasks: [
|
|
308
|
+
{ title: "Task 1", files: ["a.ts"] },
|
|
309
|
+
{ title: "Task 2", files: ["b.ts"] },
|
|
310
|
+
],
|
|
311
|
+
},
|
|
312
|
+
agentContext: "coordinator",
|
|
313
|
+
checkNoSpawn: true,
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
expect(result.isViolation).toBe(true);
|
|
317
|
+
expect(result.violationType).toBe("no_worker_spawned");
|
|
318
|
+
expect(result.message).toContain("decomposition without spawning");
|
|
319
|
+
expect(result.payload.epic_title).toBe("Add feature");
|
|
320
|
+
expect(result.payload.subtask_count).toBe(2);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it("does not flag if workers were spawned", () => {
|
|
324
|
+
const result = detectCoordinatorViolation({
|
|
325
|
+
sessionId,
|
|
326
|
+
epicId,
|
|
327
|
+
toolName: "hive_create_epic",
|
|
328
|
+
toolArgs: {
|
|
329
|
+
epic_title: "Add feature",
|
|
330
|
+
subtasks: [
|
|
331
|
+
{ title: "Task 1", files: ["a.ts"] },
|
|
332
|
+
{ title: "Task 2", files: ["b.ts"] },
|
|
333
|
+
],
|
|
334
|
+
},
|
|
335
|
+
agentContext: "coordinator",
|
|
336
|
+
checkNoSpawn: false, // Workers were spawned
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
expect(result.isViolation).toBe(false);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("does not flag from worker agent", () => {
|
|
343
|
+
const result = detectCoordinatorViolation({
|
|
344
|
+
sessionId,
|
|
345
|
+
epicId,
|
|
346
|
+
toolName: "hive_create_epic",
|
|
347
|
+
toolArgs: {
|
|
348
|
+
epic_title: "Add feature",
|
|
349
|
+
subtasks: [{ title: "Task 1", files: ["a.ts"] }],
|
|
350
|
+
},
|
|
351
|
+
agentContext: "worker",
|
|
352
|
+
checkNoSpawn: true,
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
expect(result.isViolation).toBe(false);
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
describe("event capture integration", () => {
|
|
360
|
+
it("captures violation event to session file when violation detected", () => {
|
|
361
|
+
const result = detectCoordinatorViolation({
|
|
362
|
+
sessionId,
|
|
363
|
+
epicId,
|
|
364
|
+
toolName: "edit",
|
|
365
|
+
toolArgs: { filePath: "/test.ts", oldString: "a", newString: "b" },
|
|
366
|
+
agentContext: "coordinator",
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
expect(result.isViolation).toBe(true);
|
|
370
|
+
|
|
371
|
+
// Verify event was written to session file
|
|
372
|
+
const sessionDir = path.join(os.homedir(), ".config", "swarm-tools", "sessions");
|
|
373
|
+
const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
|
|
374
|
+
expect(fs.existsSync(sessionPath)).toBe(true);
|
|
375
|
+
|
|
376
|
+
const content = fs.readFileSync(sessionPath, "utf-8");
|
|
377
|
+
const lines = content.trim().split("\n");
|
|
378
|
+
expect(lines.length).toBe(1);
|
|
379
|
+
|
|
380
|
+
const event = JSON.parse(lines[0]);
|
|
381
|
+
expect(event.event_type).toBe("VIOLATION");
|
|
382
|
+
expect(event.violation_type).toBe("coordinator_edited_file");
|
|
383
|
+
expect(event.session_id).toBe(sessionId);
|
|
384
|
+
expect(event.epic_id).toBe(epicId);
|
|
385
|
+
expect(event.payload.tool).toBe("edit");
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it("does not capture event when no violation", () => {
|
|
389
|
+
const result = detectCoordinatorViolation({
|
|
390
|
+
sessionId,
|
|
391
|
+
epicId,
|
|
392
|
+
toolName: "read",
|
|
393
|
+
toolArgs: { filePath: "/test.ts" },
|
|
394
|
+
agentContext: "coordinator",
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
expect(result.isViolation).toBe(false);
|
|
398
|
+
|
|
399
|
+
// Verify no session file created
|
|
400
|
+
const sessionDir = path.join(os.homedir(), ".config", "swarm-tools", "sessions");
|
|
401
|
+
const sessionPath = path.join(sessionDir, `${sessionId}.jsonl`);
|
|
402
|
+
expect(fs.existsSync(sessionPath)).toBe(false);
|
|
403
|
+
});
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
describe("coordinator context", () => {
|
|
408
|
+
beforeEach(() => {
|
|
409
|
+
clearCoordinatorContext();
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
afterEach(() => {
|
|
413
|
+
clearCoordinatorContext();
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
describe("setCoordinatorContext", () => {
|
|
417
|
+
it("sets coordinator context", () => {
|
|
418
|
+
setCoordinatorContext({
|
|
419
|
+
isCoordinator: true,
|
|
420
|
+
epicId: "test-epic-123",
|
|
421
|
+
sessionId: "test-session-456",
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
const ctx = getCoordinatorContext();
|
|
425
|
+
expect(ctx.isCoordinator).toBe(true);
|
|
426
|
+
expect(ctx.epicId).toBe("test-epic-123");
|
|
427
|
+
expect(ctx.sessionId).toBe("test-session-456");
|
|
428
|
+
expect(ctx.activatedAt).toBeDefined();
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it("merges with existing context", () => {
|
|
432
|
+
setCoordinatorContext({
|
|
433
|
+
isCoordinator: true,
|
|
434
|
+
sessionId: "session-1",
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
setCoordinatorContext({
|
|
438
|
+
epicId: "epic-1",
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
const ctx = getCoordinatorContext();
|
|
442
|
+
expect(ctx.isCoordinator).toBe(true);
|
|
443
|
+
expect(ctx.sessionId).toBe("session-1");
|
|
444
|
+
expect(ctx.epicId).toBe("epic-1");
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
describe("isInCoordinatorContext", () => {
|
|
449
|
+
it("returns false when not in coordinator context", () => {
|
|
450
|
+
expect(isInCoordinatorContext()).toBe(false);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
it("returns true when in coordinator context", () => {
|
|
454
|
+
setCoordinatorContext({
|
|
455
|
+
isCoordinator: true,
|
|
456
|
+
epicId: "test-epic",
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
expect(isInCoordinatorContext()).toBe(true);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
it("returns false after context is cleared", () => {
|
|
463
|
+
setCoordinatorContext({
|
|
464
|
+
isCoordinator: true,
|
|
465
|
+
epicId: "test-epic",
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
clearCoordinatorContext();
|
|
469
|
+
|
|
470
|
+
expect(isInCoordinatorContext()).toBe(false);
|
|
471
|
+
});
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
describe("clearCoordinatorContext", () => {
|
|
475
|
+
it("clears all context", () => {
|
|
476
|
+
setCoordinatorContext({
|
|
477
|
+
isCoordinator: true,
|
|
478
|
+
epicId: "test-epic",
|
|
479
|
+
sessionId: "test-session",
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
clearCoordinatorContext();
|
|
483
|
+
|
|
484
|
+
const ctx = getCoordinatorContext();
|
|
485
|
+
expect(ctx.isCoordinator).toBe(false);
|
|
486
|
+
expect(ctx.epicId).toBeUndefined();
|
|
487
|
+
expect(ctx.sessionId).toBeUndefined();
|
|
488
|
+
});
|
|
489
|
+
});
|
|
490
|
+
});
|
|
106
491
|
});
|
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* @module planning-guardrails
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
import { captureCoordinatorEvent } from "./eval-capture.js";
|
|
11
|
+
|
|
10
12
|
/**
|
|
11
13
|
* Patterns that suggest file modification work
|
|
12
14
|
* These indicate the todo is about implementation, not tracking
|
|
@@ -147,3 +149,290 @@ Swarm workers can complete these ${fileModificationCount} tasks in parallel.
|
|
|
147
149
|
export function shouldAnalyzeTool(toolName: string): boolean {
|
|
148
150
|
return toolName === "todowrite" || toolName === "TodoWrite";
|
|
149
151
|
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Violation patterns for coordinator behavior detection
|
|
155
|
+
*
|
|
156
|
+
* These patterns identify when a coordinator is performing work
|
|
157
|
+
* that should be delegated to worker agents.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* // Bad: Coordinator editing files
|
|
162
|
+
* if (VIOLATION_PATTERNS.FILE_MODIFICATION_TOOLS.includes("edit")) { ... }
|
|
163
|
+
*
|
|
164
|
+
* // Good: Worker editing files
|
|
165
|
+
* // (no violation when agentContext === "worker")
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
export const VIOLATION_PATTERNS = {
|
|
169
|
+
/**
|
|
170
|
+
* Tool names that modify files
|
|
171
|
+
*
|
|
172
|
+
* Coordinators should NEVER call these tools directly.
|
|
173
|
+
* Workers reserve files and make modifications.
|
|
174
|
+
*/
|
|
175
|
+
FILE_MODIFICATION_TOOLS: ["edit", "write"],
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Tool names for file reservations
|
|
179
|
+
*
|
|
180
|
+
* Coordinators don't reserve files - workers do this
|
|
181
|
+
* before editing to prevent conflicts.
|
|
182
|
+
*/
|
|
183
|
+
RESERVATION_TOOLS: ["swarmmail_reserve", "agentmail_reserve"],
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Regex patterns that indicate test execution in bash commands
|
|
187
|
+
*
|
|
188
|
+
* Coordinators review test results, workers run tests.
|
|
189
|
+
* Matches common test runners and test file patterns.
|
|
190
|
+
*/
|
|
191
|
+
TEST_EXECUTION_PATTERNS: [
|
|
192
|
+
/\bbun\s+test\b/i,
|
|
193
|
+
/\bnpm\s+(run\s+)?test/i,
|
|
194
|
+
/\byarn\s+(run\s+)?test/i,
|
|
195
|
+
/\bpnpm\s+(run\s+)?test/i,
|
|
196
|
+
/\bjest\b/i,
|
|
197
|
+
/\bvitest\b/i,
|
|
198
|
+
/\bmocha\b/i,
|
|
199
|
+
/\bava\b/i,
|
|
200
|
+
/\btape\b/i,
|
|
201
|
+
/\.test\.(ts|js|tsx|jsx)\b/i,
|
|
202
|
+
/\.spec\.(ts|js|tsx|jsx)\b/i,
|
|
203
|
+
],
|
|
204
|
+
} as const;
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Result of violation detection
|
|
208
|
+
*/
|
|
209
|
+
export interface ViolationDetectionResult {
|
|
210
|
+
/** Whether a violation was detected */
|
|
211
|
+
isViolation: boolean;
|
|
212
|
+
|
|
213
|
+
/** Type of violation if detected */
|
|
214
|
+
violationType?:
|
|
215
|
+
| "coordinator_edited_file"
|
|
216
|
+
| "coordinator_ran_tests"
|
|
217
|
+
| "coordinator_reserved_files"
|
|
218
|
+
| "no_worker_spawned";
|
|
219
|
+
|
|
220
|
+
/** Human-readable message */
|
|
221
|
+
message?: string;
|
|
222
|
+
|
|
223
|
+
/** Payload data for the violation */
|
|
224
|
+
payload?: Record<string, unknown>;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Detect coordinator violations in real-time
|
|
229
|
+
*
|
|
230
|
+
* Checks for patterns that indicate a coordinator is doing work
|
|
231
|
+
* that should be delegated to workers:
|
|
232
|
+
* 1. Edit/Write tool calls (coordinators plan, workers implement)
|
|
233
|
+
* 2. Test execution (workers verify, coordinators review)
|
|
234
|
+
* 3. File reservations (workers reserve before editing)
|
|
235
|
+
* 4. No worker spawned after decomposition (coordinators must delegate)
|
|
236
|
+
*
|
|
237
|
+
* When a violation is detected, captures it via captureCoordinatorEvent().
|
|
238
|
+
*
|
|
239
|
+
* @param params - Detection parameters
|
|
240
|
+
* @returns Violation detection result
|
|
241
|
+
*/
|
|
242
|
+
export function detectCoordinatorViolation(params: {
|
|
243
|
+
sessionId: string;
|
|
244
|
+
epicId: string;
|
|
245
|
+
toolName: string;
|
|
246
|
+
toolArgs: Record<string, unknown>;
|
|
247
|
+
agentContext: "coordinator" | "worker";
|
|
248
|
+
checkNoSpawn?: boolean;
|
|
249
|
+
}): ViolationDetectionResult {
|
|
250
|
+
const { sessionId, epicId, toolName, toolArgs, agentContext, checkNoSpawn = false } = params;
|
|
251
|
+
|
|
252
|
+
// Only check coordinator violations
|
|
253
|
+
if (agentContext !== "coordinator") {
|
|
254
|
+
return { isViolation: false };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// Check for file modification violation
|
|
258
|
+
if (VIOLATION_PATTERNS.FILE_MODIFICATION_TOOLS.includes(toolName as any)) {
|
|
259
|
+
const file = (toolArgs.filePath as string) || "";
|
|
260
|
+
const payload = { tool: toolName, file };
|
|
261
|
+
|
|
262
|
+
captureCoordinatorEvent({
|
|
263
|
+
session_id: sessionId,
|
|
264
|
+
epic_id: epicId,
|
|
265
|
+
timestamp: new Date().toISOString(),
|
|
266
|
+
event_type: "VIOLATION",
|
|
267
|
+
violation_type: "coordinator_edited_file",
|
|
268
|
+
payload,
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
return {
|
|
272
|
+
isViolation: true,
|
|
273
|
+
violationType: "coordinator_edited_file",
|
|
274
|
+
message: `⚠️ Coordinator should not edit files directly. Coordinators should spawn workers to implement changes.`,
|
|
275
|
+
payload,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// Check for test execution violation
|
|
280
|
+
if (toolName === "bash") {
|
|
281
|
+
const command = (toolArgs.command as string) || "";
|
|
282
|
+
const isTestCommand = VIOLATION_PATTERNS.TEST_EXECUTION_PATTERNS.some((pattern) =>
|
|
283
|
+
pattern.test(command),
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
if (isTestCommand) {
|
|
287
|
+
const payload = { tool: toolName, command };
|
|
288
|
+
|
|
289
|
+
captureCoordinatorEvent({
|
|
290
|
+
session_id: sessionId,
|
|
291
|
+
epic_id: epicId,
|
|
292
|
+
timestamp: new Date().toISOString(),
|
|
293
|
+
event_type: "VIOLATION",
|
|
294
|
+
violation_type: "coordinator_ran_tests",
|
|
295
|
+
payload,
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
return {
|
|
299
|
+
isViolation: true,
|
|
300
|
+
violationType: "coordinator_ran_tests",
|
|
301
|
+
message: `⚠️ Coordinator should not run tests directly. Workers run tests as part of their implementation verification.`,
|
|
302
|
+
payload,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Check for file reservation violation
|
|
308
|
+
if (VIOLATION_PATTERNS.RESERVATION_TOOLS.includes(toolName as any)) {
|
|
309
|
+
const paths = (toolArgs.paths as string[]) || [];
|
|
310
|
+
const payload = { tool: toolName, paths };
|
|
311
|
+
|
|
312
|
+
captureCoordinatorEvent({
|
|
313
|
+
session_id: sessionId,
|
|
314
|
+
epic_id: epicId,
|
|
315
|
+
timestamp: new Date().toISOString(),
|
|
316
|
+
event_type: "VIOLATION",
|
|
317
|
+
violation_type: "coordinator_reserved_files",
|
|
318
|
+
payload,
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
isViolation: true,
|
|
323
|
+
violationType: "coordinator_reserved_files",
|
|
324
|
+
message: `⚠️ Coordinator should not reserve files. Workers reserve files before editing to prevent conflicts.`,
|
|
325
|
+
payload,
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// Check for no worker spawned after decomposition
|
|
330
|
+
if (toolName === "hive_create_epic" && checkNoSpawn) {
|
|
331
|
+
const epicTitle = (toolArgs.epic_title as string) || "";
|
|
332
|
+
const subtasks = (toolArgs.subtasks as unknown[]) || [];
|
|
333
|
+
const payload = { epic_title: epicTitle, subtask_count: subtasks.length };
|
|
334
|
+
|
|
335
|
+
captureCoordinatorEvent({
|
|
336
|
+
session_id: sessionId,
|
|
337
|
+
epic_id: epicId,
|
|
338
|
+
timestamp: new Date().toISOString(),
|
|
339
|
+
event_type: "VIOLATION",
|
|
340
|
+
violation_type: "no_worker_spawned",
|
|
341
|
+
payload,
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
return {
|
|
345
|
+
isViolation: true,
|
|
346
|
+
violationType: "no_worker_spawned",
|
|
347
|
+
message: `⚠️ Coordinator created decomposition without spawning workers. After hive_create_epic, use swarm_spawn_subtask for each task.`,
|
|
348
|
+
payload,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return { isViolation: false };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Coordinator context state
|
|
357
|
+
*
|
|
358
|
+
* Tracks whether the current session is acting as a swarm coordinator.
|
|
359
|
+
* Set when an epic is created or when swarm tools are used.
|
|
360
|
+
*/
|
|
361
|
+
interface CoordinatorContext {
|
|
362
|
+
/** Whether we're in coordinator mode */
|
|
363
|
+
isCoordinator: boolean;
|
|
364
|
+
/** Active epic ID if any */
|
|
365
|
+
epicId?: string;
|
|
366
|
+
/** Session ID for event capture */
|
|
367
|
+
sessionId?: string;
|
|
368
|
+
/** When coordinator mode was activated */
|
|
369
|
+
activatedAt?: number;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Global coordinator context state */
|
|
373
|
+
let coordinatorContext: CoordinatorContext = {
|
|
374
|
+
isCoordinator: false,
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Set coordinator context
|
|
379
|
+
*
|
|
380
|
+
* Called when swarm coordination begins (e.g., after hive_create_epic or swarm_decompose).
|
|
381
|
+
*
|
|
382
|
+
* @param ctx - Coordinator context to set
|
|
383
|
+
*/
|
|
384
|
+
export function setCoordinatorContext(ctx: Partial<CoordinatorContext>): void {
|
|
385
|
+
coordinatorContext = {
|
|
386
|
+
...coordinatorContext,
|
|
387
|
+
...ctx,
|
|
388
|
+
activatedAt: ctx.isCoordinator ? Date.now() : coordinatorContext.activatedAt,
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Get current coordinator context
|
|
394
|
+
*
|
|
395
|
+
* @returns Current coordinator context state
|
|
396
|
+
*/
|
|
397
|
+
export function getCoordinatorContext(): CoordinatorContext {
|
|
398
|
+
return { ...coordinatorContext };
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Clear coordinator context
|
|
403
|
+
*
|
|
404
|
+
* Called when swarm coordination ends (e.g., epic closed or session ends).
|
|
405
|
+
*/
|
|
406
|
+
export function clearCoordinatorContext(): void {
|
|
407
|
+
coordinatorContext = {
|
|
408
|
+
isCoordinator: false,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Check if we're in coordinator context
|
|
414
|
+
*
|
|
415
|
+
* Returns true if:
|
|
416
|
+
* 1. Coordinator context was explicitly set
|
|
417
|
+
* 2. Context was set within the last 4 hours (session timeout)
|
|
418
|
+
*
|
|
419
|
+
* @returns Whether we're currently in coordinator mode
|
|
420
|
+
*/
|
|
421
|
+
export function isInCoordinatorContext(): boolean {
|
|
422
|
+
if (!coordinatorContext.isCoordinator) {
|
|
423
|
+
return false;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// Check for session timeout (4 hours)
|
|
427
|
+
const COORDINATOR_TIMEOUT_MS = 4 * 60 * 60 * 1000;
|
|
428
|
+
if (coordinatorContext.activatedAt) {
|
|
429
|
+
const elapsed = Date.now() - coordinatorContext.activatedAt;
|
|
430
|
+
if (elapsed > COORDINATOR_TIMEOUT_MS) {
|
|
431
|
+
// Session timed out, clear context
|
|
432
|
+
clearCoordinatorContext();
|
|
433
|
+
return false;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return true;
|
|
438
|
+
}
|