From 402beac5f4d858d02ac04f3d75af5ec87f786413 Mon Sep 17 00:00:00 2001 From: RealZST Date: Sun, 29 Nov 2026 18:34:11 +0800 Subject: [PATCH] fix(store): prune a CLI row once its binary is uninstalled A CLI row was never pruned: every CLI row carries install metadata after its first scan, and such rows were exempt from stale cleanup. The row now follows the rule other file-backed rows use: kept while its recorded binary is still on disk, pruned once it is gone. Co-Authored-By: Claude Fable 5.2 --- crates/hk-core/src/store/sync.rs | 69 ++++++++++---------------------- 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/crates/hk-core/src/store/sync.rs b/crates/hk-core/src/store/sync.rs index 5c88e384..e501f596 100644 --- a/crates/hk-core/src/store/sync.rs +++ b/crates/hk-core/src/store/sync.rs @@ -34,17 +34,17 @@ impl Store { /// agent was not visited (detection can fail transiently). This is the /// same predicate `UPSERT_EXTENSION_SQL` uses to stop a scan from /// overwriting `enabled`; - /// - CLI extensions with install_meta — their binary can transiently fail - /// detection on startup, so one missing scan isn't proof of removal; /// - file-backed install_meta rows still on disk (or whose path is unknown) /// — a momentary scan gap, not a real uninstall — unless the scan now /// reports that file under another id (`source_reassigned`): the file /// has a new row, and this one would otherwise outlive it as a duplicate - /// for as long as the file exists. + /// for as long as the file exists. A CLI row's file is its binary, which + /// a scan can miss while it is installed: a desktop app's `PATH` lacks + /// the entries a shell profile adds. /// - /// Pruned (returns true): everything else that is gone, including skill and - /// plugin rows with install_meta whose files the user deleted (e.g. - /// `rm -rf ~/.claude`) — otherwise they linger forever as ghost rows. + /// Pruned (returns true): everything else that is gone, including rows + /// with install_meta whose files the user deleted (e.g. `rm -rf ~/.claude`, + /// or uninstalling a CLI) — otherwise they linger forever as ghost rows. /// /// Disabled *skills* fall in that last group by design: they carry no /// `disabled_config`, and a disabled skill still on disk is reported by the @@ -57,7 +57,6 @@ impl Store { enabled: bool, has_disabled_config: bool, has_install_meta: bool, - kind: &str, source_path: Option<&str>, source_reassigned: bool, ) -> bool { @@ -67,15 +66,9 @@ impl Store { if !agent_scanned { return true; } - if has_install_meta { - if kind == ExtensionKind::Cli.as_str() { - return false; - } - if !source_reassigned && source_path.is_none_or(Self::source_still_on_disk) { - return false; - } - } - true + !(has_install_meta + && !source_reassigned + && source_path.is_none_or(Self::source_still_on_disk)) } /// Was the row's agent among the ones the scan visited? `None` means the /// caller does not know (a partial sync), which counts as visited. A row @@ -194,7 +187,6 @@ impl Store { *enabled, *has_disabled_config, *has_install_meta, - kind, source_path.as_deref(), source_reassigned, ) @@ -516,10 +508,8 @@ mod tests { } #[test] fn test_stale_row_should_prune_decision() { - // Args: (agent_scanned, enabled, has_disabled_config, has_install_meta, kind, + // Args: (agent_scanned, enabled, has_disabled_config, has_install_meta, // source_path, source_reassigned) - let cli = ExtensionKind::Cli.as_str(); - let skill = ExtensionKind::Skill.as_str(); let exists = env!("CARGO_MANIFEST_DIR"); // guaranteed to exist let missing = "/nonexistent/harnesskit/ghost/SKILL.md"; @@ -530,7 +520,6 @@ mod tests { false, true, true, - skill, Some(missing), false )); @@ -540,7 +529,6 @@ mod tests { false, true, true, - skill, Some(exists), true )); @@ -551,23 +539,12 @@ mod tests { false, false, true, - skill, Some(missing), false )); // Sourceless rows (no install_meta) are pruned when gone — prior behavior. assert!(Store::stale_row_should_prune( - true, true, false, false, skill, None, false - )); - // CLI with install_meta is kept even when absent (flaky binary detection). - assert!(!Store::stale_row_should_prune( - true, - true, - false, - true, - cli, - Some(missing), - false + true, true, false, false, None, false )); // File-backed install_meta row whose file is gone → pruned (the ghost fix). assert!(Store::stale_row_should_prune( @@ -575,7 +552,6 @@ mod tests { true, false, true, - skill, Some(missing), false )); @@ -585,7 +561,6 @@ mod tests { true, false, true, - skill, Some(exists), false )); @@ -596,13 +571,12 @@ mod tests { true, false, true, - skill, Some(exists), true )); // Unknown source_path → kept (can't prove removal). assert!(!Store::stale_row_should_prune( - true, true, false, true, skill, None, false + true, true, false, true, None, false )); // Agent not visited by the scan: the on-disk exemptions are void, so a // file-backed install_meta row is pruned even though its file exists… @@ -611,17 +585,16 @@ mod tests { true, false, true, - skill, Some(exists), false )); - // …and so is a CLI row with install_meta; only store-held disabled - // state survives. + // …and so is one whose path is unknown; only store-held disabled state + // survives. assert!(Store::stale_row_should_prune( - false, true, false, true, cli, None, false + false, true, false, true, None, false )); assert!(!Store::stale_row_should_prune( - false, false, true, false, skill, None, false + false, false, true, false, None, false )); } #[test] @@ -728,8 +701,8 @@ mod tests { // Regression: a marketplace/git-installed skill whose files the user // deleted (e.g. `rm -rf ~/.claude`) used to linger forever because any // install_meta row was exempt from stale cleanup. It should now be - // pruned once its source_path is gone, while a CLI with install_meta and - // a skill whose file still exists are both kept. + // pruned once its source_path is gone, as should an uninstalled CLI, + // while a skill whose file still exists is kept. let (store, dir) = test_store(); let meta = || { Some(InstallMeta { @@ -763,7 +736,7 @@ mod tests { live.install_meta = meta(); store.insert_extension(&live).unwrap(); - // CLI with install_meta — binary detection is flaky, must stay. + // CLI whose binary was uninstalled. let mut cli = sample_extension(); cli.id = "cli-tool".into(); cli.name = "cli-tool".into(); @@ -823,8 +796,8 @@ mod tests { "skill whose file still exists should be kept" ); assert!( - ids.contains(&"cli-tool".to_string()), - "CLI with install_meta should be kept" + !ids.contains(&"cli-tool".to_string()), + "CLI whose binary is gone should be pruned" ); } #[test]