runner-manager 0.4.34

Local-first autoscaling manager for ephemeral GitHub Actions self-hosted runners, with a CLI and a Ratatui TUI.
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! `host cache`, `repo cache` and `org cache`: the operator's half of
//! `runner_manager_platform::dependency_cache`.
//!
//! Every command here edits `caches.toml` in the configuration directory, and
//! the daemon reads that file at every launch, so a change reaches the next
//! runner without a restart. On a managed WSL host, `--host wsl:NAME` forwards
//! the command line to the Linux binary like every other command, so the file
//! it edits, the defaults it shows and the paths it validates are the Linux
//! host's own; nothing resolved on Windows is written there.

use std::collections::BTreeMap;
use std::io::{self, Write};
use std::path::PathBuf;
use std::sync::Arc;

use runner_manager_agent::dependency_caches::DependencyCaches;
use runner_manager_domain::model::{Host, HostId, ScaleTarget, Timestamp};
use runner_manager_domain::policy::ScalePolicy;
use runner_manager_domain::store::Store;
use runner_manager_platform::dependency_cache::{
    self, CacheConfig, CacheError, CacheUsage, ResolvedRoot, Selection, Sharing, TOOLS, VarValue,
};
use runner_manager_platform::runner_env::RunnerPlatform;
use runner_manager_platform::runner_root::RootOwner;
use serde::Serialize;

use super::workspace;
use super::{
    CacheNamespaceArgs, CacheToolArgs, CacheToolState, CliError, Context, Failure,
    HostCacheCommand, OrgCacheCommand, RepoCacheCommand, write_failed,
};

// ---------------------------------------------------------------------------
// Shared resolution
// ---------------------------------------------------------------------------

fn config_path(context: &Context) -> PathBuf {
    dependency_cache::config_path_in(context.paths().config_dir())
}

fn cache_failure(error: CacheError) -> CliError {
    match error {
        CacheError::Io { .. } | CacheError::Lock(_) => {
            CliError::new(Failure::LocalState, error.to_string())
        }
        _ => CliError::new(Failure::InvalidArgument, error.to_string()),
    }
}

/// The configuration, or a failure that says where the file is.
fn load(context: &Context) -> Result<CacheConfig, CliError> {
    let path = config_path(context);
    CacheConfig::load(&path).map_err(|error| match error {
        CacheError::Io { .. } => cache_failure(error),
        _ => CliError::with_remedy(
            Failure::InvalidArgument,
            format!("{error}; no runner starts until it is fixed"),
            format!("edit or delete {}", path.display()),
        ),
    })
}

fn save(context: &Context, config: &CacheConfig) -> Result<(), CliError> {
    config.save(&config_path(context)).map_err(cache_failure)
}

/// The cache root for this host's current runner root.
fn root(
    context: &Context,
    host: Option<&Host>,
    config: &CacheConfig,
) -> Result<ResolvedRoot, String> {
    let runner_root = workspace::host_root(context.paths(), host);
    dependency_cache::resolve_root(
        config,
        runner_root.effective.as_ref().map(|root| root.as_path()),
        dependency_cache::platform_fallback_root(context.paths()),
    )
}

/// What the daemon launches with: the settings file and a runner root read
/// from the journal at each launch.
#[must_use]
pub fn daemon_caches(
    context: &Context,
    store: Arc<dyn Store>,
    host_id: HostId,
) -> DependencyCaches {
    let paths = context.paths().clone();
    let runner_root = Arc::new(move || {
        let host = store.host(host_id).ok().flatten();
        workspace::host_root(&paths, host.as_ref())
            .effective
            .map(|root| root.as_path().to_path_buf())
    });
    DependencyCaches::new(
        config_path(context),
        runner_root,
        dependency_cache::platform_fallback_root(context.paths()),
    )
}

// ---------------------------------------------------------------------------
// The snapshot `status` and `host show` print
// ---------------------------------------------------------------------------

/// The caches in `status --json`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CacheSnapshot {
    pub enabled: bool,
    pub root: Option<String>,
    pub root_source: Option<&'static str>,
    /// Why no root resolves, or why `caches.toml` cannot be used.
    pub problem: Option<String>,
    pub max_bytes: Option<u64>,
    /// From the daemon's last measurement; `null` before the first.
    pub used_bytes: Option<u64>,
    pub namespaces: Option<usize>,
    pub measured_at: Option<Timestamp>,
}

impl CacheSnapshot {
    /// One line for `status` and `host show`.
    #[must_use]
    pub fn line(&self) -> String {
        if let Some(problem) = &self.problem {
            return format!("unusable: {problem}");
        }
        if !self.enabled {
            return "off (`host cache set-enabled --enabled true`)".to_owned();
        }
        let root = self.root.as_deref().unwrap_or("?");
        let cap = self
            .max_bytes
            .map_or_else(|| "no cap".to_owned(), dependency_cache::human_bytes);
        match self.used_bytes {
            Some(used) => format!("{} of {cap} at {root}", dependency_cache::human_bytes(used)),
            None => format!("{cap} cap at {root}, not measured yet"),
        }
    }
}

/// The caches as `status` reports them.
#[must_use]
pub fn snapshot(context: &Context, host: Option<&Host>) -> CacheSnapshot {
    let config = match CacheConfig::load(&config_path(context)) {
        Ok(config) => config,
        Err(error) => {
            return CacheSnapshot {
                enabled: false,
                root: None,
                root_source: None,
                problem: Some(error.to_string()),
                max_bytes: None,
                used_bytes: None,
                namespaces: None,
                measured_at: None,
            };
        }
    };
    let resolved = root(context, host, &config);
    let usage = resolved
        .as_ref()
        .ok()
        .and_then(|root| CacheUsage::read(&root.path));
    CacheSnapshot {
        enabled: config.host_enabled(),
        root: resolved
            .as_ref()
            .ok()
            .map(|root| root.path.display().to_string()),
        root_source: resolved.as_ref().ok().map(|root| root.source.as_token()),
        problem: resolved.as_ref().err().cloned(),
        max_bytes: config.max_bytes(),
        used_bytes: usage.as_ref().map(|usage| usage.total_bytes),
        namespaces: usage.as_ref().map(|usage| usage.namespaces.len()),
        measured_at: usage.map(|usage| usage.measured_at),
    }
}

/// The cache root, when caches are on and it resolves: `host doctor` checks
/// it like a runner root.
#[must_use]
pub fn active_root(context: &Context, host: Option<&Host>) -> Option<PathBuf> {
    let config = CacheConfig::load(&config_path(context)).ok()?;
    if !config.host_enabled() {
        return None;
    }
    root(context, host, &config).ok().map(|root| root.path)
}

// ---------------------------------------------------------------------------
// host cache
// ---------------------------------------------------------------------------

/// # Errors
/// [`Failure::InvalidArgument`] for a refused value or an unusable file, and
/// [`Failure::LocalState`] when it cannot be read or written.
pub fn dispatch_host(
    context: &Context,
    command: &HostCacheCommand,
    out: &mut dyn Write,
) -> Result<(), CliError> {
    let failed = write_failed("the dependency caches");
    let mut config = load(context)?;
    match command {
        HostCacheCommand::Show => {
            let (host, policies) = host_and_policies(context)?;
            write_host_show(context, host.as_ref(), &policies, &config, out).map_err(failed)?;
        }
        HostCacheCommand::SetEnabled(args) => {
            config.enabled = (!args.enabled).then_some(false);
            save(context, &config)?;
            writeln!(
                out,
                "Dependency caches are {} for native runners started from now on.",
                if args.enabled { "on" } else { "off" }
            )
            .map_err(failed)?;
        }
        HostCacheCommand::SetRoot(args) => {
            dependency_cache::validate_root_text(&args.path).map_err(|error| {
                CliError::with_remedy(
                    Failure::InvalidArgument,
                    error.to_string(),
                    RootOwner::DependencyCache.remediation(),
                )
            })?;
            let store = context.store()?;
            // The checks every runner root passes: local, writable, outside
            // application data and not overlapping any runner root, so a
            // network share, a WSL `/mnt/<drive>` 9p mount or a path inside a
            // workspace that is removed recursively is refused.
            let root =
                workspace::check_root(context, &store, &RootOwner::DependencyCache, &args.path)?;
            config.root = Some(root.as_str().to_owned());
            save(context, &config)?;
            writeln!(
                out,
                "Dependency caches go under {} for runners started from now on. Nothing under \
                 the previous root was moved or deleted.",
                root.as_str()
            )
            .map_err(failed)?;
            if RunnerPlatform::current() == RunnerPlatform::Linux {
                // The unit's `ProtectSystem=strict` leaves only the service's
                // own directories writable, and runners inherit that sandbox.
                writeln!(
                    out,
                    "Note: a systemd service can write only its own directories. Add \
                     `ReadWritePaths={}` to the unit with a drop-in, or runners start without \
                     caches (`dependency_cache_unavailable` in the log).",
                    root.as_str()
                )
                .map_err(failed)?;
            }
        }
        HostCacheCommand::ResetRoot => {
            config.root = None;
            save(context, &config)?;
            let store = context.store()?;
            let host = super::host::local_host(&store)?;
            let resolved = root(context, host.as_ref(), &config);
            writeln!(
                out,
                "Dependency caches go under {} for runners started from now on.",
                resolved.map_or_else(|why| why, |root| root.path.display().to_string())
            )
            .map_err(failed)?;
        }
        HostCacheCommand::SetMaxSize(args) => {
            config.max_size_gib = Some(args.gib);
            save(context, &config)?;
            if args.gib == 0 {
                writeln!(out, "The dependency caches have no size cap.").map_err(failed)?;
            } else {
                writeln!(
                    out,
                    "The dependency caches are capped at {} GiB; the service removes the least \
                     recently used idle namespaces above it.",
                    args.gib
                )
                .map_err(failed)?;
            }
        }
        HostCacheCommand::SetTool(args) => {
            let id = known_tool(&args.tool)?;
            set_tool_state(&mut config.tools, id, args.state);
            save(context, &config)?;
            writeln!(
                out,
                "{id} is {} for every policy on this host unless a repository says otherwise.",
                state_word(args.state, id)
            )
            .map_err(failed)?;
        }
        HostCacheCommand::Prune => {
            let store = context.store()?;
            let host = super::host::local_host(&store)?;
            let resolved = root(context, host.as_ref(), &config)
                .map_err(|why| CliError::new(Failure::LocalState, why))?;
            if !resolved.path.is_dir() {
                writeln!(
                    out,
                    "{} does not exist yet; there is nothing to prune.",
                    resolved.path.display()
                )
                .map_err(failed)?;
                return Ok(());
            }
            let usage = dependency_cache::prune(
                &resolved.path,
                config.max_bytes(),
                &dependency_cache::runtime_holds_runner,
            )
            .map_err(cache_failure)?;
            writeln!(
                out,
                "{} in {} namespace(s) at {}; removed {}.",
                dependency_cache::human_bytes(usage.total_bytes),
                usage.namespaces.len(),
                resolved.path.display(),
                if usage.pruned.is_empty() {
                    "nothing".to_owned()
                } else {
                    usage.pruned.join(", ")
                }
            )
            .map_err(failed)?;
        }
    }
    Ok(())
}

/// This host's record and policies, for the `show` commands.
fn host_and_policies(context: &Context) -> Result<(Option<Host>, Vec<ScalePolicy>), CliError> {
    let store = context.store()?;
    let host = super::host::local_host(&store)?;
    let policies = store.policies().map_err(|source| {
        CliError::new(
            Failure::LocalState,
            format!("cannot read this host's policies: {source}"),
        )
    })?;
    Ok((host, policies))
}

fn known_tool(raw: &str) -> Result<&'static str, CliError> {
    dependency_cache::tool(raw)
        .map(|tool| tool.id)
        .ok_or_else(|| {
            CliError::with_remedy(
                Failure::InvalidArgument,
                format!(
                    "`{raw}` is not a cache this version knows; one of: {}",
                    dependency_cache::tool_ids().join(", ")
                ),
                "runner-manager host cache show",
            )
        })
}

fn set_tool_state(tools: &mut BTreeMap<String, bool>, id: &str, state: CacheToolState) {
    match state {
        CacheToolState::On => {
            tools.insert(id.to_owned(), true);
        }
        CacheToolState::Off => {
            tools.insert(id.to_owned(), false);
        }
        CacheToolState::Default => {
            tools.remove(id);
        }
    }
}

fn state_word(state: CacheToolState, id: &str) -> String {
    match state {
        CacheToolState::On => "on".to_owned(),
        CacheToolState::Off => "off".to_owned(),
        CacheToolState::Default => {
            let default = dependency_cache::tool(id).is_some_and(|tool| tool.default_enabled);
            format!(
                "back to its default ({})",
                if default { "on" } else { "off" }
            )
        }
    }
}

/// `<namespace>/npm` or `<namespace>/_slots/<n>/tool-cache`, for a table that
/// describes every target at once.
fn described_value(sharing: Sharing, dir: &str, value: VarValue) -> String {
    let base = match sharing {
        Sharing::Namespace => format!("<namespace>/{dir}"),
        Sharing::Slot => format!("<namespace>/_slots/<n>/{dir}"),
    };
    match value {
        VarValue::Dir => base,
        VarValue::Sub(sub) => format!("{base}/{sub}"),
        VarValue::Literal(literal) => literal.to_owned(),
        VarValue::Template(template) => template.replace("{dir}", &base),
    }
}

fn write_host_show(
    context: &Context,
    host: Option<&Host>,
    policies: &[ScalePolicy],
    config: &CacheConfig,
    out: &mut dyn Write,
) -> io::Result<()> {
    let platform = RunnerPlatform::current();
    let resolved = root(context, host, config);
    writeln!(
        out,
        "Dependency caches: {}",
        if config.host_enabled() { "on" } else { "off" }
    )?;
    match &resolved {
        Ok(root) => writeln!(
            out,
            "  root                      {} ({})",
            root.path.display(),
            root.source.as_token()
        )?,
        Err(why) => writeln!(out, "  root                      none: {why}")?,
    }
    writeln!(
        out,
        "  settings                  {}",
        config_path(context).display()
    )?;
    writeln!(
        out,
        "  size cap                  {}",
        config
            .max_bytes()
            .map_or_else(|| "none".to_owned(), dependency_cache::human_bytes)
    )?;
    let usage = resolved
        .as_ref()
        .ok()
        .and_then(|root| CacheUsage::read(&root.path));
    match &usage {
        Some(usage) => {
            writeln!(
                out,
                "  in use                    {} in {} namespace(s), measured {}",
                dependency_cache::human_bytes(usage.total_bytes),
                usage.namespaces.len(),
                usage.measured_at.format("%Y-%m-%d %H:%M UTC")
            )?;
            let mut largest: Vec<_> = usage.namespaces.iter().collect();
            largest.sort_by(|a, b| b.bytes.cmp(&a.bytes));
            for namespace in largest.iter().take(10) {
                writeln!(
                    out,
                    "    {:<40} {:>10}{}",
                    namespace.name,
                    dependency_cache::human_bytes(namespace.bytes),
                    if namespace.in_use { "  in use" } else { "" }
                )?;
            }
        }
        None => writeln!(
            out,
            "  in use                    not measured yet; the service measures every {} minutes, \
             `host cache prune` measures now",
            dependency_cache::PRUNE_INTERVAL.as_secs() / 60
        )?,
    }
    writeln!(out)?;
    writeln!(
        out,
        "Caches on this platform (`host cache set-tool TOOL --state on|off|default`):"
    )?;
    for tool in TOOLS.iter().filter(|tool| tool.applies_to(platform)) {
        let (enabled, source) = match config.tools.get(tool.id) {
            Some(enabled) => (*enabled, "host"),
            None => (tool.default_enabled, "default"),
        };
        writeln!(
            out,
            "  {:<17} {:<4} ({source}){}",
            tool.id,
            if enabled { "on" } else { "off" },
            if tool.sharing == Sharing::Slot {
                ", one per concurrent runner"
            } else {
                ""
            }
        )?;
        for variable in tool.variables_on(platform) {
            writeln!(
                out,
                "      {}={}",
                variable.name,
                described_value(tool.sharing, tool.dir, variable.value)
            )?;
        }
        writeln!(out, "      {}", tool.note)?;
    }
    writeln!(out)?;
    writeln!(out, "Policies:")?;
    if policies.is_empty() {
        writeln!(out, "  none yet")?;
    }
    let mut seen = std::collections::BTreeSet::new();
    for policy in policies {
        if !seen.insert((
            dependency_cache::target_key(&policy.target),
            policy.execution_policy().is_native(),
        )) {
            continue;
        }
        let selection = dependency_cache::select(config, &policy.target, platform);
        writeln!(
            out,
            "  {:<40} {}",
            policy.target.slug(),
            policy_line(policy, &selection)
        )?;
    }
    writeln!(out)?;
    writeln!(
        out,
        "A variable set in runner.env (`host env set`) always wins over these. Isolated runners \
         get no caches: their providers mount no host directory."
    )?;
    if platform == RunnerPlatform::MacOs {
        writeln!(
            out,
            "setup-python on macOS ignores the tool cache and installs under \
             /Users/runner/hostedtoolcache; create that directory for the runner account once if \
             workflows use it."
        )?;
    }
    Ok(())
}

fn policy_line(policy: &ScalePolicy, selection: &Selection) -> String {
    if !policy.execution_policy().is_native() {
        return "no cache: isolated runners use the image's environment".to_owned();
    }
    match selection.disabled {
        Some(why) => format!("no cache: {why}"),
        None => format!("namespace {}", selection.namespace.label()),
    }
}

// ---------------------------------------------------------------------------
// repo cache / org cache
// ---------------------------------------------------------------------------

/// The parts of `repo cache` and `org cache` that differ only in the target.
enum TargetCommand<'a> {
    Show,
    SetEnabled(bool),
    SetNamespace(&'a CacheNamespaceArgs),
    SetTool(&'a CacheToolArgs),
}

fn parse_repository(raw: &str) -> Result<ScaleTarget, CliError> {
    ScaleTarget::repository(raw)
        .map_err(|source| CliError::new(Failure::InvalidArgument, source.to_string()))
}

fn parse_organization(raw: &str) -> Result<ScaleTarget, CliError> {
    ScaleTarget::organization(raw)
        .map_err(|source| CliError::new(Failure::InvalidArgument, source.to_string()))
}

/// # Errors
/// As [`dispatch_host`], and a malformed `OWNER/REPO`.
pub fn dispatch_repo(
    context: &Context,
    command: &RepoCacheCommand,
    out: &mut dyn Write,
) -> Result<(), CliError> {
    let (target, command) = match command {
        RepoCacheCommand::Show(args) => (parse_repository(&args.repository)?, TargetCommand::Show),
        RepoCacheCommand::SetEnabled(args) => (
            parse_repository(&args.repository)?,
            TargetCommand::SetEnabled(args.enabled.enabled),
        ),
        RepoCacheCommand::SetNamespace(args) => (
            parse_repository(&args.repository)?,
            TargetCommand::SetNamespace(&args.namespace),
        ),
        RepoCacheCommand::SetTool(args) => (
            parse_repository(&args.repository)?,
            TargetCommand::SetTool(&args.tool),
        ),
    };
    target_command(context, &target, &command, out)
}

/// # Errors
/// As [`dispatch_host`], and a malformed organization.
pub fn dispatch_org(
    context: &Context,
    command: &OrgCacheCommand,
    out: &mut dyn Write,
) -> Result<(), CliError> {
    let (target, command) = match command {
        OrgCacheCommand::Show(args) => {
            (parse_organization(&args.organization)?, TargetCommand::Show)
        }
        OrgCacheCommand::SetEnabled(args) => (
            parse_organization(&args.organization)?,
            TargetCommand::SetEnabled(args.enabled.enabled),
        ),
        OrgCacheCommand::SetNamespace(args) => (
            parse_organization(&args.organization)?,
            TargetCommand::SetNamespace(&args.namespace),
        ),
        OrgCacheCommand::SetTool(args) => (
            parse_organization(&args.organization)?,
            TargetCommand::SetTool(&args.tool),
        ),
    };
    target_command(context, &target, &command, out)
}

fn target_command(
    context: &Context,
    target: &ScaleTarget,
    command: &TargetCommand<'_>,
    out: &mut dyn Write,
) -> Result<(), CliError> {
    let failed = write_failed("the dependency caches");
    let mut config = load(context)?;
    let slug = target.slug();
    match command {
        TargetCommand::Show => {
            let (host, policies) = host_and_policies(context)?;
            write_target_show(context, host.as_ref(), &policies, &config, target, out)
                .map_err(failed)?;
            return Ok(());
        }
        TargetCommand::SetEnabled(enabled) => {
            config.target_mut(target).enabled = Some(*enabled);
            save(context, &config)?;
            writeln!(
                out,
                "Dependency caches are {} for {slug}'s native runners started from now on.",
                if *enabled { "on" } else { "off" }
            )
            .map_err(failed)?;
        }
        TargetCommand::SetNamespace(args) => {
            if let Some(name) = &args.shared {
                dependency_cache::validate_namespace_name(name).map_err(cache_failure)?;
                config.target_mut(target).namespace = Some(name.clone());
                save(context, &config)?;
                writeln!(
                    out,
                    "{slug} now uses the shared cache `{name}`. Every policy naming it reads and \
                     writes the same files, so share only between repositories you trust \
                     equally."
                )
                .map_err(failed)?;
            } else {
                config.target_mut(target).namespace = None;
                save(context, &config)?;
                writeln!(out, "{slug} uses its own cache again.").map_err(failed)?;
            }
        }
        TargetCommand::SetTool(args) => {
            let id = known_tool(&args.tool)?;
            set_tool_state(&mut config.target_mut(target).tools, id, args.state);
            save(context, &config)?;
            let word = match args.state {
                CacheToolState::Default => "back to the host's setting".to_owned(),
                state => state_word(state, id),
            };
            writeln!(out, "{id} is {word} for {slug}.").map_err(failed)?;
        }
    }
    warn_without_policy(context, target, out)
}

/// A setting for a target this host has no policy for is kept, and said so:
/// it is most likely a typo.
fn warn_without_policy(
    context: &Context,
    target: &ScaleTarget,
    out: &mut dyn Write,
) -> Result<(), CliError> {
    let store = context.store()?;
    let known = store
        .policies()
        .map(|policies| {
            policies.iter().any(|policy| {
                dependency_cache::target_key(&policy.target) == dependency_cache::target_key(target)
            })
        })
        .unwrap_or(true);
    if !known {
        writeln!(
            out,
            "Note: this host has no policy for {}; the setting applies once one is added.",
            target.slug()
        )
        .map_err(write_failed("the dependency caches"))?;
    }
    Ok(())
}

fn write_target_show(
    context: &Context,
    host: Option<&Host>,
    policies: &[ScalePolicy],
    config: &CacheConfig,
    target: &ScaleTarget,
    out: &mut dyn Write,
) -> io::Result<()> {
    let platform = RunnerPlatform::current();
    let mut selection = dependency_cache::select(config, target, platform);
    // What the daemon defers to `runner.env`. The service's own environment
    // is not this process's, so only the file can be asked here.
    let runner_env = runner_manager_platform::runner_env::RunnerEnv::load(
        &runner_manager_platform::runner_env::path_in(context.paths().config_dir()),
    )
    .unwrap_or_default();
    selection.defer_to_operator(platform, |name| {
        runner_env
            .entries()
            .any(|(set, _)| set.eq_ignore_ascii_case(name))
    });
    writeln!(out, "Dependency caches for {}", target.slug())?;
    match selection.disabled {
        Some(why) => writeln!(out, "  state                     off: {why}")?,
        None => writeln!(out, "  state                     on")?,
    }
    let resolved = root(context, host, config);
    match &resolved {
        Ok(root) => writeln!(
            out,
            "  namespace                 {}",
            selection.namespace.dir(&root.path).display()
        )?,
        Err(why) => writeln!(
            out,
            "  namespace                 {} (no root: {why})",
            selection.namespace.label()
        )?,
    }
    let usage = resolved
        .as_ref()
        .ok()
        .and_then(|root| CacheUsage::read(&root.path))
        .and_then(|usage| {
            let label = selection.namespace.label();
            usage.namespaces.into_iter().find(|ns| ns.name == label)
        });
    if let Some(usage) = usage {
        writeln!(
            out,
            "  size                      {}{}",
            dependency_cache::human_bytes(usage.bytes),
            if usage.in_use { ", in use" } else { "" }
        )?;
    }
    let isolated = policies
        .iter()
        .filter(|policy| {
            dependency_cache::target_key(&policy.target) == dependency_cache::target_key(target)
        })
        .any(|policy| !policy.execution_policy().is_native());
    if isolated {
        writeln!(
            out,
            "  isolated profiles         get no caches; their providers mount no host directory"
        )?;
    }
    writeln!(out, "  caches:")?;
    for state in &selection.tools {
        let source = state.source.as_token();
        let names: Vec<&str> = state
            .tool
            .variables_on(platform)
            .map(|variable| variable.name)
            .collect();
        writeln!(
            out,
            "    {:<17} {:<4} ({source})  {}",
            state.tool.id,
            if state.enabled { "on" } else { "off" },
            names.join(", ")
        )?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_table_describes_shared_and_per_runner_directories_distinctly() {
        assert_eq!(
            described_value(Sharing::Namespace, "nuget", VarValue::Sub("packages")),
            "<namespace>/nuget/packages"
        );
        assert_eq!(
            described_value(Sharing::Slot, "tool-cache", VarValue::Dir),
            "<namespace>/_slots/<n>/tool-cache"
        );
        assert_eq!(
            described_value(
                Sharing::Slot,
                "maven",
                VarValue::Template("-Dmaven.repo.local={dir}")
            ),
            "-Dmaven.repo.local=<namespace>/_slots/<n>/maven"
        );
    }

    #[test]
    fn the_status_line_names_usage_the_cap_and_the_root() {
        let mut snapshot = CacheSnapshot {
            enabled: true,
            root: Some("/rman/_cache".into()),
            root_source: Some("runner-root"),
            problem: None,
            max_bytes: Some(20 * 1024 * 1024 * 1024),
            used_bytes: None,
            namespaces: None,
            measured_at: None,
        };
        assert_eq!(
            snapshot.line(),
            "20.0 GiB cap at /rman/_cache, not measured yet"
        );
        snapshot.used_bytes = Some(3 * 1024 * 1024 * 1024 / 2);
        assert_eq!(snapshot.line(), "1.5 GiB of 20.0 GiB at /rman/_cache");
        snapshot.enabled = false;
        assert!(snapshot.line().starts_with("off"));
        snapshot.problem = Some("bad".into());
        assert_eq!(snapshot.line(), "unusable: bad");
    }
}