opencrabs 0.3.12

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Install with: cargo install opencrabs
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
//! Task Management Tool
//!
//! Organize and track multi-step workflows and tasks.

use super::error::{Result, ToolError};
use super::r#trait::{Tool, ToolCapability, ToolExecutionContext, ToolResult};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::fs;
use uuid::Uuid;

/// Task management tool
pub struct TaskTool;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
enum TaskStatus {
    Pending,
    InProgress,
    Completed,
    Blocked,
    Cancelled,
}

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

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Task {
    id: String,
    description: String,
    status: TaskStatus,
    priority: TaskPriority,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
    #[serde(skip_serializing_if = "Option::is_none")]
    completed_at: Option<DateTime<Utc>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    blocked_reason: Option<String>,
    #[serde(default)]
    dependencies: Vec<String>,
    #[serde(default)]
    tags: Vec<String>,
}

#[derive(Debug, Serialize, Deserialize)]
struct TaskStore {
    tasks: HashMap<String, Task>,
}

/// File lock guard that releases the lock when dropped
struct FileLock {
    lock_path: PathBuf,
}

impl FileLock {
    /// Acquire an exclusive lock on the task store file
    async fn acquire(store_path: &Path) -> Result<Self> {
        let lock_path = store_path.with_extension("lock");

        // Ensure parent directory exists
        if let Some(parent) = lock_path.parent() {
            fs::create_dir_all(parent).await.map_err(ToolError::Io)?;
        }

        // Try to acquire lock with retries and exponential backoff
        let max_attempts = 10;
        let mut attempt = 0;
        let mut delay = Duration::from_millis(50);

        loop {
            attempt += 1;

            // Try to create lock file exclusively (fails if exists)
            match fs::OpenOptions::new()
                .write(true)
                .create_new(true)
                .open(&lock_path)
                .await
            {
                Ok(mut file) => {
                    // Write PID and timestamp to lock file for debugging
                    use tokio::io::AsyncWriteExt;
                    let lock_info = format!(
                        "pid: {}\ntimestamp: {}\n",
                        std::process::id(),
                        Utc::now().to_rfc3339()
                    );
                    let _ = file.write_all(lock_info.as_bytes()).await;
                    return Ok(Self { lock_path });
                }
                Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
                    if attempt >= max_attempts {
                        // Check if lock is stale (older than 60 seconds)
                        if let Ok(metadata) = fs::metadata(&lock_path).await
                            && let Ok(modified) = metadata.modified()
                        {
                            let age = std::time::SystemTime::now()
                                .duration_since(modified)
                                .unwrap_or_default();
                            if age > Duration::from_secs(60) {
                                // Stale lock, force remove it
                                tracing::warn!("Removing stale lock file (age: {:?})", age);
                                let _ = fs::remove_file(&lock_path).await;
                                continue;
                            }
                        }

                        return Err(ToolError::Execution(format!(
                            "Failed to acquire lock after {} attempts. \
                             Another process may be using the task store.",
                            max_attempts
                        )));
                    }

                    // Wait before retrying
                    tokio::time::sleep(delay).await;
                    delay = (delay * 2).min(Duration::from_secs(2));
                }
                Err(e) => {
                    return Err(ToolError::Io(e));
                }
            }
        }
    }

    /// Release the lock (called automatically on drop)
    async fn release(&self) -> Result<()> {
        fs::remove_file(&self.lock_path)
            .await
            .map_err(ToolError::Io)
    }
}

impl Drop for FileLock {
    fn drop(&mut self) {
        // Best-effort synchronous cleanup on drop
        // This handles cases where the lock isn't explicitly released
        let _ = std::fs::remove_file(&self.lock_path);
    }
}

impl TaskStore {
    fn new() -> Self {
        Self {
            tasks: HashMap::new(),
        }
    }

    async fn load(path: &Path) -> Result<Self> {
        if path.exists() {
            let content = fs::read_to_string(path).await.map_err(ToolError::Io)?;
            serde_json::from_str(&content)
                .map_err(|e| ToolError::Execution(format!("Failed to parse task store: {}", e)))
        } else {
            Ok(Self::new())
        }
    }

    async fn save(&self, path: &Path) -> Result<()> {
        let content = serde_json::to_string_pretty(self)
            .map_err(|e| ToolError::Execution(format!("Failed to serialize tasks: {}", e)))?;

        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).await.map_err(ToolError::Io)?;
        }

        // Atomic write: write to temp file, then rename
        let temp_path = path.with_extension("tmp");
        fs::write(&temp_path, content)
            .await
            .map_err(ToolError::Io)?;
        fs::rename(&temp_path, path).await.map_err(ToolError::Io)?;

        Ok(())
    }

    /// Load, modify, and save with file locking to prevent race conditions
    async fn with_lock<F, T>(path: &Path, operation: F) -> Result<T>
    where
        F: FnOnce(&mut Self) -> Result<T>,
    {
        // Acquire exclusive lock
        let lock = FileLock::acquire(path).await?;

        // Load current state
        let mut store = Self::load(path).await?;

        // Perform operation
        let result = operation(&mut store)?;

        // Save updated state
        store.save(path).await?;

        // Release lock explicitly (also released on drop)
        let _ = lock.release().await;

        Ok(result)
    }
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "operation")]
enum TaskOperation {
    #[serde(rename = "create")]
    Create {
        description: String,
        #[serde(default)]
        priority: Option<String>,
        #[serde(default)]
        tags: Vec<String>,
        #[serde(default)]
        dependencies: Vec<String>,
    },

    #[serde(rename = "update")]
    Update {
        task_id: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        status: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        priority: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        blocked_reason: Option<String>,
    },

    #[serde(rename = "list")]
    List {
        #[serde(skip_serializing_if = "Option::is_none")]
        status: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        priority: Option<String>,
        #[serde(default)]
        show_completed: bool,
    },

    #[serde(rename = "delete")]
    Delete { task_id: String },

    #[serde(rename = "get")]
    Get { task_id: String },

    #[serde(rename = "clear_completed")]
    ClearCompleted,
}

#[derive(Debug, Deserialize, Serialize)]
struct TaskInput {
    #[serde(flatten)]
    operation: TaskOperation,
}

fn parse_priority(priority_str: &str) -> Result<TaskPriority> {
    match priority_str.to_lowercase().as_str() {
        "low" => Ok(TaskPriority::Low),
        "medium" => Ok(TaskPriority::Medium),
        "high" => Ok(TaskPriority::High),
        "critical" => Ok(TaskPriority::Critical),
        _ => Err(ToolError::InvalidInput(format!(
            "Invalid priority: {}. Must be low, medium, high, or critical",
            priority_str
        ))),
    }
}

fn parse_status(status_str: &str) -> Result<TaskStatus> {
    match status_str.to_lowercase().as_str() {
        "pending" => Ok(TaskStatus::Pending),
        "in_progress" | "inprogress" => Ok(TaskStatus::InProgress),
        "completed" => Ok(TaskStatus::Completed),
        "blocked" => Ok(TaskStatus::Blocked),
        "cancelled" => Ok(TaskStatus::Cancelled),
        _ => Err(ToolError::InvalidInput(format!(
            "Invalid status: {}. Must be pending, in_progress, completed, blocked, or cancelled",
            status_str
        ))),
    }
}

fn get_store_path(context: &ToolExecutionContext) -> PathBuf {
    let dir = crate::config::opencrabs_home()
        .join("agents")
        .join("session");
    let _ = std::fs::create_dir_all(&dir);
    dir.join(format!("tasks_{}.json", context.session_id))
}

#[async_trait]
impl Tool for TaskTool {
    fn name(&self) -> &str {
        "task_manager"
    }

    fn description(&self) -> &str {
        "Manage multi-step workflows and tasks. Create, update, list, and track tasks with priorities, statuses, and dependencies."
    }

    fn input_schema(&self) -> Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "operation": {
                    "type": "string",
                    "description": "Operation to perform",
                    "enum": ["create", "update", "list", "delete", "get", "clear_completed"]
                },
                "description": {
                    "type": "string",
                    "description": "Task description (for create operation)"
                },
                "task_id": {
                    "type": "string",
                    "description": "Task ID (for update, delete, get operations)"
                },
                "status": {
                    "type": "string",
                    "description": "Task status",
                    "enum": ["pending", "in_progress", "completed", "blocked", "cancelled"]
                },
                "priority": {
                    "type": "string",
                    "description": "Task priority",
                    "enum": ["low", "medium", "high", "critical"]
                },
                "blocked_reason": {
                    "type": "string",
                    "description": "Reason why task is blocked (when setting status to blocked)"
                },
                "tags": {
                    "type": "array",
                    "description": "Tags for categorizing tasks",
                    "items": {
                        "type": "string"
                    },
                    "default": []
                },
                "dependencies": {
                    "type": "array",
                    "description": "List of task IDs this task depends on",
                    "items": {
                        "type": "string"
                    },
                    "default": []
                },
                "show_completed": {
                    "type": "boolean",
                    "description": "Include completed tasks in list (default: false)",
                    "default": false
                }
            },
            "required": ["operation"]
        })
    }

    fn capabilities(&self) -> Vec<ToolCapability> {
        vec![ToolCapability::ReadFiles, ToolCapability::WriteFiles]
    }

    fn requires_approval(&self) -> bool {
        false // Task management is safe
    }

    fn validate_input(&self, input: &Value) -> Result<()> {
        let _: TaskInput = serde_json::from_value(input.clone())
            .map_err(|e| ToolError::InvalidInput(format!("Invalid input: {}", e)))?;
        Ok(())
    }

    async fn execute(&self, input: Value, context: &ToolExecutionContext) -> Result<ToolResult> {
        let input: TaskInput = serde_json::from_value(input)?;
        let store_path = get_store_path(context);

        // Read-only operations don't need locking
        let result = match input.operation {
            TaskOperation::List {
                status,
                priority,
                show_completed,
            } => {
                // Read-only: acquire lock briefly just for reading
                let lock = FileLock::acquire(&store_path).await?;
                let store = TaskStore::load(&store_path).await?;
                let _ = lock.release().await;

                let mut filtered_tasks: Vec<_> = store
                    .tasks
                    .values()
                    .filter(|t| {
                        if !show_completed && t.status == TaskStatus::Completed {
                            return false;
                        }
                        if let Some(ref s) = status
                            && let Ok(target_status) = parse_status(s)
                            && t.status != target_status
                        {
                            return false;
                        }
                        if let Some(ref p) = priority
                            && let Ok(target_priority) = parse_priority(p)
                            && t.priority != target_priority
                        {
                            return false;
                        }
                        true
                    })
                    .collect();

                if filtered_tasks.is_empty() {
                    return Ok(ToolResult::success("No tasks found".to_string()));
                }

                // Sort by priority (Critical > High > Medium > Low) then by created_at
                filtered_tasks.sort_by(|a, b| {
                    let priority_order = |p: &TaskPriority| match p {
                        TaskPriority::Critical => 0,
                        TaskPriority::High => 1,
                        TaskPriority::Medium => 2,
                        TaskPriority::Low => 3,
                    };
                    priority_order(&a.priority)
                        .cmp(&priority_order(&b.priority))
                        .then_with(|| a.created_at.cmp(&b.created_at))
                });

                let mut output = format!("Found {} tasks:\n\n", filtered_tasks.len());
                for task in filtered_tasks {
                    output.push_str(&format!(
                        "[{}] {:?} | {:?}\n",
                        &task.id[..8],
                        task.status,
                        task.priority
                    ));
                    output.push_str(&format!("    {}\n", task.description));
                    if !task.tags.is_empty() {
                        output.push_str(&format!("    Tags: {}\n", task.tags.join(", ")));
                    }
                    if !task.dependencies.is_empty() {
                        output.push_str(&format!(
                            "    Dependencies: {}\n",
                            task.dependencies
                                .iter()
                                .map(|id| &id[..8])
                                .collect::<Vec<_>>()
                                .join(", ")
                        ));
                    }
                    if let Some(reason) = &task.blocked_reason {
                        output.push_str(&format!("    Blocked: {}\n", reason));
                    }
                    output.push('\n');
                }

                output
            }

            TaskOperation::Get { task_id } => {
                // Read-only: acquire lock briefly just for reading
                let lock = FileLock::acquire(&store_path).await?;
                let store = TaskStore::load(&store_path).await?;
                let _ = lock.release().await;

                let task = store.tasks.get(&task_id).ok_or_else(|| {
                    ToolError::InvalidInput(format!("Task not found: {}", task_id))
                })?;

                let mut output = format!("Task: {}\n", task.id);
                output.push_str(&format!("Description: {}\n", task.description));
                output.push_str(&format!("Status: {:?}\n", task.status));
                output.push_str(&format!("Priority: {:?}\n", task.priority));
                output.push_str(&format!(
                    "Created: {}\n",
                    task.created_at.format("%Y-%m-%d %H:%M:%S")
                ));
                output.push_str(&format!(
                    "Updated: {}\n",
                    task.updated_at.format("%Y-%m-%d %H:%M:%S")
                ));

                if let Some(completed) = task.completed_at {
                    output.push_str(&format!(
                        "Completed: {}\n",
                        completed.format("%Y-%m-%d %H:%M:%S")
                    ));
                }

                if !task.tags.is_empty() {
                    output.push_str(&format!("Tags: {}\n", task.tags.join(", ")));
                }

                if !task.dependencies.is_empty() {
                    output.push_str(&format!("Dependencies: {}\n", task.dependencies.join(", ")));
                }

                if let Some(reason) = &task.blocked_reason {
                    output.push_str(&format!("Blocked Reason: {}\n", reason));
                }

                output
            }

            // Write operations use atomic locking
            TaskOperation::Create {
                description,
                priority,
                tags,
                dependencies,
            } => {
                TaskStore::with_lock(&store_path, |store| {
                    let task_priority = if let Some(p) = priority {
                        parse_priority(&p)?
                    } else {
                        TaskPriority::Medium
                    };

                    // Check dependencies exist
                    for dep_id in &dependencies {
                        if !store.tasks.contains_key(dep_id) {
                            return Err(ToolError::InvalidInput(format!(
                                "Dependency task not found: {}",
                                dep_id
                            )));
                        }
                    }

                    let task_id = Uuid::new_v4().to_string();
                    let task = Task {
                        id: task_id.clone(),
                        description: description.clone(),
                        status: TaskStatus::Pending,
                        priority: task_priority,
                        created_at: Utc::now(),
                        updated_at: Utc::now(),
                        completed_at: None,
                        blocked_reason: None,
                        dependencies,
                        tags,
                    };

                    store.tasks.insert(task_id.clone(), task);

                    Ok(format!(
                        "Created task {}\nDescription: {}\nStatus: pending",
                        task_id, description
                    ))
                })
                .await?
            }

            TaskOperation::Update {
                task_id,
                status,
                description,
                priority,
                blocked_reason,
            } => {
                TaskStore::with_lock(&store_path, |store| {
                    // Check if task exists first
                    if !store.tasks.contains_key(&task_id) {
                        return Err(ToolError::InvalidInput(format!(
                            "Task not found: {}",
                            task_id
                        )));
                    }

                    let mut updates = Vec::new();

                    // Check dependencies before updating status
                    if let Some(ref new_status) = status {
                        let parsed_status = parse_status(new_status)?;

                        // Check dependencies before moving to in_progress or completed
                        if matches!(
                            parsed_status,
                            TaskStatus::InProgress | TaskStatus::Completed
                        ) {
                            let task_deps = store
                                .tasks
                                .get(&task_id)
                                .ok_or_else(|| {
                                    ToolError::Internal(format!(
                                        "Task {} not found after check",
                                        task_id
                                    ))
                                })?
                                .dependencies
                                .clone();
                            for dep_id in &task_deps {
                                if let Some(dep_task) = store.tasks.get(dep_id)
                                    && dep_task.status != TaskStatus::Completed
                                {
                                    return Err(ToolError::InvalidInput(format!(
                                        "Cannot update task: dependency {} is not completed",
                                        dep_id
                                    )));
                                }
                            }
                        }
                    }

                    // Now get mutable reference and update all fields
                    let task = store.tasks.get_mut(&task_id).ok_or_else(|| {
                        ToolError::Internal(format!("Task {} not found after check", task_id))
                    })?;

                    if let Some(new_status) = status {
                        let parsed_status = parse_status(&new_status)?;
                        task.status = parsed_status.clone();
                        updates.push(format!("status: {:?}", parsed_status));

                        if parsed_status == TaskStatus::Completed {
                            task.completed_at = Some(Utc::now());
                        }
                    }

                    if let Some(new_desc) = description {
                        task.description = new_desc.clone();
                        updates.push(format!("description: {}", new_desc));
                    }

                    if let Some(new_priority) = priority {
                        task.priority = parse_priority(&new_priority)?;
                        updates.push(format!("priority: {}", new_priority));
                    }

                    if let Some(reason) = blocked_reason {
                        task.blocked_reason = Some(reason.clone());
                        updates.push(format!("blocked_reason: {}", reason));
                    }

                    task.updated_at = Utc::now();

                    Ok(format!(
                        "Updated task {}\nChanges: {}",
                        task_id,
                        updates.join(", ")
                    ))
                })
                .await?
            }

            TaskOperation::Delete { task_id } => {
                TaskStore::with_lock(&store_path, |store| {
                    // Check if any other tasks depend on this task
                    let dependents: Vec<String> = store
                        .tasks
                        .values()
                        .filter(|t| t.dependencies.contains(&task_id))
                        .map(|t| t.id.clone())
                        .collect();

                    if !dependents.is_empty() {
                        return Err(ToolError::InvalidInput(format!(
                            "Cannot delete task: {} other tasks depend on it: {}",
                            dependents.len(),
                            dependents
                                .iter()
                                .map(|id| &id[..8])
                                .collect::<Vec<_>>()
                                .join(", ")
                        )));
                    }

                    let task = store.tasks.remove(&task_id).ok_or_else(|| {
                        ToolError::InvalidInput(format!("Task not found: {}", task_id))
                    })?;

                    Ok(format!(
                        "Deleted task {}\nDescription: {}",
                        task_id, task.description
                    ))
                })
                .await?
            }

            TaskOperation::ClearCompleted => {
                TaskStore::with_lock(&store_path, |store| {
                    let completed_count = store
                        .tasks
                        .iter()
                        .filter(|(_, t)| t.status == TaskStatus::Completed)
                        .count();

                    store.tasks.retain(|_, t| t.status != TaskStatus::Completed);

                    Ok(format!("Cleared {} completed tasks", completed_count))
                })
                .await?
            }
        };

        Ok(ToolResult::success(result))
    }
}