brokk-mj-controller 2.12.0

Daemon-side controller, session manager, and web server for Mjolnir
Documentation
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
//! The shared mbx build cache for Rust container sessions.
//!
//! mbx wraps Cargo: a binary named `cargo` that is really `mbx` intercepts the
//! build, looks every compiler action up in a content-addressed store, and
//! restores cached outputs instead of recompiling. Its store is an ordinary
//! directory on the container host, which every mj container on that host
//! mounts read-write at the same absolute path. Nothing is synchronized
//! between hosts and mj never runs mbx garbage collection.
//!
//! Every failure here means the session runs without the cache. Nothing in
//! this module ever fails provisioning.

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

use anyhow::{Context, Result, bail, ensure};
use sha2::{Digest, Sha256};

use super::cache_host::CacheHost;
use crate::targets::{self, CommandExecutor, CommandOutput, CommandSpec};
use mj_core::config::{BuildCacheConfig, TargetBuildCache};
use mj_core::state::{BuildCacheLimit, BuildCachePreview, SessionBuildCache};

/// The mbx release containers run. A native mbx older than this must not share
/// the same store, so a host that has one runs its sessions without the cache.
pub(super) const MBX_VERSION: &str = "1.12.0";

const MBX_X86_64_SHA256: &str = "b0d90013e5e4e55419b75db897a6b9eed0f0e3b7bc49edf355e0e6490fda6de2";
const MBX_AARCH64_SHA256: &str = "738b97bf260137aed70cd3cf849925d1f1bec5f1ba446f5e61ed851ae649bc7b";

/// Overrides the download with a local mbx binary for the current machine's
/// architecture. Used for development against an unreleased mbx.
const MBX_BINARY_ENV: &str = "MJ_MBX_BINARY";

const DEFAULT_CACHE_RELATIVE: &str = ".cache/mbx";
const HOST_CONFIG_RELATIVE: &str = ".config/mbx/config.toml";
/// The cap on the computed default budget: 100 GB, in SI bytes.
const DEFAULT_MAX_BYTES: u64 = 100_000_000_000;
const RESOLUTION_LIFETIME: Duration = Duration::from_secs(600);
const LABEL: &str = "hel-mbx";

/// What a container target's host offers as a build cache.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ResolvedBuildCache {
    /// Cache directory on the host, mounted at the same path in the container.
    pub directory: PathBuf,
    /// `MBX_GC_MAX_SIZE` for the container, or `None` when the host's own mbx
    /// configuration file already carries the budget.
    pub max_size: Option<String>,
    /// A `[target] root` the host configuration relocates outside the cache
    /// directory, which the container needs mounted at the same path too.
    pub target_root: Option<PathBuf>,
    /// The host's `~/.config/mbx/config.toml`, copied into the container so
    /// its mbx uses the host's own limits.
    pub config_file: Option<String>,
}

/// Cached host answers, keyed by host and per-target settings. Resolution runs
/// several commands on the host, and a burst of new sessions must not repeat
/// them for each one.
type Resolutions = std::collections::BTreeMap<String, (Instant, Option<ResolvedBuildCache>)>;

static RESOLUTIONS: std::sync::LazyLock<std::sync::Mutex<Resolutions>> =
    std::sync::LazyLock::new(|| std::sync::Mutex::new(Resolutions::new()));

/// Resolve the build cache for one container target, or `None` when this
/// target runs without one. The answer is cached for ten minutes.
pub(super) fn resolve(
    target: &targets::TargetTemplate,
    global: &BuildCacheConfig,
    executor: &impl CommandExecutor,
) -> Option<ResolvedBuildCache> {
    if !global.enabled {
        return None;
    }
    let (host, settings) = supported_host(target)?;
    let key = format!("{}|{settings:?}", host.key());
    if let Some((recorded, resolution)) = RESOLUTIONS.lock().expect("mbx resolutions").get(&key)
        && recorded.elapsed() < RESOLUTION_LIFETIME
    {
        return resolution.clone();
    }
    let resolution = match resolve_host(&host, &settings, executor) {
        Ok(resolution) => resolution,
        Err(error) => {
            tracing::warn!(host = key, "build cache unavailable: {error:#}");
            None
        }
    };
    RESOLUTIONS
        .lock()
        .expect("mbx resolutions")
        .insert(key, (Instant::now(), resolution.clone()));
    resolution
}

/// The targets that can share a host build cache. Apple `container` runs each
/// container in its own virtual machine, where file locks across the shared
/// store are unverified, and bare and EC2 targets are out of scope.
fn supported_host(target: &targets::TargetTemplate) -> Option<(CacheHost, TargetBuildCache)> {
    let settings = match target {
        targets::TargetTemplate::LocalPodman(container)
        | targets::TargetTemplate::LocalDocker(container)
        | targets::TargetTemplate::SshPodman { container, .. }
        | targets::TargetTemplate::SshDocker { container, .. } => {
            container.build_cache.clone().unwrap_or_default()
        }
        targets::TargetTemplate::AppleContainer(_)
        | targets::TargetTemplate::LocalBare
        | targets::TargetTemplate::AwsEc2(_)
        | targets::TargetTemplate::SshBare { .. } => return None,
    };
    Some((CacheHost::for_target(target)?, settings))
}

fn resolve_host(
    host: &CacheHost,
    settings: &TargetBuildCache,
    executor: &impl CommandExecutor,
) -> Result<Option<ResolvedBuildCache>> {
    let inspection = inspect_host(host, settings, executor)?;
    match inspection.cache {
        Some(cache) => {
            create_directory(host, &cache.directory, executor)?;
            Ok(Some(cache))
        }
        None => {
            if let Some(reason) = &inspection.preview.off_reason {
                tracing::warn!(
                    directory = inspection
                        .preview
                        .directory
                        .as_ref()
                        .map(|d| d.display().to_string()),
                    "sessions on this target run without the build cache: {reason}"
                );
            }
            Ok(None)
        }
    }
}

/// What the settings screen shows for one machine's blank build cache fields:
/// the same host inspection a session runs, without creating the directory.
/// `None` when the machine has no standing host to share a cache on.
pub fn preview_build_cache(
    machine: &mj_core::config::Machine,
    global: &BuildCacheConfig,
    executor: &impl CommandExecutor,
) -> Result<Option<BuildCachePreview>> {
    let Some(host) = CacheHost::for_machine(machine) else {
        return Ok(None);
    };
    let settings = machine.build_cache().cloned().unwrap_or_default();
    if !global.enabled {
        return Ok(Some(BuildCachePreview {
            native_mbx: None,
            directory: None,
            max_size: None,
            off_reason: Some("the build cache is turned off for every machine".into()),
        }));
    }
    inspect_host(&host, &settings, executor).map(|inspection| Some(inspection.preview))
}

/// Everything the host says about a target's build cache, read without
/// changing the host.
struct Inspection {
    preview: BuildCachePreview,
    /// The cache a session would mount, or `None` when it runs without one.
    cache: Option<ResolvedBuildCache>,
}

fn inspect_host(
    host: &CacheHost,
    settings: &TargetBuildCache,
    executor: &impl CommandExecutor,
) -> Result<Inspection> {
    let native = native_version(host, executor);
    let native_version = native.as_ref().map(|native| native.version.clone());
    let off = |preview: BuildCachePreview| Inspection {
        preview,
        cache: None,
    };
    if let Some(version) = &native_version
        && !version_at_least(version, MBX_VERSION)
    {
        return Ok(off(BuildCachePreview {
            native_mbx: native_version.clone(),
            directory: None,
            max_size: None,
            off_reason: Some(format!(
                "the host's mbx {version} is older than the {MBX_VERSION} Mjolnir installs, \
                 so they cannot share a store"
            )),
        }));
    }
    let directory = match &settings.directory {
        Some(directory) => directory.clone(),
        None => match &native {
            Some(native) => native_cache_directory(host, native, executor)?,
            None => home(host, executor)?.join(DEFAULT_CACHE_RELATIVE),
        },
    };
    ensure!(
        directory.is_absolute(),
        "build cache directory {} is not absolute",
        directory.display()
    );

    let config_file = host_config_file(host, executor)?;
    let target_root = config_file
        .as_deref()
        .and_then(|text| relocated_target_root(text, &directory));

    let max_size = match (&settings.max_size, &config_file) {
        (Some(max_size), _) => Some(max_size.clone()),
        // The host's own file carries its budgets; a second one would fight it.
        (None, Some(_)) => None,
        (None, None) => Some(default_max_size(host, &directory, executor)?),
    };
    let limit = match (&max_size, &config_file) {
        (Some(max_size), _) => BuildCacheLimit::Size(max_size.clone()),
        (None, Some(text)) => BuildCacheLimit::HostConfiguration(configured_max_size(text)),
        (None, None) => unreachable!("a missing budget is derived above"),
    };
    let preview = |off_reason: Option<String>| BuildCachePreview {
        native_mbx: native_version.clone(),
        directory: Some(directory.clone()),
        max_size: Some(limit.clone()),
        off_reason,
    };

    // The directory may not exist yet; its filesystem is its nearest
    // existing ancestor's.
    let volume = nearest_existing_ancestor(host, &directory, executor)?;
    let enabled = match settings.enabled {
        Some(enabled) => enabled,
        None => reflinks_supported(host, &volume, executor)?,
    };
    if !enabled {
        let reason = if settings.enabled == Some(false) {
            "turned off for this target".to_owned()
        } else {
            format!(
                "the filesystem under {} does not support reflinks, so restoring cached \
                 outputs would copy every byte",
                directory.display()
            )
        };
        return Ok(off(preview(Some(reason))));
    }
    if let Some(reason) = unusable_filesystem(host, &volume, executor)? {
        return Ok(off(preview(Some(format!(
            "{} is on a {reason}, where mbx's file locks are unreliable",
            directory.display()
        )))));
    }

    Ok(Inspection {
        preview: preview(None),
        cache: Some(ResolvedBuildCache {
            directory,
            max_size,
            target_root,
            config_file,
        }),
    })
}

/// The `gc.max_size` a host configuration sets, for display only.
fn configured_max_size(config_file: &str) -> Option<String> {
    let document: toml::Value = toml::from_str(config_file).ok()?;
    document
        .get("gc")?
        .get("max_size")?
        .as_str()
        .map(str::to_owned)
}

/// `true` when `found` is at least `required`, comparing release versions.
fn version_at_least(found: &str, required: &str) -> bool {
    let parse = |text: &str| semver::Version::parse(text.trim()).ok();
    match (parse(found), parse(required)) {
        (Some(found), Some(required)) => found >= required,
        // An unparsable version is not evidence of a new enough mbx.
        _ => false,
    }
}

/// The host's own mbx: the program that runs it and its version.
#[derive(Debug, Clone, PartialEq, Eq)]
struct NativeMbx {
    program: String,
    version: String,
}

/// An SSH command runs in a non-login shell whose `PATH` lacks the user's
/// Cargo bin directory, so a `cargo install`ed mbx is looked up there too.
const NATIVE_VERSION_SCRIPT: &str = r#"for m in mbx "$HOME/.cargo/bin/mbx"; do
    if v=$("$m" --version 2>/dev/null); then
        printf '%s
%s' "$m" "$v"
        exit 0
    fi
done
exit 1"#;

/// The host's own mbx, or `None` when neither `PATH` nor `~/.cargo/bin`
/// has one.
fn native_version(host: &CacheHost, executor: &impl CommandExecutor) -> Option<NativeMbx> {
    let command = host.shell_command(
        NATIVE_VERSION_SCRIPT,
        LABEL,
        [],
        "read the container host mbx version",
    );
    let output = executor.execute(&command).ok()?;
    if output.status != 0 {
        return None;
    }
    let text = String::from_utf8_lossy(&output.stdout);
    let (program, version) = text.trim().split_once('\n')?;
    Some(NativeMbx {
        program: program.to_owned(),
        version: version.split_whitespace().next_back()?.to_owned(),
    })
}

/// The host's own cache directory. `mbx cache dir` prints the store, which is
/// the `actions` directory inside the cache directory.
fn native_cache_directory(
    host: &CacheHost,
    native: &NativeMbx,
    executor: &impl CommandExecutor,
) -> Result<PathBuf> {
    let command = host.command(
        vec![
            native.program.clone(),
            "cache".to_owned(),
            "dir".to_owned(),
            "--json".to_owned(),
        ],
        "read the container host mbx cache directory",
    );
    let output = checked(executor.execute(&command)?, &command)?;
    let report: serde_json::Value =
        serde_json::from_slice(&output.stdout).context("parse the mbx cache directory report")?;
    let store = report
        .get("store")
        .and_then(serde_json::Value::as_str)
        .context("the mbx cache directory report has no store path")?;
    Path::new(store)
        .parent()
        .map(Path::to_path_buf)
        .with_context(|| format!("mbx store path {store:?} has no parent"))
}

fn home(host: &CacheHost, executor: &impl CommandExecutor) -> Result<PathBuf> {
    let command = host.shell_command(
        r#"printf '%s' "$HOME""#,
        LABEL,
        [],
        "locate the container host home directory",
    );
    let output = checked(executor.execute(&command)?, &command)?;
    let home = PathBuf::from(String::from_utf8(output.stdout).context("decode host HOME")?);
    ensure!(home.is_absolute(), "container host HOME is not absolute");
    Ok(home)
}

const READ_CONFIG_SCRIPT: &str = r#"[ -f "$1" ] || exit 3
cat -- "$1""#;

/// The host's `~/.config/mbx/config.toml`, which containers receive verbatim
/// so their mbx uses the host's own limits. mbx has no command that prints its
/// effective configuration, so the file itself is the only accurate source.
fn host_config_file(host: &CacheHost, executor: &impl CommandExecutor) -> Result<Option<String>> {
    let path = home(host, executor)?.join(HOST_CONFIG_RELATIVE);
    let command = host.shell_command(
        READ_CONFIG_SCRIPT,
        LABEL,
        [path.to_string_lossy().into_owned()],
        "read the container host mbx configuration",
    );
    let output = executor.execute(&command)?;
    if output.status == 3 {
        return Ok(None);
    }
    let output = checked(output, &command)?;
    Ok(Some(
        String::from_utf8(output.stdout).context("decode the host mbx configuration")?,
    ))
}

/// The `[target] root` a host configuration sets, when it lies outside the
/// cache directory and therefore needs its own mount.
fn relocated_target_root(config_file: &str, directory: &Path) -> Option<PathBuf> {
    let document: toml::Value = toml::from_str(config_file)
        .map_err(|error| tracing::warn!("the host mbx configuration is unreadable: {error}"))
        .ok()?;
    let root = document.get("target")?.get("root")?.as_str()?;
    let root = directory.join(root);
    (!root.starts_with(directory)).then_some(root)
}

const NEAREST_ANCESTOR_SCRIPT: &str = r#"d=$1
while [ ! -d "$d" ]; do
    parent=$(dirname -- "$d")
    if [ "$parent" = "$d" ]; then
        break
    fi
    d=$parent
done
printf '%s' "$d""#;

/// The deepest existing directory at or above `directory`. The cache directory
/// may not exist yet, and both `df` and the reflink probe need a real one.
fn nearest_existing_ancestor(
    host: &CacheHost,
    directory: &Path,
    executor: &impl CommandExecutor,
) -> Result<PathBuf> {
    let command = host.shell_command(
        NEAREST_ANCESTOR_SCRIPT,
        LABEL,
        [directory.to_string_lossy().into_owned()],
        "locate the build cache volume",
    );
    let output = checked(executor.execute(&command)?, &command)?;
    let path = PathBuf::from(String::from_utf8(output.stdout).context("decode cache ancestor")?);
    ensure!(
        path.is_absolute(),
        "build cache volume {} is not absolute",
        path.display()
    );
    Ok(path)
}

/// The budget mj gives a host that has no mbx configuration of its own: the
/// smaller of 100 GB and a quarter of the free space on the cache volume.
fn default_max_size(
    host: &CacheHost,
    directory: &Path,
    executor: &impl CommandExecutor,
) -> Result<String> {
    let volume = nearest_existing_ancestor(host, directory, executor)?;
    let command = host.command(
        vec![
            "df".to_owned(),
            "-B1".to_owned(),
            "-P".to_owned(),
            "--".to_owned(),
            volume.to_string_lossy().into_owned(),
        ],
        "measure the build cache volume",
    );
    let output = checked(executor.execute(&command)?, &command)?;
    let available = available_bytes(&String::from_utf8_lossy(&output.stdout))
        .context("read the free space on the build cache volume")?;
    Ok(format!("{}B", DEFAULT_MAX_BYTES.min(available / 4)))
}

/// The available column of `df -B1 -P` output, which is the fourth field of
/// the row after the header. A long device name wraps in some `df`
/// implementations, so the fields are counted from the end of the last row.
fn available_bytes(report: &str) -> Option<u64> {
    let row = report
        .lines()
        .filter(|line| !line.trim().is_empty())
        .nth(1)?;
    let fields = row.split_whitespace().collect::<Vec<_>>();
    // ... size used available capacity mounted-on
    let available = fields.get(fields.len().checked_sub(3)?)?;
    available.parse().ok()
}

const REFLINK_SCRIPT: &str = r#"dir=$1
d=$(mktemp -d "$dir/.mj-reflink.XXXXXX") || exit 1
printf x > "$d/a" && cp --reflink=always "$d/a" "$d/b"
status=$?
rm -rf -- "$d"
exit $status"#;

/// Whether the cache volume can clone files instead of copying their bytes.
/// Reflinks are what make restoring a cached output nearly free, so a host
/// without them defaults to running without the cache.
fn reflinks_supported(
    host: &CacheHost,
    volume: &Path,
    executor: &impl CommandExecutor,
) -> Result<bool> {
    let command = host.shell_command(
        REFLINK_SCRIPT,
        LABEL,
        [volume.to_string_lossy().into_owned()],
        "probe the build cache volume for reflinks",
    );
    Ok(executor.execute(&command)?.status == 0)
}

fn create_directory(
    host: &CacheHost,
    directory: &Path,
    executor: &impl CommandExecutor,
) -> Result<()> {
    let command = host.command(
        vec![
            "mkdir".to_owned(),
            "-p".to_owned(),
            "--".to_owned(),
            directory.to_string_lossy().into_owned(),
        ],
        "create the build cache directory",
    );
    checked(executor.execute(&command)?, &command).map(|_| ())
}

/// A filesystem mbx cannot use. It refuses NFS outright, and file locks over
/// FUSE, virtiofs, and 9p are unreliable, which a shared store depends on.
fn unusable_filesystem(
    host: &CacheHost,
    directory: &Path,
    executor: &impl CommandExecutor,
) -> Result<Option<&'static str>> {
    let filesystems =
        targets::probe_filesystem_types(host.ssh(), &[directory.to_path_buf()], executor)?;
    let filesystem = filesystems
        .first()
        .context("the filesystem probe named no filesystem")?;
    // `overlay_unsupported_filesystem` already groups virtiofs and 9p with the
    // network filesystems. The other reasons it gives are about stacking an
    // overlay, which a plain read-write bind mount does not do.
    Ok(targets::overlay_unsupported_filesystem(filesystem)
        .filter(|reason| matches!(*reason, "network filesystem" | "FUSE filesystem")))
}

fn checked(output: CommandOutput, command: &CommandSpec) -> Result<CommandOutput> {
    if output.status == 0 {
        return Ok(output);
    }
    bail!(
        "{} failed with status {}: {}",
        command.purpose,
        output.status,
        String::from_utf8_lossy(&output.stderr).trim()
    )
}

// -- the pinned mbx binary ------------------------------------------------

/// The mbx binary to install in a container of this architecture, downloading
/// and verifying the pinned release on first use.
pub(super) fn binary_for(
    locator: &targets::TargetLocator,
    executor: &impl CommandExecutor,
) -> Result<PathBuf> {
    let triple = super::worker_binary::target_architecture(locator, executor)?;
    if let Some(path) = std::env::var_os(MBX_BINARY_ENV) {
        let path = PathBuf::from(path);
        ensure!(
            path.is_file(),
            "{MBX_BINARY_ENV} does not name a file: {}",
            path.display()
        );
        if triple == host_architecture() {
            return Ok(path);
        }
        tracing::warn!(
            triple,
            "{MBX_BINARY_ENV} is for this machine's architecture; downloading the pinned mbx \
             for the target instead"
        );
    }
    download(triple)
}

/// This machine's architecture in the same spelling `target_architecture`
/// reports, so a local override is not handed to a foreign container.
fn host_architecture() -> &'static str {
    if cfg!(target_arch = "aarch64") {
        "aarch64"
    } else {
        "x86_64"
    }
}

fn release_url(triple: &str) -> String {
    format!(
        "https://github.com/jdx/mr-boxington/releases/download/v{MBX_VERSION}/mbx-{triple}-unknown-linux-musl.tar.gz"
    )
}

fn expected_digest(triple: &str) -> Result<&'static str> {
    match triple {
        "x86_64" => Ok(MBX_X86_64_SHA256),
        "aarch64" => Ok(MBX_AARCH64_SHA256),
        _ => bail!("no pinned mbx release for {triple}"),
    }
}

/// Download the pinned release once into the data directory. The archive is
/// verified against the release checksum before anything is extracted.
fn download(triple: &str) -> Result<PathBuf> {
    let expected = expected_digest(triple)?;
    let directory = mj_core::config::data_dir()
        .join("mbx")
        .join(MBX_VERSION)
        .join(triple);
    let destination = directory.join("mbx");
    if destination.is_file() {
        return Ok(destination);
    }
    std::fs::create_dir_all(&directory)
        .with_context(|| format!("create the mbx cache {}", directory.display()))?;
    let url = release_url(triple);
    let archive = reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(120))
        .build()?
        .get(&url)
        .send()
        .with_context(|| format!("download {url}"))?
        .error_for_status()
        .with_context(|| format!("download {url}"))?
        .bytes()?;
    let actual = mj_core::hex::lower_hex(Sha256::digest(&archive));
    ensure!(
        actual.eq_ignore_ascii_case(expected),
        "downloaded mbx checksum mismatch: expected {expected}, got {actual}"
    );
    let binary = extract_binary(&archive)?;
    let mut temporary = tempfile::NamedTempFile::new_in(&directory)?;
    std::io::Write::write_all(&mut temporary, &binary)?;
    temporary.as_file_mut().sync_all()?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(temporary.path(), std::fs::Permissions::from_mode(0o700))?;
    }
    match temporary.persist_noclobber(&destination) {
        Ok(_) => Ok(destination),
        Err(error) if destination.is_file() => {
            drop(error);
            Ok(destination)
        }
        Err(error) => Err(error.error)
            .with_context(|| format!("publish the mbx binary {}", destination.display())),
    }
}

/// The single `mbx` file from the release archive, which also carries its
/// licence texts.
fn extract_binary(archive: &[u8]) -> Result<Vec<u8>> {
    let mut reader = tar::Archive::new(flate2::read::GzDecoder::new(archive));
    for entry in reader.entries().context("read the mbx release archive")? {
        let mut entry = entry.context("read the mbx release archive")?;
        if entry.path().context("read an mbx archive path")?.as_ref() != Path::new("mbx") {
            continue;
        }
        let mut bytes = Vec::new();
        entry
            .read_to_end(&mut bytes)
            .context("read the mbx binary from its release archive")?;
        return Ok(bytes);
    }
    bail!("the mbx release archive contains no mbx binary")
}

// -- per-session decision -------------------------------------------------

/// Whether the primary repository is a Cargo workspace, read from the host
/// mirror the clone cache prepared. A repository whose manifest is not at its
/// root, and a session whose clone cache was not prepared, run without mbx.
pub(super) fn primary_repository_is_rust(
    host: &CacheHost,
    mirror: &Path,
    executor: &impl CommandExecutor,
) -> bool {
    let command = host.command(
        vec![
            "git".to_owned(),
            "--git-dir".to_owned(),
            mirror.to_string_lossy().into_owned(),
            "cat-file".to_owned(),
            "-e".to_owned(),
            "HEAD:Cargo.toml".to_owned(),
        ],
        "detect a Cargo workspace in the session repository",
    );
    matches!(executor.execute(&command), Ok(output) if output.status == 0)
}

/// Decide the build cache for one session and attach its mounts, returning the
/// value to record on the session. A session that already carries a decision
/// (resume, move, or a sub-agent child) reuses it without resolving again.
pub(super) fn prepare(
    target: &targets::TargetTemplate,
    global: &BuildCacheConfig,
    session: &mj_core::state::SessionRecord,
    bundle: Option<&targets::ProjectBundleSpec>,
    clone_cache: Option<&super::git_cache::PreparedCloneCache>,
    mounts: &mut Vec<targets::AdditionalMount>,
    executor: &impl CommandExecutor,
) -> Option<SessionBuildCache> {
    // A recorded cache is a directory on one particular host, so it only
    // survives a resume that stays on that host.
    let host_key = supported_host(target).map(|(host, _)| host.key());
    if let Some(recorded) = &session.build_cache {
        if host_key.as_deref() == Some(recorded.host.as_str()) {
            return attach_mounts(recorded, mounts).then(|| recorded.clone());
        }
        tracing::info!(
            session_id = session.id,
            recorded_host = recorded.host,
            host = host_key.as_deref().unwrap_or("unsupported target"),
            "the session moved to another container host, so its build cache is resolved again"
        );
    }
    // A session at the legacy shared `/workspace` would collide with every
    // other legacy session in mbx's path-keyed records.
    session.container_workspace.as_ref()?;
    let resolved = resolve(target, global, executor)?;
    let host = supported_host(target)?.0;
    let mirror = clone_cache?.mirror_for(&bundle?.primary)?;
    if !primary_repository_is_rust(&host, mirror, executor) {
        return None;
    }
    let build_cache = SessionBuildCache {
        host: host.key(),
        directory: resolved.directory,
        max_size: resolved.max_size,
        target_root: resolved.target_root,
    };
    attach_mounts(&build_cache, mounts).then_some(build_cache)
}

/// Mount the cache, and a relocated target root, read-write at the same
/// absolute paths the host uses. An attached directory that already covers one
/// of those paths wins, and the session runs without the cache.
fn attach_mounts(
    build_cache: &SessionBuildCache,
    mounts: &mut Vec<targets::AdditionalMount>,
) -> bool {
    let wanted = std::iter::once(&build_cache.directory)
        .chain(build_cache.target_root.iter())
        .collect::<Vec<_>>();
    for directory in &wanted {
        if mounts.iter().any(|mount| {
            mount.destination.starts_with(directory) || directory.starts_with(&mount.destination)
        }) {
            tracing::warn!(
                directory = %directory.display(),
                "an attached directory overlaps the build cache, so this session runs without it"
            );
            return false;
        }
    }
    for directory in wanted {
        mounts.push(targets::AdditionalMount {
            source: directory.clone(),
            destination: directory.clone(),
            access: targets::MountAccess::Rw,
        });
    }
    true
}

/// `attach_mounts` for the provisioning tests, which check the container
/// arguments the mounts produce.
#[cfg(test)]
pub(super) fn attach_mounts_for_tests(
    build_cache: &SessionBuildCache,
    mounts: &mut Vec<targets::AdditionalMount>,
) -> bool {
    attach_mounts(build_cache, mounts)
}

/// The host's mbx configuration file for this target, read through the cached
/// resolution so the worker install does not repeat the host commands.
pub(super) fn host_configuration(
    target: &targets::TargetTemplate,
    global: &BuildCacheConfig,
    executor: &impl CommandExecutor,
) -> Option<String> {
    resolve(target, global, executor)?.config_file
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::targets::{ContainerTemplate, SshTarget, TargetTemplate};
    use mj_core::config::ImagePullPolicy;
    use std::sync::Mutex;

    /// The resolution cache is process-wide, so tests that exercise it run one
    /// at a time and start from an empty cache.
    static ISOLATED: Mutex<()> = Mutex::new(());

    fn isolated() -> std::sync::MutexGuard<'static, ()> {
        let guard = ISOLATED.lock().unwrap_or_else(|error| error.into_inner());
        RESOLUTIONS.lock().expect("mbx resolutions").clear();
        guard
    }

    /// Answers canned commands by a substring of their joined argument list.
    #[derive(Default)]
    struct ProbeExecutor {
        answers: Vec<(&'static str, i32, String)>,
        seen: Mutex<Vec<String>>,
    }

    impl ProbeExecutor {
        fn new(answers: &[(&'static str, i32, &str)]) -> Self {
            Self {
                answers: answers
                    .iter()
                    .map(|(needle, status, stdout)| (*needle, *status, (*stdout).to_owned()))
                    .collect(),
                seen: Mutex::new(Vec::new()),
            }
        }

        fn ran(&self) -> Vec<String> {
            self.seen.lock().unwrap().clone()
        }
    }

    impl CommandExecutor for ProbeExecutor {
        fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
            let line = format!("{} {}", command.program, command.args.join(" "));
            self.seen.lock().unwrap().push(line.clone());
            for (needle, status, stdout) in &self.answers {
                if line.contains(needle) {
                    return Ok(CommandOutput {
                        status: *status,
                        stdout: stdout.clone().into_bytes(),
                        stderr: Vec::new(),
                    });
                }
            }
            Ok(CommandOutput {
                status: 127,
                stdout: Vec::new(),
                stderr: format!("no canned answer for {line}").into_bytes(),
            })
        }
    }

    fn container(build_cache: Option<TargetBuildCache>) -> ContainerTemplate {
        ContainerTemplate {
            image: "example/image:latest".into(),
            pull_policy: ImagePullPolicy::Missing,
            extra_run_args: Vec::new(),
            workspace_storage: Default::default(),
            build_cache,
        }
    }

    fn podman(build_cache: Option<TargetBuildCache>) -> TargetTemplate {
        TargetTemplate::LocalPodman(container(build_cache))
    }

    fn docker(build_cache: Option<TargetBuildCache>) -> TargetTemplate {
        TargetTemplate::LocalDocker(container(build_cache))
    }

    /// The settings draft's view of this machine with blank build cache
    /// fields.
    fn configured_local_machine() -> mj_core::config::Machine {
        serde_json::from_value(serde_json::json!({"kind": "local"})).unwrap()
    }

    /// The canned answers a host with no native mbx and a reflink-capable
    /// home directory gives.
    fn plain_host() -> Vec<(&'static str, i32, &'static str)> {
        vec![
            ("$m\" --version", 1, ""),
            (r#"printf '%s' "$HOME""#, 0, "/home/dev"),
            ("[ -f \"$1\" ]", 3, ""),
            ("while [ ! -d", 0, "/home/dev"),
            (
                "df -B1 -P",
                0,
                "Filesystem 1B-blocks Used Available Capacity Mounted\n/dev/sda1 1000000000000 0 800000000000 20% /home\n",
            ),
            ("mj-reflink", 0, ""),
            ("mkdir -p", 0, ""),
            ("stat -f -c %T", 0, "xfs"),
        ]
    }

    #[test]
    fn a_native_mbx_supplies_the_cache_directory_and_its_own_limits() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&[
            ("$m\" --version", 0, "mbx\nmbx 1.12.0"),
            (
                "mbx cache dir --json",
                0,
                r#"{"version":1,"store":"/mnt/fast/mbx-cache/actions"}"#,
            ),
            (r#"printf '%s' "$HOME""#, 0, "/home/dev"),
            (
                "[ -f \"$1\" ]",
                0,
                "cache_dir = \"/mnt/fast/mbx-cache\"\n[gc]\nmax_size = \"500GiB\"\n",
            ),
            ("while [ ! -d", 0, "/mnt/fast/mbx-cache"),
            ("mj-reflink", 0, ""),
            ("mkdir -p", 0, ""),
            ("stat -f -c %T", 0, "xfs"),
        ]);
        let resolved = resolve(&podman(None), &BuildCacheConfig::default(), &executor).unwrap();
        assert_eq!(resolved.directory, PathBuf::from("/mnt/fast/mbx-cache"));
        // The host's own configuration file carries the budget.
        assert_eq!(resolved.max_size, None);
        assert_eq!(resolved.target_root, None);
        assert!(resolved.config_file.unwrap().contains("500GiB"));
        assert!(
            !executor.ran().iter().any(|line| line.contains("df -B1")),
            "a host with its own configuration is not measured"
        );
    }

    #[test]
    fn a_relocated_target_root_is_reported_for_its_own_mount() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&[
            ("$m\" --version", 0, "mbx\nmbx 1.12.0"),
            (
                "mbx cache dir --json",
                0,
                r#"{"version":1,"store":"/mnt/fast/mbx-cache/actions"}"#,
            ),
            (r#"printf '%s' "$HOME""#, 0, "/home/dev"),
            (
                "[ -f \"$1\" ]",
                0,
                "[target]\nroot = \"/mnt/fast/mbx-targets\"\n",
            ),
            ("while [ ! -d", 0, "/mnt/fast/mbx-cache"),
            ("mj-reflink", 0, ""),
            ("mkdir -p", 0, ""),
            ("stat -f -c %T", 0, "xfs"),
        ]);
        let resolved = resolve(&podman(None), &BuildCacheConfig::default(), &executor).unwrap();
        assert_eq!(
            resolved.target_root,
            Some(PathBuf::from("/mnt/fast/mbx-targets"))
        );
    }

    #[test]
    fn a_target_root_inside_the_cache_directory_needs_no_second_mount() {
        assert_eq!(
            relocated_target_root("[target]\nroot = \"targets\"\n", Path::new("/cache")),
            None
        );
        assert_eq!(
            relocated_target_root("[target]\nroot = \"/cache/targets\"\n", Path::new("/cache")),
            None
        );
    }

    #[test]
    fn a_cargo_installed_mbx_off_the_path_is_queried_where_it_was_found() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.retain(|(needle, _, _)| *needle != "$m\" --version");
        answers.push(("$m\" --version", 0, "/home/dev/.cargo/bin/mbx\nmbx 1.12.0"));
        answers.push((
            "/home/dev/.cargo/bin/mbx cache dir --json",
            0,
            r#"{"version":1,"store":"/mnt/fast/mbx-cache/actions"}"#,
        ));
        let executor = ProbeExecutor::new(&answers);
        let resolved = resolve(&podman(None), &BuildCacheConfig::default(), &executor).unwrap();
        assert_eq!(resolved.directory, PathBuf::from("/mnt/fast/mbx-cache"));
    }

    #[test]
    fn an_older_native_mbx_must_not_share_the_store() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&[("$m\" --version", 0, "mbx\nmbx 1.11.9")]);
        assert_eq!(
            resolve(&podman(None), &BuildCacheConfig::default(), &executor),
            None
        );
    }

    #[test]
    fn a_host_without_mbx_falls_back_to_the_default_cache_directory() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&plain_host());
        let resolved = resolve(&podman(None), &BuildCacheConfig::default(), &executor).unwrap();
        assert_eq!(resolved.directory, PathBuf::from("/home/dev/.cache/mbx"));
        // min(100 GB, 800 GB / 4) is the 100 GB cap.
        assert_eq!(resolved.max_size.as_deref(), Some("100000000000B"));
    }

    #[test]
    fn a_small_volume_takes_a_quarter_of_its_free_space() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.retain(|(needle, _, _)| *needle != "df -B1 -P");
        answers.push((
            "df -B1 -P",
            0,
            "Filesystem 1B-blocks Used Available Capacity Mounted\n/dev/sda1 100000000 60000000 40000000 60% /home\n",
        ));
        let executor = ProbeExecutor::new(&answers);
        let resolved = resolve(&podman(None), &BuildCacheConfig::default(), &executor).unwrap();
        assert_eq!(resolved.max_size.as_deref(), Some("10000000B"));
    }

    #[test]
    fn target_overrides_win_over_every_default() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.push(("mbx cache dir", 0, r#"{"store":"/other/actions"}"#));
        let executor = ProbeExecutor::new(&answers);
        let resolved = resolve(
            &podman(Some(TargetBuildCache {
                enabled: Some(true),
                directory: Some(PathBuf::from("/mnt/nvme/mbx")),
                max_size: Some("250GiB".into()),
            })),
            &BuildCacheConfig::default(),
            &executor,
        )
        .unwrap();
        assert_eq!(resolved.directory, PathBuf::from("/mnt/nvme/mbx"));
        assert_eq!(resolved.max_size.as_deref(), Some("250GiB"));
        assert!(
            !executor
                .ran()
                .iter()
                .any(|line| line.contains("mj-reflink")),
            "an explicit enabled setting skips the reflink probe"
        );
    }

    #[test]
    fn a_volume_without_reflinks_runs_without_the_cache() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.retain(|(needle, _, _)| *needle != "mj-reflink");
        answers.push(("mj-reflink", 1, ""));
        let executor = ProbeExecutor::new(&answers);
        assert_eq!(
            resolve(&podman(None), &BuildCacheConfig::default(), &executor),
            None
        );
    }

    #[test]
    fn the_preview_names_the_resolved_values_and_the_reason_the_cache_is_off() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.retain(|(needle, _, _)| *needle != "mj-reflink");
        answers.push(("mj-reflink", 1, ""));
        let executor = ProbeExecutor::new(&answers);
        let preview = preview_build_cache(
            &configured_local_machine(),
            &BuildCacheConfig::default(),
            &executor,
        )
        .unwrap()
        .unwrap();
        assert_eq!(preview.native_mbx, None);
        assert_eq!(
            preview.directory,
            Some(PathBuf::from("/home/dev/.cache/mbx"))
        );
        assert_eq!(
            preview.max_size,
            Some(BuildCacheLimit::Size("100000000000B".into()))
        );
        assert!(
            preview
                .off_reason
                .as_deref()
                .is_some_and(|reason| reason.contains("reflinks")),
            "{:?}",
            preview.off_reason
        );
        // A preview reads the host; it never creates the directory.
        assert!(!executor.ran().iter().any(|line| line.contains("mkdir")));

        let executor = ProbeExecutor::new(&[
            ("$m\" --version", 0, "mbx\nmbx 1.12.0"),
            (
                "mbx cache dir --json",
                0,
                r#"{"version":1,"store":"/mnt/fast/mbx-cache/actions"}"#,
            ),
            (r#"printf '%s' "$HOME""#, 0, "/home/dev"),
            ("[ -f \"$1\" ]", 0, "[gc]\nmax_size = \"500GiB\"\n"),
            ("while [ ! -d", 0, "/mnt/fast"),
            ("mj-reflink", 0, ""),
            ("stat -f -c %T", 0, "xfs"),
        ]);
        let preview = preview_build_cache(
            &configured_local_machine(),
            &BuildCacheConfig::default(),
            &executor,
        )
        .unwrap()
        .unwrap();
        assert_eq!(preview.native_mbx.as_deref(), Some("1.12.0"));
        assert_eq!(
            preview.directory,
            Some(PathBuf::from("/mnt/fast/mbx-cache"))
        );
        assert_eq!(
            preview.max_size,
            Some(BuildCacheLimit::HostConfiguration(Some("500GiB".into())))
        );
        assert_eq!(preview.off_reason, None);
        assert!(!executor.ran().iter().any(|line| line.contains("mkdir")));
    }

    #[test]
    fn a_network_filesystem_runs_without_the_cache() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.retain(|(needle, _, _)| *needle != "stat -f -c %T");
        answers.push(("stat -f -c %T", 0, "nfs4"));
        let executor = ProbeExecutor::new(&answers);
        assert_eq!(
            resolve(&podman(None), &BuildCacheConfig::default(), &executor),
            None
        );
    }

    #[test]
    fn the_global_switch_short_circuits_every_host_command() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&plain_host());
        assert_eq!(
            resolve(
                &podman(None),
                &BuildCacheConfig { enabled: false },
                &executor
            ),
            None
        );
        assert!(executor.ran().is_empty());
    }

    #[test]
    fn local_podman_and_local_docker_inspect_one_machine_once() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&plain_host());
        let settings = BuildCacheConfig::default();
        let first = resolve(&podman(None), &settings, &executor).unwrap();
        let ran = executor.ran().len();
        assert!(ran > 0, "the first resolve inspects the host");
        let second = resolve(&docker(None), &settings, &executor).unwrap();
        assert_eq!(
            first, second,
            "both engines on this machine share one cache"
        );
        assert_eq!(
            executor.ran().len(),
            ran,
            "the second runtime is answered from the machine's recorded inspection: {:?}",
            executor.ran()
        );
    }

    #[test]
    fn a_machine_without_a_standing_host_has_no_build_cache_preview() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&plain_host());
        let fleet: mj_core::config::Machine = serde_json::from_value(serde_json::json!({
            "kind": "aws-ec2",
            "region": "us-east-1",
            "launch_template": "lt-1",
            "ssh_user": "ubuntu",
        }))
        .unwrap();
        assert_eq!(
            preview_build_cache(&fleet, &BuildCacheConfig::default(), &executor).unwrap(),
            None
        );
        assert!(executor.ran().is_empty());
    }

    #[test]
    fn apple_and_bare_targets_have_no_shared_build_cache() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&plain_host());
        for target in [
            TargetTemplate::AppleContainer(container(None)),
            TargetTemplate::LocalBare,
            TargetTemplate::SshBare {
                ssh: SshTarget {
                    destination: "dev@example.test".into(),
                    ssh_args: Vec::new(),
                },
                workspace_prefix: "workspaces".into(),
            },
        ] {
            assert_eq!(
                resolve(&target, &BuildCacheConfig::default(), &executor),
                None,
                "{target:?}"
            );
        }
        assert!(executor.ran().is_empty());
    }

    fn bundle() -> targets::ProjectBundleSpec {
        targets::ProjectBundleSpec {
            primary: "main".into(),
            repositories: vec![targets::RepositorySpec {
                url: Some("https://github.com/example/main.git".into()),
                push_urls: Vec::new(),
                destination: "main".into(),
                git_ref: None,
                reference: None,
            }],
        }
    }

    fn clone_cache() -> super::super::git_cache::PreparedCloneCache {
        super::super::git_cache::PreparedCloneCache::from_mirrors(
            [(
                "main".to_owned(),
                PathBuf::from("/home/dev/mirror/repo.git"),
            )]
            .into_iter()
            .collect(),
        )
    }

    fn session(container_workspace: Option<&str>) -> mj_core::state::SessionRecord {
        let mut record = crate::controller::test_support::checkpoint_test_session("session-1");
        record.container_workspace = container_workspace.map(PathBuf::from);
        record
    }

    #[test]
    fn a_rust_session_mounts_the_cache_at_the_host_path() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.push(("cat-file -e HEAD:Cargo.toml", 0, ""));
        let executor = ProbeExecutor::new(&answers);
        let mut mounts = Vec::new();
        let build_cache = prepare(
            &podman(None),
            &BuildCacheConfig::default(),
            &session(Some("/workspace/session-1")),
            Some(&bundle()),
            Some(&clone_cache()),
            &mut mounts,
            &executor,
        )
        .expect("a Rust session uses the build cache");
        assert_eq!(build_cache.directory, PathBuf::from("/home/dev/.cache/mbx"));
        assert_eq!(
            mounts,
            vec![targets::AdditionalMount {
                source: PathBuf::from("/home/dev/.cache/mbx"),
                destination: PathBuf::from("/home/dev/.cache/mbx"),
                access: targets::MountAccess::Rw,
            }]
        );
    }

    #[test]
    fn a_repository_without_a_root_manifest_runs_without_the_cache() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.push(("cat-file -e HEAD:Cargo.toml", 1, ""));
        let executor = ProbeExecutor::new(&answers);
        let mut mounts = Vec::new();
        assert_eq!(
            prepare(
                &podman(None),
                &BuildCacheConfig::default(),
                &session(Some("/workspace/session-1")),
                Some(&bundle()),
                Some(&clone_cache()),
                &mut mounts,
                &executor,
            ),
            None
        );
        assert!(mounts.is_empty());
    }

    #[test]
    fn a_session_at_the_legacy_shared_workspace_runs_without_the_cache() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.push(("cat-file -e HEAD:Cargo.toml", 0, ""));
        let executor = ProbeExecutor::new(&answers);
        let mut mounts = Vec::new();
        assert_eq!(
            prepare(
                &podman(None),
                &BuildCacheConfig::default(),
                &session(None),
                Some(&bundle()),
                Some(&clone_cache()),
                &mut mounts,
                &executor,
            ),
            None
        );
        assert!(executor.ran().is_empty());
    }

    #[test]
    fn a_session_without_a_prepared_clone_cache_runs_without_the_cache() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.push(("cat-file -e HEAD:Cargo.toml", 0, ""));
        let executor = ProbeExecutor::new(&answers);
        let mut mounts = Vec::new();
        assert_eq!(
            prepare(
                &podman(None),
                &BuildCacheConfig::default(),
                &session(Some("/workspace/session-1")),
                Some(&bundle()),
                None,
                &mut mounts,
                &executor,
            ),
            None
        );
    }

    #[test]
    fn an_apple_target_never_shares_a_build_cache() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.push(("cat-file -e HEAD:Cargo.toml", 0, ""));
        let executor = ProbeExecutor::new(&answers);
        let mut mounts = Vec::new();
        assert_eq!(
            prepare(
                &TargetTemplate::AppleContainer(container(None)),
                &BuildCacheConfig::default(),
                &session(Some("/workspace/session-1")),
                Some(&bundle()),
                Some(&clone_cache()),
                &mut mounts,
                &executor,
            ),
            None
        );
        assert!(executor.ran().is_empty());
    }

    #[test]
    fn a_resumed_session_reuses_its_recorded_cache_without_resolving_again() {
        let _isolated = isolated();
        let executor = ProbeExecutor::new(&[]);
        let mut record = session(Some("/workspace/session-1"));
        record.build_cache = Some(SessionBuildCache {
            host: "local".into(),
            directory: PathBuf::from("/mnt/fast/mbx-cache"),
            max_size: None,
            target_root: Some(PathBuf::from("/mnt/fast/mbx-targets")),
        });
        let mut mounts = Vec::new();
        let build_cache = prepare(
            &podman(None),
            &BuildCacheConfig::default(),
            &record,
            None,
            None,
            &mut mounts,
            &executor,
        )
        .expect("a resumed session keeps its build cache");
        assert_eq!(build_cache, record.build_cache.unwrap());
        assert_eq!(
            mounts
                .iter()
                .map(|mount| mount.destination.clone())
                .collect::<Vec<_>>(),
            vec![
                PathBuf::from("/mnt/fast/mbx-cache"),
                PathBuf::from("/mnt/fast/mbx-targets"),
            ]
        );
        assert!(executor.ran().is_empty());
    }

    #[test]
    fn a_session_moved_to_another_host_resolves_its_build_cache_again() {
        let _isolated = isolated();
        let mut answers = plain_host();
        answers.push(("cat-file -e HEAD:Cargo.toml", 0, ""));
        let executor = ProbeExecutor::new(&answers);
        let mut record = session(Some("/workspace/session-1"));
        record.build_cache = Some(SessionBuildCache {
            // The host the session was provisioned on, which the target below
            // is not.
            host: "ssh:dev@example.test".into(),
            directory: PathBuf::from("/mnt/fast/mbx-cache"),
            max_size: None,
            target_root: Some(PathBuf::from("/mnt/fast/mbx-targets")),
        });
        let mut mounts = Vec::new();

        let build_cache = prepare(
            &podman(None),
            &BuildCacheConfig::default(),
            &record,
            Some(&bundle()),
            Some(&clone_cache()),
            &mut mounts,
            &executor,
        )
        .expect("the destination host qualifies on its own");

        assert_eq!(build_cache.host, "local");
        assert_eq!(build_cache.directory, PathBuf::from("/home/dev/.cache/mbx"));
        assert_eq!(build_cache.target_root, None);
        assert_eq!(
            mounts
                .iter()
                .map(|mount| mount.destination.clone())
                .collect::<Vec<_>>(),
            vec![PathBuf::from("/home/dev/.cache/mbx")]
        );
        assert!(
            executor
                .ran()
                .iter()
                .any(|line| line.contains("mj-reflink"))
        );
    }

    #[test]
    fn an_attached_directory_over_the_cache_wins() {
        let build_cache = SessionBuildCache {
            host: "local-podman".into(),
            directory: PathBuf::from("/mnt/fast/mbx-cache"),
            max_size: None,
            target_root: None,
        };
        let mut mounts = vec![targets::AdditionalMount {
            source: PathBuf::from("/elsewhere"),
            destination: PathBuf::from("/mnt/fast/mbx-cache/actions"),
            access: targets::MountAccess::Ro,
        }];
        assert!(!attach_mounts(&build_cache, &mut mounts));
        assert_eq!(mounts.len(), 1);
    }

    #[test]
    fn versions_compare_by_release_order() {
        assert!(version_at_least("1.12.0", "1.12.0"));
        assert!(version_at_least("1.12.1", "1.12.0"));
        assert!(version_at_least("2.0.0", "1.12.0"));
        assert!(!version_at_least("1.11.9", "1.12.0"));
        assert!(!version_at_least("1.9.0", "1.12.0"));
        assert!(!version_at_least("not-a-version", "1.12.0"));
    }

    #[test]
    fn free_space_is_read_from_the_available_column() {
        assert_eq!(
            available_bytes(
                "Filesystem 1B-blocks Used Available Capacity Mounted on\n\
                 /dev/sda1 1000 400 600 40% /\n"
            ),
            Some(600)
        );
        assert_eq!(available_bytes("Filesystem 1B-blocks\n"), None);
    }
}