workpen 0.7.0

Workpen dest-deny and a per-child process jail (Landlock, Seatbelt, Windows).
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
//! Fail-closed leftover worktree age GC. Feature-gated (`gc`).
//!
//! Unique-work is `git status --porcelain=v1 -uall --ignored` (with
//! `status.showUntrackedFiles=all`). Last-used is `git log -1
//! --format=%ct`, the index mtime, and a bounded tree walk. Both clocks
//! are git CLI. There is no `gix` path in v1.

use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, SystemTime};

use crate::deny::{DenyPolicy, path_is_denied_glob};

/// Host knobs. Bline passes leftover_dir = ".bline-worktrees",
/// saved_ref_prefix = "refs/bline/reclaimed".
#[derive(Debug, Clone)]
pub struct GcConfig {
    pub leftover_dir: PathBuf,
    pub saved_ref_prefix: String,
    pub max_age: Duration,
    pub dry_run: bool,
    pub now: SystemTime,
}

impl GcConfig {
    pub fn new(cwd: &Path, max_age: Duration) -> Self {
        Self {
            leftover_dir: cwd.join(".workpen-worktrees"),
            saved_ref_prefix: "refs/workpen/reclaimed".into(),
            max_age,
            dry_run: false,
            now: SystemTime::now(),
        }
    }
}

/// Why a worktree must be kept. Hosts branch on this; do not parse messages.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeepReason {
    DirtyWork,
    UniqueUntracked,
    TooNew,
    UntrackedWorktree,
    NotAGitDir,
    StatusUnreadable,
    LiveCwd,
    Locked,
}

impl KeepReason {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::DirtyWork => "unique uncommitted work",
            Self::UniqueUntracked => "unique untracked files",
            Self::TooNew => "newer than --max-age",
            Self::UntrackedWorktree => "untracked leftover (not a registered worktree)",
            Self::NotAGitDir => "not a git worktree",
            Self::StatusUnreadable => "git status unreadable",
            Self::LiveCwd => "live process cwd",
            Self::Locked => "locked",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GcDecision {
    Keep { reason: KeepReason },
    Reclaim { saved_refs: Vec<String> },
}

#[derive(Debug, thiserror::Error)]
pub enum GcError {
    #[error("invalid duration: {0}")]
    InvalidDuration(String),
    #[error("worktree registry unreadable: {0}")]
    RegistryUnreadable(String),
    #[error("{0} is not a git repository; gc reclaims leftover worktrees of a git repo")]
    NotARepo(String),
    #[error("git {op} failed: {detail}")]
    Git { op: String, detail: String },
    #[error("refuse to treat home as a git workspace: {0}")]
    Home(String),
}

/// Parse a duration token (`7d`, `24h`, `60m`, `30s`). Number must be `> 0`.
/// Does not peel quotes. Hosts strip `'7d'` / `"7d"` first.
pub fn parse_max_age(raw: &str) -> Result<Duration, GcError> {
    let s = raw.trim();
    if s.chars().count() < 2 {
        return Err(GcError::InvalidDuration(format!(
            "{s} (use e.g. 30s, 60m, 24h, 7d)"
        )));
    }
    let unit_start = s.char_indices().next_back().map(|(i, _)| i).unwrap_or(0);
    let (num, unit) = s.split_at(unit_start);
    let n: u64 = num
        .trim()
        .parse()
        .map_err(|_| GcError::InvalidDuration(format!("{s} (use s, m, h, d)")))?;
    if n == 0 {
        return Err(GcError::InvalidDuration(format!(
            "duration must be greater than zero: {s}"
        )));
    }
    match unit.to_ascii_lowercase().as_str() {
        "s" => Ok(Duration::from_secs(n)),
        "m" => Ok(Duration::from_secs(n.saturating_mul(60))),
        "h" => Ok(Duration::from_secs(n.saturating_mul(3600))),
        "d" => Ok(Duration::from_secs(n.saturating_mul(86400))),
        _ => Err(GcError::InvalidDuration(format!("{s} (use s, m, h, d)"))),
    }
}

/// Age filter then classify. Untracked trees are never age-gc'd.
///
/// Uses [`DenyPolicy::default()`]. Hosts with extras should call
/// [`classify_for_age_gc_with_policy`].
pub fn classify_for_age_gc(
    path: &Path,
    untracked: bool,
    max_age: Duration,
    now: SystemTime,
) -> GcDecision {
    classify_for_age_gc_with_policy(path, untracked, max_age, now, &DenyPolicy::default())
}

/// Like [`classify_for_age_gc`], using `policy` for glob-only cache name checks.
pub fn classify_for_age_gc_with_policy(
    path: &Path,
    untracked: bool,
    max_age: Duration,
    now: SystemTime,
    policy: &DenyPolicy,
) -> GcDecision {
    if untracked {
        return GcDecision::Keep {
            reason: KeepReason::UntrackedWorktree,
        };
    }
    if newest_tree_mtime(path).is_err() {
        return GcDecision::Keep {
            reason: KeepReason::StatusUnreadable,
        };
    }
    let last_used = worktree_last_used(path).unwrap_or(now);
    if now.duration_since(last_used).unwrap_or_default() < max_age {
        return GcDecision::Keep {
            reason: KeepReason::TooNew,
        };
    }
    classify_worktree_with_policy(path, false, policy)
}

/// Remove one worktree after the unique-work check.
///
/// Discovers `--git-common-dir` from `path` (not `path.parent()`).
/// `force` unlocks porcelain locked (`git worktree remove --force --force`).
/// `force` does not skip dirty, unique, live-cwd, unreadable, or missing-git Keep.
/// An unreadable worktree registry is [`GcError::RegistryUnreadable`].
/// A same-repo checkout missing from the registry is leftover rm
/// (`remove_dir_all`) after unique-work. A directory with no `.git` is
/// [`KeepReason::NotAGitDir`].
///
/// Uses [`DenyPolicy::default()`]. Hosts with extras should call
/// [`remove_explicit_with_policy`].
pub fn remove_explicit(
    path: &Path,
    saved_ref_prefix: &str,
    force: bool,
) -> Result<GcDecision, GcError> {
    remove_explicit_with_policy(path, saved_ref_prefix, force, &DenyPolicy::default())
}

/// Like [`remove_explicit`], using `policy` for glob-only cache name checks.
pub fn remove_explicit_with_policy(
    path: &Path,
    saved_ref_prefix: &str,
    force: bool,
    policy: &DenyPolicy,
) -> Result<GcDecision, GcError> {
    if !path.is_dir() || !is_git_repo(path) {
        return Ok(GcDecision::Keep {
            reason: KeepReason::NotAGitDir,
        });
    }
    let common = git_common_dir(path).map_err(registry_unreadable)?;
    let repo = repo_cwd_from_common_dir(&common);
    let registered = registered_worktrees(&repo)?;
    let entry = registered.iter().find(|w| paths_eq(&w.path, path));
    let locked = entry.is_some_and(|w| w.locked);
    if locked && !force {
        return Ok(GcDecision::Keep {
            reason: KeepReason::Locked,
        });
    }
    if worktree_has_live_cwd(path) {
        return Ok(GcDecision::Keep {
            reason: KeepReason::LiveCwd,
        });
    }
    match classify_worktree_with_policy(path, false, policy) {
        keep @ GcDecision::Keep { .. } => Ok(keep),
        GcDecision::Reclaim { .. } => {
            let saved = match save_unique_commits(path, saved_ref_prefix) {
                Ok(saved) => saved,
                Err(_) => {
                    return Ok(GcDecision::Keep {
                        reason: KeepReason::StatusUnreadable,
                    });
                }
            };
            if entry.is_some() {
                remove_worktree(&repo, path, locked && force)?;
            } else {
                std::fs::remove_dir_all(path).map_err(|e| GcError::Git {
                    op: "leftover rm".into(),
                    detail: e.to_string(),
                })?;
            }
            Ok(GcDecision::Reclaim { saved_refs: saved })
        }
    }
}

/// Classify keep vs reclaim (age already applied by the caller when relevant).
///
/// `Reclaim.saved_refs` is empty here. Unique commits are saved in
/// [`run_gc`] and [`remove_explicit`].
///
/// Uses [`DenyPolicy::default()`]. Hosts with extras should call
/// [`classify_worktree_with_policy`].
pub fn classify_worktree(path: &Path, untracked: bool) -> GcDecision {
    classify_worktree_with_policy(path, untracked, &DenyPolicy::default())
}

/// Like [`classify_worktree`], using `policy` for glob-only cache name checks.
pub fn classify_worktree_with_policy(
    path: &Path,
    untracked: bool,
    policy: &DenyPolicy,
) -> GcDecision {
    if untracked {
        return GcDecision::Keep {
            reason: KeepReason::UntrackedWorktree,
        };
    }
    if !path.is_dir() {
        return GcDecision::Keep {
            reason: KeepReason::NotAGitDir,
        };
    }
    if !is_git_repo(path) {
        return GcDecision::Keep {
            reason: KeepReason::NotAGitDir,
        };
    }
    match unique_work_reason(path, policy) {
        Some(reason) => GcDecision::Keep { reason },
        None => GcDecision::Reclaim {
            saved_refs: Vec::new(),
        },
    }
}

/// List registered worktrees, skip primary checkout, skip locked / live cwd,
/// classify the rest. Untracked leftovers under `leftover_dir` are reported
/// as Keep(UntrackedWorktree) and never removed by age gc.
///
/// Uses [`DenyPolicy::default()`]. Hosts with extras should call
/// [`run_gc_with_policy`].
pub fn run_gc(cwd: &Path, cfg: &GcConfig) -> Result<Vec<(PathBuf, GcDecision)>, GcError> {
    run_gc_with_policy(cwd, cfg, &DenyPolicy::default())
}

/// Like [`run_gc`], using `policy` for glob-only cache name checks.
pub fn run_gc_with_policy(
    cwd: &Path,
    cfg: &GcConfig,
    policy: &DenyPolicy,
) -> Result<Vec<(PathBuf, GcDecision)>, GcError> {
    refuse_home(cwd)?;
    let registered = registered_worktrees(cwd)?;
    let registered_paths: Vec<PathBuf> = registered.iter().map(|w| w.path.clone()).collect();
    let primary = registered
        .first()
        .map(|w| w.path.clone())
        .or_else(|| repo_root(cwd).ok());
    let mut rows = Vec::new();
    for wt in &registered {
        if primary.as_ref().is_some_and(|p| paths_eq(p, &wt.path)) {
            continue;
        }
        if wt.locked {
            rows.push((
                wt.path.clone(),
                GcDecision::Keep {
                    reason: KeepReason::Locked,
                },
            ));
            continue;
        }
        if worktree_has_live_cwd(&wt.path) {
            rows.push((
                wt.path.clone(),
                GcDecision::Keep {
                    reason: KeepReason::LiveCwd,
                },
            ));
            continue;
        }
        let mut decision =
            classify_for_age_gc_with_policy(&wt.path, false, cfg.max_age, cfg.now, policy);
        if let GcDecision::Reclaim { .. } = &decision
            && !cfg.dry_run
        {
            match save_unique_commits(&wt.path, &cfg.saved_ref_prefix) {
                Ok(saved) => {
                    remove_worktree(cwd, &wt.path, false)?;
                    decision = GcDecision::Reclaim { saved_refs: saved };
                }
                Err(_) => {
                    decision = GcDecision::Keep {
                        reason: KeepReason::StatusUnreadable,
                    };
                }
            }
        }
        rows.push((wt.path.clone(), decision));
    }
    for path in untracked_leftovers(&cfg.leftover_dir, &registered_paths) {
        rows.push((
            path,
            GcDecision::Keep {
                reason: KeepReason::UntrackedWorktree,
            },
        ));
    }
    if !cfg.dry_run {
        git(cwd, &["worktree", "prune"]).map_err(|e| match e {
            GcError::Git { detail, .. } => GcError::Git {
                op: "worktree prune".into(),
                detail,
            },
            other => other,
        })?;
    }
    Ok(rows)
}

const CACHE_DIR_NAMES: &[&str] = &["target", "node_modules", ".venv", "dist", "__pycache__"];
const LAST_USED_WALK_LIMIT: usize = 4096;

struct Registered {
    path: PathBuf,
    locked: bool,
}

fn refuse_home(repo: &Path) -> Result<(), GcError> {
    let top = match git(repo, &["rev-parse", "--show-toplevel"]) {
        Ok(s) => PathBuf::from(s.trim()),
        Err(_) => return Ok(()),
    };
    if is_home(&top) {
        return Err(GcError::Home(top.display().to_string()));
    }
    Ok(())
}

fn is_home(path: &Path) -> bool {
    crate::guard::is_user_home_dir(path)
}

fn repo_root(cwd: &Path) -> Result<PathBuf, GcError> {
    let top = git(cwd, &["rev-parse", "--show-toplevel"])?;
    Ok(PathBuf::from(top.trim()))
}

fn is_git_repo(cwd: &Path) -> bool {
    if !cwd.join(".git").exists() {
        return false;
    }
    git(cwd, &["rev-parse", "--is-inside-work-tree"])
        .map(|s| s.trim() == "true")
        .unwrap_or(false)
}

fn registered_worktrees(cwd: &Path) -> Result<Vec<Registered>, GcError> {
    let out = git(cwd, &["worktree", "list", "--porcelain"]).map_err(|e| match e {
        GcError::Git { detail, .. } if detail.contains("not a git repository") => {
            GcError::NotARepo(cwd.display().to_string())
        }
        GcError::Git { detail, .. } => GcError::RegistryUnreadable(detail),
        other => other,
    })?;
    Ok(parse_worktree_list(&out))
}

fn parse_worktree_list(text: &str) -> Vec<Registered> {
    let mut trees = Vec::new();
    let mut path: Option<PathBuf> = None;
    let mut locked = false;
    let flush = |trees: &mut Vec<Registered>, path: &mut Option<PathBuf>, locked: &mut bool| {
        if let Some(p) = path.take() {
            trees.push(Registered {
                path: p,
                locked: *locked,
            });
            *locked = false;
        }
    };
    for line in text.lines() {
        if let Some(rest) = line.strip_prefix("worktree ") {
            flush(&mut trees, &mut path, &mut locked);
            path = Some(PathBuf::from(rest));
        } else if line == "locked" || line.starts_with("locked ") {
            locked = true;
        }
    }
    flush(&mut trees, &mut path, &mut locked);
    trees
}

fn untracked_leftovers(leftover_dir: &Path, registered: &[PathBuf]) -> Vec<PathBuf> {
    let Ok(rd) = std::fs::read_dir(leftover_dir) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for entry in rd.flatten() {
        let p = entry.path();
        if !p.is_dir() {
            continue;
        }
        if registered.iter().any(|r| paths_eq(r, &p)) {
            continue;
        }
        out.push(p);
    }
    out
}

fn paths_eq(a: &Path, b: &Path) -> bool {
    if a == b {
        return true;
    }
    match (std::fs::canonicalize(a), std::fs::canonicalize(b)) {
        (Ok(ca), Ok(cb)) => ca == cb,
        _ => a == b,
    }
}

fn path_is_same_or_parent(root: &Path, inner: &Path) -> bool {
    if paths_eq(root, inner) {
        return true;
    }
    match (std::fs::canonicalize(root), std::fs::canonicalize(inner)) {
        (Ok(r), Ok(i)) => i.starts_with(&r),
        _ => inner.starts_with(root),
    }
}

fn worktree_has_live_cwd(path: &Path) -> bool {
    let current = std::env::current_dir().ok();
    let other = other_process_cwds();
    worktree_has_live_cwd_from(
        path,
        current.as_deref(),
        match &other {
            Ok(cwds) => Ok(cwds.as_slice()),
            Err(()) => Err(()),
        },
    )
}

/// Fail-closed: unknown other-process cwds count as live.
fn worktree_has_live_cwd_from(
    path: &Path,
    current: Option<&Path>,
    other: Result<&[PathBuf], ()>,
) -> bool {
    if let Some(cwd) = current
        && path_is_same_or_parent(path, cwd)
    {
        return true;
    }
    match other {
        Err(()) => true,
        Ok(cwds) => cwds.iter().any(|cwd| path_is_same_or_parent(path, cwd)),
    }
}

/// Other-process cwds. `Err` when an expected probe failed.
/// Windows has no `/proc` and no `lsof`; that is not a probe failure.
fn other_process_cwds() -> Result<Vec<PathBuf>, ()> {
    #[cfg(target_os = "linux")]
    {
        if let Ok(cwds) = linux_process_cwds() {
            return Ok(cwds);
        }
    }
    #[cfg(unix)]
    {
        lsof_process_cwds()
    }
    #[cfg(not(unix))]
    {
        Ok(Vec::new())
    }
}

#[cfg(target_os = "linux")]
fn linux_process_cwds() -> Result<Vec<PathBuf>, ()> {
    let rd = std::fs::read_dir("/proc").map_err(|_| ())?;
    let mut out = Vec::new();
    for entry in rd.flatten() {
        let name = entry.file_name();
        if !name
            .to_str()
            .is_some_and(|s| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()))
        {
            continue;
        }
        let cwd_link = entry.path().join("cwd");
        if let Ok(cwd) = std::fs::read_link(cwd_link) {
            out.push(cwd);
        }
    }
    Ok(out)
}

#[cfg(unix)]
fn lsof_process_cwds() -> Result<Vec<PathBuf>, ()> {
    let out = Command::new("lsof")
        .args(["-a", "-d", "cwd", "-Fn"])
        .output()
        .map_err(|_| ())?;
    let paths: Vec<PathBuf> = String::from_utf8_lossy(&out.stdout)
        .lines()
        .filter_map(|line| line.strip_prefix('n'))
        .filter(|p| !p.is_empty())
        .map(PathBuf::from)
        .collect();
    if !out.status.success() && paths.is_empty() {
        return Err(());
    }
    Ok(paths)
}

/// Newest of HEAD committer time (`git log -1 --format=%ct`), index
/// mtime, and a bounded tree walk. Git CLI, not gix.
pub fn worktree_last_used(path: &Path) -> Option<SystemTime> {
    let mut latest = head_commit_time(path);
    if let Some(t) = index_mtime(path) {
        latest = Some(latest.map_or(t, |n| n.max(t)));
    }
    if let Ok(Some(t)) = newest_tree_mtime(path) {
        latest = Some(latest.map_or(t, |n| n.max(t)));
    }
    latest
}

fn head_commit_time(path: &Path) -> Option<SystemTime> {
    let out = git(path, &["log", "-1", "--format=%ct"]).ok()?;
    let secs: u64 = out.trim().parse().ok()?;
    Some(SystemTime::UNIX_EPOCH + Duration::from_secs(secs))
}

fn index_file(path: &Path) -> Option<PathBuf> {
    let raw = git(path, &["rev-parse", "--git-path", "index"]).ok()?;
    let index = PathBuf::from(raw.trim());
    Some(if index.is_absolute() {
        index
    } else {
        path.join(index)
    })
}

fn index_mtime(path: &Path) -> Option<SystemTime> {
    std::fs::metadata(index_file(path)?)
        .and_then(|m| m.modified())
        .ok()
}

/// `git status` refreshes the index. That must not bump last-used.
fn restore_index_mtime(path: &Path, prev: SystemTime) {
    let Some(index) = index_file(path) else {
        return;
    };
    if let Ok(file) = std::fs::OpenOptions::new().write(true).open(index) {
        let _ = file.set_modified(prev);
    }
}

/// Finished walk of file mtimes. `Err` if `read_dir` failed or the
/// budget ran out with directories still queued. `DirEntry::metadata`
/// (lstat); do not switch to `fs::metadata`.
fn newest_tree_mtime(root: &Path) -> Result<Option<SystemTime>, ()> {
    let mut newest: Option<SystemTime> = None;
    let mut remaining = LAST_USED_WALK_LIMIT;
    let mut queue = std::collections::VecDeque::new();
    queue.push_back(root.to_path_buf());
    while remaining > 0 {
        let Some(dir) = queue.pop_front() else {
            return Ok(newest);
        };
        let Ok(rd) = std::fs::read_dir(&dir) else {
            return Err(());
        };
        for entry in rd.flatten() {
            if remaining == 0 {
                return Err(());
            }
            remaining -= 1;
            let p = entry.path();
            let name = entry.file_name();
            let name = name.to_string_lossy();
            if name == ".git" || is_age_cache_dir_name(&name) {
                continue;
            }
            let Ok(meta) = entry.metadata() else {
                continue;
            };
            if meta.is_dir() {
                queue.push_back(p);
                continue;
            }
            if let Ok(t) = meta.modified() {
                newest = Some(newest.map_or(t, |n| n.max(t)));
            }
        }
    }
    if !queue.is_empty() {
        return Err(());
    }
    Ok(newest)
}

fn is_age_cache_dir_name(name: &str) -> bool {
    CACHE_DIR_NAMES.iter().any(|n| name.eq_ignore_ascii_case(n))
}

/// First path component matches [`CACHE_DIR_NAMES`] by name. No `CACHEDIR.TAG`.
/// Porcelain often lists only `!! target/`, not `!! target/.env`.
/// Cache walk matches deny globs only (full path and basename). It does
/// not apply dest-deny hardlink / incomplete-nlink fail-closed, and it
/// does not follow directory symlinks out of the cache tree.
fn cache_tree_has_denied_name(root: &Path, rel: &Path, policy: &DenyPolicy) -> bool {
    if cache_path_denied_glob(policy, rel) {
        return true;
    }
    let Some(std::path::Component::Normal(name)) = rel.components().next() else {
        return false;
    };
    let start = root.join(name);
    let mut remaining = LAST_USED_WALK_LIMIT;
    let mut queue = std::collections::VecDeque::new();
    queue.push_back(start);
    while remaining > 0 {
        let Some(dir) = queue.pop_front() else {
            return false;
        };
        let rd = match std::fs::read_dir(&dir) {
            Ok(rd) => rd,
            Err(_) => return true,
        };
        for entry in rd.flatten() {
            if remaining == 0 {
                return true;
            }
            remaining -= 1;
            let p = entry.path();
            if cache_path_denied_glob(policy, &p) {
                return true;
            }
            let is_real_dir = match entry.file_type() {
                Ok(ft) => ft.is_dir(),
                Err(_) => match std::fs::symlink_metadata(&p) {
                    Ok(meta) => meta.file_type().is_dir(),
                    Err(_) => return true,
                },
            };
            if is_real_dir {
                queue.push_back(p);
            }
        }
    }
    !queue.is_empty()
}

fn cache_path_denied_glob(policy: &DenyPolicy, path: &Path) -> bool {
    let full = path.to_string_lossy().replace('\\', "/");
    if path_is_denied_glob(policy.globs(), &full) {
        return true;
    }
    path.file_name()
        .is_some_and(|base| path_is_denied_glob(policy.globs(), &base.to_string_lossy()))
}

fn is_under_known_cache(root: &Path, file: &Path) -> bool {
    let Ok(rel) = file.strip_prefix(root) else {
        return false;
    };
    let Some(std::path::Component::Normal(name)) = rel.components().next() else {
        return false;
    };
    is_age_cache_dir_name(&name.to_string_lossy())
}

/// Unique-work is `git status --porcelain=v1 -uall --ignored`. Git CLI, not gix.
/// Porcelain `??` / `!!` under a first-component cache dir name is not unique
/// work unless a deny-glob name exists in that tree (git often lists only
/// `!! target/`, not `!! target/.env`). Other XY statuses under those names
/// are DirtyWork (tracked dirty cache paths).
fn unique_work_reason(path: &Path, policy: &DenyPolicy) -> Option<KeepReason> {
    let index_before = index_mtime(path);
    let out = match git(
        path,
        &[
            "-c",
            "status.showUntrackedFiles=all",
            "status",
            "--porcelain=v1",
            "-uall",
            "--ignored",
        ],
    ) {
        Ok(s) => s,
        Err(_) => return Some(KeepReason::StatusUnreadable),
    };
    if let Some(prev) = index_before {
        restore_index_mtime(path, prev);
    }
    let mut has_unique = false;
    for line in out.lines() {
        if line.is_empty() {
            continue;
        }
        let rel = porcelain_path(line);
        if line.starts_with("??") || line.starts_with("!!") {
            if is_under_known_cache(path, &path.join(&rel)) {
                if cache_tree_has_denied_name(path, &rel, policy) {
                    has_unique = true;
                }
                continue;
            }
            has_unique = true;
        } else {
            return Some(KeepReason::DirtyWork);
        }
    }
    // Git omits unreadable ignored dirs from porcelain. Walk them anyway.
    if !has_unique {
        for name in CACHE_DIR_NAMES {
            if !path.join(name).is_dir() {
                continue;
            }
            if cache_tree_has_denied_name(path, Path::new(name), policy) {
                has_unique = true;
                break;
            }
        }
    }
    if has_unique {
        Some(KeepReason::UniqueUntracked)
    } else {
        None
    }
}

fn porcelain_path(line: &str) -> PathBuf {
    let rest = if line.len() >= 3 { &line[3..] } else { line };
    let rest = rest.trim();
    if let Some((_, dest)) = rest.split_once(" -> ") {
        PathBuf::from(dest.trim_matches('"'))
    } else {
        PathBuf::from(rest.trim_matches('"'))
    }
}

fn save_unique_commits(path: &Path, prefix: &str) -> Result<Vec<String>, GcError> {
    let reflog = git(path, &["reflog", "--format=%H"]).map_err(|e| match e {
        GcError::Git { detail, .. } => GcError::Git {
            op: "reflog".into(),
            detail,
        },
        other => other,
    })?;
    let name = path
        .file_name()
        .map(|s| s.to_string_lossy().into_owned())
        .unwrap_or_else(|| "worktree".into());
    let name = sanitize_ref_component(&name);
    let prefix = prefix.trim_end_matches('/');
    let mut saved = Vec::new();
    let mut seen = HashSet::new();
    for sha in reflog.lines() {
        let sha = sha.trim();
        if sha.len() < 7 || !seen.insert(sha.to_owned()) {
            continue;
        }
        if !commit_is_dangling(path, sha)? {
            continue;
        }
        let refname = format!("{prefix}/{name}/{sha}");
        git(path, &["update-ref", &refname, sha]).map_err(|e| match e {
            GcError::Git { detail, .. } => GcError::Git {
                op: "update-ref".into(),
                detail,
            },
            other => other,
        })?;
        saved.push(refname);
    }
    Ok(saved)
}

fn commit_is_dangling(path: &Path, sha: &str) -> Result<bool, GcError> {
    let contains = git(path, &["branch", "-a", "--contains", sha])?;
    Ok(!contains.lines().any(|l| !l.trim().is_empty()))
}

fn sanitize_ref_component(raw: &str) -> String {
    let s: String = raw
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
                c
            } else {
                '-'
            }
        })
        .collect();
    let s = s.trim_matches('-').to_owned();
    if s.is_empty() { "worktree".into() } else { s }
}

fn git_common_dir(path: &Path) -> Result<PathBuf, GcError> {
    let raw = git(path, &["rev-parse", "--git-common-dir"])?;
    let p = PathBuf::from(raw.trim());
    if p.is_absolute() {
        Ok(p)
    } else {
        Ok(path.join(p))
    }
}

/// `$repo/.git` -> `$repo`. Bare or `--separate-git-dir` store -> the git dir.
fn repo_cwd_from_common_dir(common: &Path) -> PathBuf {
    match common.file_name() {
        Some(name) if name == ".git" => common.parent().unwrap_or(common).to_path_buf(),
        _ => common.to_path_buf(),
    }
}

fn registry_unreadable(err: GcError) -> GcError {
    match err {
        GcError::Git { detail, .. } => GcError::RegistryUnreadable(detail),
        other => other,
    }
}

fn remove_worktree(repo: &Path, tree: &Path, unlock: bool) -> Result<(), GcError> {
    let path_s = tree
        .to_str()
        .ok_or_else(|| GcError::Git {
            op: "worktree remove".into(),
            detail: "non-utf8 path".into(),
        })?
        .to_owned();
    let args: Vec<&str> = if unlock {
        vec!["worktree", "remove", "--force", "--force", &path_s]
    } else {
        vec!["worktree", "remove", "--force", &path_s]
    };
    match git(repo, &args) {
        Ok(_) => Ok(()),
        Err(_) => {
            git(tree, &args)?;
            Ok(())
        }
    }
}

/// Host keys restored after `env_clear`. No `GIT_*` except the two we set.
const GIT_FORWARD_ENV: &[&str] = &[
    "PATH",
    "HOME",
    "USER",
    "LOGNAME",
    "LANG",
    "LC_ALL",
    "LC_CTYPE",
    "TZ",
    "TMPDIR",
    "TEMP",
    "TMP",
    "SYSTEMROOT",
    "SYSTEMDRIVE",
    "WINDIR",
    "USERPROFILE",
    "COMSPEC",
    "PATHEXT",
    "HOMEDRIVE",
    "HOMEPATH",
];

fn git(cwd: &Path, args: &[&str]) -> Result<String, GcError> {
    let mut cmd = Command::new("git");
    cmd.env_clear();
    for key in GIT_FORWARD_ENV {
        if let Some(v) = std::env::var_os(key) {
            cmd.env(key, v);
        }
    }
    let out = cmd
        .env("GIT_TERMINAL_PROMPT", "0")
        .env("GIT_CONFIG_NOSYSTEM", "1")
        .args(args)
        .current_dir(cwd)
        .output()
        .map_err(|e| GcError::Git {
            op: args.join(" "),
            detail: e.to_string(),
        })?;
    if !out.status.success() {
        return Err(GcError::Git {
            op: args.join(" "),
            detail: String::from_utf8_lossy(&out.stderr).trim().into(),
        });
    }
    Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}

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

    #[test]
    fn parse_primary_and_locked() {
        let text = "worktree /repo\nHEAD abc\nbranch refs/heads/main\n\nworktree /repo/.wt\nHEAD def\nlocked\n";
        let trees = parse_worktree_list(text);
        assert_eq!(trees.len(), 2);
        assert!(!trees[0].locked);
        assert!(trees[1].locked);
    }

    #[test]
    fn parse_max_age_rejects_bare_number() {
        match parse_max_age("7") {
            Err(GcError::InvalidDuration(msg)) => {
                assert!(msg.contains("30s"), "bare number must hint 30s: {msg}");
            }
            other => panic!("bare number must stay InvalidDuration, got {other:?}"),
        }
    }

    #[test]
    fn commit_is_dangling_git_failure_is_not_dangling() {
        let dir = tempfile::tempdir().expect("tmp");
        match commit_is_dangling(dir.path(), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") {
            Ok(false) | Err(_) => {}
            Ok(true) => panic!("git failure must not treat commit as dangling"),
        }
    }

    #[test]
    fn probe_failure_is_live_when_current_dir_is_elsewhere() {
        let tree = tempfile::tempdir().expect("tree");
        let elsewhere = tempfile::tempdir().expect("elsewhere");
        assert!(
            worktree_has_live_cwd_from(tree.path(), Some(elsewhere.path()), Err(())),
            "probe failure must keep the tree"
        );
        assert!(
            !worktree_has_live_cwd_from(tree.path(), Some(elsewhere.path()), Ok(&[])),
            "successful empty scan is not live"
        );
    }

    #[test]
    fn git_forward_env_has_no_git_keys() {
        for key in GIT_FORWARD_ENV {
            assert!(
                !key.starts_with("GIT_"),
                "forward list must not include {key}"
            );
        }
        assert!(GIT_FORWARD_ENV.contains(&"PATH"));
        assert!(GIT_FORWARD_ENV.contains(&"HOME") || GIT_FORWARD_ENV.contains(&"USERPROFILE"));
    }
}