code-mesh-core 0.1.0

High-performance, WASM-powered distributed swarm intelligence core library for concurrent code execution and neural mesh computing
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
//! Todo management tool for task tracking and dependency management

use super::{Tool, ToolContext, ToolError, ToolResult};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;

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

/// Task priority enumeration
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TaskPriority {
    Low,
    Medium,
    High,
    Critical,
}

/// Task dependency type
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DependencyType {
    /// Task must complete before this task can start
    BlocksStart,
    /// Task must complete before this task can complete
    BlocksCompletion,
    /// Tasks should be worked on together
    Related,
}

/// Task dependency
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskDependency {
    pub task_id: String,
    pub dependency_type: DependencyType,
    pub description: Option<String>,
}

/// Task metadata for analytics and tracking
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskMetadata {
    pub estimated_duration: Option<chrono::Duration>,
    pub actual_duration: Option<chrono::Duration>,
    pub tags: Vec<String>,
    pub assignee: Option<String>,
    pub project: Option<String>,
    pub milestone: Option<String>,
}

/// Individual task item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Task {
    pub id: String,
    pub content: String,
    pub status: TaskStatus,
    pub priority: TaskPriority,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
    pub completed_at: Option<DateTime<Utc>>,
    pub due_date: Option<DateTime<Utc>>,
    pub dependencies: Vec<TaskDependency>,
    pub metadata: TaskMetadata,
    pub progress: f32, // 0.0 to 1.0
    pub notes: Vec<String>,
}

impl Task {
    pub fn new(id: String, content: String, priority: TaskPriority) -> Self {
        let now = Utc::now();
        Self {
            id,
            content,
            status: TaskStatus::Pending,
            priority,
            created_at: now,
            updated_at: now,
            completed_at: None,
            due_date: None,
            dependencies: Vec::new(),
            metadata: TaskMetadata {
                estimated_duration: None,
                actual_duration: None,
                tags: Vec::new(),
                assignee: None,
                project: None,
                milestone: None,
            },
            progress: 0.0,
            notes: Vec::new(),
        }
    }

    pub fn update_status(&mut self, status: TaskStatus) {
        self.status = status;
        self.updated_at = Utc::now();
        
        if self.status == TaskStatus::Completed {
            self.completed_at = Some(Utc::now());
            self.progress = 1.0;
            
            // Calculate actual duration if we have a created_at time
            self.metadata.actual_duration = Some(
                self.updated_at.signed_duration_since(self.created_at)
            );
        }
    }

    pub fn add_note(&mut self, note: String) {
        self.notes.push(note);
        self.updated_at = Utc::now();
    }

    pub fn add_dependency(&mut self, dependency: TaskDependency) {
        self.dependencies.push(dependency);
        self.updated_at = Utc::now();
    }

    pub fn is_blocked_by(&self, other_task: &Task) -> bool {
        self.dependencies.iter().any(|dep| {
            dep.task_id == other_task.id && 
            matches!(dep.dependency_type, DependencyType::BlocksStart) &&
            other_task.status != TaskStatus::Completed
        })
    }

    pub fn can_start(&self, all_tasks: &HashMap<String, Task>) -> bool {
        // Check if all blocking dependencies are completed
        for dep in &self.dependencies {
            if matches!(dep.dependency_type, DependencyType::BlocksStart) {
                if let Some(blocking_task) = all_tasks.get(&dep.task_id) {
                    if blocking_task.status != TaskStatus::Completed {
                        return false;
                    }
                }
            }
        }
        true
    }
}

/// Task list for a session
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskList {
    pub session_id: String,
    pub tasks: HashMap<String, Task>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl TaskList {
    pub fn new(session_id: String) -> Self {
        let now = Utc::now();
        Self {
            session_id,
            tasks: HashMap::new(),
            created_at: now,
            updated_at: now,
        }
    }

    pub fn add_task(&mut self, task: Task) {
        self.tasks.insert(task.id.clone(), task);
        self.updated_at = Utc::now();
    }

    pub fn get_task(&self, id: &str) -> Option<&Task> {
        self.tasks.get(id)
    }

    pub fn get_task_mut(&mut self, id: &str) -> Option<&mut Task> {
        if self.tasks.contains_key(id) {
            self.updated_at = Utc::now();
        }
        self.tasks.get_mut(id)
    }

    pub fn remove_task(&mut self, id: &str) -> Option<Task> {
        let task = self.tasks.remove(id);
        if task.is_some() {
            self.updated_at = Utc::now();
        }
        task
    }

    pub fn get_tasks_by_status(&self, status: &TaskStatus) -> Vec<&Task> {
        self.tasks.values().filter(|t| &t.status == status).collect()
    }

    pub fn get_tasks_by_priority(&self, priority: &TaskPriority) -> Vec<&Task> {
        self.tasks.values().filter(|t| &t.priority == priority).collect()
    }

    pub fn get_available_tasks(&self) -> Vec<&Task> {
        self.tasks.values()
            .filter(|t| t.status == TaskStatus::Pending && t.can_start(&self.tasks))
            .collect()
    }

    pub fn get_blocked_tasks(&self) -> Vec<&Task> {
        self.tasks.values()
            .filter(|t| t.status == TaskStatus::Pending && !t.can_start(&self.tasks))
            .collect()
    }

    pub fn get_completion_stats(&self) -> TaskStats {
        let total = self.tasks.len();
        let completed = self.get_tasks_by_status(&TaskStatus::Completed).len();
        let in_progress = self.get_tasks_by_status(&TaskStatus::InProgress).len();
        let pending = self.get_tasks_by_status(&TaskStatus::Pending).len();
        let blocked = self.get_blocked_tasks().len();
        let cancelled = self.get_tasks_by_status(&TaskStatus::Cancelled).len();

        TaskStats {
            total,
            completed,
            in_progress,
            pending,
            blocked,
            cancelled,
            completion_rate: if total > 0 { completed as f32 / total as f32 } else { 0.0 },
        }
    }
}

/// Task statistics
#[derive(Debug, Serialize, Deserialize)]
pub struct TaskStats {
    pub total: usize,
    pub completed: usize,
    pub in_progress: usize,
    pub pending: usize,
    pub blocked: usize,
    pub cancelled: usize,
    pub completion_rate: f32,
}

/// In-memory storage for task lists
#[derive(Debug)]
pub struct TaskStorage {
    task_lists: Arc<RwLock<HashMap<String, TaskList>>>,
}

impl TaskStorage {
    pub fn new() -> Self {
        Self {
            task_lists: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    pub async fn get_or_create_list(&self, session_id: &str) -> TaskList {
        let mut lists = self.task_lists.write().await;
        lists.entry(session_id.to_string())
            .or_insert_with(|| TaskList::new(session_id.to_string()))
            .clone()
    }

    pub async fn save_list(&self, list: TaskList) {
        let mut lists = self.task_lists.write().await;
        lists.insert(list.session_id.clone(), list);
    }

    pub async fn get_all_sessions(&self) -> Vec<String> {
        let lists = self.task_lists.read().await;
        lists.keys().cloned().collect()
    }
}

/// Todo management tool
pub struct TodoTool {
    storage: TaskStorage,
}

impl TodoTool {
    pub fn new() -> Self {
        Self {
            storage: TaskStorage::new(),
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(tag = "action")]
#[serde(rename_all = "snake_case")]
enum TodoAction {
    List,
    Add { content: String, priority: Option<TaskPriority> },
    Update { 
        id: String, 
        status: Option<TaskStatus>, 
        priority: Option<TaskPriority>,
        content: Option<String>,
        progress: Option<f32>,
    },
    Remove { id: String },
    AddDependency { 
        task_id: String, 
        depends_on: String, 
        dependency_type: DependencyType,
        description: Option<String>,
    },
    AddNote { id: String, note: String },
    Stats,
    Export { format: Option<String> },
}

#[async_trait]
impl Tool for TodoTool {
    fn id(&self) -> &str {
        "todo"
    }
    
    fn description(&self) -> &str {
        "Comprehensive task management tool with dependency tracking, progress reporting, and analytics"
    }
    
    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["list", "add", "update", "remove", "add_dependency", "add_note", "stats", "export"],
                    "description": "The action to perform"
                },
                "content": {
                    "type": "string",
                    "description": "Task content (for add action)"
                },
                "priority": {
                    "type": "string",
                    "enum": ["low", "medium", "high", "critical"],
                    "description": "Task priority"
                },
                "id": {
                    "type": "string",
                    "description": "Task ID (for update, remove, add_note actions)"
                },
                "status": {
                    "type": "string",
                    "enum": ["pending", "in_progress", "completed", "cancelled", "blocked"],
                    "description": "Task status (for update action)"
                },
                "progress": {
                    "type": "number",
                    "minimum": 0.0,
                    "maximum": 1.0,
                    "description": "Task progress from 0.0 to 1.0 (for update action)"
                },
                "depends_on": {
                    "type": "string",
                    "description": "ID of task this depends on (for add_dependency action)"
                },
                "dependency_type": {
                    "type": "string",
                    "enum": ["blocks_start", "blocks_completion", "related"],
                    "description": "Type of dependency (for add_dependency action)"
                },
                "note": {
                    "type": "string",
                    "description": "Note to add to task (for add_note action)"
                },
                "format": {
                    "type": "string",
                    "enum": ["json", "markdown", "csv"],
                    "description": "Export format (for export action)"
                },
                "description": {
                    "type": "string",
                    "description": "Description for dependency (for add_dependency action)"
                }
            },
            "required": ["action"]
        })
    }
    
    async fn execute(&self, args: Value, ctx: ToolContext) -> Result<ToolResult, ToolError> {
        let action: TodoAction = serde_json::from_value(args)
            .map_err(|e| ToolError::InvalidParameters(e.to_string()))?;

        let mut task_list = self.storage.get_or_create_list(&ctx.session_id).await;

        match action {
            TodoAction::List => {
                let output = format_task_list(&task_list);
                let stats = task_list.get_completion_stats();
                
                Ok(ToolResult {
                    title: format!("Task List ({} tasks)", task_list.tasks.len()),
                    output,
                    metadata: json!({
                        "stats": stats,
                        "session_id": ctx.session_id,
                        "task_count": task_list.tasks.len()
                    }),
                })
            },

            TodoAction::Add { content, priority } => {
                let task_id = Uuid::new_v4().to_string();
                let priority_value = priority.unwrap_or(TaskPriority::Medium);
                let task = Task::new(task_id.clone(), content.clone(), priority_value.clone());
                
                task_list.add_task(task);
                self.storage.save_list(task_list.clone()).await;

                Ok(ToolResult {
                    title: "Task Added".to_string(),
                    output: format!("Added task: {} (ID: {})", content, task_id),
                    metadata: json!({
                        "task_id": task_id,
                        "content": content,
                        "priority": priority_value
                    }),
                })
            },

            TodoAction::Update { id, status, priority, content, progress } => {
                if let Some(task) = task_list.get_task_mut(&id) {
                    if let Some(new_status) = status {
                        task.update_status(new_status);
                    }
                    if let Some(new_priority) = priority {
                        task.priority = new_priority;
                        task.updated_at = Utc::now();
                    }
                    if let Some(new_content) = content {
                        task.content = new_content;
                        task.updated_at = Utc::now();
                    }
                    if let Some(new_progress) = progress {
                        task.progress = new_progress.clamp(0.0, 1.0);
                        task.updated_at = Utc::now();
                    }
                    
                    let task_content = task.content.clone();
                    let task_status = task.status.clone();
                    let task_priority = task.priority.clone();
                    let task_progress = task.progress;

                    self.storage.save_list(task_list).await;

                    Ok(ToolResult {
                        title: "Task Updated".to_string(),
                        output: format!("Updated task: {}", task_content),
                        metadata: json!({
                            "task_id": id,
                            "status": task_status,
                            "priority": task_priority,
                            "progress": task_progress
                        }),
                    })
                } else {
                    Err(ToolError::InvalidParameters(format!("Task not found: {}", id)))
                }
            },

            TodoAction::Remove { id } => {
                if let Some(task) = task_list.remove_task(&id) {
                    self.storage.save_list(task_list).await;

                    Ok(ToolResult {
                        title: "Task Removed".to_string(),
                        output: format!("Removed task: {}", task.content),
                        metadata: json!({
                            "task_id": id,
                            "content": task.content
                        }),
                    })
                } else {
                    Err(ToolError::InvalidParameters(format!("Task not found: {}", id)))
                }
            },

            TodoAction::AddDependency { task_id, depends_on, dependency_type, description } => {
                // Verify both tasks exist
                if !task_list.tasks.contains_key(&task_id) {
                    return Err(ToolError::InvalidParameters(format!("Task not found: {}", task_id)));
                }
                if !task_list.tasks.contains_key(&depends_on) {
                    return Err(ToolError::InvalidParameters(format!("Dependency task not found: {}", depends_on)));
                }

                // Check for circular dependencies
                if would_create_cycle(&task_list, &task_id, &depends_on) {
                    return Err(ToolError::InvalidParameters("Adding this dependency would create a circular dependency".to_string()));
                }

                if let Some(task) = task_list.get_task_mut(&task_id) {
                    let dependency = TaskDependency {
                        task_id: depends_on.clone(),
                        dependency_type: dependency_type.clone(),
                        description,
                    };
                    task.add_dependency(dependency);
                    self.storage.save_list(task_list).await;

                    Ok(ToolResult {
                        title: "Dependency Added".to_string(),
                        output: format!("Added dependency: {} depends on {}", task_id, depends_on),
                        metadata: json!({
                            "task_id": task_id,
                            "depends_on": depends_on,
                            "dependency_type": dependency_type
                        }),
                    })
                } else {
                    Err(ToolError::InvalidParameters(format!("Task not found: {}", task_id)))
                }
            },

            TodoAction::AddNote { id, note } => {
                if let Some(task) = task_list.get_task_mut(&id) {
                    task.add_note(note.clone());
                    let task_content = task.content.clone();
                    self.storage.save_list(task_list).await;

                    Ok(ToolResult {
                        title: "Note Added".to_string(),
                        output: format!("Added note to task {}: {}", task_content, note),
                        metadata: json!({
                            "task_id": id,
                            "note": note
                        }),
                    })
                } else {
                    Err(ToolError::InvalidParameters(format!("Task not found: {}", id)))
                }
            },

            TodoAction::Stats => {
                let stats = task_list.get_completion_stats();
                let available_tasks = task_list.get_available_tasks();
                let blocked_tasks = task_list.get_blocked_tasks();

                let output = format!(
                    "Task Statistics:\n\
                     Total tasks: {}\n\
                     Completed: {}\n\
                     In progress: {}\n\
                     Pending: {}\n\
                     Blocked: {}\n\
                     Cancelled: {}\n\
                     Completion rate: {:.1}%\n\n\
                     Available tasks (can start now): {}\n\
                     Blocked tasks (waiting on dependencies): {}",
                    stats.total,
                    stats.completed,
                    stats.in_progress,
                    stats.pending,
                    stats.blocked,
                    stats.cancelled,
                    stats.completion_rate * 100.0,
                    available_tasks.len(),
                    blocked_tasks.len()
                );

                Ok(ToolResult {
                    title: "Task Statistics".to_string(),
                    output,
                    metadata: json!({
                        "stats": stats,
                        "available_tasks": available_tasks.len(),
                        "blocked_tasks": blocked_tasks.len()
                    }),
                })
            },

            TodoAction::Export { format } => {
                let format = format.as_deref().unwrap_or("json");
                let output = match format {
                    "json" => serde_json::to_string_pretty(&task_list.tasks)
                        .map_err(|e| ToolError::ExecutionFailed(format!("JSON serialization failed: {}", e)))?,
                    "markdown" => export_to_markdown(&task_list),
                    "csv" => export_to_csv(&task_list)?,
                    _ => return Err(ToolError::InvalidParameters("Unsupported export format".to_string())),
                };

                Ok(ToolResult {
                    title: format!("Task Export ({})", format),
                    output,
                    metadata: json!({
                        "format": format,
                        "task_count": task_list.tasks.len(),
                        "exported_at": Utc::now()
                    }),
                })
            },
        }
    }
}

/// Format task list for display
fn format_task_list(task_list: &TaskList) -> String {
    if task_list.tasks.is_empty() {
        return "No tasks found.".to_string();
    }

    let mut output = String::new();
    let stats = task_list.get_completion_stats();
    
    output.push_str(&format!(
        "Task List ({}% complete)\n\n",
        (stats.completion_rate * 100.0) as u32
    ));

    // Group by status
    let statuses = [
        TaskStatus::InProgress,
        TaskStatus::Pending,
        TaskStatus::Blocked,
        TaskStatus::Completed,
        TaskStatus::Cancelled,
    ];

    for status in &statuses {
        let tasks = task_list.get_tasks_by_status(status);
        if !tasks.is_empty() {
            output.push_str(&format!("## {:?} ({})\n", status, tasks.len()));
            
            for task in tasks {
                let progress_bar = create_progress_bar(task.progress);
                let dependencies = if task.dependencies.is_empty() {
                    String::new()
                } else {
                    format!(" (depends on: {})", 
                        task.dependencies.iter()
                            .map(|d| &d.task_id[..8])
                            .collect::<Vec<_>>()
                            .join(", ")
                    )
                };

                output.push_str(&format!(
                    "- [{}] {} {} {}{}\n",
                    if task.status == TaskStatus::Completed { "x" } else { " " },
                    task.content,
                    format!("({:?})", task.priority),
                    progress_bar,
                    dependencies
                ));

                if !task.notes.is_empty() {
                    for note in &task.notes {
                        output.push_str(&format!("  📝 {}\n", note));
                    }
                }
            }
            output.push('\n');
        }
    }

    output
}

/// Create a simple progress bar
fn create_progress_bar(progress: f32) -> String {
    let width = 10;
    let filled = (progress * width as f32) as usize;
    let empty = width - filled;
    format!("[{}{}] {:.0}%", "".repeat(filled), "".repeat(empty), progress * 100.0)
}

/// Export task list to markdown format
fn export_to_markdown(task_list: &TaskList) -> String {
    let mut output = format!("# Task List - {}\n\n", task_list.session_id);
    
    for task in task_list.tasks.values() {
        output.push_str(&format!(
            "## {} ({})\n\n",
            task.content,
            task.id
        ));
        
        output.push_str(&format!("- **Status**: {:?}\n", task.status));
        output.push_str(&format!("- **Priority**: {:?}\n", task.priority));
        output.push_str(&format!("- **Progress**: {:.1}%\n", task.progress * 100.0));
        output.push_str(&format!("- **Created**: {}\n", task.created_at.format("%Y-%m-%d %H:%M:%S")));
        
        if let Some(completed_at) = task.completed_at {
            output.push_str(&format!("- **Completed**: {}\n", completed_at.format("%Y-%m-%d %H:%M:%S")));
        }
        
        if !task.dependencies.is_empty() {
            output.push_str("- **Dependencies**:\n");
            for dep in &task.dependencies {
                output.push_str(&format!("  - {} ({:?})\n", dep.task_id, dep.dependency_type));
            }
        }
        
        if !task.notes.is_empty() {
            output.push_str("- **Notes**:\n");
            for note in &task.notes {
                output.push_str(&format!("  - {}\n", note));
            }
        }
        
        output.push('\n');
    }
    
    output
}

/// Export task list to CSV format
fn export_to_csv(task_list: &TaskList) -> Result<String, ToolError> {
    let mut output = "ID,Content,Status,Priority,Progress,Created,Updated,Completed,Dependencies,Notes\n".to_string();
    
    for task in task_list.tasks.values() {
        let dependencies = task.dependencies.iter()
            .map(|d| format!("{}:{:?}", d.task_id, d.dependency_type))
            .collect::<Vec<_>>()
            .join(";");
        
        let notes = task.notes.join(";");
        
        let completed = task.completed_at
            .map(|t| t.format("%Y-%m-%d %H:%M:%S").to_string())
            .unwrap_or_default();
        
        output.push_str(&format!(
            "{},{},{:?},{:?},{:.2},{},{},{},{},{}\n",
            task.id,
            task.content,
            task.status,
            task.priority,
            task.progress,
            task.created_at.format("%Y-%m-%d %H:%M:%S"),
            task.updated_at.format("%Y-%m-%d %H:%M:%S"),
            completed,
            dependencies,
            notes
        ));
    }
    
    Ok(output)
}

/// Check if adding a dependency would create a circular dependency
fn would_create_cycle(task_list: &TaskList, from_task: &str, to_task: &str) -> bool {
    let mut visited = HashSet::new();
    let mut stack = vec![to_task];
    
    while let Some(current) = stack.pop() {
        if current == from_task {
            return true; // Cycle detected
        }
        
        if visited.contains(current) {
            continue;
        }
        visited.insert(current);
        
        if let Some(task) = task_list.tasks.get(current) {
            for dep in &task.dependencies {
                if matches!(dep.dependency_type, DependencyType::BlocksStart | DependencyType::BlocksCompletion) {
                    stack.push(&dep.task_id);
                }
            }
        }
    }
    
    false
}

impl Default for TodoTool {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn test_task_creation() {
        let task = Task::new("test-1".to_string(), "Test task".to_string(), TaskPriority::High);
        assert_eq!(task.id, "test-1");
        assert_eq!(task.content, "Test task");
        assert_eq!(task.priority, TaskPriority::High);
        assert_eq!(task.status, TaskStatus::Pending);
        assert_eq!(task.progress, 0.0);
    }

    #[test]
    fn test_task_status_update() {
        let mut task = Task::new("test-1".to_string(), "Test task".to_string(), TaskPriority::Medium);
        task.update_status(TaskStatus::Completed);
        
        assert_eq!(task.status, TaskStatus::Completed);
        assert_eq!(task.progress, 1.0);
        assert!(task.completed_at.is_some());
        assert!(task.metadata.actual_duration.is_some());
    }

    #[test]
    fn test_task_dependencies() {
        let mut task1 = Task::new("task-1".to_string(), "First task".to_string(), TaskPriority::High);
        let task2 = Task::new("task-2".to_string(), "Second task".to_string(), TaskPriority::Medium);
        
        let dependency = TaskDependency {
            task_id: task2.id.clone(),
            dependency_type: DependencyType::BlocksStart,
            description: Some("Must complete first".to_string()),
        };
        
        task1.add_dependency(dependency);
        assert_eq!(task1.dependencies.len(), 1);
        assert!(task1.is_blocked_by(&task2));
    }

    #[test]
    fn test_cycle_detection() {
        let mut task_list = TaskList::new("test-session".to_string());
        
        let task1 = Task::new("task-1".to_string(), "Task 1".to_string(), TaskPriority::Medium);
        let mut task2 = Task::new("task-2".to_string(), "Task 2".to_string(), TaskPriority::Medium);
        
        // task2 depends on task1
        task2.add_dependency(TaskDependency {
            task_id: "task-1".to_string(),
            dependency_type: DependencyType::BlocksStart,
            description: None,
        });
        
        task_list.add_task(task1);
        task_list.add_task(task2);
        
        // Check if task1 depending on task2 would create a cycle
        assert!(would_create_cycle(&task_list, "task-1", "task-2"));
        assert!(!would_create_cycle(&task_list, "task-2", "task-1"));
    }
}