paladin-ai-core 0.5.1

Pure domain types for the Paladin framework — zero infrastructure dependencies
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
//! Autonomous Feature Configuration
//!
//! This module defines configuration structures for all autonomous agent features.
//! These configurations control planning mode, prompt generation, dynamic temperature,
//! and agent handoffs.

use serde::{Deserialize, Serialize};

use super::handoff::HandoffStrategy;

/// Retry configuration for handoff execution
///
/// Controls how handoffs are retried when transient errors occur.
/// Uses exponential backoff: `delay = initial_backoff_ms * (multiplier ^ attempt)`
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct HandoffRetryConfig {
    /// Maximum number of retry attempts
    pub max_retries: u32,

    /// Initial backoff delay in milliseconds
    pub initial_backoff_ms: u64,

    /// Backoff multiplier for exponential backoff
    pub backoff_multiplier: f64,
}

impl Default for HandoffRetryConfig {
    fn default() -> Self {
        Self {
            max_retries: 3,
            initial_backoff_ms: 1000,
            backoff_multiplier: 2.0,
        }
    }
}

impl HandoffRetryConfig {
    /// Creates a new retry configuration
    pub fn new(max_retries: u32, initial_backoff_ms: u64, backoff_multiplier: f64) -> Self {
        Self {
            max_retries,
            initial_backoff_ms,
            backoff_multiplier,
        }
    }

    /// Calculates the backoff delay for a given attempt
    ///
    /// Formula: `initial_backoff_ms * (backoff_multiplier ^ attempt)`
    pub fn calculate_backoff(&self, attempt: u32) -> u64 {
        let multiplier = self.backoff_multiplier.powi(attempt as i32);
        (self.initial_backoff_ms as f64 * multiplier) as u64
    }
}

/// Complete configuration for all autonomous features
///
/// All features are opt-in (disabled by default) to maintain backward compatibility.
///
/// # Example
///
/// ```
/// use paladin_core::platform::container::autonomous_config::AutonomousConfig;
///
/// let config = AutonomousConfig::default();
/// assert!(!config.planning.enabled);
/// assert!(!config.prompt_generation.enabled);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AutonomousConfig {
    /// Planning mode configuration
    pub planning: PlanningConfig,

    /// Prompt generation configuration
    pub prompt_generation: PromptConfig,

    /// Dynamic temperature configuration
    pub dynamic_temperature: TemperatureConfig,

    /// Agent handoff configuration
    pub handoffs: HandoffConfig,
}

impl AutonomousConfig {
    /// Creates a new configuration with all features disabled
    pub fn new() -> Self {
        Self::default()
    }

    /// Validates the configuration
    ///
    /// Checks for:
    /// - Valid temperature bounds
    /// - Positive max_subtasks
    /// - Valid handoff depth
    pub fn validate(&self) -> Result<(), String> {
        if self.planning.enabled {
            if self.planning.max_subtasks == 0 {
                return Err("max_subtasks must be greater than 0".to_string());
            }
            if self.planning.max_subtasks > 100 {
                return Err("max_subtasks should not exceed 100 (performance concern)".to_string());
            }
        }

        if self.dynamic_temperature.enabled {
            if self.dynamic_temperature.min < 0.0 || self.dynamic_temperature.min > 1.0 {
                return Err("temperature min must be between 0.0 and 1.0".to_string());
            }
            if self.dynamic_temperature.max < 0.0 || self.dynamic_temperature.max > 1.0 {
                return Err("temperature max must be between 0.0 and 1.0".to_string());
            }
            if self.dynamic_temperature.min >= self.dynamic_temperature.max {
                return Err("temperature min must be less than max".to_string());
            }
        }

        if self.handoffs.enabled {
            if self.handoffs.max_depth == 0 {
                return Err("handoff max_depth must be greater than 0".to_string());
            }
            if self.handoffs.max_depth > 20 {
                return Err(
                    "handoff max_depth should not exceed 20 (performance concern)".to_string(),
                );
            }
        }

        Ok(())
    }
}

/// Configuration for autonomous planning mode
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PlanningConfig {
    /// Whether autonomous planning is enabled
    pub enabled: bool,

    /// Maximum number of subtasks the planner can create
    pub max_subtasks: u32,
}

impl Default for PlanningConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            max_subtasks: 10,
        }
    }
}

impl PlanningConfig {
    /// Creates a new planning configuration with custom max_subtasks
    pub fn new(max_subtasks: u32) -> Self {
        Self {
            enabled: true,
            max_subtasks,
        }
    }

    /// Enables planning with default settings
    pub fn enabled() -> Self {
        Self {
            enabled: true,
            ..Default::default()
        }
    }
}

/// Configuration for automatic prompt generation
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct PromptConfig {
    /// Whether automatic prompt generation is enabled
    #[serde(default)]
    pub enabled: bool,

    /// Description of the agent's role and capabilities (required if enabled)
    #[serde(default)]
    pub description: Option<String>,
}

impl PromptConfig {
    /// Creates a new prompt configuration with description
    pub fn new(description: String) -> Self {
        Self {
            enabled: true,
            description: Some(description),
        }
    }

    /// Enables prompt generation (description must be set separately)
    pub fn enabled() -> Self {
        Self {
            enabled: true,
            description: None,
        }
    }

    /// Sets the agent description
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }
}

/// Configuration for dynamic temperature adjustment
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TemperatureConfig {
    /// Whether dynamic temperature is enabled
    pub enabled: bool,

    /// Minimum temperature value (0.0-1.0)
    pub min: f32,

    /// Maximum temperature value (0.0-1.0)
    pub max: f32,
}

impl Default for TemperatureConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            min: 0.1,
            max: 0.9,
        }
    }
}

impl TemperatureConfig {
    /// Creates a new temperature configuration with custom bounds
    ///
    /// # Arguments
    ///
    /// * `min` - Minimum temperature (0.0-1.0)
    /// * `max` - Maximum temperature (0.0-1.0)
    pub fn new(min: f32, max: f32) -> Self {
        Self {
            enabled: true,
            min,
            max,
        }
    }

    /// Enables dynamic temperature with default bounds
    pub fn enabled() -> Self {
        Self {
            enabled: true,
            ..Default::default()
        }
    }

    /// Sets custom temperature bounds
    pub fn with_bounds(mut self, min: f32, max: f32) -> Self {
        self.min = min;
        self.max = max;
        self
    }
}

/// Configuration for agent handoffs
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct HandoffConfig {
    /// Whether agent handoffs are enabled
    pub enabled: bool,

    /// Strategy for handoff decisions
    pub strategy: HandoffStrategy,

    /// Maximum depth of handoff chain
    pub max_depth: u32,

    /// Retry configuration for handoff execution
    pub retry: HandoffRetryConfig,
}

impl Default for HandoffConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            strategy: HandoffStrategy::default(),
            max_depth: 5,
            retry: HandoffRetryConfig::default(),
        }
    }
}

impl HandoffConfig {
    /// Creates a new handoff configuration with custom settings
    ///
    /// # Arguments
    ///
    /// * `strategy` - Handoff strategy to use
    /// * `max_depth` - Maximum handoff chain depth
    pub fn new(strategy: HandoffStrategy, max_depth: u32) -> Self {
        Self {
            enabled: true,
            strategy,
            max_depth,
            retry: HandoffRetryConfig::default(),
        }
    }

    /// Enables handoffs with default settings
    pub fn enabled() -> Self {
        Self {
            enabled: true,
            ..Default::default()
        }
    }

    /// Sets the handoff strategy
    pub fn with_strategy(mut self, strategy: HandoffStrategy) -> Self {
        self.strategy = strategy;
        self
    }

    /// Sets the maximum handoff depth
    pub fn with_max_depth(mut self, max_depth: u32) -> Self {
        self.max_depth = max_depth;
        self
    }

    /// Sets the retry configuration
    pub fn with_retry(mut self, retry: HandoffRetryConfig) -> Self {
        self.retry = retry;
        self
    }
}

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

    #[test]
    fn test_autonomous_config_default() {
        let config = AutonomousConfig::default();
        assert!(!config.planning.enabled);
        assert!(!config.prompt_generation.enabled);
        assert!(!config.dynamic_temperature.enabled);
        assert!(!config.handoffs.enabled);
    }

    #[test]
    fn test_autonomous_config_validate_valid() {
        let config = AutonomousConfig::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_autonomous_config_validate_planning_zero_subtasks() {
        let mut config = AutonomousConfig::default();
        config.planning.enabled = true;
        config.planning.max_subtasks = 0;

        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("greater than 0"));
    }

    #[test]
    fn test_autonomous_config_validate_planning_too_many_subtasks() {
        let mut config = AutonomousConfig::default();
        config.planning.enabled = true;
        config.planning.max_subtasks = 150;

        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("should not exceed 100"));
    }

    #[test]
    fn test_autonomous_config_validate_temperature_bounds() {
        let mut config = AutonomousConfig::default();
        config.dynamic_temperature.enabled = true;
        config.dynamic_temperature.min = 0.8;
        config.dynamic_temperature.max = 0.5;

        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("min must be less than max"));
    }

    #[test]
    fn test_autonomous_config_validate_temperature_invalid_range() {
        let mut config = AutonomousConfig::default();
        config.dynamic_temperature.enabled = true;
        config.dynamic_temperature.min = -0.1;

        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("between 0.0 and 1.0"));
    }

    #[test]
    fn test_autonomous_config_validate_handoff_zero_depth() {
        let mut config = AutonomousConfig::default();
        config.handoffs.enabled = true;
        config.handoffs.max_depth = 0;

        let result = config.validate();
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("greater than 0"));
    }

    #[test]
    fn test_planning_config_default() {
        let config = PlanningConfig::default();
        assert!(!config.enabled);
        assert_eq!(config.max_subtasks, 10);
    }

    #[test]
    fn test_planning_config_new() {
        let config = PlanningConfig::new(15);
        assert!(config.enabled);
        assert_eq!(config.max_subtasks, 15);
    }

    #[test]
    fn test_planning_config_enabled() {
        let config = PlanningConfig::enabled();
        assert!(config.enabled);
        assert_eq!(config.max_subtasks, 10);
    }

    #[test]
    fn test_prompt_config_default() {
        let config = PromptConfig::default();
        assert!(!config.enabled);
        assert_eq!(config.description, None);
    }

    #[test]
    fn test_prompt_config_new() {
        let config = PromptConfig::new("Data analyst".to_string());
        assert!(config.enabled);
        assert_eq!(config.description, Some("Data analyst".to_string()));
    }

    #[test]
    fn test_prompt_config_with_description() {
        let config = PromptConfig::enabled().with_description("Expert coder".to_string());
        assert!(config.enabled);
        assert_eq!(config.description, Some("Expert coder".to_string()));
    }

    #[test]
    fn test_temperature_config_default() {
        let config = TemperatureConfig::default();
        assert!(!config.enabled);
        assert_eq!(config.min, 0.1);
        assert_eq!(config.max, 0.9);
    }

    #[test]
    fn test_temperature_config_new() {
        let config = TemperatureConfig::new(0.2, 0.8);
        assert!(config.enabled);
        assert_eq!(config.min, 0.2);
        assert_eq!(config.max, 0.8);
    }

    #[test]
    fn test_temperature_config_with_bounds() {
        let config = TemperatureConfig::enabled().with_bounds(0.3, 0.7);
        assert!(config.enabled);
        assert_eq!(config.min, 0.3);
        assert_eq!(config.max, 0.7);
    }

    #[test]
    fn test_handoff_config_default() {
        let config = HandoffConfig::default();
        assert!(!config.enabled);
        assert_eq!(config.strategy, HandoffStrategy::Automatic);
        assert_eq!(config.max_depth, 5);
    }

    #[test]
    fn test_handoff_config_new() {
        let config = HandoffConfig::new(HandoffStrategy::Explicit, 10);
        assert!(config.enabled);
        assert_eq!(config.strategy, HandoffStrategy::Explicit);
        assert_eq!(config.max_depth, 10);
    }

    #[test]
    fn test_handoff_config_with_strategy() {
        let config = HandoffConfig::enabled()
            .with_strategy(HandoffStrategy::threshold(0.7))
            .with_max_depth(8);
        assert!(config.enabled);
        assert_eq!(config.max_depth, 8);
    }

    #[test]
    fn test_autonomous_config_serialization() {
        let config = AutonomousConfig::default();
        let json = serde_json::to_string(&config).unwrap();
        let deserialized: AutonomousConfig = serde_json::from_str(&json).unwrap();
        assert_eq!(config, deserialized);
    }
}