dodot-lib 5.7.0

Core library for dodot dotfiles manager
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
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
//! The orientation inventory shown before a user approves an implicit root.
//!
//! Its job is recognition, not prediction: root-wide counts per configured
//! handler category plus at most [`MAX_INVENTORY_PATHS`] example paths across
//! the whole prompt, prioritized so shell and code-execution entries — the
//! ones that can change every future shell — are the ones the user actually
//! sees. Omitted paths are counted, never silently dropped.
//!
//! Explicitly *not* a dry run: building this reads configuration and routing
//! metadata only. It never renders templates, resolves secrets, or inspects
//! candidate pack-entry contents (Spec, "Non-Goals"), which is also why a
//! first-ever run needs no rendered baseline.
//!
//! What that costs in precision is deliberate. A file is classified by the
//! handler the rules route it to, under the name it carries on disk: a
//! template appears as its source name because rendering it is exactly the
//! work this refuses to do. The prompt is asking "is this the right
//! directory?", and a source name answers that better than a rendered one
//! would.

use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};

use crate::config::{mappings_to_rules, ConfigManager};
use crate::fs::Fs;
use crate::gates::{pack_os_active, GateTable, HostFacts};
use crate::handlers::{create_registry, ExecutionPhase};
use crate::packs::scan_packs;
use crate::rules::{RuleMatch, Scanner};

use super::error::{Result, SafetyLockError};
use super::roots::ResolvedRoot;

/// The per-directory configuration file both the root and each pack may carry.
const DOTFILES_CONFIG_FILE: &str = ".dodot.toml";

/// The most paths the prompt may show, across all categories combined.
///
/// A cap, not a per-category quota: the point is that the confirmation
/// question stays on screen (Spec, story 3).
pub const MAX_INVENTORY_PATHS: usize = 10;

/// A configured-handler category, in prompt priority order.
///
/// The derived [`Ord`] *is* the priority: shell and code execution first
/// because they run or alter program state, then the categories that alter
/// path resolution and user-visible files. Sorting entries by
/// `(category, path)` therefore produces the prompt's order directly.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum InventoryCategory {
    /// Shell startup files added to every future shell session.
    Shell,
    /// Install scripts, package manifests, and other provisioning inputs.
    CodeExecution,
    /// Directories added to `PATH`.
    Path,
    /// External handlers.
    External,
    /// Symlinked files.
    Link,
    /// Everything else the configured handlers recognize.
    Other,
}

impl InventoryCategory {
    /// The categories in the order the prompt lists them.
    pub const PROMPT_ORDER: [InventoryCategory; 6] = [
        InventoryCategory::Shell,
        InventoryCategory::CodeExecution,
        InventoryCategory::Path,
        InventoryCategory::External,
        InventoryCategory::Link,
        InventoryCategory::Other,
    ];

    /// The category a handler in `phase` contributes to, or `None` when it
    /// contributes nothing.
    ///
    /// Derived from the handler's declared phase rather than a table of
    /// handler names, so a handler added later is classified by what it does
    /// instead of by whether someone remembered to extend this module.
    ///
    /// [`Filter`](ExecutionPhase::Filter) is the `None`: `ignore`, `skip`, and
    /// `gate` claim a file precisely so that nothing is deployed from it, and
    /// counting those files would inflate every number the prompt shows with
    /// entries approving the root cannot act on.
    pub fn for_phase(phase: ExecutionPhase) -> Option<Self> {
        match phase {
            ExecutionPhase::Filter => None,
            ExecutionPhase::ShellInit => Some(InventoryCategory::Shell),
            ExecutionPhase::Provision | ExecutionPhase::Setup => {
                Some(InventoryCategory::CodeExecution)
            }
            ExecutionPhase::PathExport => Some(InventoryCategory::Path),
            ExecutionPhase::External => Some(InventoryCategory::External),
            ExecutionPhase::Link => Some(InventoryCategory::Link),
        }
    }

    /// Stable, user-facing name of the category.
    pub fn label(self) -> &'static str {
        match self {
            InventoryCategory::Shell => "shell",
            InventoryCategory::CodeExecution => "code execution",
            InventoryCategory::Path => "path",
            InventoryCategory::External => "external",
            InventoryCategory::Link => "link",
            InventoryCategory::Other => "other",
        }
    }
}

/// One example path shown in the prompt.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct InventoryEntry {
    /// The category this file was recognized as. First sort key, so
    /// high-impact entries survive the path cap.
    pub category: InventoryCategory,
    /// The file's path relative to the root being approved — the prompt names
    /// the root once and stays readable.
    pub relative_path: PathBuf,
}

/// How many files a category recognized, root-wide.
///
/// Counts are unaffected by the path cap: they stay visible even when their
/// example paths were omitted.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CategoryCount {
    pub category: InventoryCategory,
    pub files: usize,
}

/// The bounded orientation inventory for one root.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RootInventory {
    /// Root-wide counts per category, in [`InventoryCategory::PROMPT_ORDER`].
    ///
    /// Categories that recognized nothing are absent rather than present with
    /// a zero: the prompt is a recognition aid, and a column of zeroes is
    /// noise between the user and the question being asked.
    pub counts: Vec<CategoryCount>,
    /// At most [`MAX_INVENTORY_PATHS`] example paths, category-priority first.
    pub sample: Vec<InventoryEntry>,
    /// How many recognized files are not in `sample`.
    pub omitted: usize,
}

impl RootInventory {
    /// Total recognized files across all categories.
    pub fn total_files(&self) -> usize {
        self.counts.iter().map(|count| count.files).sum()
    }
}

/// Build the orientation inventory for the root about to be approved.
///
/// Root-wide by construction: there is no pack filter to pass. A command that
/// operates on one pack still approves the whole path, so an inventory scoped
/// to the filter would describe less than the answer authorizes (Spec,
/// "Risks"). Every active pack under the root is walked.
///
/// The walk is the ordinary routing pipeline and nothing more: discover packs,
/// load each pack's merged configuration, generate its rules, and ask the
/// scanner which handler claims each top-level entry. No handler is
/// constructed to produce intents, no template is rendered, no secret is
/// resolved, and no candidate file's contents are read — the registry is
/// consulted only for each handler's declared phase. A directory entry counts
/// once, as the one thing routing recognized; the scanner does not descend
/// into it and neither does this.
///
/// `fs` and `host` are injected for the same reason every other Safety Lock
/// entry point takes its collaborators: this module captures no process state
/// of its own, so gating decisions stay stateable in a test rather than
/// depending on the machine the suite runs on. The injection is not total, and
/// callers should not read it as a virtualized filesystem: the pack walk,
/// routing, and registry go through `fs`, but configuration is loaded by
/// [`ConfigManager`], which reads `.dodot.toml` through `std::fs` and searches
/// ancestors for a `.git` marker. A test that varies configuration therefore
/// has to write real files, as this module's own tests do.
///
/// Fails when a `.dodot.toml` cannot be loaded
/// ([`DotfilesConfigUnusable`](SafetyLockError::DotfilesConfigUnusable)) or a
/// pack cannot be walked or routed
/// ([`PackRoutingUnusable`](SafetyLockError::PackRoutingUnusable)), each
/// naming the file or directory at fault. Both fail the inventory rather than
/// omitting the unreadable part, because the prompt's counts are the user's
/// only evidence about the root: an inventory that quietly skipped what it
/// could not read would understate what approval permits.
pub fn build_inventory(
    root: &ResolvedRoot,
    fs: &dyn Fs,
    host: &HostFacts,
) -> Result<RootInventory> {
    let root_path = root.as_path();
    let config = ConfigManager::new(root_path)
        .and_then(|manager| manager.root_config().map(|config| (manager, config)));
    let (manager, root_config) =
        config.map_err(|error| unusable_config(root_path.join(DOTFILES_CONFIG_FILE), error))?;

    let discovered = scan_packs(fs, root_path, &root_config.pack.ignore)
        .map_err(|error| unusable_routing(root_path, error))?;

    // Only `Handler::phase` is read, so the run-once handlers never reach a
    // subprocess — the same posture `handlers::configuration_handler_names`
    // takes when it inspects the registry without exercising it.
    let runner = crate::datastore::NoopCommandRunner;
    let registry = create_registry(fs, &runner);
    let scanner = Scanner::new(fs);

    let mut counts: BTreeMap<InventoryCategory, usize> = BTreeMap::new();
    let mut entries: Vec<InventoryEntry> = Vec::new();

    for pack in &discovered.packs {
        let pack_config = manager
            .config_for_pack(&pack.path)
            .map_err(|error| unusable_config(pack.path.join(DOTFILES_CONFIG_FILE), error))?;

        // A pack gated off this host deploys nothing here, so counting it
        // would describe a machine the user is not on.
        if !pack_os_active(&pack_config.pack.os, host) {
            continue;
        }

        let owner = |declared| {
            gate_configuration_owner(root_path, pack, &pack_config, &root_config, declared)
        };

        // Each failure domain is attributed from its own evidence: a
        // `merge_user` failure is always a `[gates]` entry, and a `scan_pack`
        // failure never is, because the gate table it uses was already merged
        // above. Reading one domain's verdict for the other's failure would
        // send the user to a file that is not the one that failed.
        let mut gates = GateTable::with_builtins();
        if !pack_config.gates.is_empty() {
            gates.merge_user(&pack_config.gates).map_err(|error| {
                unusable_config(
                    owner(broken_gate_entry_owner(&root_config, &pack_config)),
                    error,
                )
            })?;
        }

        let rules = mappings_to_rules(&pack_config.mappings);
        let matches = scanner
            .scan_pack(
                pack,
                &rules,
                &pack_config.pack.ignore,
                &gates,
                host,
                &pack_config.mappings.gates,
            )
            .map_err(|error| {
                let declared = broken_mapping_entry_owner(&root_config, &pack_config);
                unusable_scan(&pack.path, &owner(declared), error)
            })?;

        for matched in matches {
            let Some(category) = category_of(&matched.handler, &registry) else {
                continue;
            };

            *counts.entry(category).or_default() += 1;
            entries.push(InventoryEntry {
                category,
                // Pack-relative becomes root-relative: the prompt names the
                // root once, and the pack directory is part of what the user
                // is being asked to recognize.
                relative_path: Path::new(&pack.name)
                    .join(source_relative_path(&pack.path, &matched)),
            });
        }
    }

    // `(category, relative_path)`, which is the prompt's order — see
    // `InventoryCategory`. Sorting before the cap is what makes the cap
    // keep the highest-impact entries rather than the first ones walked.
    entries.sort();
    let recognized = entries.len();
    entries.truncate(MAX_INVENTORY_PATHS);

    Ok(RootInventory {
        counts: InventoryCategory::PROMPT_ORDER
            .iter()
            .filter_map(|category| {
                counts.get(category).map(|&files| CategoryCount {
                    category: *category,
                    files,
                })
            })
            .collect(),
        omitted: recognized - entries.len(),
        sample: entries,
    })
}

/// The entry's path as the root actually spells it, relative to its pack.
///
/// [`RuleMatch::relative_path`] is the *effective* path routing works from,
/// not the one on disk: a passing basename gate presents
/// `aliases._darwin.sh` under its stripped name `aliases.sh`, and a passing
/// directory gate lifts `_darwin/aliases.sh` to `aliases.sh` at the pack root.
/// That rewrite is right for handlers and wrong for this prompt, which asks
/// the user to recognize their own root by the names it carries — a path that
/// is not in the directory is evidence about a directory that does not exist.
/// The absolute path is never rewritten, so the displayed path is derived from
/// it; classification still uses the effective match.
fn source_relative_path<'a>(pack_path: &Path, matched: &'a RuleMatch) -> &'a Path {
    matched
        .absolute_path
        .strip_prefix(pack_path)
        .unwrap_or(matched.relative_path.as_path())
}

/// The inventory category a routed file contributes to, or `None` when it
/// contributes nothing.
///
/// A handler the registry does not know is [`Other`](InventoryCategory::Other)
/// rather than skipped: it was routed by the loaded configuration, so it is a
/// file approval would act on, and dropping it would undercount the root.
fn category_of(
    handler: &str,
    registry: &std::collections::HashMap<String, Box<dyn crate::handlers::Handler + '_>>,
) -> Option<InventoryCategory> {
    match registry.get(handler) {
        Some(handler) => InventoryCategory::for_phase(handler.phase()),
        None => Some(InventoryCategory::Other),
    }
}

fn unusable_config(config_file: PathBuf, error: crate::error::DodotError) -> SafetyLockError {
    SafetyLockError::DotfilesConfigUnusable {
        config_file,
        reason: error.to_string(),
    }
}

fn unusable_routing(
    directory: impl Into<PathBuf>,
    error: crate::error::DodotError,
) -> SafetyLockError {
    SafetyLockError::PackRoutingUnusable {
        directory: directory.into(),
        reason: error.to_string(),
    }
}

/// Which `.dodot.toml` declared the entry a gate failure is about.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum GateLayer {
    Root,
    Pack,
}

/// Whether one `[gates]` entry stands up on its own.
///
/// Validated through [`GateTable::merge_user`] rather than a reimplementation
/// of it, so the question asked here cannot drift from the question the walk
/// asks.
fn gate_entry_is_valid(label: &str, dimensions: &HashMap<String, String>) -> bool {
    let one = HashMap::from([(label.to_string(), dimensions.clone())]);
    GateTable::with_builtins().merge_user(&one).is_ok()
}

/// The layer that declared the first invalid `[gates]` entry, if any.
///
/// Per-entry, because per-entry is where the provenance survives: labels merge
/// key by key, so the pack's resolved table holds every label either file
/// declared. For each broken one, the root owns it exactly when the root's own
/// version of that same label is broken too — which is also what makes the
/// deep merge safe to reason about. A pack that adds `os` to a root label
/// already carrying a bad dimension inherits the bad dimension, and the root's
/// version fails on its own, so the root is named; a pack that breaks a label
/// the root had valid is named itself.
///
/// Entries are visited in label order so a configuration with several broken
/// entries always reports the same one.
fn broken_gate_entry_owner(
    root_config: &crate::config::DodotConfig,
    pack_config: &crate::config::DodotConfig,
) -> Option<GateLayer> {
    let mut labels: Vec<&String> = pack_config.gates.keys().collect();
    labels.sort();

    labels.into_iter().find_map(|label| {
        let dimensions = pack_config.gates.get(label)?;
        if gate_entry_is_valid(label, dimensions) {
            return None;
        }

        let root_owns = root_config
            .gates
            .get(label)
            .is_some_and(|root_dimensions| !gate_entry_is_valid(label, root_dimensions));

        Some(if root_owns {
            GateLayer::Root
        } else {
            GateLayer::Pack
        })
    })
}

/// The layer that declared the `[mappings.gates]` glob the walk could not
/// compile, if that is why it stopped.
///
/// Deliberately narrower than "any entry that looks unusable". Glob
/// compilation is the one mapping check the walk runs *eagerly*, over the
/// whole table, before it looks at a single file — so if a glob does not
/// compile, that is necessarily what stopped the walk, and the entry naming it
/// is exact: this table is a flat glob-to-label map, so an entry belongs to
/// the pack precisely when the pack's value for that glob differs from the
/// root's.
///
/// Label resolution deliberately does *not* appear here, though an unresolved
/// label reads like a defect. The walk resolves a mapping's label only when
/// that mapping's glob matches an entry it actually met, so a mapping matching
/// nothing is accepted with a label nothing defines. Treating one as a defect
/// let it answer for a failure it had no part in — a filename gate token, say
/// — and point at the file that declared the dormant mapping instead of the
/// one the error is about. Which mapping, if any, participated in a given
/// failure is knowable only from the failure itself, which arrives as prose;
/// until it carries the entry, those failures take the scope fallback in
/// [`gate_configuration_owner`] rather than a guess.
///
/// Entries are visited in glob order, matching the order
/// [`compile_mapping_gates`](crate::gates::compile_mapping_gates) validates
/// them in, so the entry chosen here is the entry that actually failed.
fn broken_mapping_entry_owner(
    root_config: &crate::config::DodotConfig,
    pack_config: &crate::config::DodotConfig,
) -> Option<GateLayer> {
    let mut globs: Vec<&String> = pack_config.mappings.gates.keys().collect();
    globs.sort();

    globs.into_iter().find_map(|glob| {
        let label = pack_config.mappings.gates.get(glob)?;

        // One entry at a time through the shared compiler rather than
        // `glob::Pattern::new` directly. `compile_mapping_gates` exists so
        // that no two callers "disagree about which globs compile, in what
        // order, or with what failure mode" (its words), and this is a third
        // caller: the whole point of asking is to reach the same verdict the
        // walk reached. Calling the pattern API directly would be the cheaper
        // spelling of a check that is only useful while it stays identical.
        // Same reason `gate_entry_is_valid` goes through `merge_user`.
        let one = HashMap::from([(glob.clone(), label.clone())]);
        if crate::gates::compile_mapping_gates(&one, "<attribution>").is_ok() {
            return None;
        }

        Some(if root_config.mappings.gates.get(glob) == Some(label) {
            GateLayer::Root
        } else {
            GateLayer::Pack
        })
    })
}

/// The `.dodot.toml` to blame for gate configuration that fails during a walk.
///
/// A pack's configuration is the root's with the pack's merged over it, and the
/// merge keeps no per-key provenance, so a rejected gate setting does not say
/// which file wrote it. `declared` is that provenance recovered from the entry
/// that actually fails — [`broken_gate_entry_owner`] for a `[gates]` failure,
/// [`broken_mapping_entry_owner`] for a `[mappings.gates]` one. Deriving it
/// from the failing entry rather than from a summary of the root's health is
/// what keeps an unrelated defect in one file from pulling the blame off the
/// other: the two are independent, and either may be broken while the other
/// causes the failure at hand.
///
/// Some failures name no entry: a filename gate token referring to a label
/// nothing defines, and a gate-routing conflict, are both about a *file* the
/// walk met rather than a line either config wrote. `declared` is `None` there
/// and the fallback is scope — the pack when it contributed gate configuration
/// of its own, the root when it inherited all of it. That case matters most
/// for a pack with no `.dodot.toml`, where naming the pack would name a file
/// that is not on disk to open (story 14: the file named has to be the file
/// the user can act on).
///
/// The conflict stays genuinely ambiguous even so — a `[mappings.gates]` entry
/// against a filename token, either of which can be changed to settle it. Both
/// remedies are spelled out in the error's own message, which is where that
/// choice belongs.
fn gate_configuration_owner(
    root_path: &Path,
    pack: &crate::packs::Pack,
    pack_config: &crate::config::DodotConfig,
    root_config: &crate::config::DodotConfig,
    declared: Option<GateLayer>,
) -> PathBuf {
    let inherited = pack_config.gates == root_config.gates
        && pack_config.mappings.gates == root_config.mappings.gates;

    match declared {
        Some(GateLayer::Root) => root_path.join(DOTFILES_CONFIG_FILE),
        Some(GateLayer::Pack) => pack.path.join(DOTFILES_CONFIG_FILE),
        None if inherited => root_path.join(DOTFILES_CONFIG_FILE),
        None => pack.path.join(DOTFILES_CONFIG_FILE),
    }
}

/// A scan failure, classified by what the user can act on.
///
/// The scanner does two jobs at once: it walks the pack, and it interprets the
/// pack's configuration — `[mappings.gates]` globs, the gate labels those
/// entries and filename tokens name, and the conflict between the two all come
/// back as [`DodotError::Config`](crate::error::DodotError::Config). Reporting
/// those as a routing failure would name a *directory* when the thing the user
/// has to open is a `.dodot.toml` (story 14: name the bad file, not the
/// neighbourhood it is in); `gate_config_file` is which one, resolved by
/// [`gate_configuration_owner`]. Everything else the walk can fail on — an
/// unreadable directory, a layout Dodot refuses — is a routing failure and
/// names the directory.
fn unusable_scan(
    pack_path: &Path,
    gate_config_file: &Path,
    error: crate::error::DodotError,
) -> SafetyLockError {
    if matches!(error, crate::error::DodotError::Config(_)) {
        unusable_config(gate_config_file.to_path_buf(), error)
    } else {
        unusable_routing(pack_path, error)
    }
}

#[cfg(test)]
mod tests {
    use crate::fs::OsFs;
    use crate::testing::{TempEnvironment, TempEnvironmentBuilder};

    use super::super::roots::{RootIdentity, RootSource};
    use super::*;

    fn host() -> HostFacts {
        HostFacts::for_tests("darwin", "arm64")
    }

    /// The inventory of `env`'s dotfiles root, as an unapproved implicit root
    /// reaching the prompt.
    fn inventory_of(env: &TempEnvironment) -> RootInventory {
        build_inventory(&resolved(env), env.fs.as_ref(), &host()).unwrap()
    }

    fn resolved(env: &TempEnvironment) -> ResolvedRoot {
        ResolvedRoot::new(
            RootIdentity::new(canonical_root(env)).unwrap(),
            RootSource::Git,
        )
    }

    /// The root as selection would have canonicalized it — the spelling every
    /// diagnostic below names, since that is the path the inventory walks.
    fn canonical_root(env: &TempEnvironment) -> PathBuf {
        std::fs::canonicalize(&env.dotfiles_root).unwrap()
    }

    /// `(category, path)` pairs, which is what the prompt renders.
    fn sample_of(inventory: &RootInventory) -> Vec<(InventoryCategory, String)> {
        inventory
            .sample
            .iter()
            .map(|entry| {
                (
                    entry.category,
                    entry.relative_path.to_string_lossy().into_owned(),
                )
            })
            .collect()
    }

    fn counts_of(inventory: &RootInventory) -> Vec<(InventoryCategory, usize)> {
        inventory
            .counts
            .iter()
            .map(|count| (count.category, count.files))
            .collect()
    }

    /// One pack exercising every default handler mapping at once.
    fn representative_pack(builder: TempEnvironmentBuilder, name: &str) -> TempEnvironmentBuilder {
        builder
            .pack(name)
            .file("aliases.sh", "alias g=git")
            .file("install.sh", "#!/bin/sh")
            .file("Brewfile", "brew \"jq\"")
            .file("externals.toml", "")
            .file("bin/tool", "#!/bin/sh")
            .file("config", "x")
            .done()
    }

    /// Story 3, and the point of the whole module: what the user is shown is
    /// every recognized file, classified once, by the handler the loaded
    /// configuration routes it to.
    ///
    /// Once, including where two rules could claim a file: `install.sh` is
    /// matched by both the install rule and the `*.sh` shell glob, and the
    /// inventory counts whichever one routing gives it — not both, and not the
    /// lower-priority one.
    #[test]
    fn every_recognized_file_lands_in_exactly_one_category() {
        let env = representative_pack(TempEnvironment::builder(), "tools").build();

        let inventory = inventory_of(&env);

        assert_eq!(
            counts_of(&inventory),
            [
                (InventoryCategory::Shell, 1),
                // `install.sh` and `Brewfile` are both code execution: the
                // category is the handler's phase, not its name.
                (InventoryCategory::CodeExecution, 2),
                (InventoryCategory::Path, 1),
                (InventoryCategory::External, 1),
                (InventoryCategory::Link, 1),
            ]
        );
        assert_eq!(inventory.total_files(), 6);
        assert_eq!(inventory.omitted, 0);
        assert_eq!(
            sample_of(&inventory),
            [
                (InventoryCategory::Shell, "tools/aliases.sh".into()),
                (InventoryCategory::CodeExecution, "tools/Brewfile".into()),
                (InventoryCategory::CodeExecution, "tools/install.sh".into()),
                (InventoryCategory::Path, "tools/bin".into()),
                (InventoryCategory::External, "tools/externals.toml".into()),
                (InventoryCategory::Link, "tools/config".into()),
            ]
        );
    }

    /// Detail order is category priority first, then pack-relative path — so
    /// the entries that can change every future shell survive the cap, and two
    /// runs over an unchanged root show the same list.
    #[test]
    fn detail_is_ordered_by_category_then_path() {
        let env = TempEnvironment::builder()
            .pack("zsh")
            .file("zshrc.sh", "")
            .done()
            .pack("apps")
            .file("gitconfig", "")
            .file("install.sh", "")
            .done()
            .pack("alacritty")
            .file("alacritty.toml", "")
            .done()
            .build();

        let inventory = inventory_of(&env);

        assert_eq!(
            sample_of(&inventory),
            [
                (InventoryCategory::Shell, "zsh/zshrc.sh".into()),
                (InventoryCategory::CodeExecution, "apps/install.sh".into()),
                (InventoryCategory::Link, "alacritty/alacritty.toml".into()),
                (InventoryCategory::Link, "apps/gitconfig".into()),
            ]
        );
        assert_eq!(sample_of(&inventory_of(&env)), sample_of(&inventory));
    }

    /// Story 3 again, from the other side: the cap keeps the confirmation
    /// question on screen. What it must not do is quietly shrink the root —
    /// the counts stay whole and the dropped paths are counted.
    #[test]
    fn detail_stops_at_ten_paths_and_counts_what_it_dropped() {
        let mut builder = TempEnvironment::builder().pack("links");
        for index in 0..20 {
            builder = builder.file(&format!("config{index:02}"), "");
        }
        let env = representative_pack(builder.done(), "tools").build();

        let inventory = inventory_of(&env);

        assert_eq!(inventory.sample.len(), MAX_INVENTORY_PATHS);
        assert_eq!(inventory.total_files(), 26);
        assert_eq!(inventory.omitted, 16);
        assert_eq!(
            inventory.omitted + inventory.sample.len(),
            inventory.total_files(),
            "the cap lost paths instead of counting them"
        );

        // Counts are unaffected by the cap: `link` shows 21 while only four
        // of its paths are detailed.
        assert_eq!(
            counts_of(&inventory),
            [
                (InventoryCategory::Shell, 1),
                (InventoryCategory::CodeExecution, 2),
                (InventoryCategory::Path, 1),
                (InventoryCategory::External, 1),
                (InventoryCategory::Link, 21),
            ]
        );

        // And what survives is the high-impact end of the order.
        assert_eq!(
            sample_of(&inventory)[..6],
            [
                (InventoryCategory::Shell, "tools/aliases.sh".into()),
                (InventoryCategory::CodeExecution, "tools/Brewfile".into()),
                (InventoryCategory::CodeExecution, "tools/install.sh".into()),
                (InventoryCategory::Path, "tools/bin".into()),
                (InventoryCategory::External, "tools/externals.toml".into()),
                (InventoryCategory::Link, "links/config00".into()),
            ]
        );
    }

    /// Custom mappings are configuration, and configuration is what the
    /// inventory reads: routing the user changed must be the routing the
    /// prompt describes, or it would orient them against a deployment that
    /// never happens.
    #[test]
    fn custom_handler_mappings_are_honoured() {
        let env = TempEnvironment::builder()
            .pack("nvim")
            .config(
                "[mappings]\n\
                 path = \"scripts\"\n\
                 shell = [\"*.zsh\"]\n",
            )
            .file("scripts/tool", "")
            .file("profile.zsh", "")
            .file("aliases.sh", "")
            .done()
            .build();

        let inventory = inventory_of(&env);

        assert_eq!(
            sample_of(&inventory),
            [
                (InventoryCategory::Shell, "nvim/profile.zsh".into()),
                (InventoryCategory::Path, "nvim/scripts".into()),
                // `bin` is no longer the PATH directory and `*.sh` no longer
                // shell, so both fall through to the catchall.
                (InventoryCategory::Link, "nvim/aliases.sh".into()),
            ]
        );
    }

    /// Files that are claimed in order to *not* be deployed — ignored packs,
    /// ignored and skipped entries, and entries gated off this host — are not
    /// recognized files. Counting them would inflate every number the user
    /// reads with things approval cannot act on.
    #[test]
    fn what_deploys_nothing_is_not_counted() {
        let env = TempEnvironment::builder()
            .pack("archive")
            .file("vimrc", "")
            .ignored()
            .done()
            .pack("vim")
            .config("[mappings]\nignore = [\"notes.md\"]\n")
            .file("vimrc", "")
            .file("notes.md", "")
            .file("README.md", "")
            .file("linux-only.sh._linux", "")
            .done()
            .build();

        let inventory = inventory_of(&env);

        assert_eq!(counts_of(&inventory), [(InventoryCategory::Link, 1)]);
        assert_eq!(
            sample_of(&inventory),
            [(InventoryCategory::Link, "vim/vimrc".into())],
            "a filtered, gated, or ignored-pack file reached the prompt"
        );
    }

    /// A pack gated off this host deploys nothing here, so it describes a
    /// machine the user is not on.
    #[test]
    fn packs_gated_off_this_host_are_not_counted() {
        let env = TempEnvironment::builder()
            .pack("linux-tools")
            .config("[pack]\nos = [\"linux\"]\n")
            .file("aliases.sh", "")
            .done()
            .pack("vim")
            .file("vimrc", "")
            .done()
            .build();

        assert_eq!(
            sample_of(&inventory_of(&env)),
            [(InventoryCategory::Link, "vim/vimrc".into())]
        );
    }

    /// A gate that *passes* is invisible to routing by design — the scanner
    /// presents `aliases._darwin.sh` as `aliases.sh` and lifts `_darwin/vimrc`
    /// to the pack root — but it must stay visible to the prompt. The user is
    /// being asked to recognize their own root, and a path that is not in the
    /// directory is evidence about a directory that does not exist. The
    /// classification still follows the effective name: `aliases._darwin.sh`
    /// is a shell file because `aliases.sh` is what the rules match.
    #[test]
    fn a_passing_gate_is_listed_under_the_name_the_root_carries() {
        let env = TempEnvironment::builder()
            .pack("zsh")
            .file("aliases._darwin.sh", "alias g=git")
            .done()
            .pack("vim")
            .file("_darwin/vimrc", "")
            .done()
            .build();

        let inventory = inventory_of(&env);

        assert_eq!(
            counts_of(&inventory),
            [(InventoryCategory::Shell, 1), (InventoryCategory::Link, 1)],
            "the passing gates changed what the entries were classified as"
        );
        assert_eq!(
            sample_of(&inventory),
            [
                (InventoryCategory::Shell, "zsh/aliases._darwin.sh".into()),
                (InventoryCategory::Link, "vim/_darwin/vimrc".into()),
            ],
            "the prompt showed a path that does not exist under the root"
        );
    }

    /// The scanner stops at a pack's top level and so does the inventory: a
    /// directory is one entry, because one entry is what routing recognized
    /// and what a handler acts on. Recursing to make the count "more accurate"
    /// would be inventing detail the deployment does not have.
    #[test]
    fn a_directory_entry_counts_once_and_is_not_descended_into() {
        let env = TempEnvironment::builder()
            .pack("vim")
            .file("colors/one.vim", "")
            .file("colors/two.vim", "")
            .file("after/ftplugin/rust.vim", "")
            .done()
            .build();

        let inventory = inventory_of(&env);

        assert_eq!(counts_of(&inventory), [(InventoryCategory::Link, 2)]);
        assert_eq!(
            sample_of(&inventory),
            [
                (InventoryCategory::Link, "vim/after".into()),
                (InventoryCategory::Link, "vim/colors".into()),
            ]
        );
    }

    /// Spec, "Non-Goals": this is a recognition aid, not a dry run. A template
    /// is listed under its source name because rendering it — and resolving
    /// what it would render *from* — is exactly the work refused here, which
    /// is also why a first-ever run needs no rendered baseline.
    #[test]
    fn a_template_is_listed_by_its_source_name_without_being_rendered() {
        let env = TempEnvironment::builder()
            .pack("git")
            .file("gitconfig.tmpl", "[user]\n  name = {{ env.USER }}\n")
            .done()
            .build();

        let before = env.list_dir_names(&env.data_dir);

        assert_eq!(
            sample_of(&inventory_of(&env)),
            [(InventoryCategory::Link, "git/gitconfig.tmpl".into())]
        );
        assert_eq!(
            env.list_dir_names(&env.data_dir),
            before,
            "building the inventory wrote Dodot state"
        );
    }

    /// The same claim as the test above, made structurally: the implementation
    /// cannot render, preprocess, or read a candidate file because it never
    /// names the machinery that would.
    #[test]
    fn the_implementation_reads_no_candidate_contents() {
        let implementation = include_str!("inventory.rs")
            .split_once("#[cfg(test)]")
            .expect("this file carries a test module")
            .0;
        let code = implementation
            .lines()
            .filter(|line| !line.trim_start().starts_with("//"))
            .collect::<Vec<_>>()
            .join("\n");

        for forbidden in [
            "preprocess",
            "render",
            "secret",
            "read_to_string",
            "read_file",
            "to_intents",
            "DataStore",
            "walk_pack_recursive",
        ] {
            assert!(
                !code.contains(forbidden),
                "the inventory reaches past routing metadata through `{forbidden}`"
            );
        }
    }

    /// Story 14: configuration Dodot cannot load is named, with its problem,
    /// and stops the inventory — an approvable decision is never produced from
    /// a root Dodot could not describe.
    #[test]
    fn invalid_configuration_names_the_offending_file() {
        for (pack_config, root_config, offender) in [
            (Some("not valid toml"), None, "vim/.dodot.toml"),
            (None, Some("[pack]\nos = [\"linux\"]\n"), ".dodot.toml"),
        ] {
            let mut builder = TempEnvironment::builder().pack("vim").file("vimrc", "");
            if let Some(contents) = pack_config {
                builder = builder.config(contents);
            }
            let env = builder.done().build();
            if let Some(contents) = root_config {
                std::fs::write(env.dotfiles_root.join(".dodot.toml"), contents).unwrap();
            }

            let error =
                build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("accepted");

            assert!(
                matches!(
                    &error,
                    SafetyLockError::DotfilesConfigUnusable { config_file, reason }
                        if config_file == &canonical_root(&env).join(offender) && !reason.is_empty()
                ),
                "unexpected error: {error}"
            );
        }
    }

    /// Story 14 again, for the configuration that fails in *use* rather than
    /// in parsing. The scanner interprets gate configuration as it walks, so
    /// an invalid `[mappings.gates]` glob, an unresolvable label, and a file
    /// gated two ways at once all surface from the walk — and naming the pack
    /// directory for them would point the user at a place instead of at the
    /// file they have to edit.
    ///
    /// Every case here declares gate configuration in the pack, which is what
    /// makes the pack's file the one to name; the root-owned half is
    /// `configuration_inherited_from_the_root_names_the_root_config_file`.
    #[test]
    fn configuration_the_walk_rejects_names_the_pack_config_file() {
        for pack_config in [
            // An invalid glob: rejected when the mapping gates compile.
            "[mappings.gates]\n\"[unclosed\" = \"darwin\"\n",
            // A label nothing defines, reached through `[mappings.gates]`.
            "[mappings.gates]\n\"aliases.sh\" = \"no-such-label\"\n",
            // A label nothing defines, reached through the filename grammar,
            // in a pack whose own `[gates]` could have defined it.
            "[gates]\nsomething-else = { os = \"darwin\" }\n",
            // One file gated two ways at once.
            "[mappings.gates]\n\"*.sh\" = \"darwin\"\n",
        ] {
            let env = TempEnvironment::builder()
                .pack("vim")
                .config(pack_config)
                .file("aliases.sh", "")
                .file("profile._no-such-label.sh", "")
                .done()
                .build();

            let error =
                build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("accepted");

            assert!(
                matches!(
                    &error,
                    SafetyLockError::DotfilesConfigUnusable { config_file, reason }
                        if config_file == &canonical_root(&env).join("vim/.dodot.toml")
                            && !reason.is_empty()
                ),
                "unexpected error for config {pack_config:?}: {error}"
            );
        }
    }

    /// The same walk-time failures, declared one layer up. Pack configuration
    /// is the root's merged with the pack's, so gate settings the root owns
    /// reach every pack's scan — and blaming the pack for them would name a
    /// `.dodot.toml` the user never wrote and, for a pack with no
    /// configuration of its own, one that is not on disk to open.
    #[test]
    fn configuration_inherited_from_the_root_names_the_root_config_file() {
        for root_config in [
            "[mappings.gates]\n\"[unclosed\" = \"darwin\"\n",
            "[mappings.gates]\n\"aliases.sh\" = \"no-such-label\"\n",
            "",
            "[mappings.gates]\n\"*.sh\" = \"darwin\"\n",
        ] {
            let env = TempEnvironment::builder()
                .pack("vim")
                .file("aliases.sh", "")
                .file("profile._no-such-label.sh", "")
                .done()
                .build();
            std::fs::write(env.dotfiles_root.join(".dodot.toml"), root_config).unwrap();

            let error =
                build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("accepted");

            assert!(
                matches!(
                    &error,
                    SafetyLockError::DotfilesConfigUnusable { config_file, reason }
                        if config_file == &canonical_root(&env).join(DOTFILES_CONFIG_FILE)
                            && !reason.is_empty()
                ),
                "unexpected error for root config {root_config:?}: {error}"
            );
        }
    }

    /// Both layers contributing is where "did the pack write any gate
    /// configuration?" stops being enough: a valid pack entry that has nothing
    /// to do with the failure would otherwise pull the blame onto the pack,
    /// where changing or deleting that entry cannot fix anything. A root whose
    /// own layer does not stand up is named even when the pack also wrote gate
    /// configuration.
    #[test]
    fn a_broken_root_is_named_even_when_the_pack_also_configures_gates() {
        for (root_config, pack_config) in [
            // Invalid `[gates]` entry in the root, unrelated valid one in the
            // pack — codex's counterexample: deleting `laptop` fixes nothing.
            (
                "[gates]\nbroken = { nonsense = \"x\" }\n",
                "[gates]\nlaptop = { os = \"darwin\" }\n",
            ),
            // Invalid root glob, unrelated valid pack mapping.
            (
                "[mappings.gates]\n\"[unclosed\" = \"darwin\"\n",
                "[mappings.gates]\n\"vimrc\" = \"darwin\"\n",
            ),
        ] {
            let env = TempEnvironment::builder()
                .pack("vim")
                .config(pack_config)
                .file("aliases.sh", "")
                .file("vimrc", "")
                .done()
                .build();
            std::fs::write(env.dotfiles_root.join(".dodot.toml"), root_config).unwrap();

            let error =
                build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("accepted");

            assert!(
                matches!(
                    &error,
                    SafetyLockError::DotfilesConfigUnusable { config_file, reason }
                        if config_file == &canonical_root(&env).join(DOTFILES_CONFIG_FILE)
                            && !reason.is_empty()
                ),
                "unexpected error for root {root_config:?} + pack {pack_config:?}: {error}"
            );
        }
    }

    /// A `[mappings.gates]` entry the walk never consults cannot answer for a
    /// failure it had no part in.
    ///
    /// The walk resolves a mapping's label only when that mapping's glob
    /// matches an entry it met, so `"*.txt" = "no-such-label"` beside no `.txt`
    /// file is accepted — the first case here proves the inventory is built.
    /// The second puts that same dormant mapping in the root and lets the walk
    /// fail on a filename gate token instead: the diagnostic is about
    /// `a._foo.sh`, so it must name the file that has something to say about
    /// `foo` — the pack — not the one that happens to hold an unrelated
    /// unresolved mapping.
    #[test]
    fn a_mapping_the_walk_never_consults_does_not_claim_another_failure() {
        let dormant = "[mappings.gates]\n\"*.txt\" = \"no-such-label\"\n";

        let env = TempEnvironment::builder()
            .pack("vim")
            .config(dormant)
            .file("vimrc", "")
            .done()
            .build();

        assert_eq!(
            sample_of(&inventory_of(&env)),
            [(InventoryCategory::Link, "vim/vimrc".into())],
            "a mapping matching nothing was treated as a defect"
        );

        let env = TempEnvironment::builder()
            .pack("vim")
            .config("[gates]\nlaptop = { os = \"darwin\" }\n")
            .file("a._foo.sh", "")
            .done()
            .build();
        std::fs::write(env.dotfiles_root.join(DOTFILES_CONFIG_FILE), dormant).unwrap();

        let error =
            build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("accepted");

        assert!(
            matches!(
                &error,
                SafetyLockError::DotfilesConfigUnusable { config_file, reason }
                    if config_file == &canonical_root(&env).join("vim/.dodot.toml")
                        && reason.contains("foo")
            ),
            "a dormant root mapping claimed a filename-gate failure: {error}"
        );
    }

    /// The mirror of the case above, and the reason blame is read off the
    /// failing entry rather than off either file's overall health: the two
    /// tables fail independently, so a defect in one file must not pull the
    /// blame away from the other file's defect that actually stopped the walk.
    ///
    /// Both directions of the interaction, in both tables.
    #[test]
    fn the_layer_that_declared_the_failing_entry_is_the_one_named() {
        struct Case {
            root: &'static str,
            pack: &'static str,
            owner: &'static str,
        }

        for case in [
            // A root mapping completed by a pack-defined label — unresolvable
            // read alone, fine merged — beside a pack glob that will not
            // compile. The walk stops on the pack's glob.
            Case {
                root: "[mappings.gates]\n\"aliases.sh\" = \"laptop\"\n",
                pack: "[gates]\nlaptop = { os = \"darwin\" }\n\
                       [mappings.gates]\n\"[unclosed\" = \"darwin\"\n",
                owner: "vim/.dodot.toml",
            },
            // A broken root `[mappings.gates]` glob beside a broken pack
            // `[gates]` entry. `merge_user` fails first, on the pack's entry,
            // and the root's unrelated mapping defect must not claim it.
            Case {
                root: "[mappings.gates]\n\"[unclosed\" = \"darwin\"\n",
                pack: "[gates]\nbroken = { nonsense = \"x\" }\n",
                owner: "vim/.dodot.toml",
            },
            // The same shape reversed: a broken root `[gates]` entry beside a
            // pack mapping that is merely unresolvable on its own. The
            // `[gates]` failure comes first and belongs to the root.
            Case {
                root: "[gates]\nbroken = { nonsense = \"x\" }\n",
                pack: "[mappings.gates]\n\"aliases.sh\" = \"darwin\"\n",
                owner: ".dodot.toml",
            },
        ] {
            let env = TempEnvironment::builder()
                .pack("vim")
                .config(case.pack)
                .file("aliases.sh", "")
                .file("vimrc", "")
                .done()
                .build();
            std::fs::write(env.dotfiles_root.join(".dodot.toml"), case.root).unwrap();

            let error =
                build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("accepted");

            assert!(
                matches!(
                    &error,
                    SafetyLockError::DotfilesConfigUnusable { config_file, .. }
                        if config_file == &canonical_root(&env).join(case.owner)
                ),
                "root {:?} + pack {:?} should have named {}: {error}",
                case.root,
                case.pack,
                case.owner
            );
        }
    }

    /// Two broken entries owned by different files, and the one the user is
    /// told about has to be the one the named file declares.
    ///
    /// The walk and the attribution replay both visit entries in key order —
    /// `merge_user` and `compile_mapping_gates` sort, rather than taking
    /// whatever order the hash gives — so the entry that fails is the entry
    /// blame is read from. Without that, the message could quote one label
    /// while the path pointed at the file holding the other.
    #[test]
    fn the_reported_defect_and_the_named_file_are_the_same_entry() {
        for (root, pack, owner, quoted) in [
            // Root owns the lexically first broken label, pack the later one.
            (
                "[gates]\naaa = { nonsense = \"x\" }\n",
                "[gates]\nzzz = { nonsense = \"x\" }\n",
                ".dodot.toml",
                "aaa",
            ),
            // Reversed: the pack owns the lexically first one.
            (
                "[gates]\nzzz = { nonsense = \"x\" }\n",
                "[gates]\naaa = { nonsense = \"x\" }\n",
                "vim/.dodot.toml",
                "aaa",
            ),
            // The same, for globs that will not compile.
            (
                "[mappings.gates]\n\"[a\" = \"darwin\"\n",
                "[mappings.gates]\n\"[z\" = \"darwin\"\n",
                ".dodot.toml",
                "[a",
            ),
            (
                "[mappings.gates]\n\"[z\" = \"darwin\"\n",
                "[mappings.gates]\n\"[a\" = \"darwin\"\n",
                "vim/.dodot.toml",
                "[a",
            ),
        ] {
            let env = TempEnvironment::builder()
                .pack("vim")
                .config(pack)
                .file("vimrc", "")
                .done()
                .build();
            std::fs::write(env.dotfiles_root.join(DOTFILES_CONFIG_FILE), root).unwrap();

            let error =
                build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("accepted");

            assert!(
                matches!(
                    &error,
                    SafetyLockError::DotfilesConfigUnusable { config_file, reason }
                        if config_file == &canonical_root(&env).join(owner)
                            && reason.contains(quoted)
                ),
                "root {root:?} + pack {pack:?} should report {quoted:?} against {owner}: {error}"
            );
        }
    }

    /// A pack that breaks a label the root had valid owns it, even though the
    /// deep merge means the resolved entry carries both files' dimensions.
    #[test]
    fn a_pack_that_breaks_an_inherited_label_is_named_for_it() {
        let env = TempEnvironment::builder()
            .pack("vim")
            .config("[gates]\nlaptop = { nonsense = \"x\" }\n")
            .file("vimrc", "")
            .done()
            .build();
        std::fs::write(
            env.dotfiles_root.join(".dodot.toml"),
            "[gates]\nlaptop = { os = \"darwin\" }\n",
        )
        .unwrap();

        let error =
            build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("accepted");

        assert!(
            matches!(
                &error,
                SafetyLockError::DotfilesConfigUnusable { config_file, .. }
                    if config_file == &canonical_root(&env).join("vim/.dodot.toml")
            ),
            "unexpected error: {error}"
        );
    }

    /// The attribution replay is read only to place blame, never to assign it.
    ///
    /// `[mappings.gates]` and `[gates]` are separate tables, so the root can
    /// legitimately map a label that only a pack defines — valid merged, and
    /// unresolvable read on its own. The inventory must still be built. (The
    /// mirror case does not exist: gate labels deep-merge key by key, so a
    /// pack adding `os` to a root label that already carries a bad dimension
    /// inherits the bad dimension too. A pack cannot repair a broken root
    /// `[gates]` entry, only a dangling root reference to one.)
    #[test]
    fn a_root_layer_a_pack_completes_is_not_an_error() {
        let env = TempEnvironment::builder()
            .pack("vim")
            .config("[gates]\nlaptop = { os = \"darwin\" }\n")
            .file("aliases.sh", "")
            .done()
            .build();
        std::fs::write(
            env.dotfiles_root.join(".dodot.toml"),
            "[mappings.gates]\n\"aliases.sh\" = \"laptop\"\n",
        )
        .unwrap();

        let inventory = build_inventory(&resolved(&env), env.fs.as_ref(), &host())
            .expect("a root mapping a pack-defined label was refused");

        assert_eq!(
            sample_of(&inventory),
            [(InventoryCategory::Shell, "vim/aliases.sh".into())]
        );
    }

    /// A root that cannot be walked fails the inventory rather than reporting
    /// an empty one: "nothing here" and "I could not look" must not read the
    /// same to a user deciding whether to authorize the path.
    #[test]
    fn a_root_that_cannot_be_routed_fails_rather_than_reporting_nothing() {
        let env = TempEnvironment::builder().build();
        // Two packs that collide on one display name — a layout dodot
        // refuses to route.
        for name in ["010-vim", "020-vim"] {
            std::fs::create_dir(env.dotfiles_root.join(name)).unwrap();
        }

        let error = build_inventory(&resolved(&env), env.fs.as_ref(), &host()).expect_err("routed");

        assert!(
            matches!(
                &error,
                SafetyLockError::PackRoutingUnusable { directory, .. }
                    if directory == &canonical_root(&env)
            ),
            "unexpected error: {error}"
        );
    }

    /// An empty root is a real, describable state — the first thing a user
    /// pointed at the wrong directory should see is that Dodot recognized
    /// nothing in it.
    #[test]
    fn an_empty_root_inventories_to_nothing() {
        let env = TempEnvironment::builder().build();

        let inventory = build_inventory(&resolved(&env), &OsFs::new(), &host()).unwrap();

        assert!(inventory.counts.is_empty());
        assert!(inventory.sample.is_empty());
        assert_eq!(inventory.omitted, 0);
        assert_eq!(inventory.total_files(), 0);
    }

    /// Categories follow the handler's declared phase, so a handler added
    /// later is classified by what it does rather than by whether this module
    /// was updated. The match is exhaustive: a new phase will not compile
    /// until it is placed.
    #[test]
    fn categories_follow_handler_phases() {
        use ExecutionPhase::*;

        assert_eq!(
            InventoryCategory::for_phase(ShellInit),
            Some(InventoryCategory::Shell)
        );
        assert_eq!(
            InventoryCategory::for_phase(Setup),
            Some(InventoryCategory::CodeExecution)
        );
        assert_eq!(
            InventoryCategory::for_phase(Provision),
            Some(InventoryCategory::CodeExecution)
        );
        assert_eq!(
            InventoryCategory::for_phase(PathExport),
            Some(InventoryCategory::Path)
        );
        assert_eq!(
            InventoryCategory::for_phase(External),
            Some(InventoryCategory::External)
        );
        assert_eq!(
            InventoryCategory::for_phase(Link),
            Some(InventoryCategory::Link)
        );
        assert_eq!(
            InventoryCategory::for_phase(Filter),
            None,
            "a handler that deploys nothing was counted"
        );
    }

    /// A handler the registry does not know is still a file the configuration
    /// routes, so it counts — as `other`, the category that exists for
    /// exactly this.
    #[test]
    fn an_unknown_handler_counts_as_other() {
        let fs = OsFs::new();
        let runner = crate::datastore::NoopCommandRunner;
        let registry = create_registry(&fs, &runner);

        assert_eq!(
            category_of("a-handler-from-the-future", &registry),
            Some(InventoryCategory::Other)
        );
        assert_eq!(
            category_of(crate::handlers::HANDLER_SHELL, &registry),
            Some(InventoryCategory::Shell)
        );
        assert_eq!(
            category_of(crate::handlers::HANDLER_IGNORE, &registry),
            None
        );
    }

    /// The prompt's priority is encoded in the type's ordering rather than a
    /// separate table, so a sort on `(category, path)` cannot drift from the
    /// documented order.
    #[test]
    fn category_ordering_is_the_prompt_priority() {
        let mut shuffled = vec![
            InventoryCategory::Other,
            InventoryCategory::Link,
            InventoryCategory::CodeExecution,
            InventoryCategory::External,
            InventoryCategory::Shell,
            InventoryCategory::Path,
        ];
        shuffled.sort();

        assert_eq!(shuffled, InventoryCategory::PROMPT_ORDER);
    }

    #[test]
    fn entries_sort_by_category_then_relative_path() {
        let mut entries = [
            InventoryEntry {
                category: InventoryCategory::Link,
                relative_path: PathBuf::from("vim/vimrc"),
            },
            InventoryEntry {
                category: InventoryCategory::Shell,
                relative_path: PathBuf::from("zsh/zshrc"),
            },
            InventoryEntry {
                category: InventoryCategory::Shell,
                relative_path: PathBuf::from("bash/bashrc"),
            },
        ];
        entries.sort();

        assert_eq!(
            entries
                .iter()
                .map(|entry| entry.relative_path.to_str().unwrap())
                .collect::<Vec<_>>(),
            ["bash/bashrc", "zsh/zshrc", "vim/vimrc"]
        );
    }

    #[test]
    fn counts_stay_whole_when_paths_are_capped() {
        let inventory = RootInventory {
            counts: vec![
                CategoryCount {
                    category: InventoryCategory::Shell,
                    files: 12,
                },
                CategoryCount {
                    category: InventoryCategory::Link,
                    files: 30,
                },
            ],
            sample: Vec::new(),
            omitted: 42,
        };

        assert_eq!(inventory.total_files(), 42);
        assert!(MAX_INVENTORY_PATHS < inventory.total_files());
    }

    #[test]
    fn category_labels_are_stable() {
        assert_eq!(
            InventoryCategory::PROMPT_ORDER
                .iter()
                .map(|category| category.label())
                .collect::<Vec<_>>(),
            [
                "shell",
                "code execution",
                "path",
                "external",
                "link",
                "other"
            ]
        );
    }
}