Skip to main content

cleanup_stale_managed_skill_files

Function cleanup_stale_managed_skill_files 

Source
pub fn cleanup_stale_managed_skill_files(
    base_path: &Path,
    new_managed_paths: &BTreeSet<String>,
)
Expand description

Delete skill-derived files a PREVIOUS materialization wrote (per the on-disk manifest) but that are no longer part of new_managed_paths – e.g. a skill’s format switched between "prompt"/"agent-skill", or it was renamed/removed. Without this, the stale file stays on disk and Claude keeps treating it as active alongside the newly selected format (reagent P1 + Codex P1/P2, PR #2322).

MUST be called before writing the new files (so a deletion never races a write to the same path); callers must call write_managed_skill_file_manifest after writing to record the new set for the next materialization. Best-effort: any individual read/parse failure is treated as “no prior manifest” (nothing to clean up yet) rather than propagated, since a missing/corrupt manifest must never block the write that follows.

Every path is resolved through crate::backend::base::safe_join_within_base before deletion — defense in depth against a manifest path that somehow escapes the working directory (e.g. a future bypass of trigger sanitization upstream, or manual tampering with the manifest file itself); such a path is skipped with a warning, never followed.