| 1 | //! Boot-time snapshot pruning. |
| 2 | //! |
| 3 | //! Called from `session_manager` once per session start. Failure is |
| 4 | //! never fatal — old snapshots taking disk space is annoying but not |
| 5 | //! correctness-breaking, so we log and move on. |
| 6 | |
| 7 | use std::io; |
| 8 | use std::path::Path; |
| 9 | use std::time::Duration; |
| 10 | |
| 11 | use super::repo::SnapshotRepo; |
| 12 | |
| 13 | /// Prune snapshots older than `max_age` for the given workspace. |
| 14 | /// |
| 15 | /// If no snapshot repo exists yet (first run) this is a cheap no-op. |
| 16 | /// Returns the number of snapshots removed. |
| 17 | pub fn prune_older_than(workspace: &Path, max_age: Duration) -> io::Result<usize> { |
| 18 | let Some(repo) = SnapshotRepo::open_existing(workspace)? else { |
| 19 | return Ok(0); |
| 20 | }; |
| 21 | let removed = repo.prune_older_than(max_age)?; |
| 22 | // `git prune --expire=now` walks the whole object store — seconds on a |
| 23 | // large snapshot repo. Nothing removed means nothing newly unreachable, |
| 24 | // so skip the walk entirely on the common boot path (#3757). |
| 25 | if removed > 0 { |
| 26 | repo.prune_unreachable_objects()?; |
| 27 | } |
| 28 | Ok(removed) |
| 29 | } |
| 30 | |
| 31 | #[cfg(test)] |
| 32 | mod tests { |
| 33 | use super::*; |
| 34 | use codewhale_runtime::test_support::{EnvVarGuard, lock_test_env}; |
| 35 | use tempfile::tempdir; |
| 36 | |
| 37 | #[test] |
| 38 | fn prune_no_repo_returns_zero() { |
| 39 | let tmp = tempdir().unwrap(); |
| 40 | let _lock = lock_test_env(); |
| 41 | let _profile = EnvVarGuard::set("CODEWHALE_HOME", tmp.path().join("profile")); |
| 42 | let removed = |
| 43 | prune_older_than(tmp.path(), codewhale_config::DEFAULT_SNAPSHOT_MAX_AGE).unwrap(); |
| 44 | assert_eq!(removed, 0); |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn prune_with_existing_repo_zero_age_clears_all() { |
| 49 | let tmp = tempdir().unwrap(); |
| 50 | let _lock = lock_test_env(); |
| 51 | let _profile = EnvVarGuard::set("CODEWHALE_HOME", tmp.path().join("profile")); |
| 52 | let workspace = tmp.path().join("ws"); |
| 53 | std::fs::create_dir_all(&workspace).unwrap(); |
| 54 | let repo = SnapshotRepo::open_or_init(&workspace).unwrap(); |
| 55 | std::fs::write(workspace.join("f.txt"), "x").unwrap(); |
| 56 | repo.snapshot("turn:0").unwrap(); |
| 57 | |
| 58 | // Same-second flake guard: see `repo::tests`. |
| 59 | std::thread::sleep(Duration::from_millis(1100)); |
| 60 | |
| 61 | let removed = prune_older_than(&workspace, Duration::from_secs(0)).unwrap(); |
| 62 | assert!(removed >= 1); |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn prune_with_uninitialized_git_dir_is_noop_without_creating_head() { |
| 67 | let tmp = tempdir().unwrap(); |
| 68 | let _lock = lock_test_env(); |
| 69 | let _profile = EnvVarGuard::set("CODEWHALE_HOME", tmp.path().join("profile")); |
| 70 | let workspace = tmp.path().join("ws"); |
| 71 | std::fs::create_dir_all(&workspace).unwrap(); |
| 72 | let git_dir = super::super::paths::snapshot_git_dir(&workspace).unwrap(); |
| 73 | std::fs::create_dir_all(&git_dir).unwrap(); |
| 74 | |
| 75 | let removed = prune_older_than(&workspace, Duration::from_secs(0)).unwrap(); |
| 76 | assert_eq!(removed, 0); |
| 77 | assert!( |
| 78 | !git_dir.join("HEAD").exists(), |
| 79 | "boot-time prune must not initialize a half-created side repo" |
| 80 | ); |
| 81 | } |
| 82 | } |
| 83 |