Skip to main content

magi/
retention.rs

1//! Bounded reclamation of retained attempts and Cargo outputs. Source recovery
2//! and build eviction have separate authority: evicting a target never retires a run.
3mod copies;
4pub mod reconcile;
5
6pub(crate) fn has_archived_seats(home: &Path, state: &RunState) -> Result<bool> {
7    let artifacts = home.join("runs").join(&state.id).join("artifacts");
8    if !artifacts.try_exists()? {
9        return Ok(false);
10    }
11    ensure!(
12        plain_dir(&artifacts) && artifacts.canonicalize()? == artifacts,
13        "noncanonical artifacts"
14    );
15    for (i, entry) in std::fs::read_dir(artifacts)?.enumerate() {
16        ensure!(i < 4096, "eviction record inspection limit exceeded");
17        let name = entry?.file_name();
18        let name = name.to_string_lossy();
19        if name.starts_with("eviction-") && name.ends_with(".json") {
20            return Ok(true);
21        }
22    }
23    Ok(false)
24}
25
26/// Re-materialize evicted read seats before execution can consume their paths.
27/// Caller owns the run's driver/owner leases and repository coordination.
28pub(crate) async fn restore_execution_seats(
29    home: &Path,
30    state: &RunState,
31    coordination: Option<&crate::repo_lock::SeatLease>,
32) -> Result<()> {
33    copies::restore_execution_seats(home, state, coordination).await
34}
35
36/// Read the binary diff after verifying its independent source archive.
37pub async fn source_diff(evidence: &Path) -> Result<Vec<u8>> {
38    crate::fold_evidence::validate_snapshot(evidence).await?;
39    Ok(std::fs::read(evidence.join("change.patch"))?)
40}
41
42/// Restore immutable checkout evidence into a new inspection repository.
43pub async fn restore_source(evidence: &Path, destination: &Path) -> Result<()> {
44    crate::fold_evidence::restore_snapshot(evidence, destination).await
45}
46
47/// Historical scan observations, independent of archive readability or deletion authority.
48pub fn seat_inspections(home: &Path, id: &str) -> Result<Vec<String>> {
49    ensure!(crate::run::is_run_id(id), "invalid run id");
50    copies::seat_inspections(home, id)
51}
52
53/// List archived checkout tips for a run, validating their durable evidence.
54pub async fn seat_evidence(home: &Path, id: &str) -> Result<Vec<String>> {
55    copies::seat_evidence(home, id).await
56}
57
58/// Validated archived heads and seat labels for the run's evidence browser.
59pub async fn seat_heads(
60    home: &Path,
61    id: &str,
62) -> Result<std::collections::BTreeMap<String, String>> {
63    copies::seat_heads(home, id).await
64}
65
66/// Read a recorded seat's diff without relying on its checkout or Git objects.
67pub async fn seat_diff(home: &Path, id: &str, head: &str) -> Result<Vec<u8>> {
68    copies::seat_diff(home, id, head).await
69}
70
71/// Restore a recorded checkout tip without changing run or recovery authority.
72pub async fn restore_seat(home: &Path, id: &str, head: &str, destination: &Path) -> Result<()> {
73    copies::restore_seat(home, id, head, destination).await
74}
75
76use crate::{
77    git,
78    proc::Quiet as _,
79    run::{Liveness, RunState},
80};
81use anyhow::{Context, Result, ensure};
82use jiff::Timestamp;
83use serde::{Deserialize, Serialize};
84use std::{
85    fs::{File, OpenOptions},
86    path::{Path, PathBuf},
87};
88
89/// An OS lease on a worktree's outputs, independent of repository coordination.
90/// Kept by the process cleanup owner until descendants terminate, including
91/// reparented tools retaining the inherited liveness witness.
92#[derive(Debug)]
93pub(crate) struct OutputLease {
94    _file: std::sync::Arc<OutputFile>,
95    _activity: bool,
96}
97
98#[derive(Debug)]
99struct OutputFile(File);
100
101impl std::ops::Deref for OutputFile {
102    type Target = File;
103    fn deref(&self) -> &File {
104        &self.0
105    }
106}
107
108#[cfg(unix)]
109impl Drop for OutputFile {
110    fn drop(&mut self) {
111        use std::os::fd::AsRawFd;
112        // SAFETY: the final shared owner releases this live descriptor's lock.
113        unsafe {
114            libc::flock(self.0.as_raw_fd(), libc::LOCK_UN);
115        }
116    }
117}
118
119impl OutputLease {
120    #[cfg(unix)]
121    pub(crate) fn descriptor(&self) -> std::os::fd::RawFd {
122        use std::os::fd::AsRawFd;
123        self._file.as_raw_fd()
124    }
125
126    /// A separate exclusive lock prevents nested/concurrent pre-clean operations.
127    pub(crate) fn pre_clean(dir: &Path) -> Result<Self> {
128        let file = OpenOptions::new()
129            .create(true)
130            .truncate(false)
131            .read(true)
132            .write(true)
133            .open(dir.join("magi-pre-clean.lock"))?;
134        crate::pr_head::try_lock_file(&file)
135            .context("pre-clean already running; retry after descendants terminate")?;
136        Ok(Self {
137            _file: std::sync::Arc::new(OutputFile(file)),
138            _activity: false,
139        })
140    }
141
142    pub(crate) fn duplicate(&self) -> Self {
143        Self {
144            _file: std::sync::Arc::clone(&self._file),
145            _activity: false,
146        }
147    }
148
149    pub(crate) fn borrow(worktree: &Path) -> Result<Option<Self>> {
150        if !worktree.join(".git").is_file() {
151            return Ok(None);
152        }
153        Self::take(worktree, false).map(Some)
154    }
155    pub(crate) fn cache(home: &Path, cache: &Path, exclusive: bool) -> Result<Self> {
156        use std::hash::{Hash, Hasher};
157        let mut hash = std::collections::hash_map::DefaultHasher::new();
158        let mut normalized = cache.to_owned();
159        let mut missing = Vec::new();
160        while !normalized.exists() {
161            let Some(name) = normalized.file_name() else {
162                break;
163            };
164            missing.push(name.to_owned());
165            if !normalized.pop() {
166                break;
167            }
168        }
169        normalized = normalized.canonicalize().unwrap_or(normalized);
170        for part in missing.into_iter().rev() {
171            normalized.push(part);
172        }
173        normalized.hash(&mut hash);
174        let dir = home.join("cache-leases");
175        std::fs::create_dir_all(&dir)?;
176        Self::lock(
177            &dir.join(format!("outputs-{:x}.lock", hash.finish())),
178            exclusive,
179        )
180    }
181    /// Reserve headroom once per volume while admitting a managed writer.
182    /// These are estimates, not filesystem quotas. The ordinary emergency
183    /// monitor remains necessary for writes exceeding the reservation.
184    pub(crate) fn admit(home: &Path, paths: &[PathBuf], min_free: u64) -> Result<Vec<Self>> {
185        #[cfg(unix)]
186        return Self::admit_isolated(home, paths, min_free);
187        #[cfg(not(unix))]
188        {
189            let _ = (home, paths, min_free);
190            Ok(Vec::new())
191        }
192    }
193
194    #[cfg(any(unix, windows))]
195    pub(crate) fn admit_isolated(
196        home: &Path,
197        paths: &[PathBuf],
198        min_free: u64,
199    ) -> Result<Vec<Self>> {
200        Self::admit_with(home, paths, min_free, crate::disk::managed_free_bytes)
201    }
202
203    #[cfg(any(unix, windows))]
204    pub(crate) fn admit_with(
205        home: &Path,
206        paths: &[PathBuf],
207        min_free: u64,
208        free_bytes: impl Fn(&Path) -> Result<u64>,
209    ) -> Result<Vec<Self>> {
210        #[cfg(unix)]
211        use std::os::unix::fs::{MetadataExt, OpenOptionsExt};
212        #[cfg(windows)]
213        use std::os::windows::fs::OpenOptionsExt;
214        if min_free == 0 {
215            return Ok(Vec::new());
216        }
217        const RESERVE: u64 = 1024 * 1024 * 1024;
218        std::fs::create_dir_all(home)?;
219        let dir = home.canonicalize()?.join("disk-admission");
220        std::fs::create_dir_all(&dir)?;
221        ensure!(
222            plain_dir(&dir) && dir.canonicalize()? == dir,
223            "disk admission directory is not canonical"
224        );
225        let mut devices = std::collections::BTreeSet::new();
226        let mut reservations = Vec::new();
227        for path in paths {
228            let probe = path
229                .ancestors()
230                .find(|p| p.exists())
231                .context("disk admission path missing")?;
232            #[cfg(unix)]
233            let device = std::fs::metadata(probe)?.dev();
234            #[cfg(windows)]
235            let device = windows_volume(probe)?;
236            if !devices.insert(device) {
237                continue;
238            }
239            let open = |path: &Path| -> Result<File> {
240                let mut opts = OpenOptions::new();
241                opts.read(true).write(true).create(true).truncate(false);
242                #[cfg(unix)]
243                opts.custom_flags(libc::O_NOFOLLOW);
244                #[cfg(windows)]
245                opts.custom_flags(0x0020_0000); // FILE_FLAG_OPEN_REPARSE_POINT
246                let file = opts.open(path)?;
247                #[cfg(windows)]
248                {
249                    use std::os::windows::fs::MetadataExt as _;
250                    ensure!(
251                        file.metadata()?.file_attributes() & 0x400 == 0,
252                        "disk admission lock is a reparse point"
253                    );
254                }
255                Ok(file)
256            };
257            // Explicitly unlock on return. A concurrent fork can briefly
258            // inherit this descriptor before exec closes it; closing only the
259            // parent's copy would leave the namespace spuriously busy.
260            let admission = Self {
261                _file: std::sync::Arc::new(OutputFile(open(&dir.join(format!("{device}.lock")))?)),
262                _activity: false,
263            };
264            let started = std::time::Instant::now();
265            loop {
266                match crate::pr_head::try_lock_file(&admission._file) {
267                    Ok(()) => break,
268                    Err(error)
269                        if lock_contended(&error)
270                            && started.elapsed() < std::time::Duration::from_secs(1) =>
271                    {
272                        std::thread::sleep(std::time::Duration::from_millis(5));
273                    }
274                    Err(error) => {
275                        return Err(error)
276                            .context("disk admission busy or unavailable; retry without dispatch");
277                    }
278                }
279            }
280            let mut active = 0u64;
281            let mut available = None;
282            for slot in 0..1024 {
283                let file = dir.join(format!("{device}-{slot}.reserve"));
284                if !file.exists() {
285                    if available.is_none() {
286                        available = Some(file);
287                    }
288                    continue;
289                }
290                let file = open(&file)?;
291                match crate::pr_head::try_lock_file(&file) {
292                    Ok(()) if available.is_none() => {
293                        available = Some(dir.join(format!("{device}-{slot}.reserve")))
294                    }
295                    Ok(()) => {}
296                    Err(_) => active = active.saturating_add(RESERVE),
297                }
298            }
299            let free = free_bytes(probe)?;
300            let required = min_free.saturating_add(active).saturating_add(RESERVE);
301            ensure!(
302                free >= required,
303                "disk admission blocked: {free} bytes free; floor {min_free}, active writer reservations {active}, next writer reservation {RESERVE} bytes (estimates, not quotas)"
304            );
305            let file = open(&available.context("disk admission reservation slots exhausted")?)?;
306            crate::pr_head::try_lock_file(&file)?;
307            reservations.push(Self {
308                _file: std::sync::Arc::new(OutputFile(file)),
309                _activity: false,
310            });
311        }
312        Ok(reservations)
313    }
314
315    #[cfg(not(any(unix, windows)))]
316    pub(crate) fn admit_isolated(
317        _home: &Path,
318        _paths: &[PathBuf],
319        _min_free: u64,
320    ) -> Result<Vec<Self>> {
321        anyhow::bail!("disk admission is unavailable on this platform")
322    }
323
324    pub(crate) fn take(worktree: &Path, exclusive: bool) -> Result<Self> {
325        let out = std::process::Command::new("git")
326            .args(["rev-parse", "--absolute-git-dir"])
327            .current_dir(worktree)
328            .quiet()
329            .output()?;
330        ensure!(
331            out.status.success(),
332            "cannot identify output lease directory"
333        );
334        let dir = PathBuf::from(String::from_utf8(out.stdout)?.trim());
335        Self::lock(&dir.join("magi-output.lock"), exclusive)
336    }
337    pub(crate) fn lock(path: &Path, exclusive: bool) -> Result<Self> {
338        let file = OpenOptions::new()
339            .create(true)
340            .truncate(false)
341            .read(true)
342            .write(true)
343            .open(path)
344            .with_context(|| format!("output lease: open {}", path.display()))?;
345        #[cfg(unix)]
346        {
347            use std::os::fd::AsRawFd;
348            let mode = if exclusive {
349                libc::LOCK_EX
350            } else {
351                libc::LOCK_SH
352            };
353            // SAFETY: live file descriptor, no retained pointers.
354            ensure!(
355                unsafe { libc::flock(file.as_raw_fd(), mode | libc::LOCK_NB) } == 0,
356                "output lease: outputs are in use or being reclaimed: {}",
357                path.display()
358            );
359        }
360        #[cfg(windows)]
361        {
362            use std::os::windows::io::AsRawHandle as _;
363            ensure!(
364                crate::chat_watch::windows::lock_file_mode(file.as_raw_handle(), true, exclusive)?,
365                "output lease: outputs are in use or being reclaimed: {}",
366                path.display()
367            );
368        }
369        #[cfg(not(any(unix, windows)))]
370        let _ = exclusive;
371        if !exclusive {
372            // Empty legacy/maintenance-only leases carry no activity witness.
373            file.set_len(1)?;
374            file.set_modified(std::time::SystemTime::now())
375                .with_context(|| format!("record output activity {}", path.display()))?;
376        }
377        Ok(Self {
378            _file: std::sync::Arc::new(OutputFile(file)),
379            _activity: !exclusive,
380        })
381    }
382}
383
384#[cfg(any(unix, windows))]
385fn lock_contended(error: &std::io::Error) -> bool {
386    matches!(
387        error.kind(),
388        std::io::ErrorKind::WouldBlock | std::io::ErrorKind::Interrupted
389    ) || cfg!(windows) && error.raw_os_error() == Some(33)
390}
391
392#[cfg(windows)]
393fn windows_volume(path: &Path) -> Result<u128> {
394    use std::os::windows::ffi::OsStrExt as _;
395    #[link(name = "kernel32")]
396    unsafe extern "system" {
397        fn GetVolumePathNameW(path: *const u16, mount: *mut u16, length: u32) -> i32;
398        fn GetVolumeNameForVolumeMountPointW(mount: *const u16, name: *mut u16, length: u32)
399        -> i32;
400    }
401    let path: Vec<_> = path
402        .canonicalize()?
403        .as_os_str()
404        .encode_wide()
405        .chain(Some(0))
406        .collect();
407    let mut mount = vec![0u16; 32_768];
408    let mut name = [0u16; 64];
409    // The buffers are valid for these Win32 calls and the input is NUL-terminated.
410    if unsafe { GetVolumePathNameW(path.as_ptr(), mount.as_mut_ptr(), mount.len() as u32) } == 0
411        || unsafe {
412            GetVolumeNameForVolumeMountPointW(mount.as_ptr(), name.as_mut_ptr(), name.len() as u32)
413        } == 0
414    {
415        return Err(std::io::Error::last_os_error()).context("identify disk admission volume");
416    }
417    let end = name
418        .iter()
419        .position(|&c| c == 0)
420        .context("invalid volume name")?;
421    let name = String::from_utf16(&name[..end])?;
422    let id = name
423        .strip_prefix(r"\\?\Volume{")
424        .and_then(|s| s.strip_suffix("}\\"))
425        .context("volume has no stable GUID")?;
426    ensure!(
427        id.bytes().all(|b| b.is_ascii_hexdigit() || b == b'-'),
428        "invalid volume GUID"
429    );
430    Ok(u128::from_str_radix(&id.replace('-', ""), 16)?)
431}
432
433#[cfg(all(test, windows))]
434#[test]
435fn isolated_windows_reservations_share_the_actual_volume_budget() {
436    let home = tempfile::tempdir().unwrap();
437    let first = home.path().join("first checkout");
438    let second = home.path().join("second 日本語 checkout");
439    std::fs::create_dir(&first).unwrap();
440    std::fs::create_dir(&second).unwrap();
441    let reserve =
442        |paths: &[PathBuf]| OutputLease::admit_with(home.path(), paths, 1, |_| Ok(1 + (1 << 30)));
443    let writer = reserve(&[first, second.clone()]).unwrap();
444    let err = reserve(std::slice::from_ref(&second)).unwrap_err();
445    assert!(
446        err.to_string()
447            .contains("active writer reservations 1073741824")
448    );
449    drop(writer);
450    assert!(reserve(&[second]).is_ok());
451}
452
453#[cfg(unix)]
454impl Drop for OutputLease {
455    fn drop(&mut self) {
456        if self._activity
457            && let Err(error) = self._file.set_modified(std::time::SystemTime::now())
458        {
459            // If release time cannot be recorded, retain a nonempty invalid
460            // witness rather than trusting the old acquisition timestamp.
461            let _ = self._file.set_len(2);
462            tracing::warn!("could not record output lease release activity: {error}");
463        }
464    }
465}
466
467#[derive(Default, Debug, Serialize, Deserialize)]
468#[serde(default)]
469pub(crate) struct Report {
470    pub checked_at: Option<Timestamp>,
471    pub limit_bytes: u64,
472    pub reclaim_budget_bytes: u64,
473    /// Logical inventory, including protected and unproven data.
474    pub measured_bytes: u64,
475    /// Logical bytes unlinked, not a measurement of physical disk recovery.
476    pub freed_bytes: u64,
477    pub measured_allocated_file_bytes: Option<u64>,
478    /// Candidates proven under locks in this pass; uninspected data is excluded.
479    pub proven_reclaimable_bytes: u64,
480    /// Estimated allocated blocks recoverable if every proven link is removed.
481    /// Inodes with links outside the candidate set contribute zero.
482    pub proven_reclaimable_allocated_bytes: Option<u64>,
483    pub protected_or_unproven_bytes: u64,
484    /// Deduplicated allocated file blocks whose final link was unlinked. Open
485    /// descriptors and shared extents can prevent an equivalent physical gain.
486    pub estimated_released_allocated_bytes: Option<u64>,
487    /// Observed free-space change on the home volume; concurrent writes, clones
488    /// and deferred reclamation mean this is not attributable to this sweep.
489    pub observed_home_free_change_bytes: Option<i64>,
490    pub retired: usize,
491    pub evicted_copies: usize,
492    pub source_scan: Option<serde_json::Value>,
493    pub removed_empty_roots: usize,
494    pub source_unlinked_logical_bytes: u64,
495    pub source_unlinked_allocated_bytes: Option<u64>,
496    pub source_evidence_added_logical_bytes: u64,
497    pub source_evidence_added_allocated_bytes: Option<u64>,
498    pub retained: Vec<String>,
499    pub failures: Vec<String>,
500}
501
502#[cfg(unix)]
503fn allocated_inventory(
504    root: &Path,
505    seen: &mut std::collections::BTreeSet<(u64, u64)>,
506) -> Result<u64> {
507    use std::os::unix::fs::MetadataExt;
508    ensure!(
509        plain_dir(root) && root.canonicalize()? == root,
510        "allocated inventory root is not canonical"
511    );
512    let mut dirs = vec![root.to_owned()];
513    let mut bytes = 0u64;
514    while let Some(dir) = dirs.pop() {
515        for entry in std::fs::read_dir(dir)? {
516            let path = entry?.path();
517            let metadata = std::fs::symlink_metadata(&path)?;
518            if metadata.is_dir() {
519                dirs.push(path);
520            } else if metadata.is_file() && seen.insert((metadata.dev(), metadata.ino())) {
521                bytes = bytes.saturating_add(metadata.blocks().saturating_mul(512));
522            }
523        }
524    }
525    Ok(bytes)
526}
527
528#[derive(Default)]
529struct ReleasedAllocation {
530    #[cfg(unix)]
531    files: std::collections::BTreeMap<PathBuf, (u64, u64, u64, u64)>,
532    #[cfg(unix)]
533    removed: std::collections::BTreeMap<(u64, u64), u64>,
534}
535
536impl ReleasedAllocation {
537    fn of(paths: &[PathBuf]) -> Result<Self> {
538        #[cfg(unix)]
539        {
540            use std::os::unix::fs::MetadataExt;
541            let mut files = std::collections::BTreeMap::new();
542            for path in paths {
543                let m = std::fs::symlink_metadata(path)?;
544                ensure!(m.is_file(), "allocation candidate is not regular");
545                files.insert(
546                    path.clone(),
547                    (m.dev(), m.ino(), m.nlink(), m.blocks().saturating_mul(512)),
548                );
549            }
550            Ok(Self {
551                files,
552                removed: Default::default(),
553            })
554        }
555        #[cfg(not(unix))]
556        {
557            let _ = paths;
558            Ok(Self::default())
559        }
560    }
561    fn reclaimable_allocated_bytes(&self) -> Option<u64> {
562        #[cfg(unix)]
563        {
564            let mut inodes = std::collections::BTreeMap::new();
565            for &(device, inode, links, blocks) in self.files.values() {
566                let entry = inodes
567                    .entry((device, inode))
568                    .or_insert((0u64, links, blocks));
569                entry.0 += 1;
570            }
571            Some(
572                inodes
573                    .values()
574                    .filter(|(count, links, _)| count == links)
575                    .fold(0u64, |sum, (_, _, blocks)| sum.saturating_add(*blocks)),
576            )
577        }
578        #[cfg(not(unix))]
579        {
580            None
581        }
582    }
583
584    fn unlinked(&mut self, path: &Path) -> Option<u64> {
585        #[cfg(unix)]
586        {
587            let (device, inode, links, blocks) = self.files[path];
588            let removed = self.removed.entry((device, inode)).or_default();
589            *removed += 1;
590            Some(if *removed == links { blocks } else { 0 })
591        }
592        #[cfg(not(unix))]
593        {
594            let _ = path;
595            None
596        }
597    }
598}
599
600fn plain_dir(path: &Path) -> bool {
601    std::fs::symlink_metadata(path).is_ok_and(|m| m.is_dir() && !m.file_type().is_symlink())
602}
603
604pub(crate) fn read_seat(path: &Path) -> bool {
605    path.file_name()
606        .and_then(|s| s.to_str())
607        .is_some_and(|name| {
608            ["review-", "judge-", "advisor-"].iter().any(|prefix| {
609                name.strip_prefix(prefix)
610                    .is_some_and(|n| n.parse::<usize>().is_ok_and(|n| n > 0))
611            })
612        })
613}
614
615/// Old run records outlive their checkout and output directories. Only an
616/// absent path means nothing to reclaim; inaccessible paths remain errors.
617fn run_paths_missing(state: &RunState) -> Result<bool> {
618    for path in [state.worktree_root(), state.repo.clone()] {
619        match std::fs::metadata(&path) {
620            Ok(_) => {}
621            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(true),
622            Err(e) => return Err(e).with_context(|| format!("inspect {}", path.display())),
623        }
624    }
625    Ok(false)
626}
627
628/// Enumerate actual registered worktrees, not just candidates in run.json.
629pub(crate) async fn worktrees(state: &RunState) -> Result<Vec<PathBuf>> {
630    let root = state.worktree_root();
631    if !plain_dir(&root) {
632        return Ok(Vec::new());
633    }
634    let list = git::git(&state.repo, &["worktree", "list", "--porcelain"]).await?;
635    registered_worktrees(state, &list)
636}
637
638fn registered_worktrees(state: &RunState, list: &str) -> Result<Vec<PathBuf>> {
639    let root = state.worktree_root();
640    if !plain_dir(&root) {
641        return Ok(Vec::new());
642    }
643    let root = root.canonicalize()?;
644    let mut paths = Vec::new();
645    for path in list.lines().filter_map(|l| l.strip_prefix("worktree ")) {
646        let path = PathBuf::from(path);
647        #[cfg(windows)]
648        let path = if plain_dir(&path) {
649            git::canonical_output_path(path.to_str().context("non UTF-8 registration")?)?
650        } else {
651            continue;
652        };
653        if plain_dir(&path)
654            && path.parent() == Some(root.as_path())
655            && path.canonicalize()? == path
656            && path.join(".git").is_file()
657        {
658            paths.push(path);
659        }
660    }
661    Ok(paths)
662}
663
664/// Recognize Cargo's generated directory, never merely an ignored directory.
665/// Unknown root entries are retained; cache identity/profile paths fail closed.
666async fn cargo_target(worktree: &Path) -> Result<PathBuf> {
667    validate_cargo_target(worktree, false).await
668}
669
670async fn validate_cargo_target(worktree: &Path, allow_missing_tag: bool) -> Result<PathBuf> {
671    let target = worktree.join("target");
672    ensure!(plain_dir(&target), "target absent or not a plain directory");
673    ensure!(target.canonicalize()? == target, "target path changed");
674    ensure!(
675        git::git(worktree, &["ls-files", "--", "target"])
676            .await?
677            .is_empty(),
678        "tracked target contents"
679    );
680    ensure!(
681        git::git_raw(worktree, &["check-ignore", "--quiet", "target/"])
682            .await?
683            .ok(),
684        "target is not ignored"
685    );
686    cache_tag(&target, allow_missing_tag)?;
687    let path = target.join(".rustc_info.json");
688    plain_file(&path, "rustc identity")?;
689    let info: serde_json::Value = serde_json::from_slice(
690        &std::fs::read(&path).with_context(|| format!("read rustc identity {}", path.display()))?,
691    )
692    .with_context(|| format!("parse rustc identity {}", path.display()))?;
693    ensure!(
694        info.get("rustc_fingerprint").is_some(),
695        "unknown Cargo identity {}",
696        path.display()
697    );
698    if allow_missing_tag && !target.join("CACHEDIR.TAG").exists() {
699        ensure!(
700            info.get("rustc_fingerprint")
701                .is_some_and(serde_json::Value::is_u64)
702                && info
703                    .get("outputs")
704                    .and_then(serde_json::Value::as_object)
705                    .is_some_and(|outputs| outputs.values().any(|output| {
706                        output.get("success").and_then(serde_json::Value::as_bool) == Some(true)
707                            && output
708                                .get("stdout")
709                                .and_then(serde_json::Value::as_str)
710                                .is_some_and(|stdout| stdout.starts_with("rustc "))
711                            && output
712                                .get("stderr")
713                                .is_some_and(serde_json::Value::is_string)
714                    }))
715                && info
716                    .get("successes")
717                    .is_some_and(serde_json::Value::is_object),
718            "missing-tag rustc identity structure invalid: {}",
719            path.display()
720        );
721        let mut profiles = 0;
722        for profile in ["debug", "release"] {
723            let dir = target.join(profile);
724            if !dir.exists() {
725                continue;
726            }
727            profile_output_dirs(&dir, true)?;
728            plain_file(&dir.join(".cargo-lock"), "missing-tag Cargo lock")?;
729            profiles += 1;
730        }
731        ensure!(
732            profiles > 0,
733            "missing-tag proof: no Cargo profile in {}",
734            target.display()
735        );
736    }
737    for name in ["debug", "release"] {
738        let path = target.join(name);
739        match std::fs::symlink_metadata(&path) {
740            Ok(metadata) => ensure!(
741                metadata.is_dir() && path.canonicalize()? == path,
742                "profile validation: not a plain directory: {}",
743                path.display()
744            ),
745            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
746            Err(error) => {
747                return Err(error)
748                    .with_context(|| format!("profile validation: inspect {}", path.display()));
749            }
750        }
751    }
752    Ok(target)
753}
754
755fn plain_file(path: &Path, stage: &str) -> Result<()> {
756    let meta = std::fs::symlink_metadata(path)
757        .with_context(|| format!("{stage}: inspect {}", path.display()))?;
758    ensure!(
759        meta.is_file() && path.canonicalize()? == path,
760        "{stage}: not a plain file: {}",
761        path.display()
762    );
763    Ok(())
764}
765
766fn cache_tag(target: &Path, allow_missing: bool) -> Result<()> {
767    let path = target.join("CACHEDIR.TAG");
768    match std::fs::symlink_metadata(&path) {
769        Err(error) if allow_missing && error.kind() == std::io::ErrorKind::NotFound => {
770            return Ok(());
771        }
772        result => {
773            result.with_context(|| format!("cache tag: inspect {}", path.display()))?;
774        }
775    }
776    plain_file(&path, "cache tag")?;
777    let tag = std::fs::read_to_string(&path)
778        .with_context(|| format!("cache tag: read {}", path.display()))?;
779    ensure!(
780        tag.starts_with("Signature: 8a477f597d28d172789f06886806bc55"),
781        "unknown Cargo cache tag: {}",
782        path.display()
783    );
784    Ok(())
785}
786
787/// Observe compiler metadata and artifacts under the output and Cargo locks.
788/// Unknown contents never become deletion authority. Unreadable timestamps or
789/// a future timestamp retain outputs, even when the configured grace is zero.
790fn output_age(target: &Path, now: Timestamp, grace: u64) -> Result<()> {
791    let mut dirs = vec![target.to_owned()];
792    let mut entries = 0;
793    while let Some(dir) = dirs.pop() {
794        for entry in std::fs::read_dir(&dir)
795            .with_context(|| format!("output activity: list {}", dir.display()))?
796        {
797            let path = entry?.path();
798            entries += 1;
799            ensure!(
800                entries <= 100_000,
801                "output activity inventory too large: {}",
802                target.display()
803            );
804            let metadata = std::fs::symlink_metadata(&path)
805                .with_context(|| format!("output activity: inspect {}", path.display()))?;
806            if metadata.is_dir() {
807                // Only supported Cargo profiles supply execution evidence.
808                if dir != target
809                    || matches!(
810                        path.file_name().and_then(|s| s.to_str()),
811                        Some("debug" | "release")
812                    )
813                {
814                    dirs.push(path);
815                }
816            } else if metadata.is_file()
817                && (dir != target || path.file_name().is_some_and(|n| n == ".rustc_info.json"))
818            {
819                let at =
820                    Timestamp::try_from(metadata.modified().with_context(|| {
821                        format!("output activity: modified {}", path.display())
822                    })?)?;
823                ensure!(
824                    crate::clean::due(now, at, grace),
825                    "output activity within grace period: {} modified {at} (now {now}, grace {grace}s)",
826                    path.display()
827                );
828            }
829        }
830    }
831    Ok(())
832}
833
834pub(crate) fn artifact_signature(path: &Path) -> Result<bool> {
835    use std::io::Read as _;
836    let mut header = [0; 8];
837    let n = File::open(path)?.read(&mut header)?;
838    let header = &header[..n];
839    Ok(header.starts_with(b"\x7fELF")
840        || header.starts_with(b"!<arch>\n")
841        || header.starts_with(b"rust\0\0\0")
842        || header.starts_with(b"MZ\x90\0")
843        || [
844            [0xfe, 0xed, 0xfa, 0xce],
845            [0xce, 0xfa, 0xed, 0xfe],
846            [0xfe, 0xed, 0xfa, 0xcf],
847            [0xcf, 0xfa, 0xed, 0xfe],
848            [0xca, 0xfe, 0xba, 0xbe],
849            [0xca, 0xfe, 0xba, 0xbf],
850        ]
851        .iter()
852        .any(|magic| header.starts_with(magic)))
853}
854
855/// Enumerate only known Cargo output locations. A v2 unit's `out` is not
856/// wholesale compiler authority: callers must still prove each file by dep-info
857/// and signature. In particular, build-script output is never traversed.
858fn profile_output_dirs(profile: &Path, require_proof: bool) -> Result<Vec<PathBuf>> {
859    let mut present = std::collections::BTreeSet::new();
860    for path in [
861        profile.to_owned(),
862        profile.join("deps"),
863        profile.join(".fingerprint"),
864        profile.join("build"),
865    ] {
866        match std::fs::symlink_metadata(&path) {
867            Ok(_) => {
868                ensure!(
869                    plain_dir(&path) && path.canonicalize()? == path,
870                    "Cargo profile proof: {} is not a plain directory",
871                    path.display()
872                );
873                present.insert(path);
874            }
875            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
876            Err(e) => return Err(e.into()),
877        }
878    }
879    if !present.contains(profile) {
880        return Ok(Vec::new());
881    }
882    let deps = present.contains(&profile.join("deps"));
883    let fingerprint = present.contains(&profile.join(".fingerprint"));
884    let mut dirs = vec![profile.to_owned()];
885    if deps || fingerprint {
886        ensure!(
887            !require_proof || (deps && fingerprint),
888            "incomplete Cargo v1 profile proof"
889        );
890        if deps {
891            dirs.push(profile.join("deps"));
892        }
893        // v1 build-script directories have flat <package>-<hash> names;
894        // a nested v2 package directory must not silently mix layouts.
895        if present.contains(&profile.join("build")) {
896            for entry in std::fs::read_dir(profile.join("build"))? {
897                let path = entry?.path();
898                if plain_dir(&path) {
899                    for entry in std::fs::read_dir(&path)? {
900                        ensure!(
901                            !entry?.file_name().to_str().is_some_and(unit_hash),
902                            "mixed Cargo profile layouts"
903                        );
904                    }
905                }
906            }
907        }
908    } else if present.contains(&profile.join("build")) {
909        let mut units = 0;
910        for package in std::fs::read_dir(profile.join("build"))? {
911            let package = package?.path();
912            let meta = std::fs::symlink_metadata(&package)?;
913            ensure!(
914                !meta.file_type().is_symlink(),
915                "Cargo v2 package is a symlink: {}",
916                package.display()
917            );
918            if !meta.is_dir() {
919                ensure!(!require_proof, "Cargo v2 package is not a directory");
920                continue;
921            }
922            ensure!(
923                package.canonicalize()? == package,
924                "Cargo v2 package is not a plain directory"
925            );
926            let mut package_units = 0;
927            for unit in std::fs::read_dir(&package)? {
928                let unit = unit?.path();
929                let meta = std::fs::symlink_metadata(&unit)?;
930                ensure!(
931                    !meta.file_type().is_symlink(),
932                    "Cargo v2 unit is a symlink: {}",
933                    unit.display()
934                );
935                // Reclamation only: a non-hash or incomplete unit is not a
936                // rustc unit, so it is retained while complete ones still
937                // count. The missing-tag proof demands every unit be valid.
938                let structured = meta.is_dir()
939                    && unit
940                        .file_name()
941                        .and_then(|n| n.to_str())
942                        .is_some_and(unit_hash)
943                    && unit.canonicalize()? == unit
944                    && ["fingerprint", "out"].iter().all(|name| {
945                        let path = unit.join(name);
946                        plain_dir(&path) && path.canonicalize().is_ok_and(|p| p == path)
947                    });
948                // A completed build-script run unit has no invoked.timestamp;
949                // it is a known structure but its output never gets authority.
950                if structured && v2_run_script_unit(&unit) {
951                    continue;
952                }
953                let complete = structured
954                    && plain_file(
955                        &unit.join("fingerprint/invoked.timestamp"),
956                        "Cargo unit fingerprint",
957                    )
958                    .is_ok();
959                if !complete {
960                    ensure!(!require_proof, "invalid or incomplete Cargo v2 unit");
961                    continue;
962                }
963                dirs.push(unit.join("out"));
964                units += 1;
965                package_units += 1;
966                ensure!(units <= 4096, "Cargo unit inventory too large");
967            }
968            ensure!(
969                package_units > 0 || !require_proof,
970                "empty Cargo v2 package"
971            );
972        }
973        ensure!(units > 0 || !require_proof, "missing Cargo v2 units");
974    } else {
975        ensure!(!require_proof, "missing Cargo profile proof");
976    }
977    Ok(dirs)
978}
979
980// A build-script run unit: fingerprint holds exactly a plain
981// `run-build-script-*` file with its json sibling, and no timestamp.
982fn v2_run_script_unit(unit: &Path) -> bool {
983    let fp = unit.join("fingerprint");
984    if std::fs::symlink_metadata(fp.join("invoked.timestamp")).is_ok() {
985        return false;
986    }
987    let Ok(entries) = std::fs::read_dir(&fp) else {
988        return false;
989    };
990    for entry in entries.flatten() {
991        let path = entry.path();
992        let name = path.file_name().unwrap_or_default().to_string_lossy();
993        if name.starts_with("run-build-script-")
994            && path.extension().is_none()
995            && plain_file(&path, "Cargo run fingerprint").is_ok()
996            && plain_file(&path.with_extension("json"), "Cargo run fingerprint json").is_ok()
997        {
998            return true;
999        }
1000    }
1001    false
1002}
1003
1004// A unit directory may also contain arbitrary build-script products. Only
1005// lib/bin rustc fingerprints matching this dep-info stem confer output authority.
1006fn v2_compiler_dep_info(dir: &Path, dep_info: &Path) -> Result<bool> {
1007    let unit = dir.parent().context("Cargo unit parent")?;
1008    let hash = unit.file_name().unwrap_or_default().to_string_lossy();
1009    let stem = dep_info.file_stem().unwrap_or_default().to_string_lossy();
1010    for entry in std::fs::read_dir(unit.join("fingerprint"))? {
1011        let path = entry?.path();
1012        let name = path.file_name().unwrap_or_default().to_string_lossy();
1013        // Cargo hashes library filenames but v2 binaries use the crate name
1014        // directly; the containing unit directory supplies their disambiguation.
1015        let library = name
1016            .strip_prefix("lib-")
1017            .is_some_and(|name| stem == format!("{}-{hash}", name.replace('-', "_")));
1018        let binary = name
1019            .strip_prefix("bin-")
1020            .is_some_and(|name| stem == name.replace('-', "_"));
1021        if library || binary {
1022            plain_file(&path, "Cargo compiler fingerprint")?;
1023            plain_file(
1024                &path.with_extension("json"),
1025                "Cargo compiler fingerprint json",
1026            )?;
1027            plain_file(
1028                &path.with_file_name(format!(
1029                    "dep-{}",
1030                    path.file_name().unwrap().to_string_lossy()
1031                )),
1032                "Cargo compiler fingerprint dep-info",
1033            )?;
1034            return Ok(true);
1035        }
1036    }
1037    Ok(false)
1038}
1039
1040fn unit_hash(name: &str) -> bool {
1041    name.len() == 16 && name.bytes().all(|b| b.is_ascii_hexdigit())
1042}
1043
1044/// Legacy tags identify a cache, not ownership of every file inside it.
1045/// Only direct rustc outputs named by dep-info may be reclaimed. Build-script
1046/// output, incremental caches, unknown files and the manifests themselves stay.
1047fn declared_outputs(target: &Path) -> Result<Vec<PathBuf>> {
1048    let mut outputs = std::collections::BTreeSet::new();
1049    for profile in ["debug", "release"] {
1050        for dir in profile_output_dirs(&target.join(profile), false)? {
1051            if !plain_dir(&dir) || dir.canonicalize()? != dir {
1052                continue;
1053            }
1054            for entry in std::fs::read_dir(&dir)? {
1055                let path = entry?.path();
1056                if path.extension().is_none_or(|ext| ext != "d")
1057                    || !std::fs::symlink_metadata(&path)?.is_file()
1058                    || std::fs::metadata(&path)?.len() > 1024 * 1024
1059                    || path.canonicalize()? != path
1060                {
1061                    continue;
1062                }
1063                if dir.file_name().is_some_and(|name| name == "out")
1064                    && !v2_compiler_dep_info(&dir, &path)?
1065                {
1066                    continue;
1067                }
1068                let body = std::fs::read_to_string(&path)?;
1069                for line in body.lines() {
1070                    let Some((names, dependencies)) = line.split_once(": ") else {
1071                        continue;
1072                    };
1073                    if dependencies.trim().is_empty() {
1074                        continue;
1075                    }
1076                    // Rustc escapes spaces and backslashes in Make paths.
1077                    // Unsupported Make syntax fails closed for that record.
1078                    let mut words = Vec::new();
1079                    let mut word = String::new();
1080                    let mut escaped = false;
1081                    for c in names.chars() {
1082                        if escaped {
1083                            word.push(c);
1084                            escaped = false;
1085                        } else if c == '\\' {
1086                            escaped = true;
1087                        } else if c.is_whitespace() {
1088                            if !word.is_empty() {
1089                                words.push(std::mem::take(&mut word));
1090                            }
1091                        } else {
1092                            word.push(c);
1093                        }
1094                    }
1095                    if escaped || word.contains(['$', '%']) {
1096                        continue;
1097                    }
1098                    if !word.is_empty() {
1099                        words.push(word);
1100                    }
1101                    for name in words {
1102                        let output = PathBuf::from(name);
1103                        let stem = path.file_stem().unwrap_or_default().to_string_lossy();
1104                        let output_stem = output.file_stem().unwrap_or_default().to_string_lossy();
1105                        let artifact_name =
1106                            output_stem == stem || output_stem == format!("lib{stem}");
1107                        let artifact_kind = output.extension().is_none_or(|ext| {
1108                            matches!(
1109                                ext.to_str(),
1110                                Some(
1111                                    "rlib"
1112                                        | "rmeta"
1113                                        | "so"
1114                                        | "dylib"
1115                                        | "a"
1116                                        | "lib"
1117                                        | "dll"
1118                                        | "exe"
1119                                        | "pdb"
1120                                )
1121                            )
1122                        });
1123                        if artifact_name
1124                            && artifact_kind
1125                            && output.parent() == Some(dir.as_path())
1126                            && output.extension().is_none_or(|ext| ext != "d")
1127                            && std::fs::symlink_metadata(&output).is_ok_and(|m| m.is_file())
1128                            && output.canonicalize().is_ok_and(|p| p == output)
1129                            && artifact_signature(&output)?
1130                        {
1131                            outputs.insert(output);
1132                        }
1133                    }
1134                }
1135            }
1136        }
1137    }
1138    Ok(outputs.into_iter().collect())
1139}
1140
1141#[derive(Serialize, Deserialize)]
1142struct CargoResidue {
1143    target: PathBuf,
1144    files: std::collections::BTreeMap<PathBuf, Vec<u8>>,
1145}
1146
1147// Keep the exact metadata as historical evidence. An old proof cannot adopt
1148// newly added files, changed metadata, symlinks or a different worktree.
1149fn residue_files(
1150    target: &Path,
1151    outputs: &[PathBuf],
1152) -> Result<std::collections::BTreeMap<PathBuf, Vec<u8>>> {
1153    let mut files = std::collections::BTreeMap::new();
1154    let mut dirs = vec![target.to_owned()];
1155    let mut bytes = 0usize;
1156    let mut entries = 0usize;
1157    while let Some(dir) = dirs.pop() {
1158        for entry in std::fs::read_dir(dir)? {
1159            let path = entry?.path();
1160            entries += 1;
1161            ensure!(entries <= 4096, "residue inventory too large");
1162            let metadata = std::fs::symlink_metadata(&path)?;
1163            ensure!(
1164                !metadata.file_type().is_symlink(),
1165                "symlink in Cargo residue"
1166            );
1167            if metadata.is_dir() {
1168                dirs.push(path);
1169            } else {
1170                ensure!(metadata.is_file(), "non-file in Cargo residue");
1171                if outputs.contains(&path) {
1172                    continue;
1173                }
1174                ensure!(metadata.len() <= 1024 * 1024, "residue file too large");
1175                let body = std::fs::read(&path)?;
1176                bytes = bytes.saturating_add(body.len());
1177                ensure!(bytes <= 8 * 1024 * 1024, "residue inventory too large");
1178                files.insert(path.strip_prefix(target)?.to_owned(), body);
1179            }
1180        }
1181    }
1182    Ok(files)
1183}
1184
1185fn residue_proof(home: &Path, id: &str, worktree: &Path) -> Result<PathBuf> {
1186    Ok(home
1187        .join("runs")
1188        .join(id)
1189        .join("build-residue")
1190        .join(worktree.file_name().context("missing worktree name")?))
1191}
1192
1193// Artifact eviction tolerates unrelated root data. Whole-target removal must
1194// still reject it, including empty directories absent from the file inventory.
1195fn residue_root(target: &Path) -> Result<()> {
1196    for entry in std::fs::read_dir(target)? {
1197        let name = entry?.file_name();
1198        ensure!(
1199            matches!(
1200                name.to_str(),
1201                Some("debug" | "release" | "CACHEDIR.TAG" | ".rustc_info.json")
1202            ),
1203            "unrecognized Cargo residue root entry"
1204        );
1205    }
1206    Ok(())
1207}
1208
1209fn record_residue(home: &Path, id: &str, target: &Path, outputs: &[PathBuf]) -> Result<()> {
1210    cache_tag(target, false)?;
1211    residue_root(target)?;
1212    let files = residue_files(target, outputs)?;
1213    for (relative, body) in &files {
1214        let path = target.join(relative);
1215        let identity = matches!(relative.to_str(), Some("CACHEDIR.TAG" | ".rustc_info.json"));
1216        let lock = matches!(
1217            relative.to_str(),
1218            Some("debug/.cargo-lock" | "release/.cargo-lock")
1219        ) && body.is_empty();
1220        let dep_info = path.extension().is_some_and(|ext| ext == "d")
1221            && outputs.iter().any(|output| {
1222                output.parent() == path.parent()
1223                    && (output.file_stem() == path.file_stem()
1224                        || output.file_stem().is_some_and(|stem| {
1225                            stem.to_string_lossy()
1226                                == format!(
1227                                    "lib{}",
1228                                    path.file_stem().unwrap_or_default().to_string_lossy()
1229                                )
1230                        }))
1231            });
1232        ensure!(identity || lock || dep_info, "unproven Cargo residue");
1233    }
1234    let proof = residue_proof(home, id, target.parent().context("missing worktree")?)?;
1235    std::fs::create_dir_all(proof.parent().context("missing proof directory")?)?;
1236    let tmp = proof.with_extension("tmp");
1237    std::fs::write(
1238        &tmp,
1239        serde_json::to_vec(&CargoResidue {
1240            target: target.to_owned(),
1241            files,
1242        })?,
1243    )?;
1244    std::fs::rename(tmp, proof)?;
1245    Ok(())
1246}
1247
1248pub(crate) async fn verify_residue(home: &Path, id: &str, worktree: &Path) -> Result<Vec<File>> {
1249    let target = cargo_target(worktree).await?;
1250    residue_root(&target)?;
1251    let locks = cargo_locks(&target)?;
1252    let proof: CargoResidue =
1253        serde_json::from_slice(&std::fs::read(residue_proof(home, id, worktree)?)?)?;
1254    ensure!(
1255        proof.target == target && proof.files == residue_files(&target, &[])?,
1256        "Cargo residue changed or contains unproven files"
1257    );
1258    Ok(locks)
1259}
1260
1261/// Supplemental exclusion for Cargo builds launched outside the managed seats.
1262pub(crate) fn cargo_locks(target: &Path) -> Result<Vec<File>> {
1263    let mut held = Vec::new();
1264    for (profile, name) in ["debug", "release"].into_iter().flat_map(|profile| {
1265        [".cargo-lock", ".cargo-build-lock", ".cargo-artifact-lock"].map(|name| (profile, name))
1266    }) {
1267        let lock = target.join(profile).join(name);
1268        match std::fs::symlink_metadata(&lock) {
1269            Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue,
1270            result => {
1271                result.with_context(|| format!("Cargo lock: inspect {}", lock.display()))?;
1272            }
1273        }
1274        {
1275            plain_file(&lock, "Cargo lock")?;
1276            let file = OpenOptions::new()
1277                .read(true)
1278                .write(true)
1279                .open(&lock)
1280                .with_context(|| format!("Cargo lock: open {}", lock.display()))?;
1281            crate::pr_head::try_lock_file(&file)
1282                .with_context(|| format!("Cargo build is active: {}", lock.display()))?;
1283            held.push(file);
1284        }
1285    }
1286    Ok(held)
1287}
1288
1289/// All repositories share one cap and one per-pass deletion budget. The report
1290/// survives a restart and explains every skipped run/target or failed operation.
1291pub(crate) async fn sweep(home: &Path, disk: &crate::config::Disk, now: Timestamp) -> Report {
1292    let home = home.to_owned();
1293    let disk = disk.clone();
1294    let runtime = tokio::runtime::Handle::current();
1295    tokio::task::spawn_blocking(move || runtime.block_on(sweep_inner(&home, &disk, now)))
1296        .await
1297        .unwrap_or_else(|e| Report {
1298            failures: vec![format!("retention worker failed: {e}")],
1299            ..Report::default()
1300        })
1301}
1302
1303#[derive(Default, Serialize, Deserialize)]
1304struct Measurement {
1305    at: Option<Timestamp>,
1306    bytes: u64,
1307}
1308
1309async fn sweep_inner(home: &Path, disk: &crate::config::Disk, now: Timestamp) -> Report {
1310    let mut report = Report {
1311        checked_at: Some(now),
1312        limit_bytes: disk.worktree_limit_bytes,
1313        reclaim_budget_bytes: disk.worktree_reclaim_bytes,
1314        ..Report::default()
1315    };
1316    if !cfg!(unix) {
1317        report
1318            .failures
1319            .push("Automatic output reclamation requires Unix process containment".into());
1320        return report;
1321    }
1322    let Ok(_janitor) = OutputLease::lock(&home.join("retention.lock"), true) else {
1323        return report;
1324    };
1325    let mut targets = Vec::new();
1326    let mut under_pressure = false;
1327    let mut registrations = std::collections::HashMap::new();
1328    let measurements_path = home.join("output-measurements.json");
1329    let mut measurements: std::collections::BTreeMap<PathBuf, Measurement> =
1330        std::fs::read(&measurements_path)
1331            .ok()
1332            .and_then(|b| serde_json::from_slice(&b).ok())
1333            .unwrap_or_default();
1334    let mut observed = std::collections::BTreeSet::new();
1335    let mut ids: Vec<_> = std::fs::read_dir(home.join("runs"))
1336        .into_iter()
1337        .flatten()
1338        .flatten()
1339        .map(|e| e.file_name().to_string_lossy().into_owned())
1340        .collect();
1341    ids.sort();
1342    for id in ids {
1343        if !crate::run::is_run_id(&id) {
1344            continue;
1345        }
1346        let state = match RunState::load_under(&id, home) {
1347            Ok(state) => state,
1348            Err(e) => {
1349                report
1350                    .retained
1351                    .push(format!("{id}: unreadable run ownership: {e:#}"));
1352                continue;
1353            }
1354        };
1355        if disk.worktree_limit_bytes == 0 {
1356            continue;
1357        }
1358        match run_paths_missing(&state) {
1359            Ok(true) => continue,
1360            Ok(false) => {}
1361            Err(e) => {
1362                report.failures.push(format!("{id}: {e:#}"));
1363                continue;
1364            }
1365        }
1366        let list = match registrations.entry(state.repo.clone()) {
1367            std::collections::hash_map::Entry::Occupied(entry) => entry.into_mut(),
1368            std::collections::hash_map::Entry::Vacant(entry) => {
1369                match git::git(&state.repo, &["worktree", "list", "--porcelain"]).await {
1370                    Ok(list) => entry.insert(list),
1371                    Err(e) => {
1372                        // The checkout may disappear after the inventory check.
1373                        // Do not confuse a missing Git executable with that case.
1374                        if matches!(run_paths_missing(&state), Ok(true)) {
1375                            continue;
1376                        }
1377                        report.failures.push(format!("{id}: {e:#}"));
1378                        continue;
1379                    }
1380                }
1381            }
1382        };
1383        let paths = match registered_worktrees(&state, list) {
1384            Ok(paths) => paths,
1385            Err(e) => {
1386                report.failures.push(format!("{id}: {e:#}"));
1387                continue;
1388            }
1389        };
1390        for path in paths {
1391            if !path.join("target").exists() {
1392                continue;
1393            }
1394            let target = path.join("target");
1395            observed.insert(target.clone());
1396            let pressure =
1397                crate::disk::free_bytes(&path).map_or(true, |free| free < disk.min_free_bytes);
1398            under_pressure |= pressure;
1399            let measurement = measurements.entry(target).or_default();
1400            if pressure
1401                || measurement
1402                    .at
1403                    .is_none_or(|at| crate::clean::due(now, at, 300))
1404            {
1405                measurement.bytes = crate::disk::dir_size(&path.join("target"));
1406                measurement.at = Some(now);
1407            }
1408            let size = measurement.bytes;
1409            report.measured_bytes = report.measured_bytes.saturating_add(size);
1410            targets.push((state.output_activity(), id.clone(), path, size));
1411        }
1412    }
1413    if under_pressure {
1414        // Inventory sizes prioritize scarce-space recovery only; deletion authority
1415        // and budget accounting come from declared outputs measured under locks.
1416        targets.sort_by_key(|t| (std::cmp::Reverse(t.3), t.0));
1417    } else {
1418        targets.sort_by_key(|t| t.0);
1419    }
1420    let free_before = crate::disk::free_bytes(home).ok();
1421    #[cfg(unix)]
1422    {
1423        let mut seen = std::collections::BTreeSet::new();
1424        report.measured_allocated_file_bytes = Some(0);
1425        report.estimated_released_allocated_bytes = Some(0);
1426        report.proven_reclaimable_allocated_bytes = Some(0);
1427        for (_, _, path, _) in &targets {
1428            report.measured_allocated_file_bytes = report
1429                .measured_allocated_file_bytes
1430                .zip(allocated_inventory(&path.join("target"), &mut seen).ok())
1431                .map(|(a, b)| a.saturating_add(b));
1432        }
1433    }
1434    let mut remaining = report.measured_bytes;
1435    let pressure_paths: Vec<_> = targets.iter().map(|t| t.2.clone()).collect();
1436    let mut deletion_started = false;
1437    let mut oversized_started = false;
1438    for (updated, id, path, _) in targets {
1439        if disk.worktree_limit_bytes == 0
1440            || remaining <= disk.worktree_limit_bytes
1441                && crate::disk::free_bytes(&path).is_ok_and(|free| free >= disk.min_free_bytes)
1442        {
1443            report.retained.push(format!("{id}: output reclamation disabled or aggregate usage and free space are within policy"));
1444            continue;
1445        }
1446        if oversized_started || report.freed_bytes >= disk.worktree_reclaim_bytes {
1447            report
1448                .retained
1449                .push(format!("{id}: per-pass byte budget spent"));
1450            continue;
1451        }
1452        let result = async {
1453            ensure!(
1454                crate::clean::due(now, updated, disk.fold_grace_secs),
1455                "execution activity within grace period: {updated} (now {now}, grace {}s)", disk.fold_grace_secs
1456            );
1457            let _owner = crate::pr_head::owner_lock(home, &id)?;
1458            let state = RunState::load_under(&id, home)?;
1459            ensure!(
1460                state.liveness_under(home, crate::daemon::is_working_on(home, &id, now))
1461                    == Liveness::Dead,
1462                "run live or ownership unknown"
1463            );
1464            ensure!(crate::clean::due(now, state.output_activity(), disk.fold_grace_secs), "execution activity after inventory: {}", state.output_activity());
1465            let _driver = crate::driver::maintenance(home, &id)?;
1466            let _outputs = OutputLease::take(&path, true)?;
1467            ensure!(
1468                worktrees(&state).await?.contains(&path),
1469                "worktree registration changed"
1470            );
1471            let target = validate_cargo_target(&path, true).await
1472                .with_context(|| format!("stage validate-target: {}", path.join("target").display()))?;
1473            let _cargo = cargo_locks(&target)
1474                .with_context(|| format!("stage cargo-locks: {}", target.display()))?;
1475            // Cargo may have finished between identity inspection and locking.
1476            // Revalidate the same authority with all writers excluded.
1477            validate_cargo_target(&path, true).await
1478                .with_context(|| format!("stage validate-target (locked): {}", target.display()))?;
1479            let lease_meta = _outputs._file.metadata().context("output lease activity: metadata")?;
1480            if lease_meta.len() > 0 {
1481                ensure!(lease_meta.len() == 1, "output lease activity witness is unreadable or incomplete for {}", path.display());
1482                let lease_at = Timestamp::try_from(lease_meta.modified().context("output lease activity: modified")?)?;
1483                ensure!(crate::clean::due(now, lease_at, disk.fold_grace_secs),
1484                    "output lease activity within grace period: {lease_at} (now {now}, grace {}s)", disk.fold_grace_secs);
1485            }
1486            output_age(&target, now, disk.fold_grace_secs)
1487                .with_context(|| format!("stage output-age: {}", target.display()))?;
1488            let mut outputs = declared_outputs(&target)
1489                .with_context(|| format!("stage declared-outputs: {}", target.display()))?;
1490            let incremental = match crate::incremental::candidates(&path) {
1491                Ok(candidates) => candidates,
1492                Err(error) => {
1493                    report.retained.push(format!("{id}: incremental cache retained: {error:#}"));
1494                    crate::incremental::Reclaim::default()
1495                }
1496            };
1497            let incremental_files = incremental.files.clone();
1498            outputs.extend(incremental_files.iter().cloned());
1499            outputs.sort();
1500            outputs.dedup();
1501            ensure!(!outputs.is_empty(), "no dep-info-proven outputs or provenance-proven incremental caches; legacy incremental/build and unknown files retained");
1502            let sizes = outputs.iter().map(|p| std::fs::metadata(p).map(|m| m.len())
1503                .with_context(|| format!("stage measure: {}", p.display())))
1504                .collect::<Result<Vec<_>>>()?;
1505            let current_size = sizes.iter().fold(0u64, |bytes, size| bytes.saturating_add(*size));
1506            report.proven_reclaimable_bytes = report.proven_reclaimable_bytes.saturating_add(current_size);
1507            let mut allocation = ReleasedAllocation::of(&outputs)?;
1508            report.proven_reclaimable_allocated_bytes = report.proven_reclaimable_allocated_bytes
1509                .zip(allocation.reclaimable_allocated_bytes()).map(|(a, b)| a.saturating_add(b));
1510            let exceeds_budget = current_size > disk.worktree_reclaim_bytes.saturating_sub(report.freed_bytes);
1511            // 予算は1回の削除量を抑えるもので、大きな出力を永久に保持するためではない。
1512            // 正の予算では最初の削除対象だけ超過を許し、その後は予算消費済みとする。
1513            // 途中で失敗しても再度許可しないよう、例外は削除開始時に消費する。
1514            ensure!(!deletion_started || !exceeds_budget,
1515                "declared outputs exceed remaining per-pass byte budget");
1516            // Record adoption before deletion; interrupted deletion is revalidated
1517            // on the next pass. No source file or branch is modified.
1518            let ledger = home.join("runs").join(&id).join("build-outputs.json");
1519            std::fs::write(
1520                &ledger,
1521                serde_json::to_vec(
1522                    &serde_json::json!({"target":target,"bytes":current_size,"at":now,"action":"reclaim-declared-outputs","identity":if target.join("CACHEDIR.TAG").exists() { "cache-tag" } else { "rustc-profile-dep-info" },"files":outputs}),
1523                )?,
1524            ).with_context(|| format!("stage ledger: {}", ledger.display()))?;
1525            // Unknown leftovers still permit artifact-only eviction, but never
1526            // authorize retirement. Only a complete proven residue is adopted.
1527            if !incremental_files.is_empty() {
1528                report.retained.push(format!("{id}: incremental proof does not authorize retirement"));
1529            } else if let Err(error) = record_residue(home, &id, &target, &outputs) {
1530                report.retained.push(format!("{id}: residue not adopted for retirement: {error:#}"));
1531            }
1532            ensure!(worktrees(&state).await?.contains(&path),
1533                "worktree registration changed before deletion");
1534            deletion_started = true;
1535            oversized_started = exceeds_budget;
1536            measurements.remove(&target);
1537            for (output, size) in outputs.iter().zip(sizes) {
1538                std::fs::remove_file(output).with_context(|| format!("stage remove: {}", output.display()))?;
1539                report.estimated_released_allocated_bytes = report.estimated_released_allocated_bytes
1540                    .zip(allocation.unlinked(output)).map(|(a, b)| a.saturating_add(b));
1541                // Preserve actual progress even if a later removal fails.
1542                report.freed_bytes = report.freed_bytes.saturating_add(size);
1543                remaining = remaining.saturating_sub(size);
1544            }
1545            incremental.finish().context("finish incremental reclamation")?;
1546            report.retained.push(format!("{id}: unmanifested files and Cargo metadata retained"));
1547            Ok::<_, anyhow::Error>(())
1548        }
1549        .await;
1550        if let Err(e) = result {
1551            report.retained.push(format!(
1552                "{id}/{}: {e:#}",
1553                path.file_name().unwrap_or_default().to_string_lossy()
1554            ));
1555        }
1556    }
1557    measurements.retain(|path, _| observed.contains(path));
1558    if let Ok(body) = serde_json::to_vec(&measurements)
1559        && let Err(e) = std::fs::write(&measurements_path, body)
1560    {
1561        report.failures.push(format!("output measurements: {e}"));
1562    }
1563    copies::evict(home, disk, now, &mut report).await;
1564    retire_empty(home, disk, now, &mut report).await;
1565    report.protected_or_unproven_bytes = report
1566        .measured_bytes
1567        .saturating_sub(report.proven_reclaimable_bytes);
1568    let pressure_remaining = pressure_paths
1569        .iter()
1570        .any(|path| crate::disk::free_bytes(path).map_or(true, |free| free < disk.min_free_bytes));
1571    if !report.failures.is_empty()
1572        || disk.worktree_limit_bytes > 0
1573            && (remaining > disk.worktree_limit_bytes || pressure_remaining)
1574    {
1575        let details = report
1576            .retained
1577            .iter()
1578            .filter(|reason| {
1579                !reason.contains("within policy")
1580                    && !reason.ends_with("unmanifested files and Cargo metadata retained")
1581            })
1582            .take(4)
1583            .chain(report.failures.iter().take(2))
1584            .cloned()
1585            .collect::<Vec<_>>()
1586            .join("; ");
1587        crate::notices::raise_in(
1588            home,
1589            crate::notices::Notice::warn(
1590                "disk:retention",
1591                format!(
1592                    "Retention remains blocked by the disk cap, free-space floor or inspection failures (logical inventory {}B, proven reclaimable {}B, protected or unproven {}B, unlinked {}B). {} Retained/held targets: {details}. Inspect the cleanup report for active owners, retained recovery sources and failures.",
1593                    report.measured_bytes,
1594                    report.proven_reclaimable_bytes,
1595                    report.protected_or_unproven_bytes,
1596                    report.freed_bytes,
1597                    if report.proven_reclaimable_bytes == 0 {
1598                        "No inspected contents have deletion authority."
1599                    } else {
1600                        "Authorized reclamation was insufficient; remaining protections and budgets still apply."
1601                    }
1602                ),
1603            ),
1604        );
1605    }
1606    report.observed_home_free_change_bytes = free_before
1607        .zip(crate::disk::free_bytes(home).ok())
1608        .map(|(before, after)| {
1609            (i128::from(after) - i128::from(before))
1610                .clamp(i128::from(i64::MIN), i128::from(i64::MAX)) as i64
1611        });
1612    let store = home.join("retention.json");
1613    if let Ok(body) = serde_json::to_vec_pretty(&report) {
1614        let tmp = home.join("retention.json.tmp");
1615        if let Err(e) = std::fs::write(&tmp, body).and_then(|()| std::fs::rename(tmp, store)) {
1616            tracing::warn!("retention report could not be saved: {e}");
1617        }
1618    }
1619    tracing::info!(
1620        measured_bytes = report.measured_bytes,
1621        freed_bytes = report.freed_bytes,
1622        retired = report.retired,
1623        retained = report.retained.len(),
1624        failures = report.failures.len(),
1625        "retained worktree cleanup"
1626    );
1627    for reason in report.retained.iter().chain(&report.failures) {
1628        tracing::debug!(%reason, "worktree retained");
1629    }
1630    report
1631}
1632
1633async fn retire_empty(
1634    home: &Path,
1635    disk: &crate::config::Disk,
1636    now: Timestamp,
1637    report: &mut Report,
1638) {
1639    if !disk.auto_fold {
1640        return;
1641    }
1642    let queue = crate::queue::Queue::at(home.join("queue"));
1643    let questions = crate::ask::Questions::at(home.join("questions"));
1644    let open = questions.list_for_effects();
1645    for listed in queue.list() {
1646        let Ok(_claim) = queue.claim(&listed.id) else {
1647            continue;
1648        };
1649        let Ok(task) = queue.get(&listed.id) else {
1650            continue;
1651        };
1652        if open
1653            .iter()
1654            .any(|q| (q.run == task.id || task.blocked_by.contains(&q.id)) && q.status.open())
1655        {
1656            report.retained.push(format!(
1657                "{}: task has an unanswered owner decision",
1658                task.id
1659            ));
1660            continue;
1661        }
1662        let sources: Vec<_> = task
1663            .runs
1664            .iter()
1665            .collect::<std::collections::BTreeSet<_>>()
1666            .into_iter()
1667            .cloned()
1668            .collect();
1669        let mut useful = Vec::new();
1670        let mut selected = Vec::new();
1671        let mut public = Vec::new();
1672        let mut unreadable = false;
1673        for id in &sources {
1674            let Ok(state) = RunState::load_under(id, home) else {
1675                unreadable = true;
1676                continue;
1677            };
1678            if state.released() || state.retired_to.is_some() {
1679                continue;
1680            }
1681            if task.pr_recovery.is_some() && state.pr_recovery == task.pr_recovery {
1682                selected.push(state.clone());
1683            }
1684            if state.pr.is_some() && state.pr_taken_over_by.is_none() {
1685                public.push(state.clone());
1686            }
1687            if state.candidates.iter().any(|c| c.commits > 0 && !c.empty) {
1688                useful.push(state);
1689            }
1690        }
1691        // Prefer a durable owner-approved selection, then one existing PR
1692        // owner, then an unambiguous useful source. Distinct historical changes
1693        // without an owner selection cannot be resolved by creation time.
1694        let pool = if !selected.is_empty() {
1695            selected
1696        } else if !public.is_empty() {
1697            public
1698        } else {
1699            useful
1700        };
1701        if unreadable || pool.len() != 1 {
1702            report.retained.push(format!(
1703                "{}: recovery authority is ambiguous or history is unreadable; {} sources retained",
1704                task.id,
1705                sources.len()
1706            ));
1707            continue;
1708        }
1709        let authority = &pool[0];
1710        if authority.candidates.is_empty() {
1711            continue;
1712        }
1713        let latest = &authority.id;
1714        for id in &sources {
1715            if id == latest || report.retired >= 5 {
1716                continue;
1717            }
1718            if let Ok(state) = RunState::load_under(id, home)
1719                && state.retired_to.as_ref() == Some(latest)
1720                && worktrees(&state).await.is_ok_and(|paths| paths.is_empty())
1721            {
1722                continue;
1723            }
1724            let result = async {
1725                ensure!(
1726                    !task
1727                        .resume_override
1728                        .as_ref()
1729                        .is_some_and(|pin| pin.pinned_run.as_ref() == Some(id)),
1730                    "operator-pinned recovery run"
1731                );
1732                ensure!(
1733                    !task.replacement.as_ref().is_some_and(|pin| pin.run == *id),
1734                    "selected replacement recovery source"
1735                );
1736                let _owner = crate::pr_head::owner_lock(home, id)?;
1737                let mut state = RunState::load_under(id, home)?;
1738                ensure!(
1739                    state.repo == authority.repo,
1740                    "repository differs from recovery authority"
1741                );
1742                ensure!(
1743                    !crate::pr_recovery::explicit_sources_retained(home, id),
1744                    "explicitly retained recovery source"
1745                );
1746                ensure!(
1747                    state.liveness_under(home, crate::daemon::is_working_on(home, id, now))
1748                        == Liveness::Dead,
1749                    "run live or ownership unknown"
1750                );
1751                let _driver = crate::driver::maintenance(home, id)?;
1752                ensure!(
1753                    crate::clean::due(now, state.updated_at, disk.fold_grace_secs),
1754                    "within grace period"
1755                );
1756                ensure!(
1757                    !open.iter().any(|q| q.run == *id && q.status.open()),
1758                    "unanswered owner decision"
1759                );
1760                ensure!(
1761                    state.pr.is_none()
1762                        && state.pr_head.is_none()
1763                        && state.pr_recovery.is_none()
1764                        && state.land_approval.is_none()
1765                        && state.merge_decision.is_none()
1766                        && state.reviews.is_empty()
1767                        && state.recovery.is_none()
1768                        && state.candidate_preservation.is_empty()
1769                        && state.interrupted_implementations.is_empty()
1770                        && !state.released(),
1771                    "PR ownership or recovery checkpoint"
1772                );
1773                let proof = prove_empty(home, &state, latest).await?;
1774                commit_retirement(home, &queue, &task, &mut state, latest, &sources, now)?;
1775                // Durable retirement precedes removal. A crash retries removal
1776                // using the same proof; it cannot make this attempt resumable.
1777                for path in &proof.paths {
1778                    ensure!(
1779                        git::worktree_remove_clean(&state.repo, path).await?,
1780                        "Git refused clean worktree removal"
1781                    );
1782                }
1783                Ok::<_, anyhow::Error>(())
1784            }
1785            .await;
1786            match result {
1787                Ok(()) => report.retired += 1,
1788                Err(e) => report.retained.push(format!("{id}: {e:#}")),
1789            }
1790        }
1791    }
1792}
1793
1794struct EmptyProof {
1795    paths: Vec<PathBuf>,
1796    _outputs: Vec<OutputLease>,
1797    _cargo: Vec<File>,
1798}
1799
1800// Compare actual source bytes with the original tree independently of index
1801// stat caches, assume-unchanged flags, sparse checkout and fsmonitor hints.
1802pub(crate) async fn tracked_base(worktree: &Path, base: &str) -> Result<()> {
1803    let tree = git::git(worktree, &["ls-tree", "-r", "--full-tree", "-z", base]).await?;
1804    let mut files = Vec::new();
1805    let mut blobs = Vec::new();
1806    for record in tree.split('\0').filter(|s| !s.is_empty()) {
1807        let (header, name) = record.split_once('\t').context("unknown tree record")?;
1808        let parts: Vec<_> = header.split_whitespace().collect();
1809        ensure!(
1810            parts.len() == 3 && matches!(parts[0], "100644" | "100755") && parts[1] == "blob",
1811            "unsupported tracked source kind"
1812        );
1813        let path = worktree.join(name);
1814        let meta = std::fs::symlink_metadata(&path)?;
1815        ensure!(
1816            meta.is_file() && !meta.file_type().is_symlink() && path.canonicalize()? == path,
1817            "noncanonical tracked source"
1818        );
1819        #[cfg(unix)]
1820        {
1821            use std::os::unix::fs::PermissionsExt as _;
1822            ensure!(
1823                (meta.permissions().mode() & 0o111 != 0) == (parts[0] == "100755"),
1824                "tracked executable mode differs from original base"
1825            );
1826        }
1827        path.to_str().context("non UTF-8 source")?;
1828        files.push(git::path_arg(&path));
1829        blobs.push(parts[2].to_owned());
1830    }
1831    for (paths, expected) in files.chunks(64).zip(blobs.chunks(64)) {
1832        let mut args = vec!["hash-object", "--no-filters", "--"];
1833        args.extend(paths.iter().map(String::as_str));
1834        let actual = git::git(worktree, &args).await?;
1835        ensure!(
1836            actual.lines().eq(expected.iter().map(String::as_str)),
1837            "tracked bytes differ from original base"
1838        );
1839    }
1840    Ok(())
1841}
1842
1843async fn prove_empty(home: &Path, state: &RunState, authority: &str) -> Result<EmptyProof> {
1844    let id = &state.id;
1845    let paths = worktrees(state).await?;
1846    let mut guards = Vec::new();
1847    let mut cargo_guards = Vec::new();
1848    for path in &paths {
1849        guards.push(OutputLease::take(path, true)?);
1850        ensure!(
1851            git::rev_parse(path, "HEAD").await? == state.base_commit,
1852            "unique or unrecorded commits"
1853        );
1854        ensure!(
1855            git::git(
1856                path,
1857                &[
1858                    "--no-optional-locks",
1859                    "status",
1860                    "--porcelain",
1861                    "--untracked-files=all"
1862                ]
1863            )
1864            .await?
1865            .is_empty(),
1866            "tracked or untracked source changes"
1867        );
1868        ensure!(
1869            git::git(
1870                path,
1871                &[
1872                    "--no-optional-locks",
1873                    "status",
1874                    "--porcelain",
1875                    "--untracked-files=all",
1876                    "--ignored",
1877                    "--",
1878                    ".",
1879                    ":(exclude)target"
1880                ]
1881            )
1882            .await?
1883            .is_empty(),
1884            "source changes, ignored files or build outputs"
1885        );
1886        tracked_base(path, &state.base_commit).await?;
1887        match std::fs::symlink_metadata(path.join("target")) {
1888            Ok(_) => cargo_guards.extend(verify_residue(home, id, path).await?),
1889            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
1890            Err(error) => return Err(error.into()),
1891        }
1892        for checkpoint in [
1893            "rebase-merge",
1894            "rebase-apply",
1895            "sequencer",
1896            "BISECT_LOG",
1897            "REVERT_HEAD",
1898            "AUTO_MERGE",
1899            "MERGE_MSG",
1900            "CHERRY_PICK_HEAD",
1901            "MERGE_HEAD",
1902            "REBASE_HEAD",
1903        ] {
1904            let location = git::git(
1905                path,
1906                &[
1907                    "rev-parse",
1908                    "--path-format=absolute",
1909                    "--git-path",
1910                    checkpoint,
1911                ],
1912            )
1913            .await?;
1914            ensure!(
1915                matches!(std::fs::symlink_metadata(location.trim()), Err(e) if e.kind() == std::io::ErrorKind::NotFound),
1916                "retained Git recovery metadata: {checkpoint}"
1917            );
1918        }
1919        // Git also initializes ORIG_HEAD when creating a worktree.
1920        // Only an origin outside the base is a recovery source.
1921        let origin_path = git::git(
1922            path,
1923            &[
1924                "rev-parse",
1925                "--path-format=absolute",
1926                "--git-path",
1927                "ORIG_HEAD",
1928            ],
1929        )
1930        .await?;
1931        match std::fs::symlink_metadata(origin_path.trim()) {
1932            Ok(meta) => {
1933                ensure!(
1934                    meta.is_file() && !meta.file_type().is_symlink(),
1935                    "unknown ORIG_HEAD artifact"
1936                );
1937                git::rev_parse(path, "ORIG_HEAD")
1938                    .await
1939                    .context("unreadable ORIG_HEAD")?;
1940            }
1941            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
1942            Err(e) => return Err(e.into()),
1943        }
1944        if let Ok(original) = git::rev_parse(path, "ORIG_HEAD").await {
1945            ensure!(
1946                git::git_raw(
1947                    path,
1948                    &["merge-base", "--is-ancestor", &original, &state.base_commit]
1949                )
1950                .await?
1951                .ok(),
1952                "ORIG_HEAD retains commits outside the original base"
1953            );
1954        }
1955        let heads = git::git(path, &["reflog", "show", "--format=%H", "HEAD"]).await?;
1956        for head in heads.lines().collect::<std::collections::BTreeSet<_>>() {
1957            ensure!(
1958                git::git_raw(
1959                    path,
1960                    &["merge-base", "--is-ancestor", head, &state.base_commit]
1961                )
1962                .await?
1963                .ok(),
1964                "worktree reflog retains commits outside the original base"
1965            );
1966        }
1967        for checkpoint in ["CHERRY_PICK_HEAD", "MERGE_HEAD", "REBASE_HEAD"] {
1968            ensure!(
1969                !git::git_raw(path, &["rev-parse", "--verify", checkpoint])
1970                    .await?
1971                    .ok(),
1972                "unfinished Git operation"
1973            );
1974        }
1975    }
1976    // Missing worktrees cannot establish that unrecorded prep edits
1977    // were empty. Recorded candidates must also point at the base.
1978    if paths.is_empty() && state.retired_to.as_deref() == Some(authority) {
1979        return Ok(EmptyProof {
1980            paths,
1981            _outputs: guards,
1982            _cargo: cargo_guards,
1983        });
1984    }
1985    ensure!(!paths.is_empty(), "no worktrees to prove empty");
1986    for candidate in &state.candidates {
1987        ensure!(
1988            (paths.contains(&candidate.worktree)
1989                || state.retired_to.as_deref() == Some(authority) && !candidate.worktree.exists())
1990                && git::rev_parse(&state.repo, &candidate.branch).await? == state.base_commit,
1991            "candidate recovery source"
1992        );
1993    }
1994    let pattern = format!("refs/heads/magi/{}/", state.short());
1995    let branches = git::git(
1996        &state.repo,
1997        &["for-each-ref", "--format=%(objectname)", &pattern],
1998    )
1999    .await?;
2000    ensure!(
2001        branches.lines().all(|head| head == state.base_commit),
2002        "unrecorded candidate branch changes"
2003    );
2004    Ok(EmptyProof {
2005        paths,
2006        _outputs: guards,
2007        _cargo: cargo_guards,
2008    })
2009}
2010
2011/// The only task-write critical section in retirement. All Git proof and
2012/// deletion happens outside it, under the run's owner/driver/output leases.
2013/// Atomic task writes make the earlier snapshot consistent; this final compare
2014/// rejects every intervening owner selection, hold, edit or history update.
2015fn commit_retirement(
2016    home: &Path,
2017    queue: &crate::queue::Queue,
2018    expected: &crate::queue::Task,
2019    state: &mut RunState,
2020    authority: &str,
2021    sources: &[String],
2022    now: Timestamp,
2023) -> Result<()> {
2024    let _write = queue.lock_task(&expected.id)?;
2025    let current = queue.get(&expected.id)?;
2026    ensure!(
2027        serde_json::to_value(&current)? == serde_json::to_value(expected)?,
2028        "task changed during retirement proof; retaining worktrees for the current owner decision"
2029    );
2030    ensure!(
2031        !current
2032            .resume_override
2033            .as_ref()
2034            .is_some_and(|pin| pin.pinned_run.as_ref() == Some(&state.id))
2035            && !current
2036                .replacement
2037                .as_ref()
2038                .is_some_and(|pin| pin.run == state.id),
2039        "current owner selected this recovery source"
2040    );
2041    ensure!(
2042        !crate::pr_recovery::explicit_sources_retained(home, &state.id),
2043        "explicitly retained recovery source"
2044    );
2045    ensure!(
2046        !crate::ask::Questions::at(home.join("questions"))
2047            .list_for_effects()
2048            .iter()
2049            .any(|q| q.status.open()
2050                && (q.run == state.id
2051                    || q.run == current.id
2052                    || current.blocked_by.contains(&q.id))),
2053        "owner decision arrived during retirement proof"
2054    );
2055    let dir = home.join("recovery-lineages");
2056    std::fs::create_dir_all(&dir)?;
2057    let tmp = dir.join(format!("{}.tmp", current.id));
2058    let lineage = serde_json::json!({"task":current.id,"authority":authority,"sources":sources,
2059        "selection":current.pr_recovery,"task_updated_at":current.updated_at,"at":now});
2060    std::fs::write(&tmp, serde_json::to_vec_pretty(&lineage)?)?;
2061    std::fs::rename(tmp, dir.join(format!("{}.json", current.id)))?;
2062    state.retired_to = Some(authority.to_owned());
2063    state.event(
2064        "retention",
2065        format!(
2066            "empty attempt retired to {authority}; recovery sources: {}",
2067            sources.join(", ")
2068        ),
2069    );
2070    state.save_under(home)?;
2071    // No Git command or worktree removal may be added before this guard drops.
2072    Ok(())
2073}
2074
2075/// Wait for the emergency reserve to be exhausted. The normal floor requests
2076/// a checkpoint first; half the floor stops owned writers before node completion.
2077/// A failed measurement also closes admission rather than allowing blind writes.
2078pub(crate) async fn emergency(paths: Vec<PathBuf>, min_free: u64, isolated: bool) -> String {
2079    let measure = if isolated {
2080        crate::disk::managed_free_bytes
2081    } else {
2082        crate::disk::free_bytes
2083    };
2084    if min_free == 0 {
2085        return std::future::pending().await;
2086    }
2087    loop {
2088        for path in &paths {
2089            let existing = path.ancestors().find(|p| p.exists()).unwrap_or(path);
2090            match measure(existing) {
2091                Ok(free) if free >= min_free / 2 => {}
2092                Ok(free) => {
2093                    return format!(
2094                        "disk pressure: {free} bytes free; stopping owned writers to preserve the emergency reserve"
2095                    );
2096                }
2097                Err(e) => return format!("disk pressure: free space measurement failed: {e:#}"),
2098            }
2099        }
2100        tokio::time::sleep(std::time::Duration::from_secs(5)).await;
2101    }
2102}
2103
2104#[cfg(all(test, unix))]
2105mod tests {
2106    use super::*;
2107    use crate::{
2108        config::{Config, Disk},
2109        queue::{Source, Task},
2110        run::{Candidate, RunStatus},
2111    };
2112
2113    struct Fx {
2114        _temp: tempfile::TempDir,
2115        home: PathBuf,
2116        repo: PathBuf,
2117        bay: PathBuf,
2118        base: String,
2119    }
2120    impl Fx {
2121        async fn new() -> Self {
2122            let temp = tempfile::tempdir().unwrap();
2123            let root = temp.path().canonicalize().unwrap();
2124            let home = root.join("home");
2125            let repo = root.join("repo");
2126            let bay = root.join("worktrees");
2127            std::fs::create_dir_all(&repo).unwrap();
2128            std::fs::create_dir_all(home.join("runs")).unwrap();
2129            git::git(&repo, &["init", "-b", "main"]).await.unwrap();
2130            git::git(&repo, &["config", "user.name", "Fixture"])
2131                .await
2132                .unwrap();
2133            git::git(&repo, &["config", "user.email", "fixture@example.invalid"])
2134                .await
2135                .unwrap();
2136            git::git(&repo, &["config", "commit.gpgsign", "false"])
2137                .await
2138                .unwrap();
2139            std::fs::write(repo.join(".gitignore"), "target/\nscratch/\n").unwrap();
2140            std::fs::write(repo.join("source"), "base\n").unwrap();
2141            git::git(&repo, &["add", "."]).await.unwrap();
2142            git::git(&repo, &["commit", "-m", "fixture"]).await.unwrap();
2143            let base = git::rev_parse(&repo, "HEAD").await.unwrap();
2144            Self {
2145                _temp: temp,
2146                home,
2147                repo,
2148                bay,
2149                base,
2150            }
2151        }
2152        async fn run(&self, tag: &str, useful: bool) -> RunState {
2153            let mut cfg = Config::default();
2154            cfg.graph.worktree_root = Some(self.bay.clone());
2155            let mut state = RunState::new(
2156                self.repo.clone(),
2157                "main".into(),
2158                self.base.clone(),
2159                "task".into(),
2160                cfg,
2161            );
2162            state.id = format!("20260101-000000-{tag}");
2163            state.status = RunStatus::Blocked;
2164            state.driver_exited = true;
2165            let wt = state.worktree_root().join("cand-A");
2166            let branch = format!("magi/{tag}/A");
2167            git::worktree_add_branch(&self.repo, &wt, &branch, &self.base)
2168                .await
2169                .unwrap();
2170            if useful {
2171                std::fs::write(wt.join("source"), tag).unwrap();
2172                git::git(&wt, &["add", "."]).await.unwrap();
2173                git::git(&wt, &["commit", "-m", "candidate change"])
2174                    .await
2175                    .unwrap();
2176            }
2177            state.candidates.push(Candidate {
2178                index: 0,
2179                label: 'A',
2180                agent: "fixture".into(),
2181                branch,
2182                worktree: wt,
2183                summary: String::new(),
2184                stat: String::new(),
2185                files: usize::from(useful),
2186                commits: usize::from(useful),
2187                empty: !useful,
2188                failed: None,
2189                verified_noop: None,
2190                duration_ms: 0,
2191                folded: false,
2192            });
2193            state.save_under(&self.home).unwrap();
2194            state
2195        }
2196        fn disk() -> Disk {
2197            Disk {
2198                worktree_limit_bytes: 1,
2199                worktree_reclaim_bytes: 1024 * 1024,
2200                fold_grace_secs: 0,
2201                min_free_bytes: 0,
2202                ..Disk::default()
2203            }
2204        }
2205        fn target(wt: &Path) {
2206            let target = wt.join("target");
2207            std::fs::create_dir_all(target.join("debug")).unwrap();
2208            let mut artifact = [1; 200];
2209            artifact[..4].copy_from_slice(b"\x7fELF");
2210            std::fs::write(target.join("debug/output"), artifact).unwrap();
2211            let output = target
2212                .join("debug/output")
2213                .display()
2214                .to_string()
2215                .replace('\\', "\\\\")
2216                .replace(' ', "\\ ");
2217            std::fs::write(target.join("debug/output.d"), format!("{output}: source\n")).unwrap();
2218            std::fs::write(target.join("debug/.cargo-lock"), "").unwrap();
2219            std::fs::write(
2220                target.join("CACHEDIR.TAG"),
2221                "Signature: 8a477f597d28d172789f06886806bc55\n",
2222            )
2223            .unwrap();
2224            std::fs::write(
2225                target.join(".rustc_info.json"),
2226                "{\"rustc_fingerprint\":123}",
2227            )
2228            .unwrap();
2229        }
2230        fn now() -> Timestamp {
2231            Timestamp::now() + jiff::SignedDuration::from_secs(2)
2232        }
2233        fn task(&self, runs: &[RunState]) -> Task {
2234            let mut task = Task::new(
2235                "fixture".into(),
2236                "task".into(),
2237                self.repo.clone(),
2238                Source::Human,
2239            );
2240            task.runs = runs.iter().map(|s| s.id.clone()).collect();
2241            crate::queue::Queue::at(self.home.join("queue"))
2242                .put(&mut task)
2243                .unwrap();
2244            task
2245        }
2246    }
2247
2248    #[cfg(unix)]
2249    #[tokio::test]
2250    async fn incremental_only_reclamation_preserves_legacy_and_unknown_data() {
2251        let f = Fx::new().await;
2252        let state = f.run("ca01", true).await;
2253        let wt = &state.candidates[0].worktree;
2254        let generation = crate::incremental::tests::generate(wt);
2255        // Remove dep-info authority: incremental proof must be independent.
2256        assert!(!declared_outputs(&wt.join("target")).unwrap().is_empty());
2257        for dir in profile_output_dirs(&wt.join("target/debug"), true).unwrap() {
2258            for entry in std::fs::read_dir(dir).unwrap() {
2259                let path = entry.unwrap().path();
2260                if path.extension().is_some_and(|x| x == "d") {
2261                    std::fs::remove_file(path).unwrap();
2262                }
2263            }
2264        }
2265        assert!(declared_outputs(&wt.join("target")).unwrap().is_empty());
2266        assert_eq!(
2267            sweep(&f.home, &Fx::disk(), Fx::now()).await.freed_bytes,
2268            0,
2269            "live generation is protected"
2270        );
2271        drop(generation);
2272        let candidates = crate::incremental::candidates(wt).unwrap();
2273        let logical: u64 = candidates
2274            .files
2275            .iter()
2276            .map(|p| std::fs::metadata(p).unwrap().len())
2277            .sum();
2278        let unknown = candidates.files[0].parent().unwrap().join("operator-note");
2279        std::fs::write(&unknown, "retain unknown data").unwrap();
2280        drop(candidates);
2281        let refused = sweep(&f.home, &Fx::disk(), Fx::now()).await;
2282        assert_eq!(refused.freed_bytes, 0, "{refused:?}");
2283        assert!(
2284            refused
2285                .retained
2286                .iter()
2287                .any(|s| s.contains("provenance mismatch"))
2288        );
2289        std::fs::remove_file(&unknown).unwrap();
2290        let legacy = wt.join("target/debug/incremental/legacy-session");
2291        std::fs::create_dir(&legacy).unwrap();
2292        // Even compiler-authored bytes do not retroactively confer ownership.
2293        let candidates = crate::incremental::candidates(wt).unwrap();
2294        let bin = candidates
2295            .files
2296            .iter()
2297            .find(|p| p.extension().is_some_and(|x| x == "bin"))
2298            .unwrap();
2299        std::fs::copy(bin, legacy.join("query-cache.bin")).unwrap();
2300        drop(candidates);
2301        let cargo = cargo_locks(&wt.join("target")).unwrap();
2302        assert_eq!(sweep(&f.home, &Fx::disk(), Fx::now()).await.freed_bytes, 0);
2303        drop(cargo);
2304        let outputs = OutputLease::borrow(wt).unwrap().unwrap();
2305        assert_eq!(sweep(&f.home, &Fx::disk(), Fx::now()).await.freed_bytes, 0);
2306        drop(outputs);
2307        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
2308        assert_eq!(report.freed_bytes, logical, "{report:?}");
2309        assert_eq!(report.proven_reclaimable_bytes, logical);
2310        assert!(report.measured_bytes > logical);
2311        assert_eq!(
2312            report.protected_or_unproven_bytes,
2313            report.measured_bytes - logical
2314        );
2315        #[cfg(unix)]
2316        {
2317            assert!(report.estimated_released_allocated_bytes.unwrap() > 0);
2318            assert!(
2319                report.measured_allocated_file_bytes.unwrap()
2320                    >= report.estimated_released_allocated_bytes.unwrap()
2321            );
2322        }
2323        assert!(report.observed_home_free_change_bytes.is_some());
2324        assert!(legacy.join("query-cache.bin").is_file());
2325        assert!(wt.join("src/lib.rs").is_file());
2326        assert_eq!(report.retired, 0);
2327    }
2328
2329    #[cfg(unix)]
2330    #[tokio::test]
2331    async fn incremental_and_deps_links_account_for_retained_third_link() {
2332        use std::os::unix::fs::MetadataExt;
2333        let f = Fx::new().await;
2334        let state = f.run("ca02", true).await;
2335        let wt = &state.candidates[0].worktree;
2336        let generation = crate::incremental::tests::generate(wt);
2337        drop(generation);
2338        let candidates = crate::incremental::candidates(wt).unwrap();
2339        let objects = candidates
2340            .files
2341            .iter()
2342            .filter(|p| p.extension().is_some_and(|x| x == "o"))
2343            .take(2)
2344            .cloned()
2345            .collect::<Vec<_>>();
2346        assert_eq!(
2347            objects.len(),
2348            2,
2349            "fixture must produce real incremental objects"
2350        );
2351        let mut outputs = candidates.files.clone();
2352        drop(candidates);
2353        let target = wt.join("target");
2354        assert!(!declared_outputs(&target).unwrap().is_empty());
2355        let generated_dirs = profile_output_dirs(&target.join("debug"), true).unwrap();
2356        // The directory Cargo itself wrote its dep-info into (v1 `deps`, v2 unit
2357        // `out`): the links below go there, so the real layout is exercised.
2358        let mut output_dir = None;
2359        for dir in &generated_dirs {
2360            for entry in std::fs::read_dir(dir).unwrap() {
2361                let path = entry.unwrap().path();
2362                if path.extension().is_some_and(|x| x == "d") {
2363                    std::fs::remove_file(path).unwrap();
2364                    if dir != &generated_dirs[0] {
2365                        output_dir = Some(dir.clone());
2366                    }
2367                }
2368            }
2369        }
2370        let output_dir = output_dir.expect("Cargo generated no recognised output directory");
2371        // Cargo also leaves hard-linked object outputs beside the dep-info.
2372        // Remove those fixture outputs so the only external links are the
2373        // ones below.
2374        for entry in std::fs::read_dir(&output_dir).unwrap() {
2375            let path = entry.unwrap().path();
2376            if path.is_file() {
2377                std::fs::remove_file(path).unwrap();
2378            }
2379        }
2380        // v2 outputs need a dep-info stem matching a compiler fingerprint of
2381        // their unit; v1 has no such rule.
2382        let stem = if output_dir.file_name().is_some_and(|n| n == "out") {
2383            let unit = output_dir.parent().unwrap();
2384            let hash = unit.file_name().unwrap().to_string_lossy().into_owned();
2385            let name = std::fs::read_dir(unit.join("fingerprint"))
2386                .unwrap()
2387                .filter_map(|e| e.unwrap().file_name().into_string().ok())
2388                .find_map(|n| {
2389                    n.strip_prefix("lib-")
2390                        .filter(|n| !n.contains('.'))
2391                        .map(str::to_owned)
2392                })
2393                .expect("Cargo unit has a lib fingerprint");
2394            format!("{}-{hash}", name.replace('-', "_"))
2395        } else {
2396            "link".to_owned()
2397        };
2398        // One dep-info file may declare several outputs; the names differ only
2399        // by the optional `lib` prefix the artifact check accepts.
2400        let mut declared = Vec::new();
2401        for (index, object) in objects.iter().enumerate() {
2402            assert_eq!(std::fs::metadata(object).unwrap().nlink(), 1);
2403            let dep = output_dir.join(if index == 0 {
2404                stem.clone()
2405            } else {
2406                format!("lib{stem}")
2407            });
2408            std::fs::hard_link(object, &dep).unwrap();
2409            declared.push(dep.display().to_string());
2410            outputs.push(dep);
2411        }
2412        std::fs::write(
2413            output_dir.join(format!("{stem}.d")),
2414            format!("{}: src/lib.rs\n", declared.join(" ")),
2415        )
2416        .unwrap();
2417        let retained = wt.join("retained-input");
2418        std::fs::hard_link(&objects[1], &retained).unwrap();
2419        let sentinel = std::fs::read(&retained).unwrap();
2420        let logical: u64 = outputs
2421            .iter()
2422            .map(|p| std::fs::metadata(p).unwrap().len())
2423            .sum();
2424        let mut seen = std::collections::BTreeSet::new();
2425        let retained_meta = std::fs::metadata(&retained).unwrap();
2426        let retained_inode = (retained_meta.dev(), retained_meta.ino());
2427        let mut expected = 0u64;
2428        for output in &outputs {
2429            let m = std::fs::metadata(output).unwrap();
2430            let inode = (m.dev(), m.ino());
2431            if inode != retained_inode && seen.insert(inode) {
2432                expected += m.blocks() * 512;
2433            }
2434        }
2435        assert!(expected > 0);
2436        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
2437        assert_eq!(report.proven_reclaimable_bytes, logical, "{report:?}");
2438        assert_eq!(report.freed_bytes, logical, "{report:?}");
2439        assert_eq!(report.proven_reclaimable_allocated_bytes, Some(expected));
2440        assert_eq!(report.estimated_released_allocated_bytes, Some(expected));
2441        assert!(outputs.iter().all(|p| !p.exists()));
2442        assert_eq!(std::fs::read(retained).unwrap(), sentinel);
2443        assert!(wt.join("src/lib.rs").is_file());
2444    }
2445
2446    #[tokio::test]
2447    async fn retention_notice_explains_unproven_inventory_and_live_owner() {
2448        let f = Fx::new().await;
2449        let state = f.run("ca03", true).await;
2450        let wt = &state.candidates[0].worktree;
2451        Fx::target(wt);
2452        std::fs::remove_file(wt.join("target/debug/output")).unwrap();
2453        let unknown = wt.join("target/debug/operator-note");
2454        std::fs::write(&unknown, vec![1; 4096]).unwrap();
2455        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
2456        assert_eq!(report.proven_reclaimable_bytes, 0);
2457        #[cfg(unix)]
2458        assert_eq!(report.proven_reclaimable_allocated_bytes, Some(0));
2459        assert!(report.protected_or_unproven_bytes >= 4096);
2460        let message = || {
2461            crate::notices::Notices::at(f.home.join("notifications"))
2462                .list()
2463                .into_iter()
2464                .find(|n| n.key == "disk:retention")
2465                .unwrap()
2466                .message
2467        };
2468        let notice = message();
2469        assert!(notice.contains("proven reclaimable 0B"), "{notice}");
2470        assert!(notice.contains("protected or unproven"), "{notice}");
2471        assert!(
2472            notice.contains("legacy incremental/build and unknown files retained"),
2473            "{notice}"
2474        );
2475        let owner = crate::pr_head::owner_lock(&f.home, &state.id).unwrap();
2476        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
2477        assert_eq!(report.freed_bytes, 0);
2478        let notice = message();
2479        assert!(notice.contains(&state.id), "{notice}");
2480        assert!(
2481            notice.contains("lock") || notice.contains("owner"),
2482            "{notice}"
2483        );
2484        drop(owner);
2485        assert_eq!(std::fs::read(unknown).unwrap(), vec![1; 4096]);
2486    }
2487
2488    #[tokio::test]
2489    async fn real_cargo_build_holds_the_target_against_reclamation() {
2490        let f = Fx::new().await;
2491        let state = f.run("ca02", true).await;
2492        let wt = &state.candidates[0].worktree;
2493        Fx::target(wt);
2494        std::fs::create_dir(wt.join("src")).unwrap();
2495        std::fs::write(
2496            wt.join("Cargo.toml"),
2497            "[package]\nname='live-cache-fixture'\nversion='0.1.0'\nedition='2021'\n[workspace]\n",
2498        )
2499        .unwrap();
2500        std::fs::write(wt.join("src/lib.rs"), "pub fn answer() -> usize { 42 }\n").unwrap();
2501        std::fs::write(
2502            wt.join("build.rs"),
2503            r#"fn main() {
2504            println!("cargo:rerun-if-env-changed=FIXTURE_READY");
2505            let Ok(ready) = std::env::var("FIXTURE_READY") else { return };
2506            let release = std::env::var("FIXTURE_RELEASE").unwrap();
2507            std::fs::write(ready, "ready").unwrap();
2508            let start = std::time::Instant::now();
2509            while !std::path::Path::new(&release).exists() {
2510                assert!(start.elapsed().as_secs() < 120, "fixture was not released");
2511                std::thread::sleep(std::time::Duration::from_millis(10));
2512            }
2513        }"#,
2514        )
2515        .unwrap();
2516        let ready = wt.join("ready");
2517        let release = wt.join("release");
2518        let mut cargo = tokio::process::Command::new("cargo");
2519        cargo.env_clear();
2520        cargo.env(
2521            "PATH",
2522            crate::build_target::command::standalone_fixture_path().unwrap(),
2523        );
2524        for key in ["HOME", "RUSTUP_HOME"] {
2525            if let Some(value) = std::env::var_os(key) {
2526                cargo.env(key, value);
2527            }
2528        }
2529        cargo
2530            .current_dir(wt)
2531            .env("RUSTUP_TOOLCHAIN", "stable")
2532            .env("CARGO_HOME", f._temp.path().join("cargo-home"))
2533            .env("CARGO_INCREMENTAL", "1")
2534            .args(["build", "--offline", "--jobs", "1", "--target-dir"])
2535            .arg(wt.join("target"));
2536        // Compile before timing the active-build handshake. Under a parallel
2537        // gate, even this tiny fixture's first compiler startup can exceed the
2538        // readiness window; compilation speed is not what this test verifies.
2539        let warm = cargo.output().await.unwrap();
2540        assert!(
2541            warm.status.success(),
2542            "fixture compilation failed: {}",
2543            String::from_utf8_lossy(&warm.stderr)
2544        );
2545        let log_path = f._temp.path().join("live-cargo.stderr");
2546        let mut child = cargo
2547            .env("FIXTURE_READY", &ready)
2548            .env("FIXTURE_RELEASE", &release)
2549            .stdout(std::process::Stdio::null())
2550            .stderr(std::fs::File::create(&log_path).unwrap())
2551            .kill_on_drop(true)
2552            .spawn()
2553            .unwrap();
2554        let readiness = tokio::time::timeout(std::time::Duration::from_secs(60), async {
2555            while !ready.exists() {
2556                if let Some(status) = child.try_wait().unwrap() {
2557                    return Err(format!("fixture exited before readiness: {status}"));
2558                }
2559                tokio::time::sleep(std::time::Duration::from_millis(10)).await;
2560            }
2561            Ok(())
2562        })
2563        .await;
2564        let arrived = matches!(readiness, Ok(Ok(())));
2565        let report = if arrived {
2566            Some(sweep(&f.home, &Fx::disk(), Fx::now()).await)
2567        } else {
2568            None
2569        };
2570        // Release and join our build before making assertions, including on failure.
2571        std::fs::write(release, "release").unwrap();
2572        let status = tokio::time::timeout(std::time::Duration::from_secs(60), child.wait()).await;
2573        if status.is_err() {
2574            child.kill().await.unwrap();
2575            child.wait().await.unwrap();
2576        }
2577        assert!(
2578            arrived,
2579            "fixture build never reached its active build script: {readiness:?}\n{}",
2580            std::fs::read_to_string(&log_path).unwrap()
2581        );
2582        assert!(
2583            status.unwrap().unwrap().success(),
2584            "fixture build failed: {}",
2585            std::fs::read_to_string(&log_path).unwrap()
2586        );
2587        let report = report.unwrap();
2588        assert_eq!(report.freed_bytes, 0, "{report:?}");
2589        assert!(
2590            report
2591                .retained
2592                .iter()
2593                .any(|s| s.contains("Cargo build is active")),
2594            "{report:?}"
2595        );
2596        assert!(wt.join("target/debug/output").is_file());
2597        assert!(crate::disk::dir_size(&wt.join("target/debug/incremental")) > 0);
2598    }
2599
2600    #[cfg(unix)]
2601    #[test]
2602    fn allocated_accounting_deduplicates_links_and_requires_the_last_link() {
2603        let temp = tempfile::tempdir().unwrap();
2604        let a = temp.path().join("a");
2605        let b = temp.path().join("b");
2606        let outside = temp.path().join("retained-input");
2607        std::fs::write(&a, vec![1; 65536]).unwrap();
2608        std::fs::hard_link(&a, &b).unwrap();
2609        std::fs::hard_link(&a, &outside).unwrap();
2610        let mut seen = Default::default();
2611        let blocks = allocated_inventory(&temp.path().canonicalize().unwrap(), &mut seen).unwrap();
2612        assert!(blocks > 0);
2613        let mut accounting = ReleasedAllocation::of(&[a.clone(), b.clone()]).unwrap();
2614        std::fs::remove_file(&a).unwrap();
2615        assert_eq!(accounting.unlinked(&a), Some(0));
2616        std::fs::remove_file(&b).unwrap();
2617        assert_eq!(accounting.unlinked(&b), Some(0));
2618        assert_eq!(std::fs::read(&outside).unwrap().len(), 65536);
2619        let mut accounting = ReleasedAllocation::of(std::slice::from_ref(&outside)).unwrap();
2620        std::fs::remove_file(&outside).unwrap();
2621        assert_eq!(accounting.unlinked(&outside), Some(blocks));
2622    }
2623
2624    #[cfg(unix)]
2625    #[test]
2626    fn relative_admission_home_matches_absolute_home_and_retains_symlink_checks() {
2627        let temp = tempfile::tempdir_in(".").unwrap();
2628        let home = PathBuf::from(temp.path().file_name().unwrap()).join("state");
2629        assert!(home.is_relative());
2630        let paths = vec![std::env::current_dir().unwrap()];
2631        let gib = 1024 * 1024 * 1024;
2632        let reservation = OutputLease::admit_with(&home, &paths, 8 * gib, |_| Ok(9 * gib)).unwrap();
2633        assert!(
2634            OutputLease::admit_with(&home.canonicalize().unwrap(), &paths, 8 * gib, |_| Ok(
2635                9 * gib
2636            ))
2637            .is_err()
2638        );
2639        drop(reservation);
2640        assert!(OutputLease::admit_with(&home, &paths, 8 * gib, |_| Ok(9 * gib)).is_ok());
2641        let bad = temp.path().join("symlink-state");
2642        std::fs::create_dir(&bad).unwrap();
2643        std::os::unix::fs::symlink(
2644            home.canonicalize().unwrap().join("disk-admission"),
2645            bad.join("disk-admission"),
2646        )
2647        .unwrap();
2648        assert!(OutputLease::admit_with(&bad, &paths, 8 * gib, |_| Ok(9 * gib)).is_err());
2649    }
2650
2651    #[cfg(unix)]
2652    #[test]
2653    fn concurrent_disk_admission_reserves_once_per_volume_until_release() {
2654        let temp = tempfile::tempdir().unwrap();
2655        let home = temp.path().canonicalize().unwrap();
2656        let paths = vec![home.clone(), home.join("not-created-yet")];
2657        let gib = 1024 * 1024 * 1024;
2658        let floor = 8 * gib;
2659        let first = OutputLease::admit_with(&home, &paths, floor, |_| Ok(floor + gib)).unwrap();
2660        assert_eq!(first.len(), 1);
2661        let blocked =
2662            OutputLease::admit_with(&home, &paths, floor, |_| Ok(floor + gib)).unwrap_err();
2663        assert!(blocked.to_string().contains("active writer reservations"));
2664        let second =
2665            OutputLease::admit_with(&home, &paths, floor, |_| Ok(floor + 2 * gib)).unwrap();
2666        drop(first);
2667        assert!(OutputLease::admit_with(&home, &paths, floor, |_| Ok(floor + gib)).is_err());
2668        drop(second);
2669        assert!(OutputLease::admit_with(&home, &paths, floor, |_| Ok(floor + gib)).is_ok());
2670        assert!(
2671            OutputLease::admit_with(&home, &paths, floor, |_| anyhow::bail!("unmeasured")).is_err()
2672        );
2673    }
2674
2675    #[cfg(unix)]
2676    #[test]
2677    fn simultaneous_writers_cannot_spend_the_same_volume_headroom() {
2678        let temp = tempfile::tempdir().unwrap();
2679        let home = temp.path().canonicalize().unwrap();
2680        let barrier = std::sync::Barrier::new(2);
2681        let gib = 1024 * 1024 * 1024;
2682        let results = std::thread::scope(|scope| {
2683            let threads: Vec<_> = (0..2)
2684                .map(|_| {
2685                    scope.spawn(|| {
2686                        barrier.wait();
2687                        OutputLease::admit_with(&home, std::slice::from_ref(&home), 100, |_| {
2688                            Ok(100 + gib)
2689                        })
2690                    })
2691                })
2692                .collect();
2693            threads
2694                .into_iter()
2695                .map(|thread| thread.join().unwrap())
2696                .collect::<Vec<_>>()
2697        });
2698        assert_eq!(results.iter().filter(|r| r.is_ok()).count(), 1);
2699        assert!(
2700            results
2701                .iter()
2702                .filter_map(|r| r.as_ref().err())
2703                .all(|e| e.to_string().contains("active writer reservations"))
2704        );
2705    }
2706
2707    #[tokio::test]
2708    async fn sweep_reclaims_real_cargo_outputs_and_preserves_source_and_unknown_files() {
2709        real_cargo_fixture(false).await;
2710    }
2711
2712    #[tokio::test]
2713    async fn missing_tag_from_precreated_target_reclaims_only_real_cargo_artifacts() {
2714        real_cargo_fixture(true).await;
2715    }
2716
2717    async fn real_cargo_fixture(precreate: bool) {
2718        let f = Fx::new().await;
2719        let state = f.run("ca00", true).await;
2720        let wt = &state.candidates[0].worktree;
2721        let source = "fn main() { println!(\"retained fixture\"); }\n";
2722        std::fs::create_dir(wt.join("src")).unwrap();
2723        std::fs::write(wt.join("src/main.rs"), source).unwrap();
2724        std::fs::write(
2725            wt.join("Cargo.toml"),
2726            "[package]\nname = \"retained-fixture\"\nversion = \"0.1.0\"\nedition = \"2021\"\n[workspace]\n",
2727        )
2728        .unwrap();
2729        if precreate {
2730            std::fs::create_dir_all(wt.join("target/magi-tools")).unwrap();
2731        }
2732        let mut cargo = std::process::Command::new("cargo");
2733        cargo.env_clear();
2734        cargo.env(
2735            "PATH",
2736            crate::build_target::command::standalone_fixture_path().unwrap(),
2737        );
2738        for key in ["HOME", "RUSTUP_HOME"] {
2739            if let Some(value) = std::env::var_os(key) {
2740                cargo.env(key, value);
2741            }
2742        }
2743        let build = cargo
2744            .current_dir(wt)
2745            .env("RUSTUP_TOOLCHAIN", "stable")
2746            .env("CARGO_HOME", f._temp.path().join("cargo-home"))
2747            .env("CARGO_INCREMENTAL", "0")
2748            .arg("--config")
2749            .arg(format!(
2750                "build.build-dir={}",
2751                serde_json::to_string(&wt.join("target")).unwrap()
2752            ))
2753            .args([
2754                "build",
2755                "--offline",
2756                "--release",
2757                "--jobs",
2758                "1",
2759                "--target-dir",
2760            ])
2761            .arg(wt.join("target"))
2762            .output()
2763            .unwrap();
2764        assert!(
2765            build.status.success(),
2766            "{}",
2767            String::from_utf8_lossy(&build.stderr)
2768        );
2769        assert_eq!(wt.join("target/CACHEDIR.TAG").exists(), !precreate);
2770        if precreate {
2771            // Cargo leaves pre-existing target roots untagged on subsequent builds too.
2772            let again = cargo.output().unwrap();
2773            assert!(
2774                again.status.success(),
2775                "{}",
2776                String::from_utf8_lossy(&again.stderr)
2777            );
2778            assert!(!wt.join("target/CACHEDIR.TAG").exists());
2779            assert!(cargo_target(wt).await.is_err());
2780        }
2781        git::git(wt, &["add", "Cargo.toml", "Cargo.lock", "src"])
2782            .await
2783            .unwrap();
2784        git::git(wt, &["commit", "-m", "real Cargo fixture"])
2785            .await
2786            .unwrap();
2787        let artifact = wt.join("target/release/retained-fixture");
2788        assert!(artifact.is_file());
2789        let unknown = wt.join("target/release/operator-note");
2790        std::fs::write(&unknown, "keep this note\n").unwrap();
2791
2792        let target = wt.join("target");
2793        let mut preserved = std::collections::BTreeMap::new();
2794        for relative in [
2795            "tmp/sentinel",
2796            "magi-tools/sentinel",
2797            "lifecycle-client-check.log",
2798        ] {
2799            let path = target.join(relative);
2800            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
2801            std::fs::write(&path, format!("retain {relative}\n")).unwrap();
2802            preserved.insert(path.clone(), std::fs::read(path).unwrap());
2803        }
2804        // Even genuine compiler bytes and valid dep-info outside supported
2805        // profiles cannot expand the deletion inventory.
2806        let decoy = target.join("magi-tools/retained-fixture");
2807        std::fs::copy(&artifact, &decoy).unwrap();
2808        let dep_info = decoy.with_extension("d");
2809        std::fs::write(&dep_info, format!("{}: src/main.rs\n", decoy.display())).unwrap();
2810        for path in [
2811            decoy,
2812            dep_info,
2813            wt.join("Cargo.toml"),
2814            wt.join("Cargo.lock"),
2815        ] {
2816            preserved.insert(path.clone(), std::fs::read(path).unwrap());
2817        }
2818        let supported_dirs = profile_output_dirs(&target.join("release"), true).unwrap();
2819        let proven = declared_outputs(&target).unwrap();
2820        assert!(
2821            proven
2822                .iter()
2823                .any(|path| path.parent() != Some(target.join("release").as_path())),
2824            "both uplifted and unit/deps binary references must be proven: {proven:?}"
2825        );
2826
2827        let out = sweep(&f.home, &Fx::disk(), Fx::now()).await;
2828        assert!(out.freed_bytes > 0, "{out:?}");
2829        for (path, bytes) in preserved {
2830            assert_eq!(std::fs::read(&path).unwrap(), bytes, "{}", path.display());
2831        }
2832        assert!(!artifact.exists(), "{out:?}");
2833        assert_eq!(
2834            std::fs::read_to_string(wt.join("src/main.rs")).unwrap(),
2835            source
2836        );
2837        assert_eq!(
2838            std::fs::read_to_string(&unknown).unwrap(),
2839            "keep this note\n"
2840        );
2841        assert_eq!(std::fs::read_to_string(wt.join("source")).unwrap(), "ca00");
2842        let ledger: serde_json::Value = serde_json::from_slice(
2843            &std::fs::read(
2844                f.home
2845                    .join("runs")
2846                    .join(&state.id)
2847                    .join("build-outputs.json"),
2848            )
2849            .unwrap(),
2850        )
2851        .unwrap();
2852        assert_eq!(
2853            ledger["identity"],
2854            if precreate {
2855                "rustc-profile-dep-info"
2856            } else {
2857                "cache-tag"
2858            }
2859        );
2860        if precreate {
2861            assert!(!target.join("CACHEDIR.TAG").exists());
2862            assert!(verify_residue(&f.home, &state.id, wt).await.is_err());
2863        }
2864        let deleted: Vec<PathBuf> = serde_json::from_value(ledger["files"].clone()).unwrap();
2865        assert!(deleted.contains(&artifact));
2866        assert_eq!(deleted, proven);
2867        for path in deleted {
2868            assert!(!path.exists());
2869            assert!(supported_dirs.contains(&path.parent().unwrap().to_owned()));
2870        }
2871        assert!(!residue_proof(&f.home, &state.id, wt).unwrap().exists());
2872    }
2873
2874    fn missing_tag_target(wt: &Path) {
2875        Fx::target(wt);
2876        let target = wt.join("target");
2877        std::fs::remove_file(target.join("CACHEDIR.TAG")).unwrap();
2878        for subdir in ["debug/deps", "debug/.fingerprint"] {
2879            std::fs::create_dir_all(target.join(subdir)).unwrap();
2880        }
2881        std::fs::write(target.join(".rustc_info.json"),
2882            r#"{"rustc_fingerprint":123,"outputs":{"1":{"success":true,"stdout":"rustc fixture","stderr":""}},"successes":{}}"#).unwrap();
2883    }
2884
2885    // Synthetic v2 coverage runs even on toolchains that still produce v1.
2886    fn v2_target(wt: &Path) -> PathBuf {
2887        missing_tag_target(wt);
2888        let target = wt.join("target");
2889        std::fs::remove_dir(target.join("debug/deps")).unwrap();
2890        std::fs::remove_dir(target.join("debug/.fingerprint")).unwrap();
2891        let unit = target.join("debug/build/fixture/0123456789abcdef");
2892        let out = unit.join("out");
2893        std::fs::create_dir_all(&out).unwrap();
2894        std::fs::create_dir(unit.join("fingerprint")).unwrap();
2895        for name in [
2896            "lib-fixture",
2897            "lib-fixture.json",
2898            "dep-lib-fixture",
2899            "invoked.timestamp",
2900        ] {
2901            std::fs::write(unit.join("fingerprint").join(name), "fixture").unwrap();
2902        }
2903        let artifact = out.join("libfixture-0123456789abcdef.rlib");
2904        std::fs::copy(target.join("debug/output"), &artifact).unwrap();
2905        std::fs::write(
2906            out.join("fixture-0123456789abcdef.d"),
2907            format!("{}: source\n", artifact.display()),
2908        )
2909        .unwrap();
2910        artifact
2911    }
2912
2913    #[tokio::test]
2914    async fn missing_tag_v2_proves_only_compiler_units_and_preserves_retained_links() {
2915        let f = Fx::new().await;
2916        let state = f.run("bc01", true).await;
2917        let wt = &state.candidates[0].worktree;
2918        let artifact = v2_target(wt);
2919        let unknown = artifact.parent().unwrap().join("operator-note");
2920        std::fs::write(&unknown, "retain").unwrap();
2921        // A valid binary and dep-info in arbitrary build-script output are not authority.
2922        let script_unit = wt.join("target/debug/build/fixture/fedcba9876543210");
2923        let script = script_unit.join("out");
2924        std::fs::create_dir_all(&script).unwrap();
2925        std::fs::create_dir(script_unit.join("fingerprint")).unwrap();
2926        std::fs::write(script_unit.join("fingerprint/invoked.timestamp"), "fixture").unwrap();
2927        let decoy = script.join("libfixture-fedcba9876543210.rlib");
2928        std::fs::copy(&artifact, &decoy).unwrap();
2929        std::fs::write(
2930            script.join("fixture-fedcba9876543210.d"),
2931            format!("{}: source\n", decoy.display()),
2932        )
2933        .unwrap();
2934        // Cargo uplifts the same inode from the unit into the profile root.
2935        let uplift = wt.join("target/debug/libfixture.rlib");
2936        std::fs::hard_link(&artifact, &uplift).unwrap();
2937        std::fs::write(
2938            wt.join("target/debug/fixture.d"),
2939            format!("{}: source\n", uplift.display()),
2940        )
2941        .unwrap();
2942        let bin_unit = wt.join("target/debug/build/fixture/1111111111111111");
2943        std::fs::create_dir_all(bin_unit.join("out")).unwrap();
2944        std::fs::create_dir(bin_unit.join("fingerprint")).unwrap();
2945        for name in [
2946            "bin-fixture",
2947            "bin-fixture.json",
2948            "dep-bin-fixture",
2949            "invoked.timestamp",
2950        ] {
2951            std::fs::write(bin_unit.join("fingerprint").join(name), "fixture").unwrap();
2952        }
2953        let bin = bin_unit.join("out/fixture");
2954        std::fs::hard_link(wt.join("target/debug/output"), &bin).unwrap();
2955        std::fs::write(
2956            bin.with_extension("d"),
2957            format!("{}: source\n", bin.display()),
2958        )
2959        .unwrap();
2960        let retained = wt.join("retained-link");
2961        std::fs::hard_link(&artifact, &retained).unwrap();
2962        use std::os::unix::fs::MetadataExt;
2963        let expected_allocation = std::fs::metadata(wt.join("target/debug/output"))
2964            .unwrap()
2965            .blocks()
2966            * 512;
2967        let sentinel = std::fs::read(&retained).unwrap();
2968        let first = sweep(&f.home, &Fx::disk(), Fx::now()).await;
2969        assert_eq!(first.freed_bytes, 800, "{first:?}");
2970        assert!(!bin.exists());
2971        assert_eq!(
2972            first.estimated_released_allocated_bytes,
2973            Some(expected_allocation)
2974        );
2975        assert_eq!(std::fs::read(&retained).unwrap(), sentinel);
2976        assert!(retained.is_file());
2977        assert!(unknown.is_file());
2978        assert!(decoy.is_file());
2979        assert!(!artifact.exists());
2980        // Unlinking proven names must never remove the retained reference's bytes.
2981        assert_eq!(std::fs::metadata(retained).unwrap().len(), 200);
2982    }
2983
2984    #[tokio::test]
2985    async fn missing_tag_v2_accepts_completed_build_script_run_units() {
2986        let f = Fx::new().await;
2987        let state = f.run("bc02", true).await;
2988        let wt = &state.candidates[0].worktree;
2989        let artifact = v2_target(wt);
2990        let run = wt.join("target/debug/build/fixture/2222222222222222");
2991        std::fs::create_dir_all(run.join("out")).unwrap();
2992        std::fs::create_dir(run.join("fingerprint")).unwrap();
2993        for name in [
2994            "run-build-script-build-script-build",
2995            "run-build-script-build-script-build.json",
2996        ] {
2997            std::fs::write(run.join("fingerprint").join(name), "fixture").unwrap();
2998        }
2999        // Build-script output carries no deletion authority.
3000        let decoy = run.join("out/libdecoy-2222222222222222.rlib");
3001        std::fs::copy(&artifact, &decoy).unwrap();
3002        std::fs::write(
3003            run.join("out/decoy-2222222222222222.d"),
3004            format!("{}: source\n", decoy.display()),
3005        )
3006        .unwrap();
3007        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3008        assert!(report.freed_bytes > 0, "{report:?}");
3009        assert!(!artifact.exists());
3010        assert!(decoy.is_file());
3011    }
3012
3013    #[tokio::test]
3014    async fn missing_tag_v2_rejects_incomplete_mixed_and_symlinked_layouts() {
3015        for invalid in [
3016            "build",
3017            "deps",
3018            ".fingerprint",
3019            "out",
3020            "fingerprint",
3021            "hash",
3022            "mixed",
3023            "unit-fingerprint",
3024            "compiler-fingerprint",
3025            "absent-out",
3026        ] {
3027            let f = Fx::new().await;
3028            let state = f.run("bc02", true).await;
3029            let wt = &state.candidates[0].worktree;
3030            let artifact = v2_target(wt);
3031            let unit = artifact.parent().unwrap().parent().unwrap();
3032            let profile = wt.join("target/debug");
3033            match invalid {
3034                "mixed" => {
3035                    std::fs::create_dir(profile.join("deps")).unwrap();
3036                    std::fs::create_dir(profile.join(".fingerprint")).unwrap();
3037                }
3038                "hash" => {
3039                    std::fs::rename(unit, unit.with_file_name("unknown")).unwrap();
3040                }
3041                "unit-fingerprint" => {
3042                    std::fs::remove_file(unit.join("fingerprint/invoked.timestamp")).unwrap();
3043                }
3044                "compiler-fingerprint" => {
3045                    std::fs::remove_file(unit.join("fingerprint/lib-fixture.json")).unwrap();
3046                }
3047                "absent-out" => {
3048                    std::fs::remove_dir_all(unit.join("out")).unwrap();
3049                }
3050                name => {
3051                    let path = if matches!(name, "out" | "fingerprint") {
3052                        unit.join(name)
3053                    } else {
3054                        profile.join(name)
3055                    };
3056                    if path.exists() {
3057                        std::fs::remove_dir_all(&path).unwrap();
3058                    }
3059                    std::os::unix::fs::symlink(&f.repo, &path).unwrap();
3060                }
3061            }
3062            let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3063            assert_eq!(report.freed_bytes, 0, "{invalid}: {report:?}");
3064            assert!(!report.retained.is_empty(), "{invalid}: {report:?}");
3065        }
3066    }
3067
3068    #[tokio::test]
3069    async fn bookkeeping_flushes_do_not_extend_artifact_grace_including_legacy_records() {
3070        for legacy in [false, true] {
3071            let f = Fx::new().await;
3072            let mut state = f.run("ac00", true).await;
3073            Fx::target(&state.candidates[0].worktree);
3074            let original = state.updated_at;
3075            let now = Fx::now() + jiff::SignedDuration::from_secs(7200);
3076            if legacy {
3077                let path = f.home.join("runs").join(&state.id).join("run.json");
3078                let mut value = serde_json::to_value(&state).unwrap();
3079                value.as_object_mut().unwrap().remove("output_activity_at");
3080                std::fs::write(path, serde_json::to_vec(&value).unwrap()).unwrap();
3081                state = RunState::load_under(&state.id, &f.home).unwrap();
3082                assert_eq!(state.output_activity(), original);
3083            }
3084            let age = state.output_activity();
3085            for offset in [30, 20, 10] {
3086                state
3087                    .save_under_at(&f.home, now - jiff::SignedDuration::from_secs(offset))
3088                    .unwrap();
3089                state = RunState::load_under(&state.id, &f.home).unwrap();
3090                assert_eq!(state.output_activity(), age);
3091            }
3092            let mut disk = Fx::disk();
3093            disk.fold_grace_secs = 3600;
3094            let report = sweep(&f.home, &disk, now).await;
3095            assert_eq!(report.freed_bytes, 200, "legacy={legacy}: {report:?}");
3096        }
3097    }
3098
3099    #[tokio::test]
3100    async fn recent_execution_outputs_and_released_output_leases_retain_artifacts() {
3101        for evidence in [
3102            "execution",
3103            "artifact",
3104            "dep-info",
3105            "fingerprint",
3106            "lease",
3107            "future",
3108        ] {
3109            let f = Fx::new().await;
3110            let mut state = f.run("ac01", true).await;
3111            let wt = state.candidates[0].worktree.clone();
3112            missing_tag_target(&wt);
3113            let now = Fx::now() + jiff::SignedDuration::from_secs(7200);
3114            let recent = now - jiff::SignedDuration::from_secs(10);
3115            match evidence {
3116                "execution" => {
3117                    state.note_output_activity(recent);
3118                    state.save_under_at(&f.home, recent).unwrap();
3119                }
3120                "lease" => {
3121                    let lease = OutputLease::borrow(&wt).unwrap().unwrap();
3122                    let file = lease._file.try_clone().unwrap();
3123                    drop(lease);
3124                    file.set_modified(std::time::SystemTime::from(recent))
3125                        .unwrap();
3126                }
3127                _ => {
3128                    let relative = match evidence {
3129                        "dep-info" => "debug/output.d",
3130                        "fingerprint" => "debug/.fingerprint/invoked.timestamp",
3131                        _ => "debug/output",
3132                    };
3133                    let path = wt.join("target").join(relative);
3134                    let file = OpenOptions::new()
3135                        .write(true)
3136                        .create(true)
3137                        .truncate(false)
3138                        .open(path)
3139                        .unwrap();
3140                    let at = if evidence == "future" {
3141                        now + jiff::SignedDuration::from_secs(10)
3142                    } else {
3143                        recent
3144                    };
3145                    file.set_modified(std::time::SystemTime::from(at)).unwrap();
3146                }
3147            }
3148            let mut disk = Fx::disk();
3149            disk.fold_grace_secs = 3600;
3150            let report = sweep(&f.home, &disk, now).await;
3151            assert_eq!(report.freed_bytes, 0, "{evidence}: {report:?}");
3152            assert!(
3153                report.retained.iter().any(|s| s.contains("grace period")
3154                    || s.contains("execution activity after inventory")),
3155                "{evidence}: {report:?}"
3156            );
3157            let aged = now + jiff::SignedDuration::from_secs(7200);
3158            let report = sweep(&f.home, &disk, aged).await;
3159            assert_eq!(report.freed_bytes, 200, "{evidence}: {report:?}");
3160        }
3161    }
3162
3163    #[tokio::test]
3164    async fn missing_tag_proof_preserves_locks_unknown_files_and_retirement_boundary() {
3165        let f = Fx::new().await;
3166        let state = f.run("ac02", true).await;
3167        let wt = &state.candidates[0].worktree;
3168        missing_tag_target(wt);
3169        let target = wt.join("target");
3170        let unknown = target.join("debug/notes");
3171        std::fs::write(&unknown, "operator data").unwrap();
3172        for lock in [".cargo-lock", ".cargo-build-lock", ".cargo-artifact-lock"] {
3173            let path = target.join("debug").join(lock);
3174            let file = OpenOptions::new()
3175                .create(true)
3176                .truncate(false)
3177                .read(true)
3178                .write(true)
3179                .open(&path)
3180                .unwrap();
3181            crate::pr_head::try_lock_file(&file).unwrap();
3182            let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3183            assert_eq!(report.freed_bytes, 0, "{lock}: {report:?}");
3184            assert!(
3185                report
3186                    .retained
3187                    .iter()
3188                    .any(|s| s.contains(path.to_str().unwrap())),
3189                "{report:?}"
3190            );
3191        }
3192        let owner = crate::pr_head::owner_lock(&f.home, &state.id).unwrap();
3193        assert_eq!(sweep(&f.home, &Fx::disk(), Fx::now()).await.freed_bytes, 0);
3194        drop(owner);
3195        let driver = crate::driver::maintenance(&f.home, &state.id).unwrap();
3196        assert_eq!(sweep(&f.home, &Fx::disk(), Fx::now()).await.freed_bytes, 0);
3197        drop(driver);
3198        let output = OutputLease::borrow(wt).unwrap().unwrap();
3199        assert_eq!(sweep(&f.home, &Fx::disk(), Fx::now()).await.freed_bytes, 0);
3200        drop(output);
3201        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3202        assert_eq!(report.freed_bytes, 200, "{report:?}");
3203        assert_eq!(std::fs::read_to_string(unknown).unwrap(), "operator data");
3204        assert!(!target.join("CACHEDIR.TAG").exists());
3205        assert!(!residue_proof(&f.home, &state.id, wt).unwrap().exists());
3206        assert!(verify_residue(&f.home, &state.id, wt).await.is_err());
3207        assert_eq!(sweep(&f.home, &Fx::disk(), Fx::now()).await.freed_bytes, 0);
3208    }
3209
3210    #[tokio::test]
3211    async fn missing_tag_partial_reclamation_can_resume_without_adopting_residue() {
3212        let f = Fx::new().await;
3213        let state = f.run("ac04", true).await;
3214        let wt = &state.candidates[0].worktree;
3215        missing_tag_target(wt);
3216        let target = wt.join("target");
3217        let first = target.join("debug/output");
3218        let second = target.join("debug/second");
3219        std::fs::copy(&first, &second).unwrap();
3220        std::fs::write(
3221            second.with_extension("d"),
3222            format!("{}: source\n", second.display()),
3223        )
3224        .unwrap();
3225        let outputs = declared_outputs(&target).unwrap();
3226        assert_eq!(outputs.len(), 2);
3227        // Simulate interruption after durable adoption and one successful unlink.
3228        std::fs::write(f.home.join("runs").join(&state.id).join("build-outputs.json"),
3229            serde_json::to_vec(&serde_json::json!({"target":target,"files":outputs,"action":"reclaim-declared-outputs"})).unwrap()).unwrap();
3230        std::fs::remove_file(first).unwrap();
3231        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3232        assert_eq!(report.freed_bytes, 200, "{report:?}");
3233        assert!(!second.exists());
3234        assert!(second.with_extension("d").exists());
3235        assert!(!residue_proof(&f.home, &state.id, wt).unwrap().exists());
3236    }
3237
3238    #[tokio::test]
3239    async fn missing_tag_proof_rejects_invalid_identity_structure_and_symlinks() {
3240        for invalid in [
3241            "identity",
3242            "deps",
3243            "fingerprint",
3244            "lock",
3245            "dep-info",
3246            "tag-symlink",
3247            "bad-tag",
3248        ] {
3249            let f = Fx::new().await;
3250            let state = f.run("ac03", true).await;
3251            let wt = &state.candidates[0].worktree;
3252            missing_tag_target(wt);
3253            let target = wt.join("target");
3254            match invalid {
3255                "identity" => std::fs::write(
3256                    target.join(".rustc_info.json"),
3257                    r#"{"rustc_fingerprint":123}"#,
3258                )
3259                .unwrap(),
3260                "deps" | "fingerprint" => {
3261                    let path = target.join("debug").join(if invalid == "deps" {
3262                        "deps"
3263                    } else {
3264                        ".fingerprint"
3265                    });
3266                    std::fs::remove_dir(&path).unwrap();
3267                    std::os::unix::fs::symlink(&f.repo, path).unwrap();
3268                }
3269                "lock" => {
3270                    std::fs::remove_file(target.join("debug/.cargo-lock")).unwrap();
3271                }
3272                "dep-info" => std::fs::write(target.join("debug/output.d"), "").unwrap(),
3273                "tag-symlink" => {
3274                    std::os::unix::fs::symlink(target.join("absent"), target.join("CACHEDIR.TAG"))
3275                        .unwrap()
3276                }
3277                "bad-tag" => std::fs::write(target.join("CACHEDIR.TAG"), "unknown").unwrap(),
3278                _ => unreachable!(),
3279            }
3280            let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3281            assert_eq!(report.freed_bytes, 0, "{invalid}: {report:?}");
3282            assert!(target.join("debug/output").is_file());
3283        }
3284    }
3285
3286    #[tokio::test]
3287    async fn sweep_budget_oversized_target_makes_progress_without_a_second_deletion() {
3288        let f = Fx::new().await;
3289        let first = f.run("ba00", true).await;
3290        let second = f.run("ba01", true).await;
3291        for state in [&first, &second] {
3292            Fx::target(&state.candidates[0].worktree);
3293        }
3294        let mut disk = Fx::disk();
3295        disk.worktree_reclaim_bytes = 100;
3296        let out = sweep(&f.home, &disk, Fx::now()).await;
3297        assert_eq!(out.freed_bytes, 200, "{out:?}");
3298        assert!(
3299            out.retained
3300                .contains(&format!("{}: per-pass byte budget spent", second.id)),
3301            "{out:?}"
3302        );
3303        assert!(
3304            !first.candidates[0]
3305                .worktree
3306                .join("target/debug/output")
3307                .exists()
3308        );
3309        assert!(
3310            second.candidates[0]
3311                .worktree
3312                .join("target/debug/output")
3313                .exists()
3314        );
3315        assert_eq!(sweep(&f.home, &disk, Fx::now()).await.freed_bytes, 200);
3316    }
3317
3318    #[tokio::test]
3319    async fn sweep_budget_pressure_prioritizes_largest_over_oldest() {
3320        let f = Fx::new().await;
3321        let small = f.run("bb00", true).await;
3322        let large = f.run("bb01", true).await;
3323        for state in [&small, &large] {
3324            Fx::target(&state.candidates[0].worktree);
3325        }
3326        let output = large.candidates[0].worktree.join("target/debug/output");
3327        std::fs::OpenOptions::new()
3328            .write(true)
3329            .open(&output)
3330            .unwrap()
3331            .set_len(1000)
3332            .unwrap();
3333        let mut disk = Fx::disk();
3334        disk.worktree_reclaim_bytes = 300;
3335        disk.min_free_bytes = u64::MAX;
3336        // Pressure must trigger reclamation even when aggregate usage is below cap.
3337        disk.worktree_limit_bytes = u64::MAX;
3338        let out = sweep(&f.home, &disk, Fx::now()).await;
3339        assert_eq!(out.freed_bytes, 1000, "{out:?}");
3340        assert!(!output.exists());
3341        assert!(
3342            small.candidates[0]
3343                .worktree
3344                .join("target/debug/output")
3345                .exists()
3346        );
3347    }
3348
3349    #[tokio::test]
3350    async fn sweep_budget_zero_remaining_and_inventory_are_distinct() {
3351        let f = Fx::new().await;
3352        let first = f.run("bc00", true).await;
3353        let second = f.run("bc01", true).await;
3354        for state in [&first, &second] {
3355            Fx::target(&state.candidates[0].worktree);
3356        }
3357        // Non-declared Cargo data must not determine whether outputs fit.
3358        std::fs::create_dir_all(
3359            first.candidates[0]
3360                .worktree
3361                .join("target/debug/incremental"),
3362        )
3363        .unwrap();
3364        std::fs::write(
3365            first.candidates[0]
3366                .worktree
3367                .join("target/debug/incremental/cache"),
3368            [0; 1000],
3369        )
3370        .unwrap();
3371        let mut disk = Fx::disk();
3372        disk.worktree_reclaim_bytes = 0;
3373        assert_eq!(sweep(&f.home, &disk, Fx::now()).await.freed_bytes, 0);
3374        disk.worktree_reclaim_bytes = 300;
3375        let out = sweep(&f.home, &disk, Fx::now()).await;
3376        assert_eq!(out.freed_bytes, 200, "{out:?}");
3377        assert!(
3378            !first.candidates[0]
3379                .worktree
3380                .join("target/debug/output")
3381                .exists()
3382        );
3383        assert!(
3384            second.candidates[0]
3385                .worktree
3386                .join("target/debug/output")
3387                .exists()
3388        );
3389        assert!(
3390            out.retained
3391                .iter()
3392                .any(|r| r.contains("declared outputs exceed"))
3393        );
3394    }
3395
3396    #[tokio::test]
3397    async fn sweep_budget_exception_preserves_grace_liveness_and_locks() {
3398        let f = Fx::new().await;
3399        let mut protected = f.run("bd00", true).await;
3400        let later = f.run("bd01", true).await;
3401        for state in [&protected, &later] {
3402            Fx::target(&state.candidates[0].worktree);
3403        }
3404        let mut disk = Fx::disk();
3405        disk.worktree_reclaim_bytes = 100;
3406        disk.fold_grace_secs = 3600;
3407        let out = sweep(&f.home, &disk, Fx::now()).await;
3408        assert_eq!(out.freed_bytes, 0);
3409        assert!(
3410            out.retained
3411                .iter()
3412                .any(|r| r.contains("within grace period"))
3413        );
3414        disk.fold_grace_secs = 0;
3415        protected.driver_exited = false;
3416        protected.driver_pid = Some(std::process::id());
3417        protected.driver_started_at = crate::proc::process_started_at(std::process::id());
3418        protected
3419            .save_under_at(&f.home, protected.updated_at)
3420            .unwrap();
3421        let out = sweep(&f.home, &disk, Fx::now()).await;
3422        assert!(
3423            out.retained
3424                .iter()
3425                .any(|r| r.contains("run live or ownership unknown"))
3426        );
3427        assert_eq!(
3428            out.freed_bytes, 200,
3429            "live target must not consume exception: {out:?}"
3430        );
3431        let output = protected.candidates[0].worktree.join("target/debug/output");
3432        assert!(output.exists());
3433        protected.driver_exited = true;
3434        protected.save_under(&f.home).unwrap();
3435        let owner = crate::pr_head::owner_lock(&f.home, &protected.id).unwrap();
3436        assert_eq!(sweep(&f.home, &disk, Fx::now()).await.freed_bytes, 0);
3437        assert!(output.exists());
3438        drop(owner);
3439        let cargo = cargo_locks(&protected.candidates[0].worktree.join("target")).unwrap();
3440        assert_eq!(sweep(&f.home, &disk, Fx::now()).await.freed_bytes, 0);
3441        assert!(output.exists());
3442        drop(cargo);
3443        let lease = OutputLease::take(&protected.candidates[0].worktree, true).unwrap();
3444        assert_eq!(sweep(&f.home, &disk, Fx::now()).await.freed_bytes, 0);
3445        assert!(output.exists());
3446        drop(lease);
3447        assert_eq!(sweep(&f.home, &disk, Fx::now()).await.freed_bytes, 200);
3448        assert!(!output.exists());
3449    }
3450
3451    #[tokio::test]
3452    async fn sweep_skips_missing_run_paths_and_reclaims_other_runs() {
3453        for remove_root in [true, false] {
3454            let f = Fx::new().await;
3455            let stale = Fx::new().await;
3456            let mut missing = stale.run("aa00", false).await;
3457            Fx::target(&missing.candidates[0].worktree);
3458            missing.save_under(&f.home).unwrap();
3459            if remove_root {
3460                std::fs::remove_dir_all(missing.worktree_root()).unwrap();
3461                // An existing non-repository proves the absent root is checked
3462                // before querying Git, even when no registrations are cached.
3463                std::fs::remove_dir_all(stale.repo.join(".git")).unwrap();
3464            } else {
3465                std::fs::remove_dir_all(&stale.repo).unwrap();
3466            }
3467            let healthy = f.run("aa01", false).await;
3468            Fx::target(&healthy.candidates[0].worktree);
3469
3470            let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3471            assert!(report.failures.is_empty(), "{report:?}");
3472            assert_eq!(report.freed_bytes, 200, "{report:?}");
3473            assert!(
3474                !healthy.candidates[0]
3475                    .worktree
3476                    .join("target/debug/output")
3477                    .exists()
3478            );
3479            if !remove_root {
3480                // Without the repository, ownership cannot be validated.
3481                assert!(
3482                    missing.candidates[0]
3483                        .worktree
3484                        .join("target/debug/output")
3485                        .exists()
3486                );
3487            }
3488        }
3489    }
3490
3491    #[tokio::test]
3492    async fn sweep_skips_missing_candidate_checkout_and_reclaims_later_runs() {
3493        let f = Fx::new().await;
3494        let mut missing = f.run("aa00", false).await;
3495        Fx::target(&missing.candidates[0].worktree);
3496        missing.repo = missing.candidates[0].worktree.clone();
3497        missing.save_under(&f.home).unwrap();
3498        std::fs::remove_dir_all(&missing.repo).unwrap();
3499        assert!(missing.worktree_root().is_dir());
3500        assert!(f.repo.is_dir());
3501
3502        let healthy = f.run("aa01", false).await;
3503        Fx::target(&healthy.candidates[0].worktree);
3504
3505        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3506        assert!(report.failures.is_empty(), "{report:?}");
3507        assert_eq!(report.freed_bytes, 200, "{report:?}");
3508        assert!(
3509            !healthy.candidates[0]
3510                .worktree
3511                .join("target/debug/output")
3512                .exists()
3513        );
3514    }
3515
3516    #[tokio::test]
3517    async fn sweep_reports_invalid_repository_and_still_reclaims_later_runs() {
3518        let f = Fx::new().await;
3519        let broken = Fx::new().await;
3520        let mut invalid = broken.run("aa00", false).await;
3521        Fx::target(&invalid.candidates[0].worktree);
3522        invalid.save_under(&f.home).unwrap();
3523        std::fs::remove_dir_all(broken.repo.join(".git")).unwrap();
3524        let healthy = f.run("aa01", false).await;
3525        Fx::target(&healthy.candidates[0].worktree);
3526
3527        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
3528        assert_eq!(report.failures.len(), 1, "{report:?}");
3529        assert!(report.failures[0].starts_with(&format!("{}:", invalid.id)));
3530        assert_eq!(report.freed_bytes, 200, "{report:?}");
3531        assert!(
3532            !healthy.candidates[0]
3533                .worktree
3534                .join("target/debug/output")
3535                .exists()
3536        );
3537        assert!(
3538            invalid.candidates[0]
3539                .worktree
3540                .join("target/debug/output")
3541                .exists()
3542        );
3543    }
3544
3545    async fn reconciliation_plan(f: &Fx, task: &Task, source: &RunState) -> serde_json::Value {
3546        let path = reconcile::run(&f.home, &task.id, &source.id, None)
3547            .await
3548            .unwrap();
3549        serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap()
3550    }
3551
3552    #[tokio::test]
3553    async fn reconciliation_standing_chat_applies_the_owners_request_without_a_phrase() {
3554        use crate::{
3555            chat_control::{Operation, dispatch},
3556            talk::{self, Talks},
3557        };
3558        let f = Fx::new().await;
3559        let source = f.run("ae00", true).await;
3560        let clean = f.run("ae01", false).await;
3561        let queue = crate::queue::Queue::at(f.home.join("queue"));
3562        let talks = Talks::at(f.home.join("talks"));
3563        let open_talk = |id: &str| -> talk::Talk {
3564            let mut conversation = serde_json::from_value(serde_json::json!({
3565                "schema":1,"id":id,"repo":f.repo,"agent":"worker","status":"open","turns":[],
3566                "created_at":Timestamp::now(),"updated_at":Timestamp::now(),
3567                "seat":crate::agent::SeatState::new("talk","worker",1)
3568            }))
3569            .unwrap();
3570            talks.put(&mut conversation).unwrap();
3571            conversation
3572        };
3573        let conversation = open_talk("talk-owner");
3574        let other = open_talk("talk-other");
3575        // A task this chat did not file: the owner's request covers it too.
3576        let task = f.task(&[source.clone(), clean.clone()]);
3577        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
3578        let operation = |apply: Option<&str>| Operation::ReconcileRetention {
3579            task: task.id.clone(),
3580            authority: source.id.clone(),
3581            apply: apply.map(str::to_owned),
3582        };
3583        // A plan saved outside the chat is not applied through it.
3584        let legacy_path = reconcile::run(&f.home, &task.id, &source.id, None)
3585            .await
3586            .unwrap();
3587        let legacy: serde_json::Value =
3588            serde_json::from_slice(&std::fs::read(legacy_path).unwrap()).unwrap();
3589        let legacy_id = legacy["id"].as_str().unwrap();
3590        assert!(
3591            dispatch(
3592                &queue,
3593                &f.home,
3594                &talks,
3595                &conversation.id,
3596                operation(Some(legacy_id))
3597            )
3598            .await
3599            .is_err()
3600        );
3601        let summary = dispatch(&queue, &f.home, &talks, &conversation.id, operation(None))
3602            .await
3603            .unwrap();
3604        assert!(summary.contains("Complete review summary"));
3605        assert!(summary.contains(&source.id) && summary.contains(&clean.id));
3606        let root = f.home.join("retention-reconciliations");
3607        let displayed: serde_json::Value =
3608            serde_json::from_str(summary.split_once('\n').unwrap().1).unwrap();
3609        let id = displayed["plan"].as_str().unwrap();
3610        let dir = root.join(id);
3611        let plan: serde_json::Value =
3612            serde_json::from_slice(&std::fs::read(dir.join("plan.json")).unwrap()).unwrap();
3613        assert_eq!(plan["version"], 2);
3614        assert!(clean.candidates[0].worktree.exists());
3615        assert!(!dir.join("approved.json").exists());
3616        // The plan stays bound to the conversation it was saved for.
3617        assert!(
3618            dispatch(&queue, &f.home, &talks, &other.id, operation(Some(id)))
3619                .await
3620                .is_err()
3621        );
3622        assert!(!dir.join("approved.json").exists());
3623        // No owner phrase or quote: the request itself applies the plan. An
3624        // interruption after the durable fence keeps the audit for a retry.
3625        let error = reconcile::interrupt_chat_after_fence(
3626            &f.home,
3627            &task,
3628            id,
3629            reconcile::ChatRequest {
3630                talks: &talks,
3631                conversation: &conversation.id,
3632            },
3633        )
3634        .await
3635        .unwrap_err();
3636        assert!(
3637            error.to_string().contains("simulated interruption"),
3638            "{error:#}"
3639        );
3640        assert!(clean.candidates[0].worktree.exists());
3641        assert!(dir.join("approved.json").exists());
3642        dispatch(
3643            &queue,
3644            &f.home,
3645            &talks,
3646            &conversation.id,
3647            operation(Some(id)),
3648        )
3649        .await
3650        .unwrap();
3651        assert!(!clean.candidates[0].worktree.exists());
3652        assert!(source.candidates[0].worktree.exists());
3653        let audit: serde_json::Value =
3654            serde_json::from_slice(&std::fs::read(dir.join("approved.json")).unwrap()).unwrap();
3655        assert_eq!(audit["version"], 2);
3656        assert_eq!(
3657            audit["owner"],
3658            serde_json::json!({"conversation": conversation.id})
3659        );
3660        dispatch(
3661            &queue,
3662            &f.home,
3663            &talks,
3664            &conversation.id,
3665            operation(Some(id)),
3666        )
3667        .await
3668        .unwrap();
3669    }
3670
3671    #[tokio::test]
3672    async fn reconciliation_proves_unmeasured_startup_candidates_empty() {
3673        let f = Fx::new().await;
3674        let source = f.run("af00", true).await;
3675        let mut clean = f.run("af01", false).await;
3676        let mut dirty = f.run("af02", false).await;
3677        for state in [&mut clean, &mut dirty] {
3678            state.status = RunStatus::Prep;
3679            state.prep_complete = Some(false);
3680            state.candidates[0].empty = false;
3681            state.save_under(&f.home).unwrap();
3682        }
3683        std::fs::write(dirty.candidates[0].worktree.join("source"), "unsaved work").unwrap();
3684        let task = f.task(&[source.clone(), clean.clone(), dirty.clone()]);
3685        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
3686        let plan = reconciliation_plan(&f, &task, &source).await;
3687        let selected: Vec<_> = plan["candidates"]
3688            .as_array()
3689            .unwrap()
3690            .iter()
3691            .filter(|e| e["selected"] == true)
3692            .collect();
3693        assert_eq!(selected.len(), 1, "{plan:#}");
3694        assert_eq!(selected[0]["run"], clean.id);
3695        reconcile::run(&f.home, &task.id, &source.id, plan["id"].as_str())
3696            .await
3697            .unwrap();
3698        assert!(!clean.candidates[0].worktree.exists());
3699        assert!(dirty.candidates[0].worktree.exists());
3700        assert!(
3701            RunState::load_under(&dirty.id, &f.home)
3702                .unwrap()
3703                .retired_to
3704                .is_none()
3705        );
3706    }
3707
3708    #[tokio::test]
3709    async fn reconciliation_blanket_retention_selected_source_and_repeated_batches() {
3710        let f = Fx::new().await;
3711        let mut source = f.run("aa00", true).await;
3712        let alternative = f.run("aa01", true).await;
3713        let head = git::rev_parse(&f.repo, &source.candidates[0].branch)
3714            .await
3715            .unwrap();
3716        source.reviewed_commits = Some(vec![head.clone()]);
3717        source.save_under(&f.home).unwrap();
3718        let mut runs = vec![source.clone(), alternative.clone()];
3719        for i in 2..14 {
3720            runs.push(f.run(&format!("aa{i:02}"), false).await);
3721        }
3722        let mut task = f.task(&runs);
3723        task.pr_recovery = Some(crate::pr_recovery::Selection {
3724            url: "https://example.invalid/pull/1".into(),
3725            branch: "selected".into(),
3726            head: head.clone(),
3727            source_head: Some(head.clone()),
3728            source_run: Some(source.id.clone()),
3729            source_proof: None,
3730            disposition: Some(crate::pr_recovery::Disposition::Land),
3731            instruction: task.instruction.clone(),
3732            approved_at: Timestamp::now(),
3733        });
3734        source.pr_recovery = task.pr_recovery.clone();
3735        source.save_under(&f.home).unwrap();
3736        runs[0] = source.clone();
3737        crate::queue::Queue::at(f.home.join("queue"))
3738            .put(&mut task)
3739            .unwrap();
3740        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
3741        let task_before = serde_json::to_value(&task).unwrap();
3742        let mut report = Report::default();
3743        retire_empty(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
3744        assert_eq!(
3745            report.retired, 0,
3746            "automatic cleanup must still refuse blanket markers"
3747        );
3748        assert!(
3749            report
3750                .retained
3751                .iter()
3752                .any(|reason| reason.contains("explicitly retained"))
3753        );
3754        let mut total = 0;
3755        for expected in [5, 5, 2, 0] {
3756            let plan = reconciliation_plan(&f, &task, &source).await;
3757            let selected: Vec<_> = plan["candidates"]
3758                .as_array()
3759                .unwrap()
3760                .iter()
3761                .filter(|e| e["selected"] == true)
3762                .collect();
3763            assert_eq!(selected.len(), expected, "{plan:#}");
3764            for e in &selected {
3765                assert!(
3766                    RunState::load_under(e["run"].as_str().unwrap(), &f.home)
3767                        .unwrap()
3768                        .retired_to
3769                        .is_none()
3770                );
3771            }
3772            let operation = plan["id"].as_str().unwrap();
3773            for _ in 0..2 {
3774                reconcile::run(&f.home, &task.id, &source.id, Some(operation))
3775                    .await
3776                    .unwrap();
3777            }
3778            total += expected;
3779        }
3780        assert_eq!(total, 12);
3781        for run in &runs {
3782            let dir = f.home.join("runs").join(&run.id);
3783            assert!(dir.join("run.json").exists());
3784            assert!(dir.join("retain-recovery-sources").exists());
3785            assert!(
3786                git::branch_exists(&f.repo, &run.candidates[0].branch)
3787                    .await
3788                    .unwrap()
3789            );
3790        }
3791        assert!(source.candidates[0].worktree.exists());
3792        assert!(alternative.candidates[0].worktree.exists());
3793        assert_eq!(
3794            git::rev_parse(&f.repo, &source.candidates[0].branch)
3795                .await
3796                .unwrap(),
3797            head
3798        );
3799        assert_eq!(
3800            serde_json::to_value(
3801                crate::queue::Queue::at(f.home.join("queue"))
3802                    .get(&task.id)
3803                    .unwrap()
3804            )
3805            .unwrap(),
3806            task_before
3807        );
3808        assert_eq!(
3809            runs.iter()
3810                .filter(|r| RunState::load_under(&r.id, &f.home)
3811                    .unwrap()
3812                    .retired_to
3813                    .is_none())
3814                .count(),
3815            2
3816        );
3817    }
3818
3819    #[tokio::test]
3820    async fn reconciliation_refuses_live_dirty_ignored_unknown_and_git_checkpoints() {
3821        let f = Fx::new().await;
3822        let source = f.run("ab00", true).await;
3823        let mut runs = vec![source.clone()];
3824        for i in 1..9 {
3825            runs.push(f.run(&format!("ab{i:02}"), false).await);
3826        }
3827        std::fs::write(runs[1].candidates[0].worktree.join("source"), "dirty").unwrap();
3828        std::fs::write(runs[2].candidates[0].worktree.join("untracked"), "source").unwrap();
3829        std::fs::create_dir(runs[3].candidates[0].worktree.join("scratch")).unwrap();
3830        std::fs::write(
3831            runs[3].candidates[0].worktree.join("scratch/source"),
3832            "ignored source",
3833        )
3834        .unwrap();
3835        std::fs::write(
3836            runs[4].worktree_root().join("unknown"),
3837            "unregistered source",
3838        )
3839        .unwrap();
3840        let checkpoint = git::git(
3841            &runs[5].candidates[0].worktree,
3842            &[
3843                "rev-parse",
3844                "--path-format=absolute",
3845                "--git-path",
3846                "sequencer",
3847            ],
3848        )
3849        .await
3850        .unwrap();
3851        std::fs::create_dir(checkpoint.trim()).unwrap();
3852        runs[6].driver_exited = false;
3853        runs[6].driver_pid = Some(std::process::id());
3854        runs[6].driver_started_at = crate::proc::process_started_at(std::process::id());
3855        runs[6].save_under(&f.home).unwrap();
3856        runs[7].driver_exited = false;
3857        runs[7].driver_pid = None;
3858        runs[7].driver_claimed = None; // Legacy record without first-claim evidence.
3859        runs[7].save_under(&f.home).unwrap();
3860        assert_eq!(runs[7].liveness_under(&f.home, false), Liveness::Unknown);
3861
3862        let hidden = &runs[8].candidates[0].worktree;
3863        git::git(hidden, &["update-index", "--assume-unchanged", "source"])
3864            .await
3865            .unwrap();
3866        std::fs::write(hidden.join("source"), "hidden dirty implementation").unwrap();
3867        assert!(
3868            git::git(hidden, &["status", "--porcelain"])
3869                .await
3870                .unwrap()
3871                .is_empty()
3872        );
3873        let task = f.task(&runs);
3874        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
3875        let plan = reconciliation_plan(&f, &task, &source).await;
3876        assert!(
3877            plan["candidates"]
3878                .as_array()
3879                .unwrap()
3880                .iter()
3881                .all(|e| e["selected"] == false),
3882            "{plan:#}"
3883        );
3884        for run in &runs {
3885            assert!(run.candidates[0].worktree.exists());
3886        }
3887    }
3888
3889    #[tokio::test]
3890    async fn reconciliation_stale_plans_and_changed_markers_fail_closed() {
3891        let f = Fx::new().await;
3892        let source = f.run("ac00", true).await;
3893        let empty = f.run("ac01", false).await;
3894        let task = f.task(&[source.clone(), empty.clone()]);
3895        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
3896        let plan = reconciliation_plan(&f, &task, &source).await;
3897        std::fs::write(
3898            f.home.join("runs").join(&source.id).join("new-report"),
3899            "new review checkpoint",
3900        )
3901        .unwrap();
3902        assert!(
3903            reconcile::run(&f.home, &task.id, &source.id, plan["id"].as_str())
3904                .await
3905                .is_err()
3906        );
3907        assert!(empty.candidates[0].worktree.exists());
3908        assert!(
3909            RunState::load_under(&empty.id, &f.home)
3910                .unwrap()
3911                .retired_to
3912                .is_none()
3913        );
3914        let marker = f
3915            .home
3916            .join("runs")
3917            .join(&empty.id)
3918            .join("retain-recovery-sources");
3919        std::fs::write(&marker, "different owner").unwrap();
3920        let plan = reconciliation_plan(&f, &task, &source).await;
3921        assert!(
3922            plan["candidates"]
3923                .as_array()
3924                .unwrap()
3925                .iter()
3926                .all(|e| e["selected"] == false)
3927        );
3928    }
3929
3930    #[tokio::test]
3931    async fn reconciliation_recovers_interrupted_state_staging_in_a_batch() {
3932        for partial in [false, true] {
3933            let f = Fx::new().await;
3934            let source = f.run("ab10", true).await;
3935            let first = f.run("ab11", false).await;
3936            let second = f.run("ab12", false).await;
3937            let task = f.task(&[source.clone(), first.clone(), second.clone()]);
3938            crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
3939            let plan = reconciliation_plan(&f, &task, &source).await;
3940            let id = plan["id"].as_str().unwrap();
3941            let staging = f.home.join("runs").join(&second.id).join("run.json.tmp");
3942            // Matching bytes without this operation's fence are not trusted.
3943            std::fs::write(&staging, b"").unwrap();
3944            assert!(
3945                reconcile::run(&f.home, &task.id, &source.id, Some(id))
3946                    .await
3947                    .is_err()
3948            );
3949            assert!(staging.exists());
3950            std::fs::remove_file(&staging).unwrap();
3951            let error = reconcile::interrupt_during_state_save(&f.home, &task, id, partial)
3952                .await
3953                .unwrap_err();
3954            assert!(
3955                error.to_string().contains("before run state rename"),
3956                "{error:#}"
3957            );
3958            assert!(
3959                RunState::load_under(&first.id, &f.home)
3960                    .unwrap()
3961                    .retired_to
3962                    .is_some()
3963            );
3964            assert!(
3965                RunState::load_under(&second.id, &f.home)
3966                    .unwrap()
3967                    .retired_to
3968                    .is_none()
3969            );
3970            assert!(first.candidates[0].worktree.exists());
3971            assert!(second.candidates[0].worktree.exists());
3972            let interrupted = std::fs::read(&staging).unwrap();
3973            // An unrelated file must survive a refused retry unchanged.
3974            std::fs::write(&staging, b"unrelated recovery source").unwrap();
3975            assert!(
3976                reconcile::run(&f.home, &task.id, &source.id, Some(id))
3977                    .await
3978                    .is_err()
3979            );
3980            assert_eq!(
3981                std::fs::read(&staging).unwrap(),
3982                b"unrelated recovery source"
3983            );
3984            std::fs::write(&staging, interrupted).unwrap();
3985            reconcile::run(&f.home, &task.id, &source.id, Some(id))
3986                .await
3987                .unwrap();
3988            assert!(!staging.exists());
3989            assert!(!first.candidates[0].worktree.exists());
3990            assert!(!second.candidates[0].worktree.exists());
3991            assert_eq!(
3992                RunState::load_under(&second.id, &f.home)
3993                    .unwrap()
3994                    .retired_to,
3995                Some(source.id)
3996            );
3997        }
3998    }
3999
4000    #[tokio::test]
4001    async fn reconciliation_resumes_only_its_exact_durable_fence() {
4002        let f = Fx::new().await;
4003        let source = f.run("ad00", true).await;
4004        let empty = f.run("ad01", false).await;
4005        let task = f.task(&[source.clone(), empty.clone()]);
4006        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
4007        let plan = reconciliation_plan(&f, &task, &source).await;
4008        let id = plan["id"].as_str().unwrap();
4009        assert!(
4010            reconcile::interrupt_after_fence(&f.home, &task, id)
4011                .await
4012                .is_err()
4013        );
4014        assert!(empty.candidates[0].worktree.exists());
4015        assert_eq!(
4016            RunState::load_under(&empty.id, &f.home)
4017                .unwrap()
4018                .retired_to
4019                .as_ref(),
4020            Some(&source.id)
4021        );
4022        reconcile::run(&f.home, &task.id, &source.id, Some(id))
4023            .await
4024            .unwrap();
4025        assert!(!empty.candidates[0].worktree.exists());
4026        // Model an interrupted removal: restore precisely the approved base worktree.
4027        git::worktree_add_branch(
4028            &f.repo,
4029            &empty.candidates[0].worktree,
4030            "restore-empty",
4031            &f.base,
4032        )
4033        .await
4034        .unwrap();
4035        // Recreated Git administration differs from approval and is refused.
4036        assert!(
4037            reconcile::run(&f.home, &task.id, &source.id, Some(id))
4038                .await
4039                .is_err()
4040        );
4041        assert!(empty.candidates[0].worktree.exists());
4042        let mut changed = RunState::load_under(&empty.id, &f.home).unwrap();
4043        changed.prep_failure = Some("new owner edit".into());
4044        changed.save_under(&f.home).unwrap();
4045        assert!(
4046            reconcile::run(&f.home, &task.id, &source.id, Some(id))
4047                .await
4048                .is_err()
4049        );
4050    }
4051
4052    #[tokio::test]
4053    async fn reconciliation_checks_prep_intent_reflogs_and_unknown_run_fields() {
4054        let f = Fx::new().await;
4055        let source = f.run("ae00", true).await;
4056        let mut prep = f.run("ae01", false).await;
4057        let candidate = prep.candidates.pop().unwrap();
4058        prep.event("prep-intent", serde_json::json!({"branch":candidate.branch,"worktree":candidate.worktree,"start":f.base,"picks":[]}).to_string());
4059        prep.save_under(&f.home).unwrap();
4060        let reset = f.run("ae02", false).await;
4061        let wt = &reset.candidates[0].worktree;
4062        std::fs::write(wt.join("source"), "unique hidden in reflog").unwrap();
4063        git::git(wt, &["add", "."]).await.unwrap();
4064        git::git(wt, &["commit", "-m", "unique implementation"])
4065            .await
4066            .unwrap();
4067        git::git(wt, &["reset", "--hard", &f.base]).await.unwrap();
4068        let unknown = f.run("ae03", false).await;
4069        let unknown_path = f.home.join("runs").join(&unknown.id).join("run.json");
4070        let mut raw: serde_json::Value =
4071            serde_json::from_slice(&std::fs::read(&unknown_path).unwrap()).unwrap();
4072        raw["unknown_checkpoint"] = serde_json::json!({"source":"retain"});
4073        std::fs::write(unknown_path, serde_json::to_vec_pretty(&raw).unwrap()).unwrap();
4074        let task = f.task(&[source.clone(), prep.clone(), reset.clone(), unknown.clone()]);
4075        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
4076        let plan = reconciliation_plan(&f, &task, &source).await;
4077        let selected: Vec<_> = plan["candidates"]
4078            .as_array()
4079            .unwrap()
4080            .iter()
4081            .filter(|e| e["selected"] == true)
4082            .collect();
4083        assert_eq!(selected.len(), 1, "{plan:#}");
4084        assert_eq!(selected[0]["run"], prep.id);
4085        reconcile::run(&f.home, &task.id, &source.id, plan["id"].as_str())
4086            .await
4087            .unwrap();
4088        assert!(!candidate.worktree.exists());
4089        assert!(reset.candidates[0].worktree.exists());
4090        assert!(unknown.candidates[0].worktree.exists());
4091    }
4092
4093    #[tokio::test]
4094    async fn cap_reclaims_blocked_candidates_and_landing_reviewers_across_repositories() {
4095        let f = Fx::new().await;
4096        let mut state = f.run("aaaa", true).await;
4097        let candidate = state.candidates[0].worktree.clone();
4098        std::fs::write(candidate.join("source"), "uncommitted work").unwrap();
4099        Fx::target(&candidate);
4100        for seat in ["review-1", "review-2"] {
4101            let wt = state.worktree_root().join(seat);
4102            git::worktree_add_detached(&f.repo, &wt, &f.base)
4103                .await
4104                .unwrap();
4105            Fx::target(&wt);
4106        }
4107        state.status = RunStatus::Landing;
4108        state.save_under(&f.home).unwrap();
4109        let second = Fx::new().await;
4110        let other = second.run("bbbb", true).await;
4111        // A global store may contain repositories with different worktree roots.
4112        let mut imported = other.clone();
4113        imported.save_under(&f.home).unwrap();
4114        Fx::target(&other.candidates[0].worktree);
4115        let guard = OutputLease::borrow(&candidate).unwrap().unwrap();
4116        let out = sweep(&f.home, &Fx::disk(), Fx::now()).await;
4117        assert!(
4118            candidate.join("target/debug/output").exists(),
4119            "active writer protected"
4120        );
4121        assert!(
4122            !state
4123                .worktree_root()
4124                .join("review-1/target/debug/output")
4125                .exists()
4126        );
4127        assert!(
4128            !state
4129                .worktree_root()
4130                .join("review-2/target/debug/output")
4131                .exists()
4132        );
4133        assert!(
4134            !other.candidates[0]
4135                .worktree
4136                .join("target/debug/output")
4137                .exists()
4138        );
4139        assert!(out.freed_bytes >= 600);
4140        drop(guard);
4141        let out = sweep(&f.home, &Fx::disk(), Fx::now()).await;
4142        assert!(out.freed_bytes >= 200);
4143        assert_eq!(
4144            std::fs::read_to_string(candidate.join("source")).unwrap(),
4145            "uncommitted work"
4146        );
4147        assert!(
4148            git::branch_exists(&f.repo, &state.candidates[0].branch)
4149                .await
4150                .unwrap()
4151        );
4152    }
4153
4154    #[tokio::test]
4155    async fn dep_info_reclamation_preserves_unmanifested_nested_files() {
4156        let f = Fx::new().await;
4157        let state = f.run("cc01", false).await;
4158        let wt = &state.candidates[0].worktree;
4159        Fx::target(wt);
4160        for dir in ["debug", "release", "debug/deps", "debug/build/script/out"] {
4161            let dir = wt.join("target").join(dir);
4162            std::fs::create_dir_all(&dir).unwrap();
4163            std::fs::write(dir.join("notes.txt"), "user evidence").unwrap();
4164        }
4165        // A dep-info dependency is an input, never deletion authority.
4166        let manifest = wt.join("target/debug/output.d");
4167        let output = wt.join("target/debug/output");
4168        std::fs::write(
4169            &manifest,
4170            format!(
4171                "{}: {}\n",
4172                output.display(),
4173                wt.join("target/debug/notes.txt").display()
4174            ),
4175        )
4176        .unwrap();
4177        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
4178        assert_eq!(report.freed_bytes, 200);
4179        assert!(!output.exists());
4180        for dir in ["debug", "release", "debug/deps", "debug/build/script/out"] {
4181            assert_eq!(
4182                std::fs::read_to_string(wt.join("target").join(dir).join("notes.txt")).unwrap(),
4183                "user evidence"
4184            );
4185        }
4186        // Metadata remains available for inspection and future safe rebuilds.
4187        assert!(manifest.exists());
4188        assert!(report.retained.iter().any(|s| s.contains("unmanifested")));
4189        std::fs::write(&output, "user note replacing an old binary").unwrap();
4190        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
4191        assert_eq!(report.freed_bytes, 0);
4192        assert_eq!(
4193            std::fs::read_to_string(&output).unwrap(),
4194            "user note replacing an old binary"
4195        );
4196    }
4197
4198    #[tokio::test]
4199    async fn measurement_cache_expires_and_disabled_eviction_does_not_walk_outputs() {
4200        let f = Fx::new().await;
4201        let state = f.run("cc02", false).await;
4202        let wt = &state.candidates[0].worktree;
4203        Fx::target(wt);
4204        let mut disk = Fx::disk();
4205        disk.worktree_limit_bytes = u64::MAX;
4206        let now = Fx::now();
4207        let first = sweep(&f.home, &disk, now).await;
4208        std::fs::write(wt.join("target/debug/output"), [1; 1000]).unwrap();
4209        assert_eq!(
4210            sweep(&f.home, &disk, now).await.measured_bytes,
4211            first.measured_bytes
4212        );
4213        assert!(
4214            sweep(&f.home, &disk, now + jiff::SignedDuration::from_secs(301))
4215                .await
4216                .measured_bytes
4217                > first.measured_bytes
4218        );
4219        disk.worktree_limit_bytes = 0;
4220        assert_eq!(sweep(&f.home, &disk, now).await.measured_bytes, 0);
4221    }
4222
4223    #[tokio::test]
4224    async fn unfinished_source_fold_reports_its_retention_reason() {
4225        let f = Fx::new().await;
4226        let mut source = f.run("cc03", true).await;
4227        let task = f.task(&[source.clone()]);
4228        let error = crate::graph::fold_run(&mut source, true, &f.home)
4229            .await
4230            .unwrap_err();
4231        assert!(error.to_string().contains(&task.id));
4232        assert!(source.candidates[0].worktree.exists());
4233        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
4234        let error = crate::graph::fold_run(&mut source, true, &f.home)
4235            .await
4236            .unwrap_err();
4237        assert!(error.to_string().contains("explicitly retained"));
4238    }
4239
4240    #[tokio::test]
4241    async fn confirmed_merged_source_folds_while_task_is_held_but_retention_still_wins() {
4242        let f = Fx::new().await;
4243        let mut source = f.run("cc04", true).await;
4244        let mut task = f.task(&[source.clone()]);
4245        task.hold_manual(Some("queue settlement pending".into()));
4246        let queue = crate::queue::Queue::at(f.home.join("queue"));
4247        queue.put(&mut task).unwrap();
4248        source.merge = Some(crate::run::MergeOutcome {
4249            mode: crate::config::MergeMode::Pr,
4250            ok: true,
4251            detail: "confirmed merged".into(),
4252            empty: false,
4253        });
4254        // Publication success and terminal labels alone are insufficient.
4255        for status in [RunStatus::Ready, RunStatus::Failed, RunStatus::Superseded] {
4256            source.status = status;
4257            assert!(
4258                crate::graph::fold_run(&mut source, true, &f.home)
4259                    .await
4260                    .is_err()
4261            );
4262            assert!(source.candidates[0].worktree.exists());
4263        }
4264        git::git(
4265            &f.repo,
4266            &["merge", "--ff-only", &source.candidates[0].branch],
4267        )
4268        .await
4269        .unwrap();
4270        git::git(&f.repo, &["update-ref", "refs/remotes/origin/main", "HEAD"])
4271            .await
4272            .unwrap();
4273        source.status = RunStatus::Merged;
4274        source.save_under(&f.home).unwrap();
4275        assert!(crate::pr_recovery::source_retention_reason(&f.home, &source).is_none());
4276        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
4277        assert!(
4278            crate::graph::fold_run(&mut source, true, &f.home)
4279                .await
4280                .unwrap_err()
4281                .to_string()
4282                .contains("explicitly retained")
4283        );
4284        // The fixture's explicit marker is removed only to exercise ordinary
4285        // confirmed-merge cleanup; no production cleanup removes such markers.
4286        std::fs::remove_file(
4287            f.home
4288                .join("runs")
4289                .join(&source.id)
4290                .join("retain-recovery-sources"),
4291        )
4292        .unwrap();
4293        let (folded, unreadable) = crate::clean::fold_due(
4294            &f.home.join("runs"),
4295            &f.home,
4296            &f.bay,
4297            &Fx::disk(),
4298            Fx::now(),
4299        )
4300        .await
4301        .unwrap();
4302        assert_eq!((folded, unreadable), (1, 0));
4303        assert!(!source.candidates[0].worktree.exists());
4304        let evidence = f
4305            .home
4306            .join("runs")
4307            .join(&source.id)
4308            .join("artifacts/fold-evidence");
4309        let names: Vec<_> = std::fs::read_dir(evidence)
4310            .unwrap()
4311            .map(|entry| entry.unwrap().file_name())
4312            .collect();
4313        assert_eq!(names, [std::ffi::OsString::from("manifest.json")]);
4314        assert_eq!(
4315            queue.get(&task.id).unwrap().status,
4316            crate::queue::TaskStatus::Held
4317        );
4318    }
4319
4320    #[tokio::test]
4321    async fn merged_fold_skips_proven_squash_and_archives_unpublished_sources() {
4322        for variant in ["exact", "partial", "checkpoint", "loser", "reviewer"] {
4323            let f = Fx::new().await;
4324            let mut state = f.run("cc06", true).await;
4325            let candidate = state.candidates[0].clone();
4326            let intermediate = git::rev_parse(&candidate.worktree, "HEAD").await.unwrap();
4327            std::fs::write(candidate.worktree.join("second"), [0, 1, 255, 42]).unwrap();
4328            git::rescue_commit(&candidate.worktree, "second candidate change")
4329                .await
4330                .unwrap();
4331            let head = git::rev_parse(&candidate.worktree, "HEAD").await.unwrap();
4332            state.tally = Some(crate::run::Tally {
4333                first_choice: Default::default(),
4334                borda: Default::default(),
4335                winner: 'A',
4336                rankings: 0,
4337                unanimous_initial: true,
4338                deliberated: false,
4339                changed_votes: 0,
4340                unanimous_final: true,
4341                tie_break: None,
4342                judges: 0,
4343                present: 0,
4344                quorum: 0,
4345                met_quorum: true,
4346                uncontested: Some("fixture".into()),
4347            });
4348            // A reviewer pinned to an intermediate candidate commit must also
4349            // be reclaimable, although neither commit ID lands in the squash.
4350            let reviewer = state.worktree_root().join("review-1");
4351            git::worktree_add_detached(&f.repo, &reviewer, &intermediate)
4352                .await
4353                .unwrap();
4354            // Concurrent base changes and later base advances prevent a mere
4355            // equality check against the current base tree from passing.
4356            std::fs::write(f.repo.join("unrelated-before"), "base advance").unwrap();
4357            git::rescue_commit(&f.repo, "advance before squash")
4358                .await
4359                .unwrap();
4360            let published = if variant == "partial" {
4361                &intermediate
4362            } else {
4363                &head
4364            };
4365            git::git(&f.repo, &["merge", "--squash", published])
4366                .await
4367                .unwrap();
4368            git::git(&f.repo, &["commit", "-m", "squash candidate"])
4369                .await
4370                .unwrap();
4371            let squash = git::rev_parse(&f.repo, "HEAD").await.unwrap();
4372            assert!(
4373                !git::git_raw(&f.repo, &["merge-base", "--is-ancestor", &head, &squash])
4374                    .await
4375                    .unwrap()
4376                    .ok()
4377            );
4378            std::fs::write(f.repo.join("unrelated-after"), "later base advance").unwrap();
4379            git::rescue_commit(&f.repo, "advance after squash")
4380                .await
4381                .unwrap();
4382            git::git(&f.repo, &["update-ref", "refs/remotes/origin/main", "HEAD"])
4383                .await
4384                .unwrap();
4385            if matches!(variant, "checkpoint" | "reviewer") {
4386                // A reset hides unique work from HEAD, but not its reflog.
4387                std::fs::write(reviewer.join("unpublished"), "reviewer checkpoint").unwrap();
4388                git::rescue_commit(&reviewer, "unpublished checkpoint")
4389                    .await
4390                    .unwrap();
4391                if variant == "checkpoint" {
4392                    git::git(&reviewer, &["reset", "--hard", &intermediate])
4393                        .await
4394                        .unwrap();
4395                }
4396            }
4397            if variant == "loser" {
4398                let mut loser = candidate.clone();
4399                loser.label = 'B';
4400                loser.branch = "magi/cc06/B".into();
4401                loser.worktree = state.worktree_root().join("cand-B");
4402                git::worktree_add_branch(&f.repo, &loser.worktree, &loser.branch, &f.base)
4403                    .await
4404                    .unwrap();
4405                std::fs::write(loser.worktree.join("unique"), "losing candidate").unwrap();
4406                git::rescue_commit(&loser.worktree, "independent loser")
4407                    .await
4408                    .unwrap();
4409                state.candidates.push(loser);
4410            }
4411            state.status = RunStatus::Merged;
4412            state.merge = Some(crate::run::MergeOutcome {
4413                mode: crate::config::MergeMode::Pr,
4414                ok: true,
4415                detail: "candidate A squash merged".into(),
4416                empty: false,
4417            });
4418            state.save_under(&f.home).unwrap();
4419            let review_head = git::rev_parse(&reviewer, "HEAD").await.unwrap();
4420            let result = crate::graph::fold_run(&mut state, true, &f.home).await;
4421            result.unwrap();
4422            assert!(
4423                !candidate.worktree.exists() && !reviewer.exists(),
4424                "{variant}"
4425            );
4426            assert!(
4427                !git::branch_exists(&f.repo, &candidate.branch)
4428                    .await
4429                    .unwrap()
4430            );
4431            let artifacts = f
4432                .home
4433                .join("runs")
4434                .join(&state.id)
4435                .join("artifacts/fold-evidence");
4436            assert_eq!(
4437                artifacts.join(format!("{head}.patch")).exists(),
4438                variant == "partial",
4439                "only a partially published winner needs evidence: {variant}"
4440            );
4441            assert_eq!(
4442                artifacts.join(format!("{intermediate}.patch")).exists(),
4443                variant == "partial",
4444                "squash proof also covers intermediate reviewer tips: {variant}"
4445            );
4446            let patches: Vec<_> = std::fs::read_dir(&artifacts)
4447                .unwrap()
4448                .map(|entry| entry.unwrap().path())
4449                .filter(|path| path.extension().is_some_and(|ext| ext == "patch"))
4450                .collect();
4451            let expected = match variant {
4452                "partial" => 2,
4453                "loser" | "reviewer" => 1,
4454                _ => 0,
4455            };
4456            assert_eq!(patches.len(), expected, "{variant}");
4457            if variant == "loser" {
4458                assert!(
4459                    std::fs::read_to_string(&patches[0])
4460                        .unwrap()
4461                        .contains("losing candidate")
4462                );
4463            }
4464            assert!(artifacts.join("manifest.json").exists());
4465            if variant == "reviewer" {
4466                assert!(
4467                    std::fs::read_to_string(artifacts.join(format!("{review_head}.patch")))
4468                        .unwrap()
4469                        .contains("reviewer checkpoint")
4470                );
4471            }
4472            crate::graph::fold_run(&mut state, true, &f.home)
4473                .await
4474                .unwrap();
4475        }
4476    }
4477
4478    #[tokio::test]
4479    async fn merged_fold_missing_review_source_preserves_artifacts_and_retries() {
4480        for missing in [true, false] {
4481            let f = Fx::new().await;
4482            let mut state = f.run("cc05", true).await;
4483            // A fetched base exists, but these local review changes were not
4484            // incorporated by ancestry, so their recovery evidence is required.
4485            git::git(
4486                &f.repo,
4487                &["update-ref", "refs/remotes/origin/main", &f.base],
4488            )
4489            .await
4490            .unwrap();
4491            let candidate = state.candidates[0].clone();
4492            if !missing {
4493                std::fs::write(f.repo.join("base-advance"), "new base").unwrap();
4494                git::rescue_commit(&f.repo, "advance base before rebase")
4495                    .await
4496                    .unwrap();
4497                git::git(&candidate.worktree, &["rebase", "main"])
4498                    .await
4499                    .unwrap();
4500                // Match the runner's post-rebase synchronization, which
4501                // clears the merge backend's transient AUTO_MERGE checkpoint.
4502                git::sync_to_head(&candidate.worktree).await.unwrap();
4503            }
4504            let head = git::rev_parse(&candidate.worktree, "HEAD").await.unwrap();
4505            // Model review-only state: no implementation-time snapshot exists.
4506            state.reviewed_commits = Some(Vec::new());
4507            state.candidates[0].worktree = state.worktree_root().join("under-review");
4508            git::git(
4509                &f.repo,
4510                &[
4511                    "worktree",
4512                    "move",
4513                    candidate.worktree.to_str().unwrap(),
4514                    state.candidates[0].worktree.to_str().unwrap(),
4515                ],
4516            )
4517            .await
4518            .unwrap();
4519            let reviewer = state.worktree_root().join("review-1");
4520            git::worktree_add_detached(&f.repo, &reviewer, &head)
4521                .await
4522                .unwrap();
4523            // A reset/rebase can leave unpublished recovery history. It must
4524            // not keep any remaining owned worktree alive indefinitely.
4525            std::fs::write(reviewer.join("obsolete"), "old history").unwrap();
4526            git::rescue_commit(&reviewer, "old review history")
4527                .await
4528                .unwrap();
4529            git::git(&reviewer, &["reset", "--hard", &head])
4530                .await
4531                .unwrap();
4532            if missing {
4533                std::fs::remove_dir_all(&state.candidates[0].worktree).unwrap();
4534            }
4535            let run_dir = f.home.join("runs").join(&state.id);
4536            let artifacts = run_dir.join("artifacts");
4537            std::fs::create_dir_all(&artifacts).unwrap();
4538            std::fs::write(artifacts.join("review.json"), "review findings").unwrap();
4539            if missing {
4540                std::fs::write(artifacts.join("cand-A.patch"), "original snapshot").unwrap();
4541            }
4542            state.status = RunStatus::Merged;
4543            state.merge = Some(crate::run::MergeOutcome {
4544                mode: crate::config::MergeMode::Pr,
4545                ok: true,
4546                detail: "confirmed squash merge".into(),
4547                empty: false,
4548            });
4549            state.save_under(&f.home).unwrap();
4550            // Unexpected external paths remain protected, even if absent.
4551            let owned = state.candidates[0].worktree.clone();
4552            state.candidates[0].worktree = f.home.join("missing-external");
4553            assert!(
4554                crate::graph::fold_run(&mut state, true, &f.home)
4555                    .await
4556                    .unwrap_err()
4557                    .to_string()
4558                    .contains("outside the inventoried")
4559            );
4560            state.candidates[0].worktree = owned;
4561            // A live output borrower blocks the entire preflight.
4562            let guard = OutputLease::borrow(&reviewer).unwrap().unwrap();
4563            assert!(
4564                crate::graph::fold_run(&mut state, true, &f.home)
4565                    .await
4566                    .is_err()
4567            );
4568            assert!(reviewer.exists());
4569            drop(guard);
4570            // Failure to persist evidence must not cause partial removal.
4571            let evidence = artifacts.join("fold-evidence");
4572            std::fs::write(&evidence, "blocked").unwrap();
4573            assert!(
4574                crate::graph::fold_run(&mut state, true, &f.home)
4575                    .await
4576                    .is_err()
4577            );
4578            assert!(reviewer.exists());
4579            std::fs::remove_file(&evidence).unwrap();
4580            std::fs::create_dir_all(&evidence).unwrap();
4581            let oversized = evidence.join("capacity-fixture");
4582            std::fs::File::create(&oversized)
4583                .unwrap()
4584                .set_len(64 * 1024 * 1024)
4585                .unwrap();
4586            assert!(
4587                crate::graph::fold_run(&mut state, true, &f.home)
4588                    .await
4589                    .is_err()
4590            );
4591            assert!(reviewer.exists());
4592            std::fs::remove_file(oversized).unwrap();
4593            // Ignored recovery files and unfinished Git operations still hold
4594            // all sources, even for a confirmed merge.
4595            std::fs::create_dir_all(reviewer.join("scratch")).unwrap();
4596            std::fs::write(reviewer.join("scratch/recovery"), "keep").unwrap();
4597            assert!(
4598                crate::graph::fold_run(&mut state, true, &f.home)
4599                    .await
4600                    .is_err()
4601            );
4602            std::fs::remove_dir_all(reviewer.join("scratch")).unwrap();
4603            let checkpoint = git::git(&reviewer, &["rev-parse", "--git-path", "CHERRY_PICK_HEAD"])
4604                .await
4605                .unwrap();
4606            std::fs::write(&checkpoint, &head).unwrap();
4607            assert!(
4608                crate::graph::fold_run(&mut state, true, &f.home)
4609                    .await
4610                    .is_err()
4611            );
4612            std::fs::remove_file(checkpoint).unwrap();
4613            let (folded, unreadable) = crate::clean::fold_due(
4614                &f.home.join("runs"),
4615                &f.home,
4616                &f.bay,
4617                &Fx::disk(),
4618                Fx::now(),
4619            )
4620            .await
4621            .unwrap();
4622            if folded != 1 {
4623                panic!(
4624                    "normal cleanup failed: {:#}",
4625                    crate::graph::fold_run(&mut state, true, &f.home)
4626                        .await
4627                        .unwrap_err()
4628                );
4629            }
4630            assert_eq!((folded, unreadable), (1, 0));
4631            assert!(!reviewer.exists() && !state.candidates[0].worktree.exists());
4632            state = RunState::load_under(&state.id, &f.home).unwrap();
4633            assert!(state.candidates[0].folded);
4634            let manifest = std::fs::read(evidence.join("manifest.json")).unwrap();
4635            crate::graph::fold_run(&mut state, true, &f.home)
4636                .await
4637                .unwrap();
4638            assert_eq!(
4639                std::fs::read(evidence.join("manifest.json")).unwrap(),
4640                manifest
4641            );
4642            git::git(&f.repo, &["reflog", "expire", "--expire=now", "--all"])
4643                .await
4644                .unwrap();
4645            git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
4646            assert!(
4647                !git::git_raw(&f.repo, &["cat-file", "-e", &head])
4648                    .await
4649                    .unwrap()
4650                    .ok()
4651            );
4652            // Restore the modified file into a fresh repository from artifact
4653            // bytes alone; no original commit or blob is consulted.
4654            let manifest: serde_json::Value = serde_json::from_slice(&manifest).unwrap();
4655            let old_blob = manifest[format!("head-{head}")]["base_blobs"][0]
4656                .as_str()
4657                .unwrap();
4658            let recovered = f.home.join("recovered");
4659            std::fs::create_dir_all(&recovered).unwrap();
4660            git::git(&recovered, &["init"]).await.unwrap();
4661            std::fs::write(
4662                recovered.join("source"),
4663                std::fs::read(evidence.join(format!("blob-{old_blob}"))).unwrap(),
4664            )
4665            .unwrap();
4666            git::git(
4667                &recovered,
4668                &[
4669                    "apply",
4670                    evidence.join(format!("{head}.patch")).to_str().unwrap(),
4671                ],
4672            )
4673            .await
4674            .unwrap();
4675            assert_eq!(
4676                std::fs::read_to_string(recovered.join("source")).unwrap(),
4677                "cc05"
4678            );
4679            assert!(
4680                !std::fs::read(evidence.join(format!("{head}.patch")))
4681                    .unwrap()
4682                    .is_empty()
4683            );
4684            assert_eq!(
4685                std::fs::read_to_string(artifacts.join("review.json")).unwrap(),
4686                "review findings"
4687            );
4688            if missing {
4689                assert_eq!(
4690                    std::fs::read_to_string(artifacts.join("cand-A.patch")).unwrap(),
4691                    "original snapshot"
4692                );
4693            } else {
4694                assert!(!artifacts.join("cand-A.patch").exists());
4695            }
4696            assert!(run_dir.join("run.json").exists());
4697        }
4698    }
4699
4700    #[tokio::test]
4701    async fn slow_retirement_proof_allows_task_updates_and_aborts_on_new_owner_pin() {
4702        use std::os::unix::fs::PermissionsExt as _;
4703        let f = Fx::new().await;
4704        let source = f.run("dd03", true).await;
4705        let empty = f.run("dd04", false).await;
4706        let task = f.task(&[source, empty.clone()]);
4707        let entered = f.repo.join("proof-entered");
4708        let release = f.repo.join("proof-release");
4709        let hook = f.repo.join("proof-monitor");
4710        std::fs::write(&hook, format!(
4711            "#!/bin/sh\n: > '{}'\nwhile ! test -f '{}'; do sleep 0.01; done\nprintf 'probe\\0'\n",
4712            entered.display(), release.display())).unwrap();
4713        std::fs::set_permissions(&hook, std::fs::Permissions::from_mode(0o755)).unwrap();
4714        git::git(
4715            &f.repo,
4716            &["config", "core.fsmonitor", hook.to_str().unwrap()],
4717        )
4718        .await
4719        .unwrap();
4720        struct ReleaseProof(PathBuf);
4721        impl Drop for ReleaseProof {
4722            fn drop(&mut self) {
4723                let _ = std::fs::write(&self.0, "continue");
4724            }
4725        }
4726        let _release_on_panic = ReleaseProof(release.clone());
4727        let home = f.home.clone();
4728        let sweep = tokio::spawn(async move {
4729            let mut report = Report::default();
4730            retire_empty(&home, &Fx::disk(), Fx::now(), &mut report).await;
4731            report
4732        });
4733        // These are deadlock watchdogs, not speed assertions. The proof stays
4734        // blocked until the task update completes, even on a loaded runner.
4735        let watchdog = std::time::Duration::from_secs(60);
4736        tokio::time::timeout(watchdog, async {
4737            while !entered.exists() {
4738                tokio::time::sleep(std::time::Duration::from_millis(10)).await;
4739            }
4740        })
4741        .await
4742        .expect("Git retirement proof did not enter the blocking hook");
4743        let queue = crate::queue::Queue::at(f.home.join("queue"));
4744        let mut selected = queue.get(&task.id).unwrap();
4745        selected.resume_override = Some(crate::queue::OperatorResume {
4746            question_id: "during-slow-proof".into(),
4747            at: Fx::now(),
4748            conductor_rehold: None,
4749            forced: false,
4750            pinned_run: Some(empty.id.clone()),
4751        });
4752        let mut update = tokio::task::spawn_blocking(move || queue.put(&mut selected));
4753        // The proof is released only after the update returns, so correctness
4754        // is decided by ordering, not speed. The 60-second bound only detects
4755        // a writer blocked behind the proof; it is not a performance threshold.
4756        let updated_while_blocked = match tokio::time::timeout(watchdog, &mut update).await {
4757            Ok(result) => {
4758                result.unwrap().unwrap();
4759                true
4760            }
4761            Err(_) => {
4762                std::fs::write(&release, "continue").unwrap();
4763                update.await.unwrap().unwrap();
4764                false
4765            }
4766        };
4767        std::fs::write(release, "continue").unwrap();
4768        let report = tokio::time::timeout(watchdog, sweep)
4769            .await
4770            .expect("Git retirement proof did not finish after release")
4771            .unwrap();
4772        assert!(
4773            updated_while_blocked,
4774            "task writers must not wait behind Git retirement proof"
4775        );
4776        assert_eq!(report.retired, 0);
4777        assert!(report.retained.iter().any(|r| r.contains("task changed")));
4778        assert!(empty.candidates[0].worktree.exists());
4779        assert!(
4780            RunState::load_under(&empty.id, &f.home)
4781                .unwrap()
4782                .retired_to
4783                .is_none()
4784        );
4785    }
4786
4787    #[tokio::test]
4788    async fn retirement_commit_rechecks_task_after_unlocked_proof_and_preserves_new_pin() {
4789        let f = Fx::new().await;
4790        let source = f.run("dd01", true).await;
4791        let mut empty = f.run("dd02", false).await;
4792        let expected = f.task(&[source.clone(), empty.clone()]);
4793        let queue = crate::queue::Queue::at(f.home.join("queue"));
4794        let _claim = queue.claim(&expected.id).unwrap();
4795        // The proof holds run/output leases, never the task write lock.
4796        let _owner = crate::pr_head::owner_lock(&f.home, &empty.id).unwrap();
4797        let _driver = crate::driver::maintenance(&f.home, &empty.id).unwrap();
4798        let _outputs = OutputLease::take(&empty.candidates[0].worktree, true).unwrap();
4799        let mut selected = queue.get(&expected.id).unwrap();
4800        selected.resume_override = Some(crate::queue::OperatorResume {
4801            question_id: "new-owner-decision".into(),
4802            at: Fx::now(),
4803            conductor_rehold: None,
4804            forced: false,
4805            pinned_run: Some(empty.id.clone()),
4806        });
4807        queue.put(&mut selected).unwrap();
4808        let error = commit_retirement(
4809            &f.home,
4810            &queue,
4811            &expected,
4812            &mut empty,
4813            &source.id,
4814            &expected.runs,
4815            Fx::now(),
4816        )
4817        .unwrap_err();
4818        assert!(error.to_string().contains("task changed"));
4819        assert!(
4820            RunState::load_under(&empty.id, &f.home)
4821                .unwrap()
4822                .retired_to
4823                .is_none()
4824        );
4825        assert!(empty.candidates[0].worktree.exists());
4826        assert_eq!(
4827            queue
4828                .get(&expected.id)
4829                .unwrap()
4830                .resume_override
4831                .unwrap()
4832                .pinned_run,
4833            Some(empty.id.clone())
4834        );
4835        assert!(
4836            !f.home
4837                .join("recovery-lineages")
4838                .join(format!("{}.json", expected.id))
4839                .exists()
4840        );
4841        // Re-reading the new snapshot must not bypass its explicit pin either.
4842        assert!(
4843            commit_retirement(
4844                &f.home,
4845                &queue,
4846                &selected,
4847                &mut empty,
4848                &source.id,
4849                &selected.runs,
4850                Fx::now()
4851            )
4852            .unwrap_err()
4853            .to_string()
4854            .contains("selected this recovery source")
4855        );
4856    }
4857
4858    #[tokio::test]
4859    async fn cargo_owners_and_driver_resumes_are_protected_with_retained_root_data() {
4860        let f = Fx::new().await;
4861        let state = f.run("cccc", false).await;
4862        let wt = &state.candidates[0].worktree;
4863        Fx::target(wt);
4864        std::fs::write(wt.join("target/notes"), "user file").unwrap();
4865        std::os::unix::fs::symlink(f.repo.join("source"), wt.join("target/debug/link")).unwrap();
4866        assert!(
4867            !declared_outputs(&wt.join("target"))
4868                .unwrap()
4869                .contains(&wt.join("target/debug/link"))
4870        );
4871        std::fs::remove_file(wt.join("target/debug/link")).unwrap();
4872        let cargo = cargo_locks(&wt.join("target")).unwrap();
4873        let out = sweep(&f.home, &Fx::disk(), Fx::now()).await;
4874        assert_eq!(out.freed_bytes, 0);
4875        assert!(out.retained.iter().any(|s| s.contains("Cargo build")));
4876        drop(cargo);
4877        let driver = crate::driver::maintenance(&f.home, &state.id).unwrap();
4878        let out = sweep(&f.home, &Fx::disk(), Fx::now()).await;
4879        assert_eq!(out.freed_bytes, 0);
4880        drop(driver);
4881        let exclusive = OutputLease::take(wt, true).unwrap();
4882        assert!(
4883            OutputLease::borrow(wt).is_err(),
4884            "resume cannot start a writer during cleanup"
4885        );
4886        drop(exclusive);
4887        assert!(sweep(&f.home, &Fx::disk(), Fx::now()).await.freed_bytes > 0);
4888        assert_eq!(
4889            std::fs::read(wt.join("target/notes")).unwrap(),
4890            b"user file"
4891        );
4892    }
4893
4894    #[tokio::test]
4895    async fn root_leftovers_allow_eviction_but_keep_empty_attempts_registered() {
4896        for leftover in ["tmp", "magi-tools", "lifecycle-client-check.log"] {
4897            let f = Fx::new().await;
4898            let source = f.run("aa10", true).await;
4899            let empty = f.run("aa11", false).await;
4900            f.task(&[source, empty.clone()]);
4901            let wt = &empty.candidates[0].worktree;
4902            Fx::target(wt);
4903            let path = wt.join("target").join(leftover);
4904            if leftover.ends_with(".log") {
4905                std::fs::write(&path, "keep log\n").unwrap();
4906            } else {
4907                // Empty unknown directories are not present in residue_files.
4908                std::fs::create_dir(&path).unwrap();
4909            }
4910            let mut disk = Fx::disk();
4911            disk.auto_fold = true;
4912            let report = sweep(&f.home, &disk, Fx::now()).await;
4913            assert_eq!(report.freed_bytes, 200, "{report:?}");
4914            assert_eq!(report.retired, 0, "{report:?}");
4915            assert!(!wt.join("target/debug/output").exists());
4916            assert!(!residue_proof(&f.home, &empty.id, wt).unwrap().exists());
4917            assert!(path.exists());
4918            if leftover.ends_with(".log") {
4919                assert_eq!(std::fs::read(&path).unwrap(), b"keep log\n");
4920            }
4921            assert_eq!(std::fs::read(wt.join("source")).unwrap(), b"base\n");
4922            assert!(worktrees(&empty).await.unwrap().contains(wt));
4923            assert!(
4924                git::branch_exists(&f.repo, &empty.candidates[0].branch)
4925                    .await
4926                    .unwrap()
4927            );
4928            assert!(
4929                RunState::load_under(&empty.id, &f.home)
4930                    .unwrap()
4931                    .retired_to
4932                    .is_none()
4933            );
4934        }
4935    }
4936
4937    #[tokio::test]
4938    async fn residue_proof_rejects_new_unknown_root_entries_including_empty_directories() {
4939        let f = Fx::new().await;
4940        let state = f.run("aa12", false).await;
4941        let wt = &state.candidates[0].worktree;
4942        Fx::target(wt);
4943        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
4944        assert_eq!(report.freed_bytes, 200, "{report:?}");
4945        assert!(verify_residue(&f.home, &state.id, wt).await.is_ok());
4946        for name in ["tmp", "magi-tools", "log"] {
4947            let path = wt.join("target").join(name);
4948            std::fs::create_dir(&path).unwrap();
4949            assert!(verify_residue(&f.home, &state.id, wt).await.is_err());
4950            std::fs::remove_dir(path).unwrap();
4951        }
4952        assert!(verify_residue(&f.home, &state.id, wt).await.is_ok());
4953    }
4954
4955    #[tokio::test]
4956    async fn target_identity_and_known_profiles_still_fail_closed_with_root_leftovers() {
4957        for invalid in [
4958            "tag",
4959            "identity",
4960            "tracked",
4961            "profile-file",
4962            "profile-symlink",
4963            "identity-symlink",
4964        ] {
4965            let f = Fx::new().await;
4966            let state = f.run("aa13", false).await;
4967            let wt = &state.candidates[0].worktree;
4968            Fx::target(wt);
4969            std::fs::create_dir(wt.join("target/tmp")).unwrap();
4970            let target = wt.join("target");
4971            match invalid {
4972                "tag" => std::fs::write(target.join("CACHEDIR.TAG"), "not Cargo").unwrap(),
4973                "identity" => std::fs::write(target.join(".rustc_info.json"), "{}").unwrap(),
4974                "tracked" => {
4975                    git::git(wt, &["add", "-f", "target/debug/output"])
4976                        .await
4977                        .unwrap();
4978                }
4979                "profile-file" => {
4980                    std::fs::write(target.join("release"), "keep").unwrap();
4981                }
4982                "profile-symlink" => {
4983                    std::os::unix::fs::symlink(target.join("debug"), target.join("release"))
4984                        .unwrap();
4985                }
4986                "identity-symlink" => {
4987                    std::fs::rename(target.join(".rustc_info.json"), target.join("saved-info"))
4988                        .unwrap();
4989                    std::os::unix::fs::symlink(
4990                        target.join("saved-info"),
4991                        target.join(".rustc_info.json"),
4992                    )
4993                    .unwrap();
4994                }
4995                _ => unreachable!(),
4996            }
4997            let before = std::fs::read(target.join("debug/output")).unwrap();
4998            let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
4999            assert_eq!(report.freed_bytes, 0, "{invalid}: {report:?}");
5000            assert_eq!(std::fs::read(target.join("debug/output")).unwrap(), before);
5001            assert!(target.join("tmp").is_dir());
5002        }
5003    }
5004
5005    #[tokio::test]
5006    async fn reclaimed_cargo_residue_allows_retirement_only_while_exact_and_unowned() {
5007        let f = Fx::new().await;
5008        let source = f.run("0000", true).await;
5009        let empty = f.run("0001", false).await;
5010        let changed = f.run("0002", false).await;
5011        let unknown = f.run("0003", false).await;
5012        let unproven = f.run("0004", false).await;
5013        f.task(&[
5014            source.clone(),
5015            empty.clone(),
5016            changed.clone(),
5017            unknown.clone(),
5018            unproven.clone(),
5019        ]);
5020        for run in [&empty, &changed, &unknown] {
5021            Fx::target(&run.candidates[0].worktree);
5022        }
5023        let reviewer = empty.worktree_root().join("review-1");
5024        git::worktree_add_detached(&f.repo, &reviewer, &f.base)
5025            .await
5026            .unwrap();
5027        Fx::target(&reviewer);
5028        let mut disk = Fx::disk();
5029        disk.auto_fold = false;
5030        let report = sweep(&f.home, &disk, Fx::now()).await;
5031        assert_eq!(report.freed_bytes, 800, "{report:?}");
5032        let wt = &empty.candidates[0].worktree;
5033        assert!(wt.join("target/debug/output.d").exists());
5034        assert!(!wt.join("target/debug/output").exists());
5035        let proof_path = residue_proof(&f.home, &empty.id, wt).unwrap();
5036        let proof: CargoResidue =
5037            serde_json::from_slice(&std::fs::read(&proof_path).unwrap()).unwrap();
5038        assert_eq!(
5039            proof.files[Path::new("debug/output.d")],
5040            std::fs::read(wt.join("target/debug/output.d")).unwrap()
5041        );
5042        std::fs::write(
5043            changed.candidates[0].worktree.join("target/debug/output.d"),
5044            "owner notes",
5045        )
5046        .unwrap();
5047        std::fs::write(
5048            unknown.candidates[0].worktree.join("target/debug/notes"),
5049            "keep",
5050        )
5051        .unwrap();
5052        Fx::target(&unproven.candidates[0].worktree);
5053        std::fs::remove_file(unproven.candidates[0].worktree.join("target/debug/output")).unwrap();
5054        disk.auto_fold = true;
5055        let locks = cargo_locks(&wt.join("target")).unwrap();
5056        let mut report = Report::default();
5057        retire_empty(&f.home, &disk, Fx::now(), &mut report).await;
5058        assert_eq!(report.retired, 0, "{report:?}");
5059        assert!(
5060            report
5061                .retained
5062                .iter()
5063                .any(|reason| reason.contains("Cargo build"))
5064        );
5065        drop(locks);
5066        retire_empty(&f.home, &disk, Fx::now(), &mut report).await;
5067        assert_eq!(report.retired, 1, "{report:?}");
5068        assert!(!wt.exists());
5069        assert!(!reviewer.exists());
5070        assert!(
5071            residue_proof(&f.home, &empty.id, &reviewer)
5072                .unwrap()
5073                .exists()
5074        );
5075        assert!(
5076            proof_path.exists(),
5077            "metadata evidence outlives the worktree"
5078        );
5079        assert_eq!(
5080            RunState::load_under(&empty.id, &f.home).unwrap().retired_to,
5081            Some(source.id)
5082        );
5083        for run in [&changed, &unknown, &unproven] {
5084            assert!(run.candidates[0].worktree.exists());
5085            assert!(
5086                RunState::load_under(&run.id, &f.home)
5087                    .unwrap()
5088                    .retired_to
5089                    .is_none()
5090            );
5091        }
5092    }
5093
5094    #[tokio::test]
5095    async fn empty_backlog_retires_to_useful_older_source_in_bounded_idempotent_passes() {
5096        let f = Fx::new().await;
5097        let source = f.run("0000", true).await;
5098        let source_head = git::rev_parse(&f.repo, &source.candidates[0].branch)
5099            .await
5100            .unwrap();
5101        let mut runs = vec![source.clone()];
5102        for n in 1..=12 {
5103            runs.push(f.run(&format!("{n:04}"), false).await);
5104        }
5105        let dirty = runs[1].candidates[0].worktree.clone();
5106        std::fs::write(dirty.join("unrecorded"), "preparation changes").unwrap();
5107        let ignored = runs[2].candidates[0].worktree.clone();
5108        std::fs::create_dir(ignored.join("scratch")).unwrap();
5109        std::fs::write(ignored.join("scratch/note"), "keep").unwrap();
5110        // A missing candidate array does not prove that an unrecorded worktree
5111        // is empty: discovery must inspect Git and the worktree itself.
5112        runs[1].candidates.clear();
5113        runs[1].save_under(&f.home).unwrap();
5114        let task = f.task(&runs);
5115        let mut total = 0;
5116        for _ in 0..4 {
5117            let mut report = Report::default();
5118            retire_empty(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5119            assert!(report.retired <= 5);
5120            total += report.retired;
5121        }
5122        assert_eq!(total, 10);
5123        assert!(dirty.exists());
5124        assert!(ignored.exists());
5125        assert_eq!(
5126            git::rev_parse(&f.repo, &source.candidates[0].branch)
5127                .await
5128                .unwrap(),
5129            source_head
5130        );
5131        let retired = RunState::load_under(&runs[3].id, &f.home).unwrap();
5132        assert_eq!(retired.retired_to.as_ref(), Some(&source.id));
5133        assert!(crate::pr_head::ensure_owner_record(&retired, Some(&retired)).is_err());
5134        let mut released = task.clone();
5135        released.release();
5136        assert_eq!(
5137            crate::daemon::unfinished_run_with(&released.runs, released.short(), |id| {
5138                RunState::load_under(id, &f.home)
5139            }),
5140            Some(source.id.clone())
5141        );
5142
5143        assert!(
5144            f.home
5145                .join("runs")
5146                .join(&retired.id)
5147                .join("run.json")
5148                .exists()
5149        );
5150        assert!(
5151            f.home
5152                .join("recovery-lineages")
5153                .join(format!("{}.json", task.id))
5154                .exists()
5155        );
5156    }
5157
5158    #[tokio::test]
5159    async fn distinct_candidates_and_explicitly_retained_sources_are_never_discarded() {
5160        let f = Fx::new().await;
5161        let a = f.run("dddd", true).await;
5162        let b = f.run("eeee", true).await;
5163        let empty = f.run("ffff", false).await;
5164        let task = f.task(&[a.clone(), b.clone(), empty.clone()]);
5165        assert!(
5166            crate::pr_recovery::source_retention_reason(&f.home, &a).is_some(),
5167            "unfinished queue history protects older source branches from ordinary folding"
5168        );
5169        let mut prior = a.clone();
5170        assert!(
5171            crate::graph::fold_run(&mut prior, true, &f.home)
5172                .await
5173                .unwrap_err()
5174                .to_string()
5175                .contains(&task.id)
5176        );
5177        let mut report = Report::default();
5178        retire_empty(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5179        assert_eq!(report.retired, 0);
5180        assert!(report.retained.iter().any(|r| r.contains("ambiguous")));
5181        crate::pr_recovery::retain_sources(&task, &f.home).unwrap();
5182        Fx::target(&a.candidates[0].worktree);
5183        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
5184        assert!(
5185            report.freed_bytes > 0,
5186            "source retention does not retain regenerable outputs"
5187        );
5188        assert!(a.candidates[0].worktree.exists());
5189        assert!(b.candidates[0].worktree.exists());
5190        assert!(empty.candidates[0].worktree.exists());
5191    }
5192
5193    #[tokio::test]
5194    async fn decisions_checkpoints_and_partial_retirement_remain_safe() {
5195        let f = Fx::new().await;
5196        let authority = f.run("p000", true).await;
5197        let question_run = f.run("p001", false).await;
5198        let mut checkpoint = f.run("p002", false).await;
5199        checkpoint.land_approval = Some(crate::run::LandApproval {
5200            question: "pending".into(),
5201            head: f.base.clone(),
5202        });
5203        checkpoint.save_under(&f.home).unwrap();
5204        let retained = f.run("p003", false).await;
5205        std::fs::write(
5206            f.home
5207                .join("runs")
5208                .join(&retained.id)
5209                .join("retain-recovery-sources"),
5210            "owner",
5211        )
5212        .unwrap();
5213        let mut partial = f.run("p004", false).await;
5214        let reviewer = partial.worktree_root().join("review-1");
5215        git::worktree_add_detached(&f.repo, &reviewer, &f.base)
5216            .await
5217            .unwrap();
5218        // Model a crash after the retirement fence but only one removal.
5219        partial.retired_to = Some(authority.id.clone());
5220        partial.save_under(&f.home).unwrap();
5221        assert!(
5222            git::worktree_remove_clean(&f.repo, &partial.candidates[0].worktree)
5223                .await
5224                .unwrap()
5225        );
5226        let git_checkpoint = f.run("p005", false).await;
5227        let location = git::git(
5228            &git_checkpoint.candidates[0].worktree,
5229            &[
5230                "rev-parse",
5231                "--path-format=absolute",
5232                "--git-path",
5233                "sequencer",
5234            ],
5235        )
5236        .await
5237        .unwrap();
5238        std::fs::create_dir(location.trim()).unwrap();
5239        let mut preservation = f.run("p006", false).await;
5240        preservation
5241            .candidate_preservation
5242            .insert('A', "pending preservation".into());
5243        preservation.save_under(&f.home).unwrap();
5244        let task = f.task(&[
5245            preservation.clone(),
5246            git_checkpoint.clone(),
5247            authority.clone(),
5248            question_run.clone(),
5249            checkpoint.clone(),
5250            retained.clone(),
5251            partial.clone(),
5252        ]);
5253        let questions = crate::ask::Questions::at(f.home.join("questions"));
5254        let mut question = crate::ask::Question::new(
5255            question_run.id.clone(),
5256            "implement".into(),
5257            "fixture".into(),
5258            "owner decision".into(),
5259            String::new(),
5260            vec!["keep".into()],
5261        );
5262        questions.put(&mut question).unwrap();
5263        let mut report = Report::default();
5264        retire_empty(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5265        assert_eq!(report.retired, 1, "{report:?}");
5266        assert!(!reviewer.exists());
5267        for state in [
5268            &question_run,
5269            &checkpoint,
5270            &retained,
5271            &git_checkpoint,
5272            &preservation,
5273        ] {
5274            assert!(state.candidates[0].worktree.exists());
5275            assert!(
5276                RunState::load_under(&state.id, &f.home)
5277                    .unwrap()
5278                    .retired_to
5279                    .is_none()
5280            );
5281        }
5282        assert!(questions.get(&question.id).unwrap().status.open());
5283        assert_eq!(
5284            crate::queue::Queue::at(f.home.join("queue"))
5285                .get(&task.id)
5286                .unwrap()
5287                .status,
5288            task.status
5289        );
5290    }
5291
5292    #[tokio::test]
5293    async fn preservation_schema_migration_remains_distinct_from_retirement() {
5294        let f = Fx::new().await;
5295        let mut state = f.run("p007", false).await;
5296        state.schema = 15;
5297        state.save_under(&f.home).unwrap();
5298        let native = RunState::load_under(&state.id, &f.home).unwrap();
5299        assert_eq!(native.schema, crate::run::SCHEMA);
5300        assert!(native.candidate_preservation.is_empty());
5301        assert!(native.retired_to.is_none());
5302        state.schema = 14;
5303        state.save_under(&f.home).unwrap();
5304        let mut legacy = RunState::load_under(&state.id, &f.home).unwrap();
5305        assert!(legacy.candidate_preservation.contains_key(&'A'));
5306        legacy.save_under(&f.home).unwrap();
5307        let restored = RunState::load_under(&state.id, &f.home).unwrap();
5308        assert_eq!(
5309            restored.candidate_preservation,
5310            legacy.candidate_preservation
5311        );
5312    }
5313
5314    #[tokio::test]
5315    async fn cache_cleanup_excludes_process_cleanup_and_missing_path_aliases() {
5316        let f = Fx::new().await;
5317        let cache = f.repo.join("cache");
5318        let guard = OutputLease::cache(&f.home, &cache, false).unwrap();
5319        std::fs::create_dir_all(&cache).unwrap();
5320        assert!(OutputLease::cache(&f.home, &cache, true).is_err());
5321        assert!(
5322            crate::cache::maintenance_prune(&f.home, &cache, 1)
5323                .unwrap()
5324                .is_none()
5325        );
5326        drop(guard);
5327        assert!(OutputLease::cache(&f.home, &cache, true).is_ok());
5328    }
5329
5330    #[tokio::test]
5331    async fn launcher_death_does_not_release_live_tool_output_reservations() {
5332        let f = Fx::new().await;
5333        let run = f.run("abcd", false).await;
5334        let wt = &run.candidates[0].worktree;
5335        let mut helper = tokio::process::Command::new(std::env::current_exe().unwrap());
5336        helper
5337            .quiet()
5338            .args(["--exact", "seat_process::tests::crashed_launcher_helper"])
5339            .env("MAGI_TEST_CRASHED_LAUNCHER", &f.home)
5340            .env("MAGI_TEST_WORKTREE", wt)
5341            .stdin(std::process::Stdio::null())
5342            .stdout(std::process::Stdio::null())
5343            .stderr(std::process::Stdio::null())
5344            .kill_on_drop(true);
5345        assert!(
5346            tokio::time::timeout(std::time::Duration::from_secs(8), helper.status())
5347                .await
5348                .unwrap()
5349                .unwrap()
5350                .success()
5351        );
5352        let pid: i32 = std::fs::read_to_string(f.home.join("tool.pid"))
5353            .unwrap()
5354            .trim()
5355            .parse()
5356            .unwrap();
5357        struct KillTool(i32);
5358        impl Drop for KillTool {
5359            fn drop(&mut self) {
5360                // SAFETY: PID belongs to the bounded helper above.
5361                unsafe { libc::kill(self.0, libc::SIGKILL) };
5362            }
5363        }
5364        let tool = KillTool(pid);
5365        assert!(OutputLease::take(wt, true).is_err());
5366        assert!(OutputLease::cache(&f.home, &f.home.join("cache"), true).is_err());
5367        drop(tool);
5368        tokio::time::timeout(std::time::Duration::from_secs(5), async {
5369            loop {
5370                if OutputLease::take(wt, true).is_ok()
5371                    && OutputLease::cache(&f.home, &f.home.join("cache"), true).is_ok()
5372                {
5373                    break;
5374                }
5375                tokio::time::sleep(std::time::Duration::from_millis(10)).await;
5376            }
5377        })
5378        .await
5379        .unwrap();
5380    }
5381
5382    #[tokio::test]
5383    async fn orphaned_tool_keeps_output_and_cache_leases_after_cli_exit() {
5384        let f = Fx::new().await;
5385        let run = f.run("abcd", false).await;
5386        let wt = &run.candidates[0].worktree;
5387        let cache = f.repo.join("cache");
5388        std::fs::create_dir(&cache).unwrap();
5389        for (cancel, clear_owner) in [(false, true), (true, true), (false, false), (true, false)] {
5390            let pid_file = f.repo.join(format!("orphan-{cancel}-{clear_owner}"));
5391            let mut cmd = tokio::process::Command::new(std::env::current_exe().unwrap());
5392            cmd.quiet()
5393                .args(["--exact", "seat_process::tests::orphan_tool_helper"])
5394                .env("MAGI_TEST_ORPHAN_PID", &pid_file)
5395                .stdin(std::process::Stdio::null())
5396                .stdout(std::process::Stdio::null())
5397                .stderr(std::process::Stdio::null())
5398                .kill_on_drop(true);
5399            if clear_owner {
5400                cmd.env("MAGI_TEST_CLEAR_OWNER", "1");
5401            } else {
5402                cmd.env("MAGI_TEST_CLOSE_FDS", "1");
5403            }
5404            let gib = 1024 * 1024 * 1024;
5405            let mut leases = vec![
5406                OutputLease::borrow(wt).unwrap().unwrap(),
5407                OutputLease::cache(&f.home, &cache, false).unwrap(),
5408            ];
5409            leases.extend(
5410                OutputLease::admit_with(
5411                    &f.home,
5412                    std::slice::from_ref(wt),
5413                    8 * gib,
5414                    |_| Ok(9 * gib),
5415                )
5416                .unwrap(),
5417            );
5418            let mut running = crate::seat_process::Running::spawn(cmd, None, leases).unwrap();
5419            assert!(
5420                tokio::time::timeout(std::time::Duration::from_secs(5), running.child().wait())
5421                    .await
5422                    .unwrap()
5423                    .unwrap()
5424                    .success()
5425            );
5426            let pid: i32 = std::fs::read_to_string(pid_file).unwrap().parse().unwrap();
5427            struct KillTool(Option<i32>);
5428            impl Drop for KillTool {
5429                fn drop(&mut self) {
5430                    // SAFETY: PID from our helper; kill also bounds panic paths.
5431                    if let Some(pid) = self.0 {
5432                        unsafe { libc::kill(pid, libc::SIGKILL) };
5433                    }
5434                }
5435            }
5436            let mut tool = KillTool(Some(pid));
5437            // The CLI has already exited and cannot supply a parent-tree
5438            // relationship. Either the witness or inherited environment must
5439            // retain ownership; test each independently.
5440            assert!(OutputLease::take(wt, true).is_err());
5441            let finish = if cancel {
5442                drop(running);
5443                None
5444            } else {
5445                Some(tokio::spawn(async move { running.finish().await }))
5446            };
5447            if clear_owner {
5448                tokio::time::sleep(std::time::Duration::from_millis(150)).await;
5449                assert!(crate::proc::pid_alive(pid as u32));
5450                assert!(OutputLease::take(wt, true).is_err(), "cancel={cancel}");
5451                assert!(OutputLease::cache(&f.home, &cache, true).is_err());
5452                assert!(
5453                    OutputLease::admit_with(&f.home, std::slice::from_ref(wt), 8 * gib, |_| Ok(
5454                        9 * gib
5455                    ))
5456                    .is_err(),
5457                    "orphan still owns its disk reservation"
5458                );
5459                assert!(
5460                    crate::cache::maintenance_prune(&f.home, &cache, 0)
5461                        .unwrap()
5462                        .is_none()
5463                );
5464                if let Some(finish) = &finish {
5465                    assert!(!finish.is_finished());
5466                }
5467                // Witness-only ownership waits for natural/external tool exit.
5468                unsafe { libc::kill(pid, libc::SIGKILL) };
5469            }
5470            if let Some(finish) = finish {
5471                tokio::time::timeout(std::time::Duration::from_secs(5), finish)
5472                    .await
5473                    .unwrap()
5474                    .unwrap();
5475            }
5476            tokio::time::timeout(std::time::Duration::from_secs(5), async {
5477                loop {
5478                    if OutputLease::take(wt, true).is_ok()
5479                        && OutputLease::cache(&f.home, &cache, true).is_ok()
5480                        && OutputLease::admit_with(
5481                            &f.home,
5482                            std::slice::from_ref(wt),
5483                            8 * gib,
5484                            |_| Ok(9 * gib),
5485                        )
5486                        .is_ok()
5487                    {
5488                        // Token-owned orphans must be stopped by cleanup itself.
5489                        if !clear_owner {
5490                            assert!(!crate::proc::pid_alive(pid as u32));
5491                        }
5492                        break;
5493                    }
5494                    tokio::time::sleep(std::time::Duration::from_millis(10)).await;
5495                }
5496            })
5497            .await
5498            .unwrap();
5499            tool.0 = None;
5500        }
5501    }
5502
5503    #[tokio::test]
5504    async fn cancellation_keeps_output_and_cache_ownership_until_tools_terminate() {
5505        let f = Fx::new().await;
5506        let run = f.run("abcd", false).await;
5507        let wt = &run.candidates[0].worktree;
5508        let cache = f.repo.join("cache");
5509        std::fs::create_dir(&cache).unwrap();
5510        let pid_file = f.repo.join("child-pid");
5511        let mut cmd = tokio::process::Command::new("sh");
5512        cmd.args([
5513            "-c",
5514            r#"sleep 30 &
5515               printf '%s\n' "$!" > "$CHILD_PID_FILE.tmp" &&
5516               mv "$CHILD_PID_FILE.tmp" "$CHILD_PID_FILE" &&
5517               wait"#,
5518        ])
5519        .quiet()
5520        .env("CHILD_PID_FILE", &pid_file)
5521        .process_group(0)
5522        .kill_on_drop(true);
5523        let running = crate::seat_process::Running::spawn(
5524            cmd,
5525            None,
5526            vec![
5527                OutputLease::borrow(wt).unwrap().unwrap(),
5528                OutputLease::cache(&f.home, &cache, false).unwrap(),
5529            ],
5530        )
5531        .unwrap();
5532        tokio::time::timeout(std::time::Duration::from_secs(5), async {
5533            while !pid_file.exists() {
5534                tokio::time::sleep(std::time::Duration::from_millis(10)).await;
5535            }
5536        })
5537        .await
5538        .unwrap();
5539        assert!(OutputLease::take(wt, true).is_err());
5540        assert!(
5541            crate::cache::maintenance_prune(&f.home, &cache, 0)
5542                .unwrap()
5543                .is_none()
5544        );
5545        let pid: u32 = std::fs::read_to_string(pid_file)
5546            .unwrap()
5547            .trim()
5548            .parse()
5549            .unwrap();
5550        drop(running);
5551        tokio::time::timeout(std::time::Duration::from_secs(10), async {
5552            loop {
5553                if let Ok(guard) = OutputLease::take(wt, true) {
5554                    assert!(
5555                        !crate::proc::pid_alive(pid),
5556                        "output lock released before tool death"
5557                    );
5558                    drop(guard);
5559                    break;
5560                }
5561                tokio::time::sleep(std::time::Duration::from_millis(10)).await;
5562            }
5563        })
5564        .await
5565        .unwrap();
5566        assert!(OutputLease::cache(&f.home, &cache, true).is_ok());
5567    }
5568
5569    #[tokio::test]
5570    async fn emergency_pressure_closes_writes_and_zero_floor_opts_out() {
5571        let f = Fx::new().await;
5572        let reason = tokio::time::timeout(
5573            std::time::Duration::from_secs(2),
5574            emergency(vec![f.repo.clone()], u64::MAX, false),
5575        )
5576        .await
5577        .unwrap();
5578        assert!(reason.contains("disk pressure"));
5579        assert!(
5580            tokio::time::timeout(
5581                std::time::Duration::from_millis(10),
5582                emergency(vec![f.repo.clone()], 0, false)
5583            )
5584            .await
5585            .is_err()
5586        );
5587        assert_eq!(
5588            std::fs::read_to_string(f.repo.join("source")).unwrap(),
5589            "base\n"
5590        );
5591    }
5592
5593    #[tokio::test]
5594    async fn automatic_requeues_cannot_refund_failed_input_generation() {
5595        let f = Fx::new().await;
5596        let mut run = f.run("1234", false).await;
5597        run.prep_failure = Some("cherry-pick conflict".into());
5598        let mut task = f.task(std::slice::from_ref(&run));
5599        let inputs = crate::queue::recovery_inputs(&run.sources);
5600        for _ in 0..108 {
5601            task.recovery_failed(&run);
5602            task.requeue();
5603            assert_eq!(task.attempts, 0);
5604        }
5605        let bytes = serde_json::to_vec(&task).unwrap();
5606        let mut restored: Task = serde_json::from_slice(&bytes).unwrap();
5607        assert!(
5608            restored
5609                .recovery_exhausted(&run.base_commit, &inputs, 2)
5610                .is_some()
5611        );
5612        assert!(
5613            restored
5614                .recovery_exhausted("different-base", &inputs, 2)
5615                .is_none()
5616        );
5617        let original_base = run.base_commit.clone();
5618        run.base_commit = "different-base".into();
5619        restored.recovery_failed(&run);
5620        assert!(
5621            restored
5622                .recovery_exhausted(&original_base, &inputs, 2)
5623                .is_some(),
5624            "returning to old inputs does not replenish their budget"
5625        );
5626        assert!(
5627            restored
5628                .recovery_exhausted(&run.base_commit, &inputs, 2)
5629                .is_none()
5630        );
5631        restored.release_operator("test-owner", None);
5632        assert!(
5633            restored
5634                .recovery_exhausted(&run.base_commit, &inputs, 2)
5635                .is_none()
5636        );
5637    }
5638    async fn inactive_source(f: &Fx, tag: &str, useful: bool) -> RunState {
5639        let mut state = f.run(tag, useful).await;
5640        state.status = RunStatus::Superseded;
5641        state.save_under(&f.home).unwrap();
5642        state
5643    }
5644
5645    fn historical_review(state: &mut RunState, head: &str) {
5646        state.reviews.push(
5647            serde_json::from_value(serde_json::json!({
5648                "round": state.reviews.len() + 1, "head": head, "reviews": []
5649            }))
5650            .unwrap(),
5651        );
5652    }
5653
5654    async fn reviewer(f: &Fx, state: &RunState, head: &str) -> PathBuf {
5655        let path = state.worktree_root().join("review-1");
5656        git::worktree_add_detached(&f.repo, &path, head)
5657            .await
5658            .unwrap();
5659        path
5660    }
5661
5662    #[tokio::test]
5663    async fn source_eviction_separates_run_authority_from_read_seats() {
5664        for status in [RunStatus::Superseded, RunStatus::Ready, RunStatus::Merged] {
5665            let f = Fx::new().await;
5666            let mut state = inactive_source(&f, "ef01", true).await;
5667            state.status = status;
5668            state.pr = Some(
5669                serde_json::from_value(serde_json::json!({
5670                    "url":"https://example.invalid/pull/1", "number":1, "state":"merged",
5671                    "checks":"green", "round":1, "rounds":1
5672                }))
5673                .unwrap(),
5674            );
5675            state.pr_head = Some(state.candidates[0].branch.clone());
5676            state.pr_merged_head = Some(f.base.clone());
5677            state.recovery = Some(
5678                serde_json::from_value(serde_json::json!({
5679                    "from_run": "20260101-000000-abcd", "generation": 1,
5680                    "mode": "full-review"
5681                }))
5682                .unwrap(),
5683            );
5684            state.pr_recovery = Some(
5685                serde_json::from_value(serde_json::json!({
5686                    "url": "https://example.invalid/pull/1", "branch": "public",
5687                    "head": f.base, "source_run": state.id,
5688                    "instruction": "keep implementation", "approved_at": Fx::now()
5689                }))
5690                .unwrap(),
5691            );
5692            state.save_under(&f.home).unwrap();
5693            let review = reviewer(&f, &state, &f.base).await;
5694            let mut other = inactive_source(&f, "ef02", true).await;
5695            other.pr_recovery = state.pr_recovery.clone();
5696            other.save_under(&f.home).unwrap();
5697            let mut task = f.task(&[state.clone(), other]);
5698            task.pr_recovery = state.pr_recovery.clone();
5699            crate::queue::Queue::at(f.home.join("queue"))
5700                .put(&mut task)
5701                .unwrap();
5702            let run_path = f.home.join("runs").join(&state.id).join("run.json");
5703            let before = std::fs::read(&run_path).unwrap();
5704            let task_before = serde_json::to_value(&task).unwrap();
5705            let mut report = Report::default();
5706            copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5707            assert_eq!(report.evicted_copies, 1, "{status:?}: {report:?}");
5708            assert!(!review.exists());
5709            assert!(state.candidates[0].worktree.exists());
5710            assert!(
5711                git::branch_exists(&f.repo, &state.candidates[0].branch)
5712                    .await
5713                    .unwrap()
5714            );
5715            assert_eq!(std::fs::read(&run_path).unwrap(), before);
5716            assert_eq!(
5717                serde_json::to_value(
5718                    crate::queue::Queue::at(f.home.join("queue"))
5719                        .get(&task.id)
5720                        .unwrap()
5721                )
5722                .unwrap(),
5723                task_before
5724            );
5725        }
5726    }
5727
5728    #[tokio::test]
5729    async fn source_eviction_retains_own_paths_and_unfinished_continuations() {
5730        for kind in ["own-path", "active-seat", "paused-session"] {
5731            let f = Fx::new().await;
5732            let mut state = inactive_source(&f, "ef03", true).await;
5733            let review = reviewer(&f, &state, &f.base).await;
5734            let expected = match kind {
5735                "own-path" => {
5736                    state.attachments.push(review.join("source"));
5737                    "run.attachments[0]"
5738                }
5739                "active-seat" => {
5740                    state.active.insert(
5741                        "review-1".into(),
5742                        serde_json::from_value(serde_json::json!({
5743                            "node":"review", "started_at":Fx::now(), "timeout_secs":60
5744                        }))
5745                        .unwrap(),
5746                    );
5747                    "continuation lease"
5748                }
5749                _ => {
5750                    state.status = RunStatus::Blocked;
5751                    state.parked = true;
5752                    let mut seat = crate::agent::SeatState::new("review-1", "fixture", 1);
5753                    seat.turns = 2;
5754                    seat.captured_session = Some("saved-session".into());
5755                    state.seats.insert("review-1".into(), seat);
5756                    "resume contract"
5757                }
5758            };
5759            state.save_under(&f.home).unwrap();
5760            let mut report = Report::default();
5761            copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5762            assert_eq!(report.evicted_copies, 0, "{kind}: {report:?}");
5763            assert!(review.exists());
5764            assert!(
5765                report.retained.iter().any(|r| r.contains(expected)),
5766                "{kind}: {report:?}"
5767            );
5768            // The execution restoration boundary leaves protected existing
5769            // seats and their sessions untouched; prep need not recreate them.
5770            restore_execution_seats(&f.home, &state, None)
5771                .await
5772                .unwrap();
5773            assert_eq!(git::rev_parse(&review, "HEAD").await.unwrap(), f.base);
5774        }
5775    }
5776
5777    #[tokio::test]
5778    async fn source_eviction_distinguishes_historical_cwd_from_question_dependencies() {
5779        for kind in ["waiting", "lease", "historical", "unknown"] {
5780            let f = Fx::new().await;
5781            let state = inactive_source(&f, "ef06", true).await;
5782            let review = reviewer(&f, &state, &f.base).await;
5783            let store = crate::ask::Questions::at(f.home.join("questions"));
5784            let mut question = crate::ask::Question::new(
5785                "20260101-000000-ffff".into(),
5786                "review".into(),
5787                "review-1".into(),
5788                "fixture".into(),
5789                "fixture".into(),
5790                Vec::new(),
5791            );
5792            question.cwd = Some(review.to_string_lossy().into_owned());
5793            if kind != "waiting" {
5794                question.status = crate::ask::QuestionStatus::Abandoned;
5795            }
5796            store.put(&mut question).unwrap();
5797            if kind == "lease" {
5798                store.beat(&question.id, crate::ask::WaiterKind::Daemon);
5799            }
5800            if kind == "unknown" {
5801                let path = store.path_of(&question.id);
5802                let mut raw: serde_json::Value =
5803                    serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
5804                raw["future_cwd_authority"] = serde_json::Value::Null;
5805                std::fs::write(path, serde_json::to_vec(&raw).unwrap()).unwrap();
5806            }
5807            let mut report = Report::default();
5808            copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5809            assert_eq!(
5810                report.evicted_copies,
5811                usize::from(kind == "historical"),
5812                "{kind}: {report:?}"
5813            );
5814            assert_eq!(review.exists(), kind != "historical");
5815            if kind == "waiting" || kind == "lease" {
5816                assert!(
5817                    report
5818                        .retained
5819                        .iter()
5820                        .any(|r| r.contains("question") && r.contains("cwd")),
5821                    "{report:?}"
5822                );
5823            }
5824        }
5825    }
5826
5827    #[tokio::test]
5828    async fn archived_seat_setup_reuses_repository_coordination() {
5829        crate::run::pin_test_home();
5830        let f = Fx::new().await;
5831        let mut state = inactive_source(&f, "ef07", true).await;
5832        let review = reviewer(&f, &state, &f.base).await;
5833        let mut report = Report::default();
5834        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5835        assert_eq!(report.evicted_copies, 1, "{report:?}");
5836        git::acquire_worktree_config(&f.repo).await.unwrap();
5837        state.config.verify.build_cache = crate::config::BuildCache::Isolated;
5838        state.config.verify.shell = Some(vec!["sh".into(), "-c".into()]);
5839        state.config.disk.min_free_bytes = 0;
5840        state.config.worktree.setup = vec![crate::config::SetupStep {
5841            run: Some("touch setup-completed".into()),
5842            ..Default::default()
5843        }];
5844        std::fs::write(f.repo.join(".git/fixture-contract"), "fixture-v1").unwrap();
5845        state.config.coordination = Some(crate::config::Coordination {
5846            contract_file: "fixture-contract".into(),
5847            contract_token: "fixture-v1".into(),
5848            lock_file: "fixture-lock".into(),
5849        });
5850        let timeout = std::time::Duration::from_secs(15);
5851        let wait = crate::repo_lock::WaitPolicy::new(timeout, || false);
5852        let lease = crate::repo_lock::SeatLease::operation_with(
5853            &f.repo,
5854            state.config.coordination.as_ref(),
5855            &wait,
5856        )
5857        .await
5858        .unwrap()
5859        .unwrap();
5860        for _ in 0..2 {
5861            tokio::time::timeout(
5862                timeout,
5863                restore_execution_seats(&f.home, &state, Some(&lease)),
5864            )
5865            .await
5866            .expect("restoration must not wait for its own repository operation")
5867            .unwrap();
5868        }
5869        assert!(review.join("setup-completed").is_file());
5870    }
5871
5872    #[tokio::test]
5873    async fn source_eviction_failed_execution_setup_keeps_its_checkpoint() {
5874        let f = Fx::new().await;
5875        let mut state = inactive_source(&f, "ef07", true).await;
5876        let review = reviewer(&f, &state, &f.base).await;
5877        let mut report = Report::default();
5878        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5879        assert_eq!(report.evicted_copies, 1, "{report:?}");
5880        state.config.worktree.setup = vec![crate::config::SetupStep {
5881            run: Some("printf preserved > source; exit 1".into()),
5882            ..Default::default()
5883        }];
5884        state.save_under(&f.home).unwrap();
5885        assert!(
5886            restore_execution_seats(&f.home, &state, None)
5887                .await
5888                .is_err()
5889        );
5890        let marker = f
5891            .home
5892            .join("runs")
5893            .join(&state.id)
5894            .join("artifacts/restore-review-1.pending");
5895        assert!(marker.exists());
5896        assert_eq!(
5897            std::fs::read_to_string(review.join("source")).unwrap(),
5898            "preserved"
5899        );
5900        assert!(
5901            restore_execution_seats(&f.home, &state, None)
5902                .await
5903                .unwrap_err()
5904                .to_string()
5905                .contains("interrupted execution restoration")
5906        );
5907        let mut retry = Report::default();
5908        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut retry).await;
5909        assert_eq!(retry.evicted_copies, 0, "{retry:?}");
5910        assert!(
5911            retry
5912                .retained
5913                .iter()
5914                .any(|r| r.contains("restoration checkpoint")),
5915            "{retry:?}"
5916        );
5917        assert!(marker.exists());
5918        assert_eq!(
5919            std::fs::read_to_string(review.join("source")).unwrap(),
5920            "preserved"
5921        );
5922    }
5923
5924    #[tokio::test]
5925    async fn source_eviction_retains_borrowed_or_promisor_anchor_stores() {
5926        for kind in [
5927            "alternates",
5928            "promisor-config",
5929            "promisor-pack",
5930            "objects-symlink",
5931        ] {
5932            let f = Fx::new().await;
5933            let state = inactive_source(&f, "ef11", true).await;
5934            let review = reviewer(&f, &state, &f.base).await;
5935            let common = crate::repo_lock::common_dir(&f.repo)
5936                .await
5937                .unwrap()
5938                .unwrap();
5939            match kind {
5940                "alternates" => {
5941                    let borrowed = f._temp.path().join("borrowed-objects");
5942                    std::fs::create_dir(&borrowed).unwrap();
5943                    std::fs::write(
5944                        common.join("objects/info/alternates"),
5945                        format!("{}\n", borrowed.display()),
5946                    )
5947                    .unwrap();
5948                }
5949                "promisor-config" => {
5950                    git::git(&f.repo, &["config", "remote.origin.promisor", "true"])
5951                        .await
5952                        .unwrap();
5953                }
5954                "promisor-pack" => {
5955                    std::fs::write(common.join("objects/pack/fixture.promisor"), "").unwrap();
5956                }
5957                _ => {
5958                    let borrowed = f._temp.path().join("borrowed-objects");
5959                    std::fs::rename(common.join("objects"), &borrowed).unwrap();
5960                    std::os::unix::fs::symlink(borrowed, common.join("objects")).unwrap();
5961                }
5962            }
5963            let mut report = Report::default();
5964            copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
5965            assert_eq!(report.evicted_copies, 0, "{kind}: {report:?}");
5966            assert!(review.exists());
5967            assert!(
5968                report.retained.iter().any(|r| r.contains("source anchor")),
5969                "{kind}: {report:?}"
5970            );
5971        }
5972    }
5973
5974    #[tokio::test]
5975    async fn source_eviction_bounds_long_shared_history_and_requires_its_retained_anchor() {
5976        let mut f = Fx::new().await;
5977        crate::fold_evidence::fixtures::shared_history(&f.repo, &f.base, 5000).await;
5978        f.base = git::rev_parse(&f.repo, "HEAD").await.unwrap();
5979        assert_eq!(
5980            git::git(&f.repo, &["rev-list", "--count", &f.base])
5981                .await
5982                .unwrap(),
5983            "5001"
5984        );
5985        let state = inactive_source(&f, "ef10", true).await;
5986        let review = reviewer(&f, &state, &f.base).await;
5987        std::fs::write(review.join("source"), "bounded review delta").unwrap();
5988        git::git(&review, &["add", "."]).await.unwrap();
5989        let tree = git::git(&review, &["write-tree"]).await.unwrap();
5990        let head = git::git(
5991            &review,
5992            &["commit-tree", &tree, "-p", &f.base, "-m", "review delta"],
5993        )
5994        .await
5995        .unwrap();
5996        git::git(&review, &["checkout", "--detach", &head])
5997            .await
5998            .unwrap();
5999        let run_file = f.home.join("runs").join(&state.id).join("run.json");
6000        let run_before = std::fs::read(&run_file).unwrap();
6001        let mut report = Report::default();
6002        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
6003        assert_eq!(report.evicted_copies, 1, "{report:?}");
6004        assert!(!review.exists());
6005        assert!(state.candidates[0].worktree.exists());
6006        assert!(
6007            git::branch_exists(&f.repo, &state.candidates[0].branch)
6008                .await
6009                .unwrap()
6010        );
6011        assert_eq!(std::fs::read(&run_file).unwrap(), run_before);
6012        let archive = f
6013            .home
6014            .join("runs")
6015            .join(&state.id)
6016            .join("artifacts/source-evidence")
6017            .join(format!("{head}-{}", f.base));
6018        let manifest: serde_json::Value =
6019            serde_json::from_slice(&std::fs::read(archive.join("manifest.json")).unwrap()).unwrap();
6020        assert_eq!(manifest["anchor"], f.base);
6021        assert!(
6022            std::fs::metadata(archive.join("objects.pack"))
6023                .unwrap()
6024                .len()
6025                < 16 * 1024
6026        );
6027        let pin = format!("refs/magi/source-anchors/{}", f.base);
6028        assert_eq!(git::rev_parse(&f.repo, &pin).await.unwrap(), f.base);
6029        git::git(&f.repo, &["reflog", "expire", "--expire=now", "--all"])
6030            .await
6031            .unwrap();
6032        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6033        assert!(
6034            !git::git_raw(&f.repo, &["cat-file", "-e", &head])
6035                .await
6036                .unwrap()
6037                .ok()
6038        );
6039        let other = git::rev_parse(&state.candidates[0].worktree, "HEAD")
6040            .await
6041            .unwrap();
6042        // Missing, moved, symbolic and symlink pins cannot authorize import.
6043        for kind in ["missing", "moved", "symbolic", "symlink"] {
6044            git::git(&f.repo, &["update-ref", "--no-deref", "-d", &pin])
6045                .await
6046                .unwrap();
6047            match kind {
6048                "moved" => {
6049                    git::git(&f.repo, &["update-ref", &pin, &other])
6050                        .await
6051                        .unwrap();
6052                }
6053                "symbolic" => {
6054                    git::git(&f.repo, &["symbolic-ref", &pin, "refs/heads/main"])
6055                        .await
6056                        .unwrap();
6057                }
6058                "symlink" => {
6059                    let common = crate::repo_lock::common_dir(&f.repo)
6060                        .await
6061                        .unwrap()
6062                        .unwrap();
6063                    let target = f._temp.path().join("operator-anchor");
6064                    std::fs::write(&target, format!("{}\n", f.base)).unwrap();
6065                    let file = common.join(&pin);
6066                    std::fs::create_dir_all(file.parent().unwrap()).unwrap();
6067                    std::os::unix::fs::symlink(target, file).unwrap();
6068                }
6069                _ => {}
6070            }
6071            assert!(
6072                restore_execution_seats(&f.home, &state, None)
6073                    .await
6074                    .is_err(),
6075                "{kind}"
6076            );
6077            assert!(!review.exists());
6078            assert!(
6079                !git::git_raw(&f.repo, &["cat-file", "-e", &head])
6080                    .await
6081                    .unwrap()
6082                    .ok()
6083            );
6084            if kind == "symlink" {
6085                let common = crate::repo_lock::common_dir(&f.repo)
6086                    .await
6087                    .unwrap()
6088                    .unwrap();
6089                std::fs::remove_file(common.join(&pin)).unwrap();
6090            } else {
6091                git::git(&f.repo, &["update-ref", "--no-deref", "-d", &pin])
6092                    .await
6093                    .unwrap();
6094            }
6095            git::git(&f.repo, &["update-ref", &pin, &f.base])
6096                .await
6097                .unwrap();
6098        }
6099        // Fixture-only ref deletion proves the pin, rather than an incidental
6100        // main/candidate branch, keeps all shared ancestors through GC.
6101        assert!(
6102            git::worktree_remove_clean(&f.repo, &state.candidates[0].worktree)
6103                .await
6104                .unwrap()
6105        );
6106        git::git(
6107            &f.repo,
6108            &[
6109                "update-ref",
6110                "-d",
6111                &format!("refs/heads/{}", state.candidates[0].branch),
6112            ],
6113        )
6114        .await
6115        .unwrap();
6116        git::git(&f.repo, &["update-ref", "-d", "refs/heads/main"])
6117            .await
6118            .unwrap();
6119        git::git(&f.repo, &["reflog", "expire", "--expire=now", "--all"])
6120            .await
6121            .unwrap();
6122        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6123        assert_eq!(
6124            git::git(&f.repo, &["for-each-ref", "--format=%(refname)"])
6125                .await
6126                .unwrap(),
6127            pin
6128        );
6129        assert_eq!(
6130            git::git(&f.repo, &["rev-list", "--count", &f.base])
6131                .await
6132                .unwrap(),
6133            "5001"
6134        );
6135        restore_execution_seats(&f.home, &state, None)
6136            .await
6137            .unwrap();
6138        assert_eq!(
6139            git::git(&review, &["rev-list", "--count", "HEAD"])
6140                .await
6141                .unwrap(),
6142            "5002"
6143        );
6144        assert_eq!(
6145            git::git(&f.repo, &["rev-parse", "--is-shallow-repository"])
6146                .await
6147                .unwrap(),
6148            "false"
6149        );
6150        git::git(&f.repo, &["fsck", "--full"]).await.unwrap();
6151        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6152        let patch = seat_diff(&f.home, &state.id, &head).await.unwrap();
6153        assert!(String::from_utf8_lossy(&patch).contains("bounded review delta"));
6154        std::fs::remove_dir_all(&f.repo).unwrap();
6155        assert_eq!(seat_diff(&f.home, &state.id, &head).await.unwrap(), patch);
6156        let inspection = f._temp.path().join("shared-history-inspection");
6157        restore_seat(&f.home, &state.id, &head, &inspection)
6158            .await
6159            .unwrap();
6160        assert_eq!(git::rev_parse(&inspection, "HEAD").await.unwrap(), head);
6161        assert_eq!(
6162            git::git(&inspection, &["rev-parse", "--is-shallow-repository"])
6163                .await
6164                .unwrap(),
6165            "true"
6166        );
6167        assert_eq!(
6168            git::git(&inspection, &["log", "--format=%H"])
6169                .await
6170                .unwrap(),
6171            format!("{head}\n{}", f.base)
6172        );
6173        assert!(!inspection.join(".git/objects/info/alternates").exists());
6174        git::git(&inspection, &["fsck", "--full"]).await.unwrap();
6175        git::git(&inspection, &["gc", "--prune=now"]).await.unwrap();
6176    }
6177
6178    #[tokio::test]
6179    async fn source_eviction_retains_tree_only_archives_and_rejects_import_before_writes() {
6180        let f = Fx::new().await;
6181        let state = inactive_source(&f, "ef08", true).await;
6182        let review = reviewer(&f, &state, &f.base).await;
6183        let tree = git::rev_parse(&review, "HEAD^{tree}").await.unwrap();
6184        let parent = git::git(&review, &["commit-tree", &tree, "-m", "U0"])
6185            .await
6186            .unwrap();
6187        let head = git::git(&review, &["commit-tree", &tree, "-p", &parent, "-m", "U1"])
6188            .await
6189            .unwrap();
6190        git::git(&review, &["checkout", "--detach", &head])
6191            .await
6192            .unwrap();
6193        let archive =
6194            crate::fold_evidence::fixtures::tree_only_archive(&state, &f.home, &head).await;
6195        let mut report = Report::default();
6196        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
6197        assert_eq!(report.evicted_copies, 0, "{report:?}");
6198        assert!(review.exists());
6199        assert!(
6200            report
6201                .retained
6202                .iter()
6203                .any(|r| r.contains("incomplete archived commit ancestry")),
6204            "{report:?}"
6205        );
6206        // Simulate a historical checkout already removed by the old writer.
6207        assert!(git::worktree_remove_clean(&f.repo, &review).await.unwrap());
6208        git::git(&f.repo, &["reflog", "expire", "--expire=now", "--all"])
6209            .await
6210            .unwrap();
6211        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6212        for oid in [&parent, &head] {
6213            assert!(
6214                !git::git_raw(&f.repo, &["cat-file", "-e", oid])
6215                    .await
6216                    .unwrap()
6217                    .ok()
6218            );
6219        }
6220        let patch = source_diff(&archive).await.unwrap();
6221        assert!(
6222            crate::fold_evidence::import_snapshot(&archive, &f.repo)
6223                .await
6224                .is_err()
6225        );
6226        assert!(
6227            !git::git_raw(&f.repo, &["cat-file", "-e", &head])
6228                .await
6229                .unwrap()
6230                .ok()
6231        );
6232        git::git(&f.repo, &["fsck", "--full"]).await.unwrap();
6233        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6234        let inspection = f._temp.path().join("legacy-tree-inspection");
6235        restore_source(&archive, &inspection).await.unwrap();
6236        assert_eq!(git::git(&inspection, &["write-tree"]).await.unwrap(), tree);
6237        assert!(
6238            !git::git_raw(&inspection, &["rev-parse", "--verify", "HEAD"])
6239                .await
6240                .unwrap()
6241                .ok()
6242        );
6243        git::git(&inspection, &["fsck", "--full"]).await.unwrap();
6244        git::git(&inspection, &["gc", "--prune=now"]).await.unwrap();
6245        assert_eq!(source_diff(&archive).await.unwrap(), patch);
6246        assert!(state.candidates[0].worktree.exists());
6247    }
6248
6249    #[tokio::test]
6250    async fn source_eviction_retains_ancestry_above_expanded_budget() {
6251        let f = Fx::new().await;
6252        let state = inactive_source(&f, "ef09", true).await;
6253        let review = reviewer(&f, &state, &f.base).await;
6254        std::fs::File::create(review.join("ancestor-only"))
6255            .unwrap()
6256            .set_len(64 * 1024 * 1024 + 1)
6257            .unwrap();
6258        git::git(&review, &["add", "."]).await.unwrap();
6259        let parent_tree = git::git(&review, &["write-tree"]).await.unwrap();
6260        let parent = git::git(
6261            &review,
6262            &["commit-tree", &parent_tree, "-m", "large ancestor"],
6263        )
6264        .await
6265        .unwrap();
6266        std::fs::remove_file(review.join("ancestor-only")).unwrap();
6267        git::git(&review, &["add", "."]).await.unwrap();
6268        let tree = git::git(&review, &["write-tree"]).await.unwrap();
6269        let head = git::git(
6270            &review,
6271            &[
6272                "commit-tree",
6273                &tree,
6274                "-p",
6275                &parent,
6276                "-m",
6277                "small final tree",
6278            ],
6279        )
6280        .await
6281        .unwrap();
6282        git::git(&review, &["checkout", "--detach", &head])
6283            .await
6284            .unwrap();
6285        let mut report = Report::default();
6286        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
6287        assert_eq!(report.evicted_copies, 0, "{report:?}");
6288        assert!(
6289            report
6290                .retained
6291                .iter()
6292                .any(|r| r.contains("expanded object limit exceeded")),
6293            "{report:?}"
6294        );
6295        assert!(review.exists());
6296        assert!(state.candidates[0].worktree.exists());
6297    }
6298
6299    #[tokio::test]
6300    async fn source_eviction_restores_execution_after_gc_and_preserves_generations() {
6301        let f = Fx::new().await;
6302        let state = inactive_source(&f, "ef04", true).await;
6303        let review = reviewer(&f, &state, &f.base).await;
6304        std::fs::write(review.join("ancestor-only"), "ancestral content").unwrap();
6305        git::git(&review, &["add", "."]).await.unwrap();
6306        let parent_tree = git::git(&review, &["write-tree"]).await.unwrap();
6307        let parent = git::git(
6308            &review,
6309            &["commit-tree", &parent_tree, "-m", "unrelated parent"],
6310        )
6311        .await
6312        .unwrap();
6313        std::fs::remove_file(review.join("ancestor-only")).unwrap();
6314        std::fs::write(review.join("source"), "unrelated review checkpoint").unwrap();
6315        git::git(&review, &["add", "."]).await.unwrap();
6316        let tree = git::git(&review, &["write-tree"]).await.unwrap();
6317        let head = git::git(
6318            &review,
6319            &[
6320                "commit-tree",
6321                &tree,
6322                "-p",
6323                &parent,
6324                "-m",
6325                "unrelated review checkpoint",
6326            ],
6327        )
6328        .await
6329        .unwrap();
6330        git::git(&review, &["checkout", "--detach", &head])
6331            .await
6332            .unwrap();
6333        let mut report = Report::default();
6334        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
6335        assert_eq!(report.evicted_copies, 1, "{report:?}");
6336        let ledger = f
6337            .home
6338            .join("runs")
6339            .join(&state.id)
6340            .join("artifacts/eviction-review-1.json");
6341        let original = std::fs::read(&ledger).unwrap();
6342        let diff = seat_diff(&f.home, &state.id, &head).await.unwrap();
6343        assert!(String::from_utf8_lossy(&diff).contains("unrelated review checkpoint"));
6344        git::git(&f.repo, &["reflog", "expire", "--expire=now", "--all"])
6345            .await
6346            .unwrap();
6347        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6348        assert!(
6349            !git::git_raw(&f.repo, &["cat-file", "-e", &head])
6350                .await
6351                .unwrap()
6352                .ok()
6353        );
6354        assert!(
6355            !git::git_raw(&f.repo, &["cat-file", "-e", &parent])
6356                .await
6357                .unwrap()
6358                .ok()
6359        );
6360        assert_eq!(seat_diff(&f.home, &state.id, &head).await.unwrap(), diff);
6361        assert!(
6362            seat_heads(&f.home, &state.id)
6363                .await
6364                .unwrap()
6365                .contains_key(&head)
6366        );
6367        restore_execution_seats(&f.home, &state, None)
6368            .await
6369            .unwrap();
6370        assert_eq!(git::rev_parse(&review, "HEAD").await.unwrap(), head);
6371        assert_eq!(
6372            git::git(&review, &["log", "--format=%H"]).await.unwrap(),
6373            format!("{head}\n{parent}")
6374        );
6375        git::git(&f.repo, &["fsck", "--full"]).await.unwrap();
6376        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6377        let inspection = f._temp.path().join("complete-history-inspection");
6378        restore_seat(&f.home, &state.id, &head, &inspection)
6379            .await
6380            .unwrap();
6381        assert_eq!(
6382            git::git(&inspection, &["log", "--format=%H"])
6383                .await
6384                .unwrap(),
6385            format!("{head}\n{parent}")
6386        );
6387        git::git(&inspection, &["fsck", "--full"]).await.unwrap();
6388        git::git(&inspection, &["gc", "--prune=now"]).await.unwrap();
6389
6390        assert_eq!(git::git(&review, &["write-tree"]).await.unwrap(), tree);
6391        assert!(
6392            !git::git_raw(&review, &["symbolic-ref", "-q", "HEAD"])
6393                .await
6394                .unwrap()
6395                .ok()
6396        );
6397        assert_eq!(
6398            std::fs::read_to_string(review.join("source")).unwrap(),
6399            "unrelated review checkpoint"
6400        );
6401        std::fs::write(review.join("operator-notes"), "keep").unwrap();
6402        assert!(
6403            restore_execution_seats(&f.home, &state, None)
6404                .await
6405                .is_err()
6406        );
6407        assert_eq!(
6408            std::fs::read_to_string(review.join("operator-notes")).unwrap(),
6409            "keep"
6410        );
6411        std::fs::remove_file(review.join("operator-notes")).unwrap();
6412        restore_execution_seats(&f.home, &state, None)
6413            .await
6414            .unwrap();
6415        let mut second = Report::default();
6416        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut second).await;
6417        assert_eq!(second.evicted_copies, 1, "{second:?}");
6418        assert_eq!(std::fs::read(&ledger).unwrap(), original);
6419        assert!(ledger.with_file_name("eviction-review-1-2.json").exists());
6420        // A historical v1 fixture has no explicit base field. Neither schema
6421        // may reinterpret its immutable archive when the owning run advances.
6422        let mut legacy: serde_json::Value = serde_json::from_slice(&original).unwrap();
6423        legacy["version"] = 1.into();
6424        legacy.as_object_mut().unwrap().remove("base");
6425        std::fs::write(&ledger, serde_json::to_vec(&legacy).unwrap()).unwrap();
6426        let mut advanced = state.clone();
6427        advanced.base_commit = git::rev_parse(&state.candidates[0].worktree, "HEAD")
6428            .await
6429            .unwrap();
6430        advanced.save_under(&f.home).unwrap();
6431        assert_eq!(seat_diff(&f.home, &state.id, &head).await.unwrap(), diff);
6432        // Corruption blocks execution before a replacement checkout is made.
6433        let raw: serde_json::Value = serde_json::from_slice(&original).unwrap();
6434        let archive = PathBuf::from(raw["evidence"].as_str().unwrap());
6435        let patch = std::fs::read(archive.join("change.patch")).unwrap();
6436        std::fs::write(archive.join("change.patch"), "corrupt").unwrap();
6437        assert!(
6438            restore_execution_seats(&f.home, &state, None)
6439                .await
6440                .is_err()
6441        );
6442        assert!(!review.exists());
6443        std::fs::write(archive.join("change.patch"), patch).unwrap();
6444        std::fs::create_dir(&review).unwrap();
6445        std::fs::write(review.join("operator-notes"), "keep").unwrap();
6446        assert!(
6447            restore_execution_seats(&f.home, &state, None)
6448                .await
6449                .is_err()
6450        );
6451        assert_eq!(
6452            std::fs::read_to_string(review.join("operator-notes")).unwrap(),
6453            "keep"
6454        );
6455    }
6456
6457    #[tokio::test]
6458    async fn source_eviction_preserves_history_and_restores_binary_modes_after_gc() {
6459        use std::os::unix::fs::PermissionsExt;
6460        let mut f = Fx::new().await;
6461        std::fs::write(f.repo.join("binary"), b"old\0binary\xff").unwrap();
6462        git::git(&f.repo, &["add", "."]).await.unwrap();
6463        git::git(&f.repo, &["commit", "-m", "binary base"])
6464            .await
6465            .unwrap();
6466        f.base = git::rev_parse(&f.repo, "HEAD").await.unwrap();
6467        let mut state = inactive_source(&f, "ec01", true).await;
6468        let candidate = state.candidates[0].worktree.clone();
6469        let earlier = git::rev_parse(&candidate, "HEAD").await.unwrap();
6470        historical_review(&mut state, &earlier);
6471        let review = reviewer(&f, &state, &earlier).await;
6472        std::fs::remove_file(candidate.join("source")).unwrap();
6473        std::fs::write(candidate.join("binary"), b"new\0binary\xfe").unwrap();
6474        std::fs::write(candidate.join("script"), "run\n").unwrap();
6475        std::fs::set_permissions(
6476            candidate.join("script"),
6477            std::fs::Permissions::from_mode(0o755),
6478        )
6479        .unwrap();
6480        git::git(&candidate, &["add", "."]).await.unwrap();
6481        git::git(&candidate, &["commit", "-m", "binary deletion and mode"])
6482            .await
6483            .unwrap();
6484        let head = git::rev_parse(&candidate, "HEAD").await.unwrap();
6485        let tree = git::rev_parse(&candidate, "HEAD^{tree}").await.unwrap();
6486        let base_tree = git::rev_parse(&candidate, &format!("{}^{{tree}}", f.base))
6487            .await
6488            .unwrap();
6489        historical_review(&mut state, &head);
6490        historical_review(&mut state, &head);
6491        state.status = RunStatus::Superseded;
6492        state.save_under(&f.home).unwrap();
6493        let task = f.task(std::slice::from_ref(&state));
6494        let before = serde_json::to_value(&state).unwrap();
6495        git::git(&review, &["reset", "--hard", &head])
6496            .await
6497            .unwrap();
6498        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
6499        assert_eq!(report.evicted_copies, 1, "{report:?}");
6500        assert!(!review.exists());
6501        assert!(candidate.exists(), "selected implementation survives");
6502        assert!(report.source_unlinked_logical_bytes > 0);
6503        assert!(report.source_unlinked_allocated_bytes.is_some());
6504        assert!(report.source_evidence_added_allocated_bytes.is_some());
6505        assert_eq!(
6506            serde_json::to_value(RunState::load_under(&state.id, &f.home).unwrap()).unwrap(),
6507            before
6508        );
6509        assert_eq!(
6510            crate::queue::Queue::at(f.home.join("queue"))
6511                .get(&task.id)
6512                .unwrap()
6513                .runs,
6514            task.runs
6515        );
6516        let archive = f
6517            .home
6518            .join("runs")
6519            .join(&state.id)
6520            .join("artifacts/source-evidence")
6521            .join(format!("{head}-{}", f.base));
6522        let patch = source_diff(&archive).await.unwrap();
6523        assert!(String::from_utf8_lossy(&patch).contains("GIT binary patch"));
6524        assert!(String::from_utf8_lossy(&patch).contains("100755"));
6525        assert!(
6526            git::worktree_remove_clean(&f.repo, &candidate)
6527                .await
6528                .unwrap()
6529        );
6530        git::git(&f.repo, &["branch", "-D", &state.candidates[0].branch])
6531            .await
6532            .unwrap();
6533        git::git(&f.repo, &["reflog", "expire", "--expire=now", "--all"])
6534            .await
6535            .unwrap();
6536        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6537        assert!(
6538            !git::git_raw(&f.repo, &["cat-file", "-e", &head])
6539                .await
6540                .unwrap()
6541                .ok()
6542        );
6543        // Even the remaining base repository is unavailable to the reader.
6544        std::fs::remove_dir_all(&f.repo).unwrap();
6545        assert_eq!(source_diff(&archive).await.unwrap(), patch);
6546        let restored = f._temp.path().join("restored");
6547        restore_source(&archive, &restored).await.unwrap();
6548        assert_eq!(git::git(&restored, &["write-tree"]).await.unwrap(), tree);
6549        assert_eq!(
6550            std::fs::read(restored.join("binary")).unwrap(),
6551            b"new\0binary\xfe"
6552        );
6553        assert!(!restored.join("source").exists());
6554        assert_ne!(
6555            std::fs::metadata(restored.join("script"))
6556                .unwrap()
6557                .permissions()
6558                .mode()
6559                & 0o111,
6560            0
6561        );
6562        assert!(restore_source(&archive, &restored).await.is_err());
6563        // Reconstruct from the archived base plus the displayed binary patch,
6564        // rather than relying only on checking out the archived final tree.
6565        git::git(&restored, &["read-tree", "--reset", "-u", &base_tree])
6566            .await
6567            .unwrap();
6568        let patch_file = f._temp.path().join("saved.patch");
6569        std::fs::write(&patch_file, &patch).unwrap();
6570        git::git(
6571            &restored,
6572            &["apply", "--index", "--binary", patch_file.to_str().unwrap()],
6573        )
6574        .await
6575        .unwrap();
6576        assert_eq!(git::git(&restored, &["write-tree"]).await.unwrap(), tree);
6577        assert_eq!(
6578            std::fs::read(restored.join("binary")).unwrap(),
6579            b"new\0binary\xfe"
6580        );
6581    }
6582
6583    #[tokio::test]
6584    async fn source_eviction_archives_unrelated_tips_and_allows_lineage_references() {
6585        let f = Fx::new().await;
6586        let state = inactive_source(&f, "ec10", true).await;
6587        let review = reviewer(&f, &state, &f.base).await;
6588        let mut successor = inactive_source(&f, "ec11", true).await;
6589        successor.retired_to = Some(state.id.clone());
6590        successor.save_under(&f.home).unwrap();
6591        f.task(&[state.clone(), successor]);
6592
6593        let mut tips = Vec::new();
6594        for contents in ["reflog-only evidence", "ORIG_HEAD-only evidence"] {
6595            std::fs::write(review.join("source"), contents).unwrap();
6596            git::git(&review, &["add", "."]).await.unwrap();
6597            let tree = git::git(&review, &["write-tree"]).await.unwrap();
6598            let tip = git::git(
6599                &review,
6600                &["commit-tree", &tree, "-p", &f.base, "-m", contents],
6601            )
6602            .await
6603            .unwrap();
6604            git::git(&review, &["reset", "--hard", &tip]).await.unwrap();
6605            tips.push(tip);
6606            git::git(&review, &["reset", "--hard", &f.base])
6607                .await
6608                .unwrap();
6609        }
6610        // Keep one tip exclusively in ORIG_HEAD, the other in the HEAD log.
6611        git::git(&review, &["reflog", "delete", "--rewrite", "HEAD@{1}"])
6612            .await
6613            .unwrap();
6614        git::git(&review, &["update-ref", "ORIG_HEAD", &tips[1]])
6615            .await
6616            .unwrap();
6617        let mut report = Report::default();
6618        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
6619        assert_eq!(report.evicted_copies, 1, "{report:?}");
6620        assert!(!review.exists());
6621        git::git(&f.repo, &["reflog", "expire", "--expire=now", "--all"])
6622            .await
6623            .unwrap();
6624        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
6625        for tip in &tips {
6626            assert!(
6627                !git::git_raw(&f.repo, &["cat-file", "-e", tip])
6628                    .await
6629                    .unwrap()
6630                    .ok()
6631            );
6632        }
6633        // The run-oriented interface also works without the source object store.
6634        std::fs::remove_dir_all(&f.repo).unwrap();
6635        let shown = seat_evidence(&f.home, &state.id).await.unwrap();
6636        for (i, tip) in tips.iter().enumerate() {
6637            assert!(
6638                shown
6639                    .iter()
6640                    .any(|row| row.contains(tip) && row.contains("patch_bytes="))
6641            );
6642            let destination = f._temp.path().join(format!("restore-history-{i}"));
6643            restore_seat(&f.home, &state.id, tip, &destination)
6644                .await
6645                .unwrap();
6646            assert_eq!(
6647                std::fs::read_to_string(destination.join("source")).unwrap(),
6648                ["reflog-only evidence", "ORIG_HEAD-only evidence"][i]
6649            );
6650        }
6651        assert!(
6652            restore_seat(
6653                &f.home,
6654                &state.id,
6655                "unknown",
6656                &f._temp.path().join("missing")
6657            )
6658            .await
6659            .is_err()
6660        );
6661    }
6662
6663    #[tokio::test]
6664    async fn source_eviction_revalidates_archive_immediately_before_unlink() {
6665        fn corrupt(path: &Path) -> Result<()> {
6666            let root = path.parent().unwrap();
6667            // Locate the fixture's durable artifact store through its run record
6668            // path supplied below by a test-only hook in the artifact tree.
6669            let marker = root.join("archive-to-corrupt");
6670            let archive = std::fs::read_to_string(&marker)?;
6671            std::fs::write(Path::new(&archive).join("change.patch"), "corrupt")?;
6672            Ok(())
6673        }
6674        let f = Fx::new().await;
6675        let state = inactive_source(&f, "ec12", true).await;
6676        let review = reviewer(&f, &state, &f.base).await;
6677        let archive = f
6678            .home
6679            .join("runs")
6680            .join(&state.id)
6681            .join("artifacts/source-evidence")
6682            .join(format!("{}-{}", f.base, f.base));
6683        std::fs::write(
6684            state.worktree_root().join("archive-to-corrupt"),
6685            archive.to_str().unwrap(),
6686        )
6687        .unwrap();
6688        let mut report = Report::default();
6689        copies::evict_with(&f.home, &Fx::disk(), Fx::now(), &mut report, corrupt).await;
6690        assert_eq!(report.evicted_copies, 0, "{report:?}");
6691        assert!(review.exists());
6692        assert!(
6693            report
6694                .retained
6695                .iter()
6696                .any(|r| r.contains("corrupt snapshot")),
6697            "{report:?}"
6698        );
6699    }
6700
6701    #[tokio::test]
6702    async fn source_eviction_fails_closed_for_dirty_ignored_borrowed_and_authoritative_copies() {
6703        for kind in [
6704            "dirty",
6705            "hidden",
6706            "ignored",
6707            "empty-directory",
6708            "untracked",
6709            "borrowed",
6710            "borrowed-symlink",
6711            "lease",
6712            "owner",
6713            "driver",
6714            "task-pin",
6715            "task-replacement",
6716            "malformed-orig-head",
6717            "grace",
6718            "resumable",
6719            "archive-symlink",
6720            "root-symlink",
6721            "checkpoint",
6722            "symlink",
6723            "retained",
6724            "unreadable",
6725            "unknown-field",
6726            "unknown-task-field",
6727        ] {
6728            let f = Fx::new().await;
6729            let mut state = inactive_source(&f, "ec02", true).await;
6730            let head = git::rev_parse(&state.candidates[0].worktree, "HEAD")
6731                .await
6732                .unwrap();
6733            historical_review(&mut state, &head);
6734            let review = reviewer(&f, &state, &head).await;
6735            let mut output = None;
6736            let mut owner = None;
6737            let mut driver = None;
6738            let mut disk = Fx::disk();
6739            match kind {
6740                "dirty" => std::fs::write(review.join("source"), "notes").unwrap(),
6741                "hidden" => {
6742                    git::git(&review, &["update-index", "--assume-unchanged", "source"])
6743                        .await
6744                        .unwrap();
6745                    std::fs::write(review.join("source"), "notes").unwrap();
6746                }
6747                "ignored" => {
6748                    std::fs::create_dir(review.join("scratch")).unwrap();
6749                    std::fs::write(review.join("scratch/notes"), "notes").unwrap();
6750                }
6751                "empty-directory" => std::fs::create_dir(review.join("owner-notes")).unwrap(),
6752                "untracked" => std::fs::write(review.join("notes"), "notes").unwrap(),
6753                "borrowed" => {
6754                    let mut other = f.run("ec03", true).await;
6755                    other.candidates[0].worktree = review.clone();
6756                    other.save_under(&f.home).unwrap();
6757                }
6758                "borrowed-symlink" => {
6759                    let alias = f._temp.path().join("borrowed-alias");
6760                    std::os::unix::fs::symlink(&review, &alias).unwrap();
6761                    let mut other = f.run("ec03", true).await;
6762                    other.candidates[0].worktree = alias;
6763                    other.save_under(&f.home).unwrap();
6764                }
6765                "lease" => output = OutputLease::borrow(&review).unwrap(),
6766                "owner" => owner = Some(crate::pr_head::owner_lock(&f.home, &state.id).unwrap()),
6767                "driver" => driver = Some(crate::driver::maintenance(&f.home, &state.id).unwrap()),
6768                "task-pin" => {
6769                    let mut task = f.task(std::slice::from_ref(&state));
6770                    task.resume_override = Some(crate::queue::OperatorResume {
6771                        question_id: "fixture-pin".into(),
6772                        at: Fx::now(),
6773                        conductor_rehold: None,
6774                        forced: true,
6775                        pinned_run: Some(state.id.clone()),
6776                    });
6777                    crate::queue::Queue::at(f.home.join("queue"))
6778                        .put(&mut task)
6779                        .unwrap();
6780                }
6781                "task-replacement" => {
6782                    let mut task = f.task(std::slice::from_ref(&state));
6783                    task.replacement = Some(crate::merge_decision::Replacement {
6784                        question: "fixture-replacement".into(),
6785                        run: state.id.clone(),
6786                        pr: "https://example.invalid/pull/1".into(),
6787                        agent: "worker".into(),
6788                        context: String::new(),
6789                    });
6790                    crate::queue::Queue::at(f.home.join("queue"))
6791                        .put(&mut task)
6792                        .unwrap();
6793                }
6794                "malformed-orig-head" => {
6795                    let admin = git::git(&review, &["rev-parse", "--absolute-git-dir"])
6796                        .await
6797                        .unwrap();
6798                    std::fs::write(Path::new(&admin).join("ORIG_HEAD"), "not-an-object-id")
6799                        .unwrap();
6800                }
6801                "grace" => disk.fold_grace_secs = 3600,
6802                "resumable" => state.status = RunStatus::Blocked,
6803                "archive-symlink" => {
6804                    let artifacts = f.home.join("runs").join(&state.id).join("artifacts");
6805                    std::fs::create_dir_all(&artifacts).unwrap();
6806                    let outside = f._temp.path().join("outside");
6807                    std::fs::create_dir(&outside).unwrap();
6808                    std::os::unix::fs::symlink(&outside, artifacts.join("source-evidence"))
6809                        .unwrap();
6810                }
6811                "root-symlink" => {
6812                    let root = state.worktree_root();
6813                    let outside = f._temp.path().join("outside-root");
6814                    std::fs::rename(&root, &outside).unwrap();
6815                    std::os::unix::fs::symlink(&outside, &root).unwrap();
6816                }
6817                "checkpoint" => {
6818                    let admin = git::git(&review, &["rev-parse", "--absolute-git-dir"])
6819                        .await
6820                        .unwrap();
6821                    std::fs::write(Path::new(&admin).join("MERGE_MSG"), "checkpoint").unwrap();
6822                }
6823                "symlink" => {
6824                    std::fs::remove_file(review.join("source")).unwrap();
6825                    std::os::unix::fs::symlink(".gitignore", review.join("source")).unwrap();
6826                }
6827                "retained" => std::fs::write(
6828                    f.home
6829                        .join("runs")
6830                        .join(&state.id)
6831                        .join("retain-recovery-sources"),
6832                    "owner",
6833                )
6834                .unwrap(),
6835                "unreadable" => {
6836                    std::fs::create_dir_all(f.home.join("runs/20260101-000000-ffff")).unwrap();
6837                }
6838                "unknown-field" | "unknown-task-field" => {}
6839                _ => unreachable!(),
6840            }
6841            state.save_under(&f.home).unwrap();
6842            if kind == "unknown-field" {
6843                let path = f.home.join("runs").join(&state.id).join("run.json");
6844                let mut raw: serde_json::Value =
6845                    serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
6846                raw["future_source_owner"] = serde_json::json!({"path": review});
6847                std::fs::write(&path, serde_json::to_vec(&raw).unwrap()).unwrap();
6848            }
6849            if kind == "unknown-task-field" {
6850                let task = f.task(std::slice::from_ref(&state));
6851                let path = crate::queue::Queue::at(f.home.join("queue")).path_of(&task.id);
6852                let mut raw: serde_json::Value =
6853                    serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
6854                raw["future_source_owner"] = serde_json::json!({"path": review});
6855                std::fs::write(&path, serde_json::to_vec(&raw).unwrap()).unwrap();
6856            }
6857            let mut report = Report::default();
6858            copies::evict(&f.home, &disk, Fx::now(), &mut report).await;
6859            assert_eq!(report.evicted_copies, 0, "{kind}: {report:?}");
6860            assert!(review.exists(), "{kind}: {report:?}");
6861            assert!(!report.retained.is_empty(), "{kind}: {report:?}");
6862            drop(output);
6863            drop(owner);
6864            drop(driver);
6865        }
6866    }
6867
6868    #[tokio::test]
6869    async fn source_eviction_reconciles_interruption_and_refuses_corrupt_archives() {
6870        let f = Fx::new().await;
6871        let state = inactive_source(&f, "ec04", true).await;
6872        let review = reviewer(&f, &state, &f.base).await;
6873        let mut report = Report::default();
6874        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
6875        assert_eq!(report.evicted_copies, 1, "{report:?}");
6876        let artifacts = f.home.join("runs").join(&state.id).join("artifacts");
6877        let ledger = artifacts.join("eviction-review-1.json");
6878        let mut record: serde_json::Value =
6879            serde_json::from_slice(&std::fs::read(&ledger).unwrap()).unwrap();
6880        record["phase"] = "pending".into();
6881        std::fs::write(&ledger, serde_json::to_vec(&record).unwrap()).unwrap();
6882        let archive = PathBuf::from(record["evidence"].as_str().unwrap());
6883        let pack = std::fs::read(archive.join("objects.pack")).unwrap();
6884        std::fs::write(archive.join("objects.pack"), b"corrupt").unwrap();
6885        let mut blocked = Report::default();
6886        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut blocked).await;
6887        assert!(
6888            blocked
6889                .retained
6890                .iter()
6891                .any(|r| r.contains("corrupt snapshot")),
6892            "{blocked:?}"
6893        );
6894        assert_eq!(
6895            serde_json::from_slice::<serde_json::Value>(&std::fs::read(&ledger).unwrap()).unwrap()
6896                ["phase"],
6897            "pending"
6898        );
6899        assert!(
6900            restore_source(&archive, &f._temp.path().join("bad-restore"))
6901                .await
6902                .is_err()
6903        );
6904        std::fs::write(archive.join("objects.pack"), &pack).unwrap();
6905        let mut retried = Report::default();
6906        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut retried).await;
6907        assert_eq!(
6908            serde_json::from_slice::<serde_json::Value>(&std::fs::read(&ledger).unwrap()).unwrap()
6909                ["phase"],
6910            "evicted"
6911        );
6912        assert_eq!(retried.evicted_copies, 0);
6913        assert!(!review.exists());
6914        // A stopped archive publisher can resume its immutable staging files.
6915        let pending = archive.with_extension("pending");
6916        std::fs::rename(&archive, &pending).unwrap();
6917        assert_eq!(
6918            crate::fold_evidence::snapshot(&state, &f.home, &f.base, 64 * 1024 * 1024)
6919                .await
6920                .unwrap(),
6921            archive
6922        );
6923        assert!(!pending.exists());
6924    }
6925
6926    #[tokio::test]
6927    async fn source_eviction_reclaims_superseded_empty_attempt_without_retiring_lineage() {
6928        let f = Fx::new().await;
6929        let mut empty = inactive_source(&f, "ec05", false).await;
6930        historical_review(&mut empty, &f.base);
6931        empty.status = RunStatus::Superseded;
6932        empty.save_under(&f.home).unwrap();
6933        let source = inactive_source(&f, "ec06", true).await;
6934        let task = f.task(&[empty.clone(), source.clone()]);
6935        let before = serde_json::to_value(&empty).unwrap();
6936        let mut report = Report::default();
6937        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
6938        assert_eq!(report.evicted_copies, 1, "{report:?}");
6939        assert!(!empty.candidates[0].worktree.exists());
6940        assert!(source.candidates[0].worktree.exists());
6941        assert!(
6942            git::branch_exists(&f.repo, &empty.candidates[0].branch)
6943                .await
6944                .unwrap()
6945        );
6946        assert_eq!(
6947            serde_json::to_value(RunState::load_under(&empty.id, &f.home).unwrap()).unwrap(),
6948            before
6949        );
6950        let mut retry = Report::default();
6951        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut retry).await;
6952        assert_eq!(retry.removed_empty_roots, 1, "{retry:?}");
6953        assert!(!empty.worktree_root().exists());
6954        assert_eq!(
6955            crate::queue::Queue::at(f.home.join("queue"))
6956                .get(&task.id)
6957                .unwrap()
6958                .runs,
6959            task.runs
6960        );
6961    }
6962    #[tokio::test]
6963    async fn source_eviction_retries_pending_intent_and_exact_missing_registration() {
6964        fn interrupt(_: &Path) -> Result<()> {
6965            anyhow::bail!("fixture interruption before unlink")
6966        }
6967        fn vanish(path: &Path) -> Result<()> {
6968            std::fs::remove_dir_all(path)?;
6969            anyhow::bail!("fixture interruption after checkout unlink")
6970        }
6971        for stage in [
6972            "intent",
6973            "missing-registration",
6974            "changed-admin",
6975            "missing-archive",
6976            "wrong-identity",
6977        ] {
6978            let f = Fx::new().await;
6979            let state = inactive_source(&f, "ec07", true).await;
6980            let review = reviewer(&f, &state, &f.base).await;
6981            let admin = PathBuf::from(
6982                git::git(&review, &["rev-parse", "--absolute-git-dir"])
6983                    .await
6984                    .unwrap(),
6985            );
6986            let mut interrupted = Report::default();
6987            copies::evict_with(
6988                &f.home,
6989                &Fx::disk(),
6990                Fx::now(),
6991                &mut interrupted,
6992                if stage == "missing-registration" || stage == "changed-admin" {
6993                    vanish
6994                } else {
6995                    interrupt
6996                },
6997            )
6998            .await;
6999            assert_eq!(interrupted.evicted_copies, 0, "{stage}: {interrupted:?}");
7000            let ledger = f
7001                .home
7002                .join("runs")
7003                .join(&state.id)
7004                .join("artifacts/eviction-review-1.json");
7005            let record: serde_json::Value =
7006                serde_json::from_slice(&std::fs::read(&ledger).unwrap_or_else(|error| {
7007                    panic!("{stage}: missing pending ledger: {error}; {interrupted:?}")
7008                }))
7009                .unwrap();
7010            assert_eq!(record["phase"], "pending");
7011            if stage == "changed-admin" {
7012                std::fs::write(admin.join("owner-note"), "keep").unwrap();
7013            }
7014            if stage == "missing-archive" {
7015                std::fs::remove_file(
7016                    Path::new(record["evidence"].as_str().unwrap()).join("objects.pack"),
7017                )
7018                .unwrap();
7019            }
7020            if stage == "wrong-identity" {
7021                let head = git::rev_parse(&state.candidates[0].worktree, "HEAD")
7022                    .await
7023                    .unwrap();
7024                let other =
7025                    crate::fold_evidence::snapshot(&state, &f.home, &head, 64 * 1024 * 1024)
7026                        .await
7027                        .unwrap();
7028                let original = Path::new(record["evidence"].as_str().unwrap());
7029                for name in [
7030                    "manifest.json",
7031                    "objects.pack",
7032                    "change.patch",
7033                    "change.index",
7034                ] {
7035                    std::fs::copy(other.join(name), original.join(name)).unwrap();
7036                }
7037            }
7038            let mut retried = Report::default();
7039            copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut retried).await;
7040            let phase =
7041                serde_json::from_slice::<serde_json::Value>(&std::fs::read(&ledger).unwrap())
7042                    .unwrap()["phase"]
7043                    .clone();
7044            if stage == "changed-admin" || stage == "missing-archive" || stage == "wrong-identity" {
7045                assert_eq!(phase, "pending", "{stage}: {retried:?}");
7046                assert!(!retried.retained.is_empty());
7047                if stage == "changed-admin" {
7048                    assert!(admin.join("owner-note").exists());
7049                } else {
7050                    assert!(review.exists());
7051                }
7052            } else {
7053                assert_eq!(phase, "evicted", "{stage}: {retried:?}");
7054                assert!(!review.exists());
7055                assert!(!admin.exists());
7056            }
7057        }
7058    }
7059    fn source_census(home: &Path) -> serde_json::Value {
7060        serde_json::from_slice(&std::fs::read(home.join("source-eviction-cursor")).unwrap())
7061            .unwrap()
7062    }
7063
7064    #[tokio::test]
7065    async fn source_eviction_scan_bounds_missing_root_checkpoints_and_eventually_reclaims() {
7066        let f = Fx::new().await;
7067        let state = inactive_source(&f, "ffff", true).await;
7068        let review = reviewer(&f, &state, &f.base).await;
7069        for i in 0..130 {
7070            let mut old = state.clone();
7071            old.id = format!("20260101-000000-{i:04x}");
7072            old.candidates.clear();
7073            old.save_under(&f.home).unwrap();
7074            // Missing roots with ledger records must still be inspected.
7075            if i == 0 {
7076                let artifacts = f.home.join("runs").join(&old.id).join("artifacts");
7077                std::fs::create_dir_all(&artifacts).unwrap();
7078                std::fs::write(artifacts.join("eviction-review-1.json"), "{}").unwrap();
7079            }
7080        }
7081        // Each invocation reloads the durable cursor. The corrupt ledger uses
7082        // one normal inspection slot; the remaining missing roots use a
7083        // separate 64-entry skip budget, bounding full-census checkpoint writes.
7084        // Repeat through wrap so idle sweeps cannot reintroduce an unbounded pass.
7085        for cycle in 1..=2 {
7086            for (pass, expected_next) in [65, 129, 131].into_iter().enumerate() {
7087                let mut report = Report::default();
7088                copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7089                assert!(report.failures.is_empty(), "{report:?}");
7090                assert_eq!(report.evicted_copies, usize::from(cycle == 1 && pass == 2));
7091                assert_eq!(review.exists(), cycle == 1 && pass < 2);
7092                let census = source_census(&f.home);
7093                assert_eq!(census["current"]["next"], expected_next);
7094                assert_eq!(census["current"]["completed_at"].is_null(), pass < 2);
7095                assert_eq!(census["current"]["number"], cycle);
7096                assert_eq!(
7097                    census["current"]["runs"]["20260101-000000-0000"]["root"]["outcome"],
7098                    "error"
7099                );
7100                assert_eq!(
7101                    census["current"]["runs"]["20260101-000000-0001"]["root"]["detail"],
7102                    "root absent and no eviction records; nothing to reclaim"
7103                );
7104                if pass < 2 {
7105                    let next_id = format!("20260101-000000-{expected_next:04x}");
7106                    assert_eq!(
7107                        census["current"]["runs"][next_id]["root"]["outcome"],
7108                        "not-yet-inspected"
7109                    );
7110                }
7111            }
7112        }
7113    }
7114
7115    #[tokio::test]
7116    async fn source_eviction_scan_reaches_old_seats_despite_empty_roots_and_timestamp_churn() {
7117        let f = Fx::new().await;
7118        let mut state = inactive_source(&f, "ffff", true).await;
7119        let review = reviewer(&f, &state, &f.base).await;
7120        // Empty checkout roots still need ownership checks and consume a
7121        // discovery slot; mutable timestamp ordering must not move the cursor.
7122        let mut history = Vec::new();
7123        for i in 0..130 {
7124            let mut old = state.clone();
7125            old.id = format!("20260101-000000-{i:04x}");
7126            old.candidates.clear();
7127            old.save_under(&f.home).unwrap();
7128            std::fs::create_dir_all(old.worktree_root()).unwrap();
7129            history.push(old);
7130        }
7131        state
7132            .save_under_at(
7133                &f.home,
7134                Timestamp::now() + jiff::SignedDuration::from_secs(10),
7135            )
7136            .unwrap();
7137        for pass in 0..3 {
7138            let mut report = Report::default();
7139            copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7140            assert!(report.failures.is_empty(), "{report:?}");
7141            let census = source_census(&f.home);
7142            assert_eq!(
7143                census["current"]["next"].as_u64().unwrap(),
7144                ((pass + 1) * 64).min(131)
7145            );
7146            if pass < 2 {
7147                assert!(review.exists());
7148                assert!(census["current"]["completed_at"].is_null());
7149                assert_eq!(
7150                    census["current"]["runs"][&state.id]["root"]["outcome"],
7151                    "not-yet-inspected"
7152                );
7153            }
7154            // Reorder the records on disk, including the cursor's former ID.
7155            // Loading a fresh scan on the next invocation models a restart.
7156            for old in history.iter_mut().take(64) {
7157                old.updated_at = Timestamp::now();
7158                old.save_under(&f.home).unwrap();
7159            }
7160            if pass == 1 {
7161                // Reproduce the old cursor's non-progress: moving its timestamp
7162                // before already visited records makes its next 64 slots revisit
7163                // those roots, excluding the eligible tail again. Repeating this
7164                // bookkeeping permutation can keep the tail out indefinitely.
7165                // Anchor the permutation to the saved tail, so a slow scan
7166                // cannot move these records past it as wall-clock time advances.
7167                let older = state.updated_at - jiff::SignedDuration::from_secs(100);
7168                for old in history.iter_mut().take(128) {
7169                    if f.home.join("runs").join(&old.id).exists() {
7170                        old.save_under_at(&f.home, older).unwrap();
7171                    }
7172                }
7173                history[127]
7174                    .save_under_at(&f.home, older - jiff::SignedDuration::from_secs(100))
7175                    .unwrap();
7176                let mut legacy: Vec<_> = history
7177                    .iter()
7178                    .filter(|r| f.home.join("runs").join(&r.id).exists())
7179                    .cloned()
7180                    .collect();
7181                legacy.push(state.clone());
7182                legacy.sort_by_key(|r| r.updated_at);
7183                let index = legacy.iter().position(|r| r.id == history[127].id).unwrap();
7184                let len = legacy.len();
7185                legacy.rotate_left((index + 1) % len);
7186                assert!(legacy.iter().take(64).all(|r| r.id != state.id));
7187            }
7188            if pass == 0 {
7189                std::fs::remove_dir_all(f.home.join("runs").join(&history[64].id)).unwrap();
7190                let mut added = state.clone();
7191                added.id = "20260101-000000-fffe".into();
7192                added.candidates.clear();
7193                added.save_under(&f.home).unwrap();
7194            }
7195        }
7196        assert!(!review.exists());
7197        let census = source_census(&f.home);
7198        assert!(!census["current"]["completed_at"].is_null());
7199        assert_eq!(
7200            census["current"]["runs"][&state.id]["seats"]["review-1"]["outcome"],
7201            "evicted"
7202        );
7203        assert_eq!(
7204            census["current"]["runs"][&history[64].id]["root"]["outcome"],
7205            "error"
7206        );
7207        let mut next = Report::default();
7208        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut next).await;
7209        let wrapped = source_census(&f.home);
7210        assert_eq!(wrapped["current"]["number"], 2);
7211        assert_eq!(wrapped["previous"], census["current"]);
7212        assert!(
7213            wrapped["current"]["runs"]
7214                .get("20260101-000000-fffe")
7215                .is_some()
7216        );
7217    }
7218
7219    #[tokio::test]
7220    async fn source_eviction_scan_resumes_within_run_and_bounds_failed_seats() {
7221        fn fail_one(path: &Path) -> Result<()> {
7222            ensure!(
7223                path.file_name().unwrap() != "review-2",
7224                "injected checkpoint failure"
7225            );
7226            Ok(())
7227        }
7228        let f = Fx::new().await;
7229        let state = inactive_source(&f, "ef20", true).await;
7230        let mut paths = Vec::new();
7231        for i in 1..=8 {
7232            let path = state.worktree_root().join(format!("review-{i}"));
7233            git::worktree_add_detached(&f.repo, &path, &f.base)
7234                .await
7235                .unwrap();
7236            paths.push(path);
7237        }
7238        std::fs::write(paths[2].join("owner-data"), "retain me").unwrap();
7239        let mut first = Report::default();
7240        copies::evict_with(&f.home, &Fx::disk(), Fx::now(), &mut first, fail_one).await;
7241        assert_eq!(first.evicted_copies, 3, "{first:?}");
7242        let census = source_census(&f.home);
7243        let seats = &census["current"]["runs"][&state.id]["seats"];
7244        assert_eq!(census["current"]["after_seat"], "review-5");
7245        assert_eq!(seats["review-2"]["outcome"], "error");
7246        assert_eq!(seats["review-2"]["stage"], "unlink");
7247        assert_eq!(seats["review-3"]["outcome"], "protected");
7248        assert_eq!(seats["review-6"]["outcome"], "not-yet-inspected");
7249        assert!(first.source_unlinked_logical_bytes > 0);
7250        assert!(first.source_evidence_added_logical_bytes > 0);
7251        let mut second = Report::default();
7252        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut second).await;
7253        assert_eq!(second.evicted_copies, 3, "{second:?}");
7254        assert!(paths[1].exists(), "failed seat waits until next cycle");
7255        assert!(paths[2].exists());
7256        assert!(!source_census(&f.home)["current"]["completed_at"].is_null());
7257        let mut third = Report::default();
7258        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut third).await;
7259        assert_eq!(third.evicted_copies, 1, "{third:?}");
7260        assert!(!paths[1].exists());
7261        assert_eq!(
7262            std::fs::read_to_string(paths[2].join("owner-data")).unwrap(),
7263            "retain me"
7264        );
7265        let shown = seat_inspections(&f.home, &state.id).unwrap().join("\n");
7266        assert!(
7267            shown.contains("cycle=1 complete=true review-2 error stage=unlink"),
7268            "{shown}"
7269        );
7270        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
7271        let destination = f.bay.join("restored-scan-seat");
7272        restore_seat(&f.home, &state.id, &f.base, &destination)
7273            .await
7274            .unwrap();
7275        assert_eq!(git::rev_parse(&destination, "HEAD").await.unwrap(), f.base);
7276    }
7277
7278    #[tokio::test]
7279    async fn source_eviction_scan_continues_after_archive_failure_and_busy_output() {
7280        let f = Fx::new().await;
7281        let failed = inactive_source(&f, "ef30", true).await;
7282        let failed_seat = reviewer(&f, &failed, &f.base).await;
7283        let evidence = f
7284            .home
7285            .join("runs")
7286            .join(&failed.id)
7287            .join("artifacts/source-evidence");
7288        std::fs::create_dir_all(&evidence).unwrap();
7289        std::fs::File::create(evidence.join("reserved-evidence"))
7290            .unwrap()
7291            .set_len(64 * 1024 * 1024)
7292            .unwrap();
7293        let state = inactive_source(&f, "ef31", true).await;
7294        let busy = reviewer(&f, &state, &f.base).await;
7295        let available = state.worktree_root().join("review-2");
7296        git::worktree_add_detached(&f.repo, &available, &f.base)
7297            .await
7298            .unwrap();
7299        let output = OutputLease::take(&busy, true).unwrap();
7300        let mut report = Report::default();
7301        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7302        assert_eq!(report.evicted_copies, 1, "{report:?}");
7303        assert!(failed_seat.exists());
7304        assert!(busy.exists());
7305        assert!(!available.exists());
7306        let census = source_census(&f.home);
7307        assert!(!census["current"]["completed_at"].is_null());
7308        assert_eq!(
7309            census["current"]["runs"][&failed.id]["seats"]["review-1"]["outcome"],
7310            "error"
7311        );
7312        assert_eq!(
7313            census["current"]["runs"][&state.id]["seats"]["review-1"]["outcome"],
7314            "protected"
7315        );
7316        drop(output);
7317        let mut retry = Report::default();
7318        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut retry).await;
7319        assert_eq!(retry.evicted_copies, 1, "{retry:?}");
7320        assert!(!busy.exists());
7321        assert!(failed_seat.exists());
7322    }
7323
7324    #[tokio::test]
7325    async fn source_eviction_scan_checkpoint_failure_stops_before_later_seats() {
7326        fn block_checkpoint(path: &Path) -> Result<()> {
7327            let home = path
7328                .parent()
7329                .unwrap()
7330                .parent()
7331                .unwrap()
7332                .parent()
7333                .unwrap()
7334                .join("home");
7335            let cursor = home.join("source-eviction-cursor");
7336            std::fs::rename(&cursor, home.join("saved-cursor"))?;
7337            std::fs::create_dir(&cursor)?;
7338            Ok(())
7339        }
7340        let f = Fx::new().await;
7341        let state = inactive_source(&f, "ef32", true).await;
7342        let first = reviewer(&f, &state, &f.base).await;
7343        let second = state.worktree_root().join("review-2");
7344        git::worktree_add_detached(&f.repo, &second, &f.base)
7345            .await
7346            .unwrap();
7347        let mut report = Report::default();
7348        copies::evict_with(
7349            &f.home,
7350            &Fx::disk(),
7351            Fx::now(),
7352            &mut report,
7353            block_checkpoint,
7354        )
7355        .await;
7356        assert_eq!(report.evicted_copies, 1, "{report:?}");
7357        assert!(!first.exists());
7358        assert!(second.exists());
7359        assert!(
7360            report
7361                .failures
7362                .iter()
7363                .any(|e| e.contains("persist source scan census"))
7364        );
7365        assert_eq!(report.source_scan.as_ref().unwrap()["outcome"], "error");
7366        std::fs::remove_dir(f.home.join("source-eviction-cursor")).unwrap();
7367        std::fs::rename(
7368            f.home.join("saved-cursor"),
7369            f.home.join("source-eviction-cursor"),
7370        )
7371        .unwrap();
7372        let mut retry = Report::default();
7373        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut retry).await;
7374        assert_eq!(retry.evicted_copies, 1, "{retry:?}");
7375        assert!(!second.exists());
7376    }
7377
7378    #[tokio::test]
7379    async fn source_eviction_scan_reports_invalid_cursor_and_global_ownership() {
7380        let f = Fx::new().await;
7381        let state = inactive_source(&f, "ef21", true).await;
7382        let review = reviewer(&f, &state, &f.base).await;
7383        let cursor = f.home.join("source-eviction-cursor");
7384        std::fs::write(&cursor, "broken cursor").unwrap();
7385        let mut report = Report::default();
7386        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7387        assert_eq!(std::fs::read_to_string(&cursor).unwrap(), "broken cursor");
7388        assert_eq!(report.source_scan.as_ref().unwrap()["outcome"], "error");
7389        assert!(review.exists());
7390        // Migrate a valid legacy cursor even if its target no longer exists.
7391        std::fs::write(&cursor, "20260101-000000-abcd").unwrap();
7392        let mut disabled = Fx::disk();
7393        disabled.auto_fold = false;
7394        let mut report = Report::default();
7395        copies::evict(&f.home, &disabled, Fx::now(), &mut report).await;
7396        assert_eq!(report.source_scan.as_ref().unwrap()["outcome"], "disabled");
7397        let record = f.home.join("runs").join(&state.id).join("run.json");
7398        let original = std::fs::read(&record).unwrap();
7399        std::fs::write(&record, "invalid ownership").unwrap();
7400        let mut report = Report::default();
7401        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7402        assert!(
7403            report
7404                .failures
7405                .iter()
7406                .any(|e| e.contains("global run ownership validation"))
7407        );
7408        assert!(review.exists());
7409        std::fs::write(record, original).unwrap();
7410        let mut report = Report::default();
7411        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7412        assert_eq!(report.evicted_copies, 1, "{report:?}");
7413        assert_eq!(source_census(&f.home)["version"], 1);
7414    }
7415
7416    #[tokio::test]
7417    async fn source_eviction_batches_reproducible_seats_without_dropping_task_history() {
7418        let f = Fx::new().await;
7419        let mut runs = Vec::new();
7420        let mut paths = Vec::new();
7421        for tag in ["ec10", "ec11", "ec12", "ec13"] {
7422            let state = inactive_source(&f, tag, true).await;
7423            for name in ["judge-1", "advisor-1"] {
7424                let path = state.worktree_root().join(name);
7425                git::worktree_add_detached(&f.repo, &path, &f.base)
7426                    .await
7427                    .unwrap();
7428                paths.push(path);
7429            }
7430            runs.push(state);
7431        }
7432        let task = f.task(&runs);
7433        let mut report = Report::default();
7434        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7435        assert_eq!(report.evicted_copies, 5, "{report:?}");
7436        let mut next = Report::default();
7437        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut next).await;
7438        assert_eq!(next.evicted_copies, 3, "{next:?}");
7439        assert!(paths.iter().all(|p| !p.exists()));
7440        assert!(runs.iter().all(|r| r.candidates[0].worktree.exists()));
7441        assert_eq!(
7442            crate::queue::Queue::at(f.home.join("queue"))
7443                .get(&task.id)
7444                .unwrap()
7445                .runs,
7446            task.runs
7447        );
7448        let limited = Fx::new().await;
7449        let state = inactive_source(&limited, "ec15", true).await;
7450        let copy = reviewer(&limited, &state, &limited.base).await;
7451        let evidence = limited
7452            .home
7453            .join("runs")
7454            .join(&state.id)
7455            .join("artifacts/source-evidence");
7456        std::fs::create_dir_all(&evidence).unwrap();
7457        let retained = evidence.join("reserved-evidence");
7458        std::fs::File::create(&retained)
7459            .unwrap()
7460            .set_len(64 * 1024 * 1024)
7461            .unwrap();
7462        let mut blocked = Report::default();
7463        copies::evict(&limited.home, &Fx::disk(), Fx::now(), &mut blocked).await;
7464        assert_eq!(blocked.evicted_copies, 0, "{blocked:?}");
7465        assert!(copy.exists());
7466        assert_eq!(
7467            std::fs::metadata(&retained).unwrap().len(),
7468            64 * 1024 * 1024
7469        );
7470        assert!(
7471            blocked
7472                .retained
7473                .iter()
7474                .any(|r| r.contains("per-run storage limit")),
7475            "{blocked:?}"
7476        );
7477    }
7478    #[tokio::test]
7479    async fn source_eviction_migrates_historical_records_without_rewriting_them() {
7480        let f = Fx::new().await;
7481        let mut state = inactive_source(&f, "ec14", true).await;
7482        historical_review(&mut state, &f.base);
7483        historical_review(&mut state, &f.base);
7484        state.status = RunStatus::Superseded;
7485        state.save_under(&f.home).unwrap();
7486        let review = reviewer(&f, &state, &f.base).await;
7487        let task = f.task(std::slice::from_ref(&state));
7488        let path = f.home.join("runs").join(&state.id).join("run.json");
7489        let mut raw: serde_json::Value =
7490            serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
7491        raw["schema"] = 10.into();
7492        raw.as_object_mut().unwrap().remove("output_activity_at");
7493        // Historical writers persisted null/empty defaults that current serde
7494        // omits. These are known fields, not unknown recovery authorities.
7495        raw["resource_wait"] = serde_json::Value::Null;
7496        raw["resource_block"] = serde_json::Value::Null;
7497        raw["driver_claimed"] = serde_json::Value::Null;
7498        raw["sources"] = serde_json::json!({"start_from":null,"pick_commits":[]});
7499        // Actual legacy spelling accepted by Graph's serde alias.
7500        let graph = raw["config"]["graph"].as_object_mut().unwrap();
7501        let count = graph.remove("implementers").unwrap();
7502        graph.insert("candidates".into(), count);
7503        let queue = crate::queue::Queue::at(f.home.join("queue"));
7504        let task_path = queue.path_of(&task.id);
7505        let mut task_raw: serde_json::Value =
7506            serde_json::from_slice(&std::fs::read(&task_path).unwrap()).unwrap();
7507        task_raw["sources"] = serde_json::json!({"start_from":null,"pick_commits":[]});
7508        let task_historical = serde_json::to_vec_pretty(&task_raw).unwrap();
7509        std::fs::write(&task_path, &task_historical).unwrap();
7510        let historical = serde_json::to_vec_pretty(&raw).unwrap();
7511        std::fs::write(&path, &historical).unwrap();
7512        let report = sweep(&f.home, &Fx::disk(), Fx::now()).await;
7513        assert_eq!(report.evicted_copies, 1, "{report:?}");
7514        assert!(!review.exists());
7515        assert_eq!(std::fs::read(&path).unwrap(), historical);
7516        assert_eq!(std::fs::read(&task_path).unwrap(), task_historical);
7517        assert!(state.candidates[0].worktree.exists());
7518        assert_eq!(
7519            crate::queue::Queue::at(f.home.join("queue"))
7520                .get(&task.id)
7521                .unwrap()
7522                .runs,
7523            task.runs
7524        );
7525    }
7526    #[tokio::test]
7527    async fn source_eviction_archives_old_only_reflog_oid_after_unrewritten_deletion() {
7528        let f = Fx::new().await;
7529        let state = inactive_source(&f, "ed01", true).await;
7530        let review = reviewer(&f, &state, &f.base).await;
7531        std::fs::write(review.join("old-only"), b"old-only\0binary\xff").unwrap();
7532        git::git(&review, &["add", "."]).await.unwrap();
7533        let tree = git::git(&review, &["write-tree"]).await.unwrap();
7534        let tip = git::git(
7535            &review,
7536            &[
7537                "commit-tree",
7538                &tree,
7539                "-p",
7540                &f.base,
7541                "-m",
7542                "temporary detached source",
7543            ],
7544        )
7545        .await
7546        .unwrap();
7547        git::git(&review, &["checkout", "--detach", &tip])
7548            .await
7549            .unwrap();
7550        git::git(&review, &["checkout", "--detach", &f.base])
7551            .await
7552            .unwrap();
7553        git::git(&review, &["reflog", "delete", "HEAD@{1}"])
7554            .await
7555            .unwrap();
7556        git::git(&review, &["update-ref", "-d", "ORIG_HEAD"])
7557            .await
7558            .unwrap();
7559        let shown = git::git(&review, &["reflog", "show", "--format=%H", "HEAD"])
7560            .await
7561            .unwrap();
7562        assert!(!shown.lines().any(|oid| oid == tip));
7563        let admin = PathBuf::from(
7564            git::git(&review, &["rev-parse", "--absolute-git-dir"])
7565                .await
7566                .unwrap(),
7567        );
7568        let raw = std::fs::read_to_string(admin.join("logs/HEAD")).unwrap();
7569        assert!(raw.lines().any(|line| line.starts_with(&format!("{tip} "))));
7570        fn interrupt(_: &Path) -> Result<()> {
7571            anyhow::bail!("fixture interruption before unlink")
7572        }
7573        let mut pending = Report::default();
7574        copies::evict_with(&f.home, &Fx::disk(), Fx::now(), &mut pending, interrupt).await;
7575        assert!(review.exists());
7576        let ledger = f
7577            .home
7578            .join("runs")
7579            .join(&state.id)
7580            .join("artifacts/eviction-review-1.json");
7581        let complete = std::fs::read(&ledger).unwrap();
7582        let mut incomplete: serde_json::Value = serde_json::from_slice(&complete).unwrap();
7583        incomplete["history"]
7584            .as_object_mut()
7585            .unwrap()
7586            .remove(&tip)
7587            .unwrap();
7588        std::fs::write(&ledger, serde_json::to_vec(&incomplete).unwrap()).unwrap();
7589        let mut blocked = Report::default();
7590        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut blocked).await;
7591        assert!(review.exists());
7592        assert!(
7593            blocked
7594                .retained
7595                .iter()
7596                .any(|reason| reason.contains("incomplete reflog checkpoint")),
7597            "{blocked:?}"
7598        );
7599        std::fs::write(&ledger, complete).unwrap();
7600        let mut report = Report::default();
7601        copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7602        assert_eq!(report.evicted_copies, 1, "{report:?}");
7603        git::git(&f.repo, &["reflog", "expire", "--expire=now", "--all"])
7604            .await
7605            .unwrap();
7606        git::git(&f.repo, &["gc", "--prune=now"]).await.unwrap();
7607        assert!(
7608            !git::git_raw(&f.repo, &["cat-file", "-e", &tip])
7609                .await
7610                .unwrap()
7611                .ok()
7612        );
7613        std::fs::remove_dir_all(&f.repo).unwrap();
7614        let destination = f._temp.path().join("restore-old-only");
7615        restore_seat(&f.home, &state.id, &tip, &destination)
7616            .await
7617            .unwrap();
7618        assert_eq!(
7619            std::fs::read(destination.join("old-only")).unwrap(),
7620            b"old-only\0binary\xff"
7621        );
7622    }
7623
7624    #[tokio::test]
7625    async fn source_eviction_crashed_driver_lease_does_not_block_cleanup_or_retry() {
7626        fn interrupt(_: &Path) -> Result<()> {
7627            anyhow::bail!("fixture interruption before unlink")
7628        }
7629        fn vanish(path: &Path) -> Result<()> {
7630            std::fs::remove_dir_all(path)?;
7631            anyhow::bail!("fixture interruption before unregistration")
7632        }
7633        for stage in ["ordinary", "intent", "missing-registration", "live-driver"] {
7634            let f = Fx::new().await;
7635            let mut state = inactive_source(&f, "ed02", true).await;
7636            state.driver_exited = false;
7637            state.driver_claimed = Some(true);
7638            state.driver_pid = Some(std::process::id());
7639            state.driver_started_at = crate::proc::process_started_at(std::process::id());
7640            state.begin_pass();
7641            state.status = RunStatus::Superseded;
7642            state.save_under(&f.home).unwrap();
7643            let review = reviewer(&f, &state, &f.base).await;
7644            // The same matching identity written by DriverGuard::record, left
7645            // behind when a process exits without its cleanup guard running.
7646            let identity = serde_json::json!({"pid":state.driver_pid,"started_at":state.driver_started_at,
7647                "pass":state.passes.last().unwrap().started_at,"pass_index":state.passes.last().unwrap().index});
7648            std::fs::write(
7649                f.home.join("runs").join(&state.id).join("driver.json"),
7650                serde_json::to_vec(&identity).unwrap(),
7651            )
7652            .unwrap();
7653            let live = if stage == "live-driver" {
7654                Some(crate::driver::maintenance(&f.home, &state.id).unwrap())
7655            } else {
7656                None
7657            };
7658            assert_eq!(
7659                state.liveness_under(&f.home, false),
7660                if live.is_some() {
7661                    Liveness::Live
7662                } else {
7663                    Liveness::Dead
7664                }
7665            );
7666            let mut report = Report::default();
7667            if stage == "intent" || stage == "missing-registration" {
7668                copies::evict_with(
7669                    &f.home,
7670                    &Fx::disk(),
7671                    Fx::now(),
7672                    &mut report,
7673                    if stage == "intent" { interrupt } else { vanish },
7674                )
7675                .await;
7676                assert_eq!(report.evicted_copies, 0, "{stage}: {report:?}");
7677                let mut retry = Report::default();
7678                copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut retry).await;
7679                assert!(!review.exists(), "{stage}: {retry:?}");
7680                let ledger = f
7681                    .home
7682                    .join("runs")
7683                    .join(&state.id)
7684                    .join("artifacts/eviction-review-1.json");
7685                let receipt: serde_json::Value =
7686                    serde_json::from_slice(&std::fs::read(ledger).unwrap()).unwrap();
7687                assert_eq!(receipt["phase"], "evicted", "{stage}: {retry:?}");
7688            } else {
7689                copies::evict(&f.home, &Fx::disk(), Fx::now(), &mut report).await;
7690                assert_eq!(
7691                    report.evicted_copies,
7692                    usize::from(live.is_none()),
7693                    "{stage}: {report:?}"
7694                );
7695                assert_eq!(review.exists(), live.is_some());
7696            }
7697            drop(live);
7698            assert!(
7699                !RunState::load_under(&state.id, &f.home)
7700                    .unwrap()
7701                    .driver_exited
7702            );
7703        }
7704    }
7705}