astrid-runtime 0.1.0

Agent runtime with sessions, context management, and orchestration for Astrid
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Subagent pool management.
//!
//! Manages lifecycle (spawn, cancel, depth/concurrency enforcement) for sub-agents.

use crate::error::{RuntimeError, RuntimeResult};
use chrono::{DateTime, Utc};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Notify, OwnedSemaphorePermit, RwLock, Semaphore};
use tokio_util::sync::CancellationToken;
use uuid::Uuid;

/// Unique identifier for a subagent instance.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct SubAgentId(String);

impl SubAgentId {
    /// Create a new random subagent ID.
    #[must_use]
    pub fn new() -> Self {
        Self(Uuid::new_v4().to_string())
    }
}

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

impl std::fmt::Display for SubAgentId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Status of a subagent instance.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SubAgentStatus {
    /// Subagent is initializing.
    Initializing,
    /// Subagent is running.
    Running,
    /// Subagent completed successfully.
    Completed,
    /// Subagent failed.
    Failed,
    /// Subagent was cancelled.
    Cancelled,
    /// Subagent timed out.
    TimedOut,
}

impl SubAgentStatus {
    /// Whether this status is terminal (done).
    fn is_terminal(self) -> bool {
        matches!(
            self,
            Self::Completed | Self::Failed | Self::Cancelled | Self::TimedOut
        )
    }
}

impl std::fmt::Display for SubAgentStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Initializing => write!(f, "initializing"),
            Self::Running => write!(f, "running"),
            Self::Completed => write!(f, "completed"),
            Self::Failed => write!(f, "failed"),
            Self::Cancelled => write!(f, "cancelled"),
            Self::TimedOut => write!(f, "timed_out"),
        }
    }
}

/// Handle to a running subagent.
#[derive(Debug)]
pub struct SubAgentHandle {
    /// Subagent ID.
    pub id: SubAgentId,

    /// Parent agent ID (if nested).
    pub parent_id: Option<SubAgentId>,

    /// Task description.
    pub task: String,

    /// Current depth (0 for first-level subagents).
    pub depth: usize,

    /// Current status (async access).
    status: Arc<RwLock<SubAgentStatus>>,

    /// Final status snapshot (sync access, set when entering terminal state).
    final_status: Arc<std::sync::Mutex<Option<SubAgentStatus>>>,

    /// When the subagent started.
    pub started_at: DateTime<Utc>,

    /// When the subagent completed (if done).
    completed_at: Arc<RwLock<Option<DateTime<Utc>>>>,

    /// Result (if completed).
    result: Arc<RwLock<Option<String>>>,

    /// Error message (if failed).
    error: Arc<RwLock<Option<String>>>,

    /// Semaphore permit — explicitly released when the handle leaves the active pool.
    permit: std::sync::Mutex<Option<OwnedSemaphorePermit>>,
}

impl SubAgentHandle {
    /// Create a new subagent handle.
    #[must_use]
    pub fn new(
        task: impl Into<String>,
        parent_id: Option<SubAgentId>,
        depth: usize,
        permit: Option<OwnedSemaphorePermit>,
    ) -> Self {
        Self {
            id: SubAgentId::new(),
            parent_id,
            task: task.into(),
            depth,
            status: Arc::new(RwLock::new(SubAgentStatus::Initializing)),
            final_status: Arc::new(std::sync::Mutex::new(None)),
            started_at: Utc::now(),
            completed_at: Arc::new(RwLock::new(None)),
            result: Arc::new(RwLock::new(None)),
            error: Arc::new(RwLock::new(None)),
            permit: std::sync::Mutex::new(permit),
        }
    }

    /// Get current status.
    pub async fn status(&self) -> SubAgentStatus {
        *self.status.read().await
    }

    /// Get final status synchronously (only set when terminal).
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub fn final_status(&self) -> Option<SubAgentStatus> {
        *self
            .final_status
            .lock()
            .expect("final_status mutex poisoned")
    }

    /// Set status.
    ///
    /// # Panics
    ///
    /// Panics if the internal mutex is poisoned.
    pub async fn set_status(&self, status: SubAgentStatus) {
        *self.status.write().await = status;
        if status.is_terminal() {
            *self.completed_at.write().await = Some(Utc::now());
            *self
                .final_status
                .lock()
                .expect("final_status mutex poisoned") = Some(status);
        }
    }

    /// Mark as running.
    pub async fn mark_running(&self) {
        self.set_status(SubAgentStatus::Running).await;
    }

    /// Mark as completed with result.
    pub async fn complete(&self, result: impl Into<String>) {
        *self.result.write().await = Some(result.into());
        self.set_status(SubAgentStatus::Completed).await;
    }

    /// Mark as failed with error.
    pub async fn fail(&self, error: impl Into<String>) {
        *self.error.write().await = Some(error.into());
        self.set_status(SubAgentStatus::Failed).await;
    }

    /// Mark as cancelled.
    pub async fn cancel(&self) {
        self.set_status(SubAgentStatus::Cancelled).await;
    }

    /// Mark as timed out.
    pub async fn timeout(&self) {
        self.set_status(SubAgentStatus::TimedOut).await;
    }

    /// Get result (if completed).
    pub async fn result(&self) -> Option<String> {
        self.result.read().await.clone()
    }

    /// Get error (if failed).
    pub async fn error(&self) -> Option<String> {
        self.error.read().await.clone()
    }

    /// Get completion time (if done).
    pub async fn completed_at(&self) -> Option<DateTime<Utc>> {
        *self.completed_at.read().await
    }

    /// Get duration (if completed).
    #[allow(clippy::arithmetic_side_effects)] // completed_at is always >= started_at
    pub async fn duration(&self) -> Option<chrono::Duration> {
        self.completed_at()
            .await
            .map(|completed| completed - self.started_at)
    }

    /// Check if done (completed, failed, cancelled, or timed out).
    pub async fn is_done(&self) -> bool {
        self.status().await.is_terminal()
    }

    /// Release the semaphore permit (called when moving out of the active pool).
    fn release_permit(&self) {
        let _ = self.permit.lock().expect("permit mutex poisoned").take();
    }
}

/// Default maximum history size before FIFO eviction.
const DEFAULT_MAX_HISTORY: usize = 1000;

/// Pool for managing subagent instances.
#[derive(Debug)]
pub struct SubAgentPool {
    /// Maximum concurrent subagents.
    max_concurrent: usize,

    /// Maximum nesting depth.
    max_depth: usize,

    /// Maximum completed history entries before FIFO eviction.
    max_history: usize,

    /// Concurrency semaphore.
    semaphore: Arc<Semaphore>,

    /// Active subagents.
    active: Arc<RwLock<HashMap<SubAgentId, Arc<SubAgentHandle>>>>,

    /// Completed subagents (for history).
    completed: Arc<RwLock<Vec<Arc<SubAgentHandle>>>>,

    /// Notified when the active pool becomes empty.
    completion_notify: Arc<Notify>,

    /// Cooperative cancellation token for all sub-agents in this pool.
    cancellation_token: CancellationToken,
}

impl SubAgentPool {
    /// Create a new subagent pool with default history limit (1000 entries).
    #[must_use]
    pub fn new(max_concurrent: usize, max_depth: usize) -> Self {
        Self::with_max_history(max_concurrent, max_depth, DEFAULT_MAX_HISTORY)
    }

    /// Create a new subagent pool with explicit history limit.
    #[must_use]
    pub fn with_max_history(max_concurrent: usize, max_depth: usize, max_history: usize) -> Self {
        Self {
            max_concurrent,
            max_depth,
            max_history,
            semaphore: Arc::new(Semaphore::new(max_concurrent)),
            active: Arc::new(RwLock::new(HashMap::new())),
            completed: Arc::new(RwLock::new(Vec::new())),
            completion_notify: Arc::new(Notify::new()),
            cancellation_token: CancellationToken::new(),
        }
    }

    /// Spawn a new subagent.
    ///
    /// # Errors
    ///
    /// Returns an error if the maximum subagent depth is exceeded or concurrency limit is reached.
    pub async fn spawn(
        &self,
        task: impl Into<String>,
        parent_id: Option<SubAgentId>,
    ) -> RuntimeResult<Arc<SubAgentHandle>> {
        let depth = if parent_id.is_some() {
            // Find parent depth
            let active = self.active.read().await;
            if let Some(parent) = parent_id.as_ref().and_then(|id| active.get(id)) {
                parent.depth.checked_add(1).ok_or_else(|| {
                    RuntimeError::SubAgentError("subagent depth overflow".to_string())
                })?
            } else {
                1
            }
        } else {
            0
        };

        if depth >= self.max_depth {
            return Err(RuntimeError::SubAgentError(format!(
                "maximum subagent depth ({}) exceeded",
                self.max_depth
            )));
        }

        // Try to acquire semaphore permit
        let permit = self.semaphore.clone().try_acquire_owned().map_err(|_| {
            RuntimeError::SubAgentError("maximum concurrent subagents reached".into())
        })?;

        let handle = Arc::new(SubAgentHandle::new(task, parent_id, depth, Some(permit)));

        // Store in active map
        self.active
            .write()
            .await
            .insert(handle.id.clone(), handle.clone());

        Ok(handle)
    }

    /// Release a subagent from the active pool and move to history.
    ///
    /// Releases the semaphore permit so another sub-agent can be spawned.
    /// The handle's status should already be set (completed/failed/timed out)
    /// before calling this.
    pub async fn release(&self, id: &SubAgentId) {
        let mut active = self.active.write().await;
        if let Some(handle) = active.remove(id) {
            handle.release_permit();
            self.push_to_history(handle).await;
            if active.is_empty() {
                self.completion_notify.notify_waiters();
            }
        }
    }

    /// Stop a specific subagent: cancel it and move to history.
    pub async fn stop(&self, id: &SubAgentId) -> Option<Arc<SubAgentHandle>> {
        let mut active = self.active.write().await;
        if let Some(handle) = active.remove(id) {
            handle.cancel().await;
            handle.release_permit();
            self.push_to_history(handle.clone()).await;
            if active.is_empty() {
                self.completion_notify.notify_waiters();
            }
            Some(handle)
        } else {
            None
        }
    }

    /// Push a handle to history with FIFO eviction at capacity.
    async fn push_to_history(&self, handle: Arc<SubAgentHandle>) {
        let mut completed = self.completed.write().await;
        if completed.len() >= self.max_history {
            completed.remove(0);
        }
        completed.push(handle);
    }

    /// Get active subagent by ID.
    pub async fn get(&self, id: &SubAgentId) -> Option<Arc<SubAgentHandle>> {
        self.active.read().await.get(id).cloned()
    }

    /// List active subagents.
    pub async fn list_active(&self) -> Vec<Arc<SubAgentHandle>> {
        self.active.read().await.values().cloned().collect()
    }

    /// Get count of active subagents.
    pub async fn active_count(&self) -> usize {
        self.active.read().await.len()
    }

    /// Get available capacity.
    #[must_use]
    pub fn available_permits(&self) -> usize {
        self.semaphore.available_permits()
    }

    /// Check if a child can be spawned for the given parent without actually spawning.
    ///
    /// Returns `true` if both depth limit and concurrency permit are available.
    pub async fn can_spawn_child(&self, parent_id: &SubAgentId) -> bool {
        let active = self.active.read().await;
        let parent_depth = if let Some(parent) = active.get(parent_id) {
            parent.depth
        } else {
            return false; // parent not found
        };

        let Some(child_depth) = parent_depth.checked_add(1) else {
            return false;
        };
        if child_depth >= self.max_depth {
            return false;
        }

        self.semaphore.available_permits() > 0
    }

    /// Get the cancellation token for cooperative cancellation.
    ///
    /// Sub-agent executors should select on this token in their run loop.
    /// Cancelling this token signals all sub-agents to stop cooperatively.
    #[must_use]
    pub fn cancellation_token(&self) -> CancellationToken {
        self.cancellation_token.clone()
    }

    /// Cancel all active subagents and move them to history.
    ///
    /// Also cancels the cooperative cancellation token so in-flight sub-agents
    /// can observe the cancellation and stop gracefully.
    pub async fn cancel_all(&self) {
        // Signal cooperative cancellation to all sub-agents.
        self.cancellation_token.cancel();

        let mut active = self.active.write().await;
        let handles: Vec<Arc<SubAgentHandle>> = active.drain().map(|(_, h)| h).collect();
        drop(active);

        for handle in handles {
            handle.cancel().await;
            handle.release_permit();
            self.push_to_history(handle).await;
        }

        self.completion_notify.notify_waiters();
    }

    /// Wait until the active pool is empty.
    pub async fn wait_for_completion(&self) {
        loop {
            if self.active.read().await.is_empty() {
                return;
            }
            self.completion_notify.notified().await;
        }
    }

    /// Wait until the active pool is empty, or the timeout expires.
    ///
    /// Returns `true` if the pool drained before the timeout, `false` otherwise.
    pub async fn wait_for_completion_timeout(&self, timeout: Duration) -> bool {
        tokio::select! {
            () = self.wait_for_completion() => true,
            () = tokio::time::sleep(timeout) => {
                self.active.read().await.is_empty()
            }
        }
    }

    /// Get direct children of a parent subagent (from both active and completed).
    pub async fn get_children(&self, parent_id: &SubAgentId) -> Vec<Arc<SubAgentHandle>> {
        let mut children = Vec::new();

        let active = self.active.read().await;
        for handle in active.values() {
            if handle.parent_id.as_ref() == Some(parent_id) {
                children.push(handle.clone());
            }
        }
        drop(active);

        let completed = self.completed.read().await;
        for handle in &*completed {
            if handle.parent_id.as_ref() == Some(parent_id) {
                children.push(handle.clone());
            }
        }

        children
    }

    /// Get all descendants of a parent subagent (BFS, from both active and completed).
    pub async fn get_subtree(&self, parent_id: &SubAgentId) -> Vec<Arc<SubAgentHandle>> {
        // Collect all handles into a single list for BFS
        let active = self.active.read().await;
        let completed = self.completed.read().await;

        let all_handles: Vec<Arc<SubAgentHandle>> = active
            .values()
            .cloned()
            .chain(completed.iter().cloned())
            .collect();
        drop(active);
        drop(completed);

        let mut result = Vec::new();
        let mut queue = VecDeque::new();
        queue.push_back(parent_id.clone());

        while let Some(current_id) = queue.pop_front() {
            for handle in &all_handles {
                if handle.parent_id.as_ref() == Some(&current_id) {
                    result.push(handle.clone());
                    queue.push_back(handle.id.clone());
                }
            }
        }

        result
    }

    /// Cancel all active descendants of a parent subagent and move them to history.
    ///
    /// Returns the number of subagents cancelled.
    pub async fn cancel_subtree(&self, parent_id: &SubAgentId) -> usize {
        // First find all descendant IDs via BFS over the active pool
        let active = self.active.read().await;
        let mut to_cancel = Vec::new();
        let mut queue = VecDeque::new();
        queue.push_back(parent_id.clone());

        while let Some(current_id) = queue.pop_front() {
            for (id, handle) in active.iter() {
                if handle.parent_id.as_ref() == Some(&current_id) {
                    to_cancel.push(id.clone());
                    queue.push_back(id.clone());
                }
            }
        }
        drop(active);

        let mut cancelled = 0usize;
        for id in &to_cancel {
            if self.stop(id).await.is_some() {
                cancelled = cancelled.saturating_add(1);
            }
        }
        cancelled
    }

    /// Get completed subagents history.
    pub async fn history(&self) -> Vec<Arc<SubAgentHandle>> {
        self.completed.read().await.clone()
    }

    /// Clear completed history.
    pub async fn clear_history(&self) {
        self.completed.write().await.clear();
    }

    /// Get pool statistics.
    pub async fn stats(&self) -> SubAgentPoolStats {
        let active = self.active.read().await;
        let completed = self.completed.read().await;

        let (succeeded, failed, cancelled, timed_out) =
            completed
                .iter()
                .fold(
                    (0usize, 0usize, 0usize, 0usize),
                    |(s, f, c, t), h| match h.final_status() {
                        Some(SubAgentStatus::Completed) => (s.saturating_add(1), f, c, t),
                        Some(SubAgentStatus::Failed) => (s, f.saturating_add(1), c, t),
                        Some(SubAgentStatus::Cancelled) => (s, f, c.saturating_add(1), t),
                        Some(SubAgentStatus::TimedOut) => (s, f, c, t.saturating_add(1)),
                        _ => (s, f, c, t),
                    },
                );

        SubAgentPoolStats {
            max_concurrent: self.max_concurrent,
            max_depth: self.max_depth,
            active: active.len(),
            available: self.semaphore.available_permits(),
            total_completed: completed.len(),
            succeeded,
            failed,
            cancelled,
            timed_out,
        }
    }
}

/// Statistics for a subagent pool.
#[derive(Debug, Clone)]
pub struct SubAgentPoolStats {
    /// Maximum concurrent subagents.
    pub max_concurrent: usize,
    /// Maximum nesting depth.
    pub max_depth: usize,
    /// Currently active subagents.
    pub active: usize,
    /// Available permits.
    pub available: usize,
    /// Total completed subagents.
    pub total_completed: usize,
    /// Successfully completed.
    pub succeeded: usize,
    /// Failed subagents.
    pub failed: usize,
    /// Cancelled subagents.
    pub cancelled: usize,
    /// Timed out subagents.
    pub timed_out: usize,
}

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

    #[tokio::test]
    async fn test_subagent_lifecycle() {
        let handle = SubAgentHandle::new("test task", None, 0, None);

        assert_eq!(handle.status().await, SubAgentStatus::Initializing);
        assert!(!handle.is_done().await);

        handle.mark_running().await;
        assert_eq!(handle.status().await, SubAgentStatus::Running);

        handle.complete("success").await;
        assert_eq!(handle.status().await, SubAgentStatus::Completed);
        assert!(handle.is_done().await);
        assert_eq!(handle.result().await, Some("success".into()));
    }

    #[tokio::test]
    async fn test_subagent_failure() {
        let handle = SubAgentHandle::new("test task", None, 0, None);

        handle.mark_running().await;
        handle.fail("something went wrong").await;

        assert_eq!(handle.status().await, SubAgentStatus::Failed);
        assert!(handle.is_done().await);
        assert_eq!(handle.error().await, Some("something went wrong".into()));
    }

    #[tokio::test]
    async fn test_subagent_final_status() {
        let handle = SubAgentHandle::new("test task", None, 0, None);
        assert_eq!(handle.final_status(), None);

        handle.mark_running().await;
        assert_eq!(handle.final_status(), None);

        handle.complete("done").await;
        assert_eq!(handle.final_status(), Some(SubAgentStatus::Completed));
    }

    #[tokio::test]
    async fn test_pool_spawn() {
        let pool = SubAgentPool::new(5, 3);

        let handle = pool.spawn("task 1", None).await.unwrap();
        assert_eq!(pool.active_count().await, 1);

        pool.release(&handle.id).await;
        assert_eq!(pool.active_count().await, 0);
        assert_eq!(pool.history().await.len(), 1);
    }

    #[tokio::test]
    async fn test_pool_max_depth() {
        let pool = SubAgentPool::new(5, 2);

        // Depth 0
        let h1 = pool.spawn("task 1", None).await.unwrap();
        assert_eq!(h1.depth, 0);

        // Depth 1
        let h2 = pool.spawn("task 2", Some(h1.id.clone())).await.unwrap();
        assert_eq!(h2.depth, 1);

        // Depth 2 should fail (max_depth = 2 means 0 and 1 only)
        let result = pool.spawn("task 3", Some(h2.id.clone())).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_pool_cancel_all() {
        let pool = SubAgentPool::new(5, 3);

        let h1 = pool.spawn("task 1", None).await.unwrap();
        let h2 = pool.spawn("task 2", None).await.unwrap();

        pool.cancel_all().await;

        assert_eq!(h1.status().await, SubAgentStatus::Cancelled);
        assert_eq!(h2.status().await, SubAgentStatus::Cancelled);
        // Handles should be moved to history
        assert_eq!(pool.active_count().await, 0);
        assert_eq!(pool.history().await.len(), 2);
    }

    #[tokio::test]
    async fn test_semaphore_limits_concurrency() {
        let pool = SubAgentPool::new(2, 5);

        let _h1 = pool.spawn("task 1", None).await.unwrap();
        let _h2 = pool.spawn("task 2", None).await.unwrap();

        // Third spawn should fail — semaphore exhausted
        let result = pool.spawn("task 3", None).await;
        assert!(result.is_err());
        assert_eq!(pool.available_permits(), 0);
    }

    #[tokio::test]
    async fn test_permit_released_on_complete() {
        let pool = SubAgentPool::new(1, 5);

        let h1 = pool.spawn("task 1", None).await.unwrap();
        assert_eq!(pool.available_permits(), 0);

        // Completing moves handle to history, dropping the permit
        pool.release(&h1.id).await;
        assert_eq!(pool.available_permits(), 1);

        // Should be able to spawn again
        let _h2 = pool.spawn("task 2", None).await.unwrap();
        assert_eq!(pool.available_permits(), 0);
    }

    #[tokio::test]
    async fn test_stop_cancels_and_moves_to_history() {
        let pool = SubAgentPool::new(5, 3);

        let h = pool.spawn("task 1", None).await.unwrap();
        let id = h.id.clone();

        let stopped = pool.stop(&id).await;
        assert!(stopped.is_some());

        let handle = stopped.unwrap();
        assert_eq!(handle.status().await, SubAgentStatus::Cancelled);
        assert_eq!(pool.active_count().await, 0);
        assert_eq!(pool.history().await.len(), 1);
    }

    #[tokio::test]
    async fn test_stop_nonexistent_returns_none() {
        let pool = SubAgentPool::new(5, 3);
        let result = pool.stop(&SubAgentId::new()).await;
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_can_spawn_child_checks_depth_and_concurrency() {
        let pool = SubAgentPool::new(3, 2);

        // Depth 0
        let h1 = pool.spawn("task 1", None).await.unwrap();
        assert!(pool.can_spawn_child(&h1.id).await);

        // Depth 1 (max_depth=2, so depth 1 is allowed but children of depth 1 are not)
        let h2 = pool.spawn("task 2", Some(h1.id.clone())).await.unwrap();
        assert!(!pool.can_spawn_child(&h2.id).await); // depth 2 >= max_depth

        // Non-existent parent
        assert!(!pool.can_spawn_child(&SubAgentId::new()).await);
    }

    #[tokio::test]
    async fn test_can_spawn_child_checks_concurrency() {
        let pool = SubAgentPool::new(2, 5);

        let h1 = pool.spawn("task 1", None).await.unwrap();
        let _h2 = pool.spawn("task 2", None).await.unwrap();

        // No permits left
        assert!(!pool.can_spawn_child(&h1.id).await);
    }

    #[tokio::test]
    async fn test_wait_for_completion_returns_when_empty() {
        let pool = Arc::new(SubAgentPool::new(5, 3));

        // Empty pool — should return immediately
        pool.wait_for_completion().await;

        let h = pool.spawn("task 1", None).await.unwrap();
        let h_id = h.id.clone();
        let pool_clone = pool.clone();

        let waiter = tokio::spawn(async move {
            pool_clone.wait_for_completion().await;
        });

        // Give the waiter a moment to register
        tokio::time::sleep(Duration::from_millis(10)).await;
        assert!(!waiter.is_finished());

        pool.release(&h_id).await;

        // Waiter should finish now
        tokio::time::timeout(Duration::from_secs(1), waiter)
            .await
            .expect("waiter should complete")
            .expect("waiter should not panic");
    }

    #[tokio::test]
    async fn test_wait_for_completion_timeout_returns_false_on_expiry() {
        let pool = SubAgentPool::new(5, 3);

        let _h = pool.spawn("task 1", None).await.unwrap();

        let drained = pool
            .wait_for_completion_timeout(Duration::from_millis(50))
            .await;
        assert!(!drained);
    }

    #[tokio::test]
    async fn test_wait_for_completion_timeout_returns_true_on_drain() {
        let pool = Arc::new(SubAgentPool::new(5, 3));

        let h = pool.spawn("task 1", None).await.unwrap();
        let h_id = h.id.clone();
        let pool_clone = pool.clone();

        tokio::spawn(async move {
            tokio::time::sleep(Duration::from_millis(20)).await;
            pool_clone.release(&h_id).await;
        });

        let drained = pool
            .wait_for_completion_timeout(Duration::from_secs(2))
            .await;
        assert!(drained);
    }

    #[tokio::test]
    async fn test_get_children_returns_direct_children_only() {
        let pool = SubAgentPool::new(10, 5);

        let root = pool.spawn("root", None).await.unwrap();
        let child1 = pool.spawn("child1", Some(root.id.clone())).await.unwrap();
        let child2 = pool.spawn("child2", Some(root.id.clone())).await.unwrap();
        let _grandchild = pool
            .spawn("grandchild", Some(child1.id.clone()))
            .await
            .unwrap();

        let children = pool.get_children(&root.id).await;
        assert_eq!(children.len(), 2);

        let child_ids: Vec<_> = children.iter().map(|c| c.id.clone()).collect();
        assert!(child_ids.contains(&child1.id));
        assert!(child_ids.contains(&child2.id));
    }

    #[tokio::test]
    async fn test_get_children_includes_completed() {
        let pool = SubAgentPool::new(10, 5);

        let root = pool.spawn("root", None).await.unwrap();
        let child = pool.spawn("child", Some(root.id.clone())).await.unwrap();
        let child_id = child.id.clone();

        // Move child to completed
        pool.release(&child_id).await;

        let children = pool.get_children(&root.id).await;
        assert_eq!(children.len(), 1);
        assert_eq!(children[0].id, child_id);
    }

    #[tokio::test]
    async fn test_get_subtree_returns_all_descendants() {
        let pool = SubAgentPool::new(10, 5);

        let root = pool.spawn("root", None).await.unwrap();
        let child1 = pool.spawn("child1", Some(root.id.clone())).await.unwrap();
        let child2 = pool.spawn("child2", Some(root.id.clone())).await.unwrap();
        let grandchild1 = pool
            .spawn("grandchild1", Some(child1.id.clone()))
            .await
            .unwrap();
        let grandchild2 = pool
            .spawn("grandchild2", Some(child2.id.clone()))
            .await
            .unwrap();

        let subtree = pool.get_subtree(&root.id).await;
        assert_eq!(subtree.len(), 4);

        let ids: Vec<_> = subtree.iter().map(|h| h.id.clone()).collect();
        assert!(ids.contains(&child1.id));
        assert!(ids.contains(&child2.id));
        assert!(ids.contains(&grandchild1.id));
        assert!(ids.contains(&grandchild2.id));
        // Root itself should NOT be in the subtree
        assert!(!ids.contains(&root.id));
    }

    #[tokio::test]
    async fn test_cancel_subtree_cancels_only_descendants() {
        let pool = SubAgentPool::new(10, 5);

        let root = pool.spawn("root", None).await.unwrap();
        let child = pool.spawn("child", Some(root.id.clone())).await.unwrap();
        let grandchild = pool
            .spawn("grandchild", Some(child.id.clone()))
            .await
            .unwrap();
        let _sibling = pool.spawn("sibling", None).await.unwrap();

        let cancelled = pool.cancel_subtree(&root.id).await;
        assert_eq!(cancelled, 2); // child + grandchild

        // Root should still be active
        assert!(pool.get(&root.id).await.is_some());
        // Child and grandchild should be in history
        assert!(pool.get(&child.id).await.is_none());
        assert!(pool.get(&grandchild.id).await.is_none());
        // Sibling should still be active
        assert!(pool.get(&_sibling.id).await.is_some());
    }

    #[tokio::test]
    async fn test_stats_counts_are_accurate() {
        let pool = SubAgentPool::new(10, 5);

        let h1 = pool.spawn("task 1", None).await.unwrap();
        let h2 = pool.spawn("task 2", None).await.unwrap();
        let h3 = pool.spawn("task 3", None).await.unwrap();
        let h4 = pool.spawn("task 4", None).await.unwrap();

        // Complete one successfully
        h1.complete("result").await;
        pool.release(&h1.id).await;

        // Fail one
        h2.fail("error").await;
        pool.release(&h2.id).await;

        // Cancel one
        pool.stop(&h3.id).await;

        // Timeout one
        h4.timeout().await;
        pool.release(&h4.id).await;

        let stats = pool.stats().await;
        assert_eq!(stats.max_concurrent, 10);
        assert_eq!(stats.max_depth, 5);
        assert_eq!(stats.active, 0);
        assert_eq!(stats.total_completed, 4);
        assert_eq!(stats.succeeded, 1);
        assert_eq!(stats.failed, 1);
        assert_eq!(stats.cancelled, 1);
        assert_eq!(stats.timed_out, 1);
    }

    #[tokio::test]
    async fn test_stats_with_active() {
        let pool = SubAgentPool::new(5, 3);

        let _h1 = pool.spawn("task 1", None).await.unwrap();
        let h2 = pool.spawn("task 2", None).await.unwrap();
        h2.complete("done").await;
        pool.release(&h2.id).await;

        let stats = pool.stats().await;
        assert_eq!(stats.active, 1);
        assert_eq!(stats.total_completed, 1);
        assert_eq!(stats.succeeded, 1);
        assert_eq!(stats.available, 4);
    }

    #[tokio::test]
    async fn test_history_eviction_at_capacity() {
        let pool = SubAgentPool::with_max_history(10, 5, 3);

        // Spawn and release 5 subagents into a pool with max_history=3
        for i in 0..5 {
            let h = pool.spawn(format!("task {i}"), None).await.unwrap();
            h.complete(format!("result {i}")).await;
            pool.release(&h.id).await;
        }

        // Only the 3 most recent should be in history
        let history = pool.history().await;
        assert_eq!(history.len(), 3);
        // Oldest entry should be task 2 (tasks 0 and 1 were evicted)
        assert_eq!(history[0].task, "task 2");
        assert_eq!(history[1].task, "task 3");
        assert_eq!(history[2].task, "task 4");
    }
}