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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
import { describe, it, expect, vi } from 'vitest';
import { AgentLoop } from '../core/agent-loop';
import type {
  ProviderAdapter,
  ToolExecutor,
  ContextManager,
  ProgressReporter,
  CostTracker,
  AgentLoopConfig,
  ParsedResponse,
  ToolResult,
  ToolCall,
  ToolDef,
  Message,
  UsageInfo,
} from '../core/types';

// --- Mock factories ---

function createMockProvider(responses: ParsedResponse[]): ProviderAdapter {
  let callIndex = 0;
  return {
    call: vi.fn(async () => {
      if (callIndex >= responses.length) {
        return { content: '' };
      }
      return responses[callIndex++];
    }),
    getModel: () => 'test-model',
    getProvider: () => 'test-provider',
    supportsTools: () => true,
  };
}

function createMockExecutor(results?: Map<string, ToolResult>): ToolExecutor {
  return {
    execute: vi.fn(async (toolCall: ToolCall) => {
      if (results && results.has(toolCall.id)) {
        return results.get(toolCall.id)!;
      }
      return {
        tool_call_id: toolCall.id,
        content: 'OK',
        success: true,
      };
    }),
    getDefinitions: () => [{ name: 'bash', description: 'Run shell', parameters: {} }] as ToolDef[],
  };
}

function createMockContext(): ContextManager {
  const messages: Message[] = [];
  return {
    getMessages: () => messages,
    getSanitizedMessages: () => messages,
    setSystemPrompt: vi.fn((prompt: string) => {
      if (messages.length > 0 && messages[0].role === 'system') {
        messages[0] = { role: 'system', content: prompt };
      } else {
        messages.unshift({ role: 'system', content: prompt });
      }
    }),
    addUserMessage: vi.fn((content: string) => {
      messages.push({ role: 'user', content });
    }),
    addAssistantTurn: vi.fn((response: ParsedResponse) => {
      messages.push({
        role: 'assistant',
        content: response.content || '',
        ...(response.toolCalls?.length ? { tool_calls: response.toolCalls } : {}),
      });
    }),
    addToolResults: vi.fn((results: ToolResult[]) => {
      for (const r of results) {
        messages.push({ role: 'tool', content: r.content, tool_call_id: r.tool_call_id });
      }
    }),
    importMessages: vi.fn(),
    needsCompaction: () => false,
    compact: vi.fn(async () => undefined),
    getTokenEstimate: () => 1000,
    getCompactionCount: () => 0,
  };
}

function createMockProgress(): ProgressReporter {
  return { onEvent: vi.fn() };
}

function createMockCost(): CostTracker {
  return {
    record: vi.fn(),
    getTurnCost: () => 0,
    getTotalCost: () => 0,
    getTotalUsage: () => ({ promptTokens: 0, completionTokens: 0, totalTokens: 0 }),
    resetTurn: vi.fn(),
  };
}

function defaultConfig(overrides?: Partial<AgentLoopConfig>): AgentLoopConfig {
  return {
    maxIterations: 20,
    maxNudges: 2,
    maxDuplicateToolCalls: 3,
    agentType: 'orchestrator',
    isReadOnly: false,
    ...overrides,
  };
}

// --- Tests ---

describe('AgentLoop', () => {
  it('completes when model returns statusComplete signal', async () => {
    // 1. Provider returns a tool call, 2. Provider returns text (after status signal)
    const toolCall: ToolCall = {
      id: 'tc1',
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"status --task \\"build site\\" --done \\"all\\" --remaining \\"none\\" --complete"}' },
    };

    const provider = createMockProvider([
      { content: '', toolCalls: [toolCall] },
      { content: 'All done!' },
    ]);

    const toolResult: ToolResult = {
      tool_call_id: 'tc1',
      content: 'Status recorded.',
      success: true,
      signals: { statusComplete: true, statusResult: { task: 'build site', done: 'all', remaining: 'none', complete: true, hasExplicitFlag: true } },
    };
    const results = new Map([['tc1', toolResult]]);
    const executor = createMockExecutor(results);
    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig();

    const loop = new AgentLoop({
      config,
      provider,
      executor,
      context,
      progress,
      cost,
    });

    const result = await loop.run('Build me a site');
    expect(result.success).toBe(true);
    expect(result.toolCount).toBe(1);
    expect(result.turnCount).toBeGreaterThanOrEqual(1);
  });

  it('stops after maxNudges when model returns text without tool calls', async () => {
    // Model always returns text, never tools — should nudge then stop
    const provider = createMockProvider([
      { content: 'Let me think about that...' },
      { content: 'Still thinking...' },
      { content: 'Almost there...' },
      { content: 'One more thought...' },
      { content: 'Final thought.' },
    ]);

    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig({ maxNudges: 2 });

    const loop = new AgentLoop({
      config,
      provider,
      executor: createMockExecutor(),
      context,
      progress,
      cost,
    });

    const result = await loop.run('Do something');
    // First response = text, nudge 1, second response = text, nudge 2, third response = text, break
    expect(result.success).toBe(false);
    expect(result.summary).toContain('nudge');
  });

  it('nudges toward bash and retries when the model calls a non-bash tool (stream early-aborted)', async () => {
    const statusTool: ToolCall = {
      id: 'tc1',
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"status --task \\"x\\" --done \\"y\\" --remaining \\"none\\" --complete"}' },
    };
    // 1) stream aborted on a 'cat' tool name, 2) retry uses bash correctly and completes
    const provider = createMockProvider([
      { invalidToolName: 'cat' },
      { content: '', toolCalls: [statusTool] },
      { content: 'done' },
    ]);
    const results = new Map([['tc1', {
      tool_call_id: 'tc1',
      content: 'Status recorded.',
      success: true,
      signals: { statusComplete: true, statusResult: { task: 'x', done: 'y', remaining: 'none', complete: true, hasExplicitFlag: true } },
    } as ToolResult]]);
    const context = createMockContext();
    const progress = createMockProgress();

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor: createMockExecutor(results),
      context,
      progress,
      cost: createMockCost(),
    });

    const result = await loop.run('do it');

    expect(progress.onEvent).toHaveBeenCalledWith('malformed_tool_call', expect.objectContaining({ invalidToolName: 'cat' }));
    const nudges = (context.addUserMessage as ReturnType<typeof vi.fn>).mock.calls.map((c) => c[0] as string);
    expect(nudges.some((m) => m.includes('cat') && m.includes('bash'))).toBe(true);
    expect(result.success).toBe(true);
  });

  it('stops (does not loop forever) when the model keeps calling a non-bash tool', async () => {
    const provider = createMockProvider(Array.from({ length: 8 }, () => ({ invalidToolName: 'cat' } as ParsedResponse)));
    const progress = createMockProgress();

    const loop = new AgentLoop({
      config: defaultConfig({ maxNudges: 2 }),
      provider,
      executor: createMockExecutor(),
      context: createMockContext(),
      progress,
      cost: createMockCost(),
    });

    const result = await loop.run('do it');
    expect(result.success).toBe(false);
    // The wrong-tool nudge fired (capped at MAX_MALFORMED_RETRIES) before falling through.
    const malformed = (progress.onEvent as ReturnType<typeof vi.fn>).mock.calls.filter((c) => c[0] === 'malformed_tool_call');
    expect(malformed.length).toBeGreaterThanOrEqual(2);
  });

  it('interview agent pauses (awaiting_user) on a prose question instead of nudging', async () => {
    // A prose question is content with no tool call and no status — the
    // interview must hand control back to the user, not nudge toward completion.
    const provider = createMockProvider([
      { content: "What's your company's name?" },
    ]);
    const loop = new AgentLoop({
      config: defaultConfig({ agentType: 'interview' }),
      provider,
      executor: createMockExecutor(),
      context: createMockContext(),
      progress: createMockProgress(),
      cost: createMockCost(),
    });

    const result = await loop.run('Understand a company');
    expect(result.exitReason).toBe('awaiting_user');
    expect(result.success).toBe(true);
  });

  it('interview agent does not pause on a reasoning-only turn (no user-facing content)', async () => {
    // Reasoning without content is not a question — it should retry/nudge, not pause.
    const provider = createMockProvider([
      { reasoningDetails: [{ type: 'reasoning.text', text: 'thinking' }] as ParsedResponse['reasoningDetails'] },
    ]);
    const loop = new AgentLoop({
      config: defaultConfig({ agentType: 'interview', maxNudges: 1 }),
      provider,
      executor: createMockExecutor(),
      context: createMockContext(),
      progress: createMockProgress(),
      cost: createMockCost(),
    });

    const result = await loop.run('Understand a company');
    expect(result.exitReason).not.toBe('awaiting_user');
  });

  it('detects duplicate tool calls and stops after maxDuplicateToolCalls', async () => {
    const duplicateCall: ToolCall = {
      id: 'tc-dup',
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"ls"}' },
    };

    // Same tool call repeated multiple times
    const provider = createMockProvider([
      { content: '', toolCalls: [{ ...duplicateCall, id: 'tc1' }] },
      { content: '', toolCalls: [{ ...duplicateCall, id: 'tc2' }] },
      { content: '', toolCalls: [{ ...duplicateCall, id: 'tc3' }] },
      { content: '', toolCalls: [{ ...duplicateCall, id: 'tc4' }] },
    ]);

    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig({ maxDuplicateToolCalls: 3 });

    const loop = new AgentLoop({
      config,
      provider,
      executor: createMockExecutor(),
      context,
      progress,
      cost,
    });

    const result = await loop.run('Do the thing');
    expect(result.success).toBe(false);
    expect(result.summary).toContain('loop');
  });

  it('calls completionGate before accepting completion', async () => {
    let gateCallCount = 0;
    const completionGate = vi.fn(async () => {
      gateCallCount++;
      // First call returns error (blocks completion), second returns null (allows it)
      if (gateCallCount === 1) return 'Runtime error: undefined is not a function';
      return null;
    });

    // Use different command strings so they don't trigger duplicate detection
    const toolCall1: ToolCall = {
      id: 'tc1',
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"status --task \\"build\\" --done \\"all\\" --remaining \\"none\\" --complete"}' },
    };
    const toolCall2: ToolCall = {
      id: 'tc2',
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"status --task \\"build site\\" --done \\"everything\\" --remaining \\"none\\" --complete"}' },
    };

    const toolResult1: ToolResult = {
      tool_call_id: 'tc1',
      content: 'Status recorded.',
      success: true,
      signals: { statusComplete: true, statusResult: { task: 'build', done: 'all', remaining: 'none', complete: true, hasExplicitFlag: true } },
    };
    const toolResult2: ToolResult = {
      tool_call_id: 'tc2',
      content: 'Status recorded.',
      success: true,
      signals: { statusComplete: true, statusResult: { task: 'build site', done: 'everything', remaining: 'none', complete: true, hasExplicitFlag: true } },
    };

    // First iteration: tool call with statusComplete. Gate blocks.
    // Second iteration: different tool call with statusComplete. Gate allows.
    const provider = createMockProvider([
      { content: '', toolCalls: [toolCall1] },
      { content: '', toolCalls: [toolCall2] },
    ]);

    const results = new Map([
      ['tc1', toolResult1],
      ['tc2', toolResult2],
    ]);
    const executor = createMockExecutor(results);
    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig({ completionGate });

    const loop = new AgentLoop({
      config,
      provider,
      executor,
      context,
      progress,
      cost,
    });

    const result = await loop.run('Build it');
    expect(completionGate).toHaveBeenCalledTimes(2);
    expect(result.success).toBe(true);
  });

  it('handles provider error via onPausableError with continue', async () => {
    let callCount = 0;
    const provider: ProviderAdapter = {
      call: vi.fn(async () => {
        callCount++;
        if (callCount === 1) {
          throw new Error('Rate limited');
        }
        // Second call succeeds with a tool call that signals complete
        return {
          content: '',
          toolCalls: [{
            id: 'tc1',
            type: 'function' as const,
            function: { name: 'bash', arguments: '{"command":"status --task \\"x\\" --done \\"x\\" --remaining \\"none\\" --complete"}' },
          }],
        };
      }),
      getModel: () => 'test-model',
      getProvider: () => 'test-provider',
      supportsTools: () => true,
    };

    const onPausableError = vi.fn(async () => 'continue' as const);

    const toolResult: ToolResult = {
      tool_call_id: 'tc1',
      content: 'Status recorded.',
      success: true,
      signals: { statusComplete: true, statusResult: { task: 'x', done: 'x', remaining: 'none', complete: true, hasExplicitFlag: true } },
    };
    const results = new Map([['tc1', toolResult]]);
    const executor = createMockExecutor(results);
    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig({ onPausableError });

    const loop = new AgentLoop({
      config,
      provider,
      executor,
      context,
      progress,
      cost,
    });

    const result = await loop.run('Do it');
    expect(onPausableError).toHaveBeenCalledTimes(1);
    expect(result.success).toBe(true);
  });

  it('explore agent exits immediately without needing status', async () => {
    const provider = createMockProvider([
      { content: 'Here is the analysis of the codebase...' },
    ]);

    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig({ agentType: 'explore' });

    const loop = new AgentLoop({
      config,
      provider,
      executor: createMockExecutor(),
      context,
      progress,
      cost,
    });

    const result = await loop.run('Analyze this');
    expect(result.success).toBe(true);
    expect(result.turnCount).toBe(1);
    // Should not have nudged — immediate exit
    const events = (progress.onEvent as ReturnType<typeof vi.fn>).mock.calls.map(c => c[0]);
    expect(events).not.toContain('nudge');
  });

  it('handles onPausableError returning stop', async () => {
    const provider: ProviderAdapter = {
      call: vi.fn(async () => {
        throw new Error('Fatal error');
      }),
      getModel: () => 'test-model',
      getProvider: () => 'test-provider',
      supportsTools: () => true,
    };

    const onPausableError = vi.fn(async () => 'stop' as const);
    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig({ onPausableError });

    const loop = new AgentLoop({
      config,
      provider,
      executor: createMockExecutor(),
      context,
      progress,
      cost,
    });

    const result = await loop.run('Do it');
    expect(onPausableError).toHaveBeenCalledTimes(1);
    expect(result.success).toBe(false);
    expect(result.summary).toContain('error');
  });

  it('extracts tool calls from text for models without native tool support', async () => {
    const provider: ProviderAdapter = {
      call: vi.fn(async () => ({
        content: '```bash\nls -la\n```',
        toolCalls: undefined,
      })),
      getModel: () => 'test-model',
      getProvider: () => 'test-provider',
      supportsTools: () => false, // no native tool support
    };

    // After extraction, the tool call signals complete
    const executor: ToolExecutor = {
      execute: vi.fn(async (toolCall: ToolCall) => ({
        tool_call_id: toolCall.id,
        content: 'files listed',
        success: true,
        signals: { statusComplete: true, statusResult: { task: 'x', done: 'x', remaining: 'none', complete: true, hasExplicitFlag: true } },
      })),
      getDefinitions: () => [{ name: 'bash', description: 'Run shell', parameters: {} }],
    };

    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig();

    const loop = new AgentLoop({
      config,
      provider,
      executor,
      context,
      progress,
      cost,
    });

    const result = await loop.run('List files');
    expect(executor.execute).toHaveBeenCalled();
    expect(result.toolCount).toBe(1);
  });

  it('stops when stop() is called', async () => {
    let callCount = 0;
    const provider: ProviderAdapter = {
      call: vi.fn(async () => {
        callCount++;
        return {
          content: '',
          toolCalls: [{
            id: `tc${callCount}`,
            type: 'function' as const,
            function: { name: 'bash', arguments: `{"command":"echo ${callCount}"}` },
          }],
        };
      }),
      getModel: () => 'test-model',
      getProvider: () => 'test-provider',
      supportsTools: () => true,
    };

    const context = createMockContext();
    const progress = createMockProgress();
    const cost = createMockCost();
    const config = defaultConfig();

    const loop = new AgentLoop({
      config,
      provider,
      executor: createMockExecutor(),
      context,
      progress,
      cost,
    });

    // Stop after first iteration
    (provider.call as ReturnType<typeof vi.fn>).mockImplementation(async () => {
      if (callCount > 0) loop.stop();
      callCount++;
      return {
        content: '',
        toolCalls: [{
          id: `tc${callCount}`,
          type: 'function' as const,
          function: { name: 'bash', arguments: `{"command":"echo ${callCount}"}` },
        }],
      };
    });

    const result = await loop.run('Do something');
    expect(result.success).toBe(false);
    expect(result.summary).toContain('Stopped');
  });

  it('triggers compaction using token estimate when provider reports no usage', async () => {
    const toolCall: ToolCall = {
      id: 'tc1',
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"status --task \\"t\\" --done \\"d\\" --remaining \\"none\\" --complete"}' },
    };
    // Provider returns no usage (simulating providers that don't support stream_options)
    const provider = createMockProvider([
      { content: '', toolCalls: [toolCall] },
      { content: 'Done.' },
    ]);

    const context = createMockContext();
    // getTokenEstimate returns a value above threshold — should trigger compaction
    context.getTokenEstimate = () => 999999;
    context.needsCompaction = (tokenCount: number) => tokenCount >= 80000;

    const toolResult: ToolResult = {
      tool_call_id: 'tc1',
      content: 'Status recorded.',
      success: true,
      signals: { statusComplete: true, statusResult: { task: 't', done: 'd', remaining: 'none', complete: true, hasExplicitFlag: true } },
    };
    const executor = createMockExecutor(new Map([['tc1', toolResult]]));
    const progress = createMockProgress();
    // promptTokens=0 simulates missing usage
    const cost = createMockCost();

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor,
      context,
      progress,
      cost,
    });

    await loop.run('Build something');
    expect(context.compact).toHaveBeenCalled();
  });

  it('uses reported promptTokens for compaction check when available', async () => {
    const toolCall: ToolCall = {
      id: 'tc1',
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"status --task \\"t\\" --done \\"d\\" --remaining \\"none\\" --complete"}' },
    };
    const provider = createMockProvider([
      { content: '', toolCalls: [toolCall], usage: { promptTokens: 90000, completionTokens: 500, totalTokens: 90500 } },
      { content: 'Done.' },
    ]);

    const context = createMockContext();
    context.getTokenEstimate = () => 5000; // Low estimate — should NOT be used
    context.needsCompaction = (tokenCount: number) => tokenCount >= 80000;

    const toolResult: ToolResult = {
      tool_call_id: 'tc1',
      content: 'ok',
      success: true,
      signals: { statusComplete: true, statusResult: { task: 't', done: 'd', remaining: 'none', complete: true, hasExplicitFlag: true } },
    };
    const executor = createMockExecutor(new Map([['tc1', toolResult]]));
    const progress = createMockProgress();
    const cost = createMockCost();
    cost.getTotalUsage = () => ({ promptTokens: 90000, completionTokens: 500, totalTokens: 90500 });

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor,
      context,
      progress,
      cost,
    });

    await loop.run('Build something');
    expect(context.compact).toHaveBeenCalled();
  });

  it('does not compact when both promptTokens and estimate are below threshold', async () => {
    const toolCall: ToolCall = {
      id: 'tc1',
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"status --task \\"t\\" --done \\"d\\" --remaining \\"none\\" --complete"}' },
    };
    const provider = createMockProvider([
      { content: '', toolCalls: [toolCall] },
      { content: 'Done.' },
    ]);

    const context = createMockContext();
    context.getTokenEstimate = () => 5000;
    context.needsCompaction = (tokenCount: number) => tokenCount >= 80000;

    const toolResult: ToolResult = {
      tool_call_id: 'tc1',
      content: 'ok',
      success: true,
      signals: { statusComplete: true, statusResult: { task: 't', done: 'd', remaining: 'none', complete: true, hasExplicitFlag: true } },
    };
    const executor = createMockExecutor(new Map([['tc1', toolResult]]));
    const progress = createMockProgress();
    const cost = createMockCost();

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor,
      context,
      progress,
      cost,
    });

    await loop.run('Build something');
    expect(context.compact).not.toHaveBeenCalled();
  });
});

describe('AgentLoop history integrity and retry safeguards', () => {
  const statusToolCall = (id: string): ToolCall => ({
    id,
    type: 'function',
    function: { name: 'bash', arguments: '{"command":"status --task \\"t\\" --done \\"d\\" --remaining \\"none\\" --complete"}' },
  });
  const statusResultFor = (id: string): ToolResult => ({
    tool_call_id: id,
    content: 'ok',
    success: true,
    signals: { statusComplete: true, statusResult: { task: 't', done: 'd', remaining: 'none', complete: true, hasExplicitFlag: true } },
  });

  it('sends sanitized messages (orphan repair) to the provider', async () => {
    const sanitized: Message[] = [{ role: 'user', content: 'SANITIZED' }];
    const context = createMockContext();
    context.getSanitizedMessages = () => sanitized;
    const provider = createMockProvider([{ content: 'findings' }]);

    const loop = new AgentLoop({
      config: defaultConfig({ agentType: 'explore' }),
      provider,
      executor: createMockExecutor(),
      context,
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    await loop.run('hi');

    const sentMessages = (provider.call as ReturnType<typeof vi.fn>).mock.calls[0][0].messages;
    expect(sentMessages).toBe(sanitized);
  });

  it('uses its own last response promptTokens for compaction, not the shared cost tracker', async () => {
    // Shared tracker reports a small (child-contaminated) value; this loop's own
    // response reported 90000 prompt tokens — compaction must use 90000.
    const provider = createMockProvider([
      { content: '', toolCalls: [statusToolCall('tc1')], usage: { promptTokens: 90000, completionTokens: 500, totalTokens: 90500 } },
      { content: 'Done.' },
    ]);
    const context = createMockContext();
    context.getTokenEstimate = () => 5000;
    const needsCompaction = vi.fn((tokenCount: number) => tokenCount >= 80000);
    context.needsCompaction = needsCompaction;

    const cost = createMockCost();
    cost.getTotalUsage = () => ({ promptTokens: 50, completionTokens: 0, totalTokens: 50 });

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor: createMockExecutor(new Map([['tc1', statusResultFor('tc1')]])),
      context,
      progress: createMockProgress(),
      cost,
    });
    await loop.run('Build something');

    expect(needsCompaction).toHaveBeenCalledWith(90000);
    expect(context.compact).toHaveBeenCalled();
  });

  it('commits executed tool results to context before terminating on loop detection', async () => {
    const dup = (id: string): ToolCall => ({
      id,
      type: 'function',
      function: { name: 'bash', arguments: '{"command":"ls"}' },
    });
    // One batch: first call executes, the next 3 identical calls trip the
    // duplicate cap (maxDuplicateToolCalls: 3) mid-batch.
    const provider = createMockProvider([
      { content: '', toolCalls: [dup('a'), dup('b'), dup('c'), dup('d')] },
    ]);
    const context = createMockContext();

    const loop = new AgentLoop({
      config: defaultConfig({ maxDuplicateToolCalls: 3 }),
      provider,
      executor: createMockExecutor(),
      context,
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    const result = await loop.run('Do something');

    expect(result.summary).toContain('loop');
    // The assistant turn and the executed result for 'a' must be in history
    expect(context.addAssistantTurn).toHaveBeenCalledTimes(1);
    expect(context.addToolResults).toHaveBeenCalled();
    const committed = (context.addToolResults as ReturnType<typeof vi.fn>).mock.calls[0][0] as ToolResult[];
    expect(committed.some(r => r.tool_call_id === 'a' && r.success)).toBe(true);
  });

  it('passes a per-turn turnId to the tool executor context', async () => {
    const call = (id: string, cmd: string): ToolCall => ({
      id,
      type: 'function',
      function: { name: 'bash', arguments: `{"command":"${cmd}"}` },
    });
    const provider = createMockProvider([
      { content: '', toolCalls: [call('t1', 'ls')] },
      { content: '', toolCalls: [call('t2', 'status --task \\"t\\" --done \\"d\\" --remaining \\"none\\" --complete')] },
      { content: 'Done.' },
    ]);
    const turnIds: Array<number | undefined> = [];
    const executor: ToolExecutor = {
      execute: vi.fn(async (toolCall: ToolCall, ctx) => {
        turnIds.push(ctx.turnId);
        if (toolCall.id === 't2') return statusResultFor('t2');
        return { tool_call_id: toolCall.id, content: 'OK', success: true };
      }),
      getDefinitions: () => [{ name: 'bash', description: 'Run shell', parameters: {} }],
    };

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor,
      context: createMockContext(),
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    await loop.run('Do something');

    expect(turnIds).toEqual([1, 2]);
  });

  it('passes its abort signal to compact()', async () => {
    const provider = createMockProvider([
      { content: '', toolCalls: [statusToolCall('tc1')], usage: { promptTokens: 90000, completionTokens: 1, totalTokens: 90001 } },
      { content: 'Done.' },
    ]);
    const context = createMockContext();
    context.needsCompaction = (tokenCount: number) => tokenCount >= 80000;

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor: createMockExecutor(new Map([['tc1', statusResultFor('tc1')]])),
      context,
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    await loop.run('Build something');

    const compactOpts = (context.compact as ReturnType<typeof vi.fn>).mock.calls[0][1];
    expect(compactOpts?.signal).toBeInstanceOf(AbortSignal);
  });

  it('persists reasoning-only turns and injects a targeted retry instead of the status nudge', async () => {
    const reasoningOnly = {
      content: '',
      reasoningDetails: [{ type: 'thinking', text: 'I should check index.html first.' }],
    };
    const provider = createMockProvider([
      reasoningOnly,
      { content: '', toolCalls: [statusToolCall('tc1')] },
      { content: 'Done.' },
    ]);
    const context = createMockContext();
    const progress = createMockProgress();

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor: createMockExecutor(new Map([['tc1', statusResultFor('tc1')]])),
      context,
      progress,
      cost: createMockCost(),
    });
    const result = await loop.run('Build a landing page');

    // The reasoning-only assistant turn must be persisted with its reasoning details
    const assistantTurns = (context.addAssistantTurn as ReturnType<typeof vi.fn>).mock.calls.map(c => c[0]);
    expect(assistantTurns.some(r => r.reasoningDetails?.length && !r.content)).toBe(true);

    // The injected user message must be the targeted retry, not the status nudge
    const userMessages = (context.addUserMessage as ReturnType<typeof vi.fn>).mock.calls.map(c => c[0]);
    const retryMsg = userMessages.find((m: string) => typeof m === 'string' && m.includes('only reasoning'));
    expect(retryMsg).toBeDefined();
    expect(userMessages.some((m: string) => typeof m === 'string' && m.includes('status command'))).toBe(false);

    expect((progress.onEvent as ReturnType<typeof vi.fn>).mock.calls.some(c => c[0] === 'reasoning_only_retry')).toBe(true);
    expect(result.success).toBe(true);
  });

  it('falls back to status nudges after reasoning-only retries are exhausted', async () => {
    const reasoningOnly = () => ({
      content: '',
      reasoningDetails: [{ type: 'thinking', text: 'Thinking again from scratch.' }],
    });
    const provider = createMockProvider([
      reasoningOnly(), reasoningOnly(), reasoningOnly(), reasoningOnly(), reasoningOnly(),
    ]);
    const progress = createMockProgress();

    const loop = new AgentLoop({
      config: defaultConfig({ maxNudges: 1 }),
      provider,
      executor: createMockExecutor(),
      context: createMockContext(),
      progress,
      cost: createMockCost(),
    });
    const result = await loop.run('Build a landing page');

    const retryEvents = (progress.onEvent as ReturnType<typeof vi.fn>).mock.calls.filter(c => c[0] === 'reasoning_only_retry');
    expect(retryEvents).toHaveLength(2);
    expect(result.success).toBe(false);
    expect(result.exitReason).toBe('nudge_exhaustion');
  });

  it('keeps reasoning details on assistant turns that have content', async () => {
    const provider = createMockProvider([
      { content: 'Here is my answer.', reasoningDetails: [{ type: 'thinking', text: 'Considered options.' }] },
      { content: '', toolCalls: [statusToolCall('tc1')] },
      { content: 'Done.' },
    ]);
    const context = createMockContext();

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor: createMockExecutor(new Map([['tc1', statusResultFor('tc1')]])),
      context,
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    await loop.run('Question');

    const assistantTurns = (context.addAssistantTurn as ReturnType<typeof vi.fn>).mock.calls.map(c => c[0]);
    const withContent = assistantTurns.find(r => r.content === 'Here is my answer.');
    expect(withContent?.reasoningDetails?.length).toBe(1);
  });

  it('wraps synthetic harness messages in automated_reminder tags, but never the real user prompt', async () => {
    // Run produces: reasoning-only retry, then text-only nudges
    const provider = createMockProvider([
      { content: '', reasoningDetails: [{ type: 'thinking', text: 'hmm' }] },
      { content: 'text without status' },
      { content: 'more text' },
    ]);
    const context = createMockContext();

    const loop = new AgentLoop({
      config: defaultConfig({ maxNudges: 1 }),
      provider,
      executor: createMockExecutor(),
      context,
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    await loop.run('Build a landing page');

    const userMessages = (context.addUserMessage as ReturnType<typeof vi.fn>).mock.calls
      .map(c => c[0])
      .filter((m): m is string => typeof m === 'string');

    // The real user prompt is untouched
    expect(userMessages[0]).toBe('Build a landing page');
    // Every synthetic injection is wrapped
    const synthetic = userMessages.slice(1);
    expect(synthetic.length).toBeGreaterThan(0);
    for (const msg of synthetic) {
      expect(msg.startsWith('<automated_reminder>')).toBe(true);
      expect(msg.endsWith('</automated_reminder>')).toBe(true);
    }
  });

  it('replaces large repeated tool outputs with a dedup marker', async () => {
    const bigContent = 'SKILL FILE CONTENT '.repeat(50); // ~950 chars
    const call = (id: string, cmd: string): ToolCall => ({
      id,
      type: 'function',
      function: { name: 'bash', arguments: JSON.stringify({ command: cmd }) },
    });
    const provider = createMockProvider([
      { content: '', toolCalls: [call('t1', 'cat /skill.md')] },
      { content: '', toolCalls: [call('t2', 'head -n 999 /skill.md')] },
      { content: '', toolCalls: [call('t3', 'status --task "t" --done "d" --remaining "none" --complete')] },
      { content: 'Done.' },
    ]);
    const executor: ToolExecutor = {
      execute: vi.fn(async (toolCall: ToolCall) => {
        if (toolCall.id === 't3') return statusResultFor('t3');
        return { tool_call_id: toolCall.id, content: bigContent, success: true };
      }),
      getDefinitions: () => [{ name: 'bash', description: 'Run shell', parameters: {} }],
    };
    const context = createMockContext();

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor,
      context,
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    await loop.run('Do something');

    const toolBatches = (context.addToolResults as ReturnType<typeof vi.fn>).mock.calls.map(c => c[0] as ToolResult[]);
    // First read keeps full content
    expect(toolBatches[0][0].content).toBe(bigContent);
    // Identical repeat is replaced with a marker
    expect(toolBatches[1][0].content).not.toBe(bigContent);
    expect(toolBatches[1][0].content).toContain('identical');
    expect(toolBatches[1][0].success).toBe(true);
  });

  it('does not dedup small repeated tool outputs', async () => {
    const call = (id: string, cmd: string): ToolCall => ({
      id,
      type: 'function',
      function: { name: 'bash', arguments: JSON.stringify({ command: cmd }) },
    });
    const provider = createMockProvider([
      { content: '', toolCalls: [call('t1', 'mkdir /a')] },
      { content: '', toolCalls: [call('t2', 'mkdir /b')] },
      { content: '', toolCalls: [call('t3', 'status --task "t" --done "d" --remaining "none" --complete')] },
      { content: 'Done.' },
    ]);
    const executor: ToolExecutor = {
      execute: vi.fn(async (toolCall: ToolCall) => {
        if (toolCall.id === 't3') return statusResultFor('t3');
        return { tool_call_id: toolCall.id, content: 'Command succeeded with no output', success: true };
      }),
      getDefinitions: () => [{ name: 'bash', description: 'Run shell', parameters: {} }],
    };
    const context = createMockContext();

    const loop = new AgentLoop({
      config: defaultConfig(),
      provider,
      executor,
      context,
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    await loop.run('Do something');

    const toolBatches = (context.addToolResults as ReturnType<typeof vi.fn>).mock.calls.map(c => c[0] as ToolResult[]);
    expect(toolBatches[1][0].content).toBe('Command succeeded with no output');
  });

  it('reports the exit reason in the loop result', async () => {
    const provider = createMockProvider([
      { content: 'text only' },
      { content: 'more text' },
      { content: 'final text' },
    ]);
    const loop = new AgentLoop({
      config: defaultConfig({ maxNudges: 2 }),
      provider,
      executor: createMockExecutor(),
      context: createMockContext(),
      progress: createMockProgress(),
      cost: createMockCost(),
    });
    const result = await loop.run('Do something');
    expect(result.exitReason).toBe('nudge_exhaustion');
  });
});