terraphim_agent_evolution 1.19.3

Agent evolution system for Terraphim AI
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
//! Agent task list evolution with complete lifecycle tracking

use std::collections::{BTreeMap, HashMap};

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use terraphim_persistence::Persistable;
use uuid::Uuid;

use crate::{AgentId, EvolutionError, EvolutionResult, TaskId};

/// Safe conversion from chrono::Duration to std::Duration
fn chrono_to_std_duration(chrono_duration: chrono::Duration) -> Option<std::time::Duration> {
    let nanos = chrono_duration.num_nanoseconds()?;
    if nanos < 0 {
        None
    } else {
        Some(std::time::Duration::from_nanos(nanos as u64))
    }
}

/// Versioned task list evolution system
#[derive(Debug, Clone)]
pub struct TasksEvolution {
    pub agent_id: AgentId,
    pub current_state: TasksState,
    pub history: BTreeMap<DateTime<Utc>, TasksState>,
}

impl TasksEvolution {
    /// Create a new task evolution tracker
    pub fn new(agent_id: AgentId) -> Self {
        Self {
            agent_id,
            current_state: TasksState::default(),
            history: BTreeMap::new(),
        }
    }

    /// Add a new task
    pub async fn add_task(&mut self, task: AgentTask) -> EvolutionResult<()> {
        log::debug!("Adding task: {} - {}", task.id, task.content);

        self.current_state.add_task(task);
        self.save_current_state().await?;

        Ok(())
    }

    /// Start working on a task (move to in_progress)
    pub async fn start_task(&mut self, task_id: &TaskId) -> EvolutionResult<()> {
        log::debug!("Starting task: {}", task_id);

        self.current_state.start_task(task_id)?;
        self.save_current_state().await?;

        Ok(())
    }

    /// Complete a task
    pub async fn complete_task(&mut self, task_id: &TaskId, result: &str) -> EvolutionResult<()> {
        log::info!("Completing task: {} with result: {}", task_id, result);

        self.current_state.complete_task(task_id, result)?;
        self.save_current_state().await?;

        Ok(())
    }

    /// Block a task (waiting on dependencies)
    pub async fn block_task(&mut self, task_id: &TaskId, reason: String) -> EvolutionResult<()> {
        log::debug!("Blocking task: {} - reason: {}", task_id, reason);

        self.current_state.block_task(task_id, reason)?;
        self.save_current_state().await?;

        Ok(())
    }

    /// Cancel a task
    pub async fn cancel_task(&mut self, task_id: &TaskId, reason: String) -> EvolutionResult<()> {
        log::debug!("Cancelling task: {} - reason: {}", task_id, reason);

        self.current_state.cancel_task(task_id, reason)?;
        self.save_current_state().await?;

        Ok(())
    }

    /// Update task progress
    pub async fn update_progress(
        &mut self,
        task_id: &TaskId,
        progress: &str,
    ) -> EvolutionResult<()> {
        self.current_state.update_progress(task_id, progress)?;
        self.save_current_state().await?;

        Ok(())
    }

    /// Add workflow tasks (multiple tasks from a workflow)
    pub async fn add_workflow_tasks(
        &mut self,
        workflow_steps: &[crate::WorkflowStep],
    ) -> EvolutionResult<()> {
        for (i, step) in workflow_steps.iter().enumerate() {
            let task = AgentTask {
                id: format!("workflow_task_{}", i),
                content: step.description.clone(),
                active_form: format!("Working on: {}", step.description),
                status: TaskStatus::Pending,
                priority: Priority::Medium,
                created_at: Utc::now(),
                updated_at: Utc::now(),
                deadline: None,
                dependencies: vec![],
                subtasks: vec![],
                parent_task: None,
                goal_alignment_score: 0.8, // Default alignment
                estimated_duration: step.estimated_duration,
                actual_duration: None,
                metadata: HashMap::new(),
            };

            self.add_task(task).await?;
        }

        Ok(())
    }

    /// Save a versioned snapshot
    pub async fn save_version(&self, timestamp: DateTime<Utc>) -> EvolutionResult<()> {
        let versioned_tasks = VersionedTaskList {
            agent_id: self.agent_id.clone(),
            timestamp,
            state: self.current_state.clone(),
        };

        versioned_tasks.save().await?;
        log::debug!(
            "Saved task list version for agent {} at {}",
            self.agent_id,
            timestamp
        );

        Ok(())
    }

    /// Load task state at a specific time
    pub async fn load_version(&self, timestamp: DateTime<Utc>) -> EvolutionResult<TasksState> {
        let mut versioned_tasks = VersionedTaskList::new(self.get_version_key(timestamp));
        let loaded = versioned_tasks.load().await?;
        Ok(loaded.state)
    }

    /// Get the storage key for a specific version
    pub fn get_version_key(&self, timestamp: DateTime<Utc>) -> String {
        format!("agent_{}/tasks/v_{}", self.agent_id, timestamp.timestamp())
    }

    /// Save the current state
    async fn save_current_state(&self) -> EvolutionResult<()> {
        let current_tasks = CurrentTasksState {
            agent_id: self.agent_id.clone(),
            state: self.current_state.clone(),
        };

        current_tasks.save().await?;
        Ok(())
    }
}

/// Current task state of an agent
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct TasksState {
    pub pending: Vec<AgentTask>,
    pub in_progress: Vec<AgentTask>,
    pub completed: Vec<CompletedTask>,
    pub blocked: Vec<BlockedTask>,
    pub cancelled: Vec<CancelledTask>,
    pub metadata: TasksMetadata,
}

impl TasksState {
    /// Add a new task
    pub fn add_task(&mut self, task: AgentTask) {
        self.pending.push(task);
        self.metadata.last_updated = Utc::now();
        self.metadata.total_tasks_created += 1;
    }

    /// Start a task (move from pending to in_progress)
    pub fn start_task(&mut self, task_id: &TaskId) -> EvolutionResult<()> {
        if let Some(pos) = self.pending.iter().position(|t| t.id == *task_id) {
            let mut task = self.pending.remove(pos);
            task.status = TaskStatus::InProgress;
            task.updated_at = Utc::now();
            self.in_progress.push(task);
            self.metadata.last_updated = Utc::now();
            Ok(())
        } else {
            Err(EvolutionError::TaskNotFound(task_id.clone()))
        }
    }

    /// Complete a task
    pub fn complete_task(&mut self, task_id: &TaskId, result: &str) -> EvolutionResult<()> {
        if let Some(pos) = self.in_progress.iter().position(|t| t.id == *task_id) {
            let task = self.in_progress.remove(pos);
            let completed_task = CompletedTask {
                original_task: task.clone(),
                completed_at: Utc::now(),
                result: result.to_string(),
                actual_duration: chrono_to_std_duration(Utc::now() - task.created_at),
                success: true,
            };

            self.completed.push(completed_task);
            self.metadata.last_updated = Utc::now();
            self.metadata.total_completed += 1;
            Ok(())
        } else if let Some(pos) = self.pending.iter().position(|t| t.id == *task_id) {
            // Allow completing pending tasks directly
            let task = self.pending.remove(pos);
            let completed_task = CompletedTask {
                original_task: task.clone(),
                completed_at: Utc::now(),
                result: result.to_string(),
                actual_duration: chrono_to_std_duration(Utc::now() - task.created_at),
                success: true,
            };

            self.completed.push(completed_task);
            self.metadata.last_updated = Utc::now();
            self.metadata.total_completed += 1;
            Ok(())
        } else {
            Err(EvolutionError::TaskNotFound(task_id.clone()))
        }
    }

    /// Block a task
    pub fn block_task(&mut self, task_id: &TaskId, reason: String) -> EvolutionResult<()> {
        if let Some(pos) = self.in_progress.iter().position(|t| t.id == *task_id) {
            let task = self.in_progress.remove(pos);
            let blocked_task = BlockedTask {
                original_task: task,
                blocked_at: Utc::now(),
                reason,
                dependencies: vec![],
            };

            self.blocked.push(blocked_task);
            self.metadata.last_updated = Utc::now();
            Ok(())
        } else {
            Err(EvolutionError::TaskNotFound(task_id.clone()))
        }
    }

    /// Cancel a task
    pub fn cancel_task(&mut self, task_id: &TaskId, reason: String) -> EvolutionResult<()> {
        let mut found = false;

        // Try pending first
        if let Some(pos) = self.pending.iter().position(|t| t.id == *task_id) {
            let task = self.pending.remove(pos);
            let cancelled_task = CancelledTask {
                original_task: task,
                cancelled_at: Utc::now(),
                reason: reason.clone(),
            };
            self.cancelled.push(cancelled_task);
            found = true;
        }

        // Try in_progress
        if !found {
            if let Some(pos) = self.in_progress.iter().position(|t| t.id == *task_id) {
                let task = self.in_progress.remove(pos);
                let cancelled_task = CancelledTask {
                    original_task: task,
                    cancelled_at: Utc::now(),
                    reason: reason.clone(),
                };
                self.cancelled.push(cancelled_task);
                found = true;
            }
        }

        if found {
            self.metadata.last_updated = Utc::now();
            self.metadata.total_cancelled += 1;
            Ok(())
        } else {
            Err(EvolutionError::TaskNotFound(task_id.clone()))
        }
    }

    /// Update task progress
    pub fn update_progress(&mut self, task_id: &TaskId, progress: &str) -> EvolutionResult<()> {
        if let Some(task) = self.in_progress.iter_mut().find(|t| t.id == *task_id) {
            task.metadata.insert(
                "progress".to_string(),
                serde_json::Value::String(progress.to_string()),
            );
            task.updated_at = Utc::now();
            self.metadata.last_updated = Utc::now();
            Ok(())
        } else {
            Err(EvolutionError::TaskNotFound(task_id.clone()))
        }
    }

    /// Calculate task completion rate
    pub fn calculate_completion_rate(&self) -> f64 {
        let total = self.total_tasks();
        if total > 0 {
            self.completed.len() as f64 / total as f64
        } else {
            0.0
        }
    }

    /// Calculate goal alignment score based on completed tasks
    pub fn calculate_alignment_score(&self) -> f64 {
        if self.completed.is_empty() {
            return 0.5; // Neutral if no completed tasks
        }

        let total_alignment: f64 = self
            .completed
            .iter()
            .map(|ct| ct.original_task.goal_alignment_score)
            .sum();

        total_alignment / self.completed.len() as f64
    }

    /// Get total number of tasks
    pub fn total_tasks(&self) -> usize {
        self.pending.len()
            + self.in_progress.len()
            + self.completed.len()
            + self.blocked.len()
            + self.cancelled.len()
    }

    /// Get number of completed tasks
    pub fn completed_tasks(&self) -> usize {
        self.completed.len()
    }

    /// Get number of pending tasks
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }

    /// Get number of in-progress tasks
    pub fn in_progress_count(&self) -> usize {
        self.in_progress.len()
    }

    /// Get number of blocked tasks
    pub fn blocked_count(&self) -> usize {
        self.blocked.len()
    }

    /// Calculate average task complexity
    pub fn calculate_average_complexity(&self) -> f64 {
        let all_tasks: Vec<&AgentTask> = self
            .pending
            .iter()
            .chain(self.in_progress.iter())
            .chain(self.completed.iter().map(|ct| &ct.original_task))
            .chain(self.blocked.iter().map(|bt| &bt.original_task))
            .chain(self.cancelled.iter().map(|ct| &ct.original_task))
            .collect();

        if all_tasks.is_empty() {
            0.0
        } else {
            // Use complexity as a simple metric based on content length and priority
            let total_complexity: f64 = all_tasks
                .iter()
                .map(|task| {
                    let length_complexity = task.content.len() as f64 / 100.0; // Normalize by 100 chars
                    let priority_complexity = match task.priority {
                        Priority::Low => 1.0,
                        Priority::Medium => 2.0,
                        Priority::High => 3.0,
                        Priority::Critical => 4.0,
                    };
                    length_complexity + priority_complexity
                })
                .sum();
            total_complexity / all_tasks.len() as f64
        }
    }
}

/// Individual agent task
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentTask {
    pub id: TaskId,
    pub content: String,
    pub active_form: String, // "Working on X" vs "Work on X"
    pub status: TaskStatus,
    pub priority: Priority,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub deadline: Option<DateTime<Utc>>,
    pub dependencies: Vec<TaskId>,
    pub subtasks: Vec<TaskId>,
    pub parent_task: Option<TaskId>,
    pub goal_alignment_score: f64,
    pub estimated_duration: Option<std::time::Duration>,
    pub actual_duration: Option<std::time::Duration>,
    pub metadata: HashMap<String, serde_json::Value>,
}

impl AgentTask {
    /// Create a new task
    pub fn new(content: String) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            active_form: format!("Working on: {}", content),
            content,
            status: TaskStatus::Pending,
            priority: Priority::Medium,
            created_at: Utc::now(),
            updated_at: Utc::now(),
            deadline: None,
            dependencies: vec![],
            subtasks: vec![],
            parent_task: None,
            goal_alignment_score: 0.5,
            estimated_duration: None,
            actual_duration: None,
            metadata: HashMap::new(),
        }
    }

    /// Check if task is overdue
    pub fn is_overdue(&self) -> bool {
        if let Some(deadline) = self.deadline {
            Utc::now() > deadline
        } else {
            false
        }
    }

    /// Get task age
    pub fn age(&self) -> chrono::Duration {
        Utc::now() - self.created_at
    }
}

/// Task status enumeration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TaskStatus {
    Pending,
    InProgress,
    Completed,
    Blocked,
    Cancelled,
}

/// Task priority levels
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub enum Priority {
    Low,
    Medium,
    High,
    Critical,
}

/// Completed task record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletedTask {
    pub original_task: AgentTask,
    pub completed_at: DateTime<Utc>,
    pub result: String,
    pub actual_duration: Option<std::time::Duration>,
    pub success: bool,
}

/// Blocked task record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockedTask {
    pub original_task: AgentTask,
    pub blocked_at: DateTime<Utc>,
    pub reason: String,
    pub dependencies: Vec<TaskId>,
}

/// Cancelled task record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CancelledTask {
    pub original_task: AgentTask,
    pub cancelled_at: DateTime<Utc>,
    pub reason: String,
}

/// Task list metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TasksMetadata {
    pub created_at: DateTime<Utc>,
    pub last_updated: DateTime<Utc>,
    pub total_tasks_created: u32,
    pub total_completed: u32,
    pub total_cancelled: u32,
    pub average_completion_time: Option<std::time::Duration>,
}

impl Default for TasksMetadata {
    fn default() -> Self {
        let now = Utc::now();
        Self {
            created_at: now,
            last_updated: now,
            total_tasks_created: 0,
            total_completed: 0,
            total_cancelled: 0,
            average_completion_time: None,
        }
    }
}

/// Workflow step for task creation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowStep {
    pub description: String,
    pub estimated_duration: Option<std::time::Duration>,
}

/// Versioned task list for persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VersionedTaskList {
    pub agent_id: AgentId,
    pub timestamp: DateTime<Utc>,
    pub state: TasksState,
}

#[async_trait]
impl Persistable for VersionedTaskList {
    fn new(_key: String) -> Self {
        Self {
            agent_id: String::new(),
            timestamp: Utc::now(),
            state: TasksState::default(),
        }
    }

    async fn save(&self) -> terraphim_persistence::Result<()> {
        self.save_to_all().await
    }

    async fn save_to_one(&self, profile_name: &str) -> terraphim_persistence::Result<()> {
        self.save_to_profile(profile_name).await
    }

    async fn load(&mut self) -> terraphim_persistence::Result<Self> {
        let key = self.get_key();
        self.load_from_operator(
            &key,
            &terraphim_persistence::DeviceStorage::instance()
                .await?
                .fastest_op,
        )
        .await
    }

    fn get_key(&self) -> String {
        format!(
            "agent_{}/tasks/v_{}",
            self.agent_id,
            self.timestamp.timestamp()
        )
    }
}

/// Current task state for persistence
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CurrentTasksState {
    pub agent_id: AgentId,
    pub state: TasksState,
}

#[async_trait]
impl Persistable for CurrentTasksState {
    fn new(key: String) -> Self {
        Self {
            agent_id: key,
            state: TasksState::default(),
        }
    }

    async fn save(&self) -> terraphim_persistence::Result<()> {
        self.save_to_all().await
    }

    async fn save_to_one(&self, profile_name: &str) -> terraphim_persistence::Result<()> {
        self.save_to_profile(profile_name).await
    }

    async fn load(&mut self) -> terraphim_persistence::Result<Self> {
        let key = self.get_key();
        self.load_from_operator(
            &key,
            &terraphim_persistence::DeviceStorage::instance()
                .await?
                .fastest_op,
        )
        .await
    }

    fn get_key(&self) -> String {
        format!("agent_{}/tasks/current", self.agent_id)
    }
}

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

    #[tokio::test]
    async fn test_task_evolution_creation() {
        let agent_id = "test_agent".to_string();
        let tasks = TasksEvolution::new(agent_id.clone());

        assert_eq!(tasks.agent_id, agent_id);
        assert_eq!(tasks.current_state.total_tasks(), 0);
    }

    #[tokio::test]
    async fn test_task_lifecycle() {
        let mut tasks = TasksEvolution::new("test_agent".to_string());

        // Add a task
        let task = AgentTask::new("Test task".to_string());
        let task_id = task.id.clone();

        tasks.add_task(task).await.unwrap();
        assert_eq!(tasks.current_state.pending.len(), 1);

        // Start the task
        tasks.start_task(&task_id).await.unwrap();
        assert_eq!(tasks.current_state.pending.len(), 0);
        assert_eq!(tasks.current_state.in_progress.len(), 1);

        // Complete the task
        tasks
            .complete_task(&task_id, "Task completed successfully")
            .await
            .unwrap();
        assert_eq!(tasks.current_state.in_progress.len(), 0);
        assert_eq!(tasks.current_state.completed.len(), 1);
    }

    #[tokio::test]
    async fn test_task_completion_rate() {
        let mut state = TasksState::default();

        // Add some tasks
        state.add_task(AgentTask::new("Task 1".to_string()));
        state.add_task(AgentTask::new("Task 2".to_string()));

        let task_id = state.pending[0].id.clone();
        state.complete_task(&task_id, "Done").unwrap();

        assert_eq!(state.calculate_completion_rate(), 0.5);
    }

    #[tokio::test]
    async fn test_task_blocking() {
        let mut tasks = TasksEvolution::new("test_agent".to_string());

        let task = AgentTask::new("Blocking test".to_string());
        let task_id = task.id.clone();

        tasks.add_task(task).await.unwrap();
        tasks.start_task(&task_id).await.unwrap();
        tasks
            .block_task(&task_id, "Waiting for dependency".to_string())
            .await
            .unwrap();

        assert_eq!(tasks.current_state.blocked.len(), 1);
        assert_eq!(tasks.current_state.in_progress.len(), 0);
    }
}