agent-procs 0.6.3

Concurrent process runner for AI agents
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
use crate::daemon::log_writer::{self, OutputLine};
use crate::daemon::port_allocator::PortAllocator;
use crate::paths;
use crate::protocol::{
    ErrorCode, ProcessInfo, ProcessState, Response, RestartMode, RestartPolicy,
    Stream as ProtoStream, WatchConfig, process_url,
};
use crate::session::IdCounter;
use std::collections::HashMap;
use std::process::Stdio;
use std::sync::Arc;
use std::sync::atomic::AtomicU64;
use std::time::{Duration, Instant};
use tokio::process::{Child, Command};
use tokio::sync::broadcast;

const DEFAULT_MAX_LOG_BYTES: u64 = 50 * 1024 * 1024; // 50MB

/// Returns true if `name` is a valid DNS label: 1-63 lowercase alphanumeric/hyphen
/// chars, not starting or ending with a hyphen.
#[must_use]
pub fn is_valid_dns_label(name: &str) -> bool {
    if name.is_empty() || name.len() > 63 {
        return false;
    }
    if name.starts_with('-') || name.ends_with('-') {
        return false;
    }
    name.chars()
        .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
}

pub struct ManagedProcess {
    pub name: String,
    pub id: String,
    pub command: String,
    pub cwd: Option<String>,
    pub env: HashMap<String, String>,
    pub child: Option<Child>,
    pub pid: u32,
    pub started_at: Instant,
    pub exit_code: Option<i32>,
    pub port: Option<u16>,
    // Supervisor fields
    pub restart_policy: Option<RestartPolicy>,
    pub watch_config: Option<WatchConfig>,
    pub restart_count: u32,
    pub manually_stopped: bool,
    pub restart_pending: bool,
    pub failed: bool,
    pub supervisor_tx: Option<tokio::sync::mpsc::Sender<String>>,
    pub capture_handles: Vec<tokio::task::JoinHandle<()>>,
    pub watch_handle: Option<crate::daemon::watcher::WatchHandle>,
}

pub struct ProcessManager {
    processes: HashMap<String, ManagedProcess>,
    id_counter: IdCounter,
    session: String,
    pub output_tx: broadcast::Sender<OutputLine>,
    port_allocator: PortAllocator,
}

impl ProcessManager {
    pub fn new(session: &str) -> Self {
        let (output_tx, _) = broadcast::channel(1024);
        Self {
            processes: HashMap::new(),
            id_counter: IdCounter::new(),
            session: session.to_string(),
            output_tx,
            port_allocator: PortAllocator::new(),
        }
    }

    // Keep the async API used by the daemon actor; process setup is synchronous,
    // while the spawned capture tasks perform the asynchronous I/O.
    #[allow(unknown_lints)]
    #[allow(unsafe_code, clippy::unused_async, clippy::unused_async_trait_impl)]
    pub async fn spawn_process(
        &mut self,
        command: &str,
        name: Option<String>,
        cwd: Option<&str>,
        env: Option<&HashMap<String, String>>,
        port: Option<u16>,
    ) -> Response {
        let id = self.id_counter.next_id();
        let name = name.unwrap_or_else(|| id.clone());

        // Reject names that could cause path traversal in log files
        if name.contains('/') || name.contains('\\') || name.contains("..") || name.contains('\0') {
            return Response::Error {
                code: ErrorCode::General,
                message: format!("invalid process name: {}", name),
            };
        }

        // When proxy is active, names must be valid DNS labels for subdomain routing
        if self.port_allocator.is_proxy_enabled() && !is_valid_dns_label(&name) {
            return Response::Error {
                code: ErrorCode::General,
                message: format!(
                    "invalid process name for proxy: '{}' (must be lowercase alphanumeric/hyphens, max 63 chars)",
                    name
                ),
            };
        }

        // Resolve the port: use explicit port, auto-assign if proxy is enabled, or None
        let resolved_port = if let Some(p) = port {
            Some(p)
        } else if self.port_allocator.is_proxy_enabled() {
            let assigned: std::collections::HashSet<u16> = self
                .processes
                .values()
                .filter(|p| p.child.is_some())
                .filter_map(|p| p.port)
                .collect();
            match self.port_allocator.auto_assign_port(&assigned) {
                Ok(p) => Some(p),
                Err(e) => {
                    return Response::Error {
                        code: ErrorCode::General,
                        message: e.to_string(),
                    };
                }
            }
        } else {
            None
        };

        if self.processes.contains_key(&name) {
            return Response::Error {
                code: ErrorCode::General,
                message: format!("process already exists: {}", name),
            };
        }

        let log_dir = paths::log_dir(&self.session);
        let _ = std::fs::create_dir_all(&log_dir);

        let mut cmd = Command::new("sh");
        cmd.arg("-c")
            .arg(command)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        if let Some(dir) = cwd {
            cmd.current_dir(dir);
        }
        if let Some(p) = resolved_port {
            // Inject PORT and HOST; user-supplied env takes precedence
            let mut merged_env: HashMap<String, String> = HashMap::new();
            merged_env.insert("PORT".to_string(), p.to_string());
            merged_env.insert("HOST".to_string(), "127.0.0.1".to_string());
            if let Some(env_vars) = env {
                for (k, v) in env_vars {
                    merged_env.insert(k.clone(), v.clone());
                }
            }
            cmd.envs(&merged_env);
        } else if let Some(env_vars) = env {
            cmd.envs(env_vars);
        }
        // SAFETY: `setpgid(0, 0)` creates a new process group with the child as
        // leader.  This must happen before exec so that all grandchildren inherit
        // the group.  The parent uses this PGID to signal the entire tree on stop.
        unsafe {
            cmd.pre_exec(|| {
                nix::unistd::setpgid(nix::unistd::Pid::from_raw(0), nix::unistd::Pid::from_raw(0))
                    .map_err(std::io::Error::other)?;
                Ok(())
            });
        }

        let mut child = match cmd.spawn() {
            Ok(c) => c,
            Err(e) => {
                return Response::Error {
                    code: ErrorCode::General,
                    message: format!("failed to spawn: {}", e),
                };
            }
        };

        let pid = child.id().unwrap_or(0);

        // Shared sequence counter for interleaved ordering across streams
        let seq_counter = Arc::new(AtomicU64::new(0));

        // Spawn output capture tasks via log_writer
        // Supervisor channel: stdout gets the real sender, stderr gets a dummy
        let (sup_tx, sup_rx_stdout) = tokio::sync::mpsc::channel::<String>(16);
        let (stderr_sup_sender, sup_rx_stderr) = tokio::sync::mpsc::channel::<String>(16);
        drop(stderr_sup_sender);

        let mut capture_handles = Vec::new();

        if let Some(stdout) = child.stdout.take() {
            let tx = self.output_tx.clone();
            let pname = name.clone();
            let path = log_dir.join(format!("{}.stdout", name));
            let seq = Arc::clone(&seq_counter);
            let handle = tokio::spawn(async move {
                log_writer::capture_output(
                    stdout,
                    &path,
                    &pname,
                    ProtoStream::Stdout,
                    tx,
                    DEFAULT_MAX_LOG_BYTES,
                    log_writer::DEFAULT_MAX_ROTATED_FILES,
                    seq,
                    sup_rx_stdout,
                )
                .await;
            });
            capture_handles.push(handle);
        }
        if let Some(stderr) = child.stderr.take() {
            let tx = self.output_tx.clone();
            let pname = name.clone();
            let path = log_dir.join(format!("{}.stderr", name));
            let seq = Arc::clone(&seq_counter);
            let handle = tokio::spawn(async move {
                log_writer::capture_output(
                    stderr,
                    &path,
                    &pname,
                    ProtoStream::Stderr,
                    tx,
                    DEFAULT_MAX_LOG_BYTES,
                    log_writer::DEFAULT_MAX_ROTATED_FILES,
                    seq,
                    sup_rx_stderr,
                )
                .await;
            });
            capture_handles.push(handle);
        }

        self.processes.insert(
            name.clone(),
            ManagedProcess {
                name: name.clone(),
                id: id.clone(),
                command: command.to_string(),
                cwd: cwd.map(std::string::ToString::to_string),
                env: env.cloned().unwrap_or_default(),
                child: Some(child),
                pid,
                started_at: Instant::now(),
                exit_code: None,
                port: resolved_port,
                restart_policy: None,
                watch_config: None,
                restart_count: 0,
                manually_stopped: false,
                restart_pending: false,
                failed: false,
                supervisor_tx: Some(sup_tx),
                capture_handles,
                watch_handle: None,
            },
        );

        let url = resolved_port.map(|p| process_url(&name, p, None));
        Response::RunOk {
            name,
            id,
            pid,
            port: resolved_port,
            url,
        }
    }

    pub async fn stop_process(&mut self, target: &str) -> Response {
        let proc = match self.find_mut(target) {
            Some(p) => p,
            None => {
                return Response::Error {
                    code: ErrorCode::NotFound,
                    message: format!("process not found: {}", target),
                };
            }
        };

        proc.manually_stopped = true;

        if let Some(ref child) = proc.child {
            let raw_pid = child.id().unwrap_or(0) as i32;
            if raw_pid > 0 {
                // Signal the entire process group (child PID == PGID due to setpgid in pre_exec)
                let pgid = nix::unistd::Pid::from_raw(raw_pid);
                let _ = nix::sys::signal::killpg(pgid, nix::sys::signal::Signal::SIGTERM);
            }
        }

        // Wait up to 10s for graceful exit, then SIGKILL
        if let Some(ref mut child) = proc.child {
            let wait_result = tokio::time::timeout(Duration::from_secs(10), child.wait()).await;

            match wait_result {
                Ok(Ok(status)) => {
                    proc.exit_code = status.code();
                }
                _ => {
                    // Timed out or error — force kill the process group
                    let raw_pid = proc.pid as i32;
                    if raw_pid > 0 {
                        let pgid = nix::unistd::Pid::from_raw(raw_pid);
                        let _ = nix::sys::signal::killpg(pgid, nix::sys::signal::Signal::SIGKILL);
                    }
                    let _ = child.wait().await;
                    proc.exit_code = Some(-9);
                }
            }
            proc.child = None;
        }

        Response::Ok {
            message: format!("stopped {}", target),
        }
    }

    pub async fn stop_all(&mut self) -> Response {
        let names: Vec<String> = self.processes.keys().cloned().collect();
        for name in names {
            let _ = self.stop_process(&name).await;
        }
        self.processes.clear();
        Response::Ok {
            message: "all processes stopped".into(),
        }
    }

    pub async fn restart_process(&mut self, target: &str) -> Response {
        let (command, name, cwd, env, port, restart_policy, watch_config) = match self.find(target)
        {
            Some(p) => (
                p.command.clone(),
                p.name.clone(),
                p.cwd.clone(),
                p.env.clone(),
                p.port,
                p.restart_policy.clone(),
                p.watch_config.clone(),
            ),
            None => {
                return Response::Error {
                    code: ErrorCode::NotFound,
                    message: format!("process not found: {}", target),
                };
            }
        };
        // Clear supervisor flags — restart is intentional
        if let Some(p) = self.find_mut(target) {
            p.manually_stopped = false;
            p.restart_count = 0;
            p.failed = false;
            p.restart_pending = false;
        }
        let _ = self.stop_process(target).await;
        self.processes.remove(&name);
        let env = if env.is_empty() { None } else { Some(env) };
        let resp = self
            .spawn_process(
                &command,
                Some(name.clone()),
                cwd.as_deref(),
                env.as_ref(),
                port,
            )
            .await;
        // Re-attach restart/watch config so manual restart preserves supervisor behavior
        if let Response::RunOk { .. } = resp
            && let Some(p) = self.find_mut(&name)
        {
            p.restart_policy = restart_policy;
            p.watch_config = watch_config;
        }
        resp
    }

    pub fn enable_proxy(&mut self) {
        self.port_allocator.enable_proxy();
    }

    pub fn status(&mut self) -> Response {
        self.refresh_exit_states();
        Response::Status {
            processes: self.build_process_infos(),
        }
    }

    /// Returns `None` if process not found or still running.
    /// Returns `Some(exit_code)` if process has exited (`exit_code` is None for signal kills).
    pub fn is_process_exited(&mut self, target: &str) -> Option<Option<i32>> {
        self.refresh_exit_states();
        self.find(target).and_then(|p| {
            if p.child.is_none() {
                Some(p.exit_code)
            } else {
                None
            }
        })
    }

    pub(crate) fn refresh_exit_states(&mut self) -> bool {
        let mut changed = false;
        for proc in self.processes.values_mut() {
            if proc.child.is_some()
                && proc.exit_code.is_none()
                && let Some(ref mut child) = proc.child
                && let Ok(Some(status)) = child.try_wait()
            {
                proc.exit_code = status.code();
                proc.child = None;
                changed = true;
            }
        }
        changed
    }

    pub fn session_name(&self) -> &str {
        &self.session
    }

    pub fn has_process(&self, target: &str) -> bool {
        self.find(target).is_some()
    }

    /// Returns a map of running process names to their assigned ports.
    pub fn running_ports(&self) -> HashMap<String, u16> {
        self.processes
            .iter()
            .filter_map(|(name, p)| {
                if p.child.is_some() {
                    p.port.map(|port| (name.clone(), port))
                } else {
                    None
                }
            })
            .collect()
    }

    /// Non-mutating status snapshot for use by the proxy status page.
    /// May show stale exit states since it skips `refresh_exit_states()`.
    pub fn status_snapshot(&self) -> Response {
        Response::Status {
            processes: self.build_process_infos(),
        }
    }

    fn build_process_infos(&self) -> Vec<ProcessInfo> {
        let mut infos: Vec<ProcessInfo> = self
            .processes
            .values()
            .map(|p| ProcessInfo {
                name: p.name.clone(),
                id: p.id.clone(),
                pid: p.pid,
                state: if p.child.is_some() {
                    ProcessState::Running
                } else if p.failed {
                    ProcessState::Failed
                } else {
                    ProcessState::Exited
                },
                exit_code: p.exit_code,
                uptime_secs: if p.child.is_some() {
                    Some(p.started_at.elapsed().as_secs())
                } else {
                    None
                },
                command: p.command.clone(),
                port: p.port,
                url: p.port.map(|port| process_url(&p.name, port, None)),
                restart_count: if p.restart_count > 0 {
                    Some(p.restart_count)
                } else {
                    None
                },
                max_restarts: p.restart_policy.as_ref().and_then(|rp| rp.max_restarts),
                restart_policy: p.restart_policy.as_ref().map(|rp| match rp.mode {
                    RestartMode::Always => "always".into(),
                    RestartMode::OnFailure => "on-failure".into(),
                    RestartMode::Never => "never".into(),
                }),
                watched: if p.watch_config.is_some() {
                    Some(true)
                } else {
                    None
                },
            })
            .collect();
        infos.sort_by(|a, b| a.name.cmp(&b.name));
        infos
    }

    pub(crate) fn find(&self, target: &str) -> Option<&ManagedProcess> {
        self.processes
            .get(target)
            .or_else(|| self.processes.values().find(|p| p.id == target))
    }

    pub(crate) fn find_mut(&mut self, target: &str) -> Option<&mut ManagedProcess> {
        if self.processes.contains_key(target) {
            self.processes.get_mut(target)
        } else {
            self.processes.values_mut().find(|p| p.id == target)
        }
    }

    /// Classify exited processes that have a restart policy into "restartable" vs "exhausted".
    /// Returns `(restartable, exhausted)` name vectors in a single pass.
    pub fn classify_restart_candidates(&self) -> (Vec<String>, Vec<String>) {
        let mut restartable = Vec::new();
        let mut exhausted = Vec::new();
        for p in self.processes.values() {
            if p.child.is_some() || p.manually_stopped || p.restart_pending || p.failed {
                continue;
            }
            let Some(ref policy) = p.restart_policy else {
                continue;
            };
            if !policy.mode.should_restart(p.exit_code) {
                continue;
            }
            if policy
                .max_restarts
                .is_some_and(|max| p.restart_count >= max)
            {
                exhausted.push(p.name.clone());
            } else {
                restartable.push(p.name.clone());
            }
        }
        (restartable, exhausted)
    }

    /// Mark a process as failed (max restarts exhausted).
    pub fn mark_failed(&mut self, target: &str) {
        if let Some(p) = self.find_mut(target) {
            p.failed = true;
        }
    }

    /// Re-spawn a process in place, preserving supervisor metadata.
    /// Drains capture tasks, rotates logs, re-spawns, and carries over metadata.
    /// On spawn failure, reinserts a tombstone record with failed=true.
    pub async fn respawn_in_place(&mut self, target: &str) -> Result<(), String> {
        let proc = self
            .find(target)
            .ok_or_else(|| format!("process not found: {}", target))?;

        // 1. Save metadata and spawn args
        let command = proc.command.clone();
        let name = proc.name.clone();
        let cwd = proc.cwd.clone();
        let env = proc.env.clone();
        let port = proc.port;
        let restart_policy = proc.restart_policy.clone();
        let watch_config = proc.watch_config.clone();
        let restart_count = proc.restart_count;
        let restart_pending = proc.restart_pending;

        // 2. Drop supervisor sender (signals capture task to drain)
        if let Some(proc) = self.find_mut(target) {
            proc.supervisor_tx = None;
        }

        // 3. Await capture task JoinHandles
        if let Some(proc) = self.find_mut(target) {
            let handles = std::mem::take(&mut proc.capture_handles);
            for h in handles {
                let _ = h.await;
            }
        }

        // 4. Rotate log files
        let log_dir = crate::paths::log_dir(&self.session);
        let stdout_path = log_dir.join(format!("{}.stdout", name));
        let stderr_path = log_dir.join(format!("{}.stderr", name));
        log_writer::rotate_if_exists(&stdout_path).await;
        log_writer::rotate_if_exists(&stderr_path).await;

        // 5. Remove old record
        self.processes.remove(&name);

        // 6. Spawn fresh
        let env_opt = if env.is_empty() {
            None
        } else {
            Some(env.clone())
        };
        let resp = self
            .spawn_process(
                &command,
                Some(name.clone()),
                cwd.as_deref(),
                env_opt.as_ref(),
                port,
            )
            .await;

        match resp {
            Response::RunOk { .. } => {
                // 7. Copy metadata to new record
                if let Some(p) = self.find_mut(&name) {
                    p.restart_policy = restart_policy;
                    p.watch_config = watch_config;
                    p.restart_count = restart_count;
                    p.failed = false;
                }
                Ok(())
            }
            Response::Error { message, .. } => {
                // 8. Reinsert tombstone
                self.processes.insert(
                    name.clone(),
                    ManagedProcess {
                        name: name.clone(),
                        id: "tombstone".into(),
                        command,
                        cwd,
                        env,
                        child: None,
                        pid: 0,
                        started_at: Instant::now(),
                        exit_code: None,
                        port,
                        restart_policy,
                        watch_config,
                        restart_count,
                        manually_stopped: false,
                        restart_pending,
                        failed: true,
                        supervisor_tx: None,
                        capture_handles: Vec::new(),
                        watch_handle: None,
                    },
                );
                Err(message)
            }
            _ => Err("unexpected response from spawn".into()),
        }
    }
}

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

    #[test]
    fn test_valid_dns_labels() {
        assert!(is_valid_dns_label("api"));
        assert!(is_valid_dns_label("my-app"));
        assert!(is_valid_dns_label("a"));
        assert!(is_valid_dns_label("a1"));
        assert!(is_valid_dns_label("123"));
    }

    #[test]
    fn test_invalid_dns_labels() {
        assert!(!is_valid_dns_label(""));
        assert!(!is_valid_dns_label("-start"));
        assert!(!is_valid_dns_label("end-"));
        assert!(!is_valid_dns_label("UPPER"));
        assert!(!is_valid_dns_label("has.dot"));
        assert!(!is_valid_dns_label("has space"));
        assert!(!is_valid_dns_label(&"a".repeat(64))); // > 63 chars
        assert!(!is_valid_dns_label("has_underscore"));
    }

    #[tokio::test]
    async fn test_respawn_in_place_preserves_metadata() {
        let mut pm = ProcessManager::new("test-respawn");
        let resp = pm
            .spawn_process("echo hello", Some("worker".into()), None, None, None)
            .await;
        assert!(matches!(resp, Response::RunOk { .. }));

        // Set supervisor metadata
        if let Some(p) = pm.find_mut("worker") {
            p.restart_policy = Some(RestartPolicy {
                mode: RestartMode::OnFailure,
                max_restarts: Some(5),
                restart_delay_ms: 1000,
            });
            p.restart_count = 3;
        }

        // Wait for the process to exit
        tokio::time::sleep(std::time::Duration::from_millis(200)).await;
        pm.refresh_exit_states();

        // Respawn
        let result = pm.respawn_in_place("worker").await;
        assert!(result.is_ok());

        // Verify metadata preserved
        let p = pm.find("worker").unwrap();
        assert!(p.child.is_some()); // new process running
        assert_eq!(p.restart_count, 3);
        assert!(p.restart_policy.is_some());
        assert_eq!(
            p.restart_policy.as_ref().unwrap().mode,
            RestartMode::OnFailure
        );
        assert!(!p.failed);
    }

    #[tokio::test]
    async fn test_respawn_in_place_tombstone_on_failure() {
        let mut pm = ProcessManager::new("test-tombstone");
        let resp = pm
            .spawn_process("echo hello", Some("worker".into()), None, None, None)
            .await;
        assert!(matches!(resp, Response::RunOk { .. }));

        if let Some(p) = pm.find_mut("worker") {
            p.restart_policy = Some(RestartPolicy {
                mode: RestartMode::Always,
                max_restarts: Some(3),
                restart_delay_ms: 1000,
            });
            p.restart_count = 2;
            // Corrupt name to contain path traversal — triggers spawn validation error
            p.name = "work/er".to_string();
        }

        // Re-key in the processes map under the corrupted name
        let proc = pm.processes.remove("worker").unwrap();
        pm.processes.insert("work/er".to_string(), proc);

        tokio::time::sleep(std::time::Duration::from_millis(200)).await;
        pm.refresh_exit_states();

        let result = pm.respawn_in_place("work/er").await;
        assert!(result.is_err());

        // Tombstone should exist
        let p = pm.find("work/er").unwrap();
        assert!(p.child.is_none());
        assert!(p.failed);
        assert_eq!(p.restart_count, 2); // preserved
        assert!(p.restart_policy.is_some());
    }

    /// Helper: build an exited `ManagedProcess` with sensible defaults.
    fn make_exited_process(name: &str, exit_code: Option<i32>) -> ManagedProcess {
        ManagedProcess {
            name: name.to_string(),
            id: format!("id-{}", name),
            command: "true".to_string(),
            cwd: None,
            env: HashMap::new(),
            child: None,
            pid: 0,
            started_at: Instant::now(),
            exit_code,
            port: None,
            restart_policy: None,
            watch_config: None,
            restart_count: 0,
            manually_stopped: false,
            restart_pending: false,
            failed: false,
            supervisor_tx: None,
            capture_handles: Vec::new(),
            watch_handle: None,
        }
    }

    #[tokio::test]
    async fn test_classify_skips_running_process() {
        let mut pm = ProcessManager::new("test-classify-running");
        let mut proc = make_exited_process("runner", Some(1));
        proc.restart_policy = Some(RestartPolicy {
            mode: RestartMode::Always,
            max_restarts: None,
            restart_delay_ms: 0,
        });
        // Simulate a running process by spawning a real tokio child
        let child = Command::new("sleep")
            .arg("60")
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .spawn()
            .unwrap();
        let pid = child.id().unwrap_or(0) as i32;
        proc.child = Some(child);
        pm.processes.insert("runner".into(), proc);

        let (restartable, exhausted) = pm.classify_restart_candidates();
        assert!(restartable.is_empty());
        assert!(exhausted.is_empty());

        // Clean up the child process
        if pid > 0 {
            let _ = nix::sys::signal::kill(
                nix::unistd::Pid::from_raw(pid),
                nix::sys::signal::Signal::SIGKILL,
            );
        }
        if let Some(p) = pm.processes.get_mut("runner")
            && let Some(ref mut c) = p.child
        {
            let _ = c.wait().await;
        }
    }

    #[test]
    fn test_classify_skips_manually_stopped() {
        let mut pm = ProcessManager::new("test-classify-stopped");
        let mut proc = make_exited_process("stopped", Some(1));
        proc.restart_policy = Some(RestartPolicy {
            mode: RestartMode::Always,
            max_restarts: None,
            restart_delay_ms: 0,
        });
        proc.manually_stopped = true;
        pm.processes.insert("stopped".into(), proc);

        let (restartable, exhausted) = pm.classify_restart_candidates();
        assert!(restartable.is_empty());
        assert!(exhausted.is_empty());
    }

    #[test]
    fn test_classify_skips_clean_exit_on_failure_mode() {
        let mut pm = ProcessManager::new("test-classify-clean");
        let mut proc = make_exited_process("clean", Some(0));
        proc.restart_policy = Some(RestartPolicy {
            mode: RestartMode::OnFailure,
            max_restarts: None,
            restart_delay_ms: 0,
        });
        pm.processes.insert("clean".into(), proc);

        let (restartable, exhausted) = pm.classify_restart_candidates();
        assert!(restartable.is_empty());
        assert!(exhausted.is_empty());
    }

    #[test]
    fn test_classify_skips_restart_pending() {
        let mut pm = ProcessManager::new("test-classify-pending");
        let mut proc = make_exited_process("pending", Some(1));
        proc.restart_policy = Some(RestartPolicy {
            mode: RestartMode::Always,
            max_restarts: None,
            restart_delay_ms: 0,
        });
        proc.restart_pending = true;
        pm.processes.insert("pending".into(), proc);

        let (restartable, exhausted) = pm.classify_restart_candidates();
        assert!(restartable.is_empty());
        assert!(exhausted.is_empty());
    }

    #[test]
    fn test_classify_restartable_on_failure() {
        let mut pm = ProcessManager::new("test-classify-on-failure");
        let mut proc = make_exited_process("crasher", Some(1));
        proc.restart_policy = Some(RestartPolicy {
            mode: RestartMode::OnFailure,
            max_restarts: None,
            restart_delay_ms: 0,
        });
        pm.processes.insert("crasher".into(), proc);

        let (restartable, exhausted) = pm.classify_restart_candidates();
        assert_eq!(restartable, vec!["crasher"]);
        assert!(exhausted.is_empty());
    }

    #[test]
    fn test_classify_exhausted() {
        let mut pm = ProcessManager::new("test-classify-exhausted");
        let mut proc = make_exited_process("tired", Some(1));
        proc.restart_policy = Some(RestartPolicy {
            mode: RestartMode::Always,
            max_restarts: Some(3),
            restart_delay_ms: 0,
        });
        proc.restart_count = 3;
        pm.processes.insert("tired".into(), proc);

        let (restartable, exhausted) = pm.classify_restart_candidates();
        assert!(restartable.is_empty());
        assert_eq!(exhausted, vec!["tired"]);
    }

    #[test]
    fn test_classify_always_mode_clean_exit() {
        let mut pm = ProcessManager::new("test-classify-always-clean");
        let mut proc = make_exited_process("always-on", Some(0));
        proc.restart_policy = Some(RestartPolicy {
            mode: RestartMode::Always,
            max_restarts: None,
            restart_delay_ms: 0,
        });
        pm.processes.insert("always-on".into(), proc);

        let (restartable, exhausted) = pm.classify_restart_candidates();
        assert_eq!(restartable, vec!["always-on"]);
        assert!(exhausted.is_empty());
    }
}