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
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
//! CLI re-runs on a machine (docs/architecture/daemon/machines.md, *The
//! CLI*; fleet plan decision 15): API calls through this host's daemon with
//! `machine` set (the quick calls, `machines::forward::QUICK`), which go
//! over the machine's link and its trust table, never a `devsandbox …` run
//! there; printed by the local commands' own render fns so the output reads
//! as theirs. What has no such call yet is refused ([`refusal`]).
//!
//! [`use_of`] is the pure choice; [`named`], [`per_root`] and [`listing`] make the calls
//! through an injected [`Call`] and write to injected streams, so the tests
//! feed them fixture answers ([`follow_build_log`] sleeps through an
//! injected fn too, [`follow_next`] its pure step).

use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;

use anyhow::{Context, Result, bail};
use serde::de::DeserializeOwned;
use serde_json::{Value, json};

use crate::serve::api::{ContainerLogs, InstancesList, MAX_LINES, ProfileSet, Renamed};
use crate::serve::proto::Response;
use crate::snapshot::{InstanceRow, SandboxRow};
use crate::{Command, ServiceCommand};

/// One daemon request: method, params → its response.
pub(crate) type Call<'a> = dyn FnMut(&str, Value) -> Result<Response> + 'a;

/// What a command does on a machine.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Use {
    /// `logs` (no `--build`): `containers.logs` of the instance's container.
    Logs { lines: usize },
    /// `logs --build`: `instances.buildLog`; `--follow`, its pages
    /// ([`follow_build_log`]).
    BuildLog { follow: bool },
    /// `inspect`: `containers.inspect`.
    Inspect { json: bool },
    /// `done` (`true`) / `undone`: `instances.done`.
    Done(bool),
    /// `rename`: `instances.rename` to this name.
    Rename(String),
    /// `profile`: `instances.setProfile` to this profile; without one, the
    /// instance's row (`instances.list`), which carries its profile and the
    /// not-allowed flag but not its sandbox's allowed set.
    Profile(Option<String>),
    /// `--machine m ls`: `instances.list` per config root, its sandboxes.
    Ls { json: bool },
    /// `--machine m service ls`: `instances.list` per config root, its services.
    ServiceLs { json: bool },
    /// `--machine m ps`: one `instances.list`.
    Ps { all: bool, json: bool },
    /// Not an API call made here: what goes over the link's streams
    /// (`exec`, its sessions, `port`) and the verbs (`gc` among them, sent
    /// by `verbs_client`).
    Link,
    Refused(Refused),
}

/// What a machine refuses.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Refused {
    Vscode,
}

/// [`Use`] of `command` on a machine. Every `exec` is [`Use::Link`] here:
/// whether it gets a tty, so a session `PTY` dial or a piped one, is the
/// caller's to tell (`cli::reconnect_session`, `cli::pipe_mode`).
pub(crate) fn use_of(command: &Command) -> Use {
    match command {
        Command::Logs { build: true, follow, .. } => Use::BuildLog { follow: *follow },
        Command::Logs { lines, .. } => Use::Logs { lines: *lines },
        Command::Inspect { json, .. } => Use::Inspect { json: *json },
        Command::Done { .. } => Use::Done(true),
        Command::Undone { .. } => Use::Done(false),
        Command::Rename { new_name, .. } => Use::Rename(new_name.clone()),
        Command::Profile { profile, .. } => Use::Profile(profile.clone()),
        Command::Ls { json } => Use::Ls { json: *json },
        Command::Ps { all, json } => Use::Ps { all: *all, json: *json },
        Command::Service { cmd: ServiceCommand::Ls { json } } => Use::ServiceLs { json: *json },
        Command::Vscode { .. } => Use::Refused(Refused::Vscode),
        _ => Use::Link,
    }
}

/// Why machine `m` refuses `what`.
pub(crate) fn refusal(m: &str, what: Refused) -> anyhow::Error {
    let what = match what {
        Refused::Vscode => "`devsandbox vscode`",
    };
    anyhow::anyhow!("machine `{m}`: {what} isn't available over its link yet")
}

/// An error answer of machine `m` as ours: its message, naming `m` when
/// it's the machine's trust refusing (`denied`), whose advice (`devsandbox
/// machine trust full` there, `machine roots add`) is about that machine.
fn answer_error(m: &str, e: crate::serve::proto::ErrorBody) -> anyhow::Error {
    match e.code.as_str() {
        "denied" => anyhow::anyhow!("machine `{m}`: {}", e.message),
        _ => anyhow::anyhow!("{}", e.message),
    }
}

/// `method` with `params` (plus `machine: m`) through `call`: its result, or
/// its error as ours ([`answer_error`]).
fn call_on<T: DeserializeOwned>(call: &mut Call, m: &str, method: &str, mut params: Value) -> Result<T> {
    params["machine"] = json!(m);
    let r = call(method, params)?;
    if let Some(e) = r.error {
        return Err(answer_error(m, e));
    }
    serde_json::from_value(r.result.unwrap_or(Value::Null)).with_context(|| format!("bad {method} result from devsandbox serve"))
}

fn list(call: &mut Call, m: &str, dir: &Path, depth: Option<&str>) -> Result<InstancesList> {
    let mut params = json!({ "dir": dir });
    if let Some(depth) = depth {
        params["depth"] = json!(depth);
    }
    call_on(call, m, "instances.list", params)
}

/// Instance `name`'s row on `m`, from a listing at `root` (any root there:
/// a snapshot lists every instance the machine's trust admits, whatever its
/// `dir`).
fn row_of(call: &mut Call, m: &str, root: &Path, name: &str) -> Result<Option<InstanceRow>> {
    Ok(list(call, m, root, Some("disk"))?.snapshot.instances.into_iter().find(|r| r.name == name))
}

/// The profiles `row`'s sandbox allows, from the same listing's sandbox
/// row (`SandboxRow::profiles`; the one of the instance's own root when
/// several roots define it, else the first), or why that can't be told:
/// `devsandbox profile`'s `allowed:` line, as the local form prints it.
fn allowed_of(row: &InstanceRow, sandboxes: &[SandboxRow]) -> Result<Vec<String>, String> {
    let named: Vec<&SandboxRow> = sandboxes.iter().filter(|s| s.name == row.sandbox).collect();
    let own = named.iter().find(|s| row.root.is_some() && s.root == row.root).or(named.first());
    match own {
        Some(s) if !s.config_hash.is_empty() => Ok(s.profiles.clone()),
        _ => Err(format!("sandbox `{}` isn't in its config any more", row.sandbox)),
    }
}

/// The container of `name`: its row's (a rename keeps the container), else
/// `name` taken as a container name, as `commands::resolve_container`
/// matches one (with or without the prefix).
fn container_of(row: Option<&InstanceRow>, name: &str) -> String {
    match row {
        Some(row) => row.container.clone(),
        None if name.starts_with(crate::runtime::NAME_PREFIX) => name.to_string(),
        None => format!("{}{name}", crate::runtime::NAME_PREFIX),
    }
}

/// Run named `u` (not [`Use::Ls`] / `Ps` / `Link` / `Refused`) on
/// instance or container `name` of machine `m`, `root` its config
/// root there (or the machine's root). `tty`: stdout is a terminal (a pty
/// build log keeps its colours). The exit code.
pub(crate) fn named(call: &mut Call, u: &Use, m: &str, name: &str, root: &Path, tty: bool, out: &mut dyn Write, err: &mut dyn Write) -> Result<i32> {
    match u {
        Use::Logs { lines } => {
            let container = container_of(row_of(call, m, root, name)?.as_ref(), name);
            let logs: ContainerLogs = call_on(call, m, "containers.logs", json!({ "container": container, "lines": lines }))?;
            writeln!(out, "{}", crate::commands::logs::render_logs(&logs.text))?;
        }
        Use::BuildLog { follow: true } => {
            // By id: a rename while it's followed doesn't lose it.
            let id = row_of(call, m, root, name)?.map_or_else(|| name.to_string(), |r| r.instance_id);
            follow_build_log(call, m, name, &id, tty, out, &mut std::thread::sleep)?;
        }
        Use::BuildLog { follow: false } => {
            // The whole log, as `logs --build` prints it, up to the API's cap.
            let tail: Option<crate::build_log::Tail> =
                call_on(call, m, "instances.buildLog", json!({ "instance": name, "lines": MAX_LINES, "ansi": tty }))?;
            let Some(tail) = tail else { bail!("`{m}/{name}` has no build log") };
            if tail.cut {
                writeln!(err, "(the last {MAX_LINES} lines of `{m}/{name}`'s build log)")?;
            }
            out.write_all(tail.text.as_bytes())?;
            if !tail.text.is_empty() && !tail.text.ends_with('\n') {
                writeln!(out)?;
            }
        }
        Use::Inspect { json } => {
            let container = container_of(row_of(call, m, root, name)?.as_ref(), name);
            let doc: Value = call_on(call, m, "containers.inspect", json!({ "container": container }))?;
            writeln!(out, "{}", crate::commands::inspect::render(&doc.to_string(), *json))?;
        }
        Use::Done(done) => {
            // The line says whether it changed: the flag as the rows had it.
            let was = row_of(call, m, root, name)?.map(|r| r.done).unwrap_or(!*done);
            let set: crate::serve::api::DoneSet = call_on(call, m, "instances.done", json!({ "instance": name, "done": done }))?;
            // A placed instance whose fetch-back (this host's daemon's) failed.
            if let Some(warning) = &set.warning {
                writeln!(err, "warning: {warning}")?;
            }
            writeln!(out, "{}", crate::commands::done::status_line(name, *done, was))?;
        }
        Use::Rename(new_name) => {
            let r: Renamed = call_on(call, m, "instances.rename", json!({ "instance": name, "newName": new_name }))?;
            writeln!(out, "{}", crate::commands::rename::renamed_line(name, &r.instance))?;
        }
        Use::Profile(Some(profile)) => {
            let r: ProfileSet = call_on(call, m, "instances.setProfile", json!({ "instance": name, "profile": profile }))?;
            writeln!(out, "{}", crate::commands::profile::profile_line(name, Some(&r.profile), false))?;
        }
        Use::Profile(None) => {
            // Exact name, as the local form and `instances.setProfile` take it.
            let snapshot = list(call, m, root, Some("disk"))?.snapshot;
            let Some(row) = snapshot.instances.iter().find(|r| r.name == *name) else {
                bail!("no instance named `{name}` on machine `{m}` (see `devsandbox ps -a`)");
            };
            let allowed = allowed_of(row, &snapshot.sandboxes);
            for line in crate::commands::profile::show_lines(name, row.profile.as_deref(), row.profile_disallowed, Some(&allowed)) {
                writeln!(out, "{line}")?;
            }
        }
        Use::Ls { .. } | Use::ServiceLs { .. } | Use::Ps { .. } | Use::Link | Use::Refused(_) => {
            bail!("not a named re-run")
        }
    }
    Ok(0)
}

/// What [`follow_build_log`] does after a page read at `offset`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Follow {
    /// Ask for the next page now: this one brought something.
    Fetch,
    /// Nothing new yet: ask again after `FOLLOW_POLL`.
    Wait,
    /// The log is whole and printed.
    Done,
}

/// [`Follow`] after `page`, asked for at `offset`.
pub(crate) fn follow_next(page: &crate::build_log::Page, offset: u64) -> Follow {
    if page.complete {
        Follow::Done
    } else if page.next == offset {
        Follow::Wait
    } else {
        Follow::Fetch
    }
}

/// `logs --build --follow` of instance `name` (`id`: its `instance_id`, or
/// the name) on machine `m`: `instances.buildLog` pages from the
/// log's start until one is complete, printed as `commands::logs::build_log`
/// follows a local one (as they are on a terminal, `tty`; a pty log's
/// escapes stripped as they come elsewhere, `build_log::Strip`). The record
/// gone meanwhile (`not-found`, no log) ends it, as locally.
pub(crate) fn follow_build_log(
    call: &mut Call,
    m: &str,
    name: &str,
    id: &str,
    tty: bool,
    out: &mut dyn Write,
    sleep: &mut dyn FnMut(Duration),
) -> Result<()> {
    let mut offset = 0;
    let mut strip: Option<Option<crate::build_log::Strip>> = None;
    loop {
        let r = call("instances.buildLog", json!({ "instance": id, "offset": offset, "machine": m }))?;
        let page: Option<crate::build_log::Page> = match r.error {
            Some(e) if strip.is_some() && e.code == "not-found" => return Ok(()),
            Some(e) => return Err(answer_error(m, e)),
            None => serde_json::from_value(r.result.unwrap_or(Value::Null))
                .context("bad instances.buildLog result from devsandbox serve")?,
        };
        let Some(page) = page else {
            if strip.is_none() {
                bail!("`{m}/{name}` has no build log");
            }
            return Ok(());
        };
        let strip = strip.get_or_insert_with(|| (!tty && page.tty).then(crate::build_log::Strip::default));
        match strip {
            Some(strip) => out.write_all(&strip.feed(page.text.as_bytes()))?,
            None => out.write_all(page.text.as_bytes())?,
        }
        out.flush()?;
        let next = follow_next(&page, offset);
        offset = page.next;
        match next {
            Follow::Done => return Ok(()),
            Follow::Wait => sleep(crate::commands::logs::FOLLOW_POLL),
            Follow::Fetch => {}
        }
    }
}

/// `--machine m ps` (`u`) on machine `m`: one `instances.list` at `root`
/// (any of its roots: a snapshot lists every instance the machine's trust
/// admits, whatever its `dir`), printed by the local command's render. The
/// exit code.
pub(crate) fn listing(call: &mut Call, u: &Use, m: &str, root: &Path, out: &mut dyn Write) -> Result<i32> {
    let Use::Ps { all, json } = u else { bail!("not a listing re-run") };
    let snapshot = list(call, m, root, None)?.snapshot;
    write!(out, "{}", crate::commands::ps::render(snapshot, &[], *all, *json)?)?;
    Ok(0)
}

/// The config roots a machine root `at` stands for, from a listing of it:
/// `at` itself when it is one (a root the machine lists of its own), and the
/// config roots under it its instance rows name (a fleet root's, one per
/// placed config; a listing lists every admitted instance, so rows outside
/// `at` are other roots'). A root without a config of its own (the
/// listing's `config: ` error) stands only for those under it, when it has
/// any.
pub(crate) fn config_roots(at: &Path, list: &InstancesList) -> Vec<PathBuf> {
    let mut roots: Vec<PathBuf> = list
        .snapshot
        .instances
        .iter()
        .filter_map(|r| r.root.as_ref().map(PathBuf::from))
        .filter(|r| r.starts_with(at))
        .collect();
    let configless = list.snapshot.error.as_deref().is_some_and(|e| e.contains("config: "));
    if roots.is_empty() || !configless {
        roots.push(at.to_path_buf());
    }
    roots.sort();
    roots.dedup();
    roots
}

/// `--machine m ls|service ls` (`u`) on machine `m` with listed
/// `roots`: one `instances.list` per config root ([`config_roots`]), each
/// after its banner on `err` (`cli::root_banners`' rules: `--json` with
/// several roots is an error). The exit code.
pub(crate) fn per_root(call: &mut Call, u: &Use, m: &str, roots: &[PathBuf], out: &mut dyn Write, err: &mut dyn Write) -> Result<i32> {
    let json = match u {
        Use::Ls { json } | Use::ServiceLs { json } => *json,
        _ => bail!("not a per-root re-run"),
    };
    let mut config = Vec::new();
    for at in roots {
        config.extend(config_roots(at, &list(call, m, at, Some("disk"))?));
    }
    config.sort();
    config.dedup();
    let banners = super::root_banners(m, &config, json)?;
    for (root, banner) in config.iter().zip(banners) {
        writeln!(err, "{banner}")?;
        match u {
            Use::ServiceLs { json } => {
                // A listing for the statuses; `used_by` already cut to what the trust admits.
                let snapshot = list(call, m, root, Some("listing"))?.snapshot;
                if let Some(e) = snapshot.error {
                    if snapshot.services.is_empty() && e.contains("config: ") {
                        // Where the local `service ls` fails loading the config.
                        bail!("{m}:{}: {e}", root.display());
                    }
                    // The runtime's and the resolve failures, as the local one's stderr.
                    writeln!(err, "{e}")?;
                }
                let config = root.join(crate::config::CONFIG_FILE);
                write!(out, "{}", crate::commands::services::ls_render(&snapshot.services, *json, &config)?)?;
            }
            _ => {
                let snapshot = list(call, m, root, Some("disk"))?.snapshot;
                if json {
                    writeln!(out, "{}", crate::commands::ls::render_json(&snapshot.sandboxes)?)?;
                } else if !snapshot.sandboxes.is_empty() {
                    let lines: Vec<_> = snapshot.sandboxes.iter().map(crate::commands::ls::Line::of_row).collect();
                    write!(out, "{}", crate::commands::ls::table(&lines))?;
                } else if let Some(e) = snapshot.error.filter(|e| e.contains("config: ")) {
                    // Where the local `ls` fails loading the config.
                    bail!("{m}:{}: {e}", root.display());
                } else {
                    writeln!(out, "{}", crate::commands::ls::none_defined(root))?;
                }
            }
        }
    }
    Ok(0)
}

#[cfg(test)]
mod tests {
    use clap::Parser;

    use super::*;
    use crate::Cli;
    use crate::serve::proto::ErrorBody;

    fn command(args: &[&str]) -> Command {
        Cli::try_parse_from(std::iter::once("devsandbox").chain(args.iter().copied())).unwrap_or_else(|e| panic!("{e}")).command.unwrap()
    }

    #[test]
    fn each_command_maps_to_a_call_or_a_refusal() {
        let u = |args: &[&str]| use_of(&command(args));
        assert_eq!(u(&["logs", "lap/web"]), Use::Logs { lines: 50 });
        assert_eq!(u(&["logs", "-n", "7", "lap/web"]), Use::Logs { lines: 7 });
        assert_eq!(u(&["logs", "--build", "lap/web"]), Use::BuildLog { follow: false });
        assert_eq!(u(&["logs", "--build", "-f", "lap/web"]), Use::BuildLog { follow: true });
        assert_eq!(u(&["inspect", "lap/web"]), Use::Inspect { json: false });
        assert_eq!(u(&["inspect", "--json", "lap/web"]), Use::Inspect { json: true });
        assert_eq!(u(&["done", "lap/web"]), Use::Done(true));
        assert_eq!(u(&["undone", "lap/web"]), Use::Done(false));
        assert_eq!(u(&["rename", "lap/web", "api"]), Use::Rename("api".into()));
        assert_eq!(u(&["profile", "lap/web", "work"]), Use::Profile(Some("work".into())));
        assert_eq!(u(&["profile", "lap/web"]), Use::Profile(None));
        assert_eq!(u(&["ls"]), Use::Ls { json: false });
        assert_eq!(u(&["ls", "--json"]), Use::Ls { json: true });
        assert_eq!(u(&["ps", "-a"]), Use::Ps { all: true, json: false });
        assert_eq!(u(&["ps", "--json"]), Use::Ps { all: false, json: true });
        // `gc` is a verb: `verbs_client` sends it.
        assert_eq!(u(&["gc", "--force"]), Use::Link);
        assert_eq!(u(&["service", "ls"]), Use::ServiceLs { json: false });
        assert_eq!(u(&["service", "ls", "--json"]), Use::ServiceLs { json: true });
        assert_eq!(u(&["vscode", "lap/web"]), Use::Refused(Refused::Vscode));
        for args in [&["exec", "lap/web"][..], &["exec", "lap/web", "--", "ls"], &["port", "lap/web", "3000"], &["stop", "lap/web"]] {
            assert_eq!(u(args), Use::Link, "{args:?}");
        }
    }

    #[test]
    fn refusals_name_the_command_and_why() {
        let r = |what| refusal("lap", what).to_string();
        assert_eq!(r(Refused::Vscode), "machine `lap`: `devsandbox vscode` isn't available over its link yet");
    }

    fn row(name: &str, container: &str, root: &str, done: bool) -> Value {
        json!({"name": name, "sandbox": "web", "container": container, "status": {"state": "running", "text": "Up 3 minutes"}, "uptime_secs": 0,
            "cpu": null, "mem": null, "folder": "", "worktree": false, "services": [], "workspace": "",
            "remote_user": null, "remote_env_len": 0, "base_folder": "", "drift": false, "instance_id": "id",
            "done": done, "root": root})
    }

    fn sandbox(name: &str, source: &str, folder: Option<&str>, services: &[&str], extends: &[&str]) -> Value {
        json!({"name": name, "source": source, "folder": folder, "services": services, "extends": extends,
            "config_hash": "h", "build_hash": "", "issues": [], "dispatcher": false, "worktree_branch": null, "root": "/f/cfg"})
    }

    fn listing(rows: Vec<Value>, sandboxes: Vec<Value>, error: Option<&str>) -> Value {
        json!({"instances": rows, "sandboxes": sandboxes, "services": [], "sandbox_count": 0, "runtime_name": "docker",
            "runtime_version": null, "error": error})
    }

    /// A fake daemon: each call recorded, answered by `answer`.
    struct Fake<F> {
        calls: Vec<(String, Value)>,
        answer: F,
    }

    impl<F: FnMut(&str, &Value) -> Result<Value, (&'static str, String)>> Fake<F> {
        fn call(&mut self, method: &str, params: Value) -> Result<Response> {
            let r = (self.answer)(method, &params);
            self.calls.push((method.to_string(), params));
            Ok(match r {
                Ok(v) => Response { id: None, result: Some(v), error: None },
                Err((code, message)) => {
                    Response { id: None, result: None, error: Some(ErrorBody { code: code.into(), message, data: Default::default() }) }
                }
            })
        }
    }

    fn fake<F: FnMut(&str, &Value) -> Result<Value, (&'static str, String)>>(answer: F) -> Fake<F> {
        Fake { calls: Vec::new(), answer }
    }

    fn run_named(u: Use, name: &str, answer: impl FnMut(&str, &Value) -> Result<Value, (&'static str, String)>) -> (Result<i32>, String, String, Vec<(String, Value)>) {
        let mut f = fake(answer);
        let (mut out, mut err) = (Vec::new(), Vec::new());
        let r = named(&mut |m, p| f.call(m, p), &u, "lap", name, Path::new("/f/cfg"), false, &mut out, &mut err);
        (r, String::from_utf8(out).unwrap(), String::from_utf8(err).unwrap(), f.calls)
    }

    /// The renamed instance keeps its container: logs and inspect ask for
    /// the row's, and print what the local commands print.
    #[test]
    fn logs_and_inspect_print_as_the_local_commands() {
        let rows = || listing(vec![row("web", "devsandbox-old", "/f/cfg", false)], vec![], None);
        let (r, out, _, calls) = run_named(Use::Logs { lines: 7 }, "web", |m, _| {
            Ok(if m == "instances.list" { rows() } else { json!({"text": "a\nb"}) })
        });
        assert_eq!(r.unwrap(), 0);
        assert_eq!(out, format!("{}\n", crate::commands::logs::render_logs("a\nb")));
        assert_eq!(calls[0], ("instances.list".into(), json!({"dir": "/f/cfg", "depth": "disk", "machine": "lap"})));
        assert_eq!(calls[1], ("containers.logs".into(), json!({"container": "devsandbox-old", "lines": 7, "machine": "lap"})));

        let (_, out, ..) = run_named(Use::Logs { lines: 50 }, "web", |m, _| Ok(if m == "instances.list" { rows() } else { json!({"text": " \n"}) }));
        assert_eq!(out, "(no log output)\n");

        let doc = json!([{"Id": "abc", "State": {"Running": true}}]);
        for json in [false, true] {
            let (_, out, _, calls) =
                run_named(Use::Inspect { json }, "web", |m, _| Ok(if m == "instances.list" { rows() } else { doc.clone() }));
            assert_eq!(out, format!("{}\n", crate::commands::inspect::render(&doc.to_string(), json)));
            assert_eq!(calls[1].1, json!({"container": "devsandbox-old", "machine": "lap"}));
        }
        // A container name, not an instance: as typed, prefixed if need be.
        let (_, _, _, calls) = run_named(Use::Inspect { json: true }, "x", |m, _| Ok(if m == "instances.list" { rows() } else { json!({}) }));
        assert_eq!(calls[1].1["container"], "devsandbox-x");
    }

    #[test]
    fn build_logs_print_the_tail_or_say_there_is_none() {
        let (r, out, err, calls) = run_named(Use::BuildLog { follow: false }, "web", |_, _| Ok(json!({"text": "$ run\nok", "cut": true})));
        assert_eq!((r.unwrap(), out.as_str()), (0, "$ run\nok\n"));
        assert_eq!(err, format!("(the last {MAX_LINES} lines of `lap/web`'s build log)\n"));
        assert_eq!(calls, [("instances.buildLog".into(), json!({"instance": "web", "lines": MAX_LINES, "ansi": false, "machine": "lap"}))]);
        let (r, ..) = run_named(Use::BuildLog { follow: false }, "web", |_, _| Ok(Value::Null));
        assert_eq!(r.unwrap_err().to_string(), "`lap/web` has no build log");
        let (r, ..) = run_named(Use::BuildLog { follow: false }, "web", |_, _| Err(("not-found", "no instance `web`".into())));
        assert_eq!(r.unwrap_err().to_string(), "no instance `web`");
    }

    fn page(text: &str, next: u64, tty: bool, complete: bool) -> crate::build_log::Page {
        crate::build_log::Page { text: text.into(), next, tty, complete }
    }

    #[test]
    fn a_follow_fetches_while_pages_bring_something_and_waits_when_none_do() {
        assert_eq!(follow_next(&page("abc", 3, false, false), 0), Follow::Fetch);
        assert_eq!(follow_next(&page("", 3, false, false), 3), Follow::Wait);
        assert_eq!(follow_next(&page("abc", 6, false, true), 3), Follow::Done);
        assert_eq!(follow_next(&page("", 6, false, true), 6), Follow::Done);
    }

    /// `answers` in order (the last one again once they run out): what the
    /// follow printed, its calls' params, its sleeps and its result.
    fn run_follow(tty: bool, answers: Vec<Result<Value, (&'static str, String)>>) -> (Result<()>, String, Vec<Value>, usize) {
        let mut answers = answers.into_iter();
        let mut last = None;
        let mut f = fake(|m: &str, _: &Value| {
            assert_eq!(m, "instances.buildLog");
            match answers.next() {
                Some(a) => {
                    last = Some(a.clone());
                    a
                }
                None => last.clone().unwrap(),
            }
        });
        let (mut out, mut sleeps) = (Vec::new(), 0);
        let r = follow_build_log(&mut |m, p| f.call(m, p), "lap", "web", "id-web", tty, &mut out, &mut |d| {
            assert_eq!(d, crate::commands::logs::FOLLOW_POLL);
            sleeps += 1;
        });
        (r, String::from_utf8(out).unwrap(), f.calls.into_iter().map(|(_, p)| p).collect(), sleeps)
    }

    #[test]
    fn a_followed_build_log_prints_its_pages_until_one_is_complete() {
        let v = |p: crate::build_log::Page| Ok(serde_json::to_value(p).unwrap());
        let pages = || {
            vec![
                v(page("$ run\n", 6, false, false)),
                v(page("", 6, false, false)),
                v(page("\x1b[32mok\x1b[0m\r\n", 19, false, false)),
                v(page("end\n", 23, false, true)),
            ]
        };
        let (r, out, params, sleeps) = run_follow(false, pages());
        r.unwrap();
        assert_eq!(out, "$ run\n\x1b[32mok\x1b[0m\r\nend\n", "a pipe log as it is");
        assert_eq!(sleeps, 1);
        let offsets: Vec<_> = params.iter().map(|p| p["offset"].as_u64().unwrap()).collect();
        assert_eq!(offsets, [0, 6, 6, 19]);
        assert_eq!(params[0], json!({"instance": "id-web", "offset": 0, "machine": "lap"}));

        // A pty log: as it is on a terminal, its escapes stripped elsewhere.
        let pty = || vec![v(page("\x1b[32mok\x1b[0m\r\n", 13, true, false)), v(page("50%\r100%\r\n", 24, true, true))];
        assert_eq!(run_follow(true, pty()).1, "\x1b[32mok\x1b[0m\r\n50%\r100%\r\n");
        let mut strip = crate::build_log::Strip::default();
        let want = [strip.feed(b"\x1b[32mok\x1b[0m\r\n"), strip.feed(b"50%\r100%\r\n")].concat();
        assert_eq!(run_follow(false, pty()).1, String::from_utf8(want).unwrap());

        // No log at first is an error; the record gone later ends it.
        assert_eq!(run_follow(false, vec![Ok(Value::Null)]).0.unwrap_err().to_string(), "`lap/web` has no build log");
        let gone = || Err(("not-found", "no instance `id-web`".to_string()));
        assert_eq!(run_follow(false, vec![gone()]).0.unwrap_err().to_string(), "no instance `id-web`");
        let (r, out, ..) = run_follow(false, vec![v(page("a", 1, false, false)), gone()]);
        assert_eq!((r.is_ok(), out.as_str()), (true, "a"));
        let (r, ..) = run_follow(false, vec![v(page("a", 1, false, false)), Ok(Value::Null)]);
        assert!(r.is_ok());
    }

    /// The follow asks by the row's `instance_id`, so a rename doesn't lose it.
    #[test]
    fn a_named_follow_asks_by_instance_id() {
        let (r, out, _, calls) = run_named(Use::BuildLog { follow: true }, "web", |m, _| {
            Ok(if m == "instances.list" {
                listing(vec![row("web", "c", "/f/cfg", false)], vec![], None)
            } else {
                serde_json::to_value(page("done\n", 5, false, true)).unwrap()
            })
        });
        assert_eq!((r.unwrap(), out.as_str()), (0, "done\n"));
        assert_eq!(calls[1], ("instances.buildLog".into(), json!({"instance": "id", "offset": 0, "machine": "lap"})));
    }

    /// `done` words its line from the flag the rows had, as the local one
    /// from the state; `rename` prints the trimmed name the daemon answers.
    #[test]
    fn done_and_rename_print_as_the_local_commands() {
        for (done, was) in [(true, false), (true, true), (false, true), (false, false)] {
            let (r, out, _, calls) = run_named(Use::Done(done), "web", |m, _| {
                Ok(if m == "instances.list" { listing(vec![row("web", "c", "/f/cfg", was)], vec![], None) } else { json!({"instance": "web", "done": done}) })
            });
            assert_eq!(r.unwrap(), 0);
            assert_eq!(out, format!("{}\n", crate::commands::done::status_line("web", done, was)));
            assert_eq!(calls[1], ("instances.done".into(), json!({"instance": "web", "done": done, "machine": "lap"})));
        }
        let (r, out, err, _) = run_named(Use::Done(true), "web", |m, _| {
            Ok(if m == "instances.list" {
                listing(vec![row("web", "c", "/f/cfg", false)], vec![], None)
            } else {
                json!({"instance": "web", "done": true, "warning": "cannot fetch lap/web back: no link"})
            })
        });
        assert_eq!(r.unwrap(), 0);
        assert_eq!((out, err.as_str()), (format!("{}\n", crate::commands::done::status_line("web", true, false)), "warning: cannot fetch lap/web back: no link\n"));
        let (_, out, _, calls) = run_named(Use::Rename(" api ".into()), "web", |_, _| Ok(json!({"instance": "api"})));
        assert_eq!(out, "renamed web -> api\n");
        assert_eq!(calls, [("instances.rename".into(), json!({"instance": "web", "newName": " api ", "machine": "lap"}))]);
        let (r, ..) = run_named(Use::Rename("x".into()), "web", |_, _| Err(("invalid", "`x` is taken".into())));
        assert_eq!(r.unwrap_err().to_string(), "`x` is taken");
    }

    /// `profile` sets through `instances.setProfile` and prints the local
    /// line; without a profile it reads the row: the profile and its flag,
    /// and the allowed set from the same listing's sandbox row.
    #[test]
    fn profile_prints_as_the_local_command() {
        let (r, out, _, calls) = run_named(Use::Profile(Some("work".into())), "web", |_, _| Ok(json!({"instance": "web", "profile": "work"})));
        assert_eq!((r.unwrap(), out.as_str()), (0, "profile web: work\n"));
        assert_eq!(calls, [("instances.setProfile".into(), json!({"instance": "web", "profile": "work", "machine": "lap"}))]);
        let (r, ..) = run_named(Use::Profile(Some("x".into())), "web", |_, _| Err(("invalid", "sandbox `web` doesn't allow profile `x`; allowed: work".into())));
        assert_eq!(r.unwrap_err().to_string(), "sandbox `web` doesn't allow profile `x`; allowed: work");

        let mut flagged = row("web", "c", "/f/cfg", false);
        flagged["profile"] = json!("gone");
        flagged["profile_disallowed"] = json!(true);
        let mut web = sandbox("web", "image x", None, &[], &[]);
        web["profiles"] = json!(["work", "home"]);
        let mut elsewhere = web.clone();
        elsewhere["root"] = json!("/f/other");
        elsewhere["profiles"] = json!(["x"]);
        let (r, out, _, calls) =
            run_named(Use::Profile(None), "web", |_, _| Ok(listing(vec![flagged.clone()], vec![elsewhere.clone(), web.clone()], None)));
        assert_eq!((r.unwrap(), out.as_str()), (0, "profile web: gone (its sandbox doesn't allow it any more)\nallowed: work, home\n"));
        assert_eq!(calls.len(), 1, "read only");
        let (_, out, ..) = run_named(Use::Profile(None), "web", |_, _| Ok(listing(vec![flagged.clone()], vec![], None)));
        assert_eq!(out, "profile web: gone (its sandbox doesn't allow it any more)\nallowed: unknown (sandbox `web` isn't in its config any more)\n");
        web["profiles"] = json!([]);
        let (_, out, ..) = run_named(Use::Profile(None), "web", |_, _| Ok(listing(vec![flagged.clone()], vec![web.clone()], None)));
        assert!(out.ends_with("allowed: (none: its sandbox has no profiles)\n"), "{out}");
        let (r, ..) = run_named(Use::Profile(None), "api", |_, _| Ok(listing(vec![flagged.clone()], vec![], None)));
        assert_eq!(r.unwrap_err().to_string(), "no instance named `api` on machine `lap` (see `devsandbox ps -a`)");
    }

    fn list_of(v: Value) -> InstancesList {
        serde_json::from_value(v).unwrap()
    }

    /// A fleet root (no config of its own) stands for the config roots its
    /// rows name under it; an ssh machine's config root for itself, never
    /// for the machine's other roots its listing's rows name.
    #[test]
    fn a_root_stands_for_itself_or_the_config_roots_under_it() {
        let p = |s: &[&str]| s.iter().map(PathBuf::from).collect::<Vec<_>>();
        let rows = || vec![row("a", "c", "/f/b", false), row("b", "c", "/f/a", false), row("c", "c", "/f/a", false), row("d", "c", "/r", false)];
        let configless = Some("config: no devsandboxes.toml");
        assert_eq!(config_roots(Path::new("/f"), &list_of(listing(rows(), vec![], configless))), p(&["/f/a", "/f/b"]));
        assert_eq!(config_roots(Path::new("/f"), &list_of(listing(vec![], vec![], configless))), p(&["/f"]));
        assert_eq!(config_roots(Path::new("/r"), &list_of(listing(rows(), vec![], None))), p(&["/r"]));
        assert_eq!(config_roots(Path::new("/s"), &list_of(listing(rows(), vec![], None))), p(&["/s"]), "no instances there yet");
        // A config root with others nested in it: itself and those.
        assert_eq!(config_roots(Path::new("/f"), &list_of(listing(rows(), vec![], None))), p(&["/f", "/f/a", "/f/b"]));
    }

    fn run_per_root(u: Use, answer: impl FnMut(&str, &Value) -> Result<Value, (&'static str, String)>) -> (Result<i32>, String, String) {
        let mut f = fake(answer);
        let (mut out, mut err) = (Vec::new(), Vec::new());
        let r = per_root(&mut |m, p| f.call(m, p), &u, "lap", &[PathBuf::from("/f")], &mut out, &mut err);
        (r, String::from_utf8(out).unwrap(), String::from_utf8(err).unwrap())
    }

    #[test]
    fn ls_prints_as_the_local_command_per_config_root() {
        let sandboxes = || vec![sandbox("web", "image node", Some("app"), &["db", "redis"], &["base"]), sandbox("cli", "dockerfile D", None, &[], &[])];
        let answer = |m: &str, p: &Value| {
            assert_eq!(m, "instances.list");
            Ok(match p["dir"].as_str().unwrap() {
                "/f" => listing(vec![row("w", "c", "/f/cfg", false)], vec![], Some("config: no devsandboxes.toml")),
                _ => listing(vec![row("w", "c", "/f/cfg", false)], sandboxes(), None),
            })
        };
        let (r, out, err) = run_per_root(Use::Ls { json: false }, answer);
        assert_eq!(r.unwrap(), 0);
        assert_eq!(err, "==> lap:/f/cfg\n");
        let rows: Vec<crate::snapshot::SandboxRow> = serde_json::from_value(json!(sandboxes())).unwrap();
        let lines: Vec<_> = rows.iter().map(crate::commands::ls::Line::of_row).collect();
        assert_eq!(out, crate::commands::ls::table(&lines));
        assert_eq!(
            lines[0],
            crate::commands::ls::Line {
                name: "web".into(),
                source: "image node".into(),
                folder: Some("app".into()),
                services: Some("db, redis".into()),
                extends: Some("base".into())
            }
        );
        assert_eq!((lines[1].folder.as_deref(), lines[1].services.as_deref(), lines[1].extends.as_deref()), (None, None, None));

        let (_, out, _) = run_per_root(Use::Ls { json: true }, answer);
        assert_eq!(out, format!("{}\n", crate::commands::ls::render_json(&rows).unwrap()));
    }

    /// `--machine m ps` prints what the local `ps` prints for the machine's
    /// snapshot, from one full `instances.list`.
    #[test]
    fn ps_prints_as_the_local_command() {
        let mut done = row("a", "c", "/f/cfg", true);
        done["profile"] = json!("work");
        done["profile_disallowed"] = json!(true);
        let mut stopped = row("b", "c", "/f/cfg", false);
        stopped["status"] = json!({"state": "exited", "text": "Exited (0)"});
        let answer = listing(vec![done, stopped], vec![], None);
        let run = |all: bool, json: bool| {
            let mut f = fake(|_: &str, _: &Value| Ok(answer.clone()));
            let mut out = Vec::new();
            let r = super::listing(&mut |m, p| f.call(m, p), &Use::Ps { all, json }, "lap", Path::new("/f/cfg"), &mut out);
            (r.unwrap(), String::from_utf8(out).unwrap(), f.calls)
        };
        for (all, json) in [(false, false), (true, false), (false, true), (true, true)] {
            let (code, out, calls) = run(all, json);
            let want = crate::commands::ps::render(list_of(answer.clone()).snapshot, &[], all, json).unwrap();
            assert_eq!((code, out), (0, want));
            assert_eq!(calls, [("instances.list".into(), json!({"dir": "/f/cfg", "machine": "lap"}))]);
        }
        let (_, out, _) = run(false, false);
        assert!(out.contains("running (done)") && out.contains("work (not allowed)") && !out.contains("exited"), "{out}");

        // A trust refusal is the error, naming the machine it's about.
        let denied = "`/x` isn't one of this member's roots (`devsandbox machine roots add <path>` there adds one)";
        let mut f = fake(|_: &str, _: &Value| Err(("denied", denied.into())));
        let r = super::listing(&mut |m, p| f.call(m, p), &Use::Ps { all: false, json: false }, "lap", Path::new("/x"), &mut Vec::new());
        assert_eq!(r.unwrap_err().to_string(), format!("machine `lap`: {denied}"));
    }

    /// `service ls` prints the listing's service rows as the local one
    /// prints its own, the snapshot's error on stderr.
    #[test]
    fn service_ls_prints_as_the_local_command_per_config_root() {
        let services = json!([
            {"name": "db", "scope": "isolated", "source": "image postgres", "ports": ["5432"],
             "containers": [["devsandbox-svc-p-db-id", {"state": "running", "text": "Up 1 hour"}]], "used_by": ["w"],
             "env_len": 0, "command": null, "config_hash": "h", "drift": false, "root": "/f/cfg"},
            {"name": "cache", "scope": "global", "source": "?", "ports": [], "containers": [["devsandbox-svc-p-cache", {"state": "missing"}]],
             "used_by": [], "env_len": 0, "command": null, "config_hash": "", "drift": false, "root": "/f/cfg"},
        ]);
        let depths = std::cell::RefCell::new(Vec::new());
        let answer = |error: Option<&'static str>, services: Value| {
            let depths = &depths;
            move |_: &str, p: &Value| {
                depths.borrow_mut().push((p["dir"].clone(), p["depth"].clone()));
                // The fleet root has no config of its own.
                let error = if p["dir"] == "/f" { Some("config: no devsandboxes.toml") } else { error };
                let mut l = listing(vec![row("w", "c", "/f/cfg", false)], vec![], error);
                l["services"] = services.clone();
                Ok(l)
            }
        };
        let rows: Vec<crate::snapshot::ServiceRow> = serde_json::from_value(services.clone()).unwrap();
        let config = Path::new("/f/cfg/devsandboxes.toml");
        for json in [false, true] {
            let (r, out, err) = run_per_root(Use::ServiceLs { json }, answer(Some("service `cache`: no image"), services.clone()));
            assert_eq!(r.unwrap(), 0);
            assert_eq!(out, crate::commands::services::ls_render(&rows, json, config).unwrap());
            assert_eq!(err, "==> lap:/f/cfg\nservice `cache`: no image\n");
        }
        assert_eq!(depths.borrow().last().unwrap(), &(json!("/f/cfg"), json!("listing")));
        assert!(crate::commands::services::ls_render(&rows, false, config).unwrap().contains("running"));

        let (_, out, err) = run_per_root(Use::ServiceLs { json: false }, answer(None, json!([])));
        assert_eq!((out.as_str(), err.as_str()), ("no services defined in /f/cfg/devsandboxes.toml\n", "==> lap:/f/cfg\n"));
        let (r, ..) = run_per_root(Use::ServiceLs { json: true }, answer(Some("config: no devsandboxes.toml"), json!([])));
        assert_eq!(r.unwrap_err().to_string(), "lap:/f/cfg: config: no devsandboxes.toml");
    }

    #[test]
    fn ls_without_sandboxes_or_with_several_roots() {
        // Nothing placed: the fleet root itself, which has no config.
        let (r, ..) = run_per_root(Use::Ls { json: false }, |_, _| Ok(listing(vec![], vec![], Some("config: no devsandboxes.toml"))));
        assert_eq!(r.unwrap_err().to_string(), "lap:/f: config: no devsandboxes.toml");
        let (_, out, _) = run_per_root(Use::Ls { json: false }, |_, _| Ok(listing(vec![], vec![], None)));
        assert_eq!(out, "no sandboxes defined in /f\n");
        let two = |_: &str, p: &Value| {
            let error = (p["dir"] == "/f").then_some("config: no devsandboxes.toml");
            Ok(listing(vec![row("a", "c", "/f/a", false), row("b", "c", "/f/b", false)], vec![], error))
        };
        let (r, ..) = run_per_root(Use::Ls { json: true }, two);
        assert!(r.unwrap_err().to_string().contains("/f/a, /f/b"));
        let (r, _, err) = run_per_root(Use::Ls { json: false }, two);
        assert_eq!((r.unwrap(), err.as_str()), (0, "==> lap:/f/a\n==> lap:/f/b\n"));
    }
}