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
//! Instances-tree selection and navigation: clamping, collapse/expand, and
//! parent jumps.

use crate::tui::data::{Node, ORPHANS_NAME, group_key};
use crate::tui::remote::instance_key;

use super::{App, Tab};

impl App {
    pub(super) fn select_up(&mut self) {
        let slot = self.tab.index();
        self.selected[slot] = self.selected[slot].saturating_sub(1);
        // Moving the cursor may land on a new instance; fetch its procs now so
        // the Detail agent count appears without waiting for the next proc tick.
        self.needs_proc_fetch = true;
    }

    pub(super) fn select_down(&mut self) {
        let rows = self.row_count();
        if rows == 0 {
            return;
        }
        let slot = self.tab.index();
        self.selected[slot] = (self.selected[slot] + 1).min(rows - 1);
        self.needs_proc_fetch = true;
    }

    /// The collapse-set key for a collapsible node (its group's
    /// [`GroupKey::collapse_key`](crate::tui::data::GroupKey::collapse_key) / the orphan group),
    /// or `None` for leaf nodes (instances, empty markers).
    fn collapse_key(&self, node: Node) -> Option<String> {
        let snapshot = self.snapshot.as_ref()?;
        match node {
            Node::Sandbox(i) => snapshot.sandboxes.get(i).map(|r| group_key(r).collapse_key()),
            Node::Orphans => Some(ORPHANS_NAME.to_string()),
            Node::Instance(_) | Node::Empty(_) | Node::Proc { .. } => None,
        }
    }

    /// `→`: expand the node under the cursor. On a sandbox / orphan group this
    /// unfolds its children; on an instance it expands the process layer (and
    /// signals the event loop to fetch now). No-op on other leaves.
    pub(super) fn tree_expand(&mut self) {
        match self.selected_node() {
            Some(Node::Instance(_)) => self.expand_procs(),
            Some(node) => {
                if let Some(key) = self.collapse_key(node) {
                    self.collapsed.remove(&key);
                    self.clamp_selection();
                }
            }
            None => {}
        }
    }

    /// `space`: toggle the node under the cursor. Collapsible groups fold/unfold;
    /// an instance toggles its process layer.
    pub(super) fn tree_toggle(&mut self) {
        match self.selected_node() {
            Some(Node::Instance(_)) => {
                if let Some(name) = self.selected_instance_name() {
                    if !self.expanded_procs.remove(&name) {
                        self.expanded_procs.insert(name);
                        self.needs_proc_fetch = true;
                    }
                    self.clamp_selection();
                }
            }
            Some(node) => {
                if let Some(key) = self.collapse_key(node) {
                    if !self.collapsed.remove(&key) {
                        self.collapsed.insert(key);
                    }
                    self.clamp_selection();
                }
            }
            None => {}
        }
    }

    /// Mark the selected instance's process layer expanded and request a fetch.
    fn expand_procs(&mut self) {
        if let Some(name) = self.selected_instance_name() {
            if self.expanded_procs.insert(name) {
                self.needs_proc_fetch = true;
            }
            self.clamp_selection();
        }
    }

    /// `←`: fold one level. On a collapsible node collapse it. On a process row
    /// jump to its instance. On an instance with procs expanded collapse the
    /// procs (staying on the instance); otherwise jump to the parent sandbox.
    pub(super) fn tree_collapse(&mut self) {
        let Some(node) = self.selected_node() else {
            return;
        };
        match node {
            Node::Sandbox(_) | Node::Orphans => {
                if let Some(key) = self.collapse_key(node) {
                    self.collapsed.insert(key);
                    self.clamp_selection();
                }
            }
            Node::Proc { instance, .. } => {
                if let Some(pos) = self.instance_node_position(instance) {
                    self.selected[Tab::Instances.index()] = pos;
                }
            }
            Node::Instance(_) => {
                // First press collapses an expanded process layer (staying put);
                // otherwise fall through to the parent-sandbox jump.
                if let Some(name) = self.selected_instance_name() {
                    if self.expanded_procs.remove(&name) {
                        self.clamp_selection();
                        return;
                    }
                }
                if let Some(parent) = self.parent_index(self.selected[Tab::Instances.index()]) {
                    self.selected[Tab::Instances.index()] = parent;
                }
            }
            Node::Empty(_) => {
                if let Some(parent) = self.parent_index(self.selected[Tab::Instances.index()]) {
                    self.selected[Tab::Instances.index()] = parent;
                }
            }
        }
    }

    /// Instance index under the cursor: the instance row itself or the parent of
    /// a selected process row. `None` on sandbox / empty / orphan-group nodes.
    pub(super) fn selected_instance_index(&self) -> Option<usize> {
        match self.selected_node()? {
            Node::Instance(i) => Some(i),
            Node::Proc { instance, .. } => Some(instance),
            _ => None,
        }
    }

    /// Key of the instance under the cursor (its name; `box/web-2` on a
    /// machine, `remote::instance_key`), whether the selected node is the
    /// instance row itself or one of its process rows. `None` otherwise.
    pub(super) fn selected_instance_name(&self) -> Option<String> {
        let snapshot = self.snapshot.as_ref()?;
        let idx = self.selected_instance_index()?;
        snapshot.instances.get(idx).map(instance_key)
    }

    /// Visible-node position of `Node::Instance(instance)`, for jumping a process
    /// row's selection back onto its instance row.
    fn instance_node_position(&self, instance: usize) -> Option<usize> {
        self.visible_nodes()
            .iter()
            .position(|n| matches!(n, Node::Instance(i) if *i == instance))
    }

    /// Index of the enclosing group node (sandbox / orphan header) for the child
    /// at visible-node position `pos`: the nearest preceding `Sandbox`/`Orphans`.
    fn parent_index(&self, pos: usize) -> Option<usize> {
        let nodes = self.visible_nodes();
        nodes[..pos.min(nodes.len())]
            .iter()
            .rposition(|n| matches!(n, Node::Sandbox(_) | Node::Orphans))
    }
}

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

    #[test]
    fn selection_clamps() {
        let mut app = new_app();
        app.on_key(key(KeyCode::Up));
        assert_eq!(app.selected(), 0);

        // With zero placeholder rows, down never moves past 0.
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Char('j')));
        assert_eq!(app.selected(), 0);
    }

    #[test]
    fn selection_clamps_when_snapshot_shrinks() {
        let mut app = new_app();
        // Tree: [Sandbox(0), inst0, inst1, inst2] → 4 nodes.
        app.set_snapshot(snapshot_with(3));
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        assert_eq!(app.selected(), 3);

        app.set_snapshot(snapshot_with(1));
        assert_eq!(app.selected(), 1);

        // Empty sandbox → [Sandbox(0), Empty(0)] still has 2 rows; clamp to 1.
        app.set_snapshot(snapshot_with(0));
        assert_eq!(app.selected(), 1);
    }

    #[test]
    fn left_on_instance_jumps_to_parent_sandbox() {
        let mut app = new_app();
        app.set_snapshot(snapshot_with(2)); // [Sandbox(0), inst0, inst1]
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        assert_eq!(app.selected(), 2);
        assert_eq!(app.selected_node(), Some(Node::Instance(1)));

        app.on_key(key(KeyCode::Left));
        assert_eq!(app.selected(), 0);
        assert_eq!(app.selected_node(), Some(Node::Sandbox(0)));
    }

    #[test]
    fn collapse_hides_children_and_reclamps_selection() {
        let mut app = new_app();
        app.set_snapshot(snapshot_with(2)); // [Sandbox(0), inst0, inst1]
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        assert_eq!(app.selected(), 2);

        app.on_key(key(KeyCode::Left));
        assert_eq!(app.selected(), 0);
        app.on_key(key(KeyCode::Left));
        assert_eq!(app.visible_nodes(), vec![Node::Sandbox(0)]);
        assert_eq!(app.selected(), 0);

        app.on_key(key(KeyCode::Char(' ')));
        assert_eq!(
            app.visible_nodes(),
            vec![Node::Sandbox(0), Node::Instance(0), Node::Instance(1)],
        );
    }

    #[test]
    fn collapse_survives_snapshot_refresh_and_clamps() {
        let mut app = new_app();
        app.set_snapshot(snapshot_with(3)); // 4 nodes
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        assert_eq!(app.selected(), 3);
        app.on_key(key(KeyCode::Left)); // to sandbox, pos 0
        app.on_key(key(KeyCode::Left));
        assert_eq!(app.visible_nodes().len(), 1);

        // A refresh keeps the collapse and re-clamps (still 1 node).
        app.set_snapshot(snapshot_with(3));
        assert_eq!(app.visible_nodes().len(), 1);
        assert_eq!(app.selected(), 0);
    }

    #[test]
    fn right_on_instance_expands_procs_and_signals_fetch() {
        let mut app = new_app();
        app.set_snapshot(snapshot_with(1)); // [Sandbox(0), inst0]
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Right));
        assert!(app.expanded_procs.contains("inst0"));
        assert!(app.take_needs_proc_fetch());
        assert!(!app.take_needs_proc_fetch());
    }

    #[test]
    fn space_toggles_procs_on_instance() {
        let mut app = new_app();
        app.set_snapshot(snapshot_with(1));
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Char(' ')));
        assert!(app.expanded_procs.contains("inst0"));
        app.on_key(key(KeyCode::Char(' ')));
        assert!(!app.expanded_procs.contains("inst0"));
    }

    #[test]
    fn left_ladder_proc_to_instance_to_sandbox() {
        let mut app = new_app();
        app.set_snapshot(snapshot_with(1)); // [Sandbox(0), inst0]
        expand_with_rows(&mut app, "inst0", &["10", "20"]);
        // Tree: [Sandbox(0), Instance(0), Proc r0, Proc r1].
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        app.on_key(key(KeyCode::Down));
        assert_eq!(app.selected_node(), Some(Node::Proc { instance: 0, row: 1 }));

        app.on_key(key(KeyCode::Left));
        assert_eq!(app.selected_node(), Some(Node::Instance(0)));
        assert!(app.expanded_procs.contains("inst0"));

        app.on_key(key(KeyCode::Left));
        assert_eq!(app.selected_node(), Some(Node::Instance(0)));
        assert!(!app.expanded_procs.contains("inst0"));

        app.on_key(key(KeyCode::Left));
        assert_eq!(app.selected_node(), Some(Node::Sandbox(0)));
    }

    /// `machine_app` with both hosts' rows signed as mesh root `tree`.
    fn mesh_app() -> App {
        let mut app = machine_app();
        let mut s = app.snapshot.clone().unwrap();
        for r in &mut s.sandboxes {
            r.mesh_root = Some("tree".into());
        }
        for r in &mut s.instances {
            r.mesh_root = Some("tree".into());
        }
        app.set_snapshot(s);
        app
    }

    /// Unsigned roots: the same sandbox and instance names on two machines
    /// are two groups, the machine's after this host's, folded on their own.
    #[test]
    fn unsigned_roots_on_two_machines_are_separate_groups() {
        let mut app = machine_app();
        let tree = [Node::Sandbox(0), Node::Instance(0), Node::Instance(1), Node::Sandbox(1), Node::Instance(2), Node::Instance(3)];
        assert_eq!(app.visible_nodes(), tree);
        // A machine's sandboxes go after this host's whatever the order.
        let mut s = app.snapshot.clone().unwrap();
        s.sandboxes.reverse();
        s.instances.reverse();
        app.set_snapshot(s);
        assert_eq!(app.visible_nodes()[0], Node::Sandbox(1));
        app.set_snapshot(machine_app().snapshot.unwrap());

        // Fold box's `s`: the local one stays open.
        app.selected[Tab::Instances.index()] = 3;
        app.on_key(key(KeyCode::Char(' ')));
        let boxed = crate::tui::data::group_key(&app.snapshot.as_ref().unwrap().sandboxes[1]).collapse_key();
        assert!(app.collapsed.contains(&boxed));
        assert_eq!(app.visible_nodes(), tree[..4]);
    }

    /// One mesh root's rows on this host and on `box` are one group: the
    /// local row heads it, both hosts' instances under it, whatever order
    /// the rows come in; it folds as one.
    #[test]
    fn a_mesh_roots_rows_merge_across_machines() {
        let mut app = mesh_app();
        let tree = [Node::Sandbox(0), Node::Instance(0), Node::Instance(1), Node::Instance(2), Node::Instance(3)];
        assert_eq!(app.visible_nodes(), tree);
        let mut s = app.snapshot.clone().unwrap();
        s.sandboxes.reverse();
        app.set_snapshot(s);
        // The local row (now index 1) still heads the group.
        assert_eq!(app.visible_nodes()[..2], [Node::Sandbox(1), Node::Instance(0)]);
        assert_eq!(app.visible_nodes().len(), 5);
        let mut app = mesh_app();

        // Fold the group: every host's instances hide.
        app.on_key(key(KeyCode::Char(' ')));
        let mesh = crate::tui::data::GroupKey::Mesh { root: "tree".into(), sandbox: "s".into() };
        assert!(app.collapsed.contains(&mesh.collapse_key()));
        assert_eq!(app.visible_nodes(), [Node::Sandbox(0)]);
        app.on_key(key(KeyCode::Char(' ')));

        // box/inst0's processes are its own.
        app.selected[Tab::Instances.index()] = 3;
        assert_eq!(app.selected_instance_name().as_deref(), Some("box/inst0"));
        app.on_key(key(KeyCode::Right));
        assert!(app.expanded_procs.contains("box/inst0") && !app.expanded_procs.contains("inst0"));
        assert_eq!(app.visible_nodes()[4], Node::Proc { instance: 2, row: crate::tui::procs::MESSAGE_ROW });
        // Left from box/inst0 (procs folded) goes to the merged header.
        app.on_key(key(KeyCode::Left));
        app.on_key(key(KeyCode::Left));
        assert_eq!(app.selected_node(), Some(Node::Sandbox(0)));
    }

    /// Two roots on one host defining `s` are two groups, each with its
    /// own instances.
    #[test]
    fn two_roots_on_one_host_are_separate_groups() {
        let mut app = machine_app();
        let mut s = app.snapshot.clone().unwrap();
        // box's second instance came from a second root there.
        let mut second = s.sandboxes[1].clone();
        second.root = Some("/r/two".into());
        s.sandboxes.push(second);
        s.instances[3].root = Some("/r/two".into());
        app.set_snapshot(s);
        assert_eq!(
            app.visible_nodes(),
            [Node::Sandbox(0), Node::Instance(0), Node::Instance(1), Node::Sandbox(1), Node::Instance(2), Node::Sandbox(2), Node::Instance(3)]
        );
    }

    /// An instance with no recorded root joins the first mesh group in
    /// tree order defining its sandbox, not its host's unsigned row.
    #[test]
    fn a_rootless_instance_backfills_into_the_mesh_group() {
        let mut app = machine_app();
        let mut s = app.snapshot.clone().unwrap();
        // Only the local root is signed; box's is unsigned.
        s.sandboxes[0].mesh_root = Some("tree".into());
        s.instances[0].mesh_root = Some("tree".into());
        s.instances[1].mesh_root = Some("tree".into());
        s.instances[3].root = None;
        app.set_snapshot(s);
        assert_eq!(
            app.visible_nodes(),
            [Node::Sandbox(0), Node::Instance(0), Node::Instance(1), Node::Instance(3), Node::Sandbox(1), Node::Instance(2)]
        );
    }
}