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        for key in ["PATH", "HOME", "RUSTUP_HOME"] {
974            if let Some(value) = std::env::var_os(key) {
975                command.env(key, value);
976            }
977        }
978        let output = command
979            .current_dir(worktree)
980            .env("RUSTUP_TOOLCHAIN", "stable")
981            .env("CARGO_HOME", worktree.join("cargo-home"))
982            .env("CARGO_INCREMENTAL", "0")
983            .env(
984                "RUSTFLAGS",
985                format!("-C incremental={}", generation.path.display()),
986            )
987            // CARGO_HOME isolation does not disable ancestor .cargo/config.toml.
988            // Keep intermediates local even if a parent config separates build-dir.
989            .arg("--config")
990            .arg(format!(
991                "build.build-dir={}",
992                serde_json::to_string(&worktree.join("target")).unwrap()
993            ))
994            .args(["build", "--offline", "--jobs", "1", "--target-dir"])
995            .arg(worktree.join("target"))
996            .output()
997            .unwrap();
998        assert!(
999            output.status.success(),
1000            "{}",
1001            String::from_utf8_lossy(&output.stderr)
1002        );
1003        assert!(crate::disk::dir_size(&generation.path) > 0);
1004        generation.seal(worktree).unwrap();
1005        generation
1006    }
1007
1008    #[test]
1009    fn generation_overrides_ancestor_cargo_output_configuration() {
1010        let temp = tempfile::tempdir().unwrap();
1011        let root = temp.path().canonicalize().unwrap();
1012        fs::create_dir(root.join(".cargo")).unwrap();
1013        fs::write(
1014            root.join(".cargo/config.toml"),
1015            "[build]\ntarget-dir='operator-target'\nbuild-dir='operator-build'\n",
1016        )
1017        .unwrap();
1018        let worktree = root.join("worktree");
1019        fs::create_dir(&worktree).unwrap();
1020        assert!(
1021            std::process::Command::new("git")
1022                .arg("init")
1023                .arg(&worktree)
1024                .output()
1025                .unwrap()
1026                .status
1027                .success()
1028        );
1029        let generation = generate(&worktree);
1030        assert!(
1031            worktree
1032                .join("target/debug/libcache_fixture.rlib")
1033                .is_file()
1034        );
1035        assert!(!root.join("operator-target").exists());
1036        assert!(!root.join("operator-build").exists());
1037        drop(generation);
1038        assert!(!candidates(&worktree).unwrap().files.is_empty());
1039    }
1040
1041    #[test]
1042    fn admission_contention_waits_and_timeout_is_distinct_from_budget_exhaustion() {
1043        use std::time::Duration;
1044        let temp = tempfile::tempdir().unwrap();
1045        let path = temp.path().join("admission.lock");
1046        let held = FileLease(
1047            OpenOptions::new()
1048                .create_new(true)
1049                .read(true)
1050                .write(true)
1051                .open(&path)
1052                .unwrap(),
1053        );
1054        crate::pr_head::try_lock_file(&held).unwrap();
1055        std::thread::scope(|scope| {
1056            let (ready_tx, ready_rx) = std::sync::mpsc::channel();
1057            let (done_tx, done_rx) = std::sync::mpsc::channel();
1058            let path = path.clone();
1059            scope.spawn(move || {
1060                let waiting = FileLease(
1061                    OpenOptions::new()
1062                        .read(true)
1063                        .write(true)
1064                        .open(&path)
1065                        .unwrap(),
1066                );
1067                ready_tx.send(()).unwrap();
1068                let result = wait_for_admission(&waiting, Duration::from_secs(5));
1069                done_tx.send(result).unwrap();
1070            });
1071            ready_rx.recv_timeout(Duration::from_secs(1)).unwrap();
1072            assert!(done_rx.recv_timeout(Duration::from_millis(40)).is_err());
1073            drop(held);
1074            done_rx
1075                .recv_timeout(Duration::from_secs(5))
1076                .unwrap()
1077                .unwrap();
1078        });
1079        let held = FileLease(
1080            OpenOptions::new()
1081                .read(true)
1082                .write(true)
1083                .open(&path)
1084                .unwrap(),
1085        );
1086        crate::pr_head::try_lock_file(&held).unwrap();
1087        let waiting = OpenOptions::new()
1088            .read(true)
1089            .write(true)
1090            .open(&path)
1091            .unwrap();
1092        let error = wait_for_admission(&waiting, Duration::from_millis(20))
1093            .unwrap_err()
1094            .to_string();
1095        assert!(error.contains("wait timed out"));
1096        assert!(!error.contains("budget exhausted"));
1097    }
1098
1099    #[test]
1100    fn completed_snapshots_share_owner_locks_without_blocking_parallel_admission() {
1101        let (_temp, worktree, generation) = cargo_generation();
1102        let owner = generation.record.with_extension("owner");
1103        drop(generation);
1104        let first = completed_charge(&worktree, &owner).unwrap();
1105        let second = completed_charge(&worktree, &owner).unwrap();
1106        assert!(first.0 > 0);
1107        assert_eq!(first.0, second.0);
1108        let exclusive = FileLease(
1109            OpenOptions::new()
1110                .read(true)
1111                .write(true)
1112                .open(&owner)
1113                .unwrap(),
1114        );
1115        assert!(crate::pr_head::try_lock_file(&exclusive).is_err());
1116        let parent = worktree.join("target/debug/incremental");
1117        let fresh = Generation::create(&worktree, &parent).unwrap();
1118        assert!(fresh.path.is_dir());
1119        drop(first);
1120        drop(second);
1121        assert!(crate::pr_head::try_lock_file(&exclusive).is_ok());
1122    }
1123
1124    #[test]
1125    fn ownership_release_is_not_delayed_by_descriptor_copies() {
1126        let (_temp, worktree, generation) = cargo_generation();
1127        let store = git_dir(&worktree).unwrap().join(STORE);
1128        let open = |path: &Path| {
1129            OpenOptions::new()
1130                .read(true)
1131                .write(true)
1132                .open(path)
1133                .unwrap()
1134        };
1135        let admission_path = store.join("admission.lock");
1136        let admission = FileLease(open(&admission_path));
1137        crate::pr_head::try_lock_file(&admission).unwrap();
1138        // dup and fork share the same open-file description. Keep a copy open
1139        // deterministically, without depending on another test's fork timing.
1140        let inherited_admission = admission.try_clone().unwrap();
1141        let next = FileLease(open(&admission_path));
1142        assert!(crate::pr_head::try_lock_file(&next).is_err());
1143        drop(admission);
1144        crate::pr_head::try_lock_file(&next).unwrap();
1145        drop(next);
1146        drop(inherited_admission);
1147
1148        let owner = generation.record.with_extension("owner");
1149        let inherited_generation = generation._lock.try_clone().unwrap();
1150        assert!(completed_charge(&worktree, &owner).is_err());
1151        drop(generation);
1152        let first = completed_charge(&worktree, &owner).unwrap();
1153        let second = completed_charge(&worktree, &owner).unwrap();
1154        let inherited_first = first.1.try_clone().unwrap();
1155        let inherited_second = second.1.try_clone().unwrap();
1156        let exclusive = FileLease(open(&owner));
1157        drop(first);
1158        assert!(
1159            crate::pr_head::try_lock_file(&exclusive).is_err(),
1160            "another actual accounting owner still excludes reclamation"
1161        );
1162        drop(second);
1163        crate::pr_head::try_lock_file(&exclusive).unwrap();
1164        drop(exclusive);
1165        drop((inherited_generation, inherited_first, inherited_second));
1166
1167        let reclaim = candidates(&worktree).unwrap();
1168        let inherited_reclaim: Vec<_> = reclaim
1169            ._locks
1170            .iter()
1171            .map(|lock| lock.try_clone().unwrap())
1172            .collect();
1173        assert!(candidates(&worktree).is_err());
1174        drop(reclaim);
1175        let retry = candidates(&worktree).unwrap();
1176        assert!(!retry.files.is_empty());
1177        drop(retry);
1178        drop(inherited_reclaim);
1179    }
1180
1181    #[test]
1182    fn default_disables_incremental_and_explicit_environment_is_preserved() {
1183        let temp = tempfile::tempdir().unwrap();
1184        let mut command = tokio::process::Command::new("cargo");
1185        command.env_remove("CARGO_INCREMENTAL");
1186        configure(&mut command, temp.path()).unwrap();
1187        let env: BTreeMap<_, _> = command.as_std().get_envs().collect();
1188        assert_eq!(
1189            env[std::ffi::OsStr::new("CARGO_INCREMENTAL")],
1190            Some(std::ffi::OsStr::new("0"))
1191        );
1192        command.env("CARGO_INCREMENTAL", "1");
1193        configure(&mut command, temp.path()).unwrap();
1194        let env: BTreeMap<_, _> = command.as_std().get_envs().collect();
1195        assert_eq!(
1196            env[std::ffi::OsStr::new("CARGO_INCREMENTAL")],
1197            Some(std::ffi::OsStr::new("1"))
1198        );
1199    }
1200
1201    #[test]
1202    fn wrapper_temporary_collision_preserves_the_other_publisher() {
1203        use std::io::Write;
1204        let temp = tempfile::tempdir().unwrap();
1205        let root = temp.path().canonicalize().unwrap();
1206        let occupied = root.join("occupied.tmp");
1207        let fresh = root.join("fresh.tmp");
1208        let script = root.join("wrapper.sh");
1209        let barrier = std::sync::Barrier::new(2);
1210        std::thread::scope(|scope| {
1211            scope.spawn(|| {
1212                let mut owner = WrapperTemporary::create(|| occupied.clone()).unwrap();
1213                owner.file.write_all(b"other publisher").unwrap();
1214                barrier.wait();
1215                barrier.wait();
1216                // The competing publisher must not unlink our reserved path.
1217                assert_eq!(fs::read(&occupied).unwrap(), b"other publisher");
1218            });
1219            barrier.wait();
1220            let mut attempts = 0;
1221            let result = publish_wrapper(&script, "complete wrapper", || {
1222                attempts += 1;
1223                if attempts == 1 {
1224                    occupied.clone()
1225                } else {
1226                    fresh.clone()
1227                }
1228            });
1229            // Always release the owner, even if publication regresses.
1230            barrier.wait();
1231            result.unwrap();
1232            assert_eq!(attempts, 2);
1233            assert_eq!(fs::read(&script).unwrap(), b"complete wrapper");
1234            assert!(!fresh.exists());
1235        });
1236        assert!(!occupied.exists());
1237    }
1238
1239    #[test]
1240    fn wrapper_temporary_retries_only_collisions_and_leaves_them_intact() {
1241        let temp = tempfile::tempdir().unwrap();
1242        let occupied = temp.path().join("occupied.tmp");
1243        fs::write(&occupied, b"reserved").unwrap();
1244        let mut attempts = 0;
1245        let error = WrapperTemporary::create(|| {
1246            attempts += 1;
1247            occupied.clone()
1248        })
1249        .err()
1250        .unwrap();
1251        assert_eq!(error.kind(), std::io::ErrorKind::AlreadyExists);
1252        assert_eq!(attempts, 128);
1253        assert_eq!(fs::read(&occupied).unwrap(), b"reserved");
1254        attempts = 0;
1255        let error = WrapperTemporary::create(|| {
1256            attempts += 1;
1257            temp.path().join("missing/temporary")
1258        })
1259        .err()
1260        .unwrap();
1261        assert_eq!(error.kind(), std::io::ErrorKind::NotFound);
1262        assert_eq!(attempts, 1);
1263    }
1264
1265    #[test]
1266    fn execution_wrapper_identity_rejects_changed_unknown_and_symlink_files() {
1267        let temp = tempfile::tempdir().unwrap();
1268        let root = temp.path().canonicalize().unwrap();
1269        let (name, body) = wrapper_spec().unwrap();
1270        let script = root.join(name);
1271        fs::write(&script, &body).unwrap();
1272        assert!(current_wrapper(&script).unwrap());
1273        let unknown = root.join("operator-checkpoint");
1274        fs::write(&unknown, &body).unwrap();
1275        assert!(!current_wrapper(&unknown).unwrap());
1276        fs::write(&script, "changed").unwrap();
1277        assert!(!current_wrapper(&script).unwrap());
1278        fs::remove_file(&script).unwrap();
1279        std::os::unix::fs::symlink(&unknown, &script).unwrap();
1280        assert!(current_wrapper(&script).is_err());
1281    }
1282
1283    #[test]
1284    fn wrapper_publication_preserves_existing_destinations() {
1285        use std::os::unix::fs::{MetadataExt, symlink};
1286        let temp = tempfile::tempdir().unwrap();
1287        let root = temp.path().canonicalize().unwrap();
1288        let script = root.join("wrapper.sh");
1289        let temporary = root.join("temporary");
1290        let body = "#!/bin/sh\nexit 0\n";
1291        fs::write(&script, body).unwrap();
1292        let inode = fs::metadata(&script).unwrap().ino();
1293        publish_wrapper(&script, body, || temporary.clone()).unwrap();
1294        assert_eq!(fs::metadata(&script).unwrap().ino(), inode);
1295        assert!(!temporary.exists());
1296        fs::write(&script, "different").unwrap();
1297        assert!(publish_wrapper(&script, body, || temporary.clone()).is_err());
1298        assert_eq!(fs::read_to_string(&script).unwrap(), "different");
1299        assert!(!temporary.exists());
1300        fs::remove_file(&script).unwrap();
1301        fs::create_dir(&script).unwrap();
1302        assert!(publish_wrapper(&script, body, || temporary.clone()).is_err());
1303        assert!(script.is_dir());
1304        assert!(!temporary.exists());
1305        fs::remove_dir(&script).unwrap();
1306        let target = root.join("target");
1307        fs::write(&target, body).unwrap();
1308        symlink(&target, &script).unwrap();
1309        assert!(publish_wrapper(&script, body, || temporary.clone()).is_err());
1310        assert_eq!(fs::read_link(&script).unwrap(), target);
1311        assert_eq!(fs::read_to_string(&target).unwrap(), body);
1312        assert!(!temporary.exists());
1313    }
1314
1315    #[test]
1316    fn concurrent_configuration_publishes_complete_wrappers() {
1317        let temp = tempfile::tempdir().unwrap();
1318        let root = temp.path().canonicalize().unwrap();
1319        let worktree = root.join("worktree");
1320        assert!(
1321            std::process::Command::new("git")
1322                .args(["init", "--separate-git-dir"])
1323                .arg(root.join("metadata"))
1324                .arg(&worktree)
1325                .output()
1326                .unwrap()
1327                .status
1328                .success()
1329        );
1330        use std::os::unix::fs::PermissionsExt;
1331        let executable = std::env::current_exe().unwrap();
1332        let quoted = executable.to_string_lossy().replace('\'', "'\\''");
1333        let body = format!("#!/bin/sh\nexec '{quoted}' __magi_incremental \"$@\"\n");
1334        let barrier = std::sync::Barrier::new(8);
1335        let wrappers = std::thread::scope(|scope| {
1336            let handles: Vec<_> = (0..8)
1337                .map(|_| {
1338                    scope.spawn(|| {
1339                        let mut last = None;
1340                        barrier.wait();
1341                        for _ in 0..16 {
1342                            let mut command = tokio::process::Command::new("cargo");
1343                            configure(&mut command, &worktree).unwrap();
1344                            let env: BTreeMap<_, _> = command.as_std().get_envs().collect();
1345                            let wrapper =
1346                                PathBuf::from(env[std::ffi::OsStr::new("RUSTC_WRAPPER")].unwrap());
1347                            assert_eq!(fs::read_to_string(&wrapper).unwrap(), body);
1348                            assert_eq!(
1349                                fs::metadata(&wrapper).unwrap().permissions().mode() & 0o777,
1350                                0o700
1351                            );
1352                            if let Some(previous) = last.replace(wrapper.clone()) {
1353                                assert_eq!(previous, wrapper);
1354                            }
1355                        }
1356                        last.unwrap()
1357                    })
1358                })
1359                .collect();
1360            handles
1361                .into_iter()
1362                .map(|handle| handle.join().unwrap())
1363                .collect::<Vec<_>>()
1364        });
1365        assert!(wrappers.iter().all(|path| path == &wrappers[0]));
1366        assert!(
1367            fs::read_dir(git_dir(&worktree).unwrap())
1368                .unwrap()
1369                .all(|entry| {
1370                    !entry
1371                        .unwrap()
1372                        .file_name()
1373                        .to_string_lossy()
1374                        .ends_with(".tmp")
1375                })
1376        );
1377    }
1378
1379    #[test]
1380    fn sealing_refuses_unfinished_sessions_and_unknown_directories() {
1381        let (_temp, worktree, generation) = cargo_generation();
1382        let unknown = generation.path.join("operator-empty");
1383        fs::create_dir(&unknown).unwrap();
1384        assert!(generation.seal(&worktree).is_err());
1385        fs::remove_dir(unknown).unwrap();
1386        let crate_dir = fs::read_dir(&generation.path)
1387            .unwrap()
1388            .next()
1389            .unwrap()
1390            .unwrap()
1391            .path();
1392        let working = crate_dir.join("s-unfinished-cache-working");
1393        fs::create_dir(&working).unwrap();
1394        assert!(generation.seal(&worktree).is_err());
1395        assert!(working.exists());
1396        drop(generation);
1397        assert!(candidates(&worktree).is_err());
1398    }
1399
1400    #[test]
1401    fn unknown_empty_directories_added_after_inventory_are_retained() {
1402        let (_temp, worktree, generation) = cargo_generation();
1403        let path = generation.path.clone();
1404        drop(generation);
1405        let reclaim = candidates(&worktree).unwrap();
1406        for file in &reclaim.files {
1407            fs::remove_file(file).unwrap();
1408        }
1409        let unknown = path.join("operator-empty");
1410        fs::create_dir(&unknown).unwrap();
1411        assert!(reclaim.finish().is_err());
1412        assert!(unknown.is_dir());
1413        fs::remove_dir(unknown).unwrap();
1414        reclaim.finish().unwrap();
1415        assert!(!path.exists());
1416    }
1417
1418    #[test]
1419    fn fcntl_session_lock_is_respected_across_processes() {
1420        use std::io::{Read, Write};
1421        use std::process::{Command, Stdio};
1422        if let Some(path) = std::env::var_os("MAGI_TEST_SESSION_FILE") {
1423            let file = OpenOptions::new()
1424                .read(true)
1425                .write(true)
1426                .open(path)
1427                .unwrap();
1428            // Deliberately do not take flock: both compiler protocols must
1429            // exclude reclamation, even on hosts where they are independent.
1430            lock_posix_session(&file).unwrap();
1431            fs::write(
1432                std::env::var_os("MAGI_TEST_SESSION_READY").unwrap(),
1433                "ready",
1434            )
1435            .unwrap();
1436            std::io::stdin().read_exact(&mut [0]).unwrap();
1437            return;
1438        }
1439        let (_temp, worktree, generation) = cargo_generation();
1440        drop(generation);
1441        let candidates = candidates(&worktree).unwrap();
1442        let session = candidates
1443            .files
1444            .iter()
1445            .find(|p| p.extension().is_some_and(|x| x == "lock"))
1446            .unwrap()
1447            .clone();
1448        drop(candidates);
1449        let ready = worktree.join("session-ready");
1450        let mut child = Command::new(std::env::current_exe().unwrap())
1451            .args([
1452                "--exact",
1453                "incremental::tests::fcntl_session_lock_is_respected_across_processes",
1454            ])
1455            .env("MAGI_TEST_SESSION_FILE", &session)
1456            .env("MAGI_TEST_SESSION_READY", &ready)
1457            .stdin(Stdio::piped())
1458            .stdout(Stdio::null())
1459            .spawn()
1460            .unwrap();
1461        let started = std::time::Instant::now();
1462        while !ready.exists() && started.elapsed() < std::time::Duration::from_secs(10) {
1463            if child.try_wait().unwrap().is_some() {
1464                break;
1465            }
1466            std::thread::sleep(std::time::Duration::from_millis(10));
1467        }
1468        let observed = ready.exists();
1469        let blocked = observed
1470            && super::candidates(&worktree)
1471                .err()
1472                .is_some_and(|e| e.to_string().contains("session is live"));
1473        if observed {
1474            child.stdin.take().unwrap().write_all(b"x").unwrap();
1475        } else {
1476            child.kill().ok();
1477        }
1478        let status = child.wait().unwrap();
1479        assert!(
1480            observed && status.success(),
1481            "session helper did not acquire its lock"
1482        );
1483        assert!(blocked, "POSIX session lock must exclude reclamation");
1484        assert!(!super::candidates(&worktree).unwrap().files.is_empty());
1485        assert!(session.exists());
1486    }
1487
1488    #[test]
1489    fn real_cargo_generations_require_exact_provenance_and_live_owner_exclusion() {
1490        let (_temp, worktree, generation) = cargo_generation();
1491        assert!(
1492            candidates(&worktree).is_err(),
1493            "compiler owner still holds the generation"
1494        );
1495        let path = generation.path.clone();
1496        drop(generation);
1497        let reclaim = candidates(&worktree).unwrap();
1498        let session = reclaim
1499            .files
1500            .iter()
1501            .find(|p| p.extension().is_some_and(|x| x == "lock"))
1502            .unwrap()
1503            .clone();
1504        drop(reclaim);
1505        let live_session = FileLease(
1506            OpenOptions::new()
1507                .read(true)
1508                .write(true)
1509                .open(session)
1510                .unwrap(),
1511        );
1512        crate::pr_head::try_lock_file(&live_session).unwrap();
1513        assert!(
1514            candidates(&worktree)
1515                .err()
1516                .unwrap()
1517                .to_string()
1518                .contains("session is live")
1519        );
1520        drop(live_session);
1521        let unknown = path.join("operator-note");
1522        fs::write(&unknown, "keep unknown data").unwrap();
1523        assert!(candidates(&worktree).is_err());
1524        assert_eq!(fs::read_to_string(&unknown).unwrap(), "keep unknown data");
1525        fs::remove_file(&unknown).unwrap();
1526        let unknown_dir = path.join("operator-empty");
1527        fs::create_dir(&unknown_dir).unwrap();
1528        assert!(candidates(&worktree).is_err());
1529        fs::remove_dir(unknown_dir).unwrap();
1530        let reclaim = candidates(&worktree).unwrap();
1531        assert!(!reclaim.files.is_empty());
1532        assert!(
1533            candidates(&worktree).is_err(),
1534            "maintenance retains session/owner locks"
1535        );
1536        // Partial deletion can resume, but changed bytes cannot be adopted.
1537        let removed = reclaim.files[0].clone();
1538        fs::remove_file(removed).unwrap();
1539        drop(reclaim);
1540        let reclaim = candidates(&worktree).unwrap();
1541        let changed = reclaim
1542            .files
1543            .iter()
1544            .find(|p| p.extension().is_some_and(|e| e == "bin"))
1545            .unwrap()
1546            .clone();
1547        let original = fs::read(&changed).unwrap();
1548        fs::write(&changed, b"modified proof input").unwrap();
1549        drop(reclaim);
1550        assert!(candidates(&worktree).is_err());
1551        fs::write(&changed, original).unwrap();
1552        let reclaim = candidates(&worktree).unwrap();
1553        for file in &reclaim.files {
1554            fs::remove_file(file).unwrap();
1555        }
1556        reclaim.finish().unwrap();
1557        assert!(!path.exists());
1558        assert!(candidates(&worktree).unwrap().files.is_empty());
1559        assert!(worktree.join("src/lib.rs").is_file());
1560    }
1561
1562    #[test]
1563    fn externally_removed_sealed_generation_does_not_strand_admission() {
1564        let (_temp, worktree, generation) = cargo_generation();
1565        let path = generation.path.clone();
1566        drop(generation);
1567        let reclaim = candidates(&worktree).unwrap();
1568        for file in &reclaim.files {
1569            fs::remove_file(file).unwrap();
1570        }
1571        // Simulate a clean/reset removing only empty cache directories, while
1572        // leaving the completion proof in the worktree Git directory.
1573        let mut dirs = directory_inventory(&path)
1574            .unwrap()
1575            .into_iter()
1576            .collect::<Vec<_>>();
1577        dirs.sort_by_key(|p| std::cmp::Reverse(p.components().count()));
1578        for relative in dirs {
1579            fs::remove_dir(path.join(relative)).unwrap();
1580        }
1581        fs::remove_dir(&path).unwrap();
1582        drop(reclaim);
1583        let parent = worktree.join("target/debug/incremental");
1584        let fresh = Generation::create(&worktree, &parent).unwrap();
1585        assert_eq!(
1586            fs::read_dir(git_dir(&worktree).unwrap().join(STORE))
1587                .unwrap()
1588                .filter_map(|e| e.ok())
1589                .filter(|e| e.path().extension().is_some_and(|x| x == "owner"))
1590                .count(),
1591            1
1592        );
1593        assert!(fresh.path.is_dir());
1594    }
1595
1596    #[test]
1597    fn unsealed_attempts_consume_the_bounded_generation_budget() {
1598        let temp = tempfile::tempdir().unwrap();
1599        let worktree = temp.path().canonicalize().unwrap();
1600        assert!(
1601            std::process::Command::new("git")
1602                .arg("init")
1603                .arg(&worktree)
1604                .output()
1605                .unwrap()
1606                .status
1607                .success()
1608        );
1609        let parent = worktree.join("target/debug/incremental");
1610        fs::create_dir_all(&parent).unwrap();
1611        for _ in 0..SLOTS {
1612            let generation = Generation::create(&worktree, &parent).unwrap();
1613            fs::write(generation.path.join("unknown"), "preserve").unwrap();
1614        }
1615        assert!(Generation::create(&worktree, &parent).is_err());
1616        assert!(
1617            candidates(&worktree).unwrap().files.is_empty(),
1618            "unsealed output never grants deletion authority"
1619        );
1620    }
1621
1622    #[cfg(unix)]
1623    #[test]
1624    fn generation_symlinks_and_replaced_inodes_refuse_reclamation() {
1625        let (_temp, worktree, generation) = cargo_generation();
1626        let path = generation.path.clone();
1627        drop(generation);
1628        let files = candidates(&worktree).unwrap().files;
1629        let file = files
1630            .iter()
1631            .find(|p| p.extension().is_some_and(|e| e == "bin"))
1632            .unwrap();
1633        let saved = worktree.join("retained-input");
1634        fs::rename(file, &saved).unwrap();
1635        std::os::unix::fs::symlink(&saved, file).unwrap();
1636        assert!(candidates(&worktree).is_err());
1637        fs::remove_file(file).unwrap();
1638        fs::copy(&saved, file).unwrap();
1639        assert!(
1640            candidates(&worktree).is_err(),
1641            "same bytes at a replaced inode are insufficient"
1642        );
1643        assert!(path.exists());
1644        assert!(saved.is_file());
1645    }
1646}