devsandbox 0.8.0

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
//! The `machines.*` methods (docs/architecture/daemon/machines.md,
//! *`devsandbox machine add|ls|set|rm`*): `probe`, `add`, `list`, `get`,
//! `set`, `rm`.
//! Connection-free like `serve::api`: a function of the method, its params
//! and a [`Ctx`] (the registry path and the [`Dial`]), so tests run them
//! against a second test daemon standing in as the machine. The one
//! per-connection method, `machines.hold`, is [`hold`]: it returns the
//! [`Want`] the daemon keeps on the connection.
//!
//! `probe` and `add` open a fresh connection each (ssh, the hello, one or
//! two calls) and drop it; the held link per machine is `service`'s, which
//! `add`, `set` and `rm` tell to reload and `list` reads the live state of. `add`
//! checks the typed roots *on the machine* (`roots.list {paths}`): only it
//! can tell whether they exist, expand a quoted `~`, and canonicalize them.

use std::path::PathBuf;
use std::time::Duration;

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use super::link::{self, Link};
use super::service::{Handle, Want};
use super::ssh::{self, Dial};
use super::{Entry, check_name, default_name};
use crate::serve::api::{ApiError, RootCandidate, parse};
use crate::serve::snapshots::LinkState;

/// The hello's wait: ssh's connect and auth, the remote login shell, and a
/// lazy start of the remote daemon.
const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(20);

type Answer = Result<Value, ApiError>;

/// What the methods need from the daemon.
pub struct Ctx {
    /// `machines.toml` ([`super::path`] outside tests).
    pub registry: PathBuf,
    pub dial: Dial,
    /// This daemon's semver, which `add` warns a differing machine's against.
    pub version: String,
    /// The links: `add`/`set`/`rm` reload them, `list` reports their state. `None`
    /// only where no service runs (then every machine lists as `idle`).
    pub links: Option<Handle>,
}

/// Run `method` if it's a `machines.*` one; `None` otherwise.
pub fn call(method: &str, params: Value, ctx: &Ctx) -> Option<Answer> {
    let answer = match method {
        "machines.probe" => parse(params).and_then(|p| probe(p, ctx)),
        "machines.add" => parse(params).and_then(|p| add(p, ctx)),
        "machines.list" => list(ctx),
        "machines.get" => parse(params).and_then(|p| get(p, ctx)),
        "machines.set" => parse(params).and_then(|p| set(p, ctx)),
        "machines.rm" => parse(params).and_then(|p| rm(p, ctx)),
        _ => return None,
    };
    Some(answer)
}

/// `machines.probe`'s answer: the machine's devsandbox and its candidate
/// roots.
#[derive(Debug, Serialize, Deserialize)]
pub struct Probed {
    pub version: String,
    pub protocol: u32,
    pub roots: Vec<RootCandidate>,
}

/// `machines.add`'s answer.
#[derive(Debug, Serialize, Deserialize)]
pub struct Added {
    pub name: String,
    /// As saved: the machine's canonical paths, deduplicated.
    pub roots: Vec<PathBuf>,
    /// The machine's devsandbox semver.
    pub version: String,
    /// Set when that semver differs from this daemon's (same protocol, so
    /// allowed).
    pub warning: Option<String>,
}

/// One `machines.list` row: the registry entry and its link's live state
/// (`error` the last failure while `unreachable`; `version` from the last
/// hello).
#[derive(Debug, Serialize, Deserialize)]
pub struct MachineRow {
    pub name: String,
    pub target: String,
    pub roots: Vec<PathBuf>,
    pub command: Option<String>,
    pub agent: bool,
    pub state: LinkState,
    pub error: Option<String>,
    pub version: Option<String>,
}

#[derive(Deserialize)]
struct ProbeParams {
    target: String,
    #[serde(default)]
    command: Option<String>,
}

fn probe(p: ProbeParams, ctx: &Ctx) -> Answer {
    ssh::parse_target(&p.target).map_err(|e| ApiError::invalid(format!("{e:#}")))?;
    check_command(p.command.as_deref())?;
    // No name yet: errors say the target.
    let entry = Entry { name: p.target.clone(), target: p.target, roots: Vec::new(), command: p.command, agent: false };
    let mut link = connect(&entry, ctx)?;
    let roots: Vec<RootCandidate> = remote_call(&mut link, &entry.name, "roots.list", json!({}))?;
    let remote = link.remote();
    to_value(Probed { version: remote.version.clone(), protocol: remote.protocol, roots })
}

#[derive(Deserialize)]
struct AddParams {
    target: String,
    #[serde(default)]
    name: Option<String>,
    roots: Vec<String>,
    #[serde(default)]
    agent: bool,
    #[serde(default)]
    command: Option<String>,
}

fn add(p: AddParams, ctx: &Ctx) -> Answer {
    let invalid = |e: anyhow::Error| ApiError::invalid(format!("{e:#}"));
    ssh::parse_target(&p.target).map_err(invalid)?;
    let name = match p.name {
        Some(name) => name,
        None => default_name(&p.target).map_err(invalid)?,
    };
    check_name(&name).map_err(invalid)?;
    check_command(p.command.as_deref())?;
    if p.roots.is_empty() {
        return Err(ApiError::invalid("no roots: pass --config <path on the machine>"));
    }
    // Checked before the ssh, so a taken name fails fast; again at the save.
    if registered(ctx)?.iter().any(|e| e.name == name) {
        return Err(taken(&name));
    }

    let mut entry = Entry { name, target: p.target, roots: Vec::new(), command: p.command, agent: p.agent };
    let mut link = connect(&entry, ctx)?;
    let checked: Vec<RootCandidate> = remote_call(&mut link, &entry.name, "roots.list", json!({ "paths": p.roots }))?;
    // A daemon that ignores `paths` answers its candidates instead.
    if checked.len() != p.roots.len() {
        return Err(ApiError::failed(format!("{}: its devsandbox can't check roots; upgrade it", entry.name)));
    }
    let refused: Vec<String> = checked
        .iter()
        .zip(&p.roots)
        .filter_map(|(c, given)| c.error.as_ref().map(|e| format!("`{given}`: {e}")))
        .collect();
    if !refused.is_empty() {
        return Err(ApiError::invalid(format!("{}: {}", entry.name, refused.join("; "))));
    }
    for c in checked {
        if !entry.roots.contains(&c.root) {
            entry.roots.push(c.root);
        }
    }
    let version = link.remote().version.clone();
    drop(link);

    let saved = super::update(&ctx.registry, |entries| {
        if entries.iter().any(|e| e.name == entry.name) {
            return Err(taken(&entry.name));
        }
        entries.push(entry.clone());
        Ok(())
    });
    saved.map_err(ApiError::internal)??;
    if let Some(links) = &ctx.links {
        links.reload();
    }
    let warning = (version != ctx.version)
        .then(|| format!("{} runs devsandbox {version}, this is {}", entry.name, ctx.version));
    to_value(Added { name: entry.name, roots: entry.roots, version, warning })
}

fn list(ctx: &Ctx) -> Answer {
    let rows: Vec<MachineRow> = registered(ctx)?
        .into_iter()
        .map(|e| {
            let live = ctx.links.as_ref().and_then(|l| l.remotes().get(&e.name)).unwrap_or_default();
            MachineRow {
                name: e.name,
                target: e.target,
                roots: e.roots,
                command: e.command,
                agent: e.agent,
                state: live.state,
                error: live.error,
                version: live.version,
            }
        })
        .collect();
    to_value(rows)
}

/// `machines.get`'s answer: what an ssh command to the machine needs. No
/// argv on the wire: Rust callers build it (`ssh::tty_argv`), other clients
/// from `target` and `command`.
#[derive(Debug, Serialize, Deserialize)]
pub struct MachineInfo {
    pub name: String,
    pub target: String,
    pub command: Option<String>,
    pub roots: Vec<PathBuf>,
    pub agent: bool,
}

fn get(p: NameParams, ctx: &Ctx) -> Answer {
    let entry = registered(ctx)?
        .into_iter()
        .find(|e| e.name == p.name)
        .ok_or_else(|| ApiError::not_found(format!("no machine named `{}`", p.name)))?;
    to_value(MachineInfo {
        name: entry.name,
        target: entry.target,
        command: entry.command,
        roots: entry.roots,
        agent: entry.agent,
    })
}

#[derive(Deserialize)]
struct SetParams {
    name: String,
    agent: bool,
}

/// Change a registered machine's settings (today only `agent`). The links
/// reload only when something changed.
fn set(p: SetParams, ctx: &Ctx) -> Answer {
    let changed = super::update(&ctx.registry, |entries| {
        let entry = entries.iter_mut().find(|e| e.name == p.name)?;
        let changed = entry.agent != p.agent;
        entry.agent = p.agent;
        Some(changed)
    });
    let Some(changed) = changed.map_err(ApiError::internal)? else {
        return Err(ApiError::not_found(format!("no machine named `{}`", p.name)));
    };
    if changed && let Some(links) = &ctx.links {
        links.reload();
    }
    Ok(json!({ "ok": true }))
}

#[derive(Deserialize)]
struct NameParams {
    name: String,
}

#[derive(Deserialize)]
struct HoldParams {
    name: String,
    #[serde(default)]
    agent: Option<PathBuf>,
}

/// `machines.hold`'s answer: `agent`, whether the machine's agent relay is
/// up (always `false` for a machine with `agent` off).
#[derive(Debug, Serialize, Deserialize)]
pub struct Held {
    pub ok: bool,
    pub agent: bool,
}

/// `machines.hold {name, agent?}`: hold machine `name` wanted (its link
/// and, with `agent` on, its agent relay up) for as long as the caller keeps
/// the returned [`Want`], which the daemon keeps until the connection
/// closes. `agent` is the caller's `SSH_AUTH_SOCK`, reported to this daemon
/// (`bridge::report_agent`) like a forwarded verb's, so the relay serves
/// it. With `agent` on, waits up to [`super::forward::AGENT_WAIT`] for the
/// relay, as a forwarded verb does (the caller's ssh would race it).
pub fn hold(params: Value, ctx: &Ctx) -> Result<(Want, Held), ApiError> {
    let p: HoldParams = parse(params)?;
    if let Some(agent) = &p.agent
        && !agent.is_absolute()
    {
        return Err(ApiError::invalid(format!("`agent` must be absolute, got `{}`", agent.display())));
    }
    let no_machine = || ApiError::not_found(format!("no machine named `{}`", p.name));
    let links = ctx.links.as_ref().ok_or_else(no_machine)?;
    let on = links.entries().iter().find(|e| e.name == p.name).ok_or_else(no_machine)?.agent;
    if let Some(agent) = p.agent {
        crate::devsbd::bridge::report_agent(agent);
    }
    let want = links.want(&p.name).ok_or_else(no_machine)?;
    let ready = on && want.agent_ready(super::forward::AGENT_WAIT);
    Ok((want, Held { ok: true, agent: ready }))
}

fn rm(p: NameParams, ctx: &Ctx) -> Answer {
    let removed = super::update(&ctx.registry, |entries| {
        let before = entries.len();
        entries.retain(|e| e.name != p.name);
        entries.len() != before
    });
    if !removed.map_err(ApiError::internal)? {
        return Err(ApiError::not_found(format!("no machine named `{}`", p.name)));
    }
    if let Some(links) = &ctx.links {
        links.reload();
    }
    Ok(json!({ "ok": true }))
}

fn registered(ctx: &Ctx) -> Result<Vec<Entry>, ApiError> {
    super::load(&ctx.registry).map_err(ApiError::internal)
}

/// `command` is shell text the machine runs, like the registry's hand-set
/// field, so the only check is that there's something to run.
fn check_command(command: Option<&str>) -> Result<(), ApiError> {
    match command {
        Some(c) if c.trim().is_empty() => Err(ApiError::invalid("empty `command`: omit it for `devsandbox`")),
        _ => Ok(()),
    }
}

fn taken(name: &str) -> ApiError {
    ApiError::invalid(format!("a machine named `{name}` exists; pass --name"))
}

/// Dial `entry` and say hello: `failed` with ssh's or the hello's reason.
fn connect(entry: &Entry, ctx: &Ctx) -> Result<Link, ApiError> {
    let duplex = (ctx.dial)(entry).map_err(|e| ApiError::failed(format!("{}: {e:#}", entry.name)))?;
    link::handshake(&entry.name, duplex, HANDSHAKE_TIMEOUT).map_err(|e| ApiError::failed(e.to_string()))
}

/// One call on `link`; any error answer, or an answer that doesn't parse,
/// is `failed`, named after `machine`.
fn remote_call<T: DeserializeOwned>(link: &mut Link, machine: &str, method: &str, params: Value) -> Result<T, ApiError> {
    let r = link.conn.call(method, params).map_err(|e| ApiError::failed(format!("{machine}: {e:#}")))?;
    if let Some(e) = r.error {
        return Err(ApiError::failed(format!("{machine}: {method}: {} ({})", e.message, e.code)));
    }
    serde_json::from_value(r.result.unwrap_or(Value::Null))
        .map_err(|e| ApiError::failed(format!("{machine}: {method}: unexpected answer: {e}")))
}

fn to_value(x: impl Serialize) -> Answer {
    serde_json::to_value(x).map_err(|e| ApiError::internal(e.into()))
}

#[cfg(test)]
mod tests {
    use std::path::Path;
    use std::sync::Arc;
    use std::time::Instant;

    use super::*;
    use crate::serve::client::{self, Conn, Hello};
    use crate::serve::daemon::tests::{opts, scratch, spawn_daemon};
    use crate::serve::daemon::{Exit, Options};
    use crate::serve::endpoint;
    use crate::serve::machines::ssh;
    use crate::serve::proto::Version;

    /// Connect to the test daemon in `dir` as a client, retrying while it binds.
    fn client_at(dir: &Path) -> Conn {
        let deadline = Instant::now() + Duration::from_secs(5);
        loop {
            if let Ok(mut stream) = endpoint::connect(dir) {
                // Older than any daemon: never a handoff, whatever its semver.
                let version = Version { semver: "0.0.0".into(), build: 0 };
                match client::hello(&mut stream, &version, "test").unwrap() {
                    Hello::Ready(reply) => return Conn::new(stream, reply).unwrap(),
                    other => panic!("{other:?}"),
                }
            }
            assert!(Instant::now() < deadline, "daemon never listened");
            std::thread::sleep(Duration::from_millis(20));
        }
    }

    /// A machine: a test daemon on `base/remote` with its own state and
    /// home (`base/remote-home`), reporting `semver`.
    fn remote_opts(base: &Path, semver: &str) -> Options {
        let mut o = opts(1, 1500);
        o.version.semver = semver.into();
        o.state = Some(base.join("remote-state.toml"));
        o.home = Some(base.join("remote-home"));
        o
    }

    /// This host's test daemon on `base/local`, its registry in `base`,
    /// dialing the machine's socket in `remote`.
    fn local_opts(base: &Path, remote: Option<PathBuf>) -> Options {
        let mut o = opts(1, 1500);
        o.machines = Some(base.join("machines.toml"));
        o.dial = Some(match remote {
            Some(remote) => Arc::new(move |_: &Entry| ssh::test_support::relayed(endpoint::connect(&remote)?)),
            None => Arc::new(|e: &Entry| anyhow::bail!("ssh: Could not resolve hostname {}", e.target)),
        });
        o
    }

    fn ok(conn: &mut Conn, method: &str, params: Value) -> Value {
        let r = conn.call(method, params).unwrap();
        assert!(r.error.is_none(), "{method}: {:?}", r.error);
        r.result.unwrap()
    }

    fn err(conn: &mut Conn, method: &str, params: Value) -> (String, String) {
        let e = conn.call(method, params).unwrap().error.unwrap_or_else(|| panic!("{method} succeeded"));
        (e.code, e.message)
    }

    /// Two daemons: the machine (with `~/.devsandboxes` holding `web` and
    /// `api`, and `~/broken` whose config doesn't parse) and this host's.
    struct Pair {
        base: PathBuf,
        home: PathBuf,
        local: Conn,
        daemons: Vec<std::thread::JoinHandle<anyhow::Result<Exit>>>,
    }

    impl Pair {
        fn new(tag: &str, remote_semver: &str) -> Self {
            Self::with(tag, remote_semver, |_| {})
        }

        /// As [`Pair::new`], `local` adjusting this host's daemon's options.
        fn with(tag: &str, remote_semver: &str, local: impl FnOnce(&mut Options)) -> Self {
            let base = scratch(tag);
            let home = base.join("remote-home");
            std::fs::create_dir_all(home.join(".devsandboxes")).unwrap();
            std::fs::create_dir_all(home.join("broken")).unwrap();
            let cfg = crate::config::CONFIG_FILE;
            std::fs::write(home.join(".devsandboxes").join(cfg), "[sandbox.web]\nimage = \"a\"\n[sandbox.api]\nimage = \"a\"\n").unwrap();
            std::fs::write(home.join("broken").join(cfg), "[sandbox.web\n").unwrap();
            let remote = base.join("remote");
            let mut local_o = local_opts(&base, Some(remote.clone()));
            local(&mut local_o);
            let daemons =
                vec![spawn_daemon(&remote, remote_opts(&base, remote_semver)), spawn_daemon(&base.join("local"), local_o)];
            // The dial connects straight away: the machine must be up.
            drop(client_at(&base.join("remote")));
            let local = client_at(&base.join("local"));
            Pair { base, home, local, daemons }
        }

        fn finish(self) {
            drop(self.local);
            for d in self.daemons {
                assert_eq!(d.join().unwrap().unwrap(), Exit::Idle);
            }
            let _ = std::fs::remove_dir_all(&self.base);
        }
    }

    #[test]
    fn probe_answers_the_machines_roots() {
        let mut pair = Pair::new("mach-probe", "0.6.0");
        let r = ok(&mut pair.local, "machines.probe", json!({"target": "me@box"}));
        let dot = std::fs::canonicalize(pair.home.join(".devsandboxes")).unwrap();
        assert_eq!(
            r,
            json!({"version": "0.6.0", "protocol": 1, "roots": [{"root": dot, "sandboxes": [{"name": "api"}, {"name": "web"}], "error": null}]})
        );
        assert_eq!(err(&mut pair.local, "machines.probe", json!({"target": "-oProxyCommand=x"})).0, "invalid");
        assert_eq!(err(&mut pair.local, "machines.probe", json!({})).0, "invalid");
        for empty in ["", "  "] {
            let (code, message) = err(&mut pair.local, "machines.probe", json!({"target": "me@box", "command": empty}));
            assert!(code == "invalid" && message.contains("`command`"), "{code}: {message}");
        }
        pair.finish();
    }

    #[test]
    fn a_failing_dial_is_failed_with_its_reason() {
        let base = scratch("mach-nodial");
        let daemon = spawn_daemon(&base.join("local"), local_opts(&base, None));
        let mut local = client_at(&base.join("local"));
        let (code, message) = err(&mut local, "machines.probe", json!({"target": "me@box"}));
        assert_eq!((code.as_str(), message.as_str()), ("failed", "me@box: ssh: Could not resolve hostname me@box"));
        let (code, _) = err(&mut local, "machines.add", json!({"target": "me@box", "roots": ["/x"]}));
        assert_eq!(code, "failed");
        assert!(!base.join("machines.toml").exists(), "nothing saved");
        drop(local);
        assert_eq!(daemon.join().unwrap().unwrap(), Exit::Idle);
        let _ = std::fs::remove_dir_all(&base);
    }

    #[test]
    fn add_checks_the_roots_on_the_machine_then_saves() {
        let mut pair = Pair::new("mach-add", "0.6.0");
        let registry = pair.base.join("machines.toml");
        // A typed bad root is refused, naming it; nothing is saved.
        let (code, message) =
            err(&mut pair.local, "machines.add", json!({"target": "me@box", "roots": ["~/.devsandboxes", "~/gone", "rel"]}));
        assert_eq!(code, "invalid");
        assert!(message.starts_with("box: `~/gone`: no directory ") && message.ends_with("; `rel`: not absolute"), "{message}");
        let (code, message) = err(&mut pair.local, "machines.add", json!({"target": "me@box", "roots": ["~/broken"]}));
        assert!(code == "invalid" && message.contains("devsandboxes.toml"), "{code}: {message}");
        assert_eq!(err(&mut pair.local, "machines.add", json!({"target": "me@box", "roots": []})).0, "invalid");
        let (code, message) =
            err(&mut pair.local, "machines.add", json!({"target": "me@box", "roots": ["~"], "command": " "}));
        assert!(code == "invalid" && message.contains("`command`"), "{code}: {message}");
        assert!(!registry.exists(), "nothing saved");

        // The default name is the host; roots are the machine's canonical
        // paths, deduplicated in order.
        let dot = std::fs::canonicalize(pair.home.join(".devsandboxes")).unwrap();
        let home = std::fs::canonicalize(&pair.home).unwrap();
        let via = format!("{}/broken/../.devsandboxes", pair.home.display());
        let r = ok(&mut pair.local, "machines.add", json!({"target": "me@box", "roots": ["~/.devsandboxes", "~", via]}));
        assert_eq!(r, json!({"name": "box", "roots": [dot, home], "version": "0.6.0", "warning": null}));
        let saved = crate::serve::machines::load(&registry).unwrap();
        assert_eq!(saved, vec![Entry { name: "box".into(), target: "me@box".into(), roots: vec![dot.clone(), home], command: None, agent: false }]);

        // Taken: by default name or by `name`; `--name` gets around it.
        let (code, message) = err(&mut pair.local, "machines.add", json!({"target": "ssh://box:2200", "roots": ["~"]}));
        assert_eq!((code.as_str(), message.as_str()), ("invalid", "a machine named `box` exists; pass --name"));
        let (code, _) = err(&mut pair.local, "machines.add", json!({"target": "x", "name": "a/b", "roots": ["~"]}));
        assert_eq!(code, "invalid");
        let (code, message) = err(&mut pair.local, "machines.add", json!({"target": "me@[::1]", "roots": ["~"]}));
        assert!(code == "invalid" && message.ends_with("pass --name"), "{message}");
        let cmd = "~/.local/bin/devsandbox";
        let r = ok(
            &mut pair.local,
            "machines.add",
            json!({"target": "ssh://box:2200", "name": "box2", "roots": ["~/.devsandboxes"], "agent": true, "command": cmd}),
        );
        assert_eq!(r["name"], "box2");
        let box2 = &crate::serve::machines::load(&registry).unwrap()[1];
        assert!(box2.agent, "`agent` saved");
        assert_eq!(box2.command.as_deref(), Some(cmd), "`command` saved");
        assert!(std::fs::read_to_string(&registry).unwrap().contains("command = \"~/.local/bin/devsandbox\""));

        // Listed sorted by name, idle (no link wanted); then removed.
        let rows = ok(&mut pair.local, "machines.list", Value::Null);
        assert_eq!(
            rows,
            json!([
                {"name": "box", "target": "me@box", "roots": [dot, std::fs::canonicalize(&pair.home).unwrap()], "command": null, "agent": false, "state": "idle", "error": null, "version": null},
                {"name": "box2", "target": "ssh://box:2200", "roots": [dot], "command": cmd, "agent": true, "state": "idle", "error": null, "version": null},
            ])
        );
        let info = ok(&mut pair.local, "machines.get", json!({"name": "box2"}));
        assert_eq!(info, json!({"name": "box2", "target": "ssh://box:2200", "command": cmd, "roots": [dot], "agent": true}));
        assert_eq!(err(&mut pair.local, "machines.get", json!({"name": "nope"})).0, "not-found");
        assert_eq!(ok(&mut pair.local, "machines.rm", json!({"name": "box"})), json!({"ok": true}));
        let (code, _) = err(&mut pair.local, "machines.rm", json!({"name": "box"}));
        assert_eq!(code, "not-found");
        assert_eq!(ok(&mut pair.local, "machines.list", Value::Null).as_array().unwrap().len(), 1);
        pair.finish();
    }

    #[test]
    fn probe_and_add_dial_with_the_command() {
        let dialed: Arc<std::sync::Mutex<Vec<Option<String>>>> = Arc::default();
        let log = dialed.clone();
        let mut pair = Pair::with("mach-cmd", "0.6.0", move |o| {
            let inner = o.dial.take().unwrap();
            o.dial = Some(Arc::new(move |e: &Entry| {
                log.lock().unwrap().push(e.command.clone());
                inner(e)
            }));
        });
        let cmd = "~/.local/bin/devsandbox";
        ok(&mut pair.local, "machines.probe", json!({"target": "me@box", "command": cmd}));
        ok(&mut pair.local, "machines.probe", json!({"target": "me@box"}));
        ok(&mut pair.local, "machines.add", json!({"target": "me@box", "roots": ["~/.devsandboxes"], "command": cmd}));
        assert_eq!(*dialed.lock().unwrap(), [Some(cmd.to_string()), None, Some(cmd.to_string())]);
        pair.finish();
    }

    #[test]
    fn set_flips_agent_and_saves_it() {
        let base = scratch("mach-set");
        let registry = base.join("machines.toml");
        let entry = Entry { name: "box".into(), target: "me@box".into(), roots: vec!["/r".into()], command: None, agent: false };
        crate::serve::machines::update(&registry, |e| e.push(entry.clone())).unwrap();
        let daemon = spawn_daemon(&base.join("local"), local_opts(&base, None));
        let mut local = client_at(&base.join("local"));
        let agent = || crate::serve::machines::load(&registry).unwrap()[0].agent;
        assert_eq!(ok(&mut local, "machines.set", json!({"name": "box", "agent": true})), json!({"ok": true}));
        assert!(agent());
        assert!(std::fs::read_to_string(&registry).unwrap().contains("agent = true"));
        assert_eq!(ok(&mut local, "machines.get", json!({"name": "box"}))["agent"], true);
        // Again is a no-op, not an error.
        assert_eq!(ok(&mut local, "machines.set", json!({"name": "box", "agent": true})), json!({"ok": true}));
        assert_eq!(ok(&mut local, "machines.set", json!({"name": "box", "agent": false})), json!({"ok": true}));
        assert!(!agent());
        assert!(!std::fs::read_to_string(&registry).unwrap().contains("agent"));
        let (code, message) = err(&mut local, "machines.set", json!({"name": "nope", "agent": true}));
        assert_eq!((code.as_str(), message.as_str()), ("not-found", "no machine named `nope`"));
        assert_eq!(err(&mut local, "machines.set", json!({"name": "box"})).0, "invalid");
        assert_eq!(crate::serve::machines::load(&registry).unwrap(), vec![entry]);
        drop(local);
        assert_eq!(daemon.join().unwrap().unwrap(), Exit::Idle);
        let _ = std::fs::remove_dir_all(&base);
    }

    #[test]
    fn another_semver_is_a_warning() {
        let mut pair = Pair::new("mach-skew", "0.5.0");
        let r = ok(&mut pair.local, "machines.add", json!({"target": "box", "roots": ["~/.devsandboxes"]}));
        assert_eq!(r["warning"], "box runs devsandbox 0.5.0, this is 0.6.0");
        pair.finish();
    }

    #[test]
    fn npm_typings_match() {
        use crate::serve::dts::{assert_matches, prop_type, union_members};
        let root = || RootCandidate { root: "/cfg".into(), sandboxes: vec![], error: None };
        assert_matches("MachineProbe", &to_value(Probed { version: "0.6.0".into(), protocol: 1, roots: vec![root()] }).unwrap());
        let added = Added { name: "m".into(), roots: vec!["/cfg".into()], version: "0.6.0".into(), warning: None };
        assert_matches("MachineAdded", &to_value(added).unwrap());
        let row = MachineRow {
            name: "m".into(),
            target: "m".into(),
            roots: vec![],
            command: None,
            agent: false,
            state: LinkState::Idle,
            error: None,
            version: None,
        };
        assert_matches("MachineRow", &to_value(row).unwrap());
        let info = MachineInfo { name: "m".into(), target: "m".into(), command: None, roots: vec![], agent: false };
        assert_matches("MachineInfo", &to_value(info).unwrap());
        assert_matches("MachineHeld", &to_value(Held { ok: true, agent: false }).unwrap());
        assert_eq!(union_members("LinkState"), ["'idle'", "'connecting'", "'connected'", "'unreachable'"]);
        for (method, result) in [
            ("machines.probe", "MachineProbe"),
            ("machines.add", "MachineAdded"),
            ("machines.list", "MachineRow[]"),
            ("machines.rm", "OkResult"),
            ("machines.set", "OkResult"),
            ("machines.get", "MachineInfo"),
            ("machines.hold", "MachineHeld"),
        ] {
            let ty = prop_type("ApiMethods", method).unwrap_or_else(|| panic!("ApiMethods lacks `{method}`"));
            assert!(ty.contains(&format!("result: {result} ")), "{method}: {ty}");
        }
    }
}