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
//! Chain of Command Execution Service
//!
//! This service implements the Chain of Command Battalion orchestration pattern.
//! A Chain of Command consists of a commander Paladin and specialist Paladins,
//! where the commander coordinates and delegates tasks to specialists based on
//! the configured delegation strategy.
//!
//! # Delegation Strategies
//!
//! - **Automatic**: Commander analyzes input and selects appropriate specialist(s)
//! - **Broadcast**: All specialists execute concurrently
//! - **RoundRobin**: Rotate through specialists on consecutive calls
//! - **Custom**: User-defined delegation logic
//!
//! # Examples
//!
//! ```rust,ignore
//! use paladin_battalion::chain_of_command_service::ChainOfCommandExecutionService;
//! use paladin_core::platform::container::battalion::{ChainOfCommand, DelegationStrategy};
//! use std::sync::Arc;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let paladin_port = unimplemented!();
//! # let commander = unimplemented!();
//! # let specialists = vec![];
//! # let config = paladin::core::platform::container::battalion::BattalionConfig::default();
//! let service = ChainOfCommandExecutionService::new(paladin_port);
//! let chain = ChainOfCommand::new(commander, specialists, config)?
//!     .with_strategy(DelegationStrategy::Automatic);
//!
//! let result = service.execute(&chain, "Analyze this data").await?;
//! println!("Selected specialists: {:?}", result.selected_specialists);
//! # Ok(())
//! # }
//! ```

use paladin_core::platform::container::battalion::BattalionError;
use paladin_core::platform::container::battalion::chain_of_command::{
    ChainOfCommand, DelegationStrategy,
};
use paladin_core::platform::container::paladin_error::PaladinError;
use paladin_ports::output::paladin_port::{PaladinPort, PaladinResult};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

/// Result of executing a Chain of Command delegation
#[derive(Debug, Clone)]
pub struct DelegationResult {
    /// Names of specialists that were selected for execution
    pub selected_specialists: Vec<String>,
    /// Commander's reasoning for specialist selection (if Automatic strategy)
    pub reasoning: String,
    /// Outputs from the executed specialists
    pub outputs: Vec<String>,
}

/// Service for executing Chain of Command Battalion patterns
///
/// This service coordinates the execution of a commander Paladin and specialist
/// Paladins according to the configured delegation strategy.
pub struct ChainOfCommandExecutionService {
    paladin_port: Arc<dyn PaladinPort>,
    /// Round-robin state: maps chain ID to current index
    round_robin_state: Arc<Mutex<HashMap<String, usize>>>,
}

impl ChainOfCommandExecutionService {
    /// Create a new Chain of Command execution service
    ///
    /// # Arguments
    ///
    /// * `paladin_port` - Port for executing individual Paladins
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// use paladin_battalion::chain_of_command_service::ChainOfCommandExecutionService;
    /// use std::sync::Arc;
    ///
    /// # let paladin_port = unimplemented!();
    /// let service = ChainOfCommandExecutionService::new(paladin_port);
    /// ```
    pub fn new(paladin_port: Arc<dyn PaladinPort>) -> Self {
        Self {
            paladin_port,
            round_robin_state: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Validate a Chain of Command configuration
    ///
    /// # Arguments
    ///
    /// * `chain` - The Chain of Command to validate
    ///
    /// # Errors
    ///
    /// Returns error if the chain configuration is invalid
    pub async fn validate(&self, chain: &ChainOfCommand) -> Result<(), BattalionError> {
        chain.validate()?;
        Ok(())
    }

    /// Execute a Chain of Command with the given input
    ///
    /// The execution flow depends on the delegation strategy:
    ///
    /// 1. **Automatic**: Commander analyzes input and selects specialist(s)
    /// 2. **Broadcast**: All specialists execute concurrently
    /// 3. **RoundRobin**: Next specialist in rotation executes
    /// 4. **Custom**: User-defined logic determines execution
    ///
    /// # Arguments
    ///
    /// * `chain` - The Chain of Command to execute
    /// * `input` - Input to process
    ///
    /// # Returns
    ///
    /// Returns a `DelegationResult` containing selected specialists, reasoning, and outputs
    ///
    /// # Errors
    ///
    /// Returns error if execution fails or validation fails
    pub async fn execute(
        &self,
        chain: &ChainOfCommand,
        input: &str,
    ) -> Result<DelegationResult, BattalionError> {
        // Validate first
        self.validate(chain).await?;

        // Execute based on delegation strategy
        match chain.delegation_strategy() {
            DelegationStrategy::Automatic => self.execute_automatic(chain, input).await,
            DelegationStrategy::Broadcast => self.execute_broadcast(chain, input).await,
            DelegationStrategy::RoundRobin => self.execute_round_robin(chain, input).await,
            DelegationStrategy::Custom(logic) => self.execute_custom(chain, input, logic).await,
        }
    }

    /// Execute with Automatic delegation strategy
    ///
    /// Commander analyzes the input and selects appropriate specialist(s)
    ///
    /// The commander is provided with:
    /// 1. The original input
    /// 2. Descriptions of all available specialists
    /// 3. Instructions to select specialists and provide reasoning
    ///
    /// The commander's response should follow this format:
    /// ```text
    /// SELECT: specialist_name1, specialist_name2
    /// REASON: Explanation of why these specialists were chosen
    /// ```
    async fn execute_automatic(
        &self,
        chain: &ChainOfCommand,
        input: &str,
    ) -> Result<DelegationResult, BattalionError> {
        // Build specialist descriptions for commander context
        let specialist_descriptions: Vec<String> = chain
            .specialists()
            .iter()
            .map(|p| {
                format!(
                    "- {}: {}",
                    p.node.name,
                    p.node.system_prompt.lines().next().unwrap_or("")
                )
            })
            .collect();

        // Build commander prompt with specialist context
        let commander_prompt = format!(
            r#"You are a commander coordinating a team of specialists. Your task is to analyze the following request and select the appropriate specialist(s) to handle it.

Available Specialists:
{}

User Request:
{}

Instructions:
1. Analyze the request carefully
2. Select one or more specialists best suited for this task
3. Respond EXACTLY in this format:

SELECT: specialist_name1, specialist_name2
REASON: Brief explanation of your selection

Important: Use the exact specialist names shown above. Separate multiple specialists with commas."#,
            specialist_descriptions.join("\n"),
            input
        );

        // Execute commander to get specialist selection
        let commander_result = self
            .paladin_port
            .execute(chain.commander(), &commander_prompt)
            .await?;

        // Parse commander's response to extract specialist selection and reasoning
        let (selected_names, reasoning) =
            self.parse_commander_response(&commander_result.output)?;

        // Validate that selected specialists exist
        let selected_specialists: Vec<&paladin_core::platform::container::paladin::Paladin> =
            selected_names
                .iter()
                .map(|name| {
                    chain
                        .specialists()
                        .iter()
                        .find(|s| s.node.name == *name)
                        .ok_or_else(|| {
                            BattalionError::ExecutionError(format!(
                                "Commander selected non-existent specialist: {}",
                                name
                            ))
                        })
                })
                .collect::<Result<Vec<_>, _>>()?;

        // Execute selected specialists with the original input
        let mut outputs = Vec::new();
        for specialist in &selected_specialists {
            let result = self.paladin_port.execute(specialist, input).await?;
            outputs.push(result.output);
        }

        Ok(DelegationResult {
            selected_specialists: selected_names,
            reasoning,
            outputs,
        })
    }

    /// Parse commander's response to extract specialist selection and reasoning
    ///
    /// Expected format:
    /// ```text
    /// SELECT: specialist1, specialist2
    /// REASON: explanation text
    /// ```
    fn parse_commander_response(
        &self,
        response: &str,
    ) -> Result<(Vec<String>, String), BattalionError> {
        let mut selected = Vec::new();
        let mut reasoning = String::new();

        for line in response.lines() {
            let line = line.trim();
            if line.starts_with("SELECT:") {
                let selection = line.strip_prefix("SELECT:").unwrap().trim();
                selected = selection
                    .split(',')
                    .map(|s| s.trim().to_string())
                    .filter(|s| !s.is_empty())
                    .collect();
            } else if line.starts_with("REASON:") {
                reasoning = line.strip_prefix("REASON:").unwrap().trim().to_string();
            }
        }

        if selected.is_empty() {
            return Err(BattalionError::ExecutionError(
                "Commander did not select any specialists".to_string(),
            ));
        }

        if reasoning.is_empty() {
            reasoning = "No reasoning provided".to_string();
        }

        Ok((selected, reasoning))
    }

    /// Execute with Broadcast delegation strategy
    ///
    /// All specialists execute concurrently with the same input
    ///
    /// # Behavior
    ///
    /// - All specialists receive the same input simultaneously
    /// - Execution happens concurrently using tokio::spawn
    /// - All results are collected regardless of individual failures (per error strategy)
    /// - No commander analysis is performed
    async fn execute_broadcast(
        &self,
        chain: &ChainOfCommand,
        input: &str,
    ) -> Result<DelegationResult, BattalionError> {
        use tokio::task::JoinSet;

        let mut join_set = JoinSet::new();

        // Spawn concurrent execution for all specialists
        for specialist in chain.specialists() {
            let specialist_clone: paladin_core::platform::container::paladin::Paladin =
                specialist.clone();
            let input_clone = input.to_string();
            let port_clone = Arc::clone(&self.paladin_port);

            join_set.spawn(async move {
                let result: Result<PaladinResult, PaladinError> =
                    port_clone.execute(&specialist_clone, &input_clone).await;
                (specialist_clone.node.name.clone(), result)
            });
        }

        // Collect all results
        let mut outputs = Vec::new();
        let mut selected_specialists = Vec::new();

        while let Some(result) = join_set.join_next().await {
            match result {
                Ok((name, Ok(paladin_result))) => {
                    selected_specialists.push(name);
                    outputs.push(paladin_result.output);
                }
                Ok((name, Err(e))) => {
                    // Handle specialist failure based on error strategy
                    // For now, propagate the error
                    return Err(BattalionError::PaladinError(format!(
                        "Specialist {} failed: {}",
                        name, e
                    )));
                }
                Err(join_error) => {
                    return Err(BattalionError::ExecutionError(format!(
                        "Task join error: {}",
                        join_error
                    )));
                }
            }
        }

        Ok(DelegationResult {
            selected_specialists,
            reasoning: "Broadcast to all specialists concurrently".to_string(),
            outputs,
        })
    }

    /// Execute with RoundRobin delegation strategy
    ///
    /// Rotate through specialists on consecutive calls
    ///
    /// # Behavior
    ///
    /// - Maintains state across calls using a unique chain identifier
    /// - Cycles through specialists in order: 0 -> 1 -> 2 -> ... -> N -> 0
    /// - State is thread-safe using Mutex
    /// - Only one specialist executes per call
    async fn execute_round_robin(
        &self,
        chain: &ChainOfCommand,
        input: &str,
    ) -> Result<DelegationResult, BattalionError> {
        let specialists = chain.specialists();
        if specialists.is_empty() {
            return Err(BattalionError::ValidationError(
                "No specialists available for round-robin delegation".to_string(),
            ));
        }

        // Generate a unique ID for this chain based on commander + specialists
        let chain_id = format!(
            "{}:{}",
            chain.commander().node.name,
            specialists
                .iter()
                .map(|s| s.node.name.as_str())
                .collect::<Vec<_>>()
                .join(",")
        );

        // Get current index and increment for next call
        let current_index = {
            let mut state = self.round_robin_state.lock().unwrap();
            let index = state.entry(chain_id.clone()).or_insert(0);
            let current = *index;
            *index = (current + 1) % specialists.len();
            current
        };

        // Execute the selected specialist
        let selected_specialist = &specialists[current_index];
        let result = self
            .paladin_port
            .execute(selected_specialist, input)
            .await?;

        Ok(DelegationResult {
            selected_specialists: vec![selected_specialist.node.name.clone()],
            reasoning: format!(
                "Round-robin delegation selected specialist {} of {}",
                current_index + 1,
                specialists.len()
            ),
            outputs: vec![result.output],
        })
    }

    /// Execute with Custom delegation strategy
    ///
    /// User-defined logic determines specialist selection
    ///
    /// # Behavior
    ///
    /// - Custom logic string describes the delegation approach
    /// - For now, defaults to selecting the first specialist
    /// - Future enhancements could support more sophisticated custom logic parsing
    ///
    /// # Arguments
    ///
    /// * `chain` - The Chain of Command to execute
    /// * `input` - Input to process
    /// * `logic` - User-defined logic description
    async fn execute_custom(
        &self,
        chain: &ChainOfCommand,
        input: &str,
        logic: &str,
    ) -> Result<DelegationResult, BattalionError> {
        let specialists = chain.specialists();
        if specialists.is_empty() {
            return Err(BattalionError::ValidationError(
                "No specialists available for custom delegation".to_string(),
            ));
        }

        // For now, custom delegation defaults to selecting the first specialist
        // Future enhancement: parse logic string for more sophisticated selection
        let selected_specialist = &specialists[0];

        let result = self
            .paladin_port
            .execute(selected_specialist, input)
            .await?;

        Ok(DelegationResult {
            selected_specialists: vec![selected_specialist.node.name.clone()],
            reasoning: format!("Custom delegation using custom logic: {}", logic),
            outputs: vec![result.output],
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use paladin_core::platform::container::battalion::BattalionConfig;
    use paladin_core::platform::container::paladin::{Paladin, PaladinData};
    use paladin_ports::output::paladin_port::PaladinResult;

    fn create_test_paladin(name: &str) -> Paladin {
        let data = PaladinData {
            system_prompt: format!("{} system prompt", name),
            name: name.to_string(),
            user_name: "test_user".to_string(),
            ..Default::default()
        };
        Paladin::new(data, Some(name.to_string()))
    }

    #[test]
    fn test_service_construction() {
        use async_trait::async_trait;
        use paladin_ports::output::paladin_port::StopReason;

        struct MockPort;

        #[async_trait]
        impl PaladinPort for MockPort {
            async fn execute(
                &self,
                _paladin: &Paladin,
                _input: &str,
            ) -> Result<PaladinResult, paladin_core::platform::container::paladin_error::PaladinError>
            {
                Ok(PaladinResult {
                    output: String::new(),
                    token_count: 0,
                    execution_time_ms: 0,
                    loop_count: 1,
                    stop_reason: StopReason::Completed,
                    ..Default::default()
                })
            }

            async fn execute_stream(
                &self,
                _paladin: &Paladin,
                _input: &str,
            ) -> Result<
                paladin_ports::output::paladin_port::PaladinStream,
                paladin_core::platform::container::paladin_error::PaladinError,
            > {
                unimplemented!()
            }

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

        let port = Arc::new(MockPort);
        let _service = ChainOfCommandExecutionService::new(port);
        // Service should be created successfully
    }

    #[tokio::test]
    async fn test_validate_valid_chain() {
        use async_trait::async_trait;
        use paladin_ports::output::paladin_port::StopReason;

        struct MockPort;

        #[async_trait]
        impl PaladinPort for MockPort {
            async fn execute(
                &self,
                _paladin: &Paladin,
                _input: &str,
            ) -> Result<PaladinResult, paladin_core::platform::container::paladin_error::PaladinError>
            {
                Ok(PaladinResult {
                    output: String::new(),
                    token_count: 0,
                    execution_time_ms: 0,
                    loop_count: 1,
                    stop_reason: StopReason::Completed,
                    ..Default::default()
                })
            }

            async fn execute_stream(
                &self,
                _paladin: &Paladin,
                _input: &str,
            ) -> Result<
                paladin_ports::output::paladin_port::PaladinStream,
                paladin_core::platform::container::paladin_error::PaladinError,
            > {
                unimplemented!()
            }

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

        let port = Arc::new(MockPort);
        let service = ChainOfCommandExecutionService::new(port);

        let commander = create_test_paladin("commander");
        let specialist = create_test_paladin("specialist");
        let config = BattalionConfig::default();

        let chain = ChainOfCommand::new(commander, vec![specialist], config)
            .expect("Should create valid chain");

        let result = service.validate(&chain).await;
        assert!(result.is_ok());
    }
}