epicenter-cli 0.0.3

The state layer for Cargo builds: shared cache, durable vault, per-job dirs
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
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
//! `epicenter evict` — deliberate, user-invoked removal of units.
//! Eviction is never automatic: the caller must select units explicitly.
//!
//! Safety shape (mirrors `prune`):
//! - **dry run by default** — pass `--yes` to actually delete;
//! - **current toolchain generation is protected** unless `--force`:
//!   removing it would force a rebuild of units cargo may be using right now;
//! - at least one selector is required — there is no "evict everything".

use std::collections::BTreeMap;
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};

use anyhow::{Context, Result, bail};
use walkdir::WalkDir;

use crate::fingerprint::{self, ArtifactInfo, Unit};
use crate::util::{self, human_age, human_bytes, parse_duration};
use crate::who;

/// What the user asked to evict.
pub struct Options<'a> {
    /// Unit query: dir name, dir hash fragment, or package name.
    pub unit: Option<&'a str>,
    /// Every unit on a stale toolchain generation (the bulk selector).
    pub stale: bool,
    /// Exact (or prefix) rustc fingerprint identity.
    pub rustc_key: Option<&'a str>,
    /// Units whose artifacts are already missing (fingerprint husks).
    pub husks: bool,
    /// Units untouched for longer than this (e.g. `30d`).
    pub older_than: Option<&'a str>,
    /// The per-job working-dir tier: collect `work/<job-id>/` views
    /// (its own selector; default age 7 days).
    pub work_dirs: bool,
    /// Remove ONE job dir now (name, `work/<id>`, or a path under `work/`):
    /// explicit choices bypass the GC age window; a locked dir is refused.
    pub work_dir: Option<&'a str>,
    /// Surgical view operation: unlink units from ONE view only (`build`,
    /// a `work/<id>` job dir, or a path). Never touches the vault.
    pub work: Option<&'a str>,
    /// Package selector for `--work` (dashes and underscores both match).
    pub crate_name: Option<&'a str>,
    /// Which tier an eviction touches: the store, the views, or both.
    pub tier: Tier,
    /// Also allow evicting current-generation units.
    pub force: bool,
    /// Actually delete (without this it is a dry run).
    pub delete: bool,
}

/// What an eviction touches. The vault ("store") is the user's storage —
/// trimming it never breaks a build, only the next one compiles more.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum Tier {
    /// The store and every known view (default).
    All,
    /// The vault only — views keep their hard links.
    Store,
    /// The shared build dir and every job dir — the vault is untouched.
    View,
}

impl Tier {
    fn touches_store(self) -> bool {
        matches!(self, Tier::All | Tier::Store)
    }

    fn touches_view(self) -> bool {
        matches!(self, Tier::All | Tier::View)
    }
}

/// Default max age of a per-job working dir before `--work-dirs` collects it.
pub const DEFAULT_WORK_MAX_AGE: &str = "7d";

/// `epicenter evict [unit] [--stale|--rustc-key|--husks|--older-than|--work-dirs|--work <view>] [--tier <t>] [--force] [--yes]`
pub fn run(build_root: &Path, opts: &Options<'_>) -> Result<()> {
    // An empty selector is a wildcard (`contains("")`) — never delete by it.
    for (name, value) in [
        ("--unit", opts.unit),
        ("--rustc-key", opts.rustc_key),
        ("--crate", opts.crate_name),
    ] {
        if value.is_some_and(|value| value.is_empty()) {
            bail!("{name} must not be empty — use an explicit selector or a dry run");
        }
    }
    if let Some(target) = opts.work_dir {
        if opts.work_dirs || opts.work.is_some() {
            bail!(
                "one operation at a time — --work-dir removes ONE job dir:\n\
                 --work-dirs collects the whole tier by age, --work unlinks units inside a view"
            );
        }
        if opts.unit.is_some()
            || opts.crate_name.is_some()
            || opts.stale
            || opts.rustc_key.is_some()
            || opts.husks
            || opts.older_than.is_some()
        {
            bail!(
                "--work-dir removes exactly the named job dir and takes no selectors:\n\
                 e.g. `epicenter evict --work-dir ci-1` (dry run) then --yes"
            );
        }
        if opts.tier != Tier::All {
            bail!("--tier does not apply to --work-dir — a job dir is one view, removed whole");
        }
        return evict_work_dir(build_root, target, opts.delete);
    }

    if opts.work_dirs {
        if opts.work.is_some()
            || opts.unit.is_some()
            || opts.stale
            || opts.rustc_key.is_some()
            || opts.husks
        {
            bail!(
                "--work-dirs collects whole stale job dirs — it takes no unit selectors:\n\
                 run `epicenter evict --work-dirs` on its own, or drop --work-dirs"
            );
        }
        evict_work_dirs(build_root, opts.older_than, opts.delete)?;
        return Ok(());
    }

    if let Some(work) = opts.work {
        if opts.stale || opts.rustc_key.is_some() || opts.husks || opts.older_than.is_some() {
            bail!(
                "--work unlinks units from ONE view — only --crate / --unit apply:\n\
                 for bulk view work use `--tier view`, for whole job dirs use --work-dirs"
            );
        }
        if opts.unit.is_none() && opts.crate_name.is_none() {
            bail!(
                "nothing selected — --work needs --crate <name> or --unit <hash>:\n\
                 e.g. `epicenter evict --work ci-1 --crate serde` (dry run unless --yes)"
            );
        }
        if opts.tier != Tier::All {
            bail!(
                "--tier does not apply to --work — that operation is always view-only:\n\
                 the vault is never touched by it"
            );
        }
        return evict_from_view(build_root, opts, work);
    }

    if opts.unit.is_none()
        && !opts.stale
        && opts.rustc_key.is_none()
        && !opts.husks
        && opts.older_than.is_none()
    {
        bail!(
            "nothing selected — pass one selector:\n\
             --unit <hash|package>   a specific unit\n\
             --stale                 every unit from an older toolchain\n\
             --rustc-key <hash>      units matching a toolchain identity\n\
             --husks                 units whose artifacts are already gone\n\
             --older-than <age>      units untouched for longer (e.g. 90d)\n\
             --work-dirs             stale per-job working dirs (default 7d)\n\
             dry run unless you pass --yes — full help: `epicenter evict --help`"
        );
    }

    let store_root = crate::vault::store_root(build_root);
    let active = crate::vault::parity_key().ok();
    let age_limit = opts
        .older_than
        .map(parse_duration)
        .transpose()
        .context("invalid value for --older-than")?;
    let now = SystemTime::now();

    let mut candidates: BTreeMap<String, Candidate> = BTreeMap::new();
    let mut view_labels: Vec<String> = Vec::new();
    let mut skipped_views: Vec<String> = Vec::new();

    // Store pass: the vault is the user's storage — trimming it costs compile
    // time at most, so the selectors apply here exactly as they do to views.
    if opts.tier.touches_store() {
        for stored in crate::vault::stored_units(&store_root)? {
            if !store_selectors_hit(opts, &stored, active.as_deref(), age_limit, now) {
                continue;
            }
            let mut files = Vec::new();
            collect_tree_files(&stored.dir, &mut files);
            let current = active.as_deref() == Some(stored.generation.as_str());
            let candidate = candidate_for(&mut candidates, &stored.entry.name);
            candidate.kind = fingerprint::classify_kind(&stored.entry.memo)
                .label()
                .to_string();
            candidate.current |= current;
            candidate.newest = max_time(candidate.newest, stamp_time(stored.entry.stamp));
            push_place(
                candidate,
                Place::Store {
                    generation: stored.generation.clone(),
                    files,
                },
            );
        }
    }

    // View passes: the shared build dir plus every job dir, minus locked ones.
    if opts.tier.touches_view() {
        for (label, root) in known_views(build_root) {
            if crate::exec::dir_locked(&root) {
                skipped_views.push(label);
                continue;
            }
            let rows = who::collect(&root).with_context(|| format!("reading view {label}"))?;
            let current = active_cohort(&rows);
            for (unit, arts) in rows {
                if !view_selectors_hit(opts, &unit, &arts, &current, age_limit, now) {
                    continue;
                }
                if !view_labels.contains(&label) {
                    view_labels.push(label.clone());
                }
                let files = view_unit_files(&root, &unit, &arts);
                let artifacts: Vec<PathBuf> =
                    arts.paths.iter().map(|(path, _, _)| path.clone()).collect();
                let candidate = candidate_for(&mut candidates, &unit.dir_name);
                candidate.kind = unit.kind.label().to_string();
                candidate.current |= !current.is_empty() && who::rustc_of(&unit) == current;
                candidate.newest = max_time(candidate.newest, arts.newest);
                push_place(
                    candidate,
                    Place::View {
                        label: label.clone(),
                        unit_dir: root
                            .join(&unit.profile)
                            .join(".fingerprint")
                            .join(&unit.dir_name),
                        anchors: fingerprint::build_anchor_dirs(&root, &unit),
                        artifacts,
                        files,
                    },
                );
            }
        }
    }

    if opts.delete {
        println!("{}", util::heading("epicenter evict — removing"));
    } else {
        println!(
            "{}",
            util::heading("epicenter evict — DRY RUN (nothing is removed; pass --yes to delete)")
        );
    }

    let mut protected = 0usize;
    let mut chosen: Vec<Candidate> = Vec::new();
    for (_, candidate) in candidates {
        // Current-generation units are protected whenever a view is involved;
        // `--tier store` is explicitly the vault-trimming mode.
        if candidate.current && !opts.force && opts.tier.touches_view() {
            protected += 1;
            continue;
        }
        chosen.push(candidate);
    }
    chosen.sort_by_key(|candidate| std::cmp::Reverse(candidate.bytes));

    if chosen.is_empty() {
        println!("  nothing matches the given selectors");
        if protected > 0 {
            println!(
                "  {} {protected} current-generation unit(s) matched but are protected — use --force to include them",
                util::warn(util::GLYPH_WARN)
            );
        }
        return Ok(());
    }

    for candidate in &chosen {
        let mut labels: Vec<&str> = candidate.places.iter().map(Place::label).collect();
        labels.sort_unstable();
        labels.dedup();
        println!(
            "  {} {:<38} {:<9} {:>9}  {:>10} old  {}{}",
            util::green(util::GLYPH_PASS),
            candidate.name,
            candidate.kind,
            human_bytes(candidate.bytes),
            candidate
                .newest
                .map(human_age)
                .unwrap_or_else(|| "?".into()),
            labels.join(" + "),
            if candidate.current && opts.force {
                util::dim("  (current — forced)")
            } else if candidate.current {
                util::dim("  (current)")
            } else {
                String::new()
            },
        );
    }

    let all_files: Vec<PathBuf> = chosen
        .iter()
        .flat_map(|candidate| candidate.places.iter())
        .flat_map(|place| place.files().iter().cloned())
        .collect();
    let (nominal, freed, released, count) = account_files(&all_files);
    let places: usize = chosen.iter().map(|candidate| candidate.places.len()).sum();

    println!();
    println!(
        "  {} {} unit(s) · {} place(s) · {} file(s) · {} nominal",
        util::dim("selection:"),
        chosen.len(),
        places,
        count,
        util::green(&human_bytes(nominal))
    );
    println!(
        "  {} {} actually freed · {} links released",
        util::dim("result:   "),
        human_bytes(freed),
        human_bytes(released)
    );
    println!(
        "  {} {}",
        util::dim("tier:     "),
        tier_summary(opts.tier, &view_labels)
    );
    if protected > 0 {
        println!(
            "  {} {protected} current-generation unit(s) skipped — use --force to include them",
            util::warn(util::GLYPH_WARN)
        );
    }
    for label in &skipped_views {
        println!(
            "  {} view {label} is in use by a live build — skipped",
            util::warn(util::GLYPH_WARN)
        );
    }
    if !opts.delete {
        println!(
            "  {}",
            util::dim("run again with -y or --yes to remove them")
        );
        return Ok(());
    }

    // ── delete ──────────────────────────────────────────────────────────────
    let mut errors = 0u64;

    let mut per_generation: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for candidate in &chosen {
        for place in &candidate.places {
            if let Place::Store { generation, .. } = place {
                per_generation
                    .entry(generation.clone())
                    .or_default()
                    .push(candidate.name.clone());
            }
        }
    }
    let mut store_dropped = 0usize;
    for (generation, names) in &per_generation {
        match crate::vault::drop_units(&store_root, generation, names) {
            Ok(dropped) => store_dropped += dropped,
            Err(err) => {
                errors += 1;
                eprintln!("could not prune the vault: {err}");
            }
        }
    }

    let mut view_counts: BTreeMap<String, usize> = BTreeMap::new();
    for candidate in &chosen {
        for place in &candidate.places {
            if let Place::View {
                label,
                unit_dir,
                anchors,
                artifacts,
                ..
            } = place
            {
                remove_dir(unit_dir, &mut errors);
                for anchor in anchors {
                    if anchor.is_dir() {
                        remove_dir(anchor, &mut errors);
                    }
                }
                for artifact in artifacts {
                    remove_file(artifact, &mut errors);
                }
                *view_counts.entry(label.clone()).or_default() += 1;
            }
        }
    }

    println!();
    println!(
        "  {} {} unit(s) evicted · {} actually freed · {} links released",
        util::green(util::GLYPH_PASS),
        chosen.len(),
        human_bytes(freed),
        human_bytes(released)
    );
    if store_dropped > 0 {
        println!(
            "  {:<11} {} unit(s) dropped · manifest updated",
            util::dim("store"),
            store_dropped
        );
    }
    for (label, count) in &view_counts {
        println!("  {:<11} {} unit(s) unlinked", util::dim(label), count);
    }
    if errors > 0 {
        println!(
            "  {} {} (see messages above)",
            util::fail(util::GLYPH_FAIL),
            errors
        );
    }
    println!(
        "  {}",
        util::dim("`epicenter vault` shows what the store holds now")
    );
    Ok(())
}

/// What one `--work-dirs` pass found and did.
#[derive(Debug, Default)]
pub struct WorkReport {
    /// Job dirs seen under `work/`.
    pub scanned: usize,
    /// Collectible (old enough, not locked).
    pub eligible: usize,
    /// Younger than the age limit.
    pub young: usize,
    /// A live build holds the dir's lock.
    pub in_use: usize,
    /// Directories actually removed.
    pub removed: usize,
    /// Sum of sizes of the collected dirs (each file counts once per path).
    pub nominal_bytes: u64,
    /// Bytes whose last link lived in those dirs — actually freed.
    pub unique_bytes: u64,
    /// Bytes still linked from the store or another view.
    pub shared_bytes: u64,
    pub errors: u64,
}

/// One `work/<job-id>/` view.
struct WorkDir {
    name: String,
    path: PathBuf,
    newest: Option<SystemTime>,
    locked: bool,
    old_enough: bool,
    bytes: u64,
    unique: u64,
    shared: u64,
    files: u64,
}

/// `epicenter evict --work-dir <id|path>` — remove ONE job dir now. An
/// explicit choice is reason enough: the 7-day (or `--older-than`) window is a
/// GC policy for bulk collection, not a gate on deliberate removals. The same
/// guards apply: a dir a live build has locked is refused, and the shared
/// build dir can never be a target.
fn evict_work_dir(build_root: &Path, target: &str, delete: bool) -> Result<()> {
    let work_root = crate::exec::work_root(build_root);
    let path = resolve_work_dir(&work_root, target)?;
    let name = path
        .file_name()
        .map(|name| name.to_string_lossy().into_owned())
        .unwrap_or_default();
    let job_lock =
        match crate::util::FileLock::try_acquire(&crate::exec::job_lock_path(&work_root, &name))? {
            Some(lock) => lock,
            None => bail!(
                "job dir {} is in use by an epicenter process — remove it when it finishes",
                path.display()
            ),
        };
    if crate::exec::dir_locked(&path) {
        bail!(
            "job dir {} is in use by a live build — remove it when it finishes",
            path.display()
        );
    }
    let newest = dir_newest(&path);
    let (bytes, unique, shared, files) = dir_accounting(&path);

    if delete {
        println!(
            "{}",
            util::heading(&format!("epicenter evict --work-dir {name} — removing"))
        );
    } else {
        println!(
            "{}",
            util::heading(&format!(
                "epicenter evict --work-dir {name} — DRY RUN (nothing is removed; pass --yes)"
            ))
        );
    }
    println!(
        "  {} {}",
        util::dim(&format!("{:<11}", "job dir")),
        path.display()
    );
    println!(
        "  {} {:<40} {:>9} · {:>4} file(s)  {:>10} old  {}",
        util::green(util::GLYPH_PASS),
        name,
        human_bytes(bytes),
        files,
        newest.map(human_age).unwrap_or_else(|| "?".into()),
        util::dim("explicitly selected — age policy not applied")
    );
    println!();
    println!(
        "  {} {} nominal — {} actually freed · {} links released",
        util::dim("result:   "),
        human_bytes(bytes),
        human_bytes(unique),
        human_bytes(shared)
    );
    if !delete {
        println!("  {}", util::dim("run again with -y or --yes to remove it"));
        return Ok(());
    }
    // Rename out of the way first: a hidden tombstone is not a job dir any
    // later GC/exec can guess, and a build that races the check fails rather
    // than writing into a directory being deleted.
    let tombstone = work_root.join(format!(".{name}.deleting-{}", std::process::id()));
    if fs::symlink_metadata(&tombstone).is_ok() {
        let _ = crate::util::force_remove_dir(&tombstone);
    }
    fs::rename(&path, &tombstone).with_context(|| format!("isolating {}", path.display()))?;
    if let Err(err) = fs::remove_dir_all(&tombstone) {
        let _ = fs::rename(&tombstone, &path);
        drop(job_lock);
        return Err(err).with_context(|| format!("removing {}", path.display()));
    }
    drop(job_lock);
    println!();
    println!(
        "  {} {name} removed · {} actually freed · {} links released",
        util::green(util::GLYPH_PASS),
        human_bytes(unique),
        human_bytes(shared)
    );
    println!(
        "  {}",
        util::dim("the vault is untouched — a job dir is a view")
    );
    Ok(())
}

/// `--work-dir` targets: a job id (plain or `work/<id>`), or a path that is a
/// direct child of the work root. `build` and anything outside are refused.
fn resolve_work_dir(work_root: &Path, target: &str) -> Result<PathBuf> {
    if target == "build" {
        bail!(
            "the shared build dir is never removed — it is the core asset:\n\
             to work inside it use `epicenter evict --work build --crate <name>`"
        );
    }
    let candidate = work_root.join(target.trim_start_matches("work/"));
    let candidate = if candidate.is_dir() {
        candidate
    } else {
        PathBuf::from(target)
    };
    if !candidate.is_dir() {
        bail!("no job dir named {target:?} — `epicenter vault` shows the work tier");
    }
    let inside = candidate
        .canonicalize()
        .ok()
        .zip(work_root.canonicalize().ok())
        .map(|(child, root)| child.parent() == Some(root.as_path()))
        .unwrap_or(false);
    if !inside {
        bail!(
            "{} is not a job dir under {} — --work-dir removes the work tier only",
            candidate.display(),
            work_root.display()
        );
    }
    Ok(candidate)
}

/// The working-dir tier: `work/<job-id>/` views are disposable, so a job dir
/// whose newest activity is older than the limit is collectible. A directory
/// that a live build has locked is never touched, and the vault is never
/// touched at all — removing a view only unlinks hard links.
fn evict_work_dirs(
    build_root: &Path,
    older_than: Option<&str>,
    delete: bool,
) -> Result<WorkReport> {
    let work_root = crate::exec::work_root(build_root);
    let age_text = older_than.unwrap_or(DEFAULT_WORK_MAX_AGE);
    let limit = parse_duration(age_text).context("invalid value for --older-than")?;

    if delete {
        println!(
            "{}",
            util::heading("epicenter evict --work-dirs — removing")
        );
    } else {
        println!(
            "{}",
            util::heading(
                "epicenter evict --work-dirs — DRY RUN (nothing is removed; pass --yes to delete)"
            )
        );
    }
    let mut report = WorkReport::default();
    if !work_root.is_dir() {
        println!("  no working dirs at {}", work_root.display());
        return Ok(report);
    }

    let now = SystemTime::now();
    let mut rows: Vec<WorkDir> = Vec::new();
    for entry in
        fs::read_dir(&work_root).with_context(|| format!("reading {}", work_root.display()))?
    {
        let entry = entry?;
        if !entry.file_type()?.is_dir() {
            continue;
        }
        let name = entry.file_name().to_string_lossy().into_owned();
        if name.starts_with('.') {
            continue;
        }
        let path = entry.path();
        let newest = dir_newest(&path);
        let locked = crate::exec::dir_locked(&path);
        let (bytes, unique, shared, files) = dir_accounting(&path);
        let old_enough = newest
            .map(|m| now.duration_since(m).map_or(true, |age| age > limit))
            .unwrap_or(true);
        rows.push(WorkDir {
            name,
            path,
            newest,
            locked,
            old_enough,
            bytes,
            unique,
            shared,
            files,
        });
    }
    rows.sort_by(|a, b| a.name.cmp(&b.name));
    if rows.is_empty() {
        println!(
            "  no working dirs at {} — nothing to collect",
            work_root.display()
        );
        return Ok(report);
    }

    for row in &rows {
        report.scanned += 1;
        let (icon, note) = if row.locked {
            report.in_use += 1;
            (
                util::warn(util::GLYPH_WARN),
                util::warn("in use by a live build — never collected").to_string(),
            )
        } else if row.old_enough {
            report.eligible += 1;
            (
                util::green(util::GLYPH_PASS),
                util::green("collectible").to_string(),
            )
        } else {
            report.young += 1;
            (
                util::info(util::GLYPH_INFO),
                util::dim(&format!("younger than {age_text}")).to_string(),
            )
        };
        println!(
            "  {icon} {:<40} {:>9} · {:>4} file(s)  {:>10} old  {note}",
            row.name,
            human_bytes(row.bytes),
            row.files,
            row.newest.map(human_age).unwrap_or_else(|| "?".into()),
        );
    }

    for row in rows.iter().filter(|row| row.old_enough && !row.locked) {
        report.nominal_bytes += row.bytes;
        report.unique_bytes += row.unique;
        report.shared_bytes += row.shared;
    }
    println!();
    if report.eligible == 0 {
        println!(
            "  {} nothing older than {age_text} to collect",
            util::dim("selection:")
        );
    } else {
        println!(
            "  {} {} dir(s) · {} nominal · {} unique to the jobs (freed), {} still shared with the store",
            util::dim("selection:"),
            report.eligible,
            util::green(&human_bytes(report.nominal_bytes)),
            util::green(&human_bytes(report.unique_bytes)),
            util::dim(&human_bytes(report.shared_bytes)),
        );
    }
    if report.in_use > 0 {
        println!(
            "  {} {} dir(s) in use by a live build — left alone",
            util::warn(util::GLYPH_WARN),
            report.in_use
        );
    }
    if report.young > 0 {
        println!(
            "  {} {} dir(s) younger than {age_text} — kept",
            util::info(util::GLYPH_INFO),
            report.young
        );
    }
    if report.eligible == 0 {
        return Ok(report);
    }
    if !delete {
        println!(
            "  {}",
            util::dim("run again with -y or --yes to remove the selected dirs")
        );
        return Ok(report);
    }

    let mut freed_unique = 0u64;
    let mut freed_shared = 0u64;
    for row in rows.iter().filter(|row| row.old_enough && !row.locked) {
        let _job_lock = match crate::util::FileLock::try_acquire(&crate::exec::job_lock_path(
            &work_root, &row.name,
        ))? {
            Some(lock) => lock,
            None => {
                report.in_use += 1;
                continue;
            }
        };
        if crate::exec::dir_locked(&row.path) {
            report.in_use += 1;
            continue;
        }
        match fs::remove_dir_all(&row.path) {
            Ok(()) => {
                report.removed += 1;
                freed_unique += row.unique;
                freed_shared += row.shared;
            }
            Err(e) => {
                report.errors += 1;
                eprintln!("could not remove {}: {e}", row.path.display());
            }
        }
    }
    println!();
    println!(
        "  {} {} dir(s) evicted · {} actually freed · {} link(s) released (other links, e.g. the vault, still hold those bytes)",
        util::green(util::GLYPH_PASS),
        report.removed,
        human_bytes(freed_unique),
        util::dim(&human_bytes(freed_shared)),
    );
    if report.errors > 0 {
        println!(
            "  {} {} (see messages above)",
            util::fail(util::GLYPH_FAIL),
            report.errors
        );
    }
    println!(
        "  {}",
        util::dim("the vault is untouched — `epicenter vault` shows what remains")
    );
    Ok(report)
}

/// Newest file mtime in the tree — a job is as fresh as its last file write
/// (directory mtimes only move when entries are added or removed).
fn dir_newest(dir: &Path) -> Option<SystemTime> {
    let mut newest = None;
    for entry in WalkDir::new(dir).min_depth(1).into_iter().flatten() {
        if !entry.file_type().is_file() {
            continue;
        }
        if let Some(modified) = entry.metadata().ok().and_then(|meta| meta.modified().ok()) {
            if newest.map_or(true, |current| modified > current) {
                newest = Some(modified);
            }
        }
    }
    newest
}

/// (nominal bytes, unique bytes, shared bytes, file count) of one view.
fn dir_accounting(dir: &Path) -> (u64, u64, u64, u64) {
    let mut bytes = 0u64;
    let mut unique = 0u64;
    let mut shared = 0u64;
    let mut files = 0u64;
    for entry in WalkDir::new(dir).min_depth(1).into_iter().flatten() {
        if !entry.file_type().is_file() {
            continue;
        }
        let Ok(meta) = entry.metadata() else { continue };
        let size = meta.len();
        bytes += size;
        files += 1;
        if crate::vault::link_count(&meta) > 1 {
            shared += size;
        } else {
            unique += size;
        }
    }
    (bytes, unique, shared, files)
}

/// Resolve `--work`: `build` = the shared view; a job id (with or without
/// the `work/` prefix) = that job dir; anything else = a directory path.
fn resolve_view(build_root: &Path, work: &str) -> Result<PathBuf> {
    if work == "build" {
        return Ok(build_root.to_path_buf());
    }
    let work_root = crate::exec::work_root(build_root);
    let job = work_root.join(work.trim_start_matches("work/"));
    if fs::symlink_metadata(&job)
        .map(|meta| meta.is_dir() && !meta.file_type().is_symlink())
        .unwrap_or(false)
    {
        return Ok(job);
    }
    let path = PathBuf::from(work);
    if fs::symlink_metadata(&path)
        .map(|meta| meta.is_dir() && !meta.file_type().is_symlink())
        .unwrap_or(false)
    {
        let canonical = path.canonicalize()?;
        let work_root_canonical = work_root.canonicalize().ok();
        let build_canonical = build_root.canonicalize().ok();
        let allowed = work_root_canonical
            .iter()
            .chain(build_canonical.iter())
            .any(|root| canonical.starts_with(root));
        if allowed {
            return Ok(path);
        }
        bail!(
            "view {} is outside {} and {} — refusing to delete outside the cache",
            path.display(),
            work_root.display(),
            build_root.display()
        );
    }
    bail!(
        "no view named {work:?} — expected `build`, a job id under {}, or a directory under it",
        work_root.display()
    );
}

/// `--crate` selector: package name; dashes and underscores are interchangeable.
fn crate_matches(unit: &Unit, name: &str) -> bool {
    unit.package == name || unit.package.replace('-', "_") == name.replace('-', "_")
}

/// Selector for one view's unit (`--work` mode).
fn surgery_unit_matches(unit: &Unit, opts: &Options<'_>) -> bool {
    let mut hit = false;
    if let Some(query) = opts.unit {
        hit |= who::unit_matches(unit, query);
    }
    if let Some(name) = opts.crate_name {
        hit |= crate_matches(unit, name);
    }
    hit
}

/// Files a view-unit removal would unlink (for honest accounting).
fn view_unit_files(root: &Path, unit: &Unit, arts: &ArtifactInfo) -> Vec<PathBuf> {
    let mut files = Vec::new();
    collect_tree_files(
        &root
            .join(&unit.profile)
            .join(".fingerprint")
            .join(&unit.dir_name),
        &mut files,
    );
    // `arts.paths` already covers the `build/<anchor>/` files.
    for (path, _, _) in &arts.paths {
        files.push(path.clone());
    }
    files
}

fn collect_tree_files(dir: &Path, out: &mut Vec<PathBuf>) {
    for entry in WalkDir::new(dir).min_depth(1).into_iter().flatten() {
        if entry.file_type().is_file() {
            out.push(entry.path().to_path_buf());
        }
    }
}

/// Remove one unit from one view: fingerprint dir, build anchors, artifacts.
fn remove_view_unit(root: &Path, unit: &Unit, arts: &ArtifactInfo, errors: &mut u64) {
    let fingerprint_dir = root
        .join(&unit.profile)
        .join(".fingerprint")
        .join(&unit.dir_name);
    remove_dir(&fingerprint_dir, errors);
    for anchor in fingerprint::build_anchor_dirs(root, unit) {
        if anchor.is_dir() {
            remove_dir(&anchor, errors);
        }
    }
    for (path, _, _) in &arts.paths {
        remove_file(path, errors);
    }
}

/// (nominal, actually freed, released, file count) of a set of paths about to
/// be unlinked. A file frees its bytes only when every one of its links is in
/// the set; inodes are counted once, so hard links never inflate the total.
fn account_files(files: &[PathBuf]) -> (u64, u64, u64, u64) {
    #[cfg(unix)]
    {
        use std::collections::{HashMap, HashSet};
        use std::os::unix::fs::MetadataExt;

        let mut per_inode: HashMap<(u64, u64), (u64, u64, u64)> = HashMap::new();
        let mut seen: HashSet<&Path> = HashSet::new();
        let mut count = 0u64;
        for path in files {
            if !seen.insert(path.as_path()) {
                continue;
            }
            let Ok(meta) = fs::metadata(path) else {
                continue;
            };
            count += 1;
            let entry =
                per_inode
                    .entry((meta.dev(), meta.ino()))
                    .or_insert((meta.len(), meta.nlink(), 0));
            entry.2 += 1;
        }
        let mut nominal = 0u64;
        let mut freed = 0u64;
        for (size, links, planned) in per_inode.values() {
            nominal += size;
            if planned >= links {
                freed += size;
            }
        }
        (nominal, freed, nominal - freed, count)
    }
    #[cfg(not(unix))]
    {
        // No link counts here: report the paths as freed (single-link ceiling).
        let mut seen: std::collections::HashSet<&Path> = std::collections::HashSet::new();
        let mut nominal = 0u64;
        let mut count = 0u64;
        for path in files {
            if !seen.insert(path.as_path()) {
                continue;
            }
            if let Ok(meta) = fs::metadata(path) {
                nominal += meta.len();
                count += 1;
            }
        }
        (nominal, nominal, 0, count)
    }
}

/// One unit that matches the selectors, with every place it would be removed
/// from (a vault generation and/or the views that hold a hard link).
struct Candidate {
    name: String,
    kind: String,
    bytes: u64,
    newest: Option<SystemTime>,
    current: bool,
    places: Vec<Place>,
}

/// One place a candidate lives.
enum Place {
    Store {
        generation: String,
        files: Vec<PathBuf>,
    },
    View {
        label: String,
        unit_dir: PathBuf,
        anchors: Vec<PathBuf>,
        artifacts: Vec<PathBuf>,
        files: Vec<PathBuf>,
    },
}

impl Place {
    fn label(&self) -> &str {
        match self {
            Place::Store { .. } => "store",
            Place::View { label, .. } => label,
        }
    }

    fn files(&self) -> &[PathBuf] {
        match self {
            Place::Store { files, .. } | Place::View { files, .. } => files,
        }
    }
}

fn candidate_for<'a>(
    candidates: &'a mut BTreeMap<String, Candidate>,
    name: &str,
) -> &'a mut Candidate {
    candidates
        .entry(name.to_string())
        .or_insert_with(|| Candidate {
            name: name.to_string(),
            kind: "?".to_string(),
            bytes: 0,
            newest: None,
            current: false,
            places: Vec::new(),
        })
}

fn push_place(candidate: &mut Candidate, place: Place) {
    candidate.bytes += place
        .files()
        .iter()
        .filter_map(|path| fs::metadata(path).ok())
        .map(|meta| meta.len())
        .sum::<u64>();
    candidate.places.push(place);
}

fn stamp_time(stamp: u64) -> Option<SystemTime> {
    (stamp > 0).then(|| std::time::UNIX_EPOCH + Duration::from_secs(stamp))
}

fn max_time(left: Option<SystemTime>, right: Option<SystemTime>) -> Option<SystemTime> {
    match (left, right) {
        (Some(a), Some(b)) => Some(a.max(b)),
        (left, None) => left,
        (None, right) => right,
    }
}

/// Every view epicenter knows about: the shared build dir, then each job dir.
fn known_views(build_root: &Path) -> Vec<(String, PathBuf)> {
    let mut views = vec![("build".to_string(), build_root.to_path_buf())];
    let work_root = crate::exec::work_root(build_root);
    if let Ok(entries) = fs::read_dir(&work_root) {
        for entry in entries.flatten() {
            if !entry.file_type().map(|kind| kind.is_dir()).unwrap_or(false) {
                continue;
            }
            let name = entry.file_name().to_string_lossy().into_owned();
            if name.starts_with('.') {
                continue;
            }
            views.push((format!("work/{name}"), entry.path()));
        }
    }
    views
}

/// The largest rustc cohort of a view marks the generation its working code
/// belongs to (the heuristic `who`/`evict` have always used).
fn active_cohort(rows: &[(Unit, ArtifactInfo)]) -> String {
    who::rustc_cohorts(rows)
        .into_iter()
        .max_by_key(|(_, count)| *count)
        .map(|(id, _)| id)
        .unwrap_or_default()
}

/// Selector for one stored unit. `--stale` means "not the installed
/// toolchain's generation"; the other selectors mirror the view semantics.
fn store_selectors_hit(
    opts: &Options<'_>,
    stored: &crate::vault::StoredUnit,
    active: Option<&str>,
    limit: Option<Duration>,
    now: SystemTime,
) -> bool {
    let mut hit = false;
    if let Some(query) = opts.unit {
        hit |= stored.entry.name.contains(query);
    }
    if opts.stale {
        hit |= active.map(|key| stored.generation != key).unwrap_or(false);
    }
    if let Some(key) = opts.rustc_key {
        hit |= crate::vault::stored_unit_rustc(stored)
            .map(|id| id.starts_with(key))
            .unwrap_or(false);
    }
    if let Some(limit) = limit {
        hit |= stamp_time(stored.entry.stamp)
            .map(|stamp| now.duration_since(stamp).map_or(true, |age| age > limit))
            .unwrap_or(false);
    }
    if opts.husks {
        hit |= stored
            .entry
            .files
            .iter()
            .any(|file| !stored.dir.join(&file.path).is_file());
    }
    hit
}

/// Selector for one view's unit.
fn view_selectors_hit(
    opts: &Options<'_>,
    unit: &Unit,
    arts: &ArtifactInfo,
    current: &str,
    limit: Option<Duration>,
    now: SystemTime,
) -> bool {
    let mut hit = false;
    if let Some(query) = opts.unit {
        hit |= who::unit_matches(unit, query);
    }
    if opts.stale {
        hit |= !current.is_empty() && who::rustc_of(unit) != current;
    }
    if let Some(key) = opts.rustc_key {
        hit |= who::rustc_of(unit).starts_with(key);
    }
    if opts.husks {
        hit |= arts.paths.is_empty();
    }
    if let Some(limit) = limit {
        hit |= arts
            .newest
            .map(|newest| now.duration_since(newest).map_or(true, |age| age > limit))
            .unwrap_or(false);
    }
    hit
}

/// The `tier:` line of the selection report.
fn tier_summary(tier: Tier, views: &[String]) -> String {
    match tier {
        Tier::Store => "store — the vault only (views keep their links)".to_string(),
        Tier::View => format!("view — {} (the vault is untouched)", view_list(views)),
        Tier::All => format!("all — the vault + {}", view_list(views)),
    }
}

fn view_list(views: &[String]) -> String {
    if views.is_empty() {
        return "no views".to_string();
    }
    let mut text = views.iter().take(4).cloned().collect::<Vec<_>>().join(", ");
    if views.len() > 4 {
        text.push_str(&format!(", … (+{} more)", views.len() - 4));
    }
    text
}

/// `epicenter evict --work <view> --crate/--unit …` — unlink matching units
/// from ONE view. A freshness operation: the vault is never touched, and the
/// honest numbers say what was freed (units this view owned alone) versus
/// what was merely released (units the vault still holds).
fn evict_from_view(build_root: &Path, opts: &Options<'_>, work: &str) -> Result<()> {
    let root = resolve_view(build_root, work)?;
    if crate::exec::dir_locked(&root) {
        bail!(
            "view {} is in use by a live build — unlink when it finishes",
            root.display()
        );
    }
    let vaulted: std::collections::BTreeSet<String> =
        crate::vault::stored_units(&crate::vault::store_root(build_root))?
            .into_iter()
            .map(|unit| unit.entry.name)
            .collect();

    let rows = who::collect(&root).context("reading the view")?;
    let chosen: Vec<&(Unit, ArtifactInfo)> = rows
        .iter()
        .filter(|(unit, _)| surgery_unit_matches(unit, opts))
        .collect();

    if opts.delete {
        println!(
            "{}",
            util::heading(&format!("epicenter evict --work {work} — unlinking"))
        );
    } else {
        println!(
            "{}",
            util::heading(&format!(
                "epicenter evict --work {work} — DRY RUN (nothing is unlinked; pass --yes)"
            ))
        );
    }
    println!(
        "  {} {}",
        util::dim(&format!("{:<11}", "view")),
        root.display()
    );
    if chosen.is_empty() {
        println!("  nothing in this view matches — check `epicenter who --unit <q>`");
        return Ok(());
    }

    let mut files = Vec::new();
    for (unit, arts) in &chosen {
        files.extend(view_unit_files(&root, unit, arts));
    }
    let (nominal, freed, released, count) = account_files(&files);

    for (unit, arts) in &chosen {
        println!(
            "  {} {:<40} {:<9} {:>9}  {}",
            util::green(util::GLYPH_PASS),
            unit.dir_name,
            unit.kind.label(),
            human_bytes(arts.bytes),
            if vaulted.contains(&unit.dir_name) {
                util::dim("in the vault — Cargo will rebuild it here")
            } else {
                util::warn("only in this view — its bytes go")
            }
        );
    }
    println!();
    println!(
        "  {} {} unit(s) · {} file(s) · {} nominal",
        util::dim("selection:"),
        chosen.len(),
        count,
        util::green(&human_bytes(nominal))
    );
    println!(
        "  {} {} actually freed · {} links released (still held elsewhere, e.g. the vault)",
        util::dim("result:   "),
        human_bytes(freed),
        human_bytes(released)
    );
    if !opts.delete {
        println!(
            "  {}",
            util::dim("run again with -y or --yes to unlink these units from this view")
        );
        return Ok(());
    }

    let mut errors = 0u64;
    for (unit, arts) in &chosen {
        remove_view_unit(&root, unit, arts, &mut errors);
    }
    println!();
    println!(
        "  {} {} unit(s) unlinked from {work} · {} actually freed · {} links released",
        util::green(util::GLYPH_PASS),
        chosen.len(),
        human_bytes(freed),
        human_bytes(released)
    );
    if errors > 0 {
        println!(
            "  {} {} (see messages above)",
            util::fail(util::GLYPH_FAIL),
            errors
        );
    }
    println!(
        "  {}",
        util::dim("Cargo will rebuild these units here; the vault is untouched")
    );
    Ok(())
}

fn remove_dir(path: &Path, errors: &mut u64) {
    if let Err(e) = fs::remove_dir_all(path) {
        if e.kind() != ErrorKind::NotFound {
            *errors += 1;
            eprintln!("could not remove {}: {e}", path.display());
        }
    }
}

fn remove_file(path: &Path, errors: &mut u64) {
    if let Err(e) = fs::remove_file(path) {
        if e.kind() != ErrorKind::NotFound {
            *errors += 1;
            eprintln!("could not remove {}: {e}", path.display());
        }
    }
}

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

    /// One unit (`pkg-1122334455667788`) + its rlib in a build-dir shape.
    fn write_synthetic_unit(root: &Path) -> Result<()> {
        let unit_dir = root.join("debug/.fingerprint/pkg-1122334455667788");
        fs::create_dir_all(&unit_dir)?;
        fs::write(unit_dir.join("lib-pkg"), "0011223344556677")?;
        fs::write(unit_dir.join("dep-lib-pkg"), [0u8; 8])?;
        fs::create_dir_all(root.join("debug/deps"))?;
        fs::write(
            root.join("debug/deps/libpkg-1122334455667788.rlib"),
            [0u8; 16],
        )?;
        Ok(())
    }

    /// `{home}/build` with one unit — hermetic, so the store sits in
    /// `{home}/store` and never touches anything outside the tempdir.
    fn synthetic_home() -> Result<(tempfile::TempDir, PathBuf)> {
        let home = tempfile::tempdir()?;
        let build = home.path().join("build");
        fs::create_dir_all(&build)?;
        write_synthetic_unit(&build)?;
        Ok((home, build))
    }

    fn no_selectors() -> Options<'static> {
        Options {
            unit: None,
            stale: false,
            rustc_key: None,
            husks: false,
            older_than: None,
            work_dirs: false,
            work_dir: None,
            work: None,
            crate_name: None,
            tier: Tier::All,
            force: false,
            delete: false,
        }
    }

    #[test]
    fn requires_at_least_one_selector() -> Result<()> {
        let (_home, build) = synthetic_home()?;
        assert!(run(&build, &no_selectors()).is_err());
        Ok(())
    }

    #[test]
    fn stale_selector_skips_current_generation() -> Result<()> {
        let (_home, build) = synthetic_home()?;
        let opts = Options {
            stale: true,
            ..no_selectors()
        };
        // A single unit IS the current cohort → stale selects nothing.
        run(&build, &opts)?;
        assert!(
            build
                .join("debug/.fingerprint/pkg-1122334455667788")
                .exists()
        );
        Ok(())
    }

    #[test]
    fn evicts_fingerprint_and_artifacts_with_force() -> Result<()> {
        let (_home, build) = synthetic_home()?;
        let opts = Options {
            unit: Some("pkg-1122334455667788"),
            force: true,
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert!(
            !build
                .join("debug/.fingerprint/pkg-1122334455667788")
                .exists(),
            "fingerprint dir must be gone"
        );
        assert!(
            !build
                .join("debug/deps/libpkg-1122334455667788.rlib")
                .exists(),
            "artifact must be gone"
        );
        Ok(())
    }

    use std::time::Duration;

    /// `{home}/build` + `{home}/work` — the sibling layout of a real cache.
    fn tier_home() -> Result<tempfile::TempDir> {
        let home = tempfile::tempdir()?;
        fs::create_dir_all(home.path().join("build"))?;
        fs::create_dir_all(home.path().join("work"))?;
        Ok(home)
    }

    /// One job dir: `work/<name>/debug/libjob.rlib`, aged by `age_days`.
    fn aged_job(home: &Path, name: &str, age_days: u64) -> Result<PathBuf> {
        let debug = home.join("work").join(name).join("debug");
        fs::create_dir_all(&debug)?;
        let artifact = debug.join("libjob.rlib");
        fs::write(&artifact, [0u8; 64])?;
        if age_days > 0 {
            let when = SystemTime::now() - Duration::from_secs(age_days * 86_400);
            fs::File::open(&artifact)?.set_modified(when)?;
        }
        Ok(home.join("work").join(name))
    }

    #[test]
    fn work_dirs_collect_by_age_and_keep_young_ones() -> Result<()> {
        let home = tier_home()?;
        let build = home.path().join("build");
        let old_job = aged_job(home.path(), "old-job", 30)?;
        let fresh_job = aged_job(home.path(), "fresh-job", 0)?;

        let dry = evict_work_dirs(&build, None, false)?;
        assert_eq!((dry.scanned, dry.eligible, dry.young), (2, 1, 1));
        assert!(
            old_job.exists() && fresh_job.exists(),
            "dry run touches nothing"
        );

        let done = evict_work_dirs(&build, None, true)?;
        assert_eq!(done.removed, 1);
        assert_eq!(done.unique_bytes, 64, "unique bytes are actually freed");
        assert!(!old_job.exists(), "aged job dir collected");
        assert!(fresh_job.exists(), "young job dir kept");
        Ok(())
    }

    #[test]
    fn work_dirs_never_collect_locked_jobs() -> Result<()> {
        let home = tier_home()?;
        let build = home.path().join("build");
        let job = aged_job(home.path(), "locked-job", 30)?;
        let lock_path = job.join("debug/.cargo-build-lock");
        fs::write(&lock_path, b"")?;
        // The lock file was also written a month ago, so age alone would collect it.
        let when = SystemTime::now() - Duration::from_secs(30 * 86_400);
        fs::File::open(&lock_path)?.set_modified(when)?;

        let held = fs::OpenOptions::new().write(true).open(&lock_path)?;
        held.try_lock().context("test holds the lock")?;

        let report = evict_work_dirs(&build, None, true)?;
        assert_eq!((report.in_use, report.removed), (1, 0));
        assert!(job.exists(), "a locked job dir is never touched");

        held.unlock()?;
        drop(held);
        let report = evict_work_dirs(&build, None, true)?;
        assert_eq!(report.removed, 1);
        assert!(!job.exists());
        Ok(())
    }

    #[test]
    fn work_dirs_cannot_be_combined_with_unit_selectors() -> Result<()> {
        let home = tier_home()?;
        let opts = Options {
            work_dirs: true,
            stale: true,
            ..no_selectors()
        };
        assert!(run(&home.path().join("build"), &opts).is_err());
        Ok(())
    }

    #[test]
    fn work_dir_accounting_separates_unique_and_shared_bytes() -> Result<()> {
        let home = tier_home()?;
        let debug = home.path().join("work/aged-job/debug");
        fs::create_dir_all(&debug)?;
        let unique = debug.join("unique.rlib");
        let shared = debug.join("shared.rlib");
        fs::write(&unique, [0u8; 100])?;
        fs::write(&shared, [0u8; 200])?;
        // One artifact also lives in the store → its bytes are not freed here.
        fs::hard_link(&shared, home.path().join("store-copy.rlib"))?;
        let when = SystemTime::now() - Duration::from_secs(30 * 86_400);
        fs::File::open(&unique)?.set_modified(when)?;
        fs::File::open(&shared)?.set_modified(when)?;

        let report = evict_work_dirs(&home.path().join("build"), None, false)?;
        assert_eq!(report.unique_bytes, 100);
        assert_eq!(report.shared_bytes, 200);
        assert_eq!(report.nominal_bytes, 300);
        Ok(())
    }

    #[test]
    fn work_dir_removal_ignores_the_age_window() -> Result<()> {
        let home = tier_home()?;
        let build = home.path().join("build");
        let job = aged_job(home.path(), "fresh-job", 0)?;
        let opts = Options {
            work_dir: Some("fresh-job"),
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert!(
            !job.exists(),
            "an explicitly selected dir goes regardless of age"
        );
        Ok(())
    }

    #[test]
    fn work_dir_refuses_locked_and_unknown_targets() -> Result<()> {
        let home = tier_home()?;
        let build = home.path().join("build");
        let job = aged_job(home.path(), "locked-job", 0)?;
        let lock_path = job.join("debug/.cargo-build-lock");
        fs::write(&lock_path, b"")?;
        let held = fs::OpenOptions::new().write(true).open(&lock_path)?;
        held.try_lock().context("test holds the lock")?;

        let opts = Options {
            work_dir: Some("locked-job"),
            delete: true,
            ..no_selectors()
        };
        let err = run(&build, &opts).unwrap_err();
        assert!(err.to_string().contains("in use"), "{err}");
        assert!(job.exists(), "a locked job dir survives");
        drop(held);

        let unknown = Options {
            work_dir: Some("nope"),
            ..no_selectors()
        };
        assert!(run(&build, &unknown).is_err());
        Ok(())
    }

    #[test]
    fn work_dir_never_targets_the_shared_build_or_outside_paths() -> Result<()> {
        let home = tier_home()?;
        let build = home.path().join("build");
        let shared = Options {
            work_dir: Some("build"),
            ..no_selectors()
        };
        let err = run(&build, &shared).unwrap_err();
        assert!(err.to_string().contains("never removed"), "{err}");

        let outside = Options {
            work_dir: Some("/tmp"),
            ..no_selectors()
        };
        assert!(
            run(&build, &outside).is_err(),
            "only job dirs under work/ — /tmp is refused"
        );
        Ok(())
    }

    #[test]
    fn work_dir_rejects_selectors_and_sibling_modes() -> Result<()> {
        let home = tier_home()?;
        let build = home.path().join("build");
        let with_age = Options {
            work_dir: Some("x"),
            older_than: Some("30d"),
            ..no_selectors()
        };
        assert!(run(&build, &with_age).is_err());

        let with_dirs = Options {
            work_dir: Some("x"),
            work_dirs: true,
            ..no_selectors()
        };
        let err = run(&build, &with_dirs).unwrap_err();
        assert!(err.to_string().contains("one operation"), "{err}");
        Ok(())
    }

    /// `{home}/build` with one unit + `{home}/work/ci-1` with the same unit.
    fn tiered_views() -> Result<(tempfile::TempDir, PathBuf)> {
        let home = tempfile::tempdir()?;
        fs::create_dir_all(home.path().join("build"))?;
        fs::create_dir_all(home.path().join("work"))?;
        write_synthetic_unit(&home.path().join("build"))?;
        let job = home.path().join("work/ci-1");
        write_synthetic_unit(&job)?;
        Ok((home, job))
    }

    #[test]
    fn work_surgery_unlinks_only_the_named_view() -> Result<()> {
        let (home, job) = tiered_views()?;
        let build = home.path().join("build");
        let opts = Options {
            work: Some("ci-1"),
            crate_name: Some("pkg"),
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert!(
            !job.join("debug/.fingerprint/pkg-1122334455667788").exists(),
            "job fingerprint unlinked"
        );
        assert!(
            !job.join("debug/deps/libpkg-1122334455667788.rlib").exists(),
            "job artifact unlinked"
        );
        assert!(
            build
                .join("debug/.fingerprint/pkg-1122334455667788")
                .exists(),
            "build view untouched"
        );
        assert!(
            build
                .join("debug/deps/libpkg-1122334455667788.rlib")
                .exists(),
            "build artifact untouched"
        );
        assert!(!home.path().join("store").exists(), "no store was touched");
        Ok(())
    }

    #[test]
    fn work_surgery_accepts_the_shared_build_view() -> Result<()> {
        let (home, job) = tiered_views()?;
        let build = home.path().join("build");
        let opts = Options {
            work: Some("build"),
            unit: Some("pkg"),
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert!(
            !build
                .join("debug/.fingerprint/pkg-1122334455667788")
                .exists()
        );
        assert!(job.join("debug/.fingerprint/pkg-1122334455667788").exists());
        Ok(())
    }

    #[test]
    fn work_surgery_refuses_a_locked_view() -> Result<()> {
        let (home, job) = tiered_views()?;
        let build = home.path().join("build");
        let lock_path = job.join("debug/.cargo-build-lock");
        fs::write(&lock_path, b"")?;
        let held = fs::OpenOptions::new().write(true).open(&lock_path)?;
        held.try_lock().context("test holds the lock")?;
        let opts = Options {
            work: Some("ci-1"),
            crate_name: Some("pkg"),
            delete: true,
            ..no_selectors()
        };
        let err = run(&build, &opts).unwrap_err();
        assert!(err.to_string().contains("in use"), "{err}");
        assert!(job.join("debug/.fingerprint/pkg-1122334455667788").exists());
        Ok(())
    }

    #[test]
    fn work_surgery_requires_a_selector_and_rejects_bulk_ones() -> Result<()> {
        let (home, _job) = tiered_views()?;
        let build = home.path().join("build");
        let bare = Options {
            work: Some("ci-1"),
            ..no_selectors()
        };
        assert!(run(&build, &bare).is_err(), "needs --crate/--unit");
        let bulk = Options {
            work: Some("ci-1"),
            stale: true,
            ..no_selectors()
        };
        let err = run(&build, &bulk).unwrap_err();
        assert!(err.to_string().contains("--crate"), "{err}");
        Ok(())
    }

    #[test]
    fn work_surgery_unknown_view_errors() -> Result<()> {
        let (home, _job) = tiered_views()?;
        let build = home.path().join("build");
        let opts = Options {
            work: Some("nope"),
            crate_name: Some("pkg"),
            ..no_selectors()
        };
        assert!(run(&build, &opts).is_err());
        Ok(())
    }

    /// A home where the unit is both harvested into the store and present in
    /// the build view — the state a real `harvest` leaves behind.
    fn harvested_home() -> Result<(tempfile::TempDir, PathBuf)> {
        let (home, build) = synthetic_home()?;
        crate::vault::harvest(&build, &home.path().join("store"), None, false)?;
        Ok((home, build))
    }

    #[test]
    fn store_tier_drops_groups_and_prunes_the_manifest() -> Result<()> {
        let (home, build) = harvested_home()?;
        let store = home.path().join("store");
        assert_eq!(crate::vault::stored_units(&store)?.len(), 1, "harvested");

        // No --force: with --tier store the vault is the user's to trim.
        let opts = Options {
            unit: Some("pkg"),
            tier: Tier::Store,
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert_eq!(
            crate::vault::stored_units(&store)?.len(),
            0,
            "store record gone"
        );
        assert!(
            build
                .join("debug/.fingerprint/pkg-1122334455667788")
                .exists(),
            "the view keeps its links"
        );
        assert!(
            build
                .join("debug/deps/libpkg-1122334455667788.rlib")
                .exists(),
            "the view keeps its artifact"
        );
        Ok(())
    }

    #[test]
    fn view_tier_leaves_the_store_alone() -> Result<()> {
        let (home, build) = harvested_home()?;
        let store = home.path().join("store");
        let opts = Options {
            unit: Some("pkg"),
            tier: Tier::View,
            force: true,
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert!(
            !build
                .join("debug/.fingerprint/pkg-1122334455667788")
                .exists()
        );
        assert_eq!(
            crate::vault::stored_units(&store)?.len(),
            1,
            "store record stays"
        );
        Ok(())
    }

    #[test]
    fn default_tier_removes_store_and_view_together() -> Result<()> {
        let (home, build) = harvested_home()?;
        let store = home.path().join("store");
        let opts = Options {
            unit: Some("pkg"),
            force: true,
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert_eq!(crate::vault::stored_units(&store)?.len(), 0);
        assert!(
            !build
                .join("debug/.fingerprint/pkg-1122334455667788")
                .exists()
        );
        assert!(
            !build
                .join("debug/deps/libpkg-1122334455667788.rlib")
                .exists()
        );
        Ok(())
    }

    #[test]
    fn current_generation_is_protected_for_views() -> Result<()> {
        let (home, build) = harvested_home()?;
        let store = home.path().join("store");
        let opts = Options {
            unit: Some("pkg"),
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert!(
            build
                .join("debug/.fingerprint/pkg-1122334455667788")
                .exists(),
            "current unit protected without --force"
        );
        assert_eq!(crate::vault::stored_units(&store)?.len(), 1);
        Ok(())
    }

    #[test]
    fn stale_drops_non_active_generations_from_the_store() -> Result<()> {
        let (home, build) = harvested_home()?;
        let store = home.path().join("store");
        let old = store.join("0.0.0-deadbeef-x86_64-unknown-linux-gnu-00000000");
        let unit = old.join("units/oldpkg-0000111122223333");
        fs::create_dir_all(&unit)?;
        fs::write(unit.join("lib-old"), "aaaaaaaabbbbbbbb")?;
        fs::write(
            old.join("manifest.json"),
            r#"{"parity_key":"0.0.0-deadbeef-x86_64-unknown-linux-gnu-00000000","toolchain":"rustc 0.0.0","created":1,"updated":1,"units":[{"name":"oldpkg-0000111122223333","profile":"debug","memo":"lib-old","hash":"aaaaaaaabbbbbbbb","stamp":1,"files":[{"path":"lib-old","dest":".fingerprint/oldpkg-0000111122223333/lib-old","size":16,"role":"memo"}]}]}"#,
        )?;

        let opts = Options {
            stale: true,
            tier: Tier::Store,
            delete: true,
            ..no_selectors()
        };
        run(&build, &opts)?;
        assert!(!old.exists(), "the old generation is gone");
        assert_eq!(
            crate::vault::stored_units(&store)?.len(),
            1,
            "the active generation survives"
        );
        Ok(())
    }

    #[test]
    fn accounting_frees_only_inodes_whose_last_link_goes() -> Result<()> {
        let home = tempfile::tempdir()?;
        let a = home.path().join("a.bin");
        let b = home.path().join("b.bin");
        let store_copy = home.path().join("store-copy.bin");
        fs::write(&a, [0u8; 100])?;
        fs::write(&b, [0u8; 200])?;
        fs::hard_link(&b, &store_copy)?;

        // Both links of `b` are in the plan → its bytes are actually freed.
        let (nominal, freed, released, count) =
            account_files(&[a.clone(), b.clone(), store_copy.clone()]);
        assert_eq!((nominal, freed, released, count), (300, 300, 0, 3));

        // Only the view's links are removed → `b` is released, not freed.
        let (nominal, freed, released, count) = account_files(&[a, b]);
        assert_eq!((nominal, freed, released, count), (300, 100, 200, 2));
        Ok(())
    }
}