devsandbox 0.11.1

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
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
//! Per-instance process rows: expansion, selection, signalling, and which
//! instances the background proc fetch should poll.

use std::collections::BTreeMap;

use crate::tui::data::{ContainerStatus, Node};
use crate::tui::procs::ProcState;
use crate::tui::remote::{self, instance_key};

use super::{App, Tab};

/// A POSIX signal the process-row shortcuts can send. Kept small and typed so
/// the key handler, status text, and the event loop share one source of truth.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Signal {
    Term,
    Kill,
}

impl Signal {
    /// The name `instances.signal` takes (the daemon maps it to the number
    /// `kill -<n>` gets).
    pub fn wire(self) -> &'static str {
        match self {
            Signal::Term => "TERM",
            Signal::Kill => "KILL",
        }
    }

    /// Display name for status messages.
    pub fn name(self) -> &'static str {
        match self {
            Signal::Term => "SIGTERM",
            Signal::Kill => "SIGKILL",
        }
    }
}

/// A pending `kill` the event loop should send as `instances.signal`: signal
/// `pid` inside `instance`'s container (the daemon resolves it). Set by the
/// SIGTERM/SIGKILL process-row shortcuts.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingSignal {
    /// State key (a snapshot row name).
    pub instance: String,
    pub pid: String,
    pub signal: Signal,
}

/// A pending `sessions.rm` the event loop should send: end helper session
/// `id` of `instance` (a key). Set by `K` on a session row.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PendingSessionRm {
    pub instance: String,
    pub id: String,
}

impl App {
    /// The selected session row (`tui::procs::session_rows`) as `(instance
    /// key, session id)`; `None` on every other selection. Not gated on
    /// `signalable`: the sessions come from the helper, not `ps`.
    pub(super) fn selected_session(&self) -> Option<(String, String)> {
        let Node::Proc { instance, row } = self.selected_node()? else {
            return None;
        };
        let inst = self.snapshot.as_ref()?.instances.get(instance)?;
        let key = instance_key(inst);
        let ProcState::Rows { rows, .. } = self.procs.get(&key)? else {
            return None;
        };
        let id = crate::tui::procs::session_id(rows.get(row)?)?.to_string();
        Some((key, id))
    }

    /// `a`/`enter` on a session row: a tab attached to it, taking it over
    /// when another client is attached (the row was chosen).
    pub(super) fn attach_selected_session(&mut self) {
        if let Some((key, id)) = self.selected_session() {
            self.open_attach(&key, &id);
        }
    }

    /// `K` on a session row: queue its `sessions.rm` for the event loop.
    pub(super) fn end_selected_session(&mut self) {
        let Some((instance, id)) = self.selected_session() else {
            return;
        };
        self.status = Some(format!("ending session {id}…"));
        self.pending_session_rm = Some(PendingSessionRm { instance, id });
    }

    /// Take the pending session end for the event loop to send, if any.
    pub fn take_pending_session_rm(&mut self) -> Option<PendingSessionRm> {
        self.pending_session_rm.take()
    }

    /// Whether the cursor is on a session row (drives the help bar's legend).
    pub fn on_session_row(&self) -> bool {
        self.on_proc_row() && self.selected_session().is_some()
    }

    /// The selected process row as `(instance, pid)`, when the cursor is on a
    /// real process (not a `(message)` placeholder) with a numeric pid. Backs the
    /// SIGTERM/SIGKILL shortcuts; `None` on every other selection.
    fn selected_proc(&self) -> Option<(String, String)> {
        let snapshot = self.snapshot.as_ref()?;
        let Node::Proc { instance, row } = self.selected_node()? else {
            return None;
        };
        if row == crate::tui::procs::MESSAGE_ROW {
            return None;
        }
        let inst = snapshot.instances.get(instance)?;
        let ProcState::Rows { rows, signalable, .. } = self.procs.get(&instance_key(inst))? else {
            return None;
        };
        // A `top` fallback listing has host-namespace pids that `exec kill` can't
        // reach; don't offer signals for it.
        if !signalable {
            return None;
        }
        let proc = rows.get(row)?;
        // Defensive: `parse_top` only yields numeric pids, but never hand a
        // non-numeric token to `kill`.
        proc.pid.parse::<u32>().ok()?;
        Some((instance_key(inst), proc.pid.clone()))
    }

    /// Queue `signal` for the selected process, for the event loop to send to
    /// the daemon. No-op when the cursor is not on a signalable process.
    pub(super) fn signal_selected_proc(&mut self, signal: Signal) {
        let Some((instance, pid)) = self.selected_proc() else {
            return;
        };
        self.status = Some(format!("sending {} to pid {pid}…", signal.name()));
        self.pending_signal = Some(PendingSignal {
            instance,
            pid,
            signal,
        });
    }

    /// Take the pending process signal for the event loop to send, if any.
    pub fn take_pending_signal(&mut self) -> Option<PendingSignal> {
        self.pending_signal.take()
    }

    /// Whether the cursor is on a process row (Instances tab). Drives the help
    /// bar's process-specific legend.
    pub fn on_proc_row(&self) -> bool {
        self.tab == Tab::Instances && matches!(self.selected_node(), Some(Node::Proc { .. }))
    }

    /// Whether the selected process row can be signalled (its pids are
    /// container-namespace). False for a `(message)` row or a `top` fallback
    /// listing; drives whether the help bar advertises SIGTERM/SIGKILL.
    pub fn proc_row_signalable(&self) -> bool {
        self.selected_proc().is_some()
    }

    /// Agent-process count for a cached instance: the number of forest rows whose
    /// args name a known coding agent (see [`crate::tui::procs::is_agent`]). `None`
    /// when the instance has no fetched forest yet (not running, error, or the
    /// fetch is still in flight), which the Detail panel renders as `…`/`-`.
    pub fn agent_count(&self, instance: &str) -> Option<usize> {
        match self.procs.get(instance) {
            // Run and session rows repeat a live agent's argv: count processes only.
            Some(ProcState::Rows { rows, .. }) => Some(
                rows.iter()
                    .filter(|r| {
                        ![crate::tui::procs::RUN_PID, crate::tui::procs::SESSION_PID].contains(&r.pid.as_str())
                            && crate::tui::procs::is_agent(&r.args)
                    })
                    .count(),
            ),
            _ => None,
        }
    }

    /// The helper's session count for a cached instance (`instances.procs`'
    /// `sessions`). `None` when nothing is cached, the cache is a message, or
    /// the helper didn't answer one (missing or predating sessions).
    pub fn session_count(&self, instance: &str) -> Option<crate::tui::procs::SessionCount> {
        match self.procs.get(instance) {
            Some(ProcState::Rows { sessions, .. }) => *sessions,
            _ => None,
        }
    }

    /// The selected instance's name as a fetch target, but only when its
    /// container is running. Drives the on-demand proc fetch that backs the
    /// Detail agent count for instances that aren't expanded. `None` on non-
    /// instance nodes or a non-running selection.
    fn selected_running_instance(&self) -> Option<String> {
        let snapshot = self.snapshot.as_ref()?;
        let idx = self.selected_instance_index()?;
        let row = snapshot.instances.get(idx)?;
        let reachable = !remote::unreachable(&self.machines, row.machine.as_deref());
        (reachable && matches!(row.status, ContainerStatus::Running(_))).then(|| instance_key(row))
    }

    /// Consume the "fetch now" signal set on expand.
    pub fn take_needs_proc_fetch(&mut self) -> bool {
        std::mem::take(&mut self.needs_proc_fetch)
    }

    /// The process-list fetch targets (`instances.procs` names): every
    /// expanded instance whose container is running. Expanded instances that are
    /// not running (or absent from the snapshot) get a `(not running)` message
    /// row stored directly here — no fetch — and are omitted from the returned
    /// list. Instances no longer in the snapshot are dropped from the cache. The
    /// selected running instance is appended (deduped) so the Detail agent count
    /// has a fresh forest even when its process layer isn't expanded. Empty off
    /// the Instances tab: nothing there shows processes, and each target costs
    /// three `docker exec`s (`ps`, `devsbd run ls`, `devsbd session ls`).
    pub fn proc_fetch_targets(&mut self) -> Vec<String> {
        if self.tab != Tab::Instances {
            return Vec::new();
        }
        let Some(snapshot) = &self.snapshot else {
            return Vec::new();
        };
        let mut targets = Vec::new();
        let mut not_running: Vec<(String, &str)> = Vec::new();
        for name in &self.expanded_procs {
            match remote::find_instance(snapshot, name) {
                // A machine that isn't connected can't be asked.
                Some(row) if remote::unreachable(&self.machines, row.machine.as_deref()) => {
                    not_running.push((name.clone(), "(machine unreachable)"));
                }
                Some(row) if matches!(row.status, ContainerStatus::Running(_)) => {
                    targets.push(name.clone());
                }
                _ => not_running.push((name.clone(), "(not running)")),
            }
        }
        for (name, why) in not_running {
            self.procs.insert(name, ProcState::Message(why.to_string()));
        }
        // The selected running instance is fetched too (for the Detail agent
        // count), even when its process layer isn't expanded. Deduped against the
        // expanded targets so it's never fetched twice in one batch.
        if let Some(name) = self.selected_running_instance() {
            if !targets.contains(&name) {
                targets.push(name);
            }
        }
        targets
    }

    /// Merge a completed proc fetch into the cache, overwriting only fetched
    /// keys (other instances' cached rows are left untouched).
    pub fn apply_proc_fetch(&mut self, fetched: BTreeMap<String, ProcState>) {
        self.procs.extend(fetched);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tui::app::test_support::*;
    use crate::tui::app::*;

    /// Off the Instances tab nothing shows processes, so nothing is fetched;
    /// coming back asks for a fetch right away.
    #[test]
    fn proc_fetch_only_on_instances_tab() {
        let mut app = new_app();
        app.set_snapshot(snapshot_with_status(1, running()));
        app.on_key(key(KeyCode::Down));
        assert_eq!(app.proc_fetch_targets().len(), 1);
        app.set_tab(Tab::Ports);
        assert!(app.proc_fetch_targets().is_empty());
        app.take_needs_proc_fetch();
        app.set_tab(Tab::Instances);
        assert!(app.take_needs_proc_fetch());
        assert_eq!(app.proc_fetch_targets().len(), 1);
    }

    /// Processes are keyed per machine: the fetch targets are keys (the
    /// poll asks each machine for its own), a machine that isn't connected
    /// isn't asked, and a signal names its instance's key.
    #[test]
    fn procs_are_keyed_per_machine() {
        use crate::serve::snapshots::LinkState;
        let mut app = machine_app();
        app.expanded_procs.extend(["inst0".to_string(), "box/inst0".to_string()]);
        app.selected[Tab::Instances.index()] = 0;
        let mut targets = app.proc_fetch_targets();
        targets.sort();
        assert_eq!(targets, ["box/inst0", "inst0"]);
        app.machines[0].state = LinkState::Unreachable;
        assert_eq!(app.proc_fetch_targets(), ["inst0"]);
        assert_eq!(app.procs["box/inst0"], ProcState::Message("(machine unreachable)".into()));
        app.machines[0].state = LinkState::Connected;

        expand_with_rows(&mut app, "box/inst1", &["42"]);
        let at = app.visible_nodes().iter().position(|n| *n == Node::Proc { instance: 3, row: 0 }).unwrap();
        app.selected[Tab::Instances.index()] = at;
        app.signal_selected_proc(Signal::Term);
        assert_eq!(
            app.take_pending_signal(),
            Some(PendingSignal { instance: "box/inst1".into(), pid: "42".into(), signal: Signal::Term })
        );
    }

    /// A pushed snapshot is installed as is: the daemon carries cpu/mem over,
    /// so a row it left without them isn't filled from the last one.
    #[test]
    fn snapshots_are_installed_without_carrying_cpu_mem() {
        let mut app = new_app();
        let mut full = snapshot_with_status(2, running());
        for row in &mut full.instances {
            row.cpu = Some("1%".into());
            row.mem = Some("1MiB".into());
        }
        app.set_snapshot(full);
        let mut next = snapshot_with_status(2, running());
        next.depth = crate::snapshot::Depth::Listing;
        next.instances[0].cpu = Some("2%".into());
        next.instances[1].status = ContainerStatus::Exited("Exited (0)".into());
        app.set_snapshot(next);
        let rows = &app.snapshot.as_ref().unwrap().instances;
        assert_eq!(rows[0].cpu.as_deref(), Some("2%"));
        assert_eq!(rows[0].mem, None, "not filled from the last snapshot");
        assert_eq!(rows[1].cpu, None, "stopped: no stale cpu");
    }

    #[test]
    fn instance_shortcuts_disabled_on_proc_row() {
        let mut app = app_on_proc_row(&["10"]);
        // None of the instance-forwarding shortcuts fire on a process row.
        for code in [
            KeyCode::Char('s'),
            KeyCode::Char('o'),
            KeyCode::Char('r'),
            KeyCode::Char('l'),
            KeyCode::Char('e'),
            KeyCode::Enter,
            KeyCode::Char('T'),
        ] {
            app.on_key(key(code));
        }
        assert!(app.stopping.is_empty());
        assert!(app.take_pending_action().is_none());
        assert!(app.take_pending_signal().is_none());
        assert!(matches!(app.modal, Modal::None));
        assert!(app.prompt.is_none());
        assert!(app.terms.is_empty());
    }

    #[test]
    fn t_and_shift_k_signal_the_selected_proc() {
        let mut app = app_on_proc_row(&["10"]);
        app.on_key(key(KeyCode::Char('t')));
        assert_eq!(
            app.take_pending_signal(),
            Some(PendingSignal {
                instance: "inst0".into(),
                pid: "10".into(),
                signal: Signal::Term,
            }),
        );
        app.on_key(key(KeyCode::Char('K')));
        assert_eq!(
            app.take_pending_signal(),
            Some(PendingSignal {
                instance: "inst0".into(),
                pid: "10".into(),
                signal: Signal::Kill,
            }),
        );
    }

    #[test]
    fn signal_noop_on_message_placeholder_row() {
        use crate::tui::procs::ProcState;
        let mut app = new_app();
        app.set_snapshot(snapshot_with_status(1, running()));
        app.expanded_procs.insert("inst0".into());
        app.procs
            .insert("inst0".into(), ProcState::Message("(not running)".into()));
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        assert!(matches!(app.selected_node(), Some(Node::Proc { .. })));
        app.on_key(key(KeyCode::Char('t')));
        assert!(app.take_pending_signal().is_none());
    }

    #[test]
    fn help_bar_switches_to_signal_legend_on_proc_row() {
        let app = app_on_proc_row(&["10"]);
        assert!(app.on_proc_row());
        assert!(app.proc_row_signalable());
        let mut app2 = new_app();
        app2.set_snapshot(snapshot_with_status(1, running()));
        app2.on_key(key(KeyCode::Down));
        assert!(!app2.on_proc_row());
    }

    #[test]
    fn top_fallback_listing_is_not_signalable() {
        use crate::tui::procs::{ProcRow, ProcState};
        let mut app = new_app();
        app.set_snapshot(snapshot_with_status(1, running()));
        // Non-signalable rows: a host-side `top` fallback (pids aren't reachable
        // by `exec kill`).
        app.expanded_procs.insert("inst0".into());
        app.procs.insert(
            "inst0".into(),
            ProcState::Rows {
                rows: vec![ProcRow { pid: "660011".into(), depth: 0, args: "x".into() }],
                signalable: false,
                sessions: None,
            },
        );
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        assert!(app.on_proc_row());
        // Help bar drops the signal keys, and the keys are inert.
        assert!(!app.proc_row_signalable());
        app.on_key(key(KeyCode::Char('t')));
        app.on_key(key(KeyCode::Char('K')));
        assert!(app.take_pending_signal().is_none());
    }

    #[test]
    fn proc_fetch_targets_running_and_not_running() {
        use crate::tui::data::{ContainerStatus, InstanceRow, SandboxRow};
        let mut app = new_app();
        let mk = |name: &str, status: ContainerStatus| InstanceRow {
            name: name.into(),
            sandbox: "s".into(),
            container: format!("devsandbox-{name}"),
            status,
            uptime_secs: 0,
            cpu: None,
            mem: None,
            folder: "/f".into(),
            worktree: false,
            services: Vec::new(),
            workspace: "/w".into(),
            remote_user: None,
            remote_env_len: 0,
            base_folder: "/f".into(),
            drift: false,
            instance_id: String::new(),
            dispatcher: None,
            done: false,
            profile: None,
            profile_disallowed: false,
            browser_sandbox: None, github_repos: Vec::new(), build_notes: Vec::new(),
            phase: None,
            root: None,
            mesh_root: None,
            machine: None,
        };
        let snap = Snapshot {
            instances: vec![
                mk("up", ContainerStatus::Running("Up".into())),
                mk("down", ContainerStatus::Exited("Exited".into())),
            ],
            sandboxes: vec![SandboxRow {
                name: "s".into(),
                source: "image x".into(),
                folder: None,
                services: Vec::new(),
                extends: Vec::new(),
                config_hash: "h".into(),
                build_hash: String::new(),
                issues: Vec::new(),
                identity: Vec::new(),
                dispatcher: false,
                worktree_branch: None,
                profiles: Vec::new(),
                root: None,
                mesh_root: None,
                machine: None,
            }],
            services: Vec::new(),
            sandbox_count: 1,
            runtime_name: "docker".into(),
            runtime_version: None,
            collected_at: std::time::Instant::now(),
            depth: crate::snapshot::Depth::Full,
            error: None,
            no_runtime: false,
        };
        app.set_snapshot(snap);
        app.expanded_procs.insert("up".into());
        app.expanded_procs.insert("down".into());

        let targets = app.proc_fetch_targets();
        assert_eq!(targets, vec!["up".to_string()]);
        // Non-running instance got a (not running) message row, no fetch.
        assert_eq!(
            app.procs.get("down"),
            Some(&crate::tui::procs::ProcState::Message("(not running)".into())),
        );
    }

    #[test]
    fn run_rows_are_not_signalable() {
        let mut app = app_on_proc_row(&[crate::tui::procs::RUN_PID]);
        assert!(app.on_proc_row());
        assert!(!app.proc_row_signalable());
        app.on_key(key(KeyCode::Char('t')));
        assert!(app.take_pending_signal().is_none());
    }

    /// `K` on a session row ends it (its key's instance, machine included),
    /// `t` says what the row's keys are; neither signals.
    #[test]
    fn session_rows_end_on_shift_k_and_never_signal() {
        let mut app = new_app();
        app.set_snapshot(snapshot_with_status(1, running()));
        let mut app = select_session_row(app, "inst0", "1790000000-a1b2 attached bash -l");
        assert!(app.on_session_row() && !app.proc_row_signalable());
        app.on_key(key(KeyCode::Char('t')));
        assert_eq!(app.status.as_deref(), Some("a attaches, K ends the session"));
        assert!(app.take_pending_signal().is_none());
        app.on_key(key(KeyCode::Char('K')));
        assert_eq!(app.status.as_deref(), Some("ending session 1790000000-a1b2…"));
        assert_eq!(
            app.take_pending_session_rm(),
            Some(PendingSessionRm { instance: "inst0".into(), id: "1790000000-a1b2".into() })
        );
        assert!(app.take_pending_signal().is_none());

        let mut app = select_session_row(machine_app(), "box/inst1", "1790000000-a1b2 detached vim");
        app.on_key(key(KeyCode::Char('K')));
        assert_eq!(app.take_pending_session_rm().unwrap().instance, "box/inst1");

        let mut app = app_on_proc_row(&[crate::tui::procs::RUN_PID]);
        assert!(!app.on_session_row());
        app.on_key(key(KeyCode::Char('K')));
        assert!(app.take_pending_session_rm().is_none());
    }

    #[test]
    fn agent_count_counts_agent_rows_in_cache() {
        use crate::tui::procs::{ProcRow, ProcState};
        let mut app = new_app();
        assert_eq!(app.agent_count("x"), None);

        let rows = vec![
            ProcRow { pid: "1".into(), depth: 0, args: "/sbin/init".into() },
            ProcRow { pid: "2".into(), depth: 1, args: "node /usr/local/bin/claude".into() },
            ProcRow { pid: "3".into(), depth: 1, args: "claude --resume".into() },
            // A run row repeating an agent's argv isn't another agent.
            ProcRow {
                pid: crate::tui::procs::RUN_PID.into(),
                depth: 0,
                args: "1790000000-a1b2 running claude -p x".into(),
            },
            // Nor a session row.
            ProcRow {
                pid: crate::tui::procs::SESSION_PID.into(),
                depth: 0,
                args: "1790000001-a1b2 attached claude".into(),
            },
        ];
        app.procs.insert("x".into(), ProcState::Rows { rows, signalable: true, sessions: None });
        assert_eq!(app.agent_count("x"), Some(2));

        // A message state (not running / error) has no countable forest.
        app.procs.insert("y".into(), ProcState::Message("(not running)".into()));
        assert_eq!(app.agent_count("y"), None);
    }

    #[test]
    fn session_count_reads_the_cached_listing() {
        use crate::tui::procs::SessionCount;
        use crate::tui::procs::ProcState;
        let mut app = new_app();
        assert_eq!(app.session_count("x"), None);
        let count = SessionCount { running: 3, detached: 1 };
        app.procs.insert("x".into(), ProcState::Rows { rows: Vec::new(), signalable: true, sessions: Some(count) });
        assert_eq!(app.session_count("x"), Some(count));
        // An old helper (no `session ls`) and a message state: unknown.
        app.procs.insert("old".into(), ProcState::Rows { rows: Vec::new(), signalable: true, sessions: None });
        assert_eq!(app.session_count("old"), None);
        app.procs.insert("y".into(), ProcState::Message("(not running)".into()));
        assert_eq!(app.session_count("y"), None);
    }

    #[test]
    fn proc_fetch_targets_includes_selected_running_instance() {
        use crate::tui::data::{ContainerStatus, InstanceRow, SandboxRow};
        let mut app = new_app();
        let row = InstanceRow {
            name: "up".into(),
            sandbox: "s".into(),
            container: "devsandbox-up".into(),
            status: ContainerStatus::Running("Up".into()),
            uptime_secs: 0,
            cpu: None,
            mem: None,
            folder: "/f".into(),
            worktree: false,
            services: Vec::new(),
            workspace: "/w".into(),
            remote_user: None,
            remote_env_len: 0,
            base_folder: "/f".into(),
            drift: false,
            instance_id: String::new(),
            dispatcher: None,
            done: false,
            profile: None,
            profile_disallowed: false,
            browser_sandbox: None, github_repos: Vec::new(), build_notes: Vec::new(),
            phase: None,
            root: None,
            mesh_root: None,
            machine: None,
        };
        app.set_snapshot(Snapshot {
            instances: vec![row],
            sandboxes: vec![SandboxRow {
                name: "s".into(),
                source: "image x".into(),
                folder: None,
                services: Vec::new(),
                extends: Vec::new(),
                config_hash: "h".into(),
                build_hash: String::new(),
                issues: Vec::new(),
                identity: Vec::new(),
                dispatcher: false,
                worktree_branch: None,
                profiles: Vec::new(),
                root: None,
                mesh_root: None,
                machine: None,
            }],
            services: Vec::new(),
            sandbox_count: 1,
            runtime_name: "docker".into(),
            runtime_version: None,
            collected_at: std::time::Instant::now(),
            depth: crate::snapshot::Depth::Full,
            error: None,
            no_runtime: false,
        });

        // Cursor on the sandbox node: no instance selected, nothing expanded.
        assert!(app.proc_fetch_targets().is_empty());

        // Move onto the running instance: it becomes a fetch target for the
        // Detail agent count, without being expanded.
        app.on_key(key(KeyCode::Down));
        assert_eq!(app.proc_fetch_targets(), vec!["up".to_string()]);
        assert!(app.expanded_procs.is_empty());
    }
}