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