From 6a5dd78ef22083cdc739cfd46b22a5623f36946f Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 16:49:53 +0300 Subject: [PATCH 1/3] feat(repo): prune dead protected-cache entries on save ProtectedCache::save skips entries whose file is gone from the checkout, so .sc/protected-cache and .sc/ws/cache- stay bounded by the protected files present. A lookup compares the keyed tag of plaintext read from the file, so an entry for an absent file is never consulted; dropping it costs at most one re-seal. Co-Authored-By: Claude Sonnet 5.5 --- ROADMAP.md | 9 ++++---- crates/repo/src/cache.rs | 49 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 9733f50a..261907cb 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -784,10 +784,11 @@ scale-&-reach horizon): - **`sc rewrap` on partial clones — refused (P37).** `Repo::rewrap` calls `refuse_on_partial`; a paths-only/streaming rebuild that would lift the refusal remains a possible follow-on. -- **Protected-cache hygiene (P33 final-review follow-on).** `.sc/protected-cache` - and `.sc/ws/cache-` entries for a deleted or renamed path are never - pruned — unbounded but benign growth (a stale entry just goes unmatched). - Periodic or commit-time pruning of dead entries is the follow-on. +- **Protected-cache hygiene — shipped.** `ProtectedCache::save` skips entries + whose file is no longer on disk (deleted, renamed, outside the sparse view), so + `.sc/protected-cache` and `.sc/ws/cache-` stay bounded by the protected + files actually present; a ws cache file is already removed with its session. + A dropped entry can only cost one re-seal if the path returns. - **Theirs-side resolve carry (P33 final-review follow-on).** `sc resolve --theirs` on a PROTECTED path records into the main-tree cache today, but completion's format-dispatched carry only consults the cache against the diff --git a/crates/repo/src/cache.rs b/crates/repo/src/cache.rs index c65a7cca..ad62911c 100644 --- a/crates/repo/src/cache.rs +++ b/crates/repo/src/cache.rs @@ -214,12 +214,22 @@ impl ProtectedCache { /// Durable atomic write via the shared `fsutil` helper (fsync temp + /// rename + fsync parent dir, no residue on failure); no-op for an /// ephemeral cache. + /// + /// Entries whose file is gone from the checkout (deleted, renamed, or + /// outside the sparse view) are left out, so the file stays bounded by the + /// protected files actually on disk instead of growing for the life of the + /// repo. A lookup always compares the keyed tag of plaintext read from the + /// file, so an entry for an absent file can never be consulted; dropping it + /// at worst costs one re-seal if the path returns. pub(crate) fn save(&self) -> Result<()> { let Some(path) = &self.path else { return Ok(()); }; let mut out = String::new(); for (p, e) in &self.entries { + if !self.root.join(p).is_file() { + continue; + } out.push_str(&format!( "{} {} {} {} {}\n", e.mtime_ns, @@ -387,6 +397,45 @@ mod tests { cleanup(&layout); } + #[test] + fn save_drops_entries_for_files_no_longer_on_disk() { + let layout = tmp("prune"); + let key = local_key(&layout).unwrap(); + let open = || { + ProtectedCache::open( + key, + layout.root.clone(), + Some(layout.protected_cache_path()), + ) + }; + for f in ["keep", "gone", "moved"] { + std::fs::write(layout.root.join(f), b"v").unwrap(); + } + let id = ObjectId::of(b"c"); + let mut c = open(); + for f in ["keep", "gone", "moved"] { + c.record(f, b"v", id); + } + c.save().unwrap(); + assert_eq!(open().entries.len(), 3); + + std::fs::remove_file(layout.root.join("gone")).unwrap(); + std::fs::rename(layout.root.join("moved"), layout.root.join("moved-to")).unwrap(); + // A directory at a cached path is not a protected file either. + std::fs::create_dir(layout.root.join("gone")).unwrap(); + let mut c = open(); + c.record("moved-to", b"v", id); + c.save().unwrap(); + + let after = open(); + assert_eq!(after.unchanged("keep", b"v"), Some(id)); + assert_eq!(after.unchanged("moved-to", b"v"), Some(id)); + assert_eq!(after.unchanged("gone", b"v"), None); + assert_eq!(after.unchanged("moved", b"v"), None); + assert_eq!(after.entries.len(), 2, "dead entries are pruned on save"); + cleanup(&layout); + } + #[test] fn ephemeral_cache_never_touches_disk() { let layout = tmp("ephemeral"); From 0d0925b35a4eee9080c2b93da0336963620b46f0 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 16:58:07 +0300 Subject: [PATCH 2/3] docs(repo): note pruning's root dependence and the re-seal's new blob id Co-Authored-By: Claude Sonnet 5.5 --- ROADMAP.md | 3 ++- crates/repo/src/cache.rs | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 261907cb..027214b8 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -788,7 +788,8 @@ scale-&-reach horizon): whose file is no longer on disk (deleted, renamed, outside the sparse view), so `.sc/protected-cache` and `.sc/ws/cache-` stay bounded by the protected files actually present; a ws cache file is already removed with its session. - A dropped entry can only cost one re-seal if the path returns. + A dropped entry can only cost one re-seal if the path returns (randomized + sealing, so unchanged content gets a new blob id). - **Theirs-side resolve carry (P33 final-review follow-on).** `sc resolve --theirs` on a PROTECTED path records into the main-tree cache today, but completion's format-dispatched carry only consults the cache against the diff --git a/crates/repo/src/cache.rs b/crates/repo/src/cache.rs index ad62911c..ec0e0f9a 100644 --- a/crates/repo/src/cache.rs +++ b/crates/repo/src/cache.rs @@ -111,7 +111,9 @@ struct CacheEntry { /// checkout dir. The keyed-tag comparison that decides every `unchanged()` /// hit does not depend on `root` at all (it hashes the caller's in-memory /// plaintext), so a mismatched root only ever loses the advisory stat -/// bookkeeping — never incorrectness. +/// bookkeeping — never incorrectness. `save` resolves entries against `root` +/// too, so a mismatched root would also prune every entry at the next save (a +/// full re-seal, still never incorrect). pub(crate) struct ProtectedCache { key: [u8; 32], /// Absolute base dir of the served checkout; `root.join(rel)` is the file @@ -220,7 +222,8 @@ impl ProtectedCache { /// protected files actually on disk instead of growing for the life of the /// repo. A lookup always compares the keyed tag of plaintext read from the /// file, so an entry for an absent file can never be consulted; dropping it - /// at worst costs one re-seal if the path returns. + /// at worst costs one re-seal if the path returns. Sealing is randomized, + /// so that re-seal yields a new blob id for unchanged content. pub(crate) fn save(&self) -> Result<()> { let Some(path) = &self.path else { return Ok(()); From 6a244bf55ffdc47a175fb654b500fdc61e7ee448 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Mon, 19 Oct 2026 17:02:58 +0300 Subject: [PATCH 3/3] fix(repo): prune cache entries behind a symlink Codex: is_file() follows symlinks, so a file replaced by a symlink (or under a symlinked directory) kept a dead entry the walker can never consult. Check each path component with symlink_metadata instead. Co-Authored-By: Claude Sonnet 5.5 --- crates/repo/src/cache.rs | 65 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/crates/repo/src/cache.rs b/crates/repo/src/cache.rs index ec0e0f9a..8a33f612 100644 --- a/crates/repo/src/cache.rs +++ b/crates/repo/src/cache.rs @@ -124,6 +124,31 @@ pub(crate) struct ProtectedCache { entries: BTreeMap, } +/// Whether `rel` under `root` is a regular file reached without crossing a +/// symlink, the only kind the worktree walker reads. Every component is checked +/// with `symlink_metadata`, so a file replaced by a symlink, or a directory +/// replaced by one, counts as gone. +fn is_plain_file(root: &Path, rel: &str) -> bool { + let mut cur = root.to_path_buf(); + let mut comps = rel.split('/').peekable(); + while let Some(c) = comps.next() { + cur.push(c); + let Ok(md) = std::fs::symlink_metadata(&cur) else { + return false; + }; + let ft = md.file_type(); + let ok = if comps.peek().is_some() { + ft.is_dir() + } else { + ft.is_file() + }; + if !ok { + return false; + } + } + true +} + impl ProtectedCache { pub(crate) fn open(key: [u8; 32], root: PathBuf, path: Option) -> ProtectedCache { let mut entries = BTreeMap::new(); @@ -230,7 +255,7 @@ impl ProtectedCache { }; let mut out = String::new(); for (p, e) in &self.entries { - if !self.root.join(p).is_file() { + if !is_plain_file(&self.root, p) { continue; } out.push_str(&format!( @@ -439,6 +464,44 @@ mod tests { cleanup(&layout); } + #[cfg(unix)] + #[test] + fn save_drops_entries_behind_a_symlink() { + let layout = tmp("prune-symlink"); + let key = local_key(&layout).unwrap(); + let open = || { + ProtectedCache::open( + key, + layout.root.clone(), + Some(layout.protected_cache_path()), + ) + }; + std::fs::create_dir(layout.root.join("d")).unwrap(); + std::fs::write(layout.root.join("d/inner"), b"v").unwrap(); + std::fs::write(layout.root.join("file"), b"v").unwrap(); + std::fs::write(layout.root.join("target"), b"v").unwrap(); + let id = ObjectId::of(b"c"); + let mut c = open(); + c.record("d/inner", b"v", id); + c.record("file", b"v", id); + c.save().unwrap(); + assert_eq!(open().entries.len(), 2); + + // The file becomes a symlink to an existing file, and its parent + // directory becomes a symlink to a directory that still holds `inner`. + std::fs::remove_file(layout.root.join("file")).unwrap(); + std::os::unix::fs::symlink("target", layout.root.join("file")).unwrap(); + std::fs::rename(layout.root.join("d"), layout.root.join("real-d")).unwrap(); + std::os::unix::fs::symlink("real-d", layout.root.join("d")).unwrap(); + open().save().unwrap(); + assert_eq!( + open().entries.len(), + 0, + "the walker never reads through a symlink" + ); + cleanup(&layout); + } + #[test] fn ephemeral_cache_never_touches_disk() { let layout = tmp("ephemeral");