paladin-ports 0.5.1

Port trait definitions (hexagonal architecture contracts) for the Paladin framework
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
//! Battalion Port - Multi-Agent Orchestration Abstraction
//!
//! This module defines the output port (interface) for Battalion execution following
//! Hexagonal Architecture principles. The `BattalionPort` trait provides a clean abstraction
//! that allows the application layer to orchestrate multiple Paladin agents without being
//! coupled to specific orchestration strategies or execution backends.
//!
//! # Purpose
//!
//! The Battalion port enables coordinated execution of multiple Paladin agents using various
//! orchestration patterns while maintaining a clean separation between the domain logic
//! (what a Battalion is) and the execution logic (how it orchestrates). This allows you to:
//!
//! - Orchestrate multiple Paladins with different patterns (Formation, Phalanx, Campaign, Chain of Command)
//! - Execute Paladins sequentially, in parallel, or in complex graph workflows
//! - Monitor long-running Battalion operations asynchronously
//! - Cancel in-flight executions gracefully
//! - Track execution metadata (timing, status, results from each Paladin)
//! - Implement retry logic, checkpointing, or recovery consistently
//! - Test orchestration logic without real agent execution
//!
//! # Hexagonal Architecture (Ports & Adapters)
//!
//! ```text
//! ┌──────────────────────────────────────────────────────────────┐
//! │                    Application Layer                          │
//! │  ┌────────────────────────────────────────────────────────┐  │
//! │  │  Commander (Strategic Router)                           │  │
//! │  │  - Analyzes tasks                                       │  │
//! │  │  - Selects optimal Battalion pattern                    │  │
//! │  │  - Routes to appropriate orchestration service          │  │
//! │  └─────────────────────┬────────────────────────────────────┘  │
//! │                        │                                       │
//! │                        ↓                                       │
//! │  ┌────────────────────────────────────────────────────────┐  │
//! │  │  BattalionPort (trait)                                  │  │
//! │  │  - execute()                                            │  │
//! │  │  - status()                                             │  │
//! │  │  - cancel()                                             │  │
//! │  └────────────────────┬───────────────────────────────────┘  │
//! └─────────────────────────┼────────────────────────────────────┘
//!//!//!   ┌──────────────────────────────────────────────────────────┐
//!   │  Battalion Execution Services (adapters)                  │
//!   │  - FormationService (sequential)                          │
//!   │  - PhalanxService (parallel)                              │
//!   │  - CampaignService (graph/DAG)                            │
//!   │  - ChainOfCommandService (hierarchical)                   │
//!   │  - Each uses PaladinPort to execute individual agents     │
//!   └──────────────────────────────────────────────────────────┘
//! ```
//!
//! # Battalion Orchestration Patterns
//!
//! ## Formation (Sequential Execution)
//!
//! Execute Paladins one after another, passing output as input to the next:
//!
//! ```text
//! Paladin A → Result A → Paladin B → Result B → Paladin C → Final Result
//! ```
//!
//! **Use Cases**: Multi-stage pipelines, data transformation chains, sequential analysis
//!
//! ## Phalanx (Parallel Execution)
//!
//! Execute Paladins concurrently and aggregate results:
//!
//! ```text
//! Paladin A ─┐
//! Paladin B ─┼→ Aggregate → Final Result
//! Paladin C ─┘
//! ```
//!
//! **Use Cases**: Parallel analysis, consensus building, concurrent data processing
//!
//! ## Campaign (Graph/DAG Workflow)
//!
//! Execute Paladins in a directed acyclic graph with conditional branches:
//!
//! ```text
//!         ┌→ Paladin B ─┐
//! Start ─→│              ├→ Paladin D → End
//!         └→ Paladin C ─┘
//! ```
//!
//! **Use Cases**: Complex workflows, conditional logic, decision trees
//!
//! ## Chain of Command (Hierarchical Delegation)
//!
//! A commander Paladin delegates subtasks to specialist Paladins:
//!
//! ```text
//! Commander → [Subtask 1 → Specialist A]
//!          → [Subtask 2 → Specialist B]
//!          → [Subtask 3 → Specialist C]
//!          → Aggregate Results
//! ```
//!
//! **Use Cases**: Task decomposition, specialist routing, hierarchical processing
//!
//! # Common Use Cases
//!
//! ## 1. Execute Battalion with Monitoring
//!
//! ```ignore
//! use paladin::application::ports::output::battalion_port::{
//!     BattalionPort, BattalionStatus
//! };
//! use uuid::Uuid;
//! use std::sync::Arc;
//! use tokio::time::{sleep, Duration};
//!
//! async fn run_battalion_with_monitoring(
//!     battalion_port: Arc<dyn BattalionPort>,
//!     battalion_id: Uuid,
//! ) -> Result<(), Box<dyn std::error::Error>> {
//!     // Start execution in background
//!     let port_clone = battalion_port.clone();
//!     let execute_handle = tokio::spawn(async move {
//!         port_clone.execute(battalion_id).await
//!     });
//!
//!     // Monitor progress
//!     loop {
//!         let status = battalion_port.status(battalion_id).await?;
//!
//!         match status {
//!             BattalionStatus::Pending => println!("Waiting to start..."),
//!             BattalionStatus::Running => println!("Executing..."),
//!             BattalionStatus::Completed => {
//!                 println!("Battalion completed!");
//!                 break;
//!             }
//!             BattalionStatus::Failed(err) => {
//!                 eprintln!("Battalion failed: {}", err);
//!                 break;
//!             }
//!             BattalionStatus::Cancelled => {
//!                 println!("Battalion was cancelled");
//!                 break;
//!             }
//!         }
//!
//!         sleep(Duration::from_secs(2)).await;
//!     }
//!
//!     // Get final result
//!     let result = execute_handle.await??;
//!     println!("Final output: {}", result.final_output);
//!     println!("Total time: {}ms", result.total_execution_time_ms);
//!
//!     Ok(())
//! }
//! ```
//!
//! ## 2. Execute with Timeout and Cancellation
//!
//! ```ignore
//! use paladin::application::ports::output::battalion_port::BattalionPort;
//! use uuid::Uuid;
//! use std::sync::Arc;
//! use tokio::time::{timeout, Duration};
//!
//! async fn execute_with_timeout(
//!     battalion_port: Arc<dyn BattalionPort>,
//!     battalion_id: Uuid,
//!     timeout_seconds: u64,
//! ) -> Result<String, Box<dyn std::error::Error>> {
//!     let port_clone = battalion_port.clone();
//!
//!     match timeout(
//!         Duration::from_secs(timeout_seconds),
//!         port_clone.execute(battalion_id),
//!     )
//!     .await
//!     {
//!         Ok(Ok(result)) => {
//!             println!("Completed in time");
//!             Ok(result.final_output)
//!         }
//!         Ok(Err(e)) => {
//!             eprintln!("Execution failed: {}", e);
//!             Err(e.into())
//!         }
//!         Err(_) => {
//!             println!("Timeout reached, cancelling...");
//!             battalion_port.cancel(battalion_id).await?;
//!             Err("Battalion execution timed out".into())
//!         }
//!     }
//! }
//! ```
//!
//! ## 3. Retry Failed Battalions
//!
//! ```ignore
//! use paladin::application::ports::output::battalion_port::BattalionPort;
//! use paladin::core::platform::container::battalion::BattalionError;
//! use uuid::Uuid;
//! use std::sync::Arc;
//! use tokio::time::{sleep, Duration};
//!
//! async fn execute_with_retry(
//!     battalion_port: Arc<dyn BattalionPort>,
//!     battalion_id: Uuid,
//!     max_retries: u32,
//! ) -> Result<String, BattalionError> {
//!     let mut attempts = 0;
//!     let mut backoff = Duration::from_secs(1);
//!
//!     loop {
//!         match battalion_port.execute(battalion_id).await {
//!             Ok(result) => return Ok(result.final_output),
//!             Err(e) if attempts >= max_retries => return Err(e),
//!             Err(BattalionError::PaladinError(_)) => {
//!                 println!("Attempt {} failed, retrying...", attempts + 1);
//!                 sleep(backoff).await;
//!                 backoff *= 2;
//!                 attempts += 1;
//!             }
//!             Err(e) => return Err(e), // Non-retryable error
//!         }
//!     }
//! }
//! ```
//!
//! ## 4. Multi-Battalion Orchestration
//!
//! ```ignore
//! use paladin::application::ports::output::battalion_port::BattalionPort;
//! use uuid::Uuid;
//! use std::sync::Arc;
//!
//! async fn execute_multiple_battalions(
//!     battalion_port: Arc<dyn BattalionPort>,
//!     battalion_ids: Vec<Uuid>,
//! ) -> Vec<Result<String, Box<dyn std::error::Error>>> {
//!     let handles: Vec<_> = battalion_ids
//!         .into_iter()
//!         .map(|id| {
//!             let port = battalion_port.clone();
//!             tokio::spawn(async move {
//!                 let result = port.execute(id).await?;
//!                 Ok::<String, Box<dyn std::error::Error>>(result.final_output)
//!             })
//!         })
//!         .collect();
//!
//!     // Wait for all to complete
//!     let mut results = Vec::new();
//!     for handle in handles {
//!         results.push(handle.await.unwrap());
//!     }
//!
//!     results
//! }
//! ```
//!
//! # Error Handling
//!
//! BattalionPort methods return `BattalionError` which includes:
//!
//! | Error | Retryable? | Recovery Strategy |
//! |-------|------------|-------------------|
//! | ValidationError | No | Fix Battalion configuration (invalid pattern, missing Paladins) |
//! | PaladinError | Maybe | Retry individual Paladin or entire Battalion |
//! | ExecutionError | Maybe | Check error message, retry if transient |
//! | NotFound | No | Ensure Battalion was registered before execution |
//! | AlreadyRunning | No | Wait for completion or cancel existing execution |
//! | Cancelled | No | This is success if cancellation was intentional |
//!
//! ## Status Enum
//!
//! The `BattalionStatus` enum indicates current execution state:
//!
//! - `Pending`: Battalion registered but not started
//! - `Running`: Currently executing Paladins
//! - `Completed`: All Paladins executed successfully
//! - `Failed(String)`: Execution failed with error message
//! - `Cancelled`: Execution was cancelled via `cancel()`
//!
//! # Thread Safety
//!
//! BattalionPort is `Send + Sync`, allowing safe use across async task boundaries.
//! This is critical for:
//! - Concurrent Battalion execution
//! - Status monitoring from separate tasks
//! - Timeout/cancellation from watchdog tasks
//!
//! # Implementation Notes
//!
//! ## Adapter Implementation Checklist
//!
//! When implementing a Battalion adapter:
//!
//! 1. **Pattern Support**: Implement at least one pattern (Formation, Phalanx, Campaign, Chain of Command)
//! 2. **State Management**: Track Battalion status (Pending → Running → Completed/Failed/Cancelled)
//! 3. **Paladin Execution**: Use PaladinPort to execute individual agents
//! 4. **Result Aggregation**: Combine outputs from multiple Paladins
//! 5. **Error Propagation**: Handle Paladin failures according to pattern (fail-fast vs continue)
//! 6. **Cancellation Support**: Implement graceful cancellation of running Paladins
//! 7. **Concurrency Management**: Handle parallel execution safely (Phalanx, Campaign)
//! 8. **Status Updates**: Provide accurate real-time status via `status()`
//! 9. **Timing Metadata**: Track total execution time and per-Paladin timing
//! 10. **Idempotency**: Handle duplicate `execute()` calls gracefully
//! 11. **Cleanup**: Release resources on completion, failure, or cancellation
//!
//! ## Performance Considerations
//!
//! - **Paladin Concurrency**: Execute independent Paladins in parallel (Phalanx, Campaign)
//! - **Resource Limits**: Limit concurrent Paladin count to avoid resource exhaustion
//! - **Checkpointing**: Save intermediate results for long-running Campaigns
//! - **Status Caching**: Cache status to reduce overhead of frequent polling
//! - **Async Execution**: Use non-blocking operations throughout
//! - **Error Fast-Fail**: For Formation, fail immediately on first error to save resources
//!
//! ## Testing Strategy
//!
//! ```ignore
//! use paladin::application::ports::output::battalion_port::{
//!     BattalionPort, BattalionStatus
//! };
//! use paladin::core::platform::container::battalion::{
//!     BattalionResult, BattalionError
//! };
//! use async_trait::async_trait;
//! use uuid::Uuid;
//!
//! /// Mock Battalion port for testing
//! struct MockBattalionPort {
//!     should_fail: bool,
//!     execution_time_ms: u64,
//! }
//!
//! #[async_trait]
//! impl BattalionPort for MockBattalionPort {
//!     async fn execute(&self, _battalion_id: Uuid) -> Result<BattalionResult, BattalionError> {
//!         if self.should_fail {
//!             return Err(BattalionError::PaladinError("Mock failure".into()));
//!         }
//!
//!         Ok(BattalionResult {
//!             final_output: "Mock result".into(),
//!             paladin_results: Vec::new(),
//!             total_execution_time_ms: self.execution_time_ms,
//!             pattern_used: "Formation".into(),
//!         })
//!     }
//!
//!     async fn status(&self, _battalion_id: Uuid) -> Result<BattalionStatus, BattalionError> {
//!         if self.should_fail {
//!             Ok(BattalionStatus::Failed("Mock error".into()))
//!         } else {
//!             Ok(BattalionStatus::Completed)
//!         }
//!     }
//!
//!     async fn cancel(&self, _battalion_id: Uuid) -> Result<(), BattalionError> {
//!         Ok(())
//!     }
//! }
//! ```
//!
//! # Common Pitfalls
//!
//! 1. **Not Checking Status**: Always check status after long operations to detect failures
//! 2. **Missing Cancellation**: Implement proper cancellation to avoid zombie executions
//! 3. **Resource Leaks**: Ensure cleanup happens on all exit paths (success, error, cancel)
//! 4. **Race Conditions**: Protect shared state in concurrent patterns (Phalanx, Campaign)
//! 5. **Error Swallowing**: Don't silently ignore Paladin errors, propagate appropriately
//! 6. **Blocking Operations**: All operations must be async to avoid blocking
//! 7. **Status Polling**: Don't poll status too frequently, use reasonable intervals (1-5s)
//! 8. **Result Ordering**: Maintain Paladin execution order in results for debugging
//!
//! # Related Modules
//!
//! - [`paladin_core::platform::container::battalion`] - Battalion domain entities
//! - [`paladin_battalion::battalion`] - Battalion execution services (adapters)
//! - [`paladin_ports::output::paladin_port`] - Individual agent execution
//! - [`paladin_battalion::commander`] - Strategic pattern routing
//! - [`paladin_core::platform::container::citadel`] - Checkpointing and recovery

use async_trait::async_trait;
use uuid::Uuid;

use paladin_core::platform::container::battalion::{
    BattalionError, BattalionResult, BattalionStatus,
};

/// Port abstraction for Battalion execution
///
/// This trait defines the interface that any Battalion orchestration implementation
/// must satisfy. It follows the hexagonal architecture pattern, allowing the
/// core domain to remain independent of orchestration details.
///
/// # Capabilities
///
/// - **Orchestration Patterns**: Support for Formation, Phalanx, Campaign, Chain of Command
/// - **Asynchronous Execution**: Non-blocking execution of multi-agent workflows
/// - **Status Monitoring**: Query current execution state without blocking
/// - **Cancellation**: Gracefully cancel in-flight executions
/// - **Result Aggregation**: Combine outputs from multiple Paladins
/// - **Error Handling**: Propagate errors according to pattern (fail-fast vs continue)
///
/// # Requirements
///
/// Implementations must:
/// - Be `Send + Sync` for safe concurrent use across async tasks
/// - Integrate with PaladinPort for individual agent execution
/// - Track Battalion state (Pending → Running → Completed/Failed/Cancelled)
/// - Support at least one orchestration pattern
/// - Handle concurrent Paladin execution safely
/// - Implement graceful cancellation
/// - Aggregate results from multiple Paladins
/// - Provide accurate real-time status updates
///
/// # Examples
///
/// ## Basic Implementation Pattern
///
/// ```ignore
/// use async_trait::async_trait;
/// use paladin::application::ports::output::battalion_port::{
///     BattalionPort
/// };
/// use paladin::application::ports::output::paladin_port::PaladinPort;
/// use paladin::core::platform::container::battalion::{
///     BattalionResult, BattalionStatus, BattalionError
/// };
/// use uuid::Uuid;
/// use std::sync::Arc;
/// use std::collections::HashMap;
/// use tokio::sync::RwLock;
///
/// struct SimpleBattalionExecutor {
///     paladin_port: Arc<dyn PaladinPort>,
///     status_map: Arc<RwLock<HashMap<Uuid, BattalionStatus>>>,
/// }
///
/// #[async_trait]
/// impl BattalionPort for SimpleBattalionExecutor {
///     async fn execute(&self, battalion_id: Uuid) -> Result<BattalionResult, BattalionError> {
///         // 1. Update status to Running
///         {
///             let mut status = self.status_map.write().await;
///             status.insert(battalion_id, BattalionStatus::Running);
///         }
///
///         // 2. Execute Paladins (simplified Formation pattern)
///         let start_time = std::time::Instant::now();
///         let mut outputs = Vec::new();
///
///         // Execute each Paladin sequentially
///         // In real implementation, load Paladins from Battalion configuration
///         // for paladin in paladins {
///         //     let result = self.paladin_port.execute(&paladin, input).await?;
///         //     outputs.push(result);
///         //     input = result.output; // Pass to next
///         // }
///
///         // 3. Update status to Completed
///         {
///             let mut status = self.status_map.write().await;
///             status.insert(battalion_id, BattalionStatus::Completed);
///         }
///
///         // 4. Return result
///         Ok(BattalionResult {
///             final_output: "Combined output".into(),
///             paladin_results: outputs,
///             total_execution_time_ms: start_time.elapsed().as_millis() as u64,
///             pattern_used: "Formation".into(),
///         })
///     }
///
///     async fn status(&self, battalion_id: Uuid) -> Result<BattalionStatus, BattalionError> {
///         let status = self.status_map.read().await;
///         status
///             .get(&battalion_id)
///             .cloned()
///             .ok_or_else(|| BattalionError::NotFound(format!("Battalion {} not found", battalion_id)))
///     }
///
///     async fn cancel(&self, battalion_id: Uuid) -> Result<(), BattalionError> {
///         let mut status = self.status_map.write().await;
///         if let Some(current_status) = status.get(&battalion_id) {
///             match current_status {
///                 BattalionStatus::Running => {
///                     // Signal cancellation (in real impl, stop Paladins)
///                     status.insert(battalion_id, BattalionStatus::Cancelled);
///                     Ok(())
///                 }
///                 BattalionStatus::Completed | BattalionStatus::Failed(_) => {
///                     Err(BattalionError::ValidationError(
///                         "Cannot cancel completed Battalion".into()
///                     ))
///                 }
///                 _ => Ok(()),
///             }
///         } else {
///             Err(BattalionError::NotFound(format!("Battalion {} not found", battalion_id)))
///         }
///     }
/// }
/// ```
///
/// # Implementation Notes
///
/// ## Orchestration Patterns
///
/// ### Formation (Sequential)
///
/// ```ignore
/// async fn execute_formation(
///     &self,
///     paladins: Vec<Paladin>,
///     initial_input: &str,
/// ) -> Result<String, BattalionError> {
///     let mut current_input = initial_input.to_string();
///
///     for paladin in paladins {
///         let result = self.paladin_port.execute(&paladin, &current_input).await?;
///         current_input = result.output; // Chain output to next
///     }
///
///     Ok(current_input)
/// }
/// ```
///
/// ### Phalanx (Parallel)
///
/// ```ignore
/// async fn execute_phalanx(
///     &self,
///     paladins: Vec<Paladin>,
///     input: &str,
/// ) -> Result<Vec<String>, BattalionError> {
///     let handles: Vec<_> = paladins
///         .into_iter()
///         .map(|paladin| {
///             let port = self.paladin_port.clone();
///             let input = input.to_string();
///             tokio::spawn(async move {
///                 port.execute(&paladin, &input).await
///             })
///         })
///         .collect();
///
///     let mut results = Vec::new();
///     for handle in handles {
///         let result = handle.await??;
///         results.push(result.output);
///     }
///
///     Ok(results)
/// }
/// ```
///
/// ### Campaign (Graph/DAG)
///
/// ```ignore
/// async fn execute_campaign(
///     &self,
///     graph: &BattalionGraph,
///     initial_input: &str,
/// ) -> Result<String, BattalionError> {
///     let mut completed = HashMap::new();
///     let mut queue = vec![graph.start_node()];
///
///     while let Some(node) = queue.pop() {
///         // Check if dependencies completed
///         if !node.dependencies().iter().all(|d| completed.contains_key(d)) {
///             continue;
///         }
///
///         // Execute Paladin for this node
///         let input = build_input_from_dependencies(&completed, node.dependencies());
///         let result = self.paladin_port.execute(&node.paladin, &input).await?;
///         completed.insert(node.id, result.output);
///
///         // Add next nodes to queue
///         queue.extend(graph.next_nodes(node.id));
///     }
///
///     Ok(completed.get(&graph.end_node()).unwrap().clone())
/// }
/// ```
///
/// ## Cancellation Implementation
///
/// ```ignore
/// async fn cancel(&self, battalion_id: Uuid) -> Result<(), BattalionError> {
///     // 1. Set cancellation flag
///     self.cancellation_tokens.write().await.insert(battalion_id, true);
///
///     // 2. Stop running Paladins
///     if let Some(running_paladins) = self.active_paladins.read().await.get(&battalion_id) {
///         for paladin_id in running_paladins {
///             // Signal Paladin to stop (implementation-specific)
///             self.stop_paladin(*paladin_id).await?;
///         }
///     }
///
///     // 3. Update status
///     self.status_map.write().await.insert(battalion_id, BattalionStatus::Cancelled);
///
///     Ok(())
/// }
/// ```
///
/// # Performance Tips
///
/// 1. **Parallel Execution**: Use tokio::spawn for Phalanx and independent Campaign nodes
/// 2. **Resource Limits**: Limit concurrent Paladins with semaphore (e.g., `Semaphore::new(10)`)
/// 3. **Status Caching**: Cache status for short period to reduce lock contention
/// 4. **Batch Operations**: Group multiple status checks into single query
/// 5. **Early Cancellation Check**: Check cancellation flag between Paladin executions
/// 6. **Result Streaming**: Stream partial results as they complete (advanced)
#[async_trait]
pub trait BattalionPort: Send + Sync {
    /// Execute a Battalion and return the final result
    ///
    /// This method orchestrates the execution of all Paladins in the Battalion
    /// according to the configured pattern (Formation, Phalanx, Campaign, or
    /// Chain of Command).
    ///
    /// # Arguments
    ///
    /// * `battalion_id` - Unique identifier for this Battalion execution
    ///
    /// # Returns
    ///
    /// Returns a `BattalionResult` containing:
    /// - `final_output`: The aggregated final output from all Paladins
    /// - `paladin_results`: Individual results from each Paladin
    /// - `total_execution_time_ms`: Total execution time in milliseconds
    /// - `pattern_used`: The orchestration pattern that was executed
    ///
    /// # Errors
    ///
    /// - `BattalionError::ValidationError` - Invalid Battalion configuration
    /// - `BattalionError::PaladinError` - One or more Paladins failed
    /// - `BattalionError::ExecutionError` - Orchestration logic error
    /// - `BattalionError::NotFound` - Battalion ID not registered
    /// - `BattalionError::AlreadyRunning` - Battalion is already executing
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use paladin::application::ports::output::battalion_port::BattalionPort;
    /// use uuid::Uuid;
    /// use std::sync::Arc;
    ///
    /// async fn run_battalion(
    ///     port: Arc<dyn BattalionPort>,
    ///     battalion_id: Uuid,
    /// ) {
    ///     match port.execute(battalion_id).await {
    ///         Ok(result) => {
    ///             println!("Battalion completed!");
    ///             println!("Final output: {}", result.final_output);
    ///             println!("Total time: {}ms", result.total_execution_time_ms);
    ///             println!("Pattern: {}", result.pattern_used);
    ///             println!("Paladin count: {}", result.paladin_results.len());
    ///         }
    ///         Err(e) => {
    ///             eprintln!("Battalion failed: {}", e);
    ///         }
    ///     }
    /// }
    /// ```
    async fn execute(&self, battalion_id: Uuid) -> Result<BattalionResult, BattalionError>;

    /// Get the current status of a Battalion execution
    ///
    /// This method returns the current execution status without blocking.
    /// Useful for monitoring long-running Battalion operations from a separate task.
    ///
    /// # Arguments
    ///
    /// * `battalion_id` - Unique identifier for the Battalion execution
    ///
    /// # Returns
    ///
    /// Returns the current `BattalionStatus`:
    /// - `Pending`: Battalion registered but not started
    /// - `Running`: Currently executing Paladins
    /// - `Completed`: All Paladins executed successfully
    /// - `Failed(String)`: Execution failed with error message
    /// - `Cancelled`: Execution was cancelled
    ///
    /// # Errors
    ///
    /// - `BattalionError::NotFound` - Battalion ID not found
    /// - `BattalionError::ExecutionError` - Error retrieving status
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use paladin::application::ports::output::battalion_port::{
    ///     BattalionPort, BattalionStatus
    /// };
    /// use uuid::Uuid;
    /// use std::sync::Arc;
    /// use tokio::time::{sleep, Duration};
    ///
    /// async fn monitor_battalion(
    ///     port: Arc<dyn BattalionPort>,
    ///     battalion_id: Uuid,
    /// ) {
    ///     loop {
    ///         match port.status(battalion_id).await {
    ///             Ok(BattalionStatus::Running) => {
    ///                 println!("Still executing...");
    ///                 sleep(Duration::from_secs(2)).await;
    ///             }
    ///             Ok(BattalionStatus::Completed) => {
    ///                 println!("Execution complete!");
    ///                 break;
    ///             }
    ///             Ok(BattalionStatus::Failed(err)) => {
    ///                 eprintln!("Execution failed: {}", err);
    ///                 break;
    ///             }
    ///             Ok(status) => {
    ///                 println!("Status: {:?}", status);
    ///                 break;
    ///             }
    ///             Err(e) => {
    ///                 eprintln!("Error checking status: {}", e);
    ///                 break;
    ///             }
    ///         }
    ///     }
    /// }
    /// ```
    async fn status(&self, battalion_id: Uuid) -> Result<BattalionStatus, BattalionError>;

    /// Cancel a running Battalion execution
    ///
    /// This method attempts to gracefully cancel a Battalion that is currently
    /// executing. Running Paladins will be signaled to stop, and the Battalion
    /// status will be set to `Cancelled`.
    ///
    /// # Arguments
    ///
    /// * `battalion_id` - Unique identifier for the Battalion execution
    ///
    /// # Returns
    ///
    /// * `Ok(())` - Cancellation was successful or Battalion already stopped
    /// * `Err(BattalionError)` - Error during cancellation
    ///
    /// # Errors
    ///
    /// - `BattalionError::NotFound` - Battalion ID not found
    /// - `BattalionError::ValidationError` - Battalion already completed (cannot cancel)
    ///
    /// # Behavior
    ///
    /// - If Battalion is `Running`: Signal cancellation and update status to `Cancelled`
    /// - If Battalion is `Pending`: Cancel before execution starts
    /// - If Battalion is `Completed` or `Failed`: Return error (already finished)
    /// - If Battalion is `Cancelled`: Return Ok (idempotent)
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use paladin::application::ports::output::battalion_port::BattalionPort;
    /// use uuid::Uuid;
    /// use std::sync::Arc;
    /// use tokio::time::{timeout, Duration};
    ///
    /// async fn execute_with_timeout(
    ///     port: Arc<dyn BattalionPort>,
    ///     battalion_id: Uuid,
    /// ) {
    ///     let port_clone = port.clone();
    ///
    ///     match timeout(
    ///         Duration::from_secs(60),
    ///         port_clone.execute(battalion_id),
    ///     )
    ///     .await
    ///     {
    ///         Ok(Ok(result)) => {
    ///             println!("Completed: {}", result.final_output);
    ///         }
    ///         Ok(Err(e)) => {
    ///             eprintln!("Failed: {}", e);
    ///         }
    ///         Err(_) => {
    ///             println!("Timeout! Cancelling...");
    ///             if let Err(e) = port.cancel(battalion_id).await {
    ///                 eprintln!("Cancel failed: {}", e);
    ///             } else {
    ///                 println!("Successfully cancelled");
    ///             }
    ///         }
    ///     }
    /// }
    /// ```
    async fn cancel(&self, battalion_id: Uuid) -> Result<(), BattalionError>;
}

#[cfg(test)]
mod tests {
    use super::*;
    use paladin_core::platform::container::battalion::BattalionStatus;

    // Mock implementation for testing
    struct MockBattalionPort {
        should_fail: bool,
    }

    #[async_trait]
    impl BattalionPort for MockBattalionPort {
        async fn execute(&self, _battalion_id: Uuid) -> Result<BattalionResult, BattalionError> {
            if self.should_fail {
                Err(BattalionError::PaladinError("Mock failure".to_string()))
            } else {
                // This will fail until BattalionResult::new is available
                unimplemented!("BattalionResult construction not yet implemented")
            }
        }

        async fn status(&self, _battalion_id: Uuid) -> Result<BattalionStatus, BattalionError> {
            if self.should_fail {
                Err(BattalionError::ValidationError(
                    "Battalion not found".to_string(),
                ))
            } else {
                Ok(BattalionStatus::Running)
            }
        }

        async fn cancel(&self, _battalion_id: Uuid) -> Result<(), BattalionError> {
            if self.should_fail {
                Err(BattalionError::ValidationError(
                    "Battalion not found".to_string(),
                ))
            } else {
                Ok(())
            }
        }
    }

    #[tokio::test]
    async fn test_battalion_port_status_success() {
        let port = MockBattalionPort { should_fail: false };
        let battalion_id = Uuid::new_v4();

        let result = port.status(battalion_id).await;
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), BattalionStatus::Running);
    }

    #[tokio::test]
    async fn test_battalion_port_status_failure() {
        let port = MockBattalionPort { should_fail: true };
        let battalion_id = Uuid::new_v4();

        let result = port.status(battalion_id).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            BattalionError::ValidationError(msg) => {
                assert_eq!(msg, "Battalion not found");
            }
            _ => panic!("Expected ValidationError"),
        }
    }

    #[tokio::test]
    async fn test_battalion_port_cancel_success() {
        let port = MockBattalionPort { should_fail: false };
        let battalion_id = Uuid::new_v4();

        let result = port.cancel(battalion_id).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_battalion_port_cancel_failure() {
        let port = MockBattalionPort { should_fail: true };
        let battalion_id = Uuid::new_v4();

        let result = port.cancel(battalion_id).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_battalion_port_execute_failure() {
        let port = MockBattalionPort { should_fail: true };
        let battalion_id = Uuid::new_v4();

        let result = port.execute(battalion_id).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            BattalionError::PaladinError(msg) => {
                assert_eq!(msg, "Mock failure");
            }
            _ => panic!("Expected PaladinError"),
        }
    }

    #[test]
    fn test_battalion_port_is_send_sync() {
        // Verify trait is Send + Sync
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Box<dyn BattalionPort>>();
    }
}