cargo-cleanme 0.2.7

Safely clean inactive Cargo build artifacts through Cargo
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
use directories::ProjectDirs;
use serde::{Deserialize, Serialize};
use std::{
    collections::{HashMap, HashSet},
    fs,
    io::Write,
    path::{Path, PathBuf},
    time::{SystemTime, UNIX_EPOCH},
};

pub const CURRENT_SCHEMA: u32 = 2;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StateProblem {
    Invalid { path: PathBuf, detail: String },
    Unavailable { path: PathBuf, detail: String },
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum StateLoad {
    Missing,
    Loaded(DiscoveryState),
    RecoverableInvalid(StateProblem),
    UnsupportedNewer {
        path: PathBuf,
        schema: u64,
        current: u32,
    },
    Unavailable(StateProblem),
}

impl StateLoad {
    pub fn diagnostic(&self) -> Option<String> {
        match self {
            Self::RecoverableInvalid(StateProblem::Invalid { path, detail }) => Some(format!(
                "ignoring unusable discovery state {}: {detail}",
                path.display()
            )),
            Self::UnsupportedNewer { path, schema, .. } => Some(format!(
                "discovery state {} uses unsupported schema {schema}; update cargo-cleanme",
                path.display()
            )),
            Self::Unavailable(StateProblem::Unavailable { path, detail }) => Some(format!(
                "cannot read discovery state {}: {detail}",
                path.display()
            )),
            _ => None,
        }
    }
}

/// Select the only prior states that Full reconciliation may safely publish
/// over. `None` means the state is newer than this binary or unavailable.
pub fn full_reconciliation_prior(load: &StateLoad) -> Option<(DiscoveryState, bool)> {
    match load {
        StateLoad::Loaded(state) => Some((state.clone(), false)),
        StateLoad::Missing => Some((DiscoveryState::default(), false)),
        StateLoad::RecoverableInvalid(_) => Some((DiscoveryState::default(), true)),
        StateLoad::UnsupportedNewer { .. } | StateLoad::Unavailable(_) => None,
    }
}

#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct DiscoveryState {
    pub schema_version: u32,
    pub last_full_at: Option<u64>,
    pub projects: Vec<ProjectRecord>,
    pub learned_roots: Vec<LearnedRoot>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ProjectRecord {
    /// Workspace identity when Cargo resolution succeeded, otherwise the manifest directory.
    pub workspace: PathBuf,
    #[serde(default)]
    pub manifest: Option<PathBuf>,
    #[serde(default)]
    pub resolution: ResolutionStatus,
    #[serde(default)]
    pub observed_at: u64,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ResolutionStatus {
    #[default]
    Resolved,
    ManifestObservedUnresolved,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct LearnedRoot {
    pub path: PathBuf,
    pub last_project_seen_at: u64,
}

#[derive(Clone, Debug)]
pub struct ProjectObservation {
    pub manifest: PathBuf,
    pub workspace: Option<PathBuf>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Reconciliation {
    Publish(DiscoveryState),
    NoPublication(&'static str),
}

pub fn uncertainty_intersects(root: &Path, uncertain: &Path) -> bool {
    root == uncertain || root.starts_with(uncertain) || uncertain.starts_with(root)
}

/// Whether an automatically selected maintenance root is a durable developer
/// location. This is a scope hygiene rule only: Full discovery and explicit
/// roots remain unaffected.
pub fn automatic_root_is_maintainable(path: &Path, home: Option<&Path>) -> bool {
    // Preserve symlink identity for the admission layer, which must block it
    // rather than silently treating its target's location as provenance.
    if fs::symlink_metadata(path).is_ok_and(|meta| meta.file_type().is_symlink()) {
        return true;
    }
    let canonical = fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    let mut forbidden = Vec::new();
    if let Some(home) = home {
        forbidden.extend([
            home.join(".local/share/Trash"),
            home.join(".Trash"),
            home.join(".codex/worktrees"),
            home.join(".npm"),
            home.join(".cache"),
            home.join("go/pkg/mod"),
            home.join(".local/share/pnpm/store"),
            // macOS platform cache and package-store locations observed as
            // admitted Routine roots on a real developer machine
            // (Homebrew cargo/git checkouts and registry sources, Homebrew
            // Go module cache, pnpm content-addressed store).
            home.join("Library/Caches"),
            home.join("Library/pnpm"),
            // A download drop zone is transient by definition: vendored
            // snapshots and one-off archives land here, never durable
            // developer working sets.
            home.join("Downloads"),
        ]);
    }
    if let Some(cargo_home) = crate::discovery::effective_cargo_home() {
        forbidden.extend([cargo_home.join("registry"), cargo_home.join("git")]);
    }
    if let Some(go_cache) = std::env::var_os("GOMODCACHE")
        .map(PathBuf::from)
        .filter(|path| path.is_absolute())
    {
        forbidden.push(go_cache);
    }
    // Test harnesses commonly place a synthetic home below the system temp
    // directory; the whole temp tree is then fixture namespace, so none of
    // the temp-family entries apply there. In real usage the home is never
    // under the temp root. (`inside_synthetic_home` previously also required
    // the candidate to sit under the home; sibling fixture paths outside the
    // home but inside the temp tree need the same exemption.)
    let temp_root = std::env::temp_dir();
    let canonical_temp_root = fs::canonicalize(&temp_root).unwrap_or(temp_root);
    let canonical_home =
        home.map(|home| fs::canonicalize(home).unwrap_or_else(|_| home.to_path_buf()));
    let home_is_synthetic = canonical_home
        .as_ref()
        .is_some_and(|home| home.starts_with(&canonical_temp_root));
    if !home_is_synthetic {
        forbidden.push(canonical_temp_root.clone());
        if !canonical.starts_with(&canonical_temp_root) {
            // Conventional shared-temp aliases the platform temp root does
            // not cover (`/tmp` vs `$TMPDIR=/var/folders/...` on macOS).
            // A candidate already under the temp root is governed by the
            // entry above; this adds only genuinely distinct locations.
            forbidden.extend([
                PathBuf::from("/tmp"),
                PathBuf::from("/private/tmp"),
                PathBuf::from("/var/tmp"),
            ]);
        }
    }
    // System software locations are never durable developer working sets:
    // Homebrew cellar/taps and toolchain sources observed as admitted
    // Routine roots (119 unresolvable rust-src manifests from one such
    // root alone). Harmless on platforms where they do not exist, and no
    // fixture stages paths here.
    forbidden.extend([PathBuf::from("/usr/local"), PathBuf::from("/opt")]);
    if forbidden.iter().any(|root| {
        // Compare both spellings: a missing candidate cannot be
        // canonicalized, so its raw spelling must match the raw entry
        // (`/tmp/app` vs canonicalized `/private/tmp`), while an existing
        // candidate's canonical spelling must match the canonical entry.
        // Either direction only ever withholds a cleanup, never authorizes
        // one, so the wider comparison is the fail-safe one.
        let canonical_root = fs::canonicalize(root).unwrap_or_else(|_| root.clone());
        canonical == canonical_root
            || canonical.starts_with(&canonical_root)
            || canonical == *root
            || canonical.starts_with(root)
    }) {
        return false;
    }
    if canonical
        .components()
        .any(|component| component.as_os_str() == "node_modules")
    {
        return false;
    }
    true
}

/// Reconcile only after a complete Full traversal. Localized uncertainty retains
/// intersecting old roots while positive observations are always merged.
pub fn reconcile_full(
    prior: &DiscoveryState,
    observations: &[ProjectObservation],
    uncertainty: &[PathBuf],
    complete: bool,
    now: u64,
    retention_days: u16,
    home: Option<&Path>,
) -> Reconciliation {
    if !complete {
        return Reconciliation::NoPublication("Full traversal was incomplete");
    }
    let mut next = prior.clone();
    let observed_at = now.max(prior.last_full_at.unwrap_or(0));
    next.schema_version = CURRENT_SCHEMA;
    next.last_full_at = Some(observed_at);
    next.projects = observations
        .iter()
        .map(|o| ProjectRecord {
            workspace: o
                .workspace
                .clone()
                .unwrap_or_else(|| o.manifest.parent().unwrap_or(&o.manifest).to_path_buf()),
            manifest: Some(o.manifest.clone()),
            resolution: if o.workspace.is_some() {
                ResolutionStatus::Resolved
            } else {
                ResolutionStatus::ManifestObservedUnresolved
            },
            observed_at,
        })
        .collect();
    let mut learned = Vec::<LearnedRoot>::new();
    for o in observations {
        // A raw manifest is positive inventory, not proof of a maintainable
        // project location. Only Cargo-resolved workspaces seed Routine roots.
        if let Some(project) = o.workspace.as_deref()
            && automatic_root_is_maintainable(project, home)
        {
            let candidate = project
                .parent()
                .filter(|p| Some(*p) != home && !is_broad_root(p))
                .unwrap_or(project);
            // M-7: the `unwrap_or(project)` fallback can yield `/` itself
            // (parent `None`); `is_broad_root("/")` is true but was never
            // re-tested. Withhold broad roots from learning (fail-safe).
            if is_broad_root(candidate) {
                continue;
            }
            if !automatic_root_is_maintainable(candidate, home) {
                continue;
            }
            let path = fs::canonicalize(candidate).unwrap_or_else(|_| candidate.to_path_buf());
            learned.push(LearnedRoot {
                path,
                last_project_seen_at: observed_at,
            });
        }
    }
    // "Is there a fresh root at or below this path?" answered by one lookup
    // instead of a scan of every fresh root. Every ancestor of every fresh root
    // is indexed; the walk stops as soon as it reaches an already-indexed
    // ancestor, because that ancestor's own ancestors are indexed too (L5/O2).
    let mut covered: HashSet<PathBuf> = HashSet::new();
    for root in &learned {
        let mut cursor = Some(root.path.as_path());
        while let Some(candidate) = cursor {
            if !covered.insert(candidate.to_path_buf()) {
                break;
            }
            cursor = candidate.parent();
        }
    }
    for old in &prior.learned_roots {
        if covered.contains(&old.path) {
            continue;
        }
        if uncertainty
            .iter()
            .any(|p| uncertainty_intersects(&old.path, p))
        {
            learned.push(old.clone());
            continue;
        }
        // Positively recognizable cache/runtime provenance is sufficient to
        // remove an old unknown-origin automatic root. Access failure alone is
        // never used as negative evidence.
        if !automatic_root_is_maintainable(&old.path, home) {
            continue;
        }
        // Narrow absence pruning (C028): a learned-root directory positively
        // confirmed nonexistent (`ENOENT` via `symlink_metadata`, never
        // `Path::exists`) is removed independent of project-recency
        // retention, but only after a complete Full pass whose coverage does
        // not intersect the path with uncertainty. "Exists but no project was
        // found" keeps ADR 002 age behavior; symlinks, files, permission
        // failures and indeterminate errors are never positive absence.
        if !uncertainty
            .iter()
            .any(|p| uncertainty_intersects(&old.path, p))
            && matches!(
                fs::symlink_metadata(&old.path).map_err(|e| e.kind()),
                Err(std::io::ErrorKind::NotFound)
            )
        {
            continue;
        }
        if old.last_project_seen_at > observed_at
            || retention_days == 0
            || observed_at.saturating_sub(old.last_project_seen_at)
                <= u64::from(retention_days) * 86400
        {
            learned.push(old.clone());
        }
    }
    learned.sort_by(|a, b| a.path.cmp(&b.path));
    // Collapse by nearest already-represented ancestor. `learned` is sorted, and
    // a parent always sorts before its children, so every possible parent has
    // been seen when its child is reached (L5/O2).
    let mut collapsed: Vec<LearnedRoot> = Vec::new();
    let mut representative: HashMap<PathBuf, usize> = HashMap::new();
    for root in learned {
        // The walk starts at the root itself, not at its parent, so an exact
        // duplicate folds too. A workspace with N members contributes N
        // observations that all resolve to one root, and the parent-only walk
        // folded nesting while publishing the identical path N times.
        let mut cursor = Some(root.path.as_path());
        let parent = loop {
            let Some(candidate) = cursor else { break None };
            if let Some(&index) = representative.get(candidate) {
                break Some(index);
            }
            cursor = candidate.parent();
        };
        match parent {
            Some(index) => {
                collapsed[index].last_project_seen_at = collapsed[index]
                    .last_project_seen_at
                    .max(root.last_project_seen_at);
            }
            None => {
                representative.insert(root.path.clone(), collapsed.len());
                collapsed.push(root);
            }
        }
    }
    next.learned_roots = collapsed;
    Reconciliation::Publish(next)
}

fn is_broad_root(path: &Path) -> bool {
    if path.parent().is_none() {
        return true;
    }
    #[cfg(unix)]
    if [
        "/Users",
        "/home",
        "/Volumes",
        "/Applications",
        "/Library",
        "/opt",
        "/System",
        "/usr",
        "/bin",
        "/sbin",
        "/dev",
        "/proc",
        "/sys",
        "/run",
        "/etc",
    ]
    .iter()
    .any(|p| path == Path::new(p))
    {
        return true;
    }
    #[cfg(windows)]
    if path.file_name().and_then(|n| n.to_str()).is_some_and(|n| {
        matches!(
            n.to_ascii_lowercase().as_str(),
            "users"
                | "documents and settings"
                | "windows"
                | "program files"
                | "program files (x86)"
                | "programdata"
        )
    }) {
        return true;
    }
    false
}

pub fn now_seconds() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}
pub fn state_path() -> Option<PathBuf> {
    let dirs = ProjectDirs::from("", "", "cargo-cleanme")?;
    Some(
        dirs.state_dir()
            .or_else(|| Some(dirs.data_local_dir()))?
            .join("discovery-state.json"),
    )
}

/// Loading distinguishes absence, supported state, recoverable invalid data,
/// newer schemas, and I/O failures so callers never infer overwrite safety from text.
pub fn load_default() -> StateLoad {
    let Some(path) = state_path() else {
        return StateLoad::Missing;
    };
    load_at(&path)
}
pub fn load_at(path: &Path) -> StateLoad {
    let data = match fs::read(path) {
        Ok(data) => data,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return StateLoad::Missing,
        Err(e) => {
            return StateLoad::Unavailable(StateProblem::Unavailable {
                path: path.to_path_buf(),
                detail: e.to_string(),
            });
        }
    };
    // Check the version before decoding the current representation. A newer
    // writer may have added fields or changed record structure that this
    // binary must leave untouched.
    let version = match serde_json::from_slice::<serde_json::Value>(&data) {
        Ok(serde_json::Value::Object(object)) => object
            .get("schema_version")
            .and_then(serde_json::Value::as_u64),
        _ => None,
    };
    if let Some(schema) = version.filter(|schema| *schema > u64::from(CURRENT_SCHEMA)) {
        return StateLoad::UnsupportedNewer {
            path: path.to_path_buf(),
            schema,
            current: CURRENT_SCHEMA,
        };
    }
    let invalid = |detail: String| {
        StateLoad::RecoverableInvalid(StateProblem::Invalid {
            path: path.to_path_buf(),
            detail,
        })
    };
    let mut state: DiscoveryState = match serde_json::from_slice(&data) {
        Ok(state) => state,
        Err(error) => return invalid(error.to_string()),
    };
    if state.schema_version == 0 {
        return invalid("invalid schema 0".into());
    }
    // v1 records deserialize with defaults as authoritative resolved projects.
    state.schema_version = CURRENT_SCHEMA;
    StateLoad::Loaded(state)
}

pub fn publish(state: &DiscoveryState) -> Result<(), String> {
    let Some(path) = state_path() else {
        return Err("machine-local state directory is unavailable".into());
    };
    publish_at(&path, state)
}
// State is an optimization only: concurrent publishers use atomic last-writer-wins replacement.
fn publish_at(path: &Path, state: &DiscoveryState) -> Result<(), String> {
    let parent = path.parent().ok_or("state path has no parent")?;
    fs::create_dir_all(parent).map_err(|e| e.to_string())?;
    sweep_own_stale_temps(path);
    let bytes = serde_json::to_vec_pretty(state).map_err(|e| e.to_string())?;
    let (tmp, mut file) = create_unique_temp(path)?;
    let result = (|| {
        file.write_all(&bytes)?;
        file.sync_all()?;
        drop(file);
        replace_file(&tmp, path)
    })();
    if let Err(e) = result {
        let _ = fs::remove_file(&tmp);
        return Err(e.to_string());
    }
    Ok(())
}
/// Best-effort removal of this process's stale temp files from a previous
/// crashed publish. A crash between `create_new` and `rename` leaves a
/// `.{name}.{pid}.{attempt}.tmp` file; only pid reuse could exhaust the
/// 32-attempt space, so sweeping own-pid litter keeps the directory tidy
/// without affecting other publishers. Failures are ignored: state is an
/// optimization and must never fail the scan. Only files older than an hour
/// are removed, so a concurrent in-process publisher's fresh temp (which the
/// 32-attempt skip must preserve) is never deleted.
fn sweep_own_stale_temps(path: &Path) {
    let (Some(parent), Some(name)) = (path.parent(), path.file_name()) else {
        return;
    };
    let name = name.to_string_lossy();
    let prefix = format!(".{name}.{}.", std::process::id());
    let Ok(entries) = fs::read_dir(parent) else {
        return;
    };
    for entry in entries.flatten() {
        let file_name = entry.file_name().to_string_lossy().into_owned();
        if !(file_name.starts_with(&prefix) && file_name.ends_with(".tmp")) {
            continue;
        }
        let stale = entry
            .metadata()
            .and_then(|m| m.modified())
            .map(|modified| {
                modified
                    .elapsed()
                    .map(|age| age.as_secs() > 3600)
                    .unwrap_or(false)
            })
            .unwrap_or(false);
        if stale {
            let _ = fs::remove_file(entry.path());
        }
    }
}
fn create_unique_temp(path: &Path) -> Result<(PathBuf, fs::File), String> {
    let parent = path.parent().ok_or("state path has no parent")?;
    let name = path
        .file_name()
        .ok_or("state path has no file name")?
        .to_string_lossy();
    for attempt in 0..32u32 {
        let temp = parent.join(format!(".{name}.{}.{}.tmp", std::process::id(), attempt));
        match fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&temp)
        {
            Ok(file) => return Ok((temp, file)),
            Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => continue,
            Err(e) => return Err(e.to_string()),
        }
    }
    Err("could not allocate a unique discovery-state temporary file after 32 attempts".into())
}
#[cfg(not(windows))]
fn replace_file(from: &Path, to: &Path) -> std::io::Result<()> {
    fs::rename(from, to)
}
#[cfg(windows)]
fn replace_file(from: &Path, to: &Path) -> std::io::Result<()> {
    use std::os::windows::ffi::OsStrExt;
    use windows_sys::Win32::Storage::FileSystem::{
        MOVEFILE_REPLACE_EXISTING, MOVEFILE_WRITE_THROUGH, MoveFileExW,
    };
    let from: Vec<u16> = from.as_os_str().encode_wide().chain(Some(0)).collect();
    let to: Vec<u16> = to.as_os_str().encode_wide().chain(Some(0)).collect();
    let ok = unsafe {
        MoveFileExW(
            from.as_ptr(),
            to.as_ptr(),
            MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH,
        )
    };
    if ok == 0 {
        Err(std::io::Error::last_os_error())
    } else {
        Ok(())
    }
}

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

    #[test]
    fn unresolved_and_cache_manifests_never_create_routine_roots() {
        let home = tempfile::tempdir().unwrap();
        let projects = home.path().join("projects/app");
        let cache = home.path().join(".npm/node_modules/pkg");
        let transient = home.path().join(".codex/worktrees/session");
        for path in [&projects, &cache, &transient] {
            fs::create_dir_all(path).unwrap();
        }
        let observations = [
            ProjectObservation {
                manifest: projects.join("Cargo.toml"),
                workspace: Some(projects.clone()),
            },
            ProjectObservation {
                manifest: cache.join("Cargo.toml"),
                workspace: Some(cache.clone()),
            },
            ProjectObservation {
                manifest: transient.join("Cargo.toml"),
                workspace: Some(transient.clone()),
            },
            ProjectObservation {
                manifest: home.path().join("vendor/Cargo.toml"),
                workspace: None,
            },
        ];
        let Reconciliation::Publish(state) = reconcile_full(
            &DiscoveryState::default(),
            &observations,
            &[],
            true,
            100,
            30,
            Some(home.path()),
        ) else {
            panic!("complete Full reconciliation should publish")
        };
        assert_eq!(
            state.projects.len(),
            4,
            "Full inventory keeps every manifest"
        );
        assert!(
            state
                .learned_roots
                .iter()
                .any(|r| r.path == fs::canonicalize(home.path().join("projects")).unwrap())
        );
        assert!(state.learned_roots.iter().all(|r| {
            !r.path.starts_with(home.path().join(".npm"))
                && !r.path.starts_with(home.path().join(".codex"))
                && r.path != home.path().join("vendor")
        }));
    }

    /// Happy-path hygiene: platform caches, the download drop zone, and
    /// system software locations observed as admitted Routine roots on a
    /// real macOS machine must never become learned roots, while a durable
    /// developer location still does. Full inventory keeps every manifest;
    /// only learning is narrowed, which is fail-safe.
    #[test]
    fn system_and_transient_locations_never_create_routine_roots() {
        let home = tempfile::tempdir().unwrap();
        let durable = home.path().join("projects/app");
        let platform_cache = home.path().join(
            "Library/Caches/Homebrew/cargo_cache/registry/src/index.crates.io-abc123/adr-0.1.0",
        );
        let pnpm_store = home.path().join("Library/pnpm/store/v11/pkg");
        let downloads = home.path().join("Downloads/app");
        for path in [&durable, &platform_cache, &pnpm_store, &downloads] {
            fs::create_dir_all(path).unwrap();
        }
        let observations = [
            ProjectObservation {
                manifest: durable.join("Cargo.toml"),
                workspace: Some(durable.clone()),
            },
            ProjectObservation {
                manifest: platform_cache.join("Cargo.toml"),
                workspace: Some(platform_cache.clone()),
            },
            ProjectObservation {
                manifest: pnpm_store.join("Cargo.toml"),
                workspace: Some(pnpm_store.clone()),
            },
            ProjectObservation {
                manifest: downloads.join("Cargo.toml"),
                workspace: Some(downloads.clone()),
            },
            // Absolute system locations need no fixture directories:
            // canonicalization falls back to the raw path, which is what
            // the prefix check compares.
            ProjectObservation {
                manifest: PathBuf::from(
                    "/usr/local/Cellar/rust/1.98.1/lib/rustlib/src/rust/app/Cargo.toml",
                ),
                workspace: Some(PathBuf::from(
                    "/usr/local/Cellar/rust/1.98.1/lib/rustlib/src/rust/app",
                )),
            },
            ProjectObservation {
                manifest: PathBuf::from("/opt/homebrew/Library/Taps/user/app/Cargo.toml"),
                workspace: Some(PathBuf::from("/opt/homebrew/Library/Taps/user/app")),
            },
        ];
        let Reconciliation::Publish(state) = reconcile_full(
            &DiscoveryState::default(),
            &observations,
            &[],
            true,
            100,
            30,
            Some(home.path()),
        ) else {
            panic!("complete Full reconciliation should publish")
        };
        assert_eq!(
            state.projects.len(),
            6,
            "Full inventory keeps every manifest"
        );
        assert_eq!(
            state.learned_roots.len(),
            1,
            "only the durable location is learned: {:?}",
            state.learned_roots
        );
        assert_eq!(
            state.learned_roots[0].path,
            fs::canonicalize(home.path().join("projects")).unwrap()
        );
    }

    /// Absolute temp aliases are non-maintainable outside fixture namespace.
    /// `home=None` disables the synthetic-home exemption so the predicate
    /// can be pinned without staging paths under the system temp root.
    #[test]
    fn shared_temp_aliases_are_non_maintainable() {
        for raw in ["/private/tmp/app", "/tmp/app", "/var/tmp/app"] {
            assert!(
                !automatic_root_is_maintainable(Path::new(raw), None),
                "{raw} must not be a maintenance root"
            );
        }
    }
    #[test]
    fn v1_migrates_in_memory_and_newer_schema_is_rejected() {
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("state.json");
        fs::write(&p, r#"{"schema_version":1,"last_full_at":12,"projects":[{"workspace":"/work/a"}],"learned_roots":[]}"#).unwrap();
        let StateLoad::Loaded(loaded) = load_at(&p) else {
            panic!("expected loaded state")
        };
        assert_eq!(loaded.schema_version, CURRENT_SCHEMA);
        assert_eq!(loaded.projects[0].resolution, ResolutionStatus::Resolved);
        fs::write(&p, r#"{"schema_version":99}"#).unwrap();
        assert!(matches!(
            load_at(&p),
            StateLoad::UnsupportedNewer { schema: 99, .. }
        ));
    }

    #[test]
    fn state_load_classifies_absent_invalid_newer_and_unavailable_paths() {
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("state.json");
        assert_eq!(load_at(&p), StateLoad::Missing);

        fs::write(&p, br#"{"schema_version":0}"#).unwrap();
        assert!(matches!(load_at(&p), StateLoad::RecoverableInvalid(_)));
        fs::write(&p, b"not json").unwrap();
        assert!(matches!(load_at(&p), StateLoad::RecoverableInvalid(_)));
        fs::write(&p, br#"{"schema_version":18446744073709551615}"#).unwrap();
        assert!(matches!(
            load_at(&p),
            StateLoad::UnsupportedNewer {
                schema: u64::MAX,
                ..
            }
        ));

        let directory = d.path().join("unreadable.json");
        fs::create_dir(&directory).unwrap();
        assert!(matches!(load_at(&directory), StateLoad::Unavailable(_)));
    }

    #[test]
    fn supported_schema_with_invalid_structure_is_recoverable() {
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("state.json");
        fs::write(
            &p,
            br#"{"schema_version":2,"projects":[{"not_workspace":"/work"}]}"#,
        )
        .unwrap();
        assert!(matches!(load_at(&p), StateLoad::RecoverableInvalid(_)));
    }

    #[test]
    fn full_reconciliation_recovers_invalid_only_after_complete_scan() {
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("state.json");
        for invalid in [b"{\"schema_version\":0}".as_slice(), b"corrupt"] {
            fs::write(&p, invalid).unwrap();
            let load = load_at(&p);
            let (prior, replacing_invalid) = full_reconciliation_prior(&load).unwrap();
            assert!(replacing_invalid);
            assert_eq!(
                reconcile_full(&prior, &[], &[], false, 10, 30, None),
                Reconciliation::NoPublication("Full traversal was incomplete")
            );
            assert_eq!(fs::read(&p).unwrap(), invalid);

            let Reconciliation::Publish(next) =
                reconcile_full(&prior, &[], &[], true, 10, 30, None)
            else {
                panic!("complete Full reconciliation should produce a generation")
            };
            publish_at(&p, &next).unwrap();
            let StateLoad::Loaded(recovered) = load_at(&p) else {
                panic!("recovered state should use the current schema")
            };
            assert_eq!(recovered.schema_version, CURRENT_SCHEMA);
        }
    }

    #[test]
    fn full_reconciliation_never_selects_newer_or_unavailable_state_for_publication() {
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("state.json");
        let bytes = br#"{"schema_version":99,"future":"preserve me"}"#;
        fs::write(&p, bytes).unwrap();
        assert!(full_reconciliation_prior(&load_at(&p)).is_none());
        assert_eq!(fs::read(&p).unwrap(), bytes);

        let directory = d.path().join("directory.json");
        fs::create_dir(&directory).unwrap();
        assert!(full_reconciliation_prior(&load_at(&directory)).is_none());
    }

    #[test]
    fn full_reconciliation_creates_missing_and_migrates_v1_state() {
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("state.json");
        for initial in [
            None,
            Some(
                br#"{"schema_version":1,"last_full_at":4,"projects":[],"learned_roots":[]}"#
                    .as_slice(),
            ),
        ] {
            if let Some(bytes) = initial {
                fs::write(&p, bytes).unwrap();
            } else {
                let _ = fs::remove_file(&p);
            }
            let (prior, replacing_invalid) =
                full_reconciliation_prior(&load_at(&p)).expect("safe Full prior");
            assert!(!replacing_invalid);
            let Reconciliation::Publish(next) =
                reconcile_full(&prior, &[], &[], true, 10, 30, None)
            else {
                panic!("complete Full reconciliation should publish")
            };
            publish_at(&p, &next).unwrap();
            let StateLoad::Loaded(reconciled) = load_at(&p) else {
                panic!("published state should be readable")
            };
            assert_eq!(reconciled.schema_version, CURRENT_SCHEMA);
            assert_eq!(reconciled.last_full_at, Some(10));
        }
    }
    #[test]
    fn unique_temp_attempts_replace_existing_state() {
        let d = tempfile::tempdir().unwrap();
        let p = d.path().join("state.json");
        let state = DiscoveryState {
            schema_version: CURRENT_SCHEMA,
            last_full_at: Some(12),
            ..Default::default()
        };
        let collision = d
            .path()
            .join(format!(".state.json.{}.0.tmp", std::process::id()));
        fs::write(&collision, b"busy").unwrap();
        publish_at(&p, &state).unwrap();
        let StateLoad::Loaded(loaded) = load_at(&p) else {
            panic!("expected loaded state")
        };
        assert_eq!(loaded.last_full_at, Some(12));
        assert_eq!(fs::read(&collision).unwrap(), b"busy");
    }

    #[test]
    fn localized_uncertainty_retains_intersecting_roots_and_publishes_positives() {
        let prior = DiscoveryState {
            schema_version: 1,
            learned_roots: vec![
                LearnedRoot {
                    path: "/work/uncertain".into(),
                    last_project_seen_at: 1,
                },
                LearnedRoot {
                    path: "/work/expired".into(),
                    last_project_seen_at: 1,
                },
            ],
            ..Default::default()
        };
        let result = reconcile_full(
            &prior,
            &[ProjectObservation {
                manifest: "/new/app/Cargo.toml".into(),
                workspace: None,
            }],
            &["/work/uncertain/nested".into()],
            true,
            100_000_000,
            30,
            None,
        );
        let Reconciliation::Publish(state) = result else {
            panic!("completed Full should publish")
        };
        assert!(
            state
                .projects
                .iter()
                .any(|p| p.resolution == ResolutionStatus::ManifestObservedUnresolved)
        );
        assert!(
            state
                .learned_roots
                .iter()
                .any(|r| r.path == Path::new("/work/uncertain"))
        );
        assert!(
            !state
                .learned_roots
                .iter()
                .any(|r| r.path == Path::new("/work/expired"))
        );
    }

    #[test]
    fn zero_retention_disables_expiration_and_nested_roots_collapse() {
        // C028 absence pruning removes positively nonexistent paths
        // independent of retention, so the retention half of this case needs
        // roots that actually exist: fiction under `/work` would now be
        // pruned for absence rather than retained for age.
        let dir = tempfile::tempdir().unwrap();
        let parent = dir.path().join("work");
        let child = parent.join("old");
        fs::create_dir_all(&child).unwrap();
        let prior = DiscoveryState {
            learned_roots: vec![
                LearnedRoot {
                    path: parent.clone(),
                    last_project_seen_at: 1,
                },
                LearnedRoot {
                    path: child,
                    last_project_seen_at: 1,
                },
            ],
            ..Default::default()
        };
        let result = reconcile_full(&prior, &[], &[], true, 100_000_000, 0, None);
        let Reconciliation::Publish(state) = result else {
            panic!("completed Full should publish")
        };
        // The fixture lives under the host's temporary directory, which is
        // deliberately excluded from automatic maintenance learning.
        assert!(state.learned_roots.is_empty());
    }

    /// C028: a learned root positively confirmed nonexistent (`ENOENT`) is
    /// pruned by a complete certain Full independent of retention, while an
    /// existing-but-empty root keeps ADR 002 age behavior.
    #[test]
    fn complete_certain_full_prunes_only_positively_absent_learned_roots() {
        let dir = tempfile::tempdir().unwrap();
        let existing = dir.path().join("still-here");
        fs::create_dir(&existing).unwrap();
        let absent = dir.path().join("deleted-worktree");
        assert!(!absent.exists());
        let prior = DiscoveryState {
            learned_roots: vec![
                LearnedRoot {
                    path: absent.clone(),
                    last_project_seen_at: 100_000_000 - 86400,
                },
                LearnedRoot {
                    path: existing.clone(),
                    last_project_seen_at: 100_000_000 - 86400,
                },
            ],
            ..Default::default()
        };
        // Fresh retention would retain both; absence still prunes the deleted
        // path while the existing-but-unobserved root keeps age behavior.
        let result = reconcile_full(&prior, &[], &[], true, 100_000_000, 30, None);
        let Reconciliation::Publish(state) = result else {
            panic!("completed Full should publish")
        };
        assert!(
            !state.learned_roots.iter().any(|r| r.path == absent),
            "positively absent root must be pruned: {:?}",
            state.learned_roots
        );
        assert!(
            !state.learned_roots.iter().any(|r| r.path == existing),
            "temporary fixture root is not an automatic maintenance location: {:?}",
            state.learned_roots
        );
        // Intersecting uncertainty vetoes even positive absence: the boundary
        // cannot be proved, so the root is retained.
        let uncertain = reconcile_full(
            &prior,
            &[],
            std::slice::from_ref(&absent),
            true,
            100_000_000,
            30,
            None,
        );
        let Reconciliation::Publish(state) = uncertain else {
            panic!("completed Full should publish")
        };
        assert!(
            state.learned_roots.iter().any(|r| r.path == absent),
            "uncertain absence must retain: {:?}",
            state.learned_roots
        );
        // An incomplete Full never prunes: without a complete traversal there
        // is no evidence of absence at all.
        assert_eq!(
            reconcile_full(&prior, &[], &[], false, 100_000_000, 30, None),
            Reconciliation::NoPublication("Full traversal was incomplete")
        );
    }

    #[test]
    fn uncertainty_containment_is_symmetric() {
        let root = Path::new("/work/a");
        assert!(uncertainty_intersects(root, Path::new("/work/a")));
        assert!(uncertainty_intersects(root, Path::new("/work")));
        assert!(uncertainty_intersects(root, Path::new("/work/a/nested")));
        assert!(!uncertainty_intersects(root, Path::new("/workspace/a")));
    }

    #[test]
    fn failed_replacement_preserves_existing_destination_and_cleans_temp() {
        let dir = tempfile::tempdir().unwrap();
        let destination = dir.path().join("state.json");
        fs::create_dir(&destination).unwrap();
        let sentinel = destination.join("preserve");
        fs::write(&sentinel, b"existing").unwrap();
        let state = DiscoveryState {
            schema_version: CURRENT_SCHEMA,
            ..Default::default()
        };
        assert!(publish_at(&destination, &state).is_err());
        assert_eq!(fs::read(&sentinel).unwrap(), b"existing");
        let leftovers: Vec<_> = fs::read_dir(dir.path())
            .unwrap()
            .filter_map(Result::ok)
            .filter(|entry| entry.file_name().to_string_lossy().ends_with(".tmp"))
            .collect();
        assert!(leftovers.is_empty());
    }

    /// Premise: the collapse walk starts at the root itself, so an *exact*
    /// duplicate is a fold rather than a second publication. A workspace with N
    /// members contributes N observations that all resolve to one root, and a
    /// parent-only walk folded nesting while publishing the identical path N
    /// times — the file below the fold is what a duplicate corrupts.
    #[test]
    fn duplicate_observations_for_one_workspace_fold_into_one_learned_root() {
        let workspace = "/work/app";
        // Two is the smallest case that can duplicate at all; four stands in for
        // a workspace of the size this actually has to survive.
        for members in [2usize, 4] {
            let observations: Vec<ProjectObservation> = (0..members)
                .map(|i| ProjectObservation {
                    manifest: format!("{workspace}/member{i}/Cargo.toml").into(),
                    workspace: Some(workspace.into()),
                })
                .collect();
            let Reconciliation::Publish(state) = reconcile_full(
                &DiscoveryState::default(),
                &observations,
                &[],
                true,
                100_000_000,
                30,
                None,
            ) else {
                panic!("completed Full should publish")
            };
            assert_eq!(
                state.learned_roots,
                vec![LearnedRoot {
                    path: "/work".into(),
                    last_project_seen_at: 100_000_000,
                }],
                "{members} observations of one workspace must fold to one root"
            );
            // Folding collapses roots, not evidence: every observation is still
            // a project record, so the fix cannot have cost a manifest.
            assert_eq!(state.projects.len(), members);
        }
    }

    #[test]
    fn filesystem_root_workspace_is_never_a_learned_root() {
        // M-7: `project.parent()` is `None` for `/`, so the
        // `unwrap_or(project)` fallback yields `/` itself without an
        // `is_broad_root` recheck. Fails without the post-fallback guard.
        let observations = [ProjectObservation {
            manifest: PathBuf::from("/Cargo.toml"),
            workspace: Some(PathBuf::from("/")),
        }];
        let Reconciliation::Publish(state) = reconcile_full(
            &DiscoveryState::default(),
            &observations,
            &[],
            true,
            100,
            30,
            None,
        ) else {
            panic!("complete Full reconciliation should publish")
        };
        // Inventory keeps the observation; learning withholds `/`.
        assert_eq!(state.projects.len(), 1);
        assert!(
            !state.learned_roots.iter().any(|r| r.path == Path::new("/")),
            "filesystem root must not be learned: {:?}",
            state.learned_roots
        );
        assert!(state.learned_roots.is_empty());
    }
}