paladin-battalion 0.5.1

Multi-agent orchestration runtime for the Paladin framework — Formation, Phalanx, Campaign, Chain of Command, Conclave, Council, Grove, Maneuver, Commander
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
//! Phalanx Execution Service
//!
//! Provides orchestration logic for executing Paladins in concurrent Phalanx pattern.

use chrono::Utc;
use futures::future::{BoxFuture, FutureExt, select_ok};
use log::{debug, info, warn};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Semaphore;
use tokio::time::{Duration, timeout};
use tokio_util::sync::CancellationToken;
use uuid::Uuid;

use crate::error_aggregation::AggregatedError;
use paladin_core::platform::container::battalion::phalanx::{AggregationStrategy, Phalanx};
use paladin_core::platform::container::battalion::{
    BattalionError, BattalionResult, ErrorStrategy, TokenUsage,
};
use paladin_core::platform::container::herald::Herald;
use paladin_core::platform::container::paladin_error::PaladinError;
use paladin_ports::output::paladin_port::{PaladinPort, PaladinResult};

#[cfg(test)]
use paladin_core::platform::container::battalion::BattalionStatus;

#[cfg(test)]
use tokio::sync::mpsc;

/// Service for executing Phalanx patterns
///
/// Orchestrates concurrent Paladin execution with configurable aggregation strategies,
/// concurrency limiting via semaphore, and cancellation support.
///
/// # Example
///
/// ```ignore
/// let service = PhalanxExecutionService::new(paladin_port);
/// let result = service.execute(&phalanx, "Analyze this data").await?;
/// ```
pub struct PhalanxExecutionService {
    paladin_port: Arc<dyn PaladinPort>,
    /// Optional Herald for formatting Battalion results
    herald: Option<Arc<dyn Herald>>,
}

impl PhalanxExecutionService {
    /// Create a new Phalanx execution service
    pub fn new(paladin_port: Arc<dyn PaladinPort>) -> Self {
        Self {
            paladin_port,
            herald: None,
        }
    }

    /// Set the Herald for formatting results
    ///
    /// This allows runtime override of the default Herald. If set, this Herald
    /// will be used to format Battalion results.
    ///
    /// # Arguments
    ///
    /// * `herald` - The Herald to use for formatting
    ///
    /// # Example
    ///
    /// ```ignore
    /// let service = PhalanxExecutionService::new(paladin_port)
    ///     .with_herald(Arc::new(JsonHerald::new()));
    /// ```
    pub fn with_herald(mut self, herald: Arc<dyn Herald>) -> Self {
        self.herald = Some(herald);
        self
    }

    /// Format a Battalion result using the configured Herald
    ///
    /// Converts the Battalion result into the Herald's output format. If no Herald
    /// is configured, returns None.
    ///
    /// # Arguments
    ///
    /// * `result` - The Battalion result to format
    ///
    /// # Returns
    ///
    /// * `Ok(Some(String))` - Formatted output if Herald is configured
    /// * `Ok(None)` - If no Herald is configured
    /// * `Err(BattalionError)` - If formatting fails
    ///
    /// # Example
    ///
    /// ```ignore
    /// let formatted = service.format_result(&result)?;
    /// if let Some(output) = formatted {
    ///     println!("{}", output);
    /// }
    /// ```
    pub fn format_result(
        &self,
        result: &BattalionResult,
    ) -> Result<Option<String>, BattalionError> {
        match &self.herald {
            Some(herald) => {
                // Herald now uses actual BattalionResult directly - no conversion needed!
                herald
                    .format_battalion_result(result)
                    .map(Some)
                    .map_err(|e| {
                        BattalionError::PhalanxError(format!("Herald formatting error: {}", e))
                    })
            }
            None => Ok(None),
        }
    }

    /// Execute a Phalanx with the given input
    ///
    /// Paladins are executed concurrently according to the aggregation strategy.
    /// Respects timeout, concurrency limits, and error strategies.
    pub async fn execute(
        &self,
        phalanx: &Phalanx,
        input: &str,
    ) -> Result<BattalionResult, BattalionError> {
        let config = phalanx.config();
        let timeout_duration = Duration::from_secs(config.timeout_seconds);

        info!(
            "Starting Phalanx execution: {} with {} Paladins",
            config.name,
            phalanx.paladin_count()
        );

        // Wrap execution with timeout
        match timeout(timeout_duration, self.execute_internal(phalanx, input)).await {
            Ok(result) => result,
            Err(_) => {
                warn!(
                    "Phalanx '{}' timed out after {} seconds",
                    config.name, config.timeout_seconds
                );
                Err(BattalionError::Timeout(config.timeout_seconds))
            }
        }
    }

    /// Execute Phalanx with cancellation support
    ///
    /// Allows external cancellation of ongoing execution
    pub async fn execute_with_cancellation(
        &self,
        phalanx: &Phalanx,
        input: &str,
        cancellation_token: CancellationToken,
    ) -> Result<BattalionResult, BattalionError> {
        let config = phalanx.config();
        let timeout_duration = Duration::from_secs(config.timeout_seconds);

        tokio::select! {
            result = timeout(timeout_duration, self.execute_internal(phalanx, input)) => {
                match result {
                    Ok(r) => r,
                    Err(_) => Err(BattalionError::Timeout(config.timeout_seconds)),
                }
            }
            _ = cancellation_token.cancelled() => {
                info!("Phalanx '{}' cancelled", config.name);
                Err(BattalionError::Cancelled)
            }
        }
    }

    /// Internal execution logic
    async fn execute_internal(
        &self,
        phalanx: &Phalanx,
        input: &str,
    ) -> Result<BattalionResult, BattalionError> {
        let config = phalanx.config();
        let started_at = Utc::now();
        let battalion_id = Uuid::new_v4();

        // Validate aggregation strategy
        self.validate_aggregation_strategy(phalanx)?;

        // Get paladin names for metrics tracking
        let paladin_names: Vec<String> = phalanx
            .paladins()
            .iter()
            .map(|p| p.node.name.clone())
            .collect();

        // Execute based on aggregation strategy
        let (paladin_results, errors) = match phalanx.aggregation_strategy() {
            AggregationStrategy::CollectAll => self.execute_collect_all(phalanx, input).await?,
            AggregationStrategy::FirstSuccess => self.execute_first_success(phalanx, input).await?,
            AggregationStrategy::Majority => self.execute_majority(phalanx, input).await?,
            AggregationStrategy::Custom(fn_name) => {
                return Err(BattalionError::ConfigurationError(format!(
                    "Custom aggregation '{}' not yet implemented",
                    fn_name
                )));
            }
        };

        // Handle errors according to error strategy
        if !errors.is_empty() {
            match config.error_strategy {
                ErrorStrategy::FailFast => {
                    let mut agg_error = AggregatedError::new(phalanx.paladin_count());
                    for error in errors {
                        agg_error.add_error(BattalionError::ExecutionError(error));
                    }
                    return Err(BattalionError::AggregationError(format!(
                        "Phalanx execution failed with {} errors",
                        agg_error.errors.len()
                    )));
                }
                ErrorStrategy::ContinueOnError => {
                    warn!(
                        "Phalanx '{}' completed with {} errors (ContinueOnError)",
                        config.name,
                        errors.len()
                    );
                }
                ErrorStrategy::RetryThenContinue => {
                    // Retries handled at Paladin level in concurrent execution
                    warn!(
                        "Phalanx '{}' completed with {} errors after retries",
                        config.name,
                        errors.len()
                    );
                }
            }
        }

        // Determine final output based on aggregation
        let final_output = if paladin_results.is_empty() {
            String::new()
        } else {
            paladin_results.last().unwrap().output.clone()
        };

        // Build per-paladin metrics from execution results
        let failed_names: Vec<String> = errors
            .iter()
            .filter_map(|e| e.split(':').next().map(|s| s.trim().to_string()))
            .collect();

        let mut per_paladin_times = HashMap::new();
        let mut per_paladin_tokens = HashMap::new();
        let mut total_tokens: u64 = 0;

        // Track which successful results map to which paladin names
        // Results are returned in order matching successful paladins
        let successful_names: Vec<&String> = paladin_names
            .iter()
            .filter(|name| !failed_names.contains(name))
            .collect();

        for (i, result) in paladin_results.iter().enumerate() {
            if let Some(name) = successful_names.get(i) {
                per_paladin_times.insert((*name).clone(), result.execution_time_ms);
                per_paladin_tokens
                    .insert((*name).clone(), TokenUsage::from_total(result.token_count));
                total_tokens += u64::from(result.token_count);
            }
        }

        let paladin_success_count = paladin_results.len();
        let paladin_failure_count = errors.len();

        let completed_at = Utc::now();
        Ok(BattalionResult {
            battalion_id,
            battalion_name: config.name.clone(),
            paladin_results,
            started_at,
            completed_at,
            final_output,
            status: paladin_core::platform::container::battalion::BattalionStatus::Completed,
            strategy_used: paladin_core::platform::container::battalion::BattalionStrategy::Phalanx,
            strategy_selection_reasoning: None,
            strategy_selection_time_ms: 0,
            per_paladin_times,
            per_paladin_tokens,
            total_tokens,
            paladin_success_count,
            paladin_failure_count,
        })
    }

    /// Validate aggregation strategy requirements
    fn validate_aggregation_strategy(&self, phalanx: &Phalanx) -> Result<(), BattalionError> {
        if matches!(
            phalanx.aggregation_strategy(),
            AggregationStrategy::Majority
        ) && phalanx.paladin_count() < 3
        {
            return Err(BattalionError::ValidationError(
                "Majority aggregation requires at least 3 Paladins".to_string(),
            ));
        }
        Ok(())
    }

    /// CollectAll: Wait for all Paladins to complete
    async fn execute_collect_all(
        &self,
        phalanx: &Phalanx,
        input: &str,
    ) -> Result<(Vec<PaladinResult>, Vec<String>), BattalionError> {
        let semaphore = phalanx
            .max_concurrency()
            .map(|max| Arc::new(Semaphore::new(max)));

        let mut tasks = Vec::new();

        for paladin in phalanx.paladins() {
            let paladin_clone: paladin_core::platform::container::paladin::Paladin =
                paladin.clone();
            let input_clone = input.to_string();
            let port = self.paladin_port.clone();
            let semaphore_clone = semaphore.clone();

            let task: tokio::task::JoinHandle<Result<PaladinResult, PaladinError>> =
                tokio::spawn(async move {
                    // Acquire semaphore permit if concurrency limiting is enabled
                    let _permit = if let Some(sem) = &semaphore_clone {
                        Some(sem.acquire().await.unwrap())
                    } else {
                        None
                    };

                    debug!("Executing Paladin: {}", paladin_clone.node.name);
                    port.execute(&paladin_clone, &input_clone).await
                });

            tasks.push(task);
        }

        // Wait for all tasks to complete
        let mut results = Vec::new();
        let mut errors = Vec::new();

        for (i, task) in tasks.into_iter().enumerate() {
            match task.await {
                Ok(Ok(result)) => results.push(result),
                Ok(Err(e)) => {
                    let paladin_name = &phalanx.paladins()[i].node.name;
                    errors.push(format!("{}: {}", paladin_name, e));
                }
                Err(e) => {
                    let paladin_name = &phalanx.paladins()[i].node.name;
                    errors.push(format!("{}: Task join error: {}", paladin_name, e));
                }
            }
        }

        Ok((results, errors))
    }

    /// FirstSuccess: Return first successful result (early termination)
    async fn execute_first_success(
        &self,
        phalanx: &Phalanx,
        input: &str,
    ) -> Result<(Vec<PaladinResult>, Vec<String>), BattalionError> {
        let mut futures: Vec<BoxFuture<Result<PaladinResult, BattalionError>>> = Vec::new();

        for paladin in phalanx.paladins() {
            let paladin_clone: paladin_core::platform::container::paladin::Paladin =
                paladin.clone();
            let input_clone = input.to_string();
            let port = self.paladin_port.clone();

            let fut: BoxFuture<Result<PaladinResult, BattalionError>> = async move {
                port.execute(&paladin_clone, &input_clone)
                    .await
                    .map_err(|e| BattalionError::PaladinError(e.to_string()))
            }
            .boxed();

            futures.push(fut);
        }

        // Use select_ok to get first successful result
        match select_ok(futures).await {
            Ok((result, _remaining)) => {
                info!("FirstSuccess: Got first successful result");
                Ok((vec![result], vec![]))
            }
            Err(e) => {
                // All failed
                Err(BattalionError::ExecutionError(format!(
                    "All Paladins failed: {}",
                    e
                )))
            }
        }
    }

    /// Majority: Require consensus (≥50% agreement)
    async fn execute_majority(
        &self,
        phalanx: &Phalanx,
        input: &str,
    ) -> Result<(Vec<PaladinResult>, Vec<String>), BattalionError> {
        // First collect all results
        let (results, errors) = self.execute_collect_all(phalanx, input).await?;

        if results.is_empty() {
            return Err(BattalionError::ExecutionError(
                "No Paladin results to determine majority".to_string(),
            ));
        }

        // Count output occurrences
        let mut output_counts: HashMap<String, usize> = HashMap::new();
        for result in &results {
            *output_counts.entry(result.output.clone()).or_insert(0) += 1;
        }

        // Find majority (>50% threshold)
        let total_count = results.len();
        let majority_threshold = (total_count / 2) + 1;

        let majority_output = output_counts
            .iter()
            .find(|(_, count)| **count >= majority_threshold)
            .map(|(output, _)| output.clone());

        match majority_output {
            Some(output) => {
                info!(
                    "Majority consensus reached: {} out of {} Paladins agreed",
                    output_counts.get(&output).unwrap(),
                    total_count
                );
                // Return only the majority result
                let majority_result = results.into_iter().find(|r| r.output == output).unwrap();
                Ok((vec![majority_result], errors))
            }
            None => Err(BattalionError::ExecutionError(
                "No majority consensus reached".to_string(),
            )),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use paladin_core::base::entity::node::Node;
    use paladin_core::platform::container::battalion::BattalionConfig;
    use paladin_core::platform::container::paladin::MaxLoops;
    use paladin_core::platform::container::paladin::{Paladin, PaladinData, PaladinStatus};
    use paladin_core::platform::container::paladin_error::PaladinError;
    use paladin_ports::output::paladin_port::StopReason;
    use std::sync::Mutex;

    /// Mock PaladinPort for testing
    struct MockPaladinPort {
        call_count: Arc<Mutex<usize>>,
        fail_paladin_names: Arc<Mutex<Vec<String>>>,
        delay_ms: u64,
        output_override: Arc<Mutex<HashMap<String, String>>>,
    }

    impl MockPaladinPort {
        fn new() -> Self {
            Self {
                call_count: Arc::new(Mutex::new(0)),
                fail_paladin_names: Arc::new(Mutex::new(Vec::new())),
                delay_ms: 10,
                output_override: Arc::new(Mutex::new(HashMap::new())),
            }
        }

        fn with_failures(self, names: Vec<String>) -> Self {
            *self.fail_paladin_names.lock().unwrap() = names;
            self
        }

        fn with_output_override(self, overrides: HashMap<String, String>) -> Self {
            *self.output_override.lock().unwrap() = overrides;
            self
        }
    }

    #[async_trait]
    impl PaladinPort for MockPaladinPort {
        async fn execute(
            &self,
            paladin: &Paladin,
            input: &str,
        ) -> Result<PaladinResult, PaladinError> {
            *self.call_count.lock().unwrap() += 1;

            tokio::time::sleep(Duration::from_millis(self.delay_ms)).await;

            // Check if this Paladin should fail
            let should_fail = self
                .fail_paladin_names
                .lock()
                .unwrap()
                .contains(&paladin.node.name);

            if should_fail {
                return Err(PaladinError::ExecutionError(format!(
                    "Mock failure for {}",
                    paladin.node.name
                )));
            }

            // Check for output override
            let output = if let Some(override_output) =
                self.output_override.lock().unwrap().get(&paladin.node.name)
            {
                override_output.clone()
            } else {
                format!("{}: {}", paladin.node.name, input)
            };

            Ok(PaladinResult {
                output,
                token_count: 50,
                execution_time_ms: self.delay_ms,
                loop_count: 1,
                stop_reason: StopReason::Completed,
                ..Default::default()
            })
        }

        async fn execute_stream(
            &self,
            _paladin: &Paladin,
            _input: &str,
        ) -> Result<
            tokio::sync::mpsc::Receiver<
                Result<paladin_ports::output::paladin_port::PaladinStreamChunk, PaladinError>,
            >,
            PaladinError,
        > {
            let (_tx, rx) = mpsc::channel(1);
            Ok(rx)
        }

        fn validate(&self, _paladin: &Paladin) -> Result<(), PaladinError> {
            Ok(())
        }
    }

    fn create_paladin(name: &str) -> Paladin {
        let data = PaladinData {
            system_prompt: format!("{} prompt", name),
            name: name.to_string(),
            user_name: "TestUser".to_string(),
            model: "gpt-4".to_string(),
            temperature: 0.7,
            max_loops: MaxLoops::Fixed(3),
            stop_words: vec![],
            status: PaladinStatus::Idle,
            vision_enabled: false,
            ..Default::default()
        };
        Node::new(data, Some(name.to_string()))
    }

    #[tokio::test]
    async fn test_phalanx_service_creation() {
        let mock_port = Arc::new(MockPaladinPort::new());
        let _service = PhalanxExecutionService::new(mock_port);
    }

    #[tokio::test]
    async fn test_collect_all_strategy_success() {
        let p1 = create_paladin("Agent1");
        let p2 = create_paladin("Agent2");
        let p3 = create_paladin("Agent3");

        let phalanx =
            Phalanx::new(vec![p1, p2, p3], BattalionConfig::new("test_collect_all")).unwrap();

        let mock_port = Arc::new(MockPaladinPort::new());
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await;

        assert!(result.is_ok());
        let battalion_result = result.unwrap();
        assert_eq!(battalion_result.paladin_results.len(), 3);
        assert_eq!(battalion_result.status, BattalionStatus::Completed);
    }

    #[tokio::test]
    async fn test_collect_all_with_concurrency_limit() {
        let paladins: Vec<Paladin> = (1..=10)
            .map(|i| create_paladin(&format!("Agent{}", i)))
            .collect();

        let phalanx = Phalanx::new(paladins, BattalionConfig::new("test_concurrency"))
            .unwrap()
            .with_max_concurrency(3);

        let mock_port = Arc::new(MockPaladinPort::new());
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await;

        assert!(result.is_ok());
        let battalion_result = result.unwrap();
        assert_eq!(battalion_result.paladin_results.len(), 10);
    }

    #[tokio::test]
    async fn test_first_success_strategy() {
        let p1 = create_paladin("Agent1");
        let p2 = create_paladin("Agent2");
        let p3 = create_paladin("Agent3");

        let phalanx = Phalanx::new(vec![p1, p2, p3], BattalionConfig::new("test_first"))
            .unwrap()
            .with_aggregation(AggregationStrategy::FirstSuccess);

        let mock_port = Arc::new(MockPaladinPort::new());
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await;

        assert!(result.is_ok());
        let battalion_result = result.unwrap();
        // FirstSuccess returns only one result
        assert_eq!(battalion_result.paladin_results.len(), 1);
    }

    #[tokio::test]
    async fn test_majority_strategy_with_consensus() {
        let p1 = create_paladin("Agent1");
        let p2 = create_paladin("Agent2");
        let p3 = create_paladin("Agent3");

        let phalanx = Phalanx::new(vec![p1, p2, p3], BattalionConfig::new("test_majority"))
            .unwrap()
            .with_aggregation(AggregationStrategy::Majority);

        // Set up so Agent1 and Agent2 return "Result A", Agent3 returns different
        let mut overrides = HashMap::new();
        overrides.insert("Agent1".to_string(), "Result A".to_string());
        overrides.insert("Agent2".to_string(), "Result A".to_string());
        overrides.insert("Agent3".to_string(), "Result B".to_string());

        let mock_port = Arc::new(MockPaladinPort::new().with_output_override(overrides));
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await;

        assert!(result.is_ok());
        let battalion_result = result.unwrap();
        assert_eq!(battalion_result.paladin_results.len(), 1);
        assert_eq!(battalion_result.paladin_results[0].output, "Result A");
    }

    #[tokio::test]
    async fn test_majority_strategy_validation() {
        let p1 = create_paladin("Agent1");
        let p2 = create_paladin("Agent2");

        let phalanx = Phalanx::new(vec![p1, p2], BattalionConfig::new("test_majority_invalid"))
            .unwrap()
            .with_aggregation(AggregationStrategy::Majority);

        let mock_port = Arc::new(MockPaladinPort::new());
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await;

        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("at least 3 Paladins")
        );
    }

    #[tokio::test]
    async fn test_partial_failures_with_continue_on_error() {
        let p1 = create_paladin("Agent1");
        let p2 = create_paladin("Agent2");
        let p3 = create_paladin("Agent3");

        let config = BattalionConfig::new("test_partial_fail")
            .with_error_strategy(ErrorStrategy::ContinueOnError);

        let phalanx = Phalanx::new(vec![p1, p2, p3], config).unwrap();

        let mock_port = Arc::new(MockPaladinPort::new().with_failures(vec!["Agent2".to_string()]));
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await;

        assert!(result.is_ok());
        let battalion_result = result.unwrap();
        // Only 2 successful results (Agent1 and Agent3)
        assert_eq!(battalion_result.paladin_results.len(), 2);
    }

    #[tokio::test]
    async fn test_timeout_enforcement() {
        let p1 = create_paladin("Agent1");
        let p2 = create_paladin("Agent2");

        let config = BattalionConfig::new("test_timeout").with_timeout(1);

        let phalanx = Phalanx::new(vec![p1, p2], config).unwrap();

        let mut mock_port = MockPaladinPort::new();
        mock_port.delay_ms = 2000; // 2 seconds > 1 second timeout

        let service = PhalanxExecutionService::new(Arc::new(mock_port));

        let result = service.execute(&phalanx, "Test input").await;

        assert!(result.is_err());
        match result.unwrap_err() {
            BattalionError::Timeout(seconds) => assert_eq!(seconds, 1),
            _ => panic!("Expected Timeout error"),
        }
    }

    #[tokio::test]
    async fn test_cancellation_support() {
        let p1 = create_paladin("Agent1");
        let p2 = create_paladin("Agent2");

        let phalanx = Phalanx::new(vec![p1, p2], BattalionConfig::new("test_cancel")).unwrap();

        let mut mock_port = MockPaladinPort::new();
        mock_port.delay_ms = 1000; // 1 second delay

        let service = PhalanxExecutionService::new(Arc::new(mock_port));
        let cancellation_token = CancellationToken::new();
        let token_clone = cancellation_token.clone();

        // Cancel after 100ms
        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(100)).await;
            token_clone.cancel();
        });

        let result = service
            .execute_with_cancellation(&phalanx, "Test input", cancellation_token)
            .await;

        assert!(result.is_err());
        match result.unwrap_err() {
            BattalionError::Cancelled => {}
            _ => panic!("Expected Cancelled error"),
        }
    }

    #[tokio::test]
    async fn test_phalanx_per_paladin_timing() {
        let p1 = create_paladin("Analyst");
        let p2 = create_paladin("Reviewer");
        let p3 = create_paladin("Editor");

        let phalanx = Phalanx::new(vec![p1, p2, p3], BattalionConfig::new("timing_test")).unwrap();

        let mock_port = Arc::new(MockPaladinPort::new());
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await.unwrap();

        // per_paladin_times should be populated with entries for each Paladin
        assert_eq!(result.per_paladin_times.len(), 3);
        assert!(result.per_paladin_times.contains_key("Analyst"));
        assert!(result.per_paladin_times.contains_key("Reviewer"));
        assert!(result.per_paladin_times.contains_key("Editor"));

        // All times should be > 0 (mock has 10ms delay)
        for time_ms in result.per_paladin_times.values() {
            assert!(*time_ms > 0, "Paladin execution time should be > 0");
        }
    }

    #[tokio::test]
    async fn test_phalanx_per_paladin_tokens() {
        let p1 = create_paladin("Analyst");
        let p2 = create_paladin("Reviewer");

        let phalanx = Phalanx::new(vec![p1, p2], BattalionConfig::new("tokens_test")).unwrap();

        let mock_port = Arc::new(MockPaladinPort::new());
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await.unwrap();

        // per_paladin_tokens should be populated from PaladinResult.token_count
        assert_eq!(result.per_paladin_tokens.len(), 2);
        assert!(result.per_paladin_tokens.contains_key("Analyst"));
        assert!(result.per_paladin_tokens.contains_key("Reviewer"));

        // Mock returns token_count=50, so total_tokens for each should be 50
        let analyst_tokens = result.per_paladin_tokens.get("Analyst").unwrap();
        assert_eq!(analyst_tokens.total_tokens, 50);

        // total_tokens should be the sum across all paladins
        assert_eq!(result.total_tokens, 100); // 50 + 50
    }

    #[tokio::test]
    async fn test_phalanx_metrics_with_partial_failures() {
        let p1 = create_paladin("Success1");
        let p2 = create_paladin("Failure1");
        let p3 = create_paladin("Success2");

        let config = BattalionConfig::new("partial_metrics")
            .with_error_strategy(ErrorStrategy::ContinueOnError);

        let phalanx = Phalanx::new(vec![p1, p2, p3], config).unwrap();

        let mock_port =
            Arc::new(MockPaladinPort::new().with_failures(vec!["Failure1".to_string()]));
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await.unwrap();

        // Only successful paladins should have timing and token entries
        assert_eq!(result.per_paladin_times.len(), 2);
        assert!(result.per_paladin_times.contains_key("Success1"));
        assert!(result.per_paladin_times.contains_key("Success2"));
        assert!(!result.per_paladin_times.contains_key("Failure1"));

        assert_eq!(result.per_paladin_tokens.len(), 2);
        assert!(!result.per_paladin_tokens.contains_key("Failure1"));

        // total_tokens should only count successful paladins
        assert_eq!(result.total_tokens, 100); // 50 + 50

        // Success/failure counts should be accurate
        assert_eq!(result.paladin_success_count, 2);
        assert_eq!(result.paladin_failure_count, 1);
    }

    #[tokio::test]
    async fn test_phalanx_metrics_success_failure_counts() {
        let p1 = create_paladin("Agent1");
        let p2 = create_paladin("Agent2");
        let p3 = create_paladin("Agent3");

        let phalanx = Phalanx::new(vec![p1, p2, p3], BattalionConfig::new("count_test")).unwrap();

        let mock_port = Arc::new(MockPaladinPort::new());
        let service = PhalanxExecutionService::new(mock_port);

        let result = service.execute(&phalanx, "Test input").await.unwrap();

        // All succeed
        assert_eq!(result.paladin_success_count, 3);
        assert_eq!(result.paladin_failure_count, 0);
    }
}