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
//! Async loop executor — drives the `AgenticLoop` state machine to completion.
//!
//! Orchestrates: Generate → Detect → Execute → Integrate → Continue
//!
//! The executor accepts an `InferenceEngine` for generation, a `ToolRegistry`
//! for tool execution, and an `AutonomyGrant` for permission enforcement.
//! It streams `LoopEvent`s through a channel for real-time observability.
//!
//! Reference: AGENTIC-LOOP-SPEC.md §2.

use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;

use async_trait::async_trait;
use infernum_core::{GenerateRequest, Message, SamplingParams};
use tokio::sync::mpsc;
use tracing::{debug, info, warn};

use crate::tool::{ToolCall as BelethToolCall, ToolContext, ToolRegistry};

use super::approval::{ApprovalDecision, ApprovalGate};
use super::meta_signal::detect_meta_signal;
use super::types::*;
use super::AgenticLoop;

// ---------------------------------------------------------------------------
// Tool call detection
// ---------------------------------------------------------------------------

/// A tool call detected in model output.
#[derive(Debug, Clone)]
pub struct DetectedCall {
    /// Unique identifier for this call.
    pub id: String,
    /// Name of the tool being called.
    pub name: String,
    /// Parsed arguments.
    pub arguments: serde_json::Value,
}

/// Trait for detecting tool calls in model output.
///
/// The default implementation handles Qwen-style `<tool_call>` tags.
/// Servers may provide a model-family-aware implementation.
#[async_trait]
pub trait ToolCallDetector: Send + Sync {
    /// Detect tool calls in the given model output text.
    fn detect(&self, output: &str) -> Vec<DetectedCall>;
}

/// Default detector that parses `<tool_call>{"name":..., "arguments":...}</tool_call>` tags.
#[derive(Debug, Clone, Default)]
pub struct QwenToolCallDetector;

impl QwenToolCallDetector {
    /// Creates a new Qwen-format tool call detector.
    pub fn new() -> Self {
        Self
    }
}

#[async_trait]
impl ToolCallDetector for QwenToolCallDetector {
    fn detect(&self, output: &str) -> Vec<DetectedCall> {
        parse_tool_call_tags(output)
    }
}

// ---------------------------------------------------------------------------
// Executor configuration
// ---------------------------------------------------------------------------

/// Configuration for the loop executor.
#[derive(Debug, Clone)]
pub struct ExecutorConfig {
    /// Loop state machine configuration.
    pub loop_config: LoopConfig,
    /// Autonomy grant for permission checking.
    pub autonomy: AutonomyGrant,
    /// System prompt prepended to the context.
    pub system_prompt: Option<String>,
    /// Sampling parameters for generation.
    pub sampling: SamplingParams,
    /// Session identifier for event correlation.
    pub session_id: String,
    /// Working directory for file tools.
    pub working_dir: Option<PathBuf>,
}

impl ExecutorConfig {
    /// Creates a new executor config with the given session ID.
    pub fn new(session_id: impl Into<String>) -> Self {
        Self {
            loop_config: LoopConfig::default(),
            autonomy: AutonomyGrant::default(),
            system_prompt: None,
            sampling: SamplingParams::default().with_max_tokens(2048),
            session_id: session_id.into(),
            working_dir: None,
        }
    }

    /// Sets the loop configuration.
    pub fn with_loop_config(mut self, config: LoopConfig) -> Self {
        self.loop_config = config;
        self
    }

    /// Sets the autonomy grant.
    pub fn with_autonomy(mut self, autonomy: AutonomyGrant) -> Self {
        self.autonomy = autonomy;
        self
    }

    /// Sets the system prompt.
    pub fn with_system_prompt(mut self, prompt: impl Into<String>) -> Self {
        self.system_prompt = Some(prompt.into());
        self
    }

    /// Sets the sampling parameters.
    pub fn with_sampling(mut self, sampling: SamplingParams) -> Self {
        self.sampling = sampling;
        self
    }

    /// Sets the working directory for file tools.
    pub fn with_working_dir(mut self, dir: impl Into<PathBuf>) -> Self {
        self.working_dir = Some(dir.into());
        self
    }
}

// ---------------------------------------------------------------------------
// Loop executor
// ---------------------------------------------------------------------------

/// Errors from loop execution.
#[derive(Debug, thiserror::Error)]
pub enum LoopError {
    /// Error from the inference engine.
    #[error("engine error: {0}")]
    EngineError(String),
    /// Invalid state transition.
    #[error("transition error: {0}")]
    TransitionError(#[from] TransitionError),
    /// Tool execution error.
    #[error("tool error: {0}")]
    ToolError(String),
}

/// The async loop executor.
///
/// Drives the `AgenticLoop` state machine by generating model output,
/// detecting tool calls and meta-signals, executing tools through the
/// registry with autonomy enforcement, and integrating results back
/// into the conversation context.
///
/// When an [`ApprovalGate`] is attached, tools that require approval
/// will block until a decision is delivered (or the timeout expires),
/// rather than failing immediately. See AGENTIC-LOOP-SPEC §9.4.
pub struct LoopExecutor {
    engine: Arc<dyn abaddon::InferenceEngine>,
    tools: Arc<ToolRegistry>,
    detector: Arc<dyn ToolCallDetector>,
    config: ExecutorConfig,
    approval_gate: Option<Arc<ApprovalGate>>,
}

impl LoopExecutor {
    /// Creates a new executor.
    pub fn new(
        engine: Arc<dyn abaddon::InferenceEngine>,
        tools: Arc<ToolRegistry>,
        config: ExecutorConfig,
    ) -> Self {
        Self {
            engine,
            tools,
            detector: Arc::new(QwenToolCallDetector::new()),
            config,
            approval_gate: None,
        }
    }

    /// Sets a custom tool call detector.
    pub fn with_detector(mut self, detector: Arc<dyn ToolCallDetector>) -> Self {
        self.detector = detector;
        self
    }

    /// Attaches an approval gate for interactive tool approval.
    ///
    /// When set, tools requiring approval will block on the gate instead of
    /// failing immediately. Decisions are delivered through the gate by
    /// external systems (HTTP endpoints, CLI prompts).
    pub fn with_approval_gate(mut self, gate: Arc<ApprovalGate>) -> Self {
        self.approval_gate = Some(gate);
        self
    }

    /// Returns a reference to the approval gate, if attached.
    pub fn approval_gate(&self) -> Option<&Arc<ApprovalGate>> {
        self.approval_gate.as_ref()
    }

    /// Runs the agentic loop to completion.
    ///
    /// Streams `LoopEvent`s through `event_tx` for real-time observability.
    /// Returns the final `LoopSummary` when the loop terminates.
    pub async fn run(
        &self,
        objective: &str,
        event_tx: mpsc::Sender<LoopEvent>,
    ) -> Result<LoopSummary, LoopError> {
        let mut state_machine = AgenticLoop::new(self.config.loop_config.clone());
        let mut messages = self.build_initial_messages(objective);
        let detection_config = DetectionConfig {
            detect_implicit: self.config.loop_config.detect_implicit_signals,
            ..Default::default()
        };
        let tool_ctx = self.build_tool_context();

        // Initialized → Generating
        state_machine.start().map_err(|e| {
            warn!("Failed to start loop: {e}");
            e
        })?;

        let _ = event_tx
            .send(LoopEvent::LoopStarted {
                session_id: self.config.session_id.clone(),
                config: self.config.loop_config.clone(),
            })
            .await;

        loop {
            let iteration = state_machine.iteration();
            info!(iteration, "Agentic loop iteration starting");

            let _ = event_tx
                .send(LoopEvent::IterationStarted {
                    iteration,
                    status: state_machine.status(),
                })
                .await;

            // =================================================================
            // GENERATE
            // =================================================================
            let request = self.build_generate_request(&messages);
            let response = self.engine.generate(request).await.map_err(|e| {
                warn!("Engine generation failed: {e}");
                LoopError::EngineError(e.to_string())
            })?;

            let output = response
                .choices
                .first()
                .map(|c| c.text.clone())
                .unwrap_or_default();
            let tokens = response.usage.completion_tokens;

            debug!(tokens, output_len = output.len(), "Generation complete");

            // Generating → Detecting (may terminate on token budget)
            if let Err(e) = state_machine.generation_complete(&output, tokens) {
                let _ = event_tx
                    .send(LoopEvent::IterationCompleted {
                        iteration,
                        outcome: IterationOutcome::ResourceLimitReached,
                    })
                    .await;
                // If resource limit, break gracefully; otherwise propagate
                if matches!(e, TransitionError::ResourceLimitReached(_)) {
                    break;
                }
                return Err(e.into());
            }

            let _ = event_tx
                .send(LoopEvent::GenerationCompleted {
                    content: output.clone(),
                    tokens,
                })
                .await;

            // =================================================================
            // DETECT
            // =================================================================

            // Check for meta-signals first
            let mut terminal_signal = false;
            if let Some(signal) = detect_meta_signal(&output, &detection_config) {
                debug!(?signal, "Meta-signal detected");
                let _ = event_tx
                    .send(LoopEvent::MetaSignalDetected {
                        signal: signal.clone(),
                    })
                    .await;

                match signal {
                    MetaSignal::Answer {
                        content,
                        confidence,
                        caveats,
                    } => {
                        state_machine.answer_detected(content, confidence, caveats)?;
                        let _ = event_tx
                            .send(LoopEvent::IterationCompleted {
                                iteration,
                                outcome: IterationOutcome::AnswerProvided,
                            })
                            .await;
                        terminal_signal = true;
                    },
                    MetaSignal::Stuck {
                        attempts, request, ..
                    } => {
                        state_machine.stuck_detected(attempts, request)?;
                        let _ = event_tx
                            .send(LoopEvent::IterationCompleted {
                                iteration,
                                outcome: IterationOutcome::Stuck,
                            })
                            .await;
                        terminal_signal = true;
                    },
                    MetaSignal::Yield {
                        partial_progress,
                        suggested_expertise,
                    } => {
                        let reason = suggested_expertise
                            .first()
                            .cloned()
                            .unwrap_or_else(|| "Agent yielded".to_string());
                        state_machine.yield_detected(partial_progress, reason)?;
                        let _ = event_tx
                            .send(LoopEvent::IterationCompleted {
                                iteration,
                                outcome: IterationOutcome::Yielded,
                            })
                            .await;
                        terminal_signal = true;
                    },
                    MetaSignal::Uncertain { .. } | MetaSignal::Thinking { .. } => {
                        // Non-terminal: continue to tool call detection
                    },
                }
            }

            if terminal_signal {
                break;
            }

            // Check for tool calls
            let detected_calls = self.detector.detect(&output);

            if detected_calls.is_empty() {
                // No tool calls and no terminal signal → treat as implicit answer
                debug!("No tool calls or signals — treating as implicit answer");
                state_machine.answer_detected(output.clone(), 0.5, vec![])?;
                let _ = event_tx
                    .send(LoopEvent::IterationCompleted {
                        iteration,
                        outcome: IterationOutcome::AnswerProvided,
                    })
                    .await;
                break;
            }

            // =================================================================
            // EXECUTE
            // =================================================================

            // Detecting → Executing (may terminate on tool call limit)
            if let Err(e) = state_machine.tool_calls_detected(detected_calls.len() as u32) {
                let _ = event_tx
                    .send(LoopEvent::IterationCompleted {
                        iteration,
                        outcome: IterationOutcome::ResourceLimitReached,
                    })
                    .await;
                if matches!(e, TransitionError::ResourceLimitReached(_)) {
                    break;
                }
                return Err(e.into());
            }

            // Add assistant message to context
            messages.push(Message::assistant(&output));

            let mut agentic_results = Vec::new();

            for call in &detected_calls {
                let _ = event_tx
                    .send(LoopEvent::ToolCallDetected {
                        call_id: call.id.clone(),
                        tool: call.name.clone(),
                    })
                    .await;

                let result = self.execute_single_tool(call, &tool_ctx, &event_tx).await;

                // Add tool result to conversation context
                let content = match &result.status {
                    ResultStatus::Success | ResultStatus::PartialSuccess { .. } => {
                        serde_json::to_string(&result.data).unwrap_or_default()
                    },
                    ResultStatus::Empty => "No results found.".to_string(),
                    ResultStatus::Failed { .. } => {
                        format!(
                            "Error: {}",
                            result
                                .data
                                .get("error")
                                .and_then(|e| e.as_str())
                                .unwrap_or("Tool execution failed")
                        )
                    },
                };
                messages.push(Message::tool_result(&call.id, &content));

                agentic_results.push(result);
            }

            // =================================================================
            // INTEGRATE
            // =================================================================

            // Executing → Integrating
            state_machine.execution_complete(agentic_results)?;

            let _ = event_tx
                .send(LoopEvent::IterationCompleted {
                    iteration,
                    outcome: IterationOutcome::ToolCallsExecuted {
                        count: detected_calls.len() as u32,
                    },
                })
                .await;

            // Integrating → Generating (or resource limit)
            match state_machine.continue_loop() {
                Ok(()) => {},
                Err(TransitionError::ResourceLimitReached(_)) => {
                    debug!("Resource limit reached, terminating loop");
                    break;
                },
                Err(e) => return Err(e.into()),
            }
        }

        let summary = state_machine.summary();
        info!(
            iterations = summary.iterations_completed,
            tool_calls = summary.tool_calls_made,
            "Agentic loop completed"
        );

        let _ = event_tx
            .send(LoopEvent::LoopCompleted {
                summary: summary.clone(),
            })
            .await;

        Ok(summary)
    }

    /// Computes the effective permission for a tool call.
    ///
    /// Checks runtime overrides from `ApproveAlways` decisions before falling
    /// back to the static `AutonomyGrant`. Forbidden always takes priority.
    fn effective_permission(&self, tool_name: &str, argument: Option<&str>) -> Permission {
        let static_perm = self.config.autonomy.check(tool_name, argument);

        // Forbidden cannot be overridden by runtime approvals
        if static_perm == Permission::Forbidden {
            return Permission::Forbidden;
        }

        // Runtime overrides from ApproveAlways only apply to RequiresApproval
        if static_perm == Permission::RequiresApproval {
            if let Some(gate) = &self.approval_gate {
                if gate.is_runtime_approved(tool_name) {
                    return Permission::Allowed;
                }
            }
        }

        static_perm
    }

    /// Execute a single tool call with autonomy checking and approval blocking.
    ///
    /// When an [`ApprovalGate`] is attached and the tool requires approval,
    /// the executor registers a pending request, emits a `ToolApprovalRequired`
    /// event, and blocks until a decision arrives or the configured timeout
    /// expires. Without a gate, the tool call fails immediately with a
    /// recoverable error (backward-compatible behavior).
    async fn execute_single_tool(
        &self,
        call: &DetectedCall,
        tool_ctx: &ToolContext,
        event_tx: &mpsc::Sender<LoopEvent>,
    ) -> AgenticToolResult {
        let argument_str = serde_json::to_string(&call.arguments).ok();
        let permission = self.effective_permission(&call.name, argument_str.as_deref());

        match permission {
            Permission::Forbidden => {
                warn!(tool = %call.name, "Tool call forbidden by autonomy grant");
                AgenticToolResult {
                    call_id: call.id.clone(),
                    tool_name: call.name.clone(),
                    status: ResultStatus::Failed { recoverable: false },
                    data: serde_json::json!({"error": "Tool call forbidden by autonomy grant"}),
                    confidence: Confidence::Measured,
                    latency_ms: 0,
                    truncated: false,
                }
            },
            Permission::RequiresApproval => {
                debug!(tool = %call.name, "Tool requires approval");
                let timeout = self.config.loop_config.approval_timeout;

                if let Some(gate) = &self.approval_gate {
                    // Register the pending request BEFORE emitting the event,
                    // so the entry exists by the time a client tries to deliver.
                    let rx = gate.request(&call.id, &call.name, call.arguments.clone());

                    let _ = event_tx
                        .send(LoopEvent::ToolApprovalRequired {
                            call_id: call.id.clone(),
                            tool: call.name.clone(),
                            arguments: call.arguments.clone(),
                            timeout_secs: timeout.as_secs(),
                            pending_count: gate.pending_count(),
                        })
                        .await;

                    // Block until decision or timeout
                    match tokio::time::timeout(timeout, rx).await {
                        Ok(Ok(
                            ApprovalDecision::Approve | ApprovalDecision::ApproveAlways { .. },
                        )) => {
                            // Approved — execute the tool
                            self.execute_approved_tool(call, tool_ctx, event_tx).await
                        },
                        Ok(Ok(ApprovalDecision::Deny)) => {
                            debug!(tool = %call.name, "Tool call denied by operator");
                            AgenticToolResult {
                                call_id: call.id.clone(),
                                tool_name: call.name.clone(),
                                status: ResultStatus::Failed { recoverable: true },
                                data: serde_json::json!({"error": "Tool call denied by operator"}),
                                confidence: Confidence::Measured,
                                latency_ms: 0,
                                truncated: false,
                            }
                        },
                        Ok(Err(_)) | Err(_) => {
                            // Oneshot dropped or timeout expired
                            let secs = timeout.as_secs_f64();
                            let msg = format!(
                                "Tool approval timed out after {secs:.1}s — tool call skipped."
                            );
                            warn!(tool = %call.name, "{msg}");
                            AgenticToolResult {
                                call_id: call.id.clone(),
                                tool_name: call.name.clone(),
                                status: ResultStatus::Failed { recoverable: true },
                                data: serde_json::json!({"error": msg}),
                                confidence: Confidence::Measured,
                                latency_ms: 0,
                                truncated: false,
                            }
                        },
                    }
                } else {
                    // No approval gate — emit event and fail immediately
                    let _ = event_tx
                        .send(LoopEvent::ToolApprovalRequired {
                            call_id: call.id.clone(),
                            tool: call.name.clone(),
                            arguments: call.arguments.clone(),
                            timeout_secs: timeout.as_secs(),
                            pending_count: 0,
                        })
                        .await;
                    AgenticToolResult {
                        call_id: call.id.clone(),
                        tool_name: call.name.clone(),
                        status: ResultStatus::Failed { recoverable: true },
                        data: serde_json::json!({"error": "Tool requires approval (not granted)"}),
                        confidence: Confidence::Measured,
                        latency_ms: 0,
                        truncated: false,
                    }
                }
            },
            Permission::Allowed => self.execute_approved_tool(call, tool_ctx, event_tx).await,
        }
    }

    /// Execute a tool that has already been approved (statically or via approval protocol).
    async fn execute_approved_tool(
        &self,
        call: &DetectedCall,
        tool_ctx: &ToolContext,
        event_tx: &mpsc::Sender<LoopEvent>,
    ) -> AgenticToolResult {
        let _ = event_tx
            .send(LoopEvent::ToolExecutionStarted {
                call_id: call.id.clone(),
                tool: call.name.clone(),
            })
            .await;

        let start = Instant::now();
        let beleth_call = BelethToolCall {
            name: call.name.clone(),
            params: call.arguments.clone(),
        };

        let tool_result = self.tools.execute(&beleth_call, tool_ctx).await;
        let latency_ms = start.elapsed().as_millis() as u64;

        let agentic_result = match tool_result {
            Ok(result) => {
                let status = if result.success {
                    ResultStatus::Success
                } else {
                    ResultStatus::Failed { recoverable: true }
                };
                // Always include the tool's output in data so the model can see it
                let data = match result.data {
                    Some(mut d) => {
                        // If data exists but doesn't have output, add it
                        if d.get("output").is_none() && d.get("content").is_none() {
                            if let Some(obj) = d.as_object_mut() {
                                obj.insert("output".to_string(), serde_json::json!(result.output));
                            }
                        }
                        d
                    },
                    None => {
                        if result.success {
                            serde_json::json!({"output": result.output})
                        } else {
                            serde_json::json!({"error": result.error.as_deref().unwrap_or("unknown error")})
                        }
                    },
                };
                AgenticToolResult {
                    call_id: call.id.clone(),
                    tool_name: call.name.clone(),
                    status,
                    data,
                    confidence: Confidence::Measured,
                    latency_ms,
                    truncated: false,
                }
            },
            Err(e) => {
                warn!(tool = %call.name, error = %e, "Tool execution failed");
                AgenticToolResult {
                    call_id: call.id.clone(),
                    tool_name: call.name.clone(),
                    status: ResultStatus::Failed { recoverable: true },
                    data: serde_json::json!({"error": e.to_string()}),
                    confidence: Confidence::Unknown,
                    latency_ms,
                    truncated: false,
                }
            },
        };

        let _ = event_tx
            .send(LoopEvent::ToolExecutionCompleted {
                call_id: call.id.clone(),
                result: agentic_result.clone(),
            })
            .await;

        agentic_result
    }

    fn build_initial_messages(&self, objective: &str) -> Vec<Message> {
        let system = self
            .config
            .system_prompt
            .as_deref()
            .unwrap_or("You are a helpful assistant.");
        let tool_desc = self.tools.to_qwen_native_description();
        let system_prompt = format!(
            "{system}\n\n{tool_desc}\n\n\
             You may express uncertainty with <uncertain>...</uncertain>, \
             signal you're stuck with <stuck>...</stuck>, \
             yield with <yield>...</yield>, \
             or provide a final answer with <answer confidence=\"0.9\">...</answer>."
        );

        vec![Message::system(system_prompt), Message::user(objective)]
    }

    fn build_generate_request(&self, messages: &[Message]) -> GenerateRequest {
        GenerateRequest::chat(messages.to_vec()).with_sampling(self.config.sampling.clone())
    }

    fn build_tool_context(&self) -> ToolContext {
        let mut ctx = ToolContext::new(&self.config.session_id);
        if let Some(ref wd) = self.config.working_dir {
            ctx.state.insert(
                "working_dir".to_string(),
                serde_json::json!(wd.to_string_lossy().to_string()),
            );
        }
        ctx
    }
}

// ---------------------------------------------------------------------------
// Tool call parsing
// ---------------------------------------------------------------------------

/// Parse `<tool_call>{"name": "...", "arguments": {...}}</tool_call>` tags.
fn parse_tool_call_tags(output: &str) -> Vec<DetectedCall> {
    let mut calls = Vec::new();
    let mut search_from = 0;
    let start_tag = "<tool_call>";
    let end_tag = "</tool_call>";

    while let Some(start) = output[search_from..].find(start_tag) {
        let abs_start = search_from + start + start_tag.len();
        if let Some(end) = output[abs_start..].find(end_tag) {
            let json_str = output[abs_start..abs_start + end].trim();
            if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(json_str) {
                let name = parsed
                    .get("name")
                    .and_then(|n| n.as_str())
                    .unwrap_or("")
                    .to_string();
                let arguments = parsed
                    .get("arguments")
                    .cloned()
                    .unwrap_or(serde_json::json!({}));

                if !name.is_empty() {
                    let id = format!("call_{}", uuid::Uuid::new_v4().simple());
                    calls.push(DetectedCall {
                        id,
                        name,
                        arguments,
                    });
                }
            }
            search_from = abs_start + end + end_tag.len();
        } else {
            break;
        }
    }

    calls
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_single_tool_call() {
        let output = r#"Let me read that file.
<tool_call>
{"name": "read_file", "arguments": {"path": "src/main.rs"}}
</tool_call>"#;

        let calls = parse_tool_call_tags(output);
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "read_file");
        assert_eq!(calls[0].arguments["path"], "src/main.rs");
        assert!(calls[0].id.starts_with("call_"));
    }

    #[test]
    fn test_parse_multiple_tool_calls() {
        let output = r#"I'll read both files.
<tool_call>
{"name": "read_file", "arguments": {"path": "a.rs"}}
</tool_call>
<tool_call>
{"name": "read_file", "arguments": {"path": "b.rs"}}
</tool_call>"#;

        let calls = parse_tool_call_tags(output);
        assert_eq!(calls.len(), 2);
        assert_eq!(calls[0].arguments["path"], "a.rs");
        assert_eq!(calls[1].arguments["path"], "b.rs");
    }

    #[test]
    fn test_parse_no_tool_calls() {
        let output = "Just a regular response with no tool calls.";
        let calls = parse_tool_call_tags(output);
        assert!(calls.is_empty());
    }

    #[test]
    fn test_parse_malformed_json_ignored() {
        let output = "<tool_call>not valid json</tool_call>";
        let calls = parse_tool_call_tags(output);
        assert!(calls.is_empty());
    }

    #[test]
    fn test_parse_empty_name_ignored() {
        let output = r#"<tool_call>{"name": "", "arguments": {}}</tool_call>"#;
        let calls = parse_tool_call_tags(output);
        assert!(calls.is_empty());
    }

    #[test]
    fn test_executor_config_builder() {
        let config = ExecutorConfig::new("test-session")
            .with_system_prompt("You are a coder.")
            .with_loop_config(LoopConfig {
                max_iterations: 5,
                ..LoopConfig::default()
            })
            .with_autonomy(AutonomyGrant::default());

        assert_eq!(config.session_id, "test-session");
        assert_eq!(config.loop_config.max_iterations, 5);
        assert!(config.system_prompt.is_some());
    }

    #[test]
    fn test_qwen_detector_default() {
        let detector = QwenToolCallDetector::new();
        let output = r#"<tool_call>{"name": "bash", "arguments": {"command": "ls"}}</tool_call>"#;
        let calls = detector.detect(output);
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "bash");
    }
}