Skip to content

Fix: check every path component in is_protected, not just the first - #4

Open
Poid-ZA wants to merge 1 commit into
GarrickZ2:masterfrom
Poid-ZA:fix/protect-nested-paths
Open

Poid-ZA wants to merge 1 commit into
GarrickZ2:masterfrom
Poid-ZA:fix/protect-nested-paths

Conversation

@Poid-ZA

@Poid-ZA Poid-ZA commented Aug 14, 2026

Copy link
Copy Markdown

Problem

is_protected() only inspects the first component of the path relative to ~/.claude:

if let Some(first) = rel.components().next() {
    let name = first.as_os_str().to_string_lossy();
    return PROTECTED.iter().any(|p| *p == name.as_ref());
}

A protected name at any greater depth is therefore ignored. CLAUDE.md is in PROTECTED, but ~/.claude/projects/<slug>/CLAUDE.md has projects as its first component, so it is reported as unprotected and clean_directory() passes it to remove_file() once it is older than the expiry threshold. The same applies to agents/, commands/, and skills/ nested inside a project's data directory.

This is quiet: the file is simply gone after a clean, and because the Projects category is age-filtered rather than orphan-filtered for active projects, it can happen on a project you still use. Anything a user keeps under ~/.claude/projects/<slug>/ is exposed — I hit this with a directory of long-lived notes that had accumulated under a project slug.

Fix

Check every component instead of just the first. For a tool whose failure mode is deleting a file the user wanted to keep, matching too broadly is the safe direction to err in.

Tests

Four unit tests added:

  • protects_top_level_entries — existing behaviour preserved
  • protects_nested_entries — fails on the current implementation, passes with the fix
  • does_not_protect_ordinary_session_data — .jsonl transcripts and telemetry stay cleanable, so the fix does not over-protect
  • does_not_protect_paths_outside_claude_dir — strip_prefix miss still returns false

Verified locally against the current implementation:

test model::category::tests::protects_nested_entries ... FAILED
test result: FAILED. 3 passed; 1 failed

and with the fix applied:

test result: ok. 4 passed; 0 failed

cargo fmt --check and cargo clippy --all-targets -- -D warnings are both clean.

Note on scope

Deliberately limited to the protection bug. Two other things I noticed while reading, not touched here — happy to open separate issues if useful:

  1. DEFAULT_SELECTED = ALL pre-selects Plugins. Since cleaning is per-file and age-based, that shreds plugin git checkouts file by file (including inside .git) rather than uninstalling cleanly. On my machine that category was 1.4 GB of working tooling presented as reclaimable space.
  2. ConfigBackups is also pre-selected, so a default run deletes ~/.claude.json.backup* in the same pass that rewrites ~/.claude.json — the recovery artifact and the original are modified together.

is_protected() only inspected the first component of the path relative to
~/.claude, so a protected name at any greater depth was ignored. Files such
as projects/<slug>/CLAUDE.md or projects/<slug>/agents/*.md have `projects`
as their first component, so they were reported as unprotected and became
eligible for remove_file() during a clean.

Check every component instead. For a tool whose failure mode is deleting a
file the user wanted to keep, matching too broadly is the safe direction.

Adds unit tests covering top-level protection, nested protection, ordinary
session data staying cleanable, and paths outside claude_dir. The nested
test fails against the previous implementation.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant