mixtape-tools 0.4.0

Ready-to-use tool implementations for the mixtape agent framework
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
use mixtape_core::ToolError;
use regex::Regex;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin, Command};
use tokio::sync::Mutex;

lazy_static::lazy_static! {
    /// Patterns that indicate a process is waiting for input
    /// Matches common shell prompts, REPL prompts, and input indicators
    static ref PROMPT_PATTERNS: Regex = Regex::new(concat!(
        r"(?:",
        r">>>\s*$|",           // Python REPL
        r">\s*$|",             // Generic prompt, Node REPL
        r"\$\s*$|",            // Shell prompt
        r"#\s*$|",             // Root shell prompt
        r"%\s*$|",             // Zsh prompt
        r":\s*$|",             // Generic input prompt (e.g., "Enter name:")
        r"\?\s*$|",            // Question prompt
        r"password:\s*$|",     // Password prompt (case insensitive handled below)
        r"Password:\s*$|",
        r"\(yes/no\)\s*$|",    // Confirmation prompt
        r"\[Y/n\]\s*$|",       // Debian-style confirmation
        r"\[y/N\]\s*$|",
        r"irb.*>\s*$|",        // Ruby IRB
        r"pry.*>\s*$|",        // Ruby Pry
        r"iex.*>\s*$|",        // Elixir IEx
        r"scala>\s*$|",        // Scala REPL
        r"ghci>\s*$|",         // Haskell GHCi
        r"sqlite>\s*$|",       // SQLite
        r"mysql>\s*$|",        // MySQL
        r"postgres.*>\s*$|",   // PostgreSQL
        r"redis.*>\s*$",       // Redis CLI
        r")"
    )).expect("Invalid prompt regex");
}

/// State of a process session
#[derive(Debug, Clone, PartialEq)]
pub enum ProcessState {
    /// Process is running and producing output
    Running,
    /// Process appears to be waiting for input (prompt detected)
    WaitingForInput,
    /// Process has exited
    Completed { exit_code: Option<i32> },
    /// Process exceeded its timeout
    TimedOut,
}

/// A managed process session
pub struct Session {
    pub pid: u32,
    pub command: String,
    pub process: Child,
    pub stdin: Option<ChildStdin>,
    pub output_buffer: Arc<Mutex<Vec<String>>>,
    pub state: ProcessState,
    pub created_at: Instant,
    pub timeout_ms: Option<u64>,
}

impl Session {
    pub fn new(
        pid: u32,
        command: String,
        process: Child,
        stdin: Option<ChildStdin>,
        timeout_ms: Option<u64>,
    ) -> Self {
        Self {
            pid,
            command,
            process,
            stdin,
            output_buffer: Arc::new(Mutex::new(Vec::new())),
            state: ProcessState::Running,
            created_at: Instant::now(),
            timeout_ms,
        }
    }

    pub fn elapsed_ms(&self) -> u64 {
        self.created_at.elapsed().as_millis() as u64
    }

    pub fn is_timed_out(&self) -> bool {
        if let Some(timeout) = self.timeout_ms {
            self.elapsed_ms() > timeout
        } else {
            false
        }
    }

    /// Check if the last line of output looks like a prompt waiting for input
    pub async fn is_waiting_for_input(&self) -> bool {
        let buffer = self.output_buffer.lock().await;
        if let Some(last_line) = buffer.last() {
            // Strip ANSI escape codes for cleaner matching
            let clean_line = strip_ansi_codes(last_line);
            PROMPT_PATTERNS.is_match(&clean_line)
        } else {
            false
        }
    }

    pub async fn check_status(&mut self) -> ProcessState {
        // Check if process has exited
        if let Ok(Some(status)) = self.process.try_wait() {
            self.state = ProcessState::Completed {
                exit_code: status.code(),
            };
            return self.state.clone();
        }

        // Check for timeout
        if self.is_timed_out() {
            self.state = ProcessState::TimedOut;
            return self.state.clone();
        }

        // Check if waiting for input (prompt detected)
        if self.is_waiting_for_input().await {
            self.state = ProcessState::WaitingForInput;
            return self.state.clone();
        }

        self.state = ProcessState::Running;
        self.state.clone()
    }
}

/// Strip ANSI escape codes from a string for cleaner pattern matching
pub(crate) fn strip_ansi_codes(s: &str) -> String {
    // Simple regex to strip ANSI escape sequences
    let ansi_regex = Regex::new(r"\x1b\[[0-9;]*[a-zA-Z]").expect("Invalid ANSI regex");
    ansi_regex.replace_all(s, "").to_string()
}

/// Manager for process sessions
pub struct SessionManager {
    sessions: Arc<Mutex<HashMap<u32, Session>>>,
    next_pid: Arc<Mutex<u32>>,
}

impl SessionManager {
    pub fn new() -> Self {
        Self {
            sessions: Arc::new(Mutex::new(HashMap::new())),
            next_pid: Arc::new(Mutex::new(10000)),
        }
    }

    pub async fn create_session(
        &self,
        command: String,
        shell: Option<String>,
        timeout_ms: Option<u64>,
    ) -> Result<u32, ToolError> {
        let mut cmd = if let Some(shell_cmd) = shell {
            let mut c = Command::new(shell_cmd);
            c.arg("-c").arg(&command);
            c
        } else {
            #[cfg(unix)]
            {
                let mut c = Command::new("sh");
                c.arg("-c").arg(&command);
                c
            }
            #[cfg(windows)]
            {
                let mut c = Command::new("cmd");
                c.arg("/C").arg(&command);
                c
            }
        };

        cmd.stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .kill_on_drop(true);

        let mut child = cmd
            .spawn()
            .map_err(|e| ToolError::from(format!("Failed to spawn process: {}", e)))?;

        let stdin = child.stdin.take();
        let stdout = child.stdout.take();
        let stderr = child.stderr.take();

        let mut next_pid = self.next_pid.lock().await;
        let pid = *next_pid;
        *next_pid += 1;

        let session = Session::new(pid, command, child, stdin, timeout_ms);
        let output_buffer = session.output_buffer.clone();

        // Spawn task to capture output
        if let Some(stdout) = stdout {
            let buffer = output_buffer.clone();
            tokio::spawn(async move {
                let reader = BufReader::new(stdout);
                let mut lines = reader.lines();
                while let Ok(Some(line)) = lines.next_line().await {
                    buffer.lock().await.push(line);
                }
            });
        }

        if let Some(stderr) = stderr {
            let buffer = output_buffer.clone();
            tokio::spawn(async move {
                let reader = BufReader::new(stderr);
                let mut lines = reader.lines();
                while let Ok(Some(line)) = lines.next_line().await {
                    buffer.lock().await.push(format!("[stderr] {}", line));
                }
            });
        }

        self.sessions.lock().await.insert(pid, session);
        Ok(pid)
    }

    pub async fn get_session(&self, pid: u32) -> Option<()> {
        self.sessions.lock().await.get(&pid).map(|_| ())
    }

    pub async fn read_output(&self, pid: u32, clear: bool) -> Result<Vec<String>, ToolError> {
        let sessions = self.sessions.lock().await;
        let session = sessions
            .get(&pid)
            .ok_or_else(|| format!("Session {} not found", pid))?;

        let mut buffer = session.output_buffer.lock().await;
        let output = buffer.clone();

        if clear {
            buffer.clear();
        }

        Ok(output)
    }

    pub async fn send_input(&self, pid: u32, input: &str) -> Result<(), ToolError> {
        let mut sessions = self.sessions.lock().await;
        let session = sessions
            .get_mut(&pid)
            .ok_or_else(|| format!("Session {} not found", pid))?;

        if let Some(stdin) = &mut session.stdin {
            stdin
                .write_all(input.as_bytes())
                .await
                .map_err(|e| ToolError::from(format!("Failed to write to stdin: {}", e)))?;
            stdin
                .write_all(b"\n")
                .await
                .map_err(|e| ToolError::from(format!("Failed to write newline: {}", e)))?;
            stdin
                .flush()
                .await
                .map_err(|e| ToolError::from(format!("Failed to flush stdin: {}", e)))?;
            Ok(())
        } else {
            Err("Process has no stdin".into())
        }
    }

    pub async fn check_status(&self, pid: u32) -> Result<ProcessState, ToolError> {
        let mut sessions = self.sessions.lock().await;
        let session = sessions
            .get_mut(&pid)
            .ok_or_else(|| format!("Session {} not found", pid))?;

        Ok(session.check_status().await)
    }

    pub async fn terminate(&self, pid: u32, force: bool) -> Result<(), ToolError> {
        let mut sessions = self.sessions.lock().await;
        let session = sessions
            .get_mut(&pid)
            .ok_or_else(|| format!("Session {} not found", pid))?;

        if force {
            session
                .process
                .kill()
                .await
                .map_err(|e| ToolError::from(format!("Failed to kill process: {}", e)))?;
        } else {
            #[cfg(unix)]
            {
                use nix::sys::signal::{self, Signal};
                use nix::unistd::Pid;
                if let Some(child_pid) = session.process.id() {
                    signal::kill(Pid::from_raw(child_pid as i32), Signal::SIGTERM)
                        .map_err(|e| ToolError::from(format!("Failed to send SIGTERM: {}", e)))?;
                } else {
                    return Err("Process has no PID".into());
                }
            }
            #[cfg(not(unix))]
            {
                session
                    .process
                    .kill()
                    .await
                    .map_err(|e| ToolError::from(format!("Failed to kill process: {}", e)))?;
            }
        }

        Ok(())
    }

    pub async fn list_sessions(&self) -> Vec<(u32, String, ProcessState, u64)> {
        let mut sessions = self.sessions.lock().await;
        let mut result = Vec::new();

        for session in sessions.values_mut() {
            let state = session.check_status().await;
            result.push((
                session.pid,
                session.command.clone(),
                state,
                session.elapsed_ms(),
            ));
        }

        result
    }
}

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

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

    // ==================== strip_ansi_codes tests ====================

    #[test]
    fn test_strip_ansi_codes_empty_string() {
        assert_eq!(strip_ansi_codes(""), "");
    }

    #[test]
    fn test_strip_ansi_codes_no_codes() {
        assert_eq!(strip_ansi_codes("Hello World"), "Hello World");
    }

    #[test]
    fn test_strip_ansi_codes_single_color() {
        // Green text
        assert_eq!(strip_ansi_codes("\x1b[32mGreen\x1b[0m"), "Green");
    }

    #[test]
    fn test_strip_ansi_codes_multiple_colors() {
        assert_eq!(
            strip_ansi_codes("\x1b[31mRed\x1b[0m and \x1b[34mBlue\x1b[0m"),
            "Red and Blue"
        );
    }

    #[test]
    fn test_strip_ansi_codes_complex_sequences() {
        // Bold, underline, colors with parameters
        assert_eq!(
            strip_ansi_codes("\x1b[1;4;31mBold underline red\x1b[0m"),
            "Bold underline red"
        );
    }

    #[test]
    fn test_strip_ansi_codes_256_colors() {
        // 256 color code (e.g., orange)
        assert_eq!(strip_ansi_codes("\x1b[38;5;208mOrange\x1b[0m"), "Orange");
    }

    #[test]
    fn test_strip_ansi_codes_cursor_movement() {
        // Cursor movement codes
        assert_eq!(strip_ansi_codes("\x1b[2Aup\x1b[3Bdown"), "updown");
    }

    #[test]
    fn test_strip_ansi_codes_preserves_other_escapes() {
        // Tab and newline should be preserved
        assert_eq!(
            strip_ansi_codes("Line1\nLine2\tTabbed"),
            "Line1\nLine2\tTabbed"
        );
    }

    // ==================== PROMPT_PATTERNS tests ====================

    #[test]
    fn test_prompt_patterns_python_repl() {
        assert!(PROMPT_PATTERNS.is_match(">>> "));
        assert!(PROMPT_PATTERNS.is_match(">>>"));
    }

    #[test]
    fn test_prompt_patterns_generic_prompt() {
        assert!(PROMPT_PATTERNS.is_match("> "));
        assert!(PROMPT_PATTERNS.is_match(">"));
    }

    #[test]
    fn test_prompt_patterns_shell_prompts() {
        assert!(PROMPT_PATTERNS.is_match("$ "));
        assert!(PROMPT_PATTERNS.is_match("# ")); // root
        assert!(PROMPT_PATTERNS.is_match("% ")); // zsh
    }

    #[test]
    fn test_prompt_patterns_input_prompts() {
        assert!(PROMPT_PATTERNS.is_match("Enter name: "));
        assert!(PROMPT_PATTERNS.is_match("? "));
        assert!(PROMPT_PATTERNS.is_match("password: "));
        assert!(PROMPT_PATTERNS.is_match("Password: "));
    }

    #[test]
    fn test_prompt_patterns_confirmation() {
        assert!(PROMPT_PATTERNS.is_match("Continue? (yes/no) "));
        assert!(PROMPT_PATTERNS.is_match("[Y/n] "));
        assert!(PROMPT_PATTERNS.is_match("[y/N] "));
    }

    #[test]
    fn test_prompt_patterns_language_repls() {
        assert!(PROMPT_PATTERNS.is_match("irb(main):001:0> "));
        assert!(PROMPT_PATTERNS.is_match("pry(main)> "));
        assert!(PROMPT_PATTERNS.is_match("iex(1)> "));
        assert!(PROMPT_PATTERNS.is_match("scala> "));
        assert!(PROMPT_PATTERNS.is_match("ghci> "));
    }

    #[test]
    fn test_prompt_patterns_database_prompts() {
        assert!(PROMPT_PATTERNS.is_match("sqlite> "));
        assert!(PROMPT_PATTERNS.is_match("mysql> "));
        assert!(PROMPT_PATTERNS.is_match("postgres=# "));
        assert!(PROMPT_PATTERNS.is_match("postgres=> "));
        assert!(PROMPT_PATTERNS.is_match("redis-cli> "));
    }

    #[test]
    fn test_prompt_patterns_non_prompts() {
        // Regular output should NOT match
        assert!(!PROMPT_PATTERNS.is_match("Hello World"));
        assert!(!PROMPT_PATTERNS.is_match("Processing..."));
        assert!(!PROMPT_PATTERNS.is_match("Error: something went wrong"));
        assert!(!PROMPT_PATTERNS.is_match("123456"));
        assert!(!PROMPT_PATTERNS.is_match("file.txt"));
    }

    #[test]
    fn test_prompt_patterns_partial_matches() {
        // Should match at end of line
        assert!(PROMPT_PATTERNS.is_match("user@host:~$ "));
        // Middle of line should not match (due to $ anchor)
        assert!(!PROMPT_PATTERNS.is_match("$ echo hello"));
    }

    // ==================== ProcessState tests ====================

    #[test]
    fn test_process_state_equality() {
        assert_eq!(ProcessState::Running, ProcessState::Running);
        assert_eq!(ProcessState::WaitingForInput, ProcessState::WaitingForInput);
        assert_eq!(ProcessState::TimedOut, ProcessState::TimedOut);
        assert_eq!(
            ProcessState::Completed { exit_code: Some(0) },
            ProcessState::Completed { exit_code: Some(0) }
        );
        assert_ne!(
            ProcessState::Completed { exit_code: Some(0) },
            ProcessState::Completed { exit_code: Some(1) }
        );
    }

    #[test]
    fn test_process_state_debug() {
        let running = ProcessState::Running;
        let completed = ProcessState::Completed { exit_code: Some(0) };
        assert!(format!("{:?}", running).contains("Running"));
        assert!(format!("{:?}", completed).contains("Completed"));
    }

    #[test]
    fn test_process_state_clone() {
        let state = ProcessState::Completed {
            exit_code: Some(42),
        };
        let cloned = state.clone();
        assert_eq!(state, cloned);
    }

    // ==================== Session tests ====================

    #[tokio::test]
    async fn test_session_elapsed_ms() {
        let mut cmd = Command::new("echo");
        cmd.arg("test");
        let child = cmd.spawn().expect("Failed to spawn test process");

        let session = Session::new(1, "echo test".to_string(), child, None, None);

        // Should have some elapsed time
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        assert!(session.elapsed_ms() >= 10);
    }

    #[tokio::test]
    async fn test_session_timeout_none() {
        let mut cmd = Command::new("echo");
        cmd.arg("test");
        let child = cmd.spawn().expect("Failed to spawn test process");

        let session = Session::new(1, "echo test".to_string(), child, None, None);

        // No timeout set, should never be timed out
        assert!(!session.is_timed_out());
    }

    #[tokio::test]
    async fn test_session_timeout_not_exceeded() {
        let mut cmd = Command::new("sleep");
        cmd.arg("10");
        cmd.kill_on_drop(true);
        let child = cmd.spawn().expect("Failed to spawn test process");

        let session = Session::new(1, "sleep 10".to_string(), child, None, Some(60000));

        // Timeout is 60 seconds, should not be exceeded
        assert!(!session.is_timed_out());
    }

    #[tokio::test]
    async fn test_session_timeout_exceeded() {
        let mut cmd = Command::new("sleep");
        cmd.arg("10");
        cmd.kill_on_drop(true);
        let child = cmd.spawn().expect("Failed to spawn test process");

        // Very short timeout
        let session = Session::new(1, "sleep 10".to_string(), child, None, Some(1));

        // Wait for timeout
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        assert!(session.is_timed_out());
    }

    #[tokio::test]
    async fn test_session_is_waiting_for_input_empty_buffer() {
        let mut cmd = Command::new("echo");
        cmd.arg("test");
        let child = cmd.spawn().expect("Failed to spawn test process");

        let session = Session::new(1, "echo test".to_string(), child, None, None);

        // Empty buffer should return false
        assert!(!session.is_waiting_for_input().await);
    }

    #[tokio::test]
    async fn test_session_is_waiting_for_input_with_prompt() {
        let mut cmd = Command::new("echo");
        cmd.arg("test");
        let child = cmd.spawn().expect("Failed to spawn test process");

        let session = Session::new(1, "echo test".to_string(), child, None, None);

        // Add a prompt to the buffer
        session.output_buffer.lock().await.push(">>> ".to_string());

        assert!(session.is_waiting_for_input().await);
    }

    #[tokio::test]
    async fn test_session_is_waiting_for_input_no_prompt() {
        let mut cmd = Command::new("echo");
        cmd.arg("test");
        let child = cmd.spawn().expect("Failed to spawn test process");

        let session = Session::new(1, "echo test".to_string(), child, None, None);

        // Add regular output to the buffer
        session
            .output_buffer
            .lock()
            .await
            .push("Hello World".to_string());

        assert!(!session.is_waiting_for_input().await);
    }

    #[tokio::test]
    async fn test_session_is_waiting_for_input_ansi_prompt() {
        let mut cmd = Command::new("echo");
        cmd.arg("test");
        let child = cmd.spawn().expect("Failed to spawn test process");

        let session = Session::new(1, "echo test".to_string(), child, None, None);

        // Add a prompt with ANSI codes
        session
            .output_buffer
            .lock()
            .await
            .push("\x1b[32m>>> \x1b[0m".to_string());

        // Should still detect the prompt after stripping ANSI codes
        assert!(session.is_waiting_for_input().await);
    }

    #[tokio::test]
    async fn test_session_check_status_completed() {
        // Use a command that exits immediately
        let mut cmd = Command::new("echo");
        cmd.arg("done");
        let child = cmd.spawn().expect("Failed to spawn test process");

        let mut session = Session::new(1, "echo done".to_string(), child, None, None);

        // Poll for completion with timeout (avoid flaky fixed sleep)
        let start = std::time::Instant::now();
        let timeout = std::time::Duration::from_secs(5);
        loop {
            let state = session.check_status().await;
            if matches!(state, ProcessState::Completed { .. }) {
                return; // Success
            }
            if start.elapsed() > timeout {
                panic!("Process did not complete within timeout");
            }
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        }
    }

    #[tokio::test]
    async fn test_session_check_status_timed_out() {
        let mut cmd = Command::new("sleep");
        cmd.arg("10");
        cmd.kill_on_drop(true);
        let child = cmd.spawn().expect("Failed to spawn test process");

        // Very short timeout
        let mut session = Session::new(1, "sleep 10".to_string(), child, None, Some(1));

        // Wait for timeout
        tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;

        let state = session.check_status().await;
        assert_eq!(state, ProcessState::TimedOut);
    }

    // ==================== SessionManager tests ====================

    #[tokio::test]
    async fn test_session_manager_new() {
        let manager = SessionManager::new();
        let sessions = manager.list_sessions().await;
        assert!(sessions.is_empty());
    }

    #[tokio::test]
    async fn test_session_manager_default() {
        let manager = SessionManager::default();
        let sessions = manager.list_sessions().await;
        assert!(sessions.is_empty());
    }

    #[tokio::test]
    async fn test_session_manager_create_session() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("echo hello".to_string(), None, None)
            .await
            .expect("Failed to create session");

        assert!(pid >= 10000); // PIDs start at 10000
    }

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

        let pid1 = manager
            .create_session("echo 1".to_string(), None, None)
            .await
            .expect("Failed to create session 1");

        let pid2 = manager
            .create_session("echo 2".to_string(), None, None)
            .await
            .expect("Failed to create session 2");

        // PIDs should be sequential
        assert_eq!(pid2, pid1 + 1);
    }

    #[tokio::test]
    async fn test_session_manager_create_session_with_custom_shell() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("echo hello".to_string(), Some("/bin/sh".to_string()), None)
            .await
            .expect("Failed to create session");

        assert!(pid >= 10000);
    }

    #[tokio::test]
    async fn test_session_manager_create_session_with_timeout() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("sleep 60".to_string(), None, Some(100))
            .await
            .expect("Failed to create session");

        // Wait for timeout
        tokio::time::sleep(tokio::time::Duration::from_millis(120)).await;

        let status = manager
            .check_status(pid)
            .await
            .expect("Failed to check status");
        assert_eq!(status, ProcessState::TimedOut);
    }

    #[tokio::test]
    async fn test_session_manager_get_session_exists() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("echo test".to_string(), None, None)
            .await
            .expect("Failed to create session");

        assert!(manager.get_session(pid).await.is_some());
    }

    #[tokio::test]
    async fn test_session_manager_get_session_not_exists() {
        let manager = SessionManager::new();
        assert!(manager.get_session(99999).await.is_none());
    }

    #[tokio::test]
    async fn test_session_manager_read_output() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("echo 'test output'".to_string(), None, None)
            .await
            .expect("Failed to create session");

        // Wait for output
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        let output = manager
            .read_output(pid, false)
            .await
            .expect("Failed to read output");
        assert!(!output.is_empty());
        assert!(output.iter().any(|line| line.contains("test output")));
    }

    #[tokio::test]
    async fn test_session_manager_read_output_clear() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("echo 'test'".to_string(), None, None)
            .await
            .expect("Failed to create session");

        // Wait for output
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        // Read with clear
        let output1 = manager
            .read_output(pid, true)
            .await
            .expect("Failed to read output");
        assert!(!output1.is_empty());

        // Read again - should be empty since we cleared
        let output2 = manager
            .read_output(pid, false)
            .await
            .expect("Failed to read output");
        assert!(output2.is_empty());
    }

    #[tokio::test]
    async fn test_session_manager_read_output_not_found() {
        let manager = SessionManager::new();
        let result = manager.read_output(99999, false).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[tokio::test]
    async fn test_session_manager_send_input() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("cat".to_string(), None, None)
            .await
            .expect("Failed to create session");

        // Send input
        manager
            .send_input(pid, "hello")
            .await
            .expect("Failed to send input");

        // Wait for echo
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        let output = manager
            .read_output(pid, false)
            .await
            .expect("Failed to read output");
        assert!(output.iter().any(|line| line.contains("hello")));
    }

    #[tokio::test]
    async fn test_session_manager_send_input_not_found() {
        let manager = SessionManager::new();
        let result = manager.send_input(99999, "test").await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[tokio::test]
    async fn test_session_manager_check_status() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("echo hello".to_string(), None, None)
            .await
            .expect("Failed to create session");

        // Wait for completion
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        let status = manager
            .check_status(pid)
            .await
            .expect("Failed to check status");
        assert!(matches!(status, ProcessState::Completed { .. }));
    }

    #[tokio::test]
    async fn test_session_manager_check_status_not_found() {
        let manager = SessionManager::new();
        let result = manager.check_status(99999).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[tokio::test]
    async fn test_session_manager_terminate_force() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("sleep 60".to_string(), None, None)
            .await
            .expect("Failed to create session");

        // Force kill
        manager
            .terminate(pid, true)
            .await
            .expect("Failed to terminate");

        // Check it's completed
        tokio::time::sleep(tokio::time::Duration::from_millis(20)).await;
        let status = manager
            .check_status(pid)
            .await
            .expect("Failed to check status");
        assert!(matches!(status, ProcessState::Completed { .. }));
    }

    #[tokio::test]
    async fn test_session_manager_terminate_graceful() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("sleep 60".to_string(), None, None)
            .await
            .expect("Failed to create session");

        // Graceful terminate (SIGTERM)
        manager
            .terminate(pid, false)
            .await
            .expect("Failed to terminate");

        // Check it's completed
        tokio::time::sleep(tokio::time::Duration::from_millis(20)).await;
        let status = manager
            .check_status(pid)
            .await
            .expect("Failed to check status");
        assert!(matches!(status, ProcessState::Completed { .. }));
    }

    #[tokio::test]
    async fn test_session_manager_terminate_not_found() {
        let manager = SessionManager::new();
        let result = manager.terminate(99999, true).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

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

        let pid1 = manager
            .create_session("sleep 10".to_string(), None, None)
            .await
            .expect("Failed to create session 1");

        let pid2 = manager
            .create_session("sleep 10".to_string(), None, None)
            .await
            .expect("Failed to create session 2");

        let sessions = manager.list_sessions().await;
        assert_eq!(sessions.len(), 2);

        let pids: Vec<u32> = sessions.iter().map(|(pid, _, _, _)| *pid).collect();
        assert!(pids.contains(&pid1));
        assert!(pids.contains(&pid2));

        // Cleanup
        let _ = manager.terminate(pid1, true).await;
        let _ = manager.terminate(pid2, true).await;
    }

    #[tokio::test]
    async fn test_session_manager_list_sessions_empty() {
        let manager = SessionManager::new();
        let sessions = manager.list_sessions().await;
        assert!(sessions.is_empty());
    }

    #[tokio::test]
    async fn test_session_manager_stderr_capture() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("echo 'error message' >&2".to_string(), None, None)
            .await
            .expect("Failed to create session");

        // Wait for output
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        let output = manager
            .read_output(pid, false)
            .await
            .expect("Failed to read output");
        // stderr lines should be prefixed with [stderr]
        assert!(output.iter().any(|line| line.contains("[stderr]")));
    }

    #[tokio::test]
    async fn test_session_manager_mixed_stdout_stderr() {
        let manager = SessionManager::new();
        let pid = manager
            .create_session("echo 'stdout' && echo 'stderr' >&2".to_string(), None, None)
            .await
            .expect("Failed to create session");

        // Wait for output
        tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

        let output = manager
            .read_output(pid, false)
            .await
            .expect("Failed to read output");
        assert!(output
            .iter()
            .any(|line| line.contains("stdout") && !line.contains("[stderr]")));
        assert!(output
            .iter()
            .any(|line| line.contains("[stderr]") && line.contains("stderr")));
    }
}