Skip to main content

magi/
incremental.rs

1//! Disposable incremental generations with creation-time, per-file provenance.
2//!
3//! Legacy incremental directories are never adopted. A generation is fresh for
4//! one compiler invocation; incomplete or changed generations consume their slot
5//! until an operator investigates. This is a bound, not authority to wipe them.
6use anyhow::{Context, Result, ensure};
7use serde::{Deserialize, Serialize};
8use sha2::{Digest, Sha256};
9use std::collections::BTreeMap;
10use std::ffi::OsString;
11use std::fs::{self, File, OpenOptions};
12use std::io::Read;
13use std::path::{Path, PathBuf};
14
15/// Stable resource-admission marker emitted by the private compiler wrapper.
16pub const BLOCKED: &str = "incremental cache admission blocked";
17
18const SLOTS: usize = 4;
19const GENERATION_BYTES: u64 = 128 * 1024 * 1024;
20const STORE: &str = "magi-incremental";
21
22/// Forked subprocesses can retain a copy until exec closes it. Explicitly
23/// release our flock at the ownership boundary rather than waiting for every
24/// inherited descriptor to close. Session acquisition queries POSIX locks and
25/// then holds flock, so all these guards use the same release protocol.
26#[derive(Debug)]
27struct FileLease(File);
28
29impl std::ops::Deref for FileLease {
30    type Target = File;
31    fn deref(&self) -> &Self::Target {
32        &self.0
33    }
34}
35
36#[cfg(unix)]
37impl Drop for FileLease {
38    fn drop(&mut self) {
39        use std::os::fd::AsRawFd;
40        // SAFETY: the guard retains its live descriptor through unlock.
41        unsafe {
42            libc::flock(self.0.as_raw_fd(), libc::LOCK_UN);
43        }
44    }
45}
46
47pub(crate) fn default_environment(command: &mut tokio::process::Command) {
48    let explicit = command
49        .as_std()
50        .get_envs()
51        .find(|(key, _)| *key == "CARGO_INCREMENTAL");
52    let supplied = match explicit {
53        Some((_, value)) => value.is_some(),
54        None => std::env::var_os("CARGO_INCREMENTAL").is_some(),
55    };
56    if !supplied {
57        command.env("CARGO_INCREMENTAL", "0");
58    }
59}
60
61/// Configure a managed command, preserving explicit environment overrides and
62/// Cargo's workspace wrapper chain. The wrapper sees shell-level opt-ins too.
63pub(crate) fn configure(command: &mut tokio::process::Command, worktree: &Path) -> Result<()> {
64    default_environment(command);
65    #[cfg(not(unix))]
66    let _ = worktree;
67    #[cfg(unix)]
68    let env: BTreeMap<_, _> = command
69        .as_std()
70        .get_envs()
71        .map(|(k, v)| (k.to_owned(), v.map(ToOwned::to_owned)))
72        .collect();
73    #[cfg(unix)]
74    let value = |key: &str| {
75        env.get(std::ffi::OsStr::new(key))
76            .cloned()
77            .unwrap_or_else(|| std::env::var_os(key))
78    };
79    #[cfg(unix)]
80    if worktree.join(".git").is_file() {
81        let git = git_dir(worktree)?;
82        let (name, body) = wrapper_spec()?;
83        let script = git.join(name);
84        publish_wrapper(&script, &body, || {
85            use std::sync::atomic::{AtomicU64, Ordering};
86            static NEXT: AtomicU64 = AtomicU64::new(0);
87            git.join(format!(
88                "magi-rustc-{}-{}-{}.tmp",
89                std::process::id(),
90                NEXT.fetch_add(1, Ordering::Relaxed),
91                crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4()
92            ))
93        })?;
94        let previous = value("RUSTC_WRAPPER");
95        let previous = if previous.is_some() && previous == value("MAGI_INCREMENTAL_WRAPPER") {
96            value("MAGI_PREVIOUS_RUSTC_WRAPPER")
97        } else {
98            previous
99        };
100        command.env_remove("MAGI_PREVIOUS_RUSTC_WRAPPER");
101        if let Some(previous) = previous.filter(|v| !v.is_empty()) {
102            // Re-entering a managed command must not wrap our own wrapper.
103            if previous != script.as_os_str() {
104                command.env("MAGI_PREVIOUS_RUSTC_WRAPPER", previous);
105            }
106        }
107        command
108            .env("MAGI_INCREMENTAL_WORKTREE", worktree.canonicalize()?)
109            .env("MAGI_INCREMENTAL_WRAPPER", &script)
110            .env("RUSTC_WRAPPER", script);
111    }
112    Ok(())
113}
114
115/// Only a successfully reserved path belongs to this invocation. In particular,
116/// a failed create_new must never remove another publisher's temporary file.
117#[cfg(unix)]
118struct WrapperTemporary {
119    path: PathBuf,
120    file: File,
121}
122
123#[cfg(unix)]
124impl WrapperTemporary {
125    fn create(mut candidate: impl FnMut() -> PathBuf) -> std::io::Result<Self> {
126        for _ in 0..128 {
127            let path = candidate();
128            match OpenOptions::new().write(true).create_new(true).open(&path) {
129                Ok(file) => return Ok(Self { path, file }),
130                Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
131                Err(error) => return Err(error),
132            }
133        }
134        Err(std::io::Error::new(
135            std::io::ErrorKind::AlreadyExists,
136            "could not reserve an incremental wrapper temporary file",
137        ))
138    }
139}
140
141#[cfg(unix)]
142impl Drop for WrapperTemporary {
143    fn drop(&mut self) {
144        let _ = fs::remove_file(&self.path);
145    }
146}
147
148#[cfg(unix)]
149fn wrapper_spec() -> Result<(String, String)> {
150    let executable = std::env::current_exe()?;
151    let hash: String = Sha256::digest(executable.as_os_str().as_encoded_bytes())
152        .iter()
153        .map(|b| format!("{b:02x}"))
154        .collect();
155    let name = format!("magi-rustc-{hash}.sh");
156    let quote = |p: &Path| format!("'{}'", p.to_string_lossy().replace('\'', "'\\''"));
157    let body = format!(
158        "#!/bin/sh\nexec {} __magi_incremental \"$@\"\n",
159        quote(&executable)
160    );
161    Ok((name, body))
162}
163
164/// Execution may encounter the exact compiler wrapper its own setup created.
165/// This is not an exemption for arbitrary administration during eviction.
166pub(crate) fn current_wrapper(path: &Path) -> Result<bool> {
167    #[cfg(unix)]
168    {
169        let (name, body) = wrapper_spec()?;
170        if path.file_name() != Some(std::ffi::OsStr::new(&name)) {
171            return Ok(false);
172        }
173        Ok(plain(path)?.is_file() && fs::read(path)? == body.as_bytes())
174    }
175    #[cfg(not(unix))]
176    {
177        let _ = path;
178        Ok(false)
179    }
180}
181
182#[cfg(unix)]
183fn publish_wrapper(script: &Path, body: &str, candidate: impl FnMut() -> PathBuf) -> Result<()> {
184    use std::io::Write;
185    use std::os::unix::fs::PermissionsExt;
186    let mut temporary = WrapperTemporary::create(candidate)?;
187    temporary.file.write_all(body.as_bytes())?;
188    temporary
189        .file
190        .set_permissions(fs::Permissions::from_mode(0o700))?;
191    // Link only a complete script, without replacing an existing path.
192    // Concurrent command preparation must never observe a partial body.
193    match fs::hard_link(&temporary.path, script) {
194        Ok(()) => Ok(()),
195        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
196            ensure!(
197                plain(script)?.is_file() && fs::read(script)? == body.as_bytes(),
198                "incremental wrapper changed"
199            );
200            Ok(())
201        }
202        Err(error) => Err(error.into()),
203    }
204}
205
206fn git_dir(worktree: &Path) -> Result<PathBuf> {
207    let output = std::process::Command::new("git")
208        .current_dir(worktree)
209        .args(["rev-parse", "--absolute-git-dir"])
210        .output()?;
211    ensure!(
212        output.status.success(),
213        "incremental provenance requires a Git worktree"
214    );
215    Ok(PathBuf::from(String::from_utf8(output.stdout)?.trim()))
216}
217
218fn plain(path: &Path) -> Result<fs::Metadata> {
219    let metadata = fs::symlink_metadata(path)?;
220    ensure!(
221        !metadata.file_type().is_symlink() && path.canonicalize()? == path,
222        "incremental path is not canonical: {}",
223        path.display()
224    );
225    Ok(metadata)
226}
227
228#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
229#[serde(deny_unknown_fields)]
230struct Identity {
231    bytes: u64,
232    digest: Vec<u8>,
233    #[cfg(unix)]
234    device: u64,
235    #[cfg(unix)]
236    inode: u64,
237}
238
239fn identity(path: &Path) -> Result<Identity> {
240    let metadata = plain(path)?;
241    ensure!(
242        metadata.is_file(),
243        "incremental entry is not a regular file"
244    );
245    let mut file = File::open(path)?;
246    let mut hash = Sha256::new();
247    let mut buffer = [0; 65536];
248    loop {
249        let n = file.read(&mut buffer)?;
250        if n == 0 {
251            break;
252        }
253        hash.update(&buffer[..n]);
254    }
255    #[cfg(unix)]
256    use std::os::unix::fs::MetadataExt;
257    Ok(Identity {
258        bytes: metadata.len(),
259        digest: hash.finalize().to_vec(),
260        #[cfg(unix)]
261        device: metadata.dev(),
262        #[cfg(unix)]
263        inode: metadata.ino(),
264    })
265}
266
267#[derive(Serialize, Deserialize)]
268#[serde(deny_unknown_fields)]
269struct Proof {
270    version: u32,
271    worktree: PathBuf,
272    generation: PathBuf,
273    files: BTreeMap<PathBuf, Identity>,
274    directories: std::collections::BTreeSet<PathBuf>,
275}
276
277pub(crate) struct Generation {
278    path: PathBuf,
279    record: PathBuf,
280    // The session lock is outside the cache, and lives through compiler exit.
281    _lock: FileLease,
282}
283
284impl Generation {
285    pub(crate) fn create(worktree: &Path, parent: &Path) -> Result<Self> {
286        let target = worktree.join("target");
287        ensure!(
288            [
289                target.join("debug/incremental"),
290                target.join("release/incremental")
291            ]
292            .contains(&parent.to_owned()),
293            "explicit incremental requires a local managed target; shared/custom targets remain non-incremental"
294        );
295        // Cargo creates these parents before invoking rustc. Never follow an alias.
296        ensure!(
297            plain(&target)?.is_dir() && plain(parent)?.is_dir(),
298            "incremental parent is not a directory"
299        );
300        let store = git_dir(worktree)?.join(STORE);
301        fs::create_dir_all(&store)?;
302        ensure!(
303            plain(&store)?.is_dir(),
304            "invalid incremental provenance store"
305        );
306        // Hash immutable completed generations before taking the namespace
307        // lock. Shared owner guards keep these snapshots stable while other
308        // compiler admissions independently inspect the same completions.
309        let mut completed = BTreeMap::new();
310        let mut inspected = 0usize;
311        for entry in fs::read_dir(&store)? {
312            let owner = entry?.path();
313            if owner.extension().is_some_and(|x| x == "owner") {
314                inspected += 1;
315                if inspected > 1024 {
316                    break;
317                }
318                if let Ok(snapshot) = completed_charge(worktree, &owner) {
319                    completed.insert(owner, snapshot);
320                }
321            }
322        }
323        let mut options = OpenOptions::new();
324        #[cfg(unix)]
325        {
326            use std::os::unix::fs::OpenOptionsExt;
327            options.custom_flags(libc::O_NOFOLLOW);
328        }
329        let admission = FileLease(
330            options
331                .create(true)
332                .truncate(false)
333                .read(true)
334                .write(true)
335                .open(store.join("admission.lock"))?,
336        );
337        wait_for_admission(&admission, std::time::Duration::from_secs(30))?;
338        // Reset/clean can remove sealed cache data without its Git-side proof.
339        // Forget only completed, absent generations, never unsealed attempts.
340        for entry in fs::read_dir(&store)? {
341            let record = entry?.path();
342            if record.extension().is_none_or(|x| x != "json") {
343                continue;
344            }
345            let cleanup = || -> Result<()> {
346                ensure!(
347                    plain(&record)?.is_file() && fs::metadata(&record)?.len() < 4 * 1024 * 1024,
348                    "invalid incremental proof"
349                );
350                let proof: Proof = serde_json::from_slice(&fs::read(&record)?)?;
351                validate_proof(worktree, &record, &proof)?;
352                ensure!(
353                    matches!(fs::symlink_metadata(&proof.generation), Err(e) if e.kind() == std::io::ErrorKind::NotFound),
354                    "generation still exists or is uninspectable"
355                );
356                let owner = record.with_extension("owner");
357                ensure!(
358                    plain(&owner)?.is_file() && fs::metadata(&owner)?.len() == 0,
359                    "invalid incremental owner"
360                );
361                let lock = FileLease(OpenOptions::new().read(true).write(true).open(&owner)?);
362                crate::pr_head::try_lock_file(&lock)?;
363                fs::remove_file(&record)?;
364                fs::remove_file(owner)?;
365                Ok(())
366            };
367            let _ = cleanup(); // Refusal retains both proof and its reservation.
368        }
369        // Live and unproven attempts reserve a full generation. Completed,
370        // unchanged generations consume their actual bytes, not a lifetime slot.
371        let mut charged = 0u64;
372        let mut records = 0usize;
373        for entry in fs::read_dir(&store)? {
374            let owner = entry?.path();
375            if owner.extension().is_some_and(|x| x == "owner") {
376                records += 1;
377                let bytes = completed
378                    .get(&owner)
379                    .and_then(|(bytes, guard)| snapshot_charge(&owner, *bytes, guard))
380                    .unwrap_or(GENERATION_BYTES);
381                charged = charged.saturating_add(bytes);
382            }
383        }
384        ensure!(
385            records < 1024,
386            "managed incremental provenance record limit exhausted; inspect retention report"
387        );
388        ensure!(
389            charged.saturating_add(GENERATION_BYTES) <= SLOTS as u64 * GENERATION_BYTES,
390            "managed incremental budget exhausted (512 MiB including active/unproven reservations); retain unproven caches and inspect retention report"
391        );
392        let token = crate::rng::SplitMix64::new(crate::rng::entropy()).uuid_v4();
393        let record = store.join(format!("{token}.json"));
394        let lock = FileLease(
395            OpenOptions::new()
396                .create_new(true)
397                .read(true)
398                .write(true)
399                .open(record.with_extension("owner"))?,
400        );
401        crate::pr_head::try_lock_file(&lock)?;
402        let path = parent.join(format!("magi-{token}"));
403        fs::create_dir(&path)?;
404        Ok(Self {
405            path,
406            record,
407            _lock: lock,
408        })
409    }
410
411    pub(crate) fn seal(&self, worktree: &Path) -> Result<()> {
412        finalized_sessions(&self.path)?;
413        let files = inventory(&self.path, true)?;
414        let bytes = files
415            .values()
416            .fold(0u64, |sum, f| sum.saturating_add(f.bytes));
417        ensure!(
418            bytes <= GENERATION_BYTES,
419            "managed incremental generation exceeds its 128 MiB budget"
420        );
421        let proof = Proof {
422            version: 1,
423            worktree: worktree.to_owned(),
424            generation: self.path.clone(),
425            files,
426            directories: directory_inventory(&self.path)?,
427        };
428        let temp = self.record.with_extension("tmp");
429        fs::write(&temp, serde_json::to_vec(&proof)?)?;
430        fs::rename(temp, &self.record)?;
431        Ok(())
432    }
433}
434
435// A successful compiler may leave an abandoned working session. Creation-time
436// ownership alone does not make arbitrary directories or lock names disposable.
437fn finalized_sessions(root: &Path) -> Result<()> {
438    for entry in fs::read_dir(root)? {
439        let crate_dir = entry?.path();
440        ensure!(
441            plain(&crate_dir)?.is_dir(),
442            "unknown incremental root entry"
443        );
444        let mut sessions = Vec::new();
445        let mut locks = Vec::new();
446        for entry in fs::read_dir(&crate_dir)? {
447            let path = entry?.path();
448            let name = path
449                .file_name()
450                .context("missing incremental name")?
451                .to_str()
452                .context("invalid incremental name")?
453                .to_owned();
454            if plain(&path)?.is_dir() {
455                ensure!(
456                    name.starts_with("s-")
457                        && !name.ends_with("-working")
458                        && name.split('-').count() == 4,
459                    "incremental session is unfinished or unknown"
460                );
461                for output in fs::read_dir(&path)? {
462                    ensure!(
463                        plain(&output?.path())?.is_file(),
464                        "unknown incremental session directory"
465                    );
466                }
467                sessions.push(name);
468            } else {
469                ensure!(
470                    name.starts_with("s-")
471                        && name.ends_with(".lock")
472                        && name.split('-').count() == 3,
473                    "unknown incremental session lock"
474                );
475                locks.push(name);
476            }
477        }
478        ensure!(
479            !sessions.is_empty() && sessions.len() == locks.len(),
480            "incremental session/lock inventory is incomplete"
481        );
482        for lock in locks {
483            let prefix = format!("{}-", lock.strip_suffix(".lock").unwrap());
484            ensure!(
485                sessions.iter().any(|name| name.starts_with(&prefix)),
486                "incremental session lock has no completed session"
487            );
488        }
489    }
490    Ok(())
491}
492
493// Contention is a short serialization wait, distinct from insufficient budget.
494fn wait_for_admission(file: &File, timeout: std::time::Duration) -> Result<()> {
495    let started = std::time::Instant::now();
496    loop {
497        match crate::pr_head::try_lock_file(file) {
498            Ok(()) => return Ok(()),
499            Err(error)
500                if matches!(
501                    error.kind(),
502                    std::io::ErrorKind::WouldBlock | std::io::ErrorKind::Interrupted
503                ) => {}
504            Err(error) => return Err(error).context("incremental generation admission lock"),
505        }
506        ensure!(
507            started.elapsed() < timeout,
508            "incremental generation admission wait timed out after {} ms",
509            timeout.as_millis()
510        );
511        std::thread::sleep(
512            std::time::Duration::from_millis(10).min(timeout.saturating_sub(started.elapsed())),
513        );
514    }
515}
516
517fn snapshot_charge(owner: &Path, bytes: u64, guard: &File) -> Option<u64> {
518    let current = plain(owner).ok()?;
519    let held = guard.metadata().ok()?;
520    #[cfg(unix)]
521    {
522        use std::os::unix::fs::MetadataExt;
523        (current.is_file()
524            && current.len() == 0
525            && current.dev() == held.dev()
526            && current.ino() == held.ino())
527        .then_some(bytes)
528    }
529    #[cfg(not(unix))]
530    {
531        let _ = (current, held, bytes);
532        None
533    }
534}
535
536fn lock_for_accounting(file: &File) -> std::io::Result<()> {
537    #[cfg(unix)]
538    {
539        use std::os::fd::AsRawFd;
540        // SAFETY: this live descriptor is retained with the completed snapshot.
541        if unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) } == 0 {
542            Ok(())
543        } else {
544            Err(std::io::Error::last_os_error())
545        }
546    }
547    #[cfg(not(unix))]
548    {
549        crate::pr_head::try_lock_file(file)
550    }
551}
552
553// Accounting grants no deletion authority. A shared owner guard and an exact
554// completion proof may reduce the conservative full-generation reservation.
555fn completed_charge(worktree: &Path, owner: &Path) -> Result<(u64, FileLease)> {
556    ensure!(
557        plain(owner)?.is_file() && fs::metadata(owner)?.len() == 0,
558        "invalid incremental owner"
559    );
560    let record = owner.with_extension("json");
561    // An unpublished owner may still be acquiring its initial exclusive lock.
562    // Do not briefly share-lock it before a completion proof even exists.
563    ensure!(
564        plain(&record)?.is_file() && fs::metadata(&record)?.len() < 4 * 1024 * 1024,
565        "invalid incremental proof"
566    );
567    let lock = FileLease(OpenOptions::new().read(true).write(true).open(owner)?);
568    lock_for_accounting(&lock)?;
569    let proof: Proof = serde_json::from_slice(&fs::read(record)?)?;
570    validate_proof(worktree, &owner.with_extension("json"), &proof)?;
571    let actual = inventory(&proof.generation, false)?;
572    ensure!(
573        directory_inventory(&proof.generation)?.is_subset(&proof.directories)
574            && actual
575                .iter()
576                .all(|(p, identity)| proof.files.get(p) == Some(identity)),
577        "incremental proof changed"
578    );
579    let bytes = actual
580        .values()
581        .fold(0u64, |bytes, identity| bytes.saturating_add(identity.bytes));
582    Ok((bytes, lock))
583}
584
585fn inventory(root: &Path, recognize: bool) -> Result<BTreeMap<PathBuf, Identity>> {
586    let mut files = BTreeMap::new();
587    let mut bytes = 0u64;
588    let mut dirs = vec![root.to_owned()];
589    while let Some(dir) = dirs.pop() {
590        ensure!(plain(&dir)?.is_dir(), "invalid incremental directory");
591        for entry in fs::read_dir(&dir)? {
592            let path = entry?.path();
593            let metadata = plain(&path)?;
594            if metadata.is_dir() {
595                dirs.push(path);
596            } else {
597                ensure!(
598                    metadata.is_file(),
599                    "incremental entry is not a regular file"
600                );
601                bytes = bytes.saturating_add(metadata.len());
602                ensure!(
603                    bytes <= GENERATION_BYTES,
604                    "incremental generation exceeds its budget"
605                );
606                ensure!(files.len() < 10000, "incremental inventory too large");
607                if recognize {
608                    ensure!(
609                        recognized(&path)?,
610                        "unknown incremental output: {}",
611                        path.display()
612                    );
613                }
614                files.insert(path.strip_prefix(root)?.to_owned(), identity(&path)?);
615            }
616        }
617    }
618    Ok(files)
619}
620
621fn directory_inventory(root: &Path) -> Result<std::collections::BTreeSet<PathBuf>> {
622    let mut found = std::collections::BTreeSet::new();
623    let mut dirs = vec![root.to_owned()];
624    while let Some(dir) = dirs.pop() {
625        ensure!(plain(&dir)?.is_dir(), "invalid incremental directory");
626        for entry in fs::read_dir(dir)? {
627            let path = entry?.path();
628            if plain(&path)?.is_dir() {
629                ensure!(
630                    found.len() < 10000,
631                    "incremental directory inventory too large"
632                );
633                found.insert(path.strip_prefix(root)?.to_owned());
634                dirs.push(path);
635            }
636        }
637    }
638    Ok(found)
639}
640
641fn recognized(path: &Path) -> Result<bool> {
642    let name = path.file_name().unwrap_or_default().to_string_lossy();
643    if name.ends_with(".lock") {
644        return Ok(fs::metadata(path)?.len() == 0);
645    }
646    let mut header = [0; 8];
647    let n = File::open(path)?.read(&mut header)?;
648    if matches!(
649        name.as_ref(),
650        "dep-graph.bin" | "query-cache.bin" | "work-products.bin"
651    ) {
652        return Ok(header[..n].starts_with(b"RSIC"));
653    }
654    if path.extension().is_some_and(|e| e == "o") || name == "metadata.rmeta" {
655        return crate::retention::artifact_signature(path);
656    }
657    Ok(false)
658}
659
660/// Check both Unix lock protocols used by compilers. Query the POSIX lock
661/// without acquiring it: holding both protocols can conflict on BSD. Retain
662/// the flock guard through unlink, alongside the caller's output/Cargo locks.
663/// See https://doc.rust-lang.org/stable/nightly-rustc/rustc_data_structures/flock/.
664#[cfg(unix)]
665fn lock_session(file: &File) -> std::io::Result<()> {
666    use std::os::fd::AsRawFd;
667    // SAFETY: the descriptor is live and the initialized record covers the
668    // whole file. F_GETLK only inspects locks; it does not change them.
669    let mut lock: libc::flock = unsafe { std::mem::zeroed() };
670    lock.l_type = libc::F_WRLCK as _;
671    lock.l_whence = libc::SEEK_SET as _;
672    lock.l_start = 0;
673    lock.l_len = 0;
674    if unsafe { libc::fcntl(file.as_raw_fd(), libc::F_GETLK, &mut lock) } == -1 {
675        return Err(std::io::Error::last_os_error());
676    }
677    let unlocked: libc::c_short = libc::F_UNLCK as _;
678    if lock.l_type != unlocked {
679        return Err(std::io::Error::new(
680            std::io::ErrorKind::WouldBlock,
681            "incremental session has a POSIX record lock",
682        ));
683    }
684    crate::pr_head::try_lock_file(file)
685}
686
687#[cfg(all(unix, test))]
688fn lock_posix_session(file: &File) -> std::io::Result<()> {
689    use std::os::fd::AsRawFd;
690    // SAFETY: zero initializes reserved fields; fcntl borrows the lock record
691    // only during this call and the test helper retains the live descriptor.
692    let mut lock: libc::flock = unsafe { std::mem::zeroed() };
693    lock.l_type = libc::F_WRLCK as _;
694    lock.l_whence = libc::SEEK_SET as _;
695    lock.l_start = 0;
696    lock.l_len = 0;
697    if unsafe { libc::fcntl(file.as_raw_fd(), libc::F_SETLK, &lock) } == -1 {
698        return Err(std::io::Error::last_os_error());
699    }
700    Ok(())
701}
702
703#[cfg(not(unix))]
704fn lock_session(file: &File) -> std::io::Result<()> {
705    crate::pr_head::try_lock_file(file)
706}
707
708/// Handle the private wrapper entrypoint before normal CLI startup. Existing
709/// wrappers (including Cargo's workspace wrapper argument) stay in the chain.
710pub fn wrapper(args: Vec<OsString>) -> Result<i32> {
711    tokio::runtime::Builder::new_current_thread()
712        .enable_all()
713        .build()?
714        .block_on(wrapper_inner(args))
715}
716
717async fn wrapper_inner(args: Vec<OsString>) -> Result<i32> {
718    let worktree = PathBuf::from(
719        std::env::var_os("MAGI_INCREMENTAL_WORKTREE").context("missing incremental worktree")?,
720    );
721    let mut args = args;
722    let mut generation = None;
723    for index in 0..args.len() {
724        let text = args[index].to_string_lossy();
725        let value = if index > 0 && args[index - 1] == "-C" {
726            text.strip_prefix("incremental=").map(str::to_owned)
727        } else {
728            text.strip_prefix("-Cincremental=").map(str::to_owned)
729        };
730        if let Some(value) = value {
731            ensure!(
732                generation.is_none(),
733                "multiple incremental compiler paths are unsupported"
734            );
735            let parent = PathBuf::from(value);
736            let parent = if parent.is_absolute() {
737                parent
738            } else {
739                std::env::current_dir()?.join(parent)
740            };
741            // Cargo passes the profile incremental root; rustc creates its own
742            // crate/session subdirectories beneath our fresh generation.
743            let made = Generation::create(&worktree, &parent)?;
744            let prefix = if text.starts_with("-C") {
745                "-Cincremental="
746            } else {
747                "incremental="
748            };
749            args[index] = format!("{prefix}{}", made.path.display()).into();
750            generation = Some(made);
751        }
752    }
753    let command = if let Some(previous) =
754        std::env::var_os("MAGI_PREVIOUS_RUSTC_WRAPPER").filter(|s| !s.is_empty())
755    {
756        let mut command = std::process::Command::new(previous);
757        command.args(&args);
758        command
759    } else {
760        let mut command = std::process::Command::new(args.first().context("missing compiler")?);
761        command.args(&args[1..]);
762        command
763    };
764    let mut running =
765        crate::seat_process::Running::spawn_operation(command.into(), None, Vec::new())?;
766    let result: Result<_> = async {
767        loop {
768            if let Some(status) = running.child().try_wait()? {
769                break Ok(status);
770            }
771            if let Some(generation) = &generation
772                && crate::disk::dir_size(&generation.path) > GENERATION_BYTES
773            {
774                anyhow::bail!(
775                    "managed incremental compiler exceeded 128 MiB; retained unproven output"
776                );
777            }
778            tokio::time::sleep(std::time::Duration::from_millis(200)).await;
779        }
780    }
781    .await;
782    // Keep the generation owner through confirmation of the entire invocation's
783    // descendants, including a non-exec wrapper's compiler and escaped tools.
784    running.finish().await;
785    let status = result?;
786    if status.success()
787        && let Some(generation) = generation
788    {
789        generation.seal(&worktree)?;
790    }
791    Ok(status.code().unwrap_or(1))
792}
793
794/// Validate independently of dep-info, with the caller holding owner, driver,
795/// output and Cargo locks. Keep all generation/session locks through unlink.
796#[derive(Default)]
797pub(crate) struct Reclaim {
798    pub(crate) files: Vec<PathBuf>,
799    _locks: Vec<FileLease>,
800    finished: Vec<(PathBuf, PathBuf, std::collections::BTreeSet<PathBuf>)>,
801}
802
803impl Reclaim {
804    pub(crate) fn finish(&self) -> Result<()> {
805        for (record, generation, allowed_dirs) in &self.finished {
806            if generation.exists() {
807                ensure!(
808                    inventory(generation, false)?.is_empty(),
809                    "incremental generation still contains data"
810                );
811                ensure!(
812                    directory_inventory(generation)?.is_subset(allowed_dirs),
813                    "unknown incremental directory appeared before removal"
814                );
815                let mut dirs = vec![generation.clone()];
816                let mut index = 0;
817                while index < dirs.len() {
818                    for entry in fs::read_dir(&dirs[index])? {
819                        let path = entry?.path();
820                        ensure!(plain(&path)?.is_dir(), "incremental generation changed");
821                        dirs.push(path);
822                    }
823                    index += 1;
824                }
825                for dir in dirs.into_iter().rev() {
826                    fs::remove_dir(dir)?;
827                }
828            }
829            let owner = record.with_extension("owner");
830            ensure!(
831                plain(&owner)?.len() == 0,
832                "incremental owner changed before removal"
833            );
834            fs::remove_file(record)?;
835            fs::remove_file(owner)?;
836        }
837        Ok(())
838    }
839}
840
841fn validate_proof(worktree: &Path, record: &Path, proof: &Proof) -> Result<()> {
842    ensure!(
843        proof.version == 1 && proof.worktree == worktree,
844        "incremental ownership changed"
845    );
846    let parent = proof
847        .generation
848        .parent()
849        .context("missing generation parent")?;
850    ensure!(
851        [
852            worktree.join("target/debug/incremental"),
853            worktree.join("target/release/incremental")
854        ]
855        .contains(&parent.to_owned()),
856        "incremental generation outside managed target"
857    );
858    ensure!(
859        proof
860            .generation
861            .file_name()
862            .is_some_and(|n| n.to_string_lossy()
863                == format!("magi-{}", record.file_stem().unwrap().to_string_lossy())),
864        "incremental generation identity changed"
865    );
866    Ok(())
867}
868
869pub(crate) fn candidates(worktree: &Path) -> Result<Reclaim> {
870    let store = git_dir(worktree)?.join(STORE);
871    if !store.exists() {
872        return Ok(Reclaim::default());
873    }
874    ensure!(
875        plain(&store)?.is_dir(),
876        "invalid incremental provenance store"
877    );
878    let mut outputs = Vec::new();
879    let mut locks = Vec::new();
880    let mut finished = Vec::new();
881    for entry in fs::read_dir(store)? {
882        let record = entry?.path();
883        if record.extension().is_none_or(|x| x != "json") {
884            continue;
885        }
886        ensure!(
887            plain(&record)?.is_file() && fs::metadata(&record)?.len() < 4 * 1024 * 1024,
888            "invalid incremental provenance"
889        );
890        let proof: Proof = serde_json::from_slice(&fs::read(&record)?)?;
891        validate_proof(worktree, &record, &proof)?;
892        let lock_path = record.with_extension("owner");
893        let owner_metadata = plain(&lock_path)?;
894        ensure!(
895            owner_metadata.is_file() && owner_metadata.len() == 0,
896            "incremental owner is missing or changed"
897        );
898        let owner = FileLease(OpenOptions::new().read(true).write(true).open(lock_path)?);
899        crate::pr_head::try_lock_file(&owner).context("incremental compiler is live")?;
900        locks.push(owner);
901        finished.push((
902            record.clone(),
903            proof.generation.clone(),
904            proof.directories.clone(),
905        ));
906        if !proof.generation.exists() {
907            continue;
908        }
909        let actual = inventory(&proof.generation, false)?;
910        ensure!(
911            directory_inventory(&proof.generation)?.is_subset(&proof.directories),
912            "unknown incremental directory"
913        );
914        // Partial deletion can be retried. Added, replaced or modified files
915        // refuse the entire generation, even when their signatures look valid.
916        ensure!(
917            actual.iter().all(|(p, id)| proof.files.get(p) == Some(id)),
918            "incremental provenance mismatch or unknown data"
919        );
920        for relative in actual.keys() {
921            let path = proof.generation.join(relative);
922            if path.extension().is_some_and(|x| x == "lock") {
923                let file = OpenOptions::new().read(true).write(true).open(&path)?;
924                lock_session(&file).context("incremental session is live")?;
925                locks.push(FileLease(file));
926            }
927            outputs.push(path);
928        }
929    }
930    Ok(Reclaim {
931        files: outputs,
932        _locks: locks,
933        finished,
934    })
935}
936
937#[cfg(all(test, unix))]
938pub(crate) mod tests {
939    use super::*;
940
941    // A real, dependency-free Cargo crate keeps this regression below a few MiB.
942    // No production paths participate; output directories override ancestor config.
943    fn cargo_generation() -> (tempfile::TempDir, PathBuf, Generation) {
944        let temp = tempfile::tempdir().unwrap();
945        let worktree = temp.path().canonicalize().unwrap();
946        let git = std::process::Command::new("git")
947            .arg("init")
948            .arg(&worktree)
949            .output()
950            .unwrap();
951        assert!(git.status.success());
952        let generation = generate(&worktree);
953        (temp, worktree, generation)
954    }
955
956    pub(crate) fn generate(worktree: &Path) -> Generation {
957        fs::create_dir(worktree.join("src")).unwrap();
958        fs::write(
959            worktree.join("Cargo.toml"),
960            "[package]\nname='cache-fixture'\nversion='0.1.0'\nedition='2021'\n[workspace]\n",
961        )
962        .unwrap();
963        fs::write(
964            worktree.join("src/lib.rs"),
965            "pub fn answer() -> usize { (0..100).sum() }\n",
966        )
967        .unwrap();
968        let parent = worktree.join("target/debug/incremental");
969        fs::create_dir_all(&parent).unwrap();
970        let generation = Generation::create(worktree, &parent).unwrap();
971        let mut command = std::process::Command::new("cargo");
972        command.env_clear();
973        command.env(
974            "PATH",
975            crate::build_target::command::standalone_fixture_path().unwrap(),
976        );
977        for key in ["HOME", "RUSTUP_HOME"] {
978            if let Some(value) = std::env::var_os(key) {
979                command.env(key, value);
980            }
981        }
982        let output = command
983            .current_dir(worktree)
984            .env("RUSTUP_TOOLCHAIN", "stable")
985            .env("CARGO_HOME", worktree.join("cargo-home"))
986            .env("CARGO_INCREMENTAL", "0")
987            .env(
988                "RUSTFLAGS",
989                format!("-C incremental={}", generation.path.display()),
990            )
991            // CARGO_HOME isolation does not disable ancestor .cargo/config.toml.
992            // Keep intermediates local even if a parent config separates build-dir.
993            .arg("--config")
994            .arg(format!(
995                "build.build-dir={}",
996                serde_json::to_string(&worktree.join("target")).unwrap()
997            ))
998            .args(["build", "--offline", "--jobs", "1", "--target-dir"])
999            .arg(worktree.join("target"))
1000            .output()
1001            .unwrap();
1002        assert!(
1003            output.status.success(),
1004            "{}",
1005            String::from_utf8_lossy(&output.stderr)
1006        );
1007        assert!(crate::disk::dir_size(&generation.path) > 0);
1008        generation.seal(worktree).unwrap();
1009        generation
1010    }
1011
1012    #[test]
1013    fn generation_overrides_ancestor_cargo_output_configuration() {
1014        let temp = tempfile::tempdir().unwrap();
1015        let root = temp.path().canonicalize().unwrap();
1016        fs::create_dir(root.join(".cargo")).unwrap();
1017        fs::write(
1018            root.join(".cargo/config.toml"),
1019            "[build]\ntarget-dir='operator-target'\nbuild-dir='operator-build'\n",
1020        )
1021        .unwrap();
1022        let worktree = root.join("worktree");
1023        fs::create_dir(&worktree).unwrap();
1024        assert!(
1025            std::process::Command::new("git")
1026                .arg("init")
1027                .arg(&worktree)
1028                .output()
1029                .unwrap()
1030                .status
1031                .success()
1032        );
1033        let generation = generate(&worktree);
1034        assert!(
1035            worktree
1036                .join("target/debug/libcache_fixture.rlib")
1037                .is_file()
1038        );
1039        assert!(!root.join("operator-target").exists());
1040        assert!(!root.join("operator-build").exists());
1041        drop(generation);
1042        assert!(!candidates(&worktree).unwrap().files.is_empty());
1043    }
1044
1045    #[test]
1046    fn admission_contention_waits_and_timeout_is_distinct_from_budget_exhaustion() {
1047        use std::time::Duration;
1048        let temp = tempfile::tempdir().unwrap();
1049        let path = temp.path().join("admission.lock");
1050        let held = FileLease(
1051            OpenOptions::new()
1052                .create_new(true)
1053                .read(true)
1054                .write(true)
1055                .open(&path)
1056                .unwrap(),
1057        );
1058        crate::pr_head::try_lock_file(&held).unwrap();
1059        std::thread::scope(|scope| {
1060            let (ready_tx, ready_rx) = std::sync::mpsc::channel();
1061            let (done_tx, done_rx) = std::sync::mpsc::channel();
1062            let path = path.clone();
1063            scope.spawn(move || {
1064                let waiting = FileLease(
1065                    OpenOptions::new()
1066                        .read(true)
1067                        .write(true)
1068                        .open(&path)
1069                        .unwrap(),
1070                );
1071                ready_tx.send(()).unwrap();
1072                let result = wait_for_admission(&waiting, Duration::from_secs(5));
1073                done_tx.send(result).unwrap();
1074            });
1075            ready_rx.recv_timeout(Duration::from_secs(1)).unwrap();
1076            assert!(done_rx.recv_timeout(Duration::from_millis(40)).is_err());
1077            drop(held);
1078            done_rx
1079                .recv_timeout(Duration::from_secs(5))
1080                .unwrap()
1081                .unwrap();
1082        });
1083        let held = FileLease(
1084            OpenOptions::new()
1085                .read(true)
1086                .write(true)
1087                .open(&path)
1088                .unwrap(),
1089        );
1090        crate::pr_head::try_lock_file(&held).unwrap();
1091        let waiting = OpenOptions::new()
1092            .read(true)
1093            .write(true)
1094            .open(&path)
1095            .unwrap();
1096        let error = wait_for_admission(&waiting, Duration::from_millis(20))
1097            .unwrap_err()
1098            .to_string();
1099        assert!(error.contains("wait timed out"));
1100        assert!(!error.contains("budget exhausted"));
1101    }
1102
1103    #[test]
1104    fn completed_snapshots_share_owner_locks_without_blocking_parallel_admission() {
1105        let (_temp, worktree, generation) = cargo_generation();
1106        let owner = generation.record.with_extension("owner");
1107        drop(generation);
1108        let first = completed_charge(&worktree, &owner).unwrap();
1109        let second = completed_charge(&worktree, &owner).unwrap();
1110        assert!(first.0 > 0);
1111        assert_eq!(first.0, second.0);
1112        let exclusive = FileLease(
1113            OpenOptions::new()
1114                .read(true)
1115                .write(true)
1116                .open(&owner)
1117                .unwrap(),
1118        );
1119        assert!(crate::pr_head::try_lock_file(&exclusive).is_err());
1120        let parent = worktree.join("target/debug/incremental");
1121        let fresh = Generation::create(&worktree, &parent).unwrap();
1122        assert!(fresh.path.is_dir());
1123        drop(first);
1124        drop(second);
1125        assert!(crate::pr_head::try_lock_file(&exclusive).is_ok());
1126    }
1127
1128    #[test]
1129    fn ownership_release_is_not_delayed_by_descriptor_copies() {
1130        let (_temp, worktree, generation) = cargo_generation();
1131        let store = git_dir(&worktree).unwrap().join(STORE);
1132        let open = |path: &Path| {
1133            OpenOptions::new()
1134                .read(true)
1135                .write(true)
1136                .open(path)
1137                .unwrap()
1138        };
1139        let admission_path = store.join("admission.lock");
1140        let admission = FileLease(open(&admission_path));
1141        crate::pr_head::try_lock_file(&admission).unwrap();
1142        // dup and fork share the same open-file description. Keep a copy open
1143        // deterministically, without depending on another test's fork timing.
1144        let inherited_admission = admission.try_clone().unwrap();
1145        let next = FileLease(open(&admission_path));
1146        assert!(crate::pr_head::try_lock_file(&next).is_err());
1147        drop(admission);
1148        crate::pr_head::try_lock_file(&next).unwrap();
1149        drop(next);
1150        drop(inherited_admission);
1151
1152        let owner = generation.record.with_extension("owner");
1153        let inherited_generation = generation._lock.try_clone().unwrap();
1154        assert!(completed_charge(&worktree, &owner).is_err());
1155        drop(generation);
1156        let first = completed_charge(&worktree, &owner).unwrap();
1157        let second = completed_charge(&worktree, &owner).unwrap();
1158        let inherited_first = first.1.try_clone().unwrap();
1159        let inherited_second = second.1.try_clone().unwrap();
1160        let exclusive = FileLease(open(&owner));
1161        drop(first);
1162        assert!(
1163            crate::pr_head::try_lock_file(&exclusive).is_err(),
1164            "another actual accounting owner still excludes reclamation"
1165        );
1166        drop(second);
1167        crate::pr_head::try_lock_file(&exclusive).unwrap();
1168        drop(exclusive);
1169        drop((inherited_generation, inherited_first, inherited_second));
1170
1171        let reclaim = candidates(&worktree).unwrap();
1172        let inherited_reclaim: Vec<_> = reclaim
1173            ._locks
1174            .iter()
1175            .map(|lock| lock.try_clone().unwrap())
1176            .collect();
1177        assert!(candidates(&worktree).is_err());
1178        drop(reclaim);
1179        let retry = candidates(&worktree).unwrap();
1180        assert!(!retry.files.is_empty());
1181        drop(retry);
1182        drop(inherited_reclaim);
1183    }
1184
1185    #[test]
1186    fn default_disables_incremental_and_explicit_environment_is_preserved() {
1187        let temp = tempfile::tempdir().unwrap();
1188        let mut command = tokio::process::Command::new("cargo");
1189        command.env_remove("CARGO_INCREMENTAL");
1190        configure(&mut command, temp.path()).unwrap();
1191        let env: BTreeMap<_, _> = command.as_std().get_envs().collect();
1192        assert_eq!(
1193            env[std::ffi::OsStr::new("CARGO_INCREMENTAL")],
1194            Some(std::ffi::OsStr::new("0"))
1195        );
1196        command.env("CARGO_INCREMENTAL", "1");
1197        configure(&mut command, temp.path()).unwrap();
1198        let env: BTreeMap<_, _> = command.as_std().get_envs().collect();
1199        assert_eq!(
1200            env[std::ffi::OsStr::new("CARGO_INCREMENTAL")],
1201            Some(std::ffi::OsStr::new("1"))
1202        );
1203    }
1204
1205    #[test]
1206    fn wrapper_temporary_collision_preserves_the_other_publisher() {
1207        use std::io::Write;
1208        let temp = tempfile::tempdir().unwrap();
1209        let root = temp.path().canonicalize().unwrap();
1210        let occupied = root.join("occupied.tmp");
1211        let fresh = root.join("fresh.tmp");
1212        let script = root.join("wrapper.sh");
1213        let barrier = std::sync::Barrier::new(2);
1214        std::thread::scope(|scope| {
1215            scope.spawn(|| {
1216                let mut owner = WrapperTemporary::create(|| occupied.clone()).unwrap();
1217                owner.file.write_all(b"other publisher").unwrap();
1218                barrier.wait();
1219                barrier.wait();
1220                // The competing publisher must not unlink our reserved path.
1221                assert_eq!(fs::read(&occupied).unwrap(), b"other publisher");
1222            });
1223            barrier.wait();
1224            let mut attempts = 0;
1225            let result = publish_wrapper(&script, "complete wrapper", || {
1226                attempts += 1;
1227                if attempts == 1 {
1228                    occupied.clone()
1229                } else {
1230                    fresh.clone()
1231                }
1232            });
1233            // Always release the owner, even if publication regresses.
1234            barrier.wait();
1235            result.unwrap();
1236            assert_eq!(attempts, 2);
1237            assert_eq!(fs::read(&script).unwrap(), b"complete wrapper");
1238            assert!(!fresh.exists());
1239        });
1240        assert!(!occupied.exists());
1241    }
1242
1243    #[test]
1244    fn wrapper_temporary_retries_only_collisions_and_leaves_them_intact() {
1245        let temp = tempfile::tempdir().unwrap();
1246        let occupied = temp.path().join("occupied.tmp");
1247        fs::write(&occupied, b"reserved").unwrap();
1248        let mut attempts = 0;
1249        let error = WrapperTemporary::create(|| {
1250            attempts += 1;
1251            occupied.clone()
1252        })
1253        .err()
1254        .unwrap();
1255        assert_eq!(error.kind(), std::io::ErrorKind::AlreadyExists);
1256        assert_eq!(attempts, 128);
1257        assert_eq!(fs::read(&occupied).unwrap(), b"reserved");
1258        attempts = 0;
1259        let error = WrapperTemporary::create(|| {
1260            attempts += 1;
1261            temp.path().join("missing/temporary")
1262        })
1263        .err()
1264        .unwrap();
1265        assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
1266        assert_eq!(attempts, 1);
1267    }
1268
1269    #[test]
1270    fn execution_wrapper_identity_rejects_changed_unknown_and_symlink_files() {
1271        let temp = tempfile::tempdir().unwrap();
1272        let root = temp.path().canonicalize().unwrap();
1273        let (name, body) = wrapper_spec().unwrap();
1274        let script = root.join(name);
1275        fs::write(&script, &body).unwrap();
1276        assert!(current_wrapper(&script).unwrap());
1277        let unknown = root.join("operator-checkpoint");
1278        fs::write(&unknown, &body).unwrap();
1279        assert!(!current_wrapper(&unknown).unwrap());
1280        fs::write(&script, "changed").unwrap();
1281        assert!(!current_wrapper(&script).unwrap());
1282        fs::remove_file(&script).unwrap();
1283        std::os::unix::fs::symlink(&unknown, &script).unwrap();
1284        assert!(current_wrapper(&script).is_err());
1285    }
1286
1287    #[test]
1288    fn wrapper_publication_preserves_existing_destinations() {
1289        use std::os::unix::fs::{MetadataExt, symlink};
1290        let temp = tempfile::tempdir().unwrap();
1291        let root = temp.path().canonicalize().unwrap();
1292        let script = root.join("wrapper.sh");
1293        let temporary = root.join("temporary");
1294        let body = "#!/bin/sh\nexit 0\n";
1295        fs::write(&script, body).unwrap();
1296        let inode = fs::metadata(&script).unwrap().ino();
1297        publish_wrapper(&script, body, || temporary.clone()).unwrap();
1298        assert_eq!(fs::metadata(&script).unwrap().ino(), inode);
1299        assert!(!temporary.exists());
1300        fs::write(&script, "different").unwrap();
1301        assert!(publish_wrapper(&script, body, || temporary.clone()).is_err());
1302        assert_eq!(fs::read_to_string(&script).unwrap(), "different");
1303        assert!(!temporary.exists());
1304        fs::remove_file(&script).unwrap();
1305        fs::create_dir(&script).unwrap();
1306        assert!(publish_wrapper(&script, body, || temporary.clone()).is_err());
1307        assert!(script.is_dir());
1308        assert!(!temporary.exists());
1309        fs::remove_dir(&script).unwrap();
1310        let target = root.join("target");
1311        fs::write(&target, body).unwrap();
1312        symlink(&target, &script).unwrap();
1313        assert!(publish_wrapper(&script, body, || temporary.clone()).is_err());
1314        assert_eq!(fs::read_link(&script).unwrap(), target);
1315        assert_eq!(fs::read_to_string(&target).unwrap(), body);
1316        assert!(!temporary.exists());
1317    }
1318
1319    #[test]
1320    fn concurrent_configuration_publishes_complete_wrappers() {
1321        let temp = tempfile::tempdir().unwrap();
1322        let root = temp.path().canonicalize().unwrap();
1323        let worktree = root.join("worktree");
1324        assert!(
1325            std::process::Command::new("git")
1326                .args(["init", "--separate-git-dir"])
1327                .arg(root.join("metadata"))
1328                .arg(&worktree)
1329                .output()
1330                .unwrap()
1331                .status
1332                .success()
1333        );
1334        use std::os::unix::fs::PermissionsExt;
1335        let executable = std::env::current_exe().unwrap();
1336        let quoted = executable.to_string_lossy().replace('\'', "'\\''");
1337        let body = format!("#!/bin/sh\nexec '{quoted}' __magi_incremental \"$@\"\n");
1338        let barrier = std::sync::Barrier::new(8);
1339        let wrappers = std::thread::scope(|scope| {
1340            let handles: Vec<_> = (0..8)
1341                .map(|_| {
1342                    scope.spawn(|| {
1343                        let mut last = None;
1344                        barrier.wait();
1345                        for _ in 0..16 {
1346                            let mut command = tokio::process::Command::new("cargo");
1347                            configure(&mut command, &worktree).unwrap();
1348                            let env: BTreeMap<_, _> = command.as_std().get_envs().collect();
1349                            let wrapper =
1350                                PathBuf::from(env[std::ffi::OsStr::new("RUSTC_WRAPPER")].unwrap());
1351                            assert_eq!(fs::read_to_string(&wrapper).unwrap(), body);
1352                            assert_eq!(
1353                                fs::metadata(&wrapper).unwrap().permissions().mode() & 0o777,
1354                                0o700
1355                            );
1356                            if let Some(previous) = last.replace(wrapper.clone()) {
1357                                assert_eq!(previous, wrapper);
1358                            }
1359                        }
1360                        last.unwrap()
1361                    })
1362                })
1363                .collect();
1364            handles
1365                .into_iter()
1366                .map(|handle| handle.join().unwrap())
1367                .collect::<Vec<_>>()
1368        });
1369        assert!(wrappers.iter().all(|path| path == &wrappers[0]));
1370        assert!(
1371            fs::read_dir(git_dir(&worktree).unwrap())
1372                .unwrap()
1373                .all(|entry| {
1374                    !entry
1375                        .unwrap()
1376                        .file_name()
1377                        .to_string_lossy()
1378                        .ends_with(".tmp")
1379                })
1380        );
1381    }
1382
1383    #[test]
1384    fn sealing_refuses_unfinished_sessions_and_unknown_directories() {
1385        let (_temp, worktree, generation) = cargo_generation();
1386        let unknown = generation.path.join("operator-empty");
1387        fs::create_dir(&unknown).unwrap();
1388        assert!(generation.seal(&worktree).is_err());
1389        fs::remove_dir(unknown).unwrap();
1390        let crate_dir = fs::read_dir(&generation.path)
1391            .unwrap()
1392            .next()
1393            .unwrap()
1394            .unwrap()
1395            .path();
1396        let working = crate_dir.join("s-unfinished-cache-working");
1397        fs::create_dir(&working).unwrap();
1398        assert!(generation.seal(&worktree).is_err());
1399        assert!(working.exists());
1400        drop(generation);
1401        assert!(candidates(&worktree).is_err());
1402    }
1403
1404    #[test]
1405    fn unknown_empty_directories_added_after_inventory_are_retained() {
1406        let (_temp, worktree, generation) = cargo_generation();
1407        let path = generation.path.clone();
1408        drop(generation);
1409        let reclaim = candidates(&worktree).unwrap();
1410        for file in &reclaim.files {
1411            fs::remove_file(file).unwrap();
1412        }
1413        let unknown = path.join("operator-empty");
1414        fs::create_dir(&unknown).unwrap();
1415        assert!(reclaim.finish().is_err());
1416        assert!(unknown.is_dir());
1417        fs::remove_dir(unknown).unwrap();
1418        reclaim.finish().unwrap();
1419        assert!(!path.exists());
1420    }
1421
1422    #[test]
1423    fn fcntl_session_lock_is_respected_across_processes() {
1424        use std::io::{Read, Write};
1425        use std::process::{Command, Stdio};
1426        if let Some(path) = std::env::var_os("MAGI_TEST_SESSION_FILE") {
1427            let file = OpenOptions::new()
1428                .read(true)
1429                .write(true)
1430                .open(path)
1431                .unwrap();
1432            // Deliberately do not take flock: both compiler protocols must
1433            // exclude reclamation, even on hosts where they are independent.
1434            lock_posix_session(&file).unwrap();
1435            fs::write(
1436                std::env::var_os("MAGI_TEST_SESSION_READY").unwrap(),
1437                "ready",
1438            )
1439            .unwrap();
1440            std::io::stdin().read_exact(&mut [0]).unwrap();
1441            return;
1442        }
1443        let (_temp, worktree, generation) = cargo_generation();
1444        drop(generation);
1445        let candidates = candidates(&worktree).unwrap();
1446        let session = candidates
1447            .files
1448            .iter()
1449            .find(|p| p.extension().is_some_and(|x| x == "lock"))
1450            .unwrap()
1451            .clone();
1452        drop(candidates);
1453        let ready = worktree.join("session-ready");
1454        let mut child = Command::new(std::env::current_exe().unwrap())
1455            .args([
1456                "--exact",
1457                "incremental::tests::fcntl_session_lock_is_respected_across_processes",
1458            ])
1459            .env("MAGI_TEST_SESSION_FILE", &session)
1460            .env("MAGI_TEST_SESSION_READY", &ready)
1461            .stdin(Stdio::piped())
1462            .stdout(Stdio::null())
1463            .spawn()
1464            .unwrap();
1465        let started = std::time::Instant::now();
1466        while !ready.exists() && started.elapsed() < std::time::Duration::from_secs(10) {
1467            if child.try_wait().unwrap().is_some() {
1468                break;
1469            }
1470            std::thread::sleep(std::time::Duration::from_millis(10));
1471        }
1472        let observed = ready.exists();
1473        let blocked = observed
1474            && super::candidates(&worktree)
1475                .err()
1476                .is_some_and(|e| e.to_string().contains("session is live"));
1477        if observed {
1478            child.stdin.take().unwrap().write_all(b"x").unwrap();
1479        } else {
1480            child.kill().ok();
1481        }
1482        let status = child.wait().unwrap();
1483        assert!(
1484            observed && status.success(),
1485            "session helper did not acquire its lock"
1486        );
1487        assert!(blocked, "POSIX session lock must exclude reclamation");
1488        assert!(!super::candidates(&worktree).unwrap().files.is_empty());
1489        assert!(session.exists());
1490    }
1491
1492    #[test]
1493    fn real_cargo_generations_require_exact_provenance_and_live_owner_exclusion() {
1494        let (_temp, worktree, generation) = cargo_generation();
1495        assert!(
1496            candidates(&worktree).is_err(),
1497            "compiler owner still holds the generation"
1498        );
1499        let path = generation.path.clone();
1500        drop(generation);
1501        let reclaim = candidates(&worktree).unwrap();
1502        let session = reclaim
1503            .files
1504            .iter()
1505            .find(|p| p.extension().is_some_and(|x| x == "lock"))
1506            .unwrap()
1507            .clone();
1508        drop(reclaim);
1509        let live_session = FileLease(
1510            OpenOptions::new()
1511                .read(true)
1512                .write(true)
1513                .open(session)
1514                .unwrap(),
1515        );
1516        crate::pr_head::try_lock_file(&live_session).unwrap();
1517        assert!(
1518            candidates(&worktree)
1519                .err()
1520                .unwrap()
1521                .to_string()
1522                .contains("session is live")
1523        );
1524        drop(live_session);
1525        let unknown = path.join("operator-note");
1526        fs::write(&unknown, "keep unknown data").unwrap();
1527        assert!(candidates(&worktree).is_err());
1528        assert_eq!(fs::read_to_string(&unknown).unwrap(), "keep unknown data");
1529        fs::remove_file(&unknown).unwrap();
1530        let unknown_dir = path.join("operator-empty");
1531        fs::create_dir(&unknown_dir).unwrap();
1532        assert!(candidates(&worktree).is_err());
1533        fs::remove_dir(unknown_dir).unwrap();
1534        let reclaim = candidates(&worktree).unwrap();
1535        assert!(!reclaim.files.is_empty());
1536        assert!(
1537            candidates(&worktree).is_err(),
1538            "maintenance retains session/owner locks"
1539        );
1540        // Partial deletion can resume, but changed bytes cannot be adopted.
1541        let removed = reclaim.files[0].clone();
1542        fs::remove_file(removed).unwrap();
1543        drop(reclaim);
1544        let reclaim = candidates(&worktree).unwrap();
1545        let changed = reclaim
1546            .files
1547            .iter()
1548            .find(|p| p.extension().is_some_and(|e| e == "bin"))
1549            .unwrap()
1550            .clone();
1551        let original = fs::read(&changed).unwrap();
1552        fs::write(&changed, b"modified proof input").unwrap();
1553        drop(reclaim);
1554        assert!(candidates(&worktree).is_err());
1555        fs::write(&changed, original).unwrap();
1556        let reclaim = candidates(&worktree).unwrap();
1557        for file in &reclaim.files {
1558            fs::remove_file(file).unwrap();
1559        }
1560        reclaim.finish().unwrap();
1561        assert!(!path.exists());
1562        assert!(candidates(&worktree).unwrap().files.is_empty());
1563        assert!(worktree.join("src/lib.rs").is_file());
1564    }
1565
1566    #[test]
1567    fn externally_removed_sealed_generation_does_not_strand_admission() {
1568        let (_temp, worktree, generation) = cargo_generation();
1569        let path = generation.path.clone();
1570        drop(generation);
1571        let reclaim = candidates(&worktree).unwrap();
1572        for file in &reclaim.files {
1573            fs::remove_file(file).unwrap();
1574        }
1575        // Simulate a clean/reset removing only empty cache directories, while
1576        // leaving the completion proof in the worktree Git directory.
1577        let mut dirs = directory_inventory(&path)
1578            .unwrap()
1579            .into_iter()
1580            .collect::<Vec<_>>();
1581        dirs.sort_by_key(|p| std::cmp::Reverse(p.components().count()));
1582        for relative in dirs {
1583            fs::remove_dir(path.join(relative)).unwrap();
1584        }
1585        fs::remove_dir(&path).unwrap();
1586        drop(reclaim);
1587        let parent = worktree.join("target/debug/incremental");
1588        let fresh = Generation::create(&worktree, &parent).unwrap();
1589        assert_eq!(
1590            fs::read_dir(git_dir(&worktree).unwrap().join(STORE))
1591                .unwrap()
1592                .filter_map(|e| e.ok())
1593                .filter(|e| e.path().extension().is_some_and(|x| x == "owner"))
1594                .count(),
1595            1
1596        );
1597        assert!(fresh.path.is_dir());
1598    }
1599
1600    #[test]
1601    fn unsealed_attempts_consume_the_bounded_generation_budget() {
1602        let temp = tempfile::tempdir().unwrap();
1603        let worktree = temp.path().canonicalize().unwrap();
1604        assert!(
1605            std::process::Command::new("git")
1606                .arg("init")
1607                .arg(&worktree)
1608                .output()
1609                .unwrap()
1610                .status
1611                .success()
1612        );
1613        let parent = worktree.join("target/debug/incremental");
1614        fs::create_dir_all(&parent).unwrap();
1615        for _ in 0..SLOTS {
1616            let generation = Generation::create(&worktree, &parent).unwrap();
1617            fs::write(generation.path.join("unknown"), "preserve").unwrap();
1618        }
1619        assert!(Generation::create(&worktree, &parent).is_err());
1620        assert!(
1621            candidates(&worktree).unwrap().files.is_empty(),
1622            "unsealed output never grants deletion authority"
1623        );
1624    }
1625
1626    #[cfg(unix)]
1627    #[test]
1628    fn generation_symlinks_and_replaced_inodes_refuse_reclamation() {
1629        let (_temp, worktree, generation) = cargo_generation();
1630        let path = generation.path.clone();
1631        drop(generation);
1632        let files = candidates(&worktree).unwrap().files;
1633        let file = files
1634            .iter()
1635            .find(|p| p.extension().is_some_and(|e| e == "bin"))
1636            .unwrap();
1637        let saved = worktree.join("retained-input");
1638        fs::rename(file, &saved).unwrap();
1639        std::os::unix::fs::symlink(&saved, file).unwrap();
1640        assert!(candidates(&worktree).is_err());
1641        fs::remove_file(file).unwrap();
1642        fs::copy(&saved, file).unwrap();
1643        assert!(
1644            candidates(&worktree).is_err(),
1645            "same bytes at a replaced inode are insufficient"
1646        );
1647        assert!(path.exists());
1648        assert!(saved.is_file());
1649    }
1650}