agent-loadout 1.0.0

Loadout CLI: manage MCP servers, skills and secret references for AI coding clients
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
//! `loadout remote add folder|git`, `ls`, `rm`, `push`, `pull`, `machines`,
//! `token` and `create-github` (SPEC 9.6 to 9.9, 14). Remotes are
//! machine-local (`state/remotes.toml`); none exists until one is added.
//!
//! Exit codes: a remote that cannot be reached is an error for `push` and
//! `pull` themselves (1), never for local work; `pull` exits 4 when fields
//! conflict (`state/merge/MERGE_STATE.json` lists them).

use std::io::IsTerminal;
use std::path::Path;

use agent_loadout_core::remote::config::{
    FolderMode, RemoteConfig, RemoteKind, RemotesFile, check_name,
};
use agent_loadout_core::remote::credential::token_account;
use agent_loadout_core::remote::folder::{self, MachineEntry, PullReport, SiblingPull};
use agent_loadout_core::remote::gh;
use agent_loadout_core::remote::git::{self, GitAuth, SystemRunner};
use agent_loadout_core::remote::sync::{self, PushResult, Pushed, SyncEnv};
use agent_loadout_core::remote::sync_artifacts::Artifact;
use agent_loadout_core::remote::unc::SystemUnc;
use agent_loadout_core::store::Store;
use agent_loadout_core::store::merge::MergeOutcome;
use agent_loadout_core::{CoreError, ErrorCode, brand};
use clap::{Args, Subcommand};
use serde_json::{Value, json};

use super::{
    Context, EXIT_UNRESOLVED, JSON_SCHEMA, error_json, path_json, print_json, require_store,
};

static UNC: SystemUnc = SystemUnc;
static RUNNER: SystemRunner = SystemRunner {
    idle_timeout: git::IDLE_TIMEOUT,
};

#[derive(Debug, Args)]
pub struct RemoteArgs {
    #[command(subcommand)]
    command: RemoteCommand,
}

#[derive(Debug, Subcommand)]
enum RemoteCommand {
    /// Add a remote.
    Add {
        #[command(subcommand)]
        kind: AddKind,
    },
    /// List this machine's remotes.
    Ls,
    /// Remove a remote (its files stay where they are; a stored token is
    /// deleted).
    Rm { name: String },
    /// Push the store to one remote [default: every remote].
    Push { name: Option<String> },
    /// Pull and merge other machines' changes [default: every remote].
    Pull { name: Option<String> },
    /// List the machines a folder remote holds.
    Machines { name: String },
    /// Store the token piped on stdin for a git remote with `auth = token`.
    Token {
        name: String,
        /// Delete the stored token instead.
        #[arg(long)]
        clear: bool,
    },
    /// Create a private GitHub repository with the GitHub CLI and add it as
    /// a git remote (`auth = gh`).
    CreateGithub {
        /// Repository name: NAME or OWNER/NAME.
        repo: String,
        /// Name of the remote to add.
        #[arg(long, default_value = "github")]
        name: String,
    },
}

#[derive(Debug, Subcommand)]
enum AddKind {
    /// A local or network folder (USB drive, NAS share, synced folder).
    Folder {
        name: String,
        path: std::path::PathBuf,
        /// repo-per-machine (default), mirror or archive.
        #[arg(long, default_value = "repo-per-machine")]
        mode: String,
        /// Archive mode: archives to keep [default: 10].
        #[arg(long)]
        keep: Option<u32>,
    },
    /// Any git URL (https, ssh or scp-style), through the system git.
    Git {
        name: String,
        url: String,
        /// system (default: your git's own credentials), gh (GitHub CLI)
        /// or token (a token stored with `remote token`).
        #[arg(long, default_value = "system")]
        auth: String,
        /// With --auth token: the username sent with the token
        /// [default: x-access-token].
        #[arg(long)]
        username: Option<String>,
    },
}

pub fn run(ctx: &Context, args: &RemoteArgs) -> Result<u8, CoreError> {
    match &args.command {
        RemoteCommand::Add {
            kind:
                AddKind::Folder {
                    name,
                    path,
                    mode,
                    keep,
                },
        } => add_folder(ctx, name, path, mode, *keep).map(|()| 0),
        RemoteCommand::Add {
            kind:
                AddKind::Git {
                    name,
                    url,
                    auth,
                    username,
                },
        } => add_git(ctx, name, url, auth, username.as_deref()).map(|()| 0),
        RemoteCommand::Ls => list(ctx).map(|()| 0),
        RemoteCommand::Rm { name } => remove(ctx, name).map(|()| 0),
        RemoteCommand::Push { name } => push(ctx, name.as_deref()),
        RemoteCommand::Pull { name } => pull(ctx, name.as_deref()),
        RemoteCommand::Machines { name } => machines(ctx, name).map(|()| 0),
        RemoteCommand::Token { name, clear } => token(ctx, name, *clear).map(|()| 0),
        RemoteCommand::CreateGithub { repo, name } => create_github(ctx, repo, name).map(|()| 0),
    }
}

fn remotes(ctx: &Context) -> Result<RemotesFile, CoreError> {
    RemotesFile::load(ctx.fs(), &ctx.home.state_dir())
}

fn add_folder(
    ctx: &Context,
    name: &str,
    path: &Path,
    mode: &str,
    keep: Option<u32>,
) -> Result<(), CoreError> {
    let mode = FolderMode::parse(mode)?;
    let path = std::path::absolute(path).map_err(|err| {
        CoreError::new(ErrorCode::InvalidPath, format!("invalid path: {err}")).with_path(path)
    })?;
    if path.starts_with(&ctx.home.path) {
        return Err(CoreError::new(
            ErrorCode::InvalidPath,
            format!("a remote cannot live inside {}", brand::HOME_ENV_VAR),
        )
        .with_path(&path));
    }
    let mut remote = RemoteConfig::folder(name, &path.to_string_lossy(), mode);
    remote.keep = keep;
    let mut file = remotes(ctx)?;
    file.add(remote)?;
    file.save(ctx.fs(), &ctx.home.state_dir())?;
    let reachable = ctx.fs().is_dir(&path);
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "added": name,
            "path": path_json(&path),
            "mode": mode.as_str(),
            "reachable": reachable,
        }));
    } else {
        println!(
            "Added folder remote {name} at {} ({}).",
            path.display(),
            mode.as_str()
        );
        if !reachable {
            eprintln!(
                "warning[remote_unreachable]: {} is not there now; pushes try again later",
                path.display()
            );
        }
    }
    Ok(())
}

fn add_git(
    ctx: &Context,
    name: &str,
    url: &str,
    auth: &str,
    username: Option<&str>,
) -> Result<(), CoreError> {
    let auth = GitAuth::parse(Some(auth))?;
    if url.trim().is_empty() || url.starts_with('-') {
        return Err(CoreError::new(
            ErrorCode::InvalidValue,
            format!("invalid git URL {url:?}"),
        ));
    }
    if auth == GitAuth::Gh && !git::is_github_url(url) {
        return Err(CoreError::new(
            ErrorCode::InvalidValue,
            "auth gh is for github.com URLs; use system or token for other hosts",
        ));
    }
    let mut remote = RemoteConfig::git(name, url, auth);
    remote.username = username.map(str::to_owned);
    let mut file = remotes(ctx)?;
    file.add(remote)?;
    file.save(ctx.fs(), &ctx.home.state_dir())?;
    let mut warnings = Vec::new();
    if git::locate(&ctx.env, ctx.fs()).is_none() {
        warnings.push(format!("git is not on PATH; pushes to {name} need it"));
    }
    if auth == GitAuth::Gh && gh::locate(&ctx.env, ctx.fs()).is_none() {
        warnings.push("the GitHub CLI (gh) is not on PATH".to_owned());
    }
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "added": name,
            "url": url,
            "auth": auth.as_str(),
            "warnings": warnings
                .iter()
                .map(|message| json!({ "code": "tool_not_found", "message": message }))
                .collect::<Vec<_>>(),
        }));
        return Ok(());
    }
    println!("Added git remote {name} at {url} (auth {}).", auth.as_str());
    for message in &warnings {
        eprintln!("warning[tool_not_found]: {message}");
    }
    if auth == GitAuth::Token {
        println!(
            "Next: pipe the token into `{} remote token {name}`.",
            brand::BIN_NAME
        );
    }
    Ok(())
}

fn list(ctx: &Context) -> Result<(), CoreError> {
    let file = remotes(ctx)?;
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "remotes": file.remotes.iter().map(remote_json).collect::<Vec<_>>(),
        }));
        return Ok(());
    }
    if file.remotes.is_empty() {
        println!("No backup remote.");
    }
    for remote in &file.remotes {
        let detail = match remote.kind {
            RemoteKind::Folder => remote.folder_mode().as_str().to_owned(),
            RemoteKind::Git => format!("auth {}", remote.auth.as_deref().unwrap_or("system")),
        };
        println!(
            "{:<20} {:<7} {:<17} {}",
            remote.name,
            remote.kind.as_str(),
            detail,
            remote
                .path
                .as_deref()
                .or(remote.url.as_deref())
                .unwrap_or_default()
        );
    }
    Ok(())
}

fn remote_json(remote: &RemoteConfig) -> Value {
    json!({
        "name": remote.name,
        "kind": remote.kind.as_str(),
        "path": remote.path,
        "url": remote.url,
        "mode": remote.mode.map(FolderMode::as_str),
        "auth": remote.auth,
        "username": remote.username,
        "push": remote.push,
        "pull": remote.pull,
    })
}

fn remove(ctx: &Context, name: &str) -> Result<(), CoreError> {
    let mut file = remotes(ctx)?;
    let removed = file.remove(name)?;
    file.save(ctx.fs(), &ctx.home.state_dir())?;
    let token_deleted = removed.kind == RemoteKind::Git
        && ctx
            .keyring()
            .delete(brand::KEYRING_SERVICE, &token_account(name))
            .unwrap_or(false);
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "removed": name,
            "token_deleted": token_deleted,
        }));
    } else {
        println!("Removed remote {name}; its files were left in place.");
        if token_deleted {
            println!("Deleted its token from the keyring.");
        }
    }
    Ok(())
}

fn git_remote<'a>(file: &'a RemotesFile, name: &str) -> Result<&'a RemoteConfig, CoreError> {
    let remote = file
        .get(name)
        .ok_or_else(|| CoreError::new(ErrorCode::NotFound, format!("no remote named {name:?}")))?;
    if remote.kind != RemoteKind::Git {
        return Err(CoreError::new(
            ErrorCode::InvalidValue,
            format!("{name} is not a git remote"),
        ));
    }
    Ok(remote)
}

fn token(ctx: &Context, name: &str, clear: bool) -> Result<(), CoreError> {
    let file = remotes(ctx)?;
    let remote = git_remote(&file, name)?;
    let account = token_account(name);
    if clear {
        let deleted = ctx
            .keyring()
            .delete(brand::KEYRING_SERVICE, &account)
            .map_err(|resolution| resolution.into_error(&account))?;
        if ctx.json {
            print_json(&json!({ "schema": JSON_SCHEMA, "remote": name, "deleted": deleted }));
        } else if deleted {
            println!("Deleted the token of {name}.");
        } else {
            println!("{name} had no token stored.");
        }
        return Ok(());
    }
    if GitAuth::parse(remote.auth.as_deref())? != GitAuth::Token {
        return Err(CoreError::new(
            ErrorCode::InvalidValue,
            format!("{name} does not use auth token"),
        ));
    }
    let value = super::secrets::read_value()?;
    ctx.keyring()
        .set(brand::KEYRING_SERVICE, &account, &value)
        .map_err(|resolution| resolution.into_error(&account))?;
    if ctx.json {
        print_json(&json!({ "schema": JSON_SCHEMA, "remote": name, "stored": true }));
    } else {
        println!("Stored the token of {name} in {}.", ctx.keyring().name());
    }
    Ok(())
}

fn create_github(ctx: &Context, repo: &str, name: &str) -> Result<(), CoreError> {
    // Checked before gh creates anything, so a bad name leaves no
    // repository behind.
    check_name(name)?;
    let mut file = remotes(ctx)?;
    if file.get(name).is_some() {
        return Err(CoreError::new(
            ErrorCode::AlreadyExists,
            format!("a remote named {name:?} exists already"),
        ));
    }
    let gh_path = gh::locate(&ctx.env, ctx.fs()).ok_or_else(|| {
        CoreError::new(
            ErrorCode::ToolNotFound,
            "the GitHub CLI (gh) is not on PATH; create the repository on any host and add it \
             with `remote add git`",
        )
    })?;
    let url = gh::create_private_repo(&ctx.env, &gh_path, &RUNNER, repo)?;
    file.add(RemoteConfig::git(name, &url, GitAuth::Gh))?;
    file.save(ctx.fs(), &ctx.home.state_dir())?;
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "created": url,
            "added": name,
            "auth": GitAuth::Gh.as_str(),
        }));
    } else {
        println!("Created private repository {url} and added it as git remote {name} (auth gh).");
    }
    Ok(())
}

/// Git's progress, shown on a terminal only, on one line per remote: the
/// line ends when the next remote starts and when the guard drops (as one
/// guard per remote did before the push and pull moved into core).
struct Progress {
    show: bool,
    /// The remote whose line is on screen.
    shown: Option<String>,
}

impl Progress {
    fn new() -> Self {
        Self {
            show: std::io::stderr().is_terminal(),
            shown: None,
        }
    }

    fn line(&mut self, remote: &str, line: &str) {
        if !self.show {
            return;
        }
        if self.shown.as_deref().is_some_and(|shown| shown != remote) {
            eprintln!();
        }
        eprint!("\r{line}\x1b[K");
        self.shown = Some(remote.to_owned());
    }
}

impl Drop for Progress {
    fn drop(&mut self) {
        if self.shown.is_some() {
            eprintln!();
        }
    }
}

fn sync_env() -> SyncEnv<'static> {
    SyncEnv {
        unc: &UNC,
        runner: &RUNNER,
        helper: std::env::current_exe().ok(),
        app_version: env!("CARGO_PKG_VERSION"),
    }
}

/// Pushes to the selected remotes; one result per remote, recorded in
/// `state/remote-status.toml`.
pub fn push_to(
    store: &Store<'_>,
    file: &RemotesFile,
    name: Option<&str>,
) -> Result<Vec<PushResult>, CoreError> {
    let mut progress = Progress::new();
    sync::push_to(store, file, name, &sync_env(), &mut |remote, line| {
        progress.line(remote, line)
    })
}

pub fn push_json(store: &Store<'_>, results: &[PushResult]) -> Value {
    Value::Array(
        results
            .iter()
            .map(|(name, result)| match result {
                Ok(Pushed::Folder(outcome)) => json!({
                    "remote": name,
                    "ok": true,
                    "kind": "folder",
                    "dir": path_json(&outcome.dir),
                    "mode": outcome.mode.as_str(),
                    "head": outcome.head.as_str(),
                    "archive": outcome.archive,
                }),
                Ok(Pushed::Git(outcome)) => json!({
                    "remote": name,
                    "ok": true,
                    "kind": "git",
                    "head": outcome.head.as_str(),
                    "pulled": outcome.pulled.as_ref().map(|report| pull_json(store, report)),
                }),
                Err(err) => json!({ "remote": name, "ok": false, "error": error_json(err) }),
            })
            .collect(),
    )
}

pub fn print_pushes(store: &Store<'_>, results: &[PushResult]) {
    for (name, result) in results {
        match result {
            Ok(Pushed::Folder(outcome)) => println!(
                "Pushed {} to {name} ({}, {}).",
                outcome.head.short(),
                outcome.mode.as_str(),
                outcome.dir.display()
            ),
            Ok(Pushed::Git(outcome)) => {
                if let Some(report) = &outcome.pulled {
                    println!("{name} had changes this machine lacked; pulled them first:");
                    print_pull(store, report);
                }
                println!("Pushed {} to {name}.", outcome.head.short());
            }
            Err(err) => eprintln!("warning[{}]: {name}: {err}", err.code()),
        }
    }
}

fn push(ctx: &Context, name: Option<&str>) -> Result<u8, CoreError> {
    let store = require_store(ctx, "nothing to push")?;
    let file = remotes(ctx)?;
    if file.remotes.is_empty() {
        return Err(no_remote());
    }
    let results = push_to(&store, &file, name)?;
    if ctx.json {
        print_json(&json!({ "schema": JSON_SCHEMA, "pushes": push_json(&store, &results) }));
    } else {
        print_pushes(&store, &results);
    }
    // A push stopped by conflicting changes on the remote is unresolved
    // (4, SPEC 14); any other failure is 1.
    let failed = results.iter().filter_map(|(_, r)| r.as_ref().err());
    Ok(
        if failed
            .clone()
            .any(|err| err.code() == ErrorCode::MergeConflict)
        {
            EXIT_UNRESOLVED
        } else if failed.count() > 0 {
            1
        } else {
            0
        },
    )
}

fn no_remote() -> CoreError {
    CoreError::new(
        ErrorCode::NotFound,
        format!(
            "no backup remote; add one with `{bin} remote add folder <NAME> <PATH>` or `{bin} \
             remote add git <NAME> <URL>`",
            bin = brand::BIN_NAME
        ),
    )
}

fn pull(ctx: &Context, name: Option<&str>) -> Result<u8, CoreError> {
    let store = require_store(ctx, "nothing to merge into")?;
    let file = remotes(ctx)?;
    if file.remotes.is_empty() {
        return Err(no_remote());
    }
    let mut progress = Progress::new();
    let reports = sync::pull_from(&store, &file, name, &sync_env(), &mut |remote, line| {
        progress.line(remote, line)
    })?;
    drop(progress);
    let conflicts = reports
        .iter()
        .any(|(_, r)| r.as_ref().is_ok_and(|r| r.conflicts() > 0));
    // A remote that failed, or a machine whose merge failed for another
    // reason than objects still on their way (`remote_incomplete`).
    let failed = reports.iter().any(|(_, r)| match r {
        Err(_) => true,
        Ok(report) => report.siblings.iter().any(|s| {
            s.outcome
                .as_ref()
                .is_err_and(|err| err.code() != ErrorCode::RemoteIncomplete)
        }),
    });
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "pulls": reports.iter().map(|(name, result)| match result {
                Ok(report) => pull_json(&store, report),
                Err(err) => json!({ "remote": name, "ok": false, "error": error_json(err) }),
            }).collect::<Vec<_>>(),
        }));
    } else {
        for (name, result) in &reports {
            match result {
                Ok(report) => print_pull(&store, report),
                Err(err) => eprintln!("warning[{}]: {name}: {err}", err.code()),
            }
        }
    }
    Ok(if conflicts {
        EXIT_UNRESOLVED
    } else if failed {
        1
    } else {
        0
    })
}

fn outcome_json(sibling: &SiblingPull) -> Value {
    let (outcome, extra) = match &sibling.outcome {
        Ok(MergeOutcome::UpToDate) => ("up_to_date", Value::Null),
        Ok(MergeOutcome::FastForward(id)) => ("fast_forward", json!(id.as_str())),
        Ok(MergeOutcome::Merged(id)) => ("merged", json!(id.as_str())),
        Ok(MergeOutcome::Conflicts { conflicts, .. }) => (
            "conflicts",
            Value::Array(
                conflicts
                    .iter()
                    .map(|c| json!({ "file": c.file, "field": c.field }))
                    .collect(),
            ),
        ),
        Err(err) => ("error", error_json(err)),
    };
    json!({
        "machine": sibling.machine_id,
        "label": sibling.label,
        "outcome": outcome,
        "detail": extra,
    })
}

fn artifacts_json(artifacts: &[Artifact]) -> Value {
    Value::Array(
        artifacts
            .iter()
            .map(|a| json!({ "code": "sync_artifact_ignored", "path": path_json(&a.path), "tool": a.tool }))
            .collect(),
    )
}

fn pull_json(store: &Store<'_>, report: &PullReport) -> Value {
    let (servers, skills) = sync::changed_by_pull(store, report);
    json!({
        "remote": report.remote,
        "ok": true,
        "tag": report.tag,
        "head_before": report.head_before.as_str(),
        "head_after": report.head_after.as_str(),
        "machines": report.siblings.iter().map(outcome_json).collect::<Vec<_>>(),
        "tags_imported": report.tags_imported,
        "tags_moved": report.tags_moved,
        "changed_servers": servers,
        "changed_skills": skills,
        "conflicts": report.conflicts(),
        "warnings": artifacts_json(&report.artifacts),
    })
}

fn print_pull(store: &Store<'_>, report: &PullReport) {
    for tag in &report.tags_moved {
        eprintln!(
            "warning[tag_moved]: {}: tag {tag} now points where the remote's tag of that name does",
            report.remote
        );
    }
    for sibling in &report.siblings {
        let line = match &sibling.outcome {
            Ok(MergeOutcome::UpToDate) => "up to date".to_owned(),
            Ok(MergeOutcome::FastForward(id)) => format!("fast-forward to {}", id.short()),
            Ok(MergeOutcome::Merged(id)) => format!("merged as {}", id.short()),
            Ok(MergeOutcome::Conflicts { conflicts, .. }) => format!(
                "{} conflicting field(s), nothing merged; see state/merge/MERGE_STATE.json",
                conflicts.len()
            ),
            Err(err) => format!("error[{}]: {err}", err.code()),
        };
        match &sibling.machine_id {
            Some(id) => println!("{}/{} ({id}): {line}", report.remote, sibling.label),
            None => println!("{}: {line}", report.remote),
        }
    }
    for artifact in &report.artifacts {
        eprintln!(
            "info[sync_artifact_ignored]: {} ({}); left in place",
            artifact.path.display(),
            artifact.tool
        );
    }
    let (servers, skills) = sync::changed_by_pull(store, report);
    if report.head_before != report.head_after {
        println!(
            "{servers} server(s), {skills} skill(s) changed by pull. Review with `{bin} diff` and \
             apply with `{bin} apply`.",
            bin = brand::BIN_NAME
        );
    }
}

pub fn machine_json(entry: &MachineEntry) -> Value {
    json!({
        "store_id": entry.store_id,
        "machine_id": entry.machine_id,
        "label": entry.manifest.label,
        "os": entry.manifest.os,
        "pushed_at": entry.manifest.pushed_at,
        "head": entry.manifest.head,
        "subject": entry.subject,
        "dir": path_json(&entry.dir),
    })
}

pub fn print_machines(entries: &[MachineEntry]) {
    for entry in entries {
        println!(
            "{}  {:<16} {:<8} {}  {}",
            entry.machine_id,
            entry.manifest.label,
            entry.manifest.os,
            entry.manifest.pushed_at,
            entry.subject.as_deref().unwrap_or("")
        );
    }
}

fn machines(ctx: &Context, name: &str) -> Result<(), CoreError> {
    let file = remotes(ctx)?;
    let remote = file
        .get(name)
        .ok_or_else(|| CoreError::new(ErrorCode::NotFound, format!("no remote named {name:?}")))?;
    if remote.kind != RemoteKind::Folder {
        return Err(CoreError::new(
            ErrorCode::InvalidValue,
            format!("{name} is a git remote; only folder remotes hold machine subfolders"),
        ));
    }
    let root = folder::reachable_root(ctx.fs(), ctx.env.os(), &UNC, remote)?;
    let listing = folder::list(ctx.fs(), &root, None)?;
    if ctx.json {
        print_json(&json!({
            "schema": JSON_SCHEMA,
            "machines": listing.machines.iter().map(machine_json).collect::<Vec<_>>(),
            "warnings": artifacts_json(&listing.artifacts),
        }));
        return Ok(());
    }
    if listing.machines.is_empty() {
        println!("No machines in {name}.");
    }
    print_machines(&listing.machines);
    for artifact in &listing.artifacts {
        eprintln!(
            "info[sync_artifact_ignored]: {} ({})",
            artifact.path.display(),
            artifact.tool
        );
    }
    Ok(())
}