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
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
/*
Task Container

A Task is a platform-level container that extends the base Action component
to provide specific execution capabilities. Tasks represent individual units
of work that can be executed independently or as part of a larger Job.

Tasks add:
- Service-based execution through TaskService trait
- Task-specific error handling and states
- Integration with the Action lifecycle
- Platform-level orchestration capabilities

Tasks are the building blocks for Jobs and can be scheduled, queued,
and executed through various platform services.
*/

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::path::{Component, Path, PathBuf};
use std::sync::{Arc, Mutex};
use thiserror::Error;
use uuid::Uuid;

// Import the base Action component
use crate::base::component::action::{
    Action, ActionError, ActionPriority, ActionResult, ActionStatus,
};

/// Task-specific execution modes
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TaskExecutionMode {
    /// Execute once and complete
    OneTime,
    /// Can be retried on failure
    Retryable,
    /// Idempotent - safe to run multiple times
    Idempotent,
}

/// Task container that extends the base Action component
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Task {
    /// Base action functionality
    pub action: Action,
    /// Task-specific execution mode
    pub execution_mode: TaskExecutionMode,
    /// Service name that will execute this task
    pub service_name: String,
    /// Task-specific metadata
    pub task_metadata: std::collections::HashMap<String, serde_json::Value>,
}

impl Task {
    /// Creates a new Task with the specified service
    pub fn new(name: String, description: String, service_name: String) -> Self {
        let action = Action::new(
            name.clone(),
            description,
            "task_manager".to_string(),
            service_name.clone(),
        );

        Self {
            action,
            execution_mode: TaskExecutionMode::Retryable,
            service_name,
            task_metadata: std::collections::HashMap::new(),
        }
    }

    /// Creates a new Task with full configuration
    pub fn new_with_config(
        name: String,
        description: String,
        service_name: String,
        execution_mode: TaskExecutionMode,
        priority: ActionPriority,
    ) -> Self {
        let action = Action::new(
            name.clone(),
            description,
            "task_manager".to_string(),
            service_name.clone(),
        )
        .with_priority(priority);

        Self {
            action,
            execution_mode,
            service_name,
            task_metadata: std::collections::HashMap::new(),
        }
    }

    /// Sets the execution mode
    pub fn with_execution_mode(mut self, mode: TaskExecutionMode) -> Self {
        self.execution_mode = mode;
        self
    }

    /// Sets task priority
    pub fn with_priority(mut self, priority: ActionPriority) -> Self {
        self.action = self.action.with_priority(priority);
        self
    }

    /// Sets timeout for task execution
    pub fn with_timeout(mut self, timeout_seconds: u32) -> Self {
        self.action = self.action.with_timeout(timeout_seconds);
        self
    }

    /// Adds task-specific metadata
    pub fn add_metadata<T: Serialize>(&mut self, key: String, value: T) -> Result<(), TaskError> {
        let json_value = serde_json::to_value(value)
            .map_err(|e| TaskError::SerializationError(e.to_string()))?;
        self.task_metadata.insert(key, json_value);
        Ok(())
    }

    /// Gets task metadata
    pub fn get_metadata(&self, key: &str) -> Option<&serde_json::Value> {
        self.task_metadata.get(key)
    }

    /// Executes the task using the provided service
    pub async fn execute<T: TaskService + ?Sized>(&mut self, service: &T) -> Result<(), TaskError> {
        // Check if task can be executed
        if !self.action.can_execute() {
            return Err(TaskError::InvalidState(self.action.status.clone()));
        }

        // Start execution
        self.action.start_execution();

        // Execute the task
        let start_time = std::time::Instant::now();
        let execution_result = service.execute(&self.action).await;
        let duration_ms = start_time.elapsed().as_millis() as u64;

        // Handle result
        match execution_result {
            Ok(result_data) => {
                let action_result = ActionResult {
                    success: true,
                    duration_ms,
                    data: result_data,
                    error: None,
                    metadata: std::collections::HashMap::new(),
                };
                self.action.complete_execution(action_result);
                Ok(())
            }
            Err(task_error) => {
                let error_message = task_error.to_string();
                let can_retry = self.action.fail_execution(error_message, duration_ms);

                if can_retry && matches!(self.execution_mode, TaskExecutionMode::Retryable) {
                    Err(TaskError::RetryableFailure(task_error.to_string()))
                } else {
                    Err(task_error)
                }
            }
        }
    }

    /// Executes the task without a service (for testing or simple tasks)
    pub async fn execute_simple(&mut self) -> Result<(), TaskError> {
        if !self.action.can_execute() {
            return Err(TaskError::InvalidState(self.action.status.clone()));
        }

        self.action.start_execution();

        // Simulate simple execution
        let start_time = std::time::Instant::now();
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;
        let duration_ms = start_time.elapsed().as_millis() as u64;

        let action_result = ActionResult {
            success: true,
            duration_ms,
            data: Some(serde_json::json!({"message": "Task completed successfully"})),
            error: None,
            metadata: std::collections::HashMap::new(),
        };

        self.action.complete_execution(action_result);
        Ok(())
    }

    /// Cancels the task
    pub fn cancel(&mut self) {
        self.action.cancel();
    }

    /// Resets the task for re-execution
    pub fn reset(&mut self) {
        self.action.reset();
    }

    /// Checks if the task can be executed
    pub fn can_execute(&self) -> bool {
        self.action.can_execute()
    }

    /// Checks if the task is in a terminal state
    pub fn is_complete(&self) -> bool {
        self.action.is_terminal()
    }

    /// Gets the task's current status
    pub fn status(&self) -> &ActionStatus {
        &self.action.status
    }

    /// Gets the task ID
    pub fn id(&self) -> Uuid {
        self.action.id
    }

    /// Gets the task name
    pub fn name(&self) -> &str {
        &self.action.name
    }

    /// Gets execution statistics
    pub fn execution_stats(&self) -> TaskStats {
        TaskStats {
            execution_count: self.action.execution_count,
            success_rate: self.action.success_rate(),
            average_duration_ms: self.action.average_duration_ms(),
            last_execution: self.action.last_execution,
        }
    }

    /// Creates a new instance for re-execution (useful for recurring tasks)
    pub fn clone_for_new_execution(&self) -> Self {
        let mut cloned = self.clone();
        cloned.action = self.action.clone_for_new_execution();
        cloned
    }
}

impl PartialEq for Task {
    fn eq(&self, other: &Self) -> bool {
        self.action.id == other.action.id
    }
}

impl Eq for Task {}

/// Task execution statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskStats {
    pub execution_count: u32,
    pub success_rate: f64,
    pub average_duration_ms: Option<u64>,
    pub last_execution: Option<chrono::DateTime<chrono::Utc>>,
}

/// Task-specific errors
#[derive(Debug, Error)]
pub enum TaskError {
    #[error("Task execution failed: {0}")]
    ExecutionFailed(String),
    #[error("Service unavailable: {0}")]
    ServiceUnavailable(String),
    #[error("Task execution timed out")]
    Timeout,
    #[error("Task failed but can be retried: {0}")]
    RetryableFailure(String),
    #[error("Serialization error: {0}")]
    SerializationError(String),
    #[error("Task is not in a valid state for execution: {0:?}")]
    InvalidState(ActionStatus),
    #[error("Action error: {0}")]
    ActionError(#[from] ActionError),
}

/// Trait for services that can execute tasks
#[async_trait]
pub trait TaskService: Debug + Send + Sync {
    /// Returns the service name
    fn name(&self) -> &str;

    /// Executes the task and returns optional result data
    async fn execute(&self, action: &Action) -> Result<Option<serde_json::Value>, TaskError>;

    /// Checks if the service can handle the given task
    fn can_handle(&self, task: &Task) -> bool {
        task.service_name == self.name()
    }

    /// Clones the service for re-use
    fn clone_service(&self) -> Box<dyn TaskService>;
}

// ---------------------------------------------------------------------------
// Default task services
// ---------------------------------------------------------------------------

/// Validates a caller-supplied relative artifact name and guards against
/// path-traversal (OWASP A01). Absolute paths and any `..`/root components are
/// rejected so writes can never escape their configured base directory.
fn safe_relative_name(name: &str) -> Result<PathBuf, TaskError> {
    let candidate = Path::new(name);
    if candidate.as_os_str().is_empty() {
        return Err(TaskError::ExecutionFailed(
            "artifact name must not be empty".to_string(),
        ));
    }
    if candidate.is_absolute() {
        return Err(TaskError::ExecutionFailed(
            "artifact name must be relative".to_string(),
        ));
    }
    for component in candidate.components() {
        if !matches!(component, Component::Normal(_)) {
            return Err(TaskError::ExecutionFailed(
                "artifact name must not traverse directories".to_string(),
            ));
        }
    }
    Ok(candidate.to_path_buf())
}

/// Persists a backup artifact into a configured, path-constrained directory.
#[derive(Debug, Clone)]
pub struct DataBackupService {
    /// Base directory that all backups are written beneath.
    pub backup_path: String,
}

impl DataBackupService {
    /// Creates a new backup service rooted at `backup_path`.
    pub fn new(backup_path: impl Into<String>) -> Self {
        Self {
            backup_path: backup_path.into(),
        }
    }
}

#[async_trait]
impl TaskService for DataBackupService {
    fn name(&self) -> &str {
        "DataBackupService"
    }

    async fn execute(&self, action: &Action) -> Result<Option<serde_json::Value>, TaskError> {
        let base = Path::new(&self.backup_path);
        tokio::fs::create_dir_all(base).await.map_err(|e| {
            TaskError::ExecutionFailed(format!("failed to prepare backup dir: {e}"))
        })?;

        // Resolve the target file name, guarding against path traversal.
        let file_name = match action.get_argument("backup_name").and_then(|v| v.as_str()) {
            Some(name) => safe_relative_name(name)?,
            None => PathBuf::from(format!("{}.backup.json", action.id)),
        };
        let target = base.join(&file_name);

        // Use an explicit payload when provided, otherwise persist a manifest
        // derived from the task itself.
        let payload = match action.get_argument("payload") {
            Some(value) => serde_json::to_vec_pretty(value),
            None => serde_json::to_vec_pretty(&serde_json::json!({
                "task_id": action.id,
                "task_name": action.name,
                "description": action.description,
                "backed_up_at": chrono::Utc::now(),
            })),
        }
        .map_err(|e| TaskError::SerializationError(e.to_string()))?;

        let bytes_written = payload.len();
        tokio::fs::write(&target, &payload)
            .await
            .map_err(|e| TaskError::ExecutionFailed(format!("backup write failed: {e}")))?;

        Ok(Some(serde_json::json!({
            "backup_path": target.to_string_lossy(),
            "bytes_written": bytes_written,
            "status": "completed",
            "timestamp": chrono::Utc::now(),
        })))
    }

    fn clone_service(&self) -> Box<dyn TaskService> {
        Box::new(self.clone())
    }
}

/// Builds and persists a simple term-frequency index artifact.
#[derive(Debug, Clone)]
pub struct ContentIndexingService {
    /// Logical name of the index this service maintains.
    pub index_name: String,
}

impl ContentIndexingService {
    /// Creates a new indexing service for the given `index_name`.
    pub fn new(index_name: impl Into<String>) -> Self {
        Self {
            index_name: index_name.into(),
        }
    }

    /// Base directory where index artifacts are persisted.
    fn index_dir() -> PathBuf {
        std::env::temp_dir().join("paladin_index")
    }
}

#[async_trait]
impl TaskService for ContentIndexingService {
    fn name(&self) -> &str {
        "ContentIndexingService"
    }

    async fn execute(&self, action: &Action) -> Result<Option<serde_json::Value>, TaskError> {
        // The index name doubles as part of the artifact file name, so guard it.
        let safe_name = safe_relative_name(&self.index_name)?;
        let dir = Self::index_dir();
        tokio::fs::create_dir_all(&dir)
            .await
            .map_err(|e| TaskError::ExecutionFailed(format!("failed to prepare index dir: {e}")))?;

        // Index either explicit content or the task description as a fallback.
        let content = action
            .get_argument("content")
            .and_then(|v| v.as_str())
            .unwrap_or(action.description.as_str());

        let mut term_counts: BTreeMap<String, usize> = BTreeMap::new();
        for term in content.split_whitespace() {
            *term_counts.entry(term.to_lowercase()).or_insert(0) += 1;
        }

        let artifact = serde_json::json!({
            "index_name": self.index_name,
            "task_id": action.id,
            "terms": term_counts,
            "indexed_at": chrono::Utc::now(),
        });
        let bytes = serde_json::to_vec_pretty(&artifact)
            .map_err(|e| TaskError::SerializationError(e.to_string()))?;
        let target = dir.join(format!(
            "{}-{}.index.json",
            safe_name.to_string_lossy(),
            action.id
        ));
        tokio::fs::write(&target, &bytes)
            .await
            .map_err(|e| TaskError::ExecutionFailed(format!("index write failed: {e}")))?;

        Ok(Some(serde_json::json!({
            "index_name": self.index_name,
            "index_path": target.to_string_lossy(),
            "terms_indexed": term_counts.len(),
            "status": "indexed",
            "timestamp": chrono::Utc::now(),
        })))
    }

    fn clone_service(&self) -> Box<dyn TaskService> {
        Box::new(self.clone())
    }
}

/// A captured outbound email message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EmailMessage {
    /// Recipient address.
    pub to: String,
    /// Message subject line.
    pub subject: String,
    /// Message body.
    pub body: String,
}

/// Injectable transport seam for delivering email notifications. Production
/// deployments supply a real SMTP transport; tests supply observable or
/// failing sinks without touching the network.
#[async_trait]
pub trait EmailSink: Debug + Send + Sync {
    /// Delivers a single message, returning a `TaskError` on failure.
    async fn deliver(&self, message: &EmailMessage) -> Result<(), TaskError>;

    /// Clones the sink behind a shared pointer for reuse.
    fn clone_sink(&self) -> Arc<dyn EmailSink>;
}

/// Default in-process sink that records delivered messages for inspection.
#[derive(Debug, Clone, Default)]
pub struct InMemoryEmailSink {
    delivered: Arc<Mutex<Vec<EmailMessage>>>,
}

impl InMemoryEmailSink {
    /// Creates an empty in-memory sink.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns a snapshot of all messages delivered so far.
    pub fn delivered(&self) -> Vec<EmailMessage> {
        self.delivered
            .lock()
            .map(|guard| guard.clone())
            .unwrap_or_default()
    }
}

#[async_trait]
impl EmailSink for InMemoryEmailSink {
    async fn deliver(&self, message: &EmailMessage) -> Result<(), TaskError> {
        self.delivered
            .lock()
            .map_err(|e| TaskError::ExecutionFailed(format!("sink poisoned: {e}")))?
            .push(message.clone());
        Ok(())
    }

    fn clone_sink(&self) -> Arc<dyn EmailSink> {
        Arc::new(self.clone())
    }
}

/// Dispatches email notifications through an injectable [`EmailSink`].
#[derive(Debug, Clone)]
pub struct EmailNotificationService {
    /// SMTP server identifier recorded with each dispatch.
    pub smtp_server: String,
    /// Transport seam used to actually deliver messages.
    sink: Arc<dyn EmailSink>,
}

impl EmailNotificationService {
    /// Creates a service that records messages in an in-memory sink.
    pub fn new(smtp_server: impl Into<String>) -> Self {
        Self {
            smtp_server: smtp_server.into(),
            sink: Arc::new(InMemoryEmailSink::new()),
        }
    }

    /// Creates a service backed by a caller-supplied transport sink.
    pub fn with_sink(smtp_server: impl Into<String>, sink: Arc<dyn EmailSink>) -> Self {
        Self {
            smtp_server: smtp_server.into(),
            sink,
        }
    }
}

#[async_trait]
impl TaskService for EmailNotificationService {
    fn name(&self) -> &str {
        "EmailNotificationService"
    }

    async fn execute(&self, action: &Action) -> Result<Option<serde_json::Value>, TaskError> {
        let to = action
            .get_argument("to_email")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown@example.com")
            .to_string();
        let subject = action
            .get_argument("subject")
            .and_then(|v| v.as_str())
            .unwrap_or("(no subject)")
            .to_string();
        let body = action
            .get_argument("body")
            .and_then(|v| v.as_str())
            .unwrap_or(action.description.as_str())
            .to_string();

        let message = EmailMessage {
            to: to.clone(),
            subject,
            body,
        };
        self.sink.deliver(&message).await?;

        Ok(Some(serde_json::json!({
            "smtp_server": self.smtp_server,
            "to_email": to,
            "status": "sent",
            "timestamp": chrono::Utc::now(),
        })))
    }

    fn clone_service(&self) -> Box<dyn TaskService> {
        Box::new(self.clone())
    }
}

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

    #[tokio::test]
    async fn test_task_creation() {
        let task = Task::new(
            "Test Task".to_string(),
            "A test task".to_string(),
            "TestService".to_string(),
        );

        assert_eq!(task.name(), "Test Task");
        assert_eq!(task.service_name, "TestService");
        assert_eq!(task.execution_mode, TaskExecutionMode::Retryable);
        assert!(task.can_execute());
    }

    #[tokio::test]
    async fn test_task_execution_with_service() {
        let mut task = Task::new(
            "Backup Task".to_string(),
            "Backup data task".to_string(),
            "DataBackupService".to_string(),
        );

        let temp = std::env::temp_dir().join(format!("paladin_backup_test_{}", Uuid::new_v4()));
        let service = DataBackupService::new(temp.to_string_lossy().to_string());

        let result = task.execute(&service).await;
        assert!(result.is_ok());
        assert_eq!(task.status(), &ActionStatus::Completed);
        assert_eq!(task.action.execution_count, 1);
    }

    #[tokio::test]
    async fn test_task_simple_execution() {
        let mut task = Task::new(
            "Simple Task".to_string(),
            "A simple task".to_string(),
            "SimpleService".to_string(),
        );

        let result = task.execute_simple().await;
        assert!(result.is_ok());
        assert_eq!(task.status(), &ActionStatus::Completed);
    }

    #[tokio::test]
    async fn test_task_with_metadata() {
        let mut task = Task::new(
            "Metadata Task".to_string(),
            "Task with metadata".to_string(),
            "TestService".to_string(),
        );

        task.add_metadata("custom_field".to_string(), "custom_value")
            .unwrap();
        task.add_metadata("priority_level".to_string(), 5).unwrap();

        assert_eq!(
            task.get_metadata("custom_field"),
            Some(&json!("custom_value"))
        );
        assert_eq!(task.get_metadata("priority_level"), Some(&json!(5)));
    }

    #[tokio::test]
    async fn test_task_configuration() {
        let task = Task::new_with_config(
            "Config Task".to_string(),
            "Configured task".to_string(),
            "ConfigService".to_string(),
            TaskExecutionMode::Idempotent,
            ActionPriority::High,
        )
        .with_timeout(30);

        assert_eq!(task.execution_mode, TaskExecutionMode::Idempotent);
        assert_eq!(task.action.priority, ActionPriority::High);
        assert_eq!(task.action.timeout_seconds, Some(30));
    }

    #[tokio::test]
    async fn test_task_statistics() {
        let mut task = Task::new(
            "Stats Task".to_string(),
            "Task for statistics".to_string(),
            "StatsService".to_string(),
        );

        // Execute the task
        task.execute_simple().await.unwrap();

        let stats = task.execution_stats();
        assert_eq!(stats.execution_count, 1);
        assert_eq!(stats.success_rate, 100.0);
        assert!(stats.average_duration_ms.is_some());
        assert!(stats.last_execution.is_some());
    }

    #[tokio::test]
    async fn test_task_clone_for_new_execution() {
        let mut original = Task::new(
            "Original Task".to_string(),
            "Original task".to_string(),
            "TestService".to_string(),
        );

        // Execute the original
        original.execute_simple().await.unwrap();

        // Clone for new execution
        let cloned = original.clone_for_new_execution();

        assert_ne!(original.id(), cloned.id());
        assert_eq!(cloned.status(), &ActionStatus::Pending);
        assert_eq!(cloned.action.execution_count, 0);
    }

    #[tokio::test]
    async fn test_email_notification_service() {
        let mut task = Task::new(
            "Email Task".to_string(),
            "Send email notification".to_string(),
            "EmailNotificationService".to_string(),
        );

        // Add email arguments
        task.action
            .add_argument("to_email".to_string(), "user@example.com")
            .unwrap();
        task.action
            .add_argument("subject".to_string(), "Test Subject")
            .unwrap();

        let service = EmailNotificationService::new("smtp.example.com");

        let result = task.execute(&service).await;
        assert!(result.is_ok());
        assert_eq!(task.status(), &ActionStatus::Completed);
    }

    #[tokio::test]
    async fn test_data_backup_service_writes_artifact() {
        let dir = std::env::temp_dir().join(format!("paladin_backup_{}", Uuid::new_v4()));
        let service = DataBackupService::new(dir.to_string_lossy().to_string());

        let action = Action::new(
            "backup".to_string(),
            "nightly backup".to_string(),
            "task_manager".to_string(),
            "DataBackupService".to_string(),
        );
        let result = service.execute(&action).await.unwrap().unwrap();

        let path = result["backup_path"].as_str().unwrap();
        assert!(
            std::path::Path::new(path).exists(),
            "backup file must exist"
        );
        assert!(result["bytes_written"].as_u64().unwrap() > 0);
        assert_eq!(result["status"], "completed");
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_data_backup_service_rejects_path_traversal() {
        let dir = std::env::temp_dir().join(format!("paladin_backup_{}", Uuid::new_v4()));
        let service = DataBackupService::new(dir.to_string_lossy().to_string());

        let mut action = Action::new(
            "backup".to_string(),
            "malicious backup".to_string(),
            "task_manager".to_string(),
            "DataBackupService".to_string(),
        );
        action
            .add_argument("backup_name".to_string(), "../escape.json")
            .unwrap();

        let result = service.execute(&action).await;
        assert!(matches!(result, Err(TaskError::ExecutionFailed(_))));
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn test_content_indexing_service_builds_index() {
        let service = ContentIndexingService::new(format!("idx_{}", Uuid::new_v4()));
        let mut action = Action::new(
            "index".to_string(),
            "index this".to_string(),
            "task_manager".to_string(),
            "ContentIndexingService".to_string(),
        );
        action
            .add_argument("content".to_string(), "alpha beta beta gamma")
            .unwrap();

        let result = service.execute(&action).await.unwrap().unwrap();
        let path = result["index_path"].as_str().unwrap();
        assert!(std::path::Path::new(path).exists());
        assert_eq!(result["terms_indexed"].as_u64().unwrap(), 3);
        assert_eq!(result["status"], "indexed");
        let _ = std::fs::remove_file(path);
    }

    #[tokio::test]
    async fn test_email_service_delivers_to_sink() {
        let sink = InMemoryEmailSink::new();
        let service = EmailNotificationService::with_sink("smtp.test", sink.clone_sink());

        let mut action = Action::new(
            "email".to_string(),
            "hello body".to_string(),
            "task_manager".to_string(),
            "EmailNotificationService".to_string(),
        );
        action
            .add_argument("to_email".to_string(), "a@b.com")
            .unwrap();
        action.add_argument("subject".to_string(), "Hi").unwrap();

        let result = service.execute(&action).await.unwrap().unwrap();
        assert_eq!(result["status"], "sent");
        let delivered = sink.delivered();
        assert_eq!(delivered.len(), 1);
        assert_eq!(delivered[0].to, "a@b.com");
        assert_eq!(delivered[0].subject, "Hi");
    }

    #[tokio::test]
    async fn test_email_service_propagates_sink_failure() {
        #[derive(Debug, Clone)]
        struct FailingSink;
        #[async_trait]
        impl EmailSink for FailingSink {
            async fn deliver(&self, _message: &EmailMessage) -> Result<(), TaskError> {
                Err(TaskError::ServiceUnavailable("transport down".to_string()))
            }
            fn clone_sink(&self) -> Arc<dyn EmailSink> {
                Arc::new(self.clone())
            }
        }

        let service = EmailNotificationService::with_sink("smtp.test", Arc::new(FailingSink));
        let action = Action::new(
            "email".to_string(),
            "body".to_string(),
            "task_manager".to_string(),
            "EmailNotificationService".to_string(),
        );

        let result = service.execute(&action).await;
        assert!(matches!(result, Err(TaskError::ServiceUnavailable(_))));
    }
}