beleth 0.2.0-rc.1

Autonomous agent framework - The King commands legions
Documentation
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
//! End-to-end integration tests for the LoopExecutor.
//!
//! Tests the full Generate->Detect->Execute->Integrate cycle using a
//! ScriptedEngine mock and real tools.
//!
//! Each test validates a specific behavior of the agentic loop:
//! - Answer detection (explicit and implicit)
//! - Tool call execution cycles
//! - Autonomy enforcement (forbidden, requires-approval)
//! - Meta-signal detection (stuck, yield, thinking)
//! - Resource limits (max iterations, tool call limit)
//! - Event stream completeness

use std::sync::Arc;

use async_trait::async_trait;
use infernum_core::{
    model::LlamaVersion, response::Choice, EmbedRequest, EmbedResponse, GenerateRequest,
    GenerateResponse, ModelArchitecture, ModelId, ModelMetadata, ModelSource, RequestId, Result,
    TokenStream, Usage,
};
use parking_lot::Mutex;
use tokio::sync::mpsc;

use abaddon::InferenceEngine;
use beleth::{
    AutonomyGrant, ExecutorConfig, IterationOutcome, LoopConfig, LoopEvent, LoopExecutor,
    NaturalTermination, ResourceTermination, TerminationReason, ToolPattern, ToolRegistry,
};

// =============================================================================
// ScriptedEngine — mock InferenceEngine returning predetermined responses
// =============================================================================

struct ScriptedEngine {
    responses: Mutex<Vec<String>>,
    metadata: ModelMetadata,
    call_count: Mutex<usize>,
}

impl ScriptedEngine {
    fn new(responses: Vec<String>) -> Self {
        Self {
            responses: Mutex::new(responses),
            metadata: ModelMetadata::builder(
                "test-model",
                ModelArchitecture::Llama {
                    version: LlamaVersion::V3,
                },
            )
            .source(ModelSource::local("/tmp/test-model"))
            .build(),
            call_count: Mutex::new(0),
        }
    }

    fn make_response(text: &str) -> GenerateResponse {
        GenerateResponse {
            request_id: RequestId::new(),
            created: 0,
            model: ModelId::new("test-model"),
            choices: vec![Choice {
                index: 0,
                text: text.to_string(),
                message: None,
                finish_reason: None,
                logprobs: None,
            }],
            usage: Usage::new(10, 20),
            time_to_first_token_ms: None,
            total_time_ms: None,
        }
    }

    fn call_count(&self) -> usize {
        *self.call_count.lock()
    }
}

#[async_trait]
impl InferenceEngine for ScriptedEngine {
    async fn generate(&self, _request: GenerateRequest) -> Result<GenerateResponse> {
        let responses = self.responses.lock();
        let mut count = self.call_count.lock();
        let idx = *count;
        *count += 1;

        if idx >= responses.len() {
            // Safety net: return final answer to prevent infinite loop
            return Ok(Self::make_response(
                "<answer confidence=\"0.5\">Ran out of scripted responses</answer>",
            ));
        }

        Ok(Self::make_response(&responses[idx]))
    }

    async fn generate_stream(&self, _request: GenerateRequest) -> Result<TokenStream> {
        Ok(TokenStream::empty())
    }

    async fn embed(&self, _request: EmbedRequest) -> Result<EmbedResponse> {
        Err(infernum_core::Error::internal("Not supported in mock"))
    }

    fn model_info(&self) -> &ModelMetadata {
        &self.metadata
    }

    fn is_ready(&self) -> bool {
        true
    }
}

// =============================================================================
// Helpers
// =============================================================================

/// Config with default autonomy (requires approval for all tools).
fn make_config(session_id: &str) -> ExecutorConfig {
    ExecutorConfig::new(session_id).with_system_prompt("You are a test assistant.")
}

/// Config with permissive autonomy (auto-approve all tools).
fn make_permissive_config(session_id: &str, dir: &std::path::Path) -> ExecutorConfig {
    ExecutorConfig::new(session_id)
        .with_system_prompt("You are a test assistant.")
        .with_working_dir(dir)
        .with_autonomy(
            AutonomyGrant::builder()
                .allow(ToolPattern::Tool("*".to_string()))
                .build(),
        )
}

/// Collects all events from the channel after the sender is dropped.
async fn collect_events(mut rx: mpsc::Receiver<LoopEvent>) -> Vec<LoopEvent> {
    let mut events = Vec::new();
    while let Some(event) = rx.recv().await {
        events.push(event);
    }
    events
}

fn event_name(e: &LoopEvent) -> &'static str {
    match e {
        LoopEvent::LoopStarted { .. } => "LoopStarted",
        LoopEvent::IterationStarted { .. } => "IterationStarted",
        LoopEvent::GenerationCompleted { .. } => "GenerationCompleted",
        LoopEvent::ToolCallDetected { .. } => "ToolCallDetected",
        LoopEvent::ToolExecutionStarted { .. } => "ToolExecutionStarted",
        LoopEvent::ToolExecutionCompleted { .. } => "ToolExecutionCompleted",
        LoopEvent::ToolApprovalRequired { .. } => "ToolApprovalRequired",
        LoopEvent::IterationCompleted { .. } => "IterationCompleted",
        LoopEvent::MetaSignalDetected { .. } => "MetaSignalDetected",
        LoopEvent::LoopCompleted { .. } => "LoopCompleted",
        LoopEvent::TokenGenerated { .. } => "TokenGenerated",
        LoopEvent::ContextCompressed { .. } => "ContextCompressed",
        LoopEvent::Error { .. } => "Error",
    }
}

// =============================================================================
// Tests
// =============================================================================

/// Explicit answer via <answer> tag — loop terminates in 1 iteration.
#[tokio::test]
async fn test_executor_explicit_answer() {
    let engine = Arc::new(ScriptedEngine::new(vec![
        r#"<answer confidence="0.95">The answer is 42.</answer>"#.to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_builtins());
    let config = make_config("test-explicit-answer");
    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let summary = executor
        .run("What is the answer?", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 1);
    assert_eq!(summary.tool_calls_made, 0);
    match &summary.termination {
        TerminationReason::Natural(NaturalTermination::AnswerProvided { confidence, .. }) => {
            assert!(
                *confidence > 0.9,
                "Expected high confidence, got {confidence}"
            );
        },
        other => panic!("Expected AnswerProvided, got {other:?}"),
    }
    assert_eq!(engine.call_count(), 1);

    let events = collect_events(rx).await;
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::LoopStarted { .. })));
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::LoopCompleted { .. })));
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::MetaSignalDetected { .. })));
}

/// Implicit answer — model outputs plain text with no tools or meta-signals.
#[tokio::test]
async fn test_executor_implicit_answer() {
    let engine = Arc::new(ScriptedEngine::new(vec![
        "Rust ownership is a memory management system that ensures memory safety without a garbage collector."
            .to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_builtins());
    // Disable implicit signals so plain text doesn't accidentally match uncertainty patterns
    let config = make_config("test-implicit-answer").with_loop_config(LoopConfig {
        detect_implicit_signals: false,
        ..LoopConfig::default()
    });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, _rx) = mpsc::channel(64);

    let summary = executor
        .run("Explain Rust ownership.", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 1);
    assert_eq!(summary.tool_calls_made, 0);
    // No tools, no meta-signals → treated as implicit answer at 0.5 confidence
    match &summary.termination {
        TerminationReason::Natural(NaturalTermination::AnswerProvided { confidence, .. }) => {
            assert!(
                (*confidence - 0.5).abs() < 0.01,
                "Expected 0.5 confidence, got {confidence}"
            );
        },
        other => panic!("Expected AnswerProvided, got {other:?}"),
    }
}

/// Tool call cycle — model calls read_file, gets result, then answers.
#[tokio::test]
async fn test_executor_tool_call_and_answer() {
    let dir = tempfile::tempdir().expect("tempdir");
    let file_path = dir.path().join("test.txt");
    std::fs::write(&file_path, "Hello from the test file!\n").expect("write seed file");
    let file_path_str = file_path.to_string_lossy().to_string();

    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: call read_file
        format!(
            "Let me read that file.\n\
             <tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{file_path_str}\"}}}}\n\
             </tool_call>"
        ),
        // Iteration 2: answer with the file contents
        r#"<answer confidence="0.9">The file contains: Hello from the test file!</answer>"#
            .to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config =
        make_permissive_config("test-tool-call", dir.path()).with_loop_config(LoopConfig {
            detect_implicit_signals: false,
            ..LoopConfig::default()
        });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let summary = executor
        .run("Read test.txt", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 2);
    assert_eq!(summary.tool_calls_made, 1);
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AnswerProvided { .. })
    ));
    assert_eq!(engine.call_count(), 2);

    // Check events include tool execution
    let events = collect_events(rx).await;
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::ToolCallDetected { tool, .. } if tool == "read_file")));
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::ToolExecutionStarted { .. })));
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::ToolExecutionCompleted { .. })));
}

/// Multiple tool calls in one iteration.
#[tokio::test]
async fn test_executor_multiple_tool_calls_per_iteration() {
    let dir = tempfile::tempdir().expect("tempdir");
    let file_a = dir.path().join("a.txt");
    let file_b = dir.path().join("b.txt");
    std::fs::write(&file_a, "Content A\n").expect("write a");
    std::fs::write(&file_b, "Content B\n").expect("write b");
    let path_a = file_a.to_string_lossy().to_string();
    let path_b = file_b.to_string_lossy().to_string();

    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: two tool calls
        format!(
            "I'll read both files.\n\
             <tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_a}\"}}}}\n\
             </tool_call>\n\
             <tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_b}\"}}}}\n\
             </tool_call>"
        ),
        // Iteration 2: answer
        r#"<answer confidence="0.85">File A: Content A, File B: Content B</answer>"#.to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config =
        make_permissive_config("test-multi-tool", dir.path()).with_loop_config(LoopConfig {
            detect_implicit_signals: false,
            ..LoopConfig::default()
        });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, _rx) = mpsc::channel(64);

    let summary = executor
        .run("Read both files", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 2);
    assert_eq!(summary.tool_calls_made, 2);
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AnswerProvided { .. })
    ));
}

/// Write then read — verify tool results feed back into the conversation.
#[tokio::test]
async fn test_executor_write_then_read() {
    let dir = tempfile::tempdir().expect("tempdir");
    let file_path = dir.path().join("created.txt");
    let path_str = file_path.to_string_lossy().to_string();

    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: write a file
        format!(
            "<tool_call>\n\
             {{\"name\": \"write_file\", \"arguments\": {{\"path\": \"{path_str}\", \"content\": \"Hello from executor!\"}}}}\n\
             </tool_call>"
        ),
        // Iteration 2: read it back
        format!(
            "<tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}\n\
             </tool_call>"
        ),
        // Iteration 3: answer
        r#"<answer confidence="0.95">File created and verified.</answer>"#.to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config =
        make_permissive_config("test-write-read", dir.path()).with_loop_config(LoopConfig {
            detect_implicit_signals: false,
            ..LoopConfig::default()
        });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, _rx) = mpsc::channel(64);

    let summary = executor
        .run("Create and verify a file", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 3);
    assert_eq!(summary.tool_calls_made, 2);
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AnswerProvided { .. })
    ));

    // Verify the file was actually written to disk
    let content = std::fs::read_to_string(&file_path).expect("read created file");
    assert_eq!(content, "Hello from executor!");
}

/// Autonomy forbidden — model tries a forbidden tool, gets denied.
#[tokio::test]
async fn test_executor_forbidden_tool() {
    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: try to call bash (which is forbidden)
        r#"Let me run a command.
<tool_call>
{"name": "bash", "arguments": {"command": "echo hello"}}
</tool_call>"#
            .to_string(),
        // Iteration 2: answer after seeing the denial
        r#"<answer confidence="0.7">I cannot run bash commands.</answer>"#.to_string(),
    ]));

    let dir = tempfile::tempdir().expect("tempdir");
    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config = ExecutorConfig::new("test-forbidden")
        .with_system_prompt("You are a test assistant.")
        .with_working_dir(dir.path())
        .with_autonomy(
            AutonomyGrant::builder()
                .allow(ToolPattern::Tool("read_file".to_string()))
                .forbid(ToolPattern::Tool("bash".to_string()))
                .build(),
        )
        .with_loop_config(LoopConfig {
            detect_implicit_signals: false,
            ..LoopConfig::default()
        });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let summary = executor
        .run("Run echo hello", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 2);
    assert_eq!(summary.tool_calls_made, 1); // Still counted as detected
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AnswerProvided { .. })
    ));

    // Check events: bash detected but no ToolExecutionStarted (forbidden)
    let events = collect_events(rx).await;
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::ToolCallDetected { tool, .. } if tool == "bash")));
    let exec_started = events
        .iter()
        .filter(|e| matches!(e, LoopEvent::ToolExecutionStarted { .. }))
        .count();
    assert_eq!(
        exec_started, 0,
        "Forbidden tool should not emit ToolExecutionStarted"
    );
}

/// Default autonomy requires approval — tool denied with RequiresApproval.
#[tokio::test]
async fn test_executor_requires_approval() {
    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: try to call a tool (default = requires approval)
        r#"<tool_call>
{"name": "read_file", "arguments": {"path": "/tmp/test.txt"}}
</tool_call>"#
            .to_string(),
        // Iteration 2: answer after seeing the denial
        r#"<answer confidence="0.6">I need approval to use that tool.</answer>"#.to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    // Default autonomy grant = everything requires approval
    let config = make_config("test-approval").with_loop_config(LoopConfig {
        detect_implicit_signals: false,
        ..LoopConfig::default()
    });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let summary = executor
        .run("Read a file", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 2);

    // Check that approval was required
    let events = collect_events(rx).await;
    let approval_required = events
        .iter()
        .any(|e| matches!(e, LoopEvent::ToolApprovalRequired { tool, .. } if tool == "read_file"));
    assert!(approval_required, "Should emit ToolApprovalRequired event");
}

/// Stuck signal — model outputs <stuck> tag, loop terminates.
#[tokio::test]
async fn test_executor_stuck_signal() {
    let engine = Arc::new(ScriptedEngine::new(vec![r#"<stuck>
<attempt>Tried reading the documentation</attempt>
<hypothesis>The API might have changed</hypothesis>
<request>I need clarification on the new API format</request>
</stuck>"#
        .to_string()]));

    let tools = Arc::new(ToolRegistry::with_builtins());
    let config = make_config("test-stuck");
    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let summary = executor
        .run("Fix the API integration", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 1);
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AgentStuck { .. })
    ));
    // Stuck loops are resumable (can_resume = !is_terminal || Stuck | Yielded)
    assert!(summary.can_resume, "Stuck loops should be resumable");

    let events = collect_events(rx).await;
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::MetaSignalDetected { .. })));
    assert!(events.iter().any(|e| matches!(
        e,
        LoopEvent::IterationCompleted {
            outcome: IterationOutcome::Stuck,
            ..
        }
    )));
}

/// Yield signal — model outputs <yield> tag, loop terminates.
#[tokio::test]
async fn test_executor_yield_signal() {
    let engine = Arc::new(ScriptedEngine::new(vec![r#"<yield>
<partial>I've identified the problem is in the auth module</partial>
<expertise>security specialist</expertise>
</yield>"#
        .to_string()]));

    let tools = Arc::new(ToolRegistry::with_builtins());
    let config = make_config("test-yield");

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let summary = executor
        .run("Review the security config", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 1);
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AgentYielded { .. })
    ));
    assert!(summary.can_resume, "Yielded loops should be resumable");

    let events = collect_events(rx).await;
    assert!(events.iter().any(|e| matches!(
        e,
        LoopEvent::IterationCompleted {
            outcome: IterationOutcome::Yielded,
            ..
        }
    )));
}

/// Thinking signal is non-terminal — loop continues to tool detection.
#[tokio::test]
async fn test_executor_thinking_signal_continues() {
    let engine = Arc::new(ScriptedEngine::new(vec![
        // Output with thinking tag but no tools → implicit answer
        r#"<thinking direction="analyzing">Let me consider the options.</thinking>
The architecture uses a layered approach with clear separation of concerns."#
            .to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_builtins());
    let config = make_config("test-thinking").with_loop_config(LoopConfig {
        detect_implicit_signals: false,
        ..LoopConfig::default()
    });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let summary = executor
        .run("Analyze the architecture", tx)
        .await
        .expect("run should succeed");

    // Thinking is non-terminal, no tool calls → implicit answer
    assert_eq!(summary.iterations_completed, 1);
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AnswerProvided { .. })
    ));

    let events = collect_events(rx).await;
    assert!(events
        .iter()
        .any(|e| matches!(e, LoopEvent::MetaSignalDetected { .. })));
}

/// Max iterations — loop terminates when iteration limit is reached.
#[tokio::test]
async fn test_executor_max_iterations() {
    let dir = tempfile::tempdir().expect("tempdir");
    let file_path = dir.path().join("loop.txt");
    std::fs::write(&file_path, "test content\n").expect("write");
    let path_str = file_path.to_string_lossy().to_string();

    // Model keeps making tool calls, never provides an answer
    let engine = Arc::new(ScriptedEngine::new(vec![
        format!(
            "<tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}\n\
             </tool_call>"
        ),
        format!(
            "<tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}\n\
             </tool_call>"
        ),
        format!(
            "<tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}\n\
             </tool_call>"
        ),
        // Extra: safety net in case limit doesn't trigger
        r#"<answer confidence="0.5">Done</answer>"#.to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config = make_permissive_config("test-max-iter", dir.path()).with_loop_config(LoopConfig {
        max_iterations: 2, // Only allow 2 iterations
        detect_implicit_signals: false,
        ..LoopConfig::default()
    });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, _rx) = mpsc::channel(64);

    let summary = executor
        .run("Read the file repeatedly", tx)
        .await
        .expect("run should succeed");

    // Should complete 2 iterations, then hit the limit at continue_loop
    assert_eq!(summary.iterations_completed, 2);
    assert!(matches!(
        summary.termination,
        TerminationReason::Resource(ResourceTermination::MaxIterations {
            completed: 2,
            limit: 2
        })
    ));
}

/// Tool call limit — too many tool calls triggers resource termination.
#[tokio::test]
async fn test_executor_tool_call_limit() {
    let dir = tempfile::tempdir().expect("tempdir");
    let file_path = dir.path().join("limit.txt");
    std::fs::write(&file_path, "content\n").expect("write");
    let path_str = file_path.to_string_lossy().to_string();

    // Model makes 3 tool calls per iteration; limit is 4 total
    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: 3 tool calls (under limit of 4)
        format!(
            "<tool_call>{{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}</tool_call>\n\
             <tool_call>{{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}</tool_call>\n\
             <tool_call>{{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}</tool_call>"
        ),
        // Iteration 2: 3 more calls -> new_total = 6 > 4 -> terminated
        format!(
            "<tool_call>{{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}</tool_call>\n\
             <tool_call>{{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}</tool_call>\n\
             <tool_call>{{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}</tool_call>"
        ),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config =
        make_permissive_config("test-tool-limit", dir.path()).with_loop_config(LoopConfig {
            max_tool_calls: 4,
            detect_implicit_signals: false,
            ..LoopConfig::default()
        });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, _rx) = mpsc::channel(64);

    let summary = executor
        .run("Read a lot", tx)
        .await
        .expect("run should succeed");

    assert!(matches!(
        summary.termination,
        TerminationReason::Resource(ResourceTermination::ToolCallLimitReached { .. })
    ));
}

/// Event stream completeness — verify the expected event sequence.
#[tokio::test]
async fn test_executor_event_stream_completeness() {
    let dir = tempfile::tempdir().expect("tempdir");
    let file_path = dir.path().join("events.txt");
    std::fs::write(&file_path, "event test\n").expect("write");
    let path_str = file_path.to_string_lossy().to_string();

    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: tool call
        format!(
            "<tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{path_str}\"}}}}\n\
             </tool_call>"
        ),
        // Iteration 2: answer
        r#"<answer confidence="0.8">Found: event test</answer>"#.to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config = make_permissive_config("test-events", dir.path()).with_loop_config(LoopConfig {
        detect_implicit_signals: false,
        ..LoopConfig::default()
    });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let _summary = executor
        .run("Read the file", tx)
        .await
        .expect("run should succeed");

    let events = collect_events(rx).await;
    let names: Vec<&str> = events.iter().map(event_name).collect();

    // Expected event sequence for a 2-iteration tool->answer cycle:
    // LoopStarted
    // IterationStarted (iter 1)
    // GenerationCompleted
    // ToolCallDetected
    // ToolExecutionStarted
    // ToolExecutionCompleted
    // IterationCompleted (ToolCallsExecuted)
    // IterationStarted (iter 2)
    // GenerationCompleted
    // MetaSignalDetected
    // IterationCompleted (AnswerProvided)
    // LoopCompleted

    // Verify first and last
    assert_eq!(names[0], "LoopStarted");
    assert_eq!(*names.last().expect("events"), "LoopCompleted");

    // Count specific events
    let gen_count = names
        .iter()
        .filter(|&&n| n == "GenerationCompleted")
        .count();
    assert_eq!(gen_count, 2, "Should have 2 generation completions");

    let iter_starts = names.iter().filter(|&&n| n == "IterationStarted").count();
    assert_eq!(iter_starts, 2, "Should have 2 iteration starts");

    let tool_detections = names.iter().filter(|&&n| n == "ToolCallDetected").count();
    assert_eq!(tool_detections, 1, "Should have 1 tool detection");

    let tool_execs = names
        .iter()
        .filter(|&&n| n == "ToolExecutionCompleted")
        .count();
    assert_eq!(tool_execs, 1, "Should have 1 tool execution completion");
}

/// Answer with caveats — caveats are extracted from the answer tag.
#[tokio::test]
async fn test_executor_answer_with_caveats() {
    let engine = Arc::new(ScriptedEngine::new(vec![
        r#"<answer confidence="0.7">The result is approximately 3.14.<caveat>Rounded to 2 decimal places</caveat></answer>"#
            .to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_builtins());
    let config = make_config("test-caveats");
    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, _rx) = mpsc::channel(64);

    let summary = executor
        .run("Calculate pi", tx)
        .await
        .expect("run should succeed");

    match &summary.termination {
        TerminationReason::Natural(NaturalTermination::AnswerProvided { confidence, answer }) => {
            assert!(
                (*confidence - 0.7).abs() < 0.01,
                "Expected 0.7 confidence, got {confidence}"
            );
            assert!(
                answer.contains("3.14"),
                "Answer should contain the result: {answer}"
            );
        },
        other => panic!("Expected AnswerProvided, got {other:?}"),
    }
}

/// Tool error recovery — tool fails, model sees error and adapts.
#[tokio::test]
async fn test_executor_tool_error_recovery() {
    let dir = tempfile::tempdir().expect("tempdir");
    let missing = dir.path().join("nonexistent.txt");
    let missing_str = missing.to_string_lossy().to_string();

    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: try to read a file that doesn't exist
        format!(
            "<tool_call>\n\
             {{\"name\": \"read_file\", \"arguments\": {{\"path\": \"{missing_str}\"}}}}\n\
             </tool_call>"
        ),
        // Iteration 2: acknowledge the error
        r#"<answer confidence="0.8">The file does not exist.</answer>"#.to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config =
        make_permissive_config("test-error-recovery", dir.path()).with_loop_config(LoopConfig {
            detect_implicit_signals: false,
            ..LoopConfig::default()
        });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, rx) = mpsc::channel(64);

    let summary = executor
        .run("Read nonexistent.txt", tx)
        .await
        .expect("run should succeed even on tool error");

    assert_eq!(summary.iterations_completed, 2);
    assert_eq!(summary.tool_calls_made, 1);
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AnswerProvided { .. })
    ));

    // Verify the tool result was captured as failed
    let events = collect_events(rx).await;
    let tool_completed = events
        .iter()
        .find(|e| matches!(e, LoopEvent::ToolExecutionCompleted { .. }));
    assert!(
        tool_completed.is_some(),
        "Should have ToolExecutionCompleted event"
    );
}

/// Bash tool execution through the executor.
#[tokio::test]
async fn test_executor_bash_tool() {
    let dir = tempfile::tempdir().expect("tempdir");

    let engine = Arc::new(ScriptedEngine::new(vec![
        // Iteration 1: run a bash command
        r#"<tool_call>
{"name": "bash", "arguments": {"command": "echo hello-from-bash"}}
</tool_call>"#
            .to_string(),
        // Iteration 2: answer
        r#"<answer confidence="0.9">The command output: hello-from-bash</answer>"#.to_string(),
    ]));

    let tools = Arc::new(ToolRegistry::with_code_tools());
    let config = make_permissive_config("test-bash", dir.path()).with_loop_config(LoopConfig {
        detect_implicit_signals: false,
        ..LoopConfig::default()
    });

    let executor = LoopExecutor::new(engine.clone(), tools, config);
    let (tx, _rx) = mpsc::channel(64);

    let summary = executor
        .run("Run echo hello-from-bash", tx)
        .await
        .expect("run should succeed");

    assert_eq!(summary.iterations_completed, 2);
    assert_eq!(summary.tool_calls_made, 1);
    assert!(matches!(
        summary.termination,
        TerminationReason::Natural(NaturalTermination::AnswerProvided { .. })
    ));
}