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
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
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
//! OODA (Observe-Orient-Decide-Act) Execution Loop.
//!
//! This module provides an OODA executor inspired by Boyd's decision cycle,
//! which is used in Leviathan/Persona Framework for agent orchestration.
//!
//! ## Architecture
//!
//! ```text
//!                    ┌─────────────────────┐
//!                    │   Initial Context   │
//!                    └──────────┬──────────┘
//!//!            ┌──────────────────▼──────────────────┐
//!            │          OODA Loop                   │
//!            │  ┌────────────────────────────────┐ │
//!            │  │  1. Observe (gather info)      │ │
//!            │  │  2. Orient (analyze/synthesize)│ │
//!            │  │  3. Decide (choose action)     │ │
//!            │  │  4. Act (execute decision)     │ │
//!            │  └────────────────────────────────┘ │
//!            │              │                      │
//!            │     Continue │ or Done              │
//!            └──────────────┴──────────────────────┘
//!//!                    ┌──────────▼──────────┐
//!                    │    Final Result     │
//!                    └─────────────────────┘
//! ```
//!
//! ## Comparison with ReAct
//!
//! | Aspect | ReAct | OODA |
//! |--------|-------|------|
//! | Focus | Reasoning + Action | Situational awareness |
//! | Phases | Think → Act → Observe | Observe → Orient → Decide → Act |
//! | Strength | Explicit reasoning | Rapid adaptation |
//! | Use Case | Complex reasoning | Dynamic environments |

use std::sync::Arc;
use std::time::{Duration, Instant};

use async_trait::async_trait;
use infernum_core::{GenerateRequest, Message, Result, Role, SamplingParams};
use serde::{Deserialize, Serialize};

use crate::tool::{ToolCall, ToolContext, ToolRegistry};
use abaddon::{Engine, InferenceEngine};
use infernum_core::GenerateResponse;

// ============================================================================
// Helper Functions
// ============================================================================

/// Extracts text from the first choice in a generate response.
fn extract_response_text(response: &GenerateResponse) -> String {
    response
        .choices
        .first()
        .map(|c| c.text.clone())
        .unwrap_or_default()
}

/// JSON structure for parsing decision responses.
#[derive(Debug, Deserialize)]
struct DecisionJson {
    action_type: String,
    #[serde(default)]
    tool_name: Option<String>,
    #[serde(default)]
    tool_params: Option<serde_json::Value>,
    #[serde(default)]
    query: Option<String>,
    #[serde(default)]
    answer: Option<String>,
    #[serde(default)]
    question: Option<String>,
    #[serde(default)]
    reason: Option<String>,
    #[serde(default)]
    rationale: Option<String>,
    #[serde(default)]
    confidence: Option<f32>,
    #[serde(default)]
    expected_outcome: Option<String>,
}

/// Parses a decision response, attempting JSON first with text fallback.
fn parse_decision_response(text: &str) -> Result<OodaDecision> {
    // Try to extract JSON from the response (may be wrapped in markdown)
    let json_text = extract_json_block(text);

    if let Ok(parsed) = serde_json::from_str::<DecisionJson>(&json_text) {
        let action = match parsed.action_type.as_str() {
            "execute_tool" => DecisionAction::ExecuteTool {
                tool: parsed.tool_name.unwrap_or_else(|| "unknown".to_string()),
                params: parsed.tool_params.unwrap_or(serde_json::json!({})),
            },
            "gather_info" => DecisionAction::GatherInfo {
                query: parsed
                    .query
                    .unwrap_or_else(|| "Continue analysis".to_string()),
            },
            "final_answer" => DecisionAction::FinalAnswer {
                answer: parsed.answer.unwrap_or_else(|| text.to_string()),
            },
            "request_input" => DecisionAction::RequestInput {
                question: parsed
                    .question
                    .unwrap_or_else(|| "Need more information".to_string()),
            },
            "abort" => DecisionAction::Abort {
                reason: parsed
                    .reason
                    .unwrap_or_else(|| "Task cannot be completed".to_string()),
            },
            _ => return fallback_decision_parse(text),
        };

        return Ok(OodaDecision {
            action,
            rationale: parsed.rationale.unwrap_or_else(|| text.to_string()),
            confidence: parsed.confidence.unwrap_or(0.7),
            alternatives: Vec::new(),
            expected_outcome: parsed
                .expected_outcome
                .unwrap_or_else(|| "Task progress".to_string()),
        });
    }

    // Fallback to keyword-based parsing
    fallback_decision_parse(text)
}

/// Extracts JSON block from text (handles markdown code blocks).
fn extract_json_block(text: &str) -> String {
    // Try to find JSON in markdown code block
    if let Some(start) = text.find("```json") {
        if let Some(end) = text[start + 7..].find("```") {
            return text[start + 7..start + 7 + end].trim().to_string();
        }
    }

    // Try to find JSON in generic code block
    if let Some(start) = text.find("```") {
        let after_start = start + 3;
        // Skip language identifier if present
        let json_start = text[after_start..]
            .find('\n')
            .map(|n| after_start + n + 1)
            .unwrap_or(after_start);
        if let Some(end) = text[json_start..].find("```") {
            return text[json_start..json_start + end].trim().to_string();
        }
    }

    // Try to find raw JSON object
    if let Some(start) = text.find('{') {
        if let Some(end) = text.rfind('}') {
            if end > start {
                return text[start..=end].to_string();
            }
        }
    }

    text.to_string()
}

/// Fallback keyword-based decision parsing.
fn fallback_decision_parse(text: &str) -> Result<OodaDecision> {
    let text_lower = text.to_lowercase();

    let action = if text_lower.contains("final answer") || text_lower.contains("conclusion") {
        DecisionAction::FinalAnswer {
            answer: text.to_string(),
        }
    } else if text_lower.contains("abort")
        || text_lower.contains("cannot")
        || text_lower.contains("impossible")
    {
        DecisionAction::Abort {
            reason: text.to_string(),
        }
    } else if text_lower.contains("need input")
        || text_lower.contains("clarify")
        || text_lower.contains("ask user")
    {
        DecisionAction::RequestInput {
            question: text.to_string(),
        }
    } else {
        DecisionAction::GatherInfo {
            query: "Continue analysis".to_string(),
        }
    };

    Ok(OodaDecision {
        action,
        rationale: text.to_string(),
        confidence: 0.6, // Lower confidence for fallback parsing
        alternatives: Vec::new(),
        expected_outcome: "Task progress".to_string(),
    })
}

// ============================================================================
// Configuration
// ============================================================================

/// Configuration for the OODA executor.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OodaConfig {
    /// Maximum iterations before stopping.
    pub max_iterations: u32,
    /// Maximum tokens per phase.
    pub max_tokens_per_phase: u32,
    /// Temperature for observation phase.
    pub observe_temperature: f32,
    /// Temperature for orientation phase.
    pub orient_temperature: f32,
    /// Temperature for decision phase.
    pub decide_temperature: f32,
    /// Timeout for tool execution.
    pub tool_timeout: Duration,
    /// Whether to enable parallel observation gathering.
    pub parallel_observe: bool,
    /// Minimum confidence to accept a decision (0.0 - 1.0).
    pub min_confidence: f32,
    /// Number of retries for failed actions.
    pub action_retry_count: u32,
}

impl Default for OodaConfig {
    fn default() -> Self {
        Self {
            max_iterations: 25,
            max_tokens_per_phase: 1024,
            observe_temperature: 0.3,
            orient_temperature: 0.5,
            decide_temperature: 0.7,
            tool_timeout: Duration::from_secs(30),
            parallel_observe: true,
            min_confidence: 0.7,
            action_retry_count: 2,
        }
    }
}

impl OodaConfig {
    /// Creates a config for fast, reactive tasks.
    #[must_use]
    pub fn fast() -> Self {
        Self {
            max_iterations: 10,
            max_tokens_per_phase: 512,
            observe_temperature: 0.2,
            orient_temperature: 0.3,
            decide_temperature: 0.5,
            min_confidence: 0.6,
            ..Default::default()
        }
    }

    /// Creates a config for thorough, deliberate tasks.
    #[must_use]
    pub fn thorough() -> Self {
        Self {
            max_iterations: 50,
            max_tokens_per_phase: 2048,
            observe_temperature: 0.4,
            orient_temperature: 0.6,
            decide_temperature: 0.8,
            min_confidence: 0.85,
            action_retry_count: 3,
            ..Default::default()
        }
    }
}

// ============================================================================
// OODA Phases
// ============================================================================

/// The current phase in the OODA loop.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OodaPhase {
    /// Gathering information from the environment.
    Observe,
    /// Analyzing and synthesizing observations.
    Orient,
    /// Determining the best course of action.
    Decide,
    /// Executing the decision.
    Act,
}

impl std::fmt::Display for OodaPhase {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Observe => write!(f, "Observe"),
            Self::Orient => write!(f, "Orient"),
            Self::Decide => write!(f, "Decide"),
            Self::Act => write!(f, "Act"),
        }
    }
}

/// An observation gathered during the Observe phase.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OodaObservation {
    /// Source of the observation (tool name, environment, etc.).
    pub source: String,
    /// The observed data.
    pub data: String,
    /// Relevance score (0.0 - 1.0).
    pub relevance: f32,
    /// Whether this observation is from the current iteration.
    pub is_current: bool,
}

/// Orientation analysis from the Orient phase.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OodaOrientation {
    /// Current situation assessment.
    pub situation: String,
    /// Identified patterns or insights.
    pub patterns: Vec<String>,
    /// Potential threats or blockers.
    pub threats: Vec<String>,
    /// Opportunities identified.
    pub opportunities: Vec<String>,
    /// Mental model updates.
    pub model_updates: Vec<String>,
}

/// A decision from the Decide phase.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OodaDecision {
    /// The chosen action.
    pub action: DecisionAction,
    /// Rationale for the decision.
    pub rationale: String,
    /// Confidence in the decision (0.0 - 1.0).
    pub confidence: f32,
    /// Alternative actions considered.
    pub alternatives: Vec<String>,
    /// Expected outcome.
    pub expected_outcome: String,
}

/// Types of decisions the agent can make.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DecisionAction {
    /// Execute a tool.
    ExecuteTool {
        /// Tool name.
        tool: String,
        /// Tool parameters.
        params: serde_json::Value,
    },
    /// Gather more information.
    GatherInfo {
        /// What information to gather.
        query: String,
    },
    /// Provide final answer.
    FinalAnswer {
        /// The final answer.
        answer: String,
    },
    /// Request human input.
    RequestInput {
        /// The question to ask.
        question: String,
    },
    /// Abort the task.
    Abort {
        /// Reason for aborting.
        reason: String,
    },
}

/// Result of an action from the Act phase.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OodaActionResult {
    /// Whether the action succeeded.
    pub success: bool,
    /// Output from the action.
    pub output: String,
    /// Error message if failed.
    pub error: Option<String>,
    /// Duration of the action.
    pub duration_ms: u64,
    /// Side effects observed.
    pub side_effects: Vec<String>,
}

// ============================================================================
// OODA Step and Result
// ============================================================================

/// A complete OODA cycle step.
#[derive(Debug, Clone, Serialize)]
pub struct OodaStep {
    /// Iteration number.
    pub iteration: u32,
    /// Observations gathered.
    pub observations: Vec<OodaObservation>,
    /// Orientation analysis.
    pub orientation: Option<OodaOrientation>,
    /// Decision made.
    pub decision: Option<OodaDecision>,
    /// Action result.
    pub action_result: Option<OodaActionResult>,
    /// Total duration of this step.
    pub duration_ms: u64,
}

/// Why the OODA loop completed.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OodaCompletionReason {
    /// Task completed successfully with final answer.
    FinalAnswer,
    /// Maximum iterations reached.
    MaxIterations,
    /// Timeout exceeded.
    Timeout,
    /// Agent decided to abort.
    Aborted {
        /// The reason for aborting.
        reason: String,
    },
    /// Unrecoverable error.
    Error {
        /// The error message.
        message: String,
    },
    /// Human input requested.
    HumanInputRequired {
        /// The question to ask the human.
        question: String,
    },
}

/// Result of OODA execution.
#[derive(Debug, Clone, Serialize)]
pub struct OodaResult {
    /// Final answer if available.
    pub answer: Option<String>,
    /// Why execution completed.
    pub completion_reason: OodaCompletionReason,
    /// All steps executed.
    pub steps: Vec<OodaStep>,
    /// Total iterations.
    pub total_iterations: u32,
    /// Total duration in milliseconds.
    pub total_duration_ms: u64,
}

// ============================================================================
// OODA Executor
// ============================================================================

/// Callback for OODA execution events.
#[async_trait]
pub trait OodaCallback: Send + Sync {
    /// Called when entering a new phase.
    async fn on_phase(&self, iteration: u32, phase: OodaPhase);
    /// Called when an observation is gathered.
    async fn on_observation(&self, observation: &OodaObservation);
    /// Called when orientation is complete.
    async fn on_orientation(&self, orientation: &OodaOrientation);
    /// Called when a decision is made.
    async fn on_decision(&self, decision: &OodaDecision);
    /// Called when an action completes.
    async fn on_action(&self, result: &OodaActionResult);
}

/// No-op callback implementation.
pub struct NoOpOodaCallback;

#[async_trait]
impl OodaCallback for NoOpOodaCallback {
    async fn on_phase(&self, _iteration: u32, _phase: OodaPhase) {}
    async fn on_observation(&self, _observation: &OodaObservation) {}
    async fn on_orientation(&self, _orientation: &OodaOrientation) {}
    async fn on_decision(&self, _decision: &OodaDecision) {}
    async fn on_action(&self, _result: &OodaActionResult) {}
}

/// The OODA executor.
pub struct OodaExecutor {
    engine: Arc<Engine>,
    tools: Arc<ToolRegistry>,
    config: OodaConfig,
    callback: Arc<dyn OodaCallback>,
}

impl OodaExecutor {
    /// Creates a new OODA executor.
    pub fn new(engine: Arc<Engine>, tools: Arc<ToolRegistry>, config: OodaConfig) -> Self {
        Self {
            engine,
            tools,
            config,
            callback: Arc::new(NoOpOodaCallback),
        }
    }

    /// Sets a callback for execution events.
    #[must_use]
    pub fn with_callback(mut self, callback: Arc<dyn OodaCallback>) -> Self {
        self.callback = callback;
        self
    }

    /// Executes the OODA loop for the given task.
    pub async fn execute(&self, task: &str) -> Result<OodaResult> {
        let start = Instant::now();
        let mut steps = Vec::new();
        let mut messages = vec![
            Message {
                role: Role::System,
                content: self.system_prompt(),
                name: None,
                tool_calls: None,
                tool_call_id: None,
            },
            Message {
                role: Role::User,
                content: task.to_string(),
                name: None,
                tool_calls: None,
                tool_call_id: None,
            },
        ];

        for iteration in 1..=self.config.max_iterations {
            let step_start = Instant::now();

            // OBSERVE
            self.callback.on_phase(iteration, OodaPhase::Observe).await;
            let observations = self.observe(&messages).await?;
            for obs in &observations {
                self.callback.on_observation(obs).await;
            }

            // ORIENT
            self.callback.on_phase(iteration, OodaPhase::Orient).await;
            let orientation = self.orient(&messages, &observations).await?;
            self.callback.on_orientation(&orientation).await;

            // DECIDE
            self.callback.on_phase(iteration, OodaPhase::Decide).await;
            let decision = self.decide(&messages, &orientation).await?;
            self.callback.on_decision(&decision).await;

            // ACT
            self.callback.on_phase(iteration, OodaPhase::Act).await;
            let action_result = self.act(&decision).await?;
            self.callback.on_action(&action_result).await;

            // Record step
            let step = OodaStep {
                iteration,
                observations,
                orientation: Some(orientation),
                decision: Some(decision.clone()),
                action_result: Some(action_result.clone()),
                duration_ms: step_start.elapsed().as_millis() as u64,
            };
            steps.push(step);

            // Update context with action result
            messages.push(Message {
                role: Role::Assistant,
                content: format!(
                    "[OODA Iteration {}]\nDecision: {}\nResult: {}",
                    iteration, decision.rationale, action_result.output
                ),
                name: None,
                tool_calls: None,
                tool_call_id: None,
            });

            // Check for completion
            match &decision.action {
                DecisionAction::FinalAnswer { answer } => {
                    return Ok(OodaResult {
                        answer: Some(answer.clone()),
                        completion_reason: OodaCompletionReason::FinalAnswer,
                        steps,
                        total_iterations: iteration,
                        total_duration_ms: start.elapsed().as_millis() as u64,
                    });
                },
                DecisionAction::Abort { reason } => {
                    return Ok(OodaResult {
                        answer: None,
                        completion_reason: OodaCompletionReason::Aborted {
                            reason: reason.clone(),
                        },
                        steps,
                        total_iterations: iteration,
                        total_duration_ms: start.elapsed().as_millis() as u64,
                    });
                },
                DecisionAction::RequestInput { question } => {
                    return Ok(OodaResult {
                        answer: None,
                        completion_reason: OodaCompletionReason::HumanInputRequired {
                            question: question.clone(),
                        },
                        steps,
                        total_iterations: iteration,
                        total_duration_ms: start.elapsed().as_millis() as u64,
                    });
                },
                _ => {},
            }
        }

        Ok(OodaResult {
            answer: None,
            completion_reason: OodaCompletionReason::MaxIterations,
            steps,
            total_iterations: self.config.max_iterations,
            total_duration_ms: start.elapsed().as_millis() as u64,
        })
    }

    fn system_prompt(&self) -> String {
        format!(
            "You are an autonomous agent using the OODA (Observe-Orient-Decide-Act) decision loop.\n\n\
             Available tools:\n{}\n\n\
             For each iteration:\n\
             1. OBSERVE: Gather relevant information\n\
             2. ORIENT: Analyze patterns, threats, and opportunities\n\
             3. DECIDE: Choose the best action with rationale\n\
             4. ACT: Execute the decision\n\n\
             Always provide structured JSON responses.",
            self.tools.list().join(", ")
        )
    }

    async fn observe(&self, messages: &[Message]) -> Result<Vec<OodaObservation>> {
        let prompt = "Analyze the current situation. What observations are relevant? \
                      List key facts, context, and any information gaps.";

        let mut observe_messages = messages.to_vec();
        observe_messages.push(Message {
            role: Role::User,
            content: prompt.to_string(),
            name: None,
            tool_calls: None,
            tool_call_id: None,
        });

        let request = GenerateRequest::new(observe_messages).with_sampling(
            SamplingParams::default()
                .with_temperature(self.config.observe_temperature)
                .with_max_tokens(self.config.max_tokens_per_phase),
        );

        let response = self.engine.generate(request).await?;

        // Parse observations from response
        Ok(vec![OodaObservation {
            source: "context".to_string(),
            data: extract_response_text(&response),
            relevance: 1.0,
            is_current: true,
        }])
    }

    async fn orient(
        &self,
        messages: &[Message],
        observations: &[OodaObservation],
    ) -> Result<OodaOrientation> {
        let obs_text: String = observations
            .iter()
            .map(|o| format!("- [{}]: {}", o.source, o.data))
            .collect::<Vec<_>>()
            .join("\n");

        let prompt = format!(
            "Based on these observations:\n{}\n\n\
             Provide orientation analysis:\n\
             1. Current situation assessment\n\
             2. Patterns or insights\n\
             3. Potential threats or blockers\n\
             4. Opportunities\n\
             5. Mental model updates needed",
            obs_text
        );

        let mut orient_messages = messages.to_vec();
        orient_messages.push(Message {
            role: Role::User,
            content: prompt,
            name: None,
            tool_calls: None,
            tool_call_id: None,
        });

        let request = GenerateRequest::new(orient_messages).with_sampling(
            SamplingParams::default()
                .with_temperature(self.config.orient_temperature)
                .with_max_tokens(self.config.max_tokens_per_phase),
        );

        let response = self.engine.generate(request).await?;
        let text = extract_response_text(&response);

        Ok(OodaOrientation {
            situation: text,
            patterns: Vec::new(),
            threats: Vec::new(),
            opportunities: Vec::new(),
            model_updates: Vec::new(),
        })
    }

    async fn decide(
        &self,
        messages: &[Message],
        orientation: &OodaOrientation,
    ) -> Result<OodaDecision> {
        let tools_list = self.tools.list().join(", ");
        let prompt = format!(
            r#"Based on orientation:
{}

Decide the next action. Respond with JSON in this exact format:
{{
  "action_type": "execute_tool" | "gather_info" | "final_answer" | "request_input" | "abort",
  "tool_name": "tool name if execute_tool",
  "tool_params": {{}},
  "query": "query if gather_info",
  "answer": "answer if final_answer",
  "question": "question if request_input",
  "reason": "reason if abort",
  "rationale": "why this action",
  "confidence": 0.0-1.0,
  "expected_outcome": "what you expect"
}}

Available tools: {}"#,
            orientation.situation, tools_list
        );

        let mut decide_messages = messages.to_vec();
        decide_messages.push(Message {
            role: Role::User,
            content: prompt,
            name: None,
            tool_calls: None,
            tool_call_id: None,
        });

        let request = GenerateRequest::new(decide_messages).with_sampling(
            SamplingParams::default()
                .with_temperature(self.config.decide_temperature)
                .with_max_tokens(self.config.max_tokens_per_phase),
        );

        let response = self.engine.generate(request).await?;
        let text = extract_response_text(&response);

        // Parse JSON decision with fallback
        parse_decision_response(&text)
    }

    async fn act(&self, decision: &OodaDecision) -> Result<OodaActionResult> {
        let start = Instant::now();

        match &decision.action {
            DecisionAction::ExecuteTool { tool, params } => {
                let tool_call = ToolCall {
                    name: tool.clone(),
                    params: params.clone(),
                };
                let context = ToolContext::new("ooda-agent");

                match tokio::time::timeout(
                    self.config.tool_timeout,
                    self.tools.execute(&tool_call, &context),
                )
                .await
                {
                    Ok(Ok(result)) => Ok(OodaActionResult {
                        success: result.success,
                        output: result.output,
                        error: result.error,
                        duration_ms: start.elapsed().as_millis() as u64,
                        side_effects: Vec::new(),
                    }),
                    Ok(Err(e)) => Ok(OodaActionResult {
                        success: false,
                        output: String::new(),
                        error: Some(e.to_string()),
                        duration_ms: start.elapsed().as_millis() as u64,
                        side_effects: Vec::new(),
                    }),
                    Err(_) => Ok(OodaActionResult {
                        success: false,
                        output: String::new(),
                        error: Some("Tool execution timed out".to_string()),
                        duration_ms: start.elapsed().as_millis() as u64,
                        side_effects: Vec::new(),
                    }),
                }
            },
            DecisionAction::GatherInfo { query } => Ok(OodaActionResult {
                success: true,
                output: format!("Gathering info: {}", query),
                error: None,
                duration_ms: start.elapsed().as_millis() as u64,
                side_effects: Vec::new(),
            }),
            DecisionAction::FinalAnswer { answer } => Ok(OodaActionResult {
                success: true,
                output: answer.clone(),
                error: None,
                duration_ms: start.elapsed().as_millis() as u64,
                side_effects: Vec::new(),
            }),
            DecisionAction::RequestInput { question } => Ok(OodaActionResult {
                success: true,
                output: format!("Requesting input: {}", question),
                error: None,
                duration_ms: start.elapsed().as_millis() as u64,
                side_effects: Vec::new(),
            }),
            DecisionAction::Abort { reason } => Ok(OodaActionResult {
                success: false,
                output: format!("Aborted: {}", reason),
                error: Some(reason.clone()),
                duration_ms: start.elapsed().as_millis() as u64,
                side_effects: Vec::new(),
            }),
        }
    }
}

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

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

    #[test]
    fn test_ooda_config_default() {
        let config = OodaConfig::default();
        assert_eq!(config.max_iterations, 25);
        assert_eq!(config.tool_timeout, Duration::from_secs(30));
        assert!(config.parallel_observe);
    }

    #[test]
    fn test_ooda_config_fast() {
        let config = OodaConfig::fast();
        assert_eq!(config.max_iterations, 10);
        assert!((config.min_confidence - 0.6).abs() < 0.01);
    }

    #[test]
    fn test_ooda_config_thorough() {
        let config = OodaConfig::thorough();
        assert_eq!(config.max_iterations, 50);
        assert!((config.min_confidence - 0.85).abs() < 0.01);
    }

    #[test]
    fn test_ooda_phase_display() {
        assert_eq!(format!("{}", OodaPhase::Observe), "Observe");
        assert_eq!(format!("{}", OodaPhase::Orient), "Orient");
        assert_eq!(format!("{}", OodaPhase::Decide), "Decide");
        assert_eq!(format!("{}", OodaPhase::Act), "Act");
    }

    #[test]
    fn test_ooda_observation_creation() {
        let obs = OodaObservation {
            source: "tool:search".to_string(),
            data: "Found 5 results".to_string(),
            relevance: 0.9,
            is_current: true,
        };
        assert_eq!(obs.source, "tool:search");
        assert!(obs.is_current);
    }

    #[test]
    fn test_ooda_orientation_creation() {
        let orient = OodaOrientation {
            situation: "Task in progress".to_string(),
            patterns: vec!["Pattern A".to_string()],
            threats: vec!["Threat B".to_string()],
            opportunities: vec!["Opportunity C".to_string()],
            model_updates: Vec::new(),
        };
        assert_eq!(orient.patterns.len(), 1);
        assert_eq!(orient.threats.len(), 1);
    }

    #[test]
    fn test_decision_action_variants() {
        let tool_action = DecisionAction::ExecuteTool {
            tool: "search".to_string(),
            params: serde_json::json!({"query": "test"}),
        };
        assert!(matches!(tool_action, DecisionAction::ExecuteTool { .. }));

        let final_action = DecisionAction::FinalAnswer {
            answer: "Done".to_string(),
        };
        assert!(matches!(final_action, DecisionAction::FinalAnswer { .. }));

        let abort_action = DecisionAction::Abort {
            reason: "Impossible".to_string(),
        };
        assert!(matches!(abort_action, DecisionAction::Abort { .. }));
    }

    #[test]
    fn test_ooda_step_creation() {
        let step = OodaStep {
            iteration: 1,
            observations: vec![OodaObservation {
                source: "test".to_string(),
                data: "data".to_string(),
                relevance: 1.0,
                is_current: true,
            }],
            orientation: None,
            decision: None,
            action_result: None,
            duration_ms: 100,
        };
        assert_eq!(step.iteration, 1);
        assert_eq!(step.observations.len(), 1);
    }

    #[test]
    fn test_ooda_completion_reasons() {
        let final_answer = OodaCompletionReason::FinalAnswer;
        assert!(matches!(final_answer, OodaCompletionReason::FinalAnswer));

        let aborted = OodaCompletionReason::Aborted {
            reason: "test".to_string(),
        };
        assert!(matches!(aborted, OodaCompletionReason::Aborted { .. }));

        let human_input = OodaCompletionReason::HumanInputRequired {
            question: "help?".to_string(),
        };
        assert!(matches!(
            human_input,
            OodaCompletionReason::HumanInputRequired { .. }
        ));
    }

    #[test]
    fn test_ooda_result_creation() {
        let result = OodaResult {
            answer: Some("42".to_string()),
            completion_reason: OodaCompletionReason::FinalAnswer,
            steps: Vec::new(),
            total_iterations: 3,
            total_duration_ms: 1500,
        };
        assert_eq!(result.answer, Some("42".to_string()));
        assert_eq!(result.total_iterations, 3);
    }

    #[test]
    fn test_ooda_action_result_creation() {
        let result = OodaActionResult {
            success: true,
            output: "Done".to_string(),
            error: None,
            duration_ms: 50,
            side_effects: vec!["created file".to_string()],
        };
        assert!(result.success);
        assert_eq!(result.side_effects.len(), 1);
    }

    #[test]
    fn test_extract_json_block_raw() {
        let text = r#"{"action_type": "final_answer", "answer": "42"}"#;
        let extracted = extract_json_block(text);
        assert!(extracted.contains("final_answer"));
    }

    #[test]
    fn test_extract_json_block_markdown() {
        let text = r#"Here's the decision:
```json
{"action_type": "gather_info", "query": "search"}
```
That's my choice."#;
        let extracted = extract_json_block(text);
        assert!(extracted.contains("gather_info"));
        assert!(!extracted.contains("```"));
    }

    #[test]
    fn test_extract_json_block_generic_code_block() {
        let text = r#"```
{"action_type": "abort", "reason": "impossible"}
```"#;
        let extracted = extract_json_block(text);
        assert!(extracted.contains("abort"));
    }

    #[test]
    fn test_parse_decision_json_final_answer() {
        let json =
            r#"{"action_type": "final_answer", "answer": "The answer is 42", "confidence": 0.95}"#;
        let decision = parse_decision_response(json).unwrap();
        assert!(matches!(
            decision.action,
            DecisionAction::FinalAnswer { .. }
        ));
        assert!((decision.confidence - 0.95).abs() < 0.01);
    }

    #[test]
    fn test_parse_decision_json_execute_tool() {
        let json = r#"{"action_type": "execute_tool", "tool_name": "search", "tool_params": {"query": "rust"}}"#;
        let decision = parse_decision_response(json).unwrap();
        match decision.action {
            DecisionAction::ExecuteTool { tool, params } => {
                assert_eq!(tool, "search");
                assert_eq!(params["query"], "rust");
            },
            _ => panic!("Expected ExecuteTool"),
        }
    }

    #[test]
    fn test_parse_decision_json_gather_info() {
        let json = r#"{"action_type": "gather_info", "query": "What files exist?"}"#;
        let decision = parse_decision_response(json).unwrap();
        match decision.action {
            DecisionAction::GatherInfo { query } => {
                assert_eq!(query, "What files exist?");
            },
            _ => panic!("Expected GatherInfo"),
        }
    }

    #[test]
    fn test_parse_decision_fallback() {
        let text = "I think the final answer is that we need more data.";
        let decision = parse_decision_response(text).unwrap();
        assert!(matches!(
            decision.action,
            DecisionAction::FinalAnswer { .. }
        ));
        assert!((decision.confidence - 0.6).abs() < 0.01); // Fallback has lower confidence
    }

    #[test]
    fn test_parse_decision_fallback_abort() {
        let text = "This task is impossible to complete without more resources.";
        let decision = parse_decision_response(text).unwrap();
        assert!(matches!(decision.action, DecisionAction::Abort { .. }));
    }

    #[test]
    fn test_fallback_decision_parse_request_input() {
        let text = "I need to clarify what you mean by that.";
        let decision = fallback_decision_parse(text).unwrap();
        assert!(matches!(
            decision.action,
            DecisionAction::RequestInput { .. }
        ));
    }
}