-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-executor.test.js
More file actions
1116 lines (865 loc) · 32 KB
/
agent-executor.test.js
File metadata and controls
1116 lines (865 loc) · 32 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
/**
* Agent Executor Tests
*
* Comprehensive test suite for the AgentExecutor class covering:
* - Constructor and initialization
* - Callback system (setCallbacks, clearCallbacks, _invokeCallback)
* - Error categorization (transient, permanent, timeout)
* - Retry logic with exponential backoff
* - Fallback model switching
* - Session management
* - Metrics tracking
* - Template loading and rendering
* - CLI argument building
* - Output parsing
*/
import { describe, it, beforeEach, afterEach, mock } from 'node:test';
import assert from 'node:assert';
import { AgentExecutor } from './agent-executor.js';
import fs from 'fs';
import path from 'path';
import os from 'os';
describe('AgentExecutor - Constructor and Initialization', () => {
it('should create instance with default options', () => {
const executor = new AgentExecutor();
assert.strictEqual(executor.options.claudePath, 'claude');
assert.strictEqual(executor.options.timeout, 10 * 60 * 1000);
assert.strictEqual(executor.options.skipPermissions, true);
assert.strictEqual(executor.options.verbose, false);
assert.strictEqual(executor.options.maxRetries, 3);
assert.strictEqual(executor.options.retryBaseDelay, 1000);
});
it('should create instance with custom options', () => {
const executor = new AgentExecutor({
claudePath: '/custom/claude',
timeout: 5000,
skipPermissions: false,
verbose: true,
maxRetries: 5,
retryBaseDelay: 500
});
assert.strictEqual(executor.options.claudePath, '/custom/claude');
assert.strictEqual(executor.options.timeout, 5000);
assert.strictEqual(executor.options.skipPermissions, false);
assert.strictEqual(executor.options.verbose, true);
assert.strictEqual(executor.options.maxRetries, 5);
assert.strictEqual(executor.options.retryBaseDelay, 500);
});
it('should initialize with empty sessions', () => {
const executor = new AgentExecutor();
assert.deepStrictEqual(executor.sessions, {});
});
it('should initialize with empty template cache', () => {
const executor = new AgentExecutor();
assert.deepStrictEqual(executor.templateCache, {});
});
it('should initialize with zero metrics', () => {
const executor = new AgentExecutor();
assert.strictEqual(executor.metrics.totalCalls, 0);
assert.strictEqual(executor.metrics.totalRetries, 0);
assert.strictEqual(executor.metrics.totalFallbacks, 0);
assert.strictEqual(executor.metrics.totalCostUsd, 0);
assert.deepStrictEqual(executor.metrics.callsByAgent, {});
});
it('should initialize with null callbacks', () => {
const executor = new AgentExecutor();
assert.strictEqual(executor.callbacks.onStart, null);
assert.strictEqual(executor.callbacks.onComplete, null);
assert.strictEqual(executor.callbacks.onError, null);
assert.strictEqual(executor.callbacks.onRetry, null);
assert.strictEqual(executor.callbacks.onFallback, null);
assert.strictEqual(executor.callbacks.onStdout, null);
assert.strictEqual(executor.callbacks.onStderr, null);
});
});
describe('AgentExecutor - Callback System', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor();
});
it('should set callbacks with setCallbacks', () => {
const onStart = () => {};
const onComplete = () => {};
executor.setCallbacks({ onStart, onComplete });
assert.strictEqual(executor.callbacks.onStart, onStart);
assert.strictEqual(executor.callbacks.onComplete, onComplete);
});
it('should merge callbacks without overwriting unset ones', () => {
const onStart = () => {};
const onError = () => {};
executor.setCallbacks({ onStart });
executor.setCallbacks({ onError });
assert.strictEqual(executor.callbacks.onStart, onStart);
assert.strictEqual(executor.callbacks.onError, onError);
});
it('should clear all callbacks with clearCallbacks', () => {
executor.setCallbacks({
onStart: () => {},
onComplete: () => {},
onError: () => {}
});
executor.clearCallbacks();
assert.strictEqual(executor.callbacks.onStart, null);
assert.strictEqual(executor.callbacks.onComplete, null);
assert.strictEqual(executor.callbacks.onError, null);
});
it('should invoke callback when set', () => {
let called = false;
let receivedData = null;
executor.setCallbacks({
onStart: (data) => {
called = true;
receivedData = data;
}
});
executor._invokeCallback('onStart', { agentName: 'test', prompt: 'hello' });
assert.strictEqual(called, true);
assert.deepStrictEqual(receivedData, { agentName: 'test', prompt: 'hello' });
});
it('should not throw when invoking non-existent callback', () => {
assert.doesNotThrow(() => {
executor._invokeCallback('onStart', { agentName: 'test' });
});
});
it('should catch and suppress callback errors', () => {
executor.setCallbacks({
onStart: () => {
throw new Error('Callback error');
}
});
// Should not throw
assert.doesNotThrow(() => {
executor._invokeCallback('onStart', { agentName: 'test' });
});
});
});
describe('AgentExecutor - Error Categorization', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor();
});
describe('Timeout Errors', () => {
it('should categorize "timed out" as TIMEOUT', () => {
const result = executor.categorizeError(new Error('Request timed out'));
assert.strictEqual(result, 'TIMEOUT');
});
it('should categorize "timeout" as TIMEOUT', () => {
const result = executor.categorizeError('Connection timeout occurred');
assert.strictEqual(result, 'TIMEOUT');
});
});
describe('Transient Errors', () => {
it('should categorize ECONNRESET as TRANSIENT (case-insensitive)', () => {
// categorizeError lowercases error strings for case-insensitive matching
const result = executor.categorizeError(new Error('ECONNRESET'));
assert.strictEqual(result, 'TRANSIENT');
});
it('should categorize ETIMEDOUT as TRANSIENT (case-insensitive)', () => {
const result = executor.categorizeError(new Error('ETIMEDOUT'));
assert.strictEqual(result, 'TRANSIENT');
});
it('should categorize overloaded as TRANSIENT', () => {
const result = executor.categorizeError('Server overloaded');
assert.strictEqual(result, 'TRANSIENT');
});
it('should categorize rate_limit as TRANSIENT', () => {
const result = executor.categorizeError('rate_limit exceeded');
assert.strictEqual(result, 'TRANSIENT');
});
it('should categorize 529 as TRANSIENT', () => {
const result = executor.categorizeError('HTTP 529 error');
assert.strictEqual(result, 'TRANSIENT');
});
it('should categorize 503 as TRANSIENT', () => {
const result = executor.categorizeError('HTTP 503 Service Unavailable');
assert.strictEqual(result, 'TRANSIENT');
});
});
describe('Permanent Errors', () => {
it('should categorize invalid_api_key as PERMANENT', () => {
const result = executor.categorizeError('invalid_api_key');
assert.strictEqual(result, 'PERMANENT');
});
it('should categorize permission_denied as PERMANENT', () => {
const result = executor.categorizeError(new Error('permission_denied'));
assert.strictEqual(result, 'PERMANENT');
});
it('should categorize invalid_request as PERMANENT', () => {
const result = executor.categorizeError('invalid_request format');
assert.strictEqual(result, 'PERMANENT');
});
});
describe('Unknown Errors', () => {
it('should categorize generic error as UNKNOWN', () => {
const result = executor.categorizeError(new Error('Something went wrong'));
assert.strictEqual(result, 'UNKNOWN');
});
it('should categorize empty error as UNKNOWN', () => {
const result = executor.categorizeError('');
assert.strictEqual(result, 'UNKNOWN');
});
});
});
describe('AgentExecutor - Session Management', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor();
});
it('should return null for non-existent session', () => {
assert.strictEqual(executor.getSessionId('unknown-agent'), null);
});
it('should report no session for unknown agent', () => {
assert.strictEqual(executor.hasSession('unknown-agent'), false);
});
it('should store and retrieve session ID', () => {
executor.sessions['test-agent'] = 'session-123';
assert.strictEqual(executor.getSessionId('test-agent'), 'session-123');
assert.strictEqual(executor.hasSession('test-agent'), true);
});
it('should reset individual session', () => {
executor.sessions['agent1'] = 'session-1';
executor.sessions['agent2'] = 'session-2';
executor.resetSession('agent1');
assert.strictEqual(executor.hasSession('agent1'), false);
assert.strictEqual(executor.hasSession('agent2'), true);
});
it('should reset all sessions', () => {
executor.sessions['agent1'] = 'session-1';
executor.sessions['agent2'] = 'session-2';
executor.sessions['agent3'] = 'session-3';
executor.resetAllSessions();
assert.deepStrictEqual(executor.sessions, {});
});
});
describe('AgentExecutor - Metrics', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor();
});
it('should return copy of metrics', () => {
executor.metrics.totalCalls = 5;
executor.metrics.totalRetries = 2;
const metrics = executor.getMetrics();
assert.strictEqual(metrics.totalCalls, 5);
assert.strictEqual(metrics.totalRetries, 2);
// Verify it's a copy
metrics.totalCalls = 100;
assert.strictEqual(executor.metrics.totalCalls, 5);
});
it('should reset metrics to zero', () => {
executor.metrics.totalCalls = 10;
executor.metrics.totalRetries = 5;
executor.metrics.totalFallbacks = 2;
executor.metrics.totalCostUsd = 1.50;
executor.metrics.callsByAgent = { planner: 5, coder: 3 };
executor.resetMetrics();
assert.strictEqual(executor.metrics.totalCalls, 0);
assert.strictEqual(executor.metrics.totalRetries, 0);
assert.strictEqual(executor.metrics.totalFallbacks, 0);
assert.strictEqual(executor.metrics.totalCostUsd, 0);
assert.deepStrictEqual(executor.metrics.callsByAgent, {});
});
});
describe('AgentExecutor - Template Management', () => {
let executor;
let tempDir;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'executor-test-'));
executor = new AgentExecutor({ templatesDir: tempDir });
});
afterEach(() => {
// Clean up temp directory
if (fs.existsSync(tempDir)) {
fs.rmSync(tempDir, { recursive: true });
}
});
it('should throw error for non-existent template', () => {
assert.throws(() => {
executor.loadTemplate('nonexistent.hbs');
}, /Template not found/);
});
it('should load and compile template', () => {
const templatePath = path.join(tempDir, 'test.hbs');
fs.writeFileSync(templatePath, 'Hello {{name}}!');
const template = executor.loadTemplate('test.hbs');
assert.strictEqual(typeof template, 'function');
assert.strictEqual(template({ name: 'World' }), 'Hello World!');
});
it('should cache compiled templates', () => {
const templatePath = path.join(tempDir, 'cached.hbs');
fs.writeFileSync(templatePath, 'Template content');
const template1 = executor.loadTemplate('cached.hbs');
const template2 = executor.loadTemplate('cached.hbs');
assert.strictEqual(template1, template2);
});
it('should render template with context', () => {
const templatePath = path.join(tempDir, 'render.hbs');
fs.writeFileSync(templatePath, 'Task: {{task}}, Goal: {{goal}}');
const result = executor.renderTemplate('render.hbs', {
task: 'Write tests',
goal: '100% coverage'
});
assert.strictEqual(result, 'Task: Write tests, Goal: 100% coverage');
});
it('should clear template cache', () => {
const templatePath = path.join(tempDir, 'clear.hbs');
fs.writeFileSync(templatePath, 'Original');
executor.loadTemplate('clear.hbs');
assert.ok(Object.keys(executor.templateCache).length > 0);
executor.clearTemplateCache();
assert.deepStrictEqual(executor.templateCache, {});
});
});
describe('AgentExecutor - Tool Schema Building', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor();
});
it('should return null for empty tools array', () => {
assert.strictEqual(executor.buildToolSchema([]), null);
});
it('should return null for undefined tools', () => {
assert.strictEqual(executor.buildToolSchema(undefined), null);
});
it('should build schema from tools with name property', () => {
const tools = [
{ name: 'implementationComplete' },
{ name: 'fixComplete' }
];
const schema = executor.buildToolSchema(tools);
assert.strictEqual(schema.type, 'object');
assert.ok(schema.properties.toolCall);
assert.deepStrictEqual(schema.properties.toolCall.properties.name.enum, [
'implementationComplete',
'fixComplete'
]);
});
it('should build schema from tools with key as name', () => {
const tools = [
{ myTool: { description: 'A tool' } },
{ anotherTool: { description: 'Another tool' } }
];
const schema = executor.buildToolSchema(tools);
assert.deepStrictEqual(schema.properties.toolCall.properties.name.enum, [
'myTool',
'anotherTool'
]);
});
});
describe('AgentExecutor - CLI Argument Building', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor();
});
it('should build basic arguments', () => {
const args = executor._buildArgs('agent', 'test prompt', {});
assert.ok(args.includes('--print'));
assert.ok(args.includes('-p'));
assert.ok(args.includes('test prompt'));
assert.ok(args.includes('--output-format'));
assert.ok(args.includes('json'));
assert.ok(args.includes('--dangerously-skip-permissions'));
});
it('should include model when specified', () => {
const args = executor._buildArgs('agent', 'prompt', { model: 'opus' });
assert.ok(args.includes('--model'));
assert.ok(args.includes('opus'));
});
it('should include fallback model when specified', () => {
const args = executor._buildArgs('agent', 'prompt', { fallbackModel: 'haiku' });
assert.ok(args.includes('--fallback-model'));
assert.ok(args.includes('haiku'));
});
it('should not include fallback when _usingFallback is true', () => {
const args = executor._buildArgs('agent', 'prompt', {
fallbackModel: 'haiku',
_usingFallback: true
});
assert.ok(!args.includes('--fallback-model'));
});
it('should include session resume when session exists', () => {
executor.sessions['agent'] = 'session-xyz';
const args = executor._buildArgs('agent', 'prompt', {});
assert.ok(args.includes('--resume'));
assert.ok(args.includes('session-xyz'));
});
it('should not include session resume when newSession is true', () => {
executor.sessions['agent'] = 'session-xyz';
const args = executor._buildArgs('agent', 'prompt', { newSession: true });
assert.ok(!args.includes('--resume'));
});
it('should include max turns when specified', () => {
const args = executor._buildArgs('agent', 'prompt', { maxTurns: 10 });
assert.ok(args.includes('--max-turns'));
assert.ok(args.includes('10'));
});
it('should include tools when specified as array', () => {
const args = executor._buildArgs('agent', 'prompt', { tools: ['Read', 'Write'] });
assert.ok(args.includes('--tools'));
assert.ok(args.includes('Read,Write'));
});
it('should include tools when specified as string', () => {
const args = executor._buildArgs('agent', 'prompt', { tools: 'Read,Write' });
assert.ok(args.includes('--tools'));
assert.ok(args.includes('Read,Write'));
});
it('should include allowed tools when specified', () => {
const args = executor._buildArgs('agent', 'prompt', { allowedTools: ['Bash', 'Read'] });
assert.ok(args.includes('--allowed-tools'));
assert.ok(args.includes('Bash Read'));
});
it('should include disallowed tools when specified', () => {
const args = executor._buildArgs('agent', 'prompt', { disallowedTools: ['Write'] });
assert.ok(args.includes('--disallowed-tools'));
});
it('should include system prompt when specified', () => {
const args = executor._buildArgs('agent', 'prompt', { systemPrompt: 'You are a helpful assistant' });
assert.ok(args.includes('--system-prompt'));
assert.ok(args.includes('You are a helpful assistant'));
});
it('should include append system prompt when specified', () => {
const args = executor._buildArgs('agent', 'prompt', { appendSystemPrompt: 'Additional context' });
assert.ok(args.includes('--append-system-prompt'));
assert.ok(args.includes('Additional context'));
});
it('should include JSON schema when specified as object', () => {
const schema = { type: 'object', properties: { name: { type: 'string' } } };
const args = executor._buildArgs('agent', 'prompt', { jsonSchema: schema });
assert.ok(args.includes('--json-schema'));
const schemaIndex = args.indexOf('--json-schema');
const schemaStr = args[schemaIndex + 1];
assert.deepStrictEqual(JSON.parse(schemaStr), schema);
});
it('should include JSON schema when specified as string', () => {
const schemaStr = '{"type":"object"}';
const args = executor._buildArgs('agent', 'prompt', { jsonSchema: schemaStr });
assert.ok(args.includes('--json-schema'));
assert.ok(args.includes(schemaStr));
});
it('should use text output format when specified', () => {
const args = executor._buildArgs('agent', 'prompt', { outputFormat: 'text' });
assert.ok(!args.includes('--output-format'));
});
it('should not include --dangerously-skip-permissions when skipPermissions is false', () => {
executor = new AgentExecutor({ skipPermissions: false });
const args = executor._buildArgs('agent', 'prompt', {});
assert.ok(!args.includes('--dangerously-skip-permissions'));
});
});
describe('AgentExecutor - Output Parsing', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor();
});
it('should parse JSON output with session_id', () => {
const output = JSON.stringify({
result: 'Hello world',
session_id: 'session-abc'
});
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.response, 'Hello world');
assert.strictEqual(result.sessionId, 'session-abc');
assert.strictEqual(executor.sessions['agent'], 'session-abc');
});
it('should parse JSON output with cost info', () => {
const output = JSON.stringify({
result: 'Response',
total_cost_usd: 0.05,
duration_ms: 1500,
num_turns: 2
});
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.costUsd, 0.05);
assert.strictEqual(result.duration, 1500);
assert.strictEqual(result.numTurns, 2);
});
it('should parse JSON output with usage info', () => {
const output = JSON.stringify({
result: 'Response',
usage: {
input_tokens: 100,
output_tokens: 50
}
});
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.tokensIn, 100);
assert.strictEqual(result.tokensOut, 50);
});
it('should parse JSON output with structured_output and toolCall', () => {
const output = JSON.stringify({
result: 'Done',
structured_output: {
toolCall: {
name: 'implementationComplete',
arguments: { status: 'complete' }
}
}
});
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.ok(result.structuredOutput);
assert.strictEqual(result.toolCalls.length, 1);
assert.strictEqual(result.toolCalls[0].name, 'implementationComplete');
});
it('should parse JSON output with multiple toolCalls', () => {
const output = JSON.stringify({
result: 'Done',
structured_output: {
toolCalls: [
{ name: 'tool1', arguments: {} },
{ name: 'tool2', arguments: {} }
]
}
});
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.toolCalls.length, 2);
});
it('should fallback to text parsing for non-JSON', () => {
const output = 'Plain text response';
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.response, 'Plain text response');
});
it('should extract session ID from text output', () => {
const output = 'Response text\nsession_id: sess-12345\nMore text';
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.sessionId, 'sess-12345');
assert.strictEqual(executor.sessions['agent'], 'sess-12345');
});
it('should handle malformed JSON gracefully', () => {
const output = '{ invalid json }';
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.response, '{ invalid json }');
});
it('should prefer result field over response field', () => {
const output = JSON.stringify({
result: 'from result',
response: 'from response'
});
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.response, 'from result');
});
it('should fallback to content field', () => {
const output = JSON.stringify({
content: 'from content'
});
const result = executor._parseOutput('agent', output, 'prompt', {});
assert.strictEqual(result.response, 'from content');
});
});
describe('AgentExecutor - Sleep Helper', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor();
});
it('should delay for specified time', async () => {
const start = Date.now();
await executor.sleep(50);
const elapsed = Date.now() - start;
assert.ok(elapsed >= 45); // Allow some tolerance
assert.ok(elapsed < 200); // Increased tolerance for CI/slow environments
});
});
describe('AgentExecutor - Execute Flow Callbacks', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor({ maxRetries: 0 });
});
it('should invoke onStart callback at execution start', async () => {
let startData = null;
executor.setCallbacks({
onStart: (data) => {
startData = data;
}
});
// Mock _executeOnce to return immediately
executor._executeOnce = async () => ({ response: 'test' });
await executor.execute('agent', 'test prompt', {});
assert.ok(startData);
assert.strictEqual(startData.agentName, 'agent');
assert.strictEqual(startData.prompt, 'test prompt');
});
it('should invoke onComplete callback on successful execution', async () => {
let completeData = null;
executor.setCallbacks({
onComplete: (data) => {
completeData = data;
}
});
executor._executeOnce = async () => ({ response: 'success' });
await executor.execute('agent', 'prompt', {});
assert.ok(completeData);
assert.strictEqual(completeData.agentName, 'agent');
assert.strictEqual(completeData.result.response, 'success');
});
it('should invoke onError callback on permanent error', async () => {
let errorData = null;
executor.setCallbacks({
onError: (data) => {
errorData = data;
}
});
executor._executeOnce = async () => {
throw new Error('invalid_api_key');
};
try {
await executor.execute('agent', 'prompt', {});
} catch (e) {
// Expected
}
assert.ok(errorData);
assert.strictEqual(errorData.agentName, 'agent');
assert.ok(errorData.error.message.includes('invalid_api_key'));
});
it('should track metrics on successful execution', async () => {
executor._executeOnce = async () => ({ response: 'test' });
await executor.execute('agent', 'prompt', {});
assert.strictEqual(executor.metrics.totalCalls, 1);
assert.strictEqual(executor.metrics.callsByAgent['agent'], 1);
});
});
describe('AgentExecutor - Retry Logic', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor({
maxRetries: 2,
retryBaseDelay: 10 // Short delay for tests
});
});
it('should retry on transient error', async () => {
let attempts = 0;
let retryCallbackCount = 0;
executor.setCallbacks({
onRetry: () => {
retryCallbackCount++;
}
});
executor._executeOnce = async () => {
attempts++;
if (attempts < 2) {
throw new Error('ECONNRESET');
}
return { response: 'success after retry' };
};
const result = await executor.execute('agent', 'prompt', {});
assert.strictEqual(result.response, 'success after retry');
assert.strictEqual(attempts, 2);
assert.strictEqual(retryCallbackCount, 1);
assert.strictEqual(executor.metrics.totalRetries, 1);
});
it('should retry on timeout error', async () => {
let attempts = 0;
executor._executeOnce = async () => {
attempts++;
if (attempts < 2) {
throw new Error('Request timed out');
}
return { response: 'success' };
};
const result = await executor.execute('agent', 'prompt', {});
assert.strictEqual(result.response, 'success');
assert.strictEqual(attempts, 2);
});
it('should not retry on permanent error', async () => {
let attempts = 0;
executor._executeOnce = async () => {
attempts++;
throw new Error('invalid_api_key');
};
try {
await executor.execute('agent', 'prompt', {});
assert.fail('Should have thrown');
} catch (e) {
assert.ok(e.message.includes('invalid_api_key'));
}
assert.strictEqual(attempts, 1);
});
it('should fail after max retries exceeded', async () => {
let attempts = 0;
executor._executeOnce = async () => {
attempts++;
throw new Error('overloaded');
};
try {
await executor.execute('agent', 'prompt', {});
assert.fail('Should have thrown');
} catch (e) {
assert.ok(e.message.includes('overloaded'));
}
// Initial attempt + maxRetries
assert.strictEqual(attempts, 3);
});
it('should invoke onRetry with correct data', async () => {
let retryData = null;
executor.setCallbacks({
onRetry: (data) => {
retryData = data;
}
});
let attempts = 0;
executor._executeOnce = async () => {
attempts++;
if (attempts < 2) {
throw new Error('rate_limit exceeded');
}
return { response: 'ok' };
};
await executor.execute('agent', 'prompt', {});
assert.ok(retryData);
assert.strictEqual(retryData.agentName, 'agent');
assert.strictEqual(retryData.attempt, 1);
assert.strictEqual(retryData.maxRetries, 2);
assert.ok(retryData.delay > 0);
assert.ok(retryData.error.includes('rate_limit'));
assert.strictEqual(retryData.category, 'TRANSIENT');
});
});
describe('AgentExecutor - Fallback Model', () => {
let executor;
beforeEach(() => {
executor = new AgentExecutor({
maxRetries: 3,
retryBaseDelay: 10
});
});
it('should switch to fallback model after 2 retries', async () => {
let fallbackData = null;
let attempts = 0;
let modelUsed = null;
executor.setCallbacks({
onFallback: (data) => {
fallbackData = data;
}
});
executor._executeOnce = async (agentName, prompt, options) => {
attempts++;
modelUsed = options.model;
if (attempts < 4) {
throw new Error('overloaded');
}
return { response: 'success with fallback' };
};
const result = await executor.execute('agent', 'prompt', {
model: 'opus',
fallbackModel: 'haiku'
});
assert.strictEqual(result.response, 'success with fallback');
assert.ok(fallbackData);
assert.strictEqual(fallbackData.agentName, 'agent');
assert.strictEqual(fallbackData.model, 'haiku');
assert.strictEqual(modelUsed, 'haiku');
assert.strictEqual(executor.metrics.totalFallbacks, 1);
});
it('should not switch to fallback on first retry', async () => {
let attempts = 0;
let modelUsed = null;
executor._executeOnce = async (agentName, prompt, options) => {
attempts++;
modelUsed = options.model;
if (attempts < 2) {
throw new Error('overloaded');
}
return { response: 'success' };
};
await executor.execute('agent', 'prompt', {
model: 'opus',
fallbackModel: 'haiku'
});
// Should still be using opus after first retry
assert.strictEqual(modelUsed, 'opus');
});
it('should not fallback when no fallback model specified', async () => {
let attempts = 0;
let modelUsed = null;
executor._executeOnce = async (agentName, prompt, options) => {
attempts++;
modelUsed = options.model;
if (attempts < 3) {
throw new Error('overloaded');
}
return { response: 'success' };