-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest-phase8.ts
More file actions
1268 lines (1138 loc) · 45.8 KB
/
test-phase8.ts
File metadata and controls
1268 lines (1138 loc) · 45.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Phase 8: Claude-Code Inspired Enhancements — Test Suite
*
* Tests for:
* 8a — Multi-phase pipeline with approval gates
* 8b — Confidence-based multi-agent filtering
* 8c — Matcher-based hook filtering
* 8d — Fan-out / fan-in parallel aggregation
*
* Run with: npx ts-node test-phase8.ts
*/
import { AdapterRegistry } from './adapters/adapter-registry';
import { BaseAdapter } from './adapters/base-adapter';
import { AdapterHookManager, matchGlob, matchToolPattern } from './lib/adapter-hooks';
import type { HookContext, ExecutionHook, HookMatcher } from './lib/adapter-hooks';
import { PhasePipeline } from './lib/phase-pipeline';
import type { PhaseDefinition, PhaseResult, PipelineResult, PipelineExecutionContext } from './lib/phase-pipeline';
import { ConfidenceFilter } from './lib/confidence-filter';
import type { Finding, FilterResult, AggregationStrategy } from './lib/confidence-filter';
import { FanOutFanIn } from './lib/fan-out';
import type { FanOutStep, TaggedResult, FanInResult, FanInStrategy } from './lib/fan-out';
import type { AgentPayload, AgentContext, AgentResult } from './types/agent-adapter';
// ============================================================================
// TEST UTILITIES
// ============================================================================
const colors = {
green: '\x1b[32m',
red: '\x1b[31m',
reset: '\x1b[0m',
bold: '\x1b[1m',
} as const;
let passed = 0;
let failed = 0;
function log(msg: string, color: keyof typeof colors = 'reset') {
console.log(`${colors[color]}${msg}${colors.reset}`);
}
function header(title: string) {
console.log('\n' + '='.repeat(64));
log(` ${title}`, 'bold');
console.log('='.repeat(64));
}
function pass(test: string) { log(` [PASS] ${test}`, 'green'); passed++; }
function fail(test: string, err?: string) {
log(` [FAIL] ${test}`, 'red');
if (err) log(` ${err}`, 'red');
failed++;
}
function assert(condition: boolean, test: string, detail?: string) {
if (condition) pass(test);
else fail(test, detail);
}
function assertThrows(fn: () => void, test: string) {
try { fn(); fail(test, 'Expected to throw'); }
catch { pass(test); }
}
// ============================================================================
// MOCK ADAPTERS
// ============================================================================
class MockAdapter extends BaseAdapter {
readonly name: string;
readonly version = '1.0.0';
callCount = 0;
lastPayload?: AgentPayload;
customResult?: AgentResult;
constructor(name = 'mock') { super(); this.name = name; }
async executeAgent(agentId: string, payload: AgentPayload, _context: AgentContext): Promise<AgentResult> {
this.callCount++;
this.lastPayload = payload;
if (this.customResult) return this.customResult;
return { success: true, data: { agentId, echo: payload.params }, metadata: { adapter: this.name } };
}
}
class FailingAdapter extends BaseAdapter {
readonly name = 'failing';
readonly version = '1.0.0';
async executeAgent(): Promise<AgentResult> {
return { success: false, error: { code: 'FAIL', message: 'intentional failure', recoverable: false } };
}
}
class DelayAdapter extends BaseAdapter {
readonly name: string;
readonly version = '1.0.0';
private delayMs: number;
constructor(name: string, delayMs: number) { super(); this.name = name; this.delayMs = delayMs; }
async executeAgent(agentId: string, payload: AgentPayload): Promise<AgentResult> {
await new Promise(r => setTimeout(r, this.delayMs));
return { success: true, data: { agentId, delayed: this.delayMs } };
}
}
class ValueAdapter extends BaseAdapter {
readonly name: string;
readonly version = '1.0.0';
private value: unknown;
constructor(name: string, value: unknown) { super(); this.name = name; this.value = value; }
async executeAgent(agentId: string): Promise<AgentResult> {
return { success: true, data: this.value };
}
}
/** Helper: create a registry with a mock adapter registered for all agents */
async function makeRegistry(adapterName = 'mock'): Promise<{ registry: AdapterRegistry; adapter: MockAdapter }> {
const registry = new AdapterRegistry();
const adapter = new MockAdapter(adapterName);
await registry.addAdapter(adapter);
registry.setDefaultAdapter(adapterName);
return { registry, adapter };
}
const baseCtx: AgentContext = { agentId: 'orchestrator' };
// ============================================================================
// 8a — MULTI-PHASE PIPELINE WITH APPROVAL GATES
// ============================================================================
async function testPhasePipeline() {
header('Phase 8a — Multi-Phase Pipeline with Approval Gates');
// 1. Constructor validates non-empty phases
{
const { registry } = await makeRegistry();
assertThrows(() => new PhasePipeline(registry, baseCtx, { phases: [] }), 'Rejects empty phases');
}
// 2. Constructor validates duplicate phase names
{
const { registry } = await makeRegistry();
assertThrows(
() => new PhasePipeline(registry, baseCtx, {
phases: [
{ name: 'a', agents: ['x'] },
{ name: 'a', agents: ['y'] },
],
}),
'Rejects duplicate phase names',
);
}
// 3. Simple two-phase pipeline succeeds
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [
{ name: 'research', agents: ['researcher'] },
{ name: 'publish', agents: ['publisher'] },
],
});
const result = await pipeline.run();
assert(result.success, 'Two-phase pipeline succeeds');
assert(result.phases.length === 2, 'Reports two phase results');
}
// 4. Phase status reflects completion
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'work', agents: ['worker'] }],
});
const result = await pipeline.run();
assert(result.phases[0].status === 'completed', 'Phase status is completed');
}
// 5. Parallel agents in a phase
{
const { registry, adapter } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'scan', agents: ['a', 'b', 'c'], parallel: true }],
});
const result = await pipeline.run();
assert(result.success, 'Parallel phase succeeds');
assert(result.phases[0].agentResults.size === 3, 'All 3 parallel agents ran');
}
// 6. Sequential agents stop on first failure
{
const registry = new AdapterRegistry();
await registry.addAdapter(new MockAdapter('mock'));
await registry.addAdapter(new FailingAdapter());
registry.addRoute({ pattern: 'fail:*', adapterName: 'failing' });
registry.setDefaultAdapter('mock');
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'work', agents: ['fail:one', 'agent-b'] }],
});
const result = await pipeline.run();
assert(!result.success, 'Pipeline fails on agent failure');
assert(result.stoppedAt === 'work', 'Reports stopped at phase');
}
// 7. Approval gate — approved
{
const { registry } = await makeRegistry();
const approvals: string[] = [];
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [
{ name: 'draft', agents: ['writer'] },
{ name: 'review', agents: ['reviewer'], requiresApproval: true },
{ name: 'publish', agents: ['publisher'] },
],
onApproval: async (name) => {
approvals.push(name);
return { approved: true, approvedBy: 'admin' };
},
});
const result = await pipeline.run();
assert(result.success, 'Pipeline with approved gate succeeds');
assert(approvals.includes('review'), 'Approval callback was called for review phase');
assert(result.phases[1].status === 'approved', 'Review phase status is approved');
assert(result.phases[1].approval?.approvedBy === 'admin', 'Approval records approver');
}
// 8. Approval gate — rejected
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [
{ name: 'draft', agents: ['writer'] },
{ name: 'review', agents: ['reviewer'], requiresApproval: true },
{ name: 'publish', agents: ['publisher'] },
],
onApproval: async () => ({ approved: false, reason: 'Not ready' }),
});
const result = await pipeline.run();
assert(!result.success, 'Pipeline fails on rejection');
assert(result.stoppedAt === 'review', 'Stopped at review gate');
assert(result.stopReason === 'Not ready', 'Reports rejection reason');
assert(result.phases.length === 2, 'Only draft and review phases recorded');
}
// 9. autoApprove skips approval callback
{
const { registry } = await makeRegistry();
let callbackCalled = false;
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [
{ name: 'work', agents: ['worker'], requiresApproval: true },
{ name: 'done', agents: ['finisher'] },
],
autoApprove: true,
onApproval: async () => { callbackCalled = true; return { approved: false }; },
});
const result = await pipeline.run();
assert(result.success, 'autoApprove bypasses gate');
assert(!callbackCalled, 'onApproval callback not called when autoApprove=true');
}
// 10. No approval callback rejects by default
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'gated', agents: ['worker'], requiresApproval: true }],
});
const result = await pipeline.run();
assert(!result.success, 'No approval callback → rejection');
}
// 11. onPhaseStart and onPhaseComplete callbacks
{
const { registry } = await makeRegistry();
const starts: string[] = [];
const completes: string[] = [];
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [
{ name: 'a', agents: ['agent-a'] },
{ name: 'b', agents: ['agent-b'] },
],
onPhaseStart: (name) => starts.push(name),
onPhaseComplete: (result) => completes.push(result.phaseName),
});
await pipeline.run();
assert(starts.join(',') === 'a,b', 'onPhaseStart fires in order');
assert(completes.join(',') === 'a,b', 'onPhaseComplete fires in order');
}
// 12. payloadFactory provides custom payloads
{
const { registry, adapter } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{
name: 'custom',
agents: ['worker'],
payloadFactory: (agentId, prev) => ({
action: 'custom-action',
params: { agent: agentId, prevCount: prev.length },
}),
}],
});
await pipeline.run();
assert(adapter.lastPayload?.action === 'custom-action', 'payloadFactory sets payload');
}
// 13. Pipeline status lifecycle
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'work', agents: ['worker'] }],
});
assert(pipeline.status === 'idle', 'Initial status is idle');
await pipeline.run();
assert(pipeline.status === 'completed', 'Final status is completed');
}
// 14. Reset clears state
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'work', agents: ['worker'] }],
});
await pipeline.run();
pipeline.reset();
assert(pipeline.status === 'idle', 'Reset restores idle status');
assert(pipeline.results.length === 0, 'Reset clears results');
}
// 15. phases property returns definitions
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'a', agents: ['x'] }, { name: 'b', agents: ['y'] }],
});
assert(pipeline.phases.length === 2, 'phases getter returns definitions');
assert(pipeline.phases[0].name === 'a', 'phases[0].name matches');
}
// 16. totalMs is tracked
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'a', agents: ['x'] }],
});
const result = await pipeline.run();
assert(result.totalMs >= 0, 'totalMs is non-negative');
}
// 17. Phase durationMs is tracked
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'a', agents: ['x'] }],
});
const result = await pipeline.run();
assert(result.phases[0].durationMs >= 0, 'Phase durationMs is non-negative');
}
// 18. Approval context has pipeline info
{
const { registry } = await makeRegistry();
let capturedCtx: PipelineExecutionContext | undefined;
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [
{ name: 'a', agents: ['x'] },
{ name: 'b', agents: ['y'], requiresApproval: true },
],
onApproval: async (_name, _result, ctx) => {
capturedCtx = ctx;
return { approved: true };
},
});
await pipeline.run();
assert(capturedCtx?.currentPhaseIndex === 1, 'Approval context has correct phase index');
assert(capturedCtx?.totalPhases === 2, 'Approval context has total phases');
assert(capturedCtx?.completedPhases.length === 1, 'Approval context has completed phases');
}
// 19. Multiple approval gates in sequence
{
const { registry } = await makeRegistry();
const gates: string[] = [];
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [
{ name: 'a', agents: ['x'], requiresApproval: true },
{ name: 'b', agents: ['y'], requiresApproval: true },
{ name: 'c', agents: ['z'] },
],
onApproval: async (name) => { gates.push(name); return { approved: true }; },
});
const result = await pipeline.run();
assert(result.success, 'Multiple gates all approved');
assert(gates.length === 2, 'Both gates triggered');
}
// 20. Default payload used when no payloadFactory
{
const { registry, adapter } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'run', agents: ['x'] }],
});
await pipeline.run({ action: 'go', params: { x: 1 } });
assert(adapter.lastPayload?.action === 'go', 'Uses default payload from run()');
}
// 21. Rejected pipeline has status rejected
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'gated', agents: ['x'], requiresApproval: true }],
onApproval: async () => ({ approved: false }),
});
await pipeline.run();
assert(pipeline.status === 'rejected', 'Pipeline status is rejected');
}
// 22. Failed pipeline has status failed
{
const registry = new AdapterRegistry();
await registry.addAdapter(new FailingAdapter());
registry.setDefaultAdapter('failing');
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'work', agents: ['x'] }],
});
await pipeline.run();
assert(pipeline.status === 'failed', 'Pipeline status is failed');
}
// 23. phase description is optional
{
const { registry } = await makeRegistry();
const pipeline = new PhasePipeline(registry, baseCtx, {
phases: [{ name: 'test', agents: ['x'], description: 'A test phase' }],
});
assert(pipeline.phases[0].description === 'A test phase', 'Phase description preserved');
}
}
// ============================================================================
// 8b — CONFIDENCE-BASED MULTI-AGENT FILTERING
// ============================================================================
async function testConfidenceFilter() {
header('Phase 8b — Confidence-Based Multi-Agent Filtering');
const makeFinding = (id: string, confidence: number, source = 'agent-a'): Finding => ({
id,
description: `Finding ${id}`,
confidence,
sourceAgent: source,
});
// 1. score() normalises to 0–100
{
const filter = new ConfidenceFilter(null, null);
assert(filter.score(makeFinding('1', 150)) === 100, 'score() caps at 100');
assert(filter.score(makeFinding('2', -10)) === 0, 'score() floors at 0');
assert(filter.score(makeFinding('3', 75)) === 75, 'score() passes through in-range');
}
// 2. filter() with default threshold (70)
{
const filter = new ConfidenceFilter(null, null);
const findings = [makeFinding('a', 80), makeFinding('b', 50), makeFinding('c', 70)];
const result = filter.filter(findings);
assert(result.accepted.length === 2, 'Default threshold accepts 80 and 70');
assert(result.rejected.length === 1, 'Default threshold rejects 50');
assert(result.threshold === 70, 'Reports default threshold');
}
// 3. filter() with custom threshold
{
const filter = new ConfidenceFilter(null, null, { defaultThreshold: 90 });
const findings = [makeFinding('a', 95), makeFinding('b', 85)];
const result = filter.filter(findings);
assert(result.accepted.length === 1, 'Custom threshold=90 accepts 95');
assert(result.rejected.length === 1, 'Custom threshold=90 rejects 85');
}
// 4. filter() with override threshold
{
const filter = new ConfidenceFilter(null, null, { defaultThreshold: 90 });
const findings = [makeFinding('a', 60)];
const result = filter.filter(findings, 50);
assert(result.accepted.length === 1, 'Override threshold=50 accepts 60');
}
// 5. filter() with empty input
{
const filter = new ConfidenceFilter(null, null);
const result = filter.filter([]);
assert(result.accepted.length === 0, 'Empty input → empty accepted');
assert(result.rejected.length === 0, 'Empty input → empty rejected');
}
// 6. validate() requires registry
{
const filter = new ConfidenceFilter(null, null);
let threw = false;
try { await filter.validate(makeFinding('1', 50), 'validator'); } catch { threw = true; }
assert(threw, 'validate() throws without registry');
}
// 7. validate() requires validator agent
{
const { registry } = await makeRegistry();
const filter = new ConfidenceFilter(registry, baseCtx);
let threw = false;
try { await filter.validate(makeFinding('1', 50)); } catch { threw = true; }
assert(threw, 'validate() throws without validator agent');
}
// 8. validate() boosts confidence on success
{
const { registry } = await makeRegistry();
const filter = new ConfidenceFilter(registry, baseCtx);
const result = await filter.validate(makeFinding('1', 50), 'validator');
assert(result.validated === true, 'Validated flag is true');
assert(result.validatedBy === 'validator', 'validatedBy is set');
assert(result.confidence === 70, 'Confidence boosted by 20 on success');
}
// 9. validate() reduces confidence on failure
{
const registry = new AdapterRegistry();
await registry.addAdapter(new FailingAdapter());
registry.setDefaultAdapter('failing');
const filter = new ConfidenceFilter(registry, baseCtx);
const result = await filter.validate(makeFinding('1', 50), 'failing-agent');
assert(result.validated === false, 'Validated flag is false');
assert(result.confidence === 40, 'Confidence reduced by 10 on failure');
}
// 10. validate() caps at 100
{
const { registry } = await makeRegistry();
const filter = new ConfidenceFilter(registry, baseCtx);
const result = await filter.validate(makeFinding('1', 95), 'validator');
assert(result.confidence === 100, 'Confidence capped at 100');
}
// 11. validate() floors at 0
{
const registry = new AdapterRegistry();
await registry.addAdapter(new FailingAdapter());
registry.setDefaultAdapter('failing');
const filter = new ConfidenceFilter(registry, baseCtx);
const result = await filter.validate(makeFinding('1', 5), 'x');
assert(result.confidence === 0, 'Confidence floors at 0');
}
// 12. validateRejected() re-filters after validation
{
const { registry } = await makeRegistry();
const filter = new ConfidenceFilter(registry, baseCtx, { defaultThreshold: 70 });
const findings = [makeFinding('a', 80), makeFinding('b', 55)];
const initial = filter.filter(findings);
assert(initial.rejected.length === 1, 'Initially one rejected');
const refiltered = await filter.validateRejected(initial, 'validator');
// b was 55, boosted by 20 → 75, which passes threshold 70
assert(refiltered.accepted.length === 2, 'After validation, b is now accepted');
assert(refiltered.rejected.length === 0, 'No more rejected');
}
// 13. aggregate() — highest strategy
{
const filter = new ConfidenceFilter(null, null);
const sets = [
[makeFinding('x', 80, 'a'), makeFinding('y', 60, 'a')],
[makeFinding('x', 90, 'b'), makeFinding('y', 50, 'b')],
];
const agg = filter.aggregate(sets, 'highest');
assert(agg.findings.length === 2, 'highest: 2 findings');
const xFinding = agg.findings.find(f => f.id === 'x')!;
assert(xFinding.confidence === 90, 'highest: takes highest confidence');
}
// 14. aggregate() — average strategy
{
const filter = new ConfidenceFilter(null, null);
const sets = [
[makeFinding('x', 80, 'a')],
[makeFinding('x', 60, 'b')],
];
const agg = filter.aggregate(sets, 'average');
assert(agg.findings[0].confidence === 70, 'average: (80+60)/2 = 70');
}
// 15. aggregate() — unanimous strategy (all sources must have finding)
{
const filter = new ConfidenceFilter(null, null);
const sets = [
[makeFinding('x', 80, 'a'), makeFinding('y', 90, 'a')],
[makeFinding('x', 70, 'b')], // no y
];
const agg = filter.aggregate(sets, 'unanimous');
assert(agg.findings.length === 1, 'unanimous: only x (both sources)');
assert(agg.findings[0].id === 'x', 'unanimous: x is the unanimous finding');
}
// 16. aggregate() — majority strategy
{
const filter = new ConfidenceFilter(null, null);
const sets = [
[makeFinding('x', 80, 'a'), makeFinding('y', 90, 'a')],
[makeFinding('x', 70, 'b'), makeFinding('y', 60, 'b')],
[makeFinding('x', 75, 'c')], // no y in third
];
const agg = filter.aggregate(sets, 'majority');
// majority = floor(3/2)+1 = 2. x appears 3 times, y appears 2 times
assert(agg.findings.length === 2, 'majority: both x and y have >= 2 sources');
}
// 17. aggregate() — empty input
{
const filter = new ConfidenceFilter(null, null);
const agg = filter.aggregate([], 'highest');
assert(agg.findings.length === 0, 'Empty aggregate → empty findings');
assert(agg.sourceCount === 0, 'Empty aggregate → sourceCount 0');
}
// 18. aggregate() reports metadata
{
const filter = new ConfidenceFilter(null, null);
const sets = [
[makeFinding('a', 80, 'x')],
[makeFinding('b', 60, 'y')],
];
const agg = filter.aggregate(sets, 'highest');
assert(agg.totalInput === 2, 'totalInput counts all input findings');
assert(agg.sourceCount === 2, 'sourceCount matches input array length');
assert(agg.strategy === 'highest', 'Reports strategy used');
}
// 19. validationPayloadFactory is used
{
const { registry, adapter } = await makeRegistry();
const filter = new ConfidenceFilter(registry, baseCtx, {
validationPayloadFactory: (f) => ({ action: 'check', params: { fid: f.id } }),
});
await filter.validate(makeFinding('custom', 50), 'validator');
assert(adapter.lastPayload?.action === 'check', 'Custom payload factory used');
}
// 20. Options default threshold via constructor
{
const filter = new ConfidenceFilter(null, null, { defaultThreshold: 55 });
const result = filter.filter([makeFinding('a', 56)]);
assert(result.accepted.length === 1, 'Constructor defaultThreshold=55 accepts 56');
}
// 21. Finding metadata preserved
{
const filter = new ConfidenceFilter(null, null);
const f: Finding = { ...makeFinding('m', 80), metadata: { severity: 'high' } };
const result = filter.filter([f]);
assert(result.accepted[0].metadata?.severity === 'high', 'Metadata preserved through filter');
}
}
// ============================================================================
// 8c — MATCHER-BASED HOOK FILTERING
// ============================================================================
async function testMatcherHooks() {
header('Phase 8c — Matcher-Based Hook Filtering');
// 1. matchGlob — exact match
{
assert(matchGlob('hello', 'hello'), 'matchGlob: exact match');
}
// 2. matchGlob — star wildcard
{
assert(matchGlob('agent-*', 'agent-research'), 'matchGlob: agent-* matches agent-research');
assert(!matchGlob('agent-*', 'other-agent'), 'matchGlob: agent-* does not match other-agent');
}
// 3. matchGlob — question mark wildcard
{
assert(matchGlob('v?.0', 'v1.0'), 'matchGlob: v?.0 matches v1.0');
assert(!matchGlob('v?.0', 'v12.0'), 'matchGlob: v?.0 does not match v12.0');
}
// 4. matchGlob — case insensitive
{
assert(matchGlob('AGENT', 'agent'), 'matchGlob: case insensitive');
}
// 5. matchGlob — special regex chars escaped
{
assert(matchGlob('file.ts', 'file.ts'), 'matchGlob: dot is literal');
assert(!matchGlob('file.ts', 'filexts'), 'matchGlob: dot is not regex any-char');
}
// 6. matchToolPattern — tool with args
{
assert(matchToolPattern('Bash(git *)', 'Bash(git push)'), 'matchToolPattern: Bash(git push) matches');
assert(!matchToolPattern('Bash(git *)', 'Bash(npm install)'), 'matchToolPattern: Bash(npm install) does not match');
}
// 7. matchToolPattern — tool name only
{
assert(matchToolPattern('Bash', 'Bash'), 'matchToolPattern: bare tool name');
}
// 8. matchToolPattern — wildcard tool name
{
assert(matchToolPattern('*(*.env)', 'Edit(.env)'), 'matchToolPattern: *(*.env) matches Edit(.env)');
}
// 9. Hook with agentPattern matcher — fires for matching agent
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'agent-filter',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { agentPattern: 'scan-*' },
});
const ctx1 = hooks.createContext('scan-vuln', { action: 'run', params: {} }, baseCtx);
await hooks.runBefore(ctx1);
assert(fired, 'Hook fires for matching agent pattern');
}
// 10. Hook with agentPattern matcher — skipped for non-matching agent
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'agent-filter2',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { agentPattern: 'scan-*' },
});
const ctx1 = hooks.createContext('deploy-agent', { action: 'run', params: {} }, baseCtx);
await hooks.runBefore(ctx1);
assert(!fired, 'Hook skipped for non-matching agent');
}
// 11. Hook with actionPattern matcher
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'action-filter',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { actionPattern: 'deploy*' },
});
const ctx1 = hooks.createContext('agent', { action: 'deploy-prod', params: {} }, baseCtx);
await hooks.runBefore(ctx1);
assert(fired, 'Hook fires for matching action pattern');
}
// 12. Hook with toolPattern matcher
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'tool-filter',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { toolPattern: 'Bash(git *)' },
});
const ctx1 = hooks.createContext('agent', { action: 'run', params: {} }, baseCtx);
ctx1.metadata.tool = 'Bash(git push)';
await hooks.runBefore(ctx1);
assert(fired, 'Hook fires for matching tool pattern');
}
// 13. Hook with condition function
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'cond-filter',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { condition: (ctx) => ctx.payload.params?.urgent === true },
});
const ctx1 = hooks.createContext('agent', { action: 'run', params: { urgent: true } }, baseCtx);
await hooks.runBefore(ctx1);
assert(fired, 'Hook fires when condition returns true');
}
// 14. Hook with condition — skipped when false
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'cond-filter2',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { condition: (ctx) => ctx.payload.params?.urgent === true },
});
const ctx1 = hooks.createContext('agent', { action: 'run', params: { urgent: false } }, baseCtx);
await hooks.runBefore(ctx1);
assert(!fired, 'Hook skipped when condition returns false');
}
// 15. Combined matchers — all must pass (AND logic)
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'combo',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { agentPattern: 'scan-*', actionPattern: 'deploy*' },
});
// Agent matches, action doesn't
const ctx1 = hooks.createContext('scan-vuln', { action: 'test', params: {} }, baseCtx);
await hooks.runBefore(ctx1);
assert(!fired, 'AND logic: agent matches but action does not → skipped');
// Both match
fired = false;
const ctx2 = hooks.createContext('scan-prod', { action: 'deploy-now', params: {} }, baseCtx);
await hooks.runBefore(ctx2);
assert(fired, 'AND logic: both agent and action match → fires');
}
// 16. Hooks without matcher always fire
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'no-matcher',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
});
const ctx1 = hooks.createContext('anything', { action: 'whatever', params: {} }, baseCtx);
await hooks.runBefore(ctx1);
assert(fired, 'Hook without matcher always fires');
}
// 17. Matcher on afterExecute phase
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'after-match',
phase: 'afterExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { agentPattern: 'special-*' },
});
const ctx1 = hooks.createContext('special-agent', { action: 'run', params: {} }, baseCtx);
ctx1.result = { success: true };
await hooks.runAfter(ctx1);
assert(fired, 'Matcher works on afterExecute phase');
}
// 18. Matcher on onError phase
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'error-match',
phase: 'onError',
handler: (ctx) => { fired = true; return ctx; },
matcher: { agentPattern: 'critical-*' },
});
const ctx1 = hooks.createContext('critical-db', { action: 'query', params: {} }, baseCtx);
ctx1.error = new Error('boom');
await hooks.runOnError(ctx1);
assert(fired, 'Matcher works on onError phase');
}
// 19. Tool pattern with no args part in context
{
const hooks = new AdapterHookManager();
let fired = false;
hooks.register({
name: 'tool-no-args',
phase: 'beforeExecute',
handler: (ctx) => { fired = true; return ctx; },
matcher: { toolPattern: 'Bash(git *)' },
});
const ctx1 = hooks.createContext('agent', { action: 'run', params: {} }, baseCtx);
ctx1.metadata.tool = 'Bash'; // no args
await hooks.runBefore(ctx1);
assert(!fired, 'Tool pattern with args does not match bare tool');
}
// 20. Priority ordering still respected with matchers
{
const hooks = new AdapterHookManager();
const order: string[] = [];
hooks.register({
name: 'low-prio',
phase: 'beforeExecute',
priority: 1,
handler: (ctx) => { order.push('low'); return ctx; },
matcher: { agentPattern: '*' },
});
hooks.register({
name: 'high-prio',
phase: 'beforeExecute',
priority: 10,
handler: (ctx) => { order.push('high'); return ctx; },
matcher: { agentPattern: '*' },
});
const ctx1 = hooks.createContext('agent', { action: 'run', params: {} }, baseCtx);
await hooks.runBefore(ctx1);
assert(order[0] === 'high' && order[1] === 'low', 'Priority ordering preserved with matchers');
}
// 21. matchGlob — full wildcard
{
assert(matchGlob('*', 'anything'), 'matchGlob: * matches anything');
}
}
// ============================================================================
// 8d — FAN-OUT / FAN-IN PARALLEL AGGREGATION
// ============================================================================
async function testFanOutFanIn() {
header('Phase 8d — Fan-Out / Fan-In Parallel Aggregation');
// 1. Basic fan-out executes all steps
{
const { registry, adapter } = await makeRegistry();
const fanout = new FanOutFanIn(registry, baseCtx);
const steps: FanOutStep[] = [
{ agentId: 'a', payload: { action: 'search', params: {} } },
{ agentId: 'b', payload: { action: 'search', params: {} } },
{ agentId: 'c', payload: { action: 'search', params: {} } },
];
const results = await fanout.fanOut(steps);
assert(results.length === 3, 'Fan-out returns 3 results');
assert(adapter.callCount === 3, 'All 3 agents executed');
}
// 2. Results are tagged with index and agentId
{
const { registry } = await makeRegistry();
const fanout = new FanOutFanIn(registry, baseCtx);
const results = await fanout.fanOut([
{ agentId: 'agent-x', payload: { action: 'go', params: {} }, label: 'web' },
]);
assert(results[0].agentId === 'agent-x', 'Tagged with agentId');
assert(results[0].label === 'web', 'Tagged with label');
assert(results[0].index === 0, 'Tagged with index');
}
// 3. durationMs is tracked per result
{
const { registry } = await makeRegistry();
const fanout = new FanOutFanIn(registry, baseCtx);
const results = await fanout.fanOut([
{ agentId: 'a', payload: { action: 'go', params: {} } },
]);
assert(results[0].durationMs >= 0, 'durationMs is non-negative');
}
// 4. Concurrency limit chunks execution
{
const { registry, adapter } = await makeRegistry();
const fanout = new FanOutFanIn(registry, baseCtx);
const steps: FanOutStep[] = Array.from({ length: 6 }, (_, i) => ({
agentId: `agent-${i}`,
payload: { action: 'go', params: {} },
}));
const results = await fanout.fanOut(steps, { concurrency: 2 });
assert(results.length === 6, 'All 6 results returned with concurrency=2');
assert(adapter.callCount === 6, 'All 6 agents executed');
}
// 5. continueOnError=true (default) — continues past failures
{
const registry = new AdapterRegistry();
await registry.addAdapter(new MockAdapter('mock'));
await registry.addAdapter(new FailingAdapter());
registry.addRoute({ pattern: 'fail:*', adapterName: 'failing' });
registry.setDefaultAdapter('mock');
const fanout = new FanOutFanIn(registry, baseCtx);
const steps: FanOutStep[] = [
{ agentId: 'ok-agent', payload: { action: 'go', params: {} } },
{ agentId: 'fail:agent', payload: { action: 'go', params: {} } },
{ agentId: 'ok-agent2', payload: { action: 'go', params: {} } },
];
const results = await fanout.fanOut(steps);
assert(results.length === 3, 'All 3 results returned despite failure');
assert(!results[1].result.success, 'Second result is a failure');
assert(results[2].result.success, 'Third result still succeeds');
}
// 6. continueOnError=false — stops on first failure
{
const registry = new AdapterRegistry();
await registry.addAdapter(new MockAdapter('mock'));