a3s-code-core 1.9.2

A3S Code Core - Embeddable AI agent library with tool execution
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
//! Task Manager - Centralized Task Lifecycle Management
//!
//! Provides centralized management for all tasks in a3s-code.
//! Handles spawning, tracking, and coordinating task execution.
//!
//! ## Example
//!
//! ```rust,ignore
//! use a3s_code_core::task::{TaskManager, Task, TaskType};
//!
//! let manager = TaskManager::new();
//!
//! // Spawn a task
//! let task = Task::agent("general", "/workspace", "Analyze this");
//! let task_id = manager.spawn(task);
//!
//! // Wait for completion
//! let result = manager.wait(task_id).await;
//! assert!(result.is_ok());
//!
//! // Subscribe to updates
//! let mut rx = manager.subscribe(task_id).unwrap();
//! while let Some(event) = rx.recv().await {
//!     println!("Task update: {:?}", event);
//! }
//! ```

use crate::task::{Task, TaskId};
use std::collections::{HashMap, VecDeque};
use std::sync::RwLock;
use tokio::sync::broadcast;

/// Task lifecycle events
#[derive(Debug, Clone)]
pub enum TaskEvent {
    /// Task was spawned
    Spawned {
        task_id: TaskId,
        parent_id: Option<TaskId>,
    },
    /// Task started execution
    Started(TaskId),
    /// Task progress update
    Progress { task_id: TaskId, message: String },
    /// Task completed
    Completed { task_id: TaskId, result: TaskResult },
    /// Task failed
    Failed { task_id: TaskId, error: String },
    /// Task was killed
    Killed(TaskId),
    /// Child task spawned
    ChildSpawned { parent_id: TaskId, child_id: TaskId },
}

/// Task execution result
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TaskResult {
    /// Task ID
    pub task_id: TaskId,
    /// Whether the task completed successfully
    pub success: bool,
    /// Output value (tool result, agent response, etc.)
    pub output: Option<serde_json::Value>,
    /// Execution duration in milliseconds
    pub duration_ms: u64,
}

/// Task manager errors
#[derive(Debug, thiserror::Error)]
pub enum TaskManagerError {
    #[error("Task not found: {0}")]
    TaskNotFound(TaskId),

    #[error("Task already completed: {0}")]
    TaskAlreadyCompleted(TaskId),

    #[error("Task {0} is still running")]
    TaskStillRunning(TaskId),

    #[error("Manager is shutdown")]
    Shutdown,

    #[error("Send error: {0}")]
    SendError(String),
}

/// Centralized task lifecycle manager
pub struct TaskManager {
    /// All tasks indexed by ID
    tasks: RwLock<HashMap<TaskId, Task>>,
    /// Task results (kept after completion for retrieval)
    results: RwLock<HashMap<TaskId, TaskResult>>,
    /// Event subscribers (one per task)
    subscribers: RwLock<HashMap<TaskId, broadcast::Sender<TaskEvent>>>,
    /// Global event broadcast
    global_events: broadcast::Sender<TaskEvent>,
    /// Task queue for pending tasks
    pending_queue: RwLock<VecDeque<TaskId>>,
    /// Whether manager is shutdown
    shutdown: RwLock<bool>,
}

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

impl TaskManager {
    /// Create a new task manager
    pub fn new() -> Self {
        let (global_events, _) = broadcast::channel(1024);
        Self {
            tasks: RwLock::new(HashMap::new()),
            results: RwLock::new(HashMap::new()),
            subscribers: RwLock::new(HashMap::new()),
            global_events,
            pending_queue: RwLock::new(VecDeque::new()),
            shutdown: RwLock::new(false),
        }
    }

    /// Spawn a new task
    ///
    /// Returns the task ID.
    pub fn spawn(&self, task: Task) -> TaskId {
        if *self.shutdown.read().unwrap() {
            // Return a dummy ID, caller should check
            return task.id;
        }

        let task_id = task.id;
        let parent_id = task.parent_id;

        // Create subscription channel for this task
        let (tx, _) = broadcast::channel(64);
        self.subscribers.write().unwrap().insert(task_id, tx);

        // Add to tasks
        self.tasks.write().unwrap().insert(task_id, task);

        // Add to pending queue
        self.pending_queue.write().unwrap().push_back(task_id);

        // Emit spawn event
        let _ = self
            .global_events
            .send(TaskEvent::Spawned { task_id, parent_id });

        task_id
    }

    /// Start executing a task
    pub fn start(&self, task_id: TaskId) -> Result<(), TaskManagerError> {
        let mut tasks = self.tasks.write().unwrap();

        let task = tasks
            .get_mut(&task_id)
            .ok_or(TaskManagerError::TaskNotFound(task_id))?;

        if task.status.is_terminal() {
            return Err(TaskManagerError::TaskAlreadyCompleted(task_id));
        }

        task.start();

        // Notify subscribers
        if let Some(tx) = self.subscribers.read().unwrap().get(&task_id) {
            let _ = tx.send(TaskEvent::Started(task_id));
        }

        Ok(())
    }

    /// Update task progress
    pub fn progress(
        &self,
        task_id: TaskId,
        message: impl Into<String>,
    ) -> Result<(), TaskManagerError> {
        let tasks = self.tasks.read().unwrap();

        if !tasks.contains_key(&task_id) {
            return Err(TaskManagerError::TaskNotFound(task_id));
        }

        // Notify subscribers
        if let Some(tx) = self.subscribers.read().unwrap().get(&task_id) {
            let _ = tx.send(TaskEvent::Progress {
                task_id,
                message: message.into(),
            });
        }

        Ok(())
    }

    /// Complete a task with result
    pub fn complete(
        &self,
        task_id: TaskId,
        output: Option<serde_json::Value>,
    ) -> Result<(), TaskManagerError> {
        let mut tasks = self.tasks.write().unwrap();

        let task = tasks
            .get_mut(&task_id)
            .ok_or(TaskManagerError::TaskNotFound(task_id))?;

        if task.status.is_terminal() {
            return Err(TaskManagerError::TaskAlreadyCompleted(task_id));
        }

        let duration_ms = task.duration_ms().unwrap_or(0);
        task.complete();

        // Store result
        let result = TaskResult {
            task_id,
            success: true,
            output,
            duration_ms,
        };
        self.results
            .write()
            .unwrap()
            .insert(task_id, result.clone());

        // Notify subscribers
        if let Some(tx) = self.subscribers.read().unwrap().get(&task_id) {
            let _ = tx.send(TaskEvent::Completed { task_id, result });
        }

        Ok(())
    }

    /// Fail a task with error
    pub fn fail(&self, task_id: TaskId, error: impl Into<String>) -> Result<(), TaskManagerError> {
        let mut tasks = self.tasks.write().unwrap();

        let task = tasks
            .get_mut(&task_id)
            .ok_or(TaskManagerError::TaskNotFound(task_id))?;

        if task.status.is_terminal() {
            return Err(TaskManagerError::TaskAlreadyCompleted(task_id));
        }

        let error_msg = error.into();
        task.fail(&error_msg);

        // Store error result so wait() can retrieve it even if called after fail
        let result = TaskResult {
            task_id,
            success: false,
            output: Some(serde_json::json!({ "error": error_msg.clone() })),
            duration_ms: task.duration_ms().unwrap_or(0),
        };
        self.results.write().unwrap().insert(task_id, result);

        // Notify subscribers
        if let Some(tx) = self.subscribers.read().unwrap().get(&task_id) {
            let _ = tx.send(TaskEvent::Failed {
                task_id,
                error: error_msg,
            });
        }

        Ok(())
    }

    /// Kill a task
    pub fn kill(&self, task_id: TaskId) -> Result<(), TaskManagerError> {
        let mut tasks = self.tasks.write().unwrap();

        let task = tasks
            .get_mut(&task_id)
            .ok_or(TaskManagerError::TaskNotFound(task_id))?;

        if task.status.is_terminal() {
            return Err(TaskManagerError::TaskAlreadyCompleted(task_id));
        }

        task.kill();

        // Store killed result so wait() can retrieve it even if called after kill
        let result = TaskResult {
            task_id,
            success: false,
            output: Some(serde_json::json!({ "error": "Task was killed" })),
            duration_ms: task.duration_ms().unwrap_or(0),
        };
        self.results.write().unwrap().insert(task_id, result);

        // Notify subscribers
        if let Some(tx) = self.subscribers.read().unwrap().get(&task_id) {
            let _ = tx.send(TaskEvent::Killed(task_id));
        }

        Ok(())
    }

    /// Get task by ID
    pub fn get(&self, task_id: TaskId) -> Option<Task> {
        self.tasks.read().unwrap().get(&task_id).cloned()
    }

    /// Get task result
    pub fn get_result(&self, task_id: TaskId) -> Option<TaskResult> {
        self.results.read().unwrap().get(&task_id).cloned()
    }

    /// Check if task is terminal
    pub fn is_terminal(&self, task_id: TaskId) -> bool {
        self.tasks
            .read()
            .unwrap()
            .get(&task_id)
            .map(|t| t.is_terminal())
            .unwrap_or(true)
    }

    /// Subscribe to task events
    ///
    /// Returns a receiver that will receive all events for the task.
    /// The receiver is closed when the task completes.
    pub fn subscribe(&self, task_id: TaskId) -> Option<broadcast::Receiver<TaskEvent>> {
        self.subscribers
            .read()
            .unwrap()
            .get(&task_id)
            .map(|tx| tx.subscribe())
    }

    /// Subscribe to all task events (global)
    pub fn subscribe_all(&self) -> broadcast::Receiver<TaskEvent> {
        self.global_events.subscribe()
    }

    /// Wait for a task to complete
    ///
    /// Returns the task result when the task completes.
    pub async fn wait(&self, task_id: TaskId) -> Result<TaskResult, TaskManagerError> {
        // First check if already completed
        if let Some(result) = self.get_result(task_id) {
            if !result.success {
                let error = result
                    .output
                    .as_ref()
                    .and_then(|v| v.get("error"))
                    .and_then(|v| v.as_str())
                    .unwrap_or("Unknown error")
                    .to_string();
                return Err(TaskManagerError::SendError(error));
            }
            return Ok(result);
        }

        // Subscribe to events
        let mut rx = self
            .subscribe(task_id)
            .ok_or(TaskManagerError::TaskNotFound(task_id))?;

        // Wait for completion
        while let Ok(event) = rx.recv().await {
            match event {
                TaskEvent::Completed {
                    task_id: id,
                    result,
                } if id == task_id => {
                    return Ok(result);
                }
                TaskEvent::Failed { task_id: id, error } if id == task_id => {
                    return Err(TaskManagerError::SendError(error));
                }
                TaskEvent::Killed(id) if id == task_id => {
                    return Err(TaskManagerError::SendError("Task was killed".to_string()));
                }
                _ => {}
            }
        }

        Err(TaskManagerError::Shutdown)
    }

    /// Add a child task to a parent
    pub fn add_child(&self, parent_id: TaskId, child_id: TaskId) -> Result<(), TaskManagerError> {
        let mut tasks = self.tasks.write().unwrap();

        let parent = tasks
            .get_mut(&parent_id)
            .ok_or(TaskManagerError::TaskNotFound(parent_id))?;
        parent.add_child(child_id);

        // Notify about child spawn
        if let Some(tx) = self.subscribers.read().unwrap().get(&parent_id) {
            let _ = tx.send(TaskEvent::ChildSpawned {
                parent_id,
                child_id,
            });
        }

        Ok(())
    }

    /// Spawn a child task with the given parent ID, setting up parent-child relationship.
    ///
    /// This is a convenience method that combines spawn + add_child in one call.
    pub fn spawn_child(&self, parent_id: TaskId, task: Task) -> Result<TaskId, TaskManagerError> {
        // Verify parent exists
        if !self.tasks.read().unwrap().contains_key(&parent_id) {
            return Err(TaskManagerError::TaskNotFound(parent_id));
        }

        let child_id = self.spawn(task);
        self.add_child(parent_id, child_id)?;
        Ok(child_id)
    }

    /// Wait for all child tasks of a parent to complete.
    ///
    /// Returns a list of results for each child task.
    pub async fn wait_children(
        &self,
        parent_id: TaskId,
    ) -> Result<Vec<TaskResult>, TaskManagerError> {
        let children: Vec<TaskId> = {
            let tasks = self.tasks.read().unwrap();
            let parent = tasks
                .get(&parent_id)
                .ok_or(TaskManagerError::TaskNotFound(parent_id))?;
            parent.child_ids.clone()
        };

        let mut results = Vec::new();
        for child_id in children {
            match self.wait(child_id).await {
                Ok(result) => results.push(result),
                Err(TaskManagerError::TaskStillRunning(_)) => {
                    // Child still running, wait for it
                    let result = self.wait(child_id).await?;
                    results.push(result);
                }
                Err(e) => return Err(e),
            }
        }

        Ok(results)
    }

    /// Get all child task IDs for a parent task.
    pub fn get_children(&self, parent_id: TaskId) -> Result<Vec<TaskId>, TaskManagerError> {
        let tasks = self.tasks.read().unwrap();
        let parent = tasks
            .get(&parent_id)
            .ok_or(TaskManagerError::TaskNotFound(parent_id))?;
        Ok(parent.child_ids.clone())
    }

    /// Check if all children of a parent task are complete.
    pub fn all_children_complete(&self, parent_id: TaskId) -> bool {
        if let Some(children) = self
            .tasks
            .read()
            .unwrap()
            .get(&parent_id)
            .map(|t| &t.child_ids)
        {
            children.iter().all(|id| self.is_terminal(*id))
        } else {
            true
        }
    }

    /// Get pending task IDs (FIFO)
    pub fn pending_tasks(&self) -> Vec<TaskId> {
        self.pending_queue.read().unwrap().iter().copied().collect()
    }

    /// Pop the next pending task
    pub fn pop_pending(&self) -> Option<TaskId> {
        self.pending_queue.write().unwrap().pop_front()
    }

    /// Shutdown the manager
    pub fn shutdown(&self) {
        *self.shutdown.write().unwrap() = true;
        self.tasks.write().unwrap().clear();
        self.results.write().unwrap().clear();
        self.subscribers.write().unwrap().clear();
        self.pending_queue.write().unwrap().clear();
    }

    /// Get all tasks
    pub fn all_tasks(&self) -> HashMap<TaskId, Task> {
        self.tasks.read().unwrap().clone()
    }
}

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

    #[tokio::test]
    async fn test_task_manager_spawn_and_wait() {
        let manager = TaskManager::new();

        let task = Task::tool("read", json!({"file_path": "test.txt"}));
        let task_id = manager.spawn(task);

        manager.start(task_id).unwrap();

        let result_json = json!({"success": true, "output": "file content"});
        manager
            .complete(task_id, Some(result_json.clone()))
            .unwrap();

        let result = manager.wait(task_id).await.unwrap();
        assert_eq!(result.output, Some(result_json));
        assert!(manager.is_terminal(task_id));
    }

    #[tokio::test]
    async fn test_task_manager_fail() {
        let manager = TaskManager::new();

        let task = Task::tool("read", json!({"file_path": "nonexistent.txt"}));
        let task_id = manager.spawn(task);

        manager.start(task_id).unwrap();
        manager.fail(task_id, "File not found").unwrap();

        let result = manager.wait(task_id).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_task_manager_kill() {
        let manager = TaskManager::new();

        let task = Task::tool("bash", json!({"command": "sleep 100"}));
        let task_id = manager.spawn(task);

        manager.start(task_id).unwrap();
        manager.kill(task_id).unwrap();

        assert!(manager.is_terminal(task_id));
        assert_eq!(manager.get(task_id).unwrap().status, TaskStatus::Killed);
    }
}