Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 21 additions & 48 deletions crates/hk-core/src/store/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -194,7 +187,6 @@ impl Store {
*enabled,
*has_disabled_config,
*has_install_meta,
kind,
source_path.as_deref(),
source_reassigned,
)
Expand Down Expand Up @@ -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";

Expand All @@ -530,7 +520,6 @@ mod tests {
false,
true,
true,
skill,
Some(missing),
false
));
Expand All @@ -540,7 +529,6 @@ mod tests {
false,
true,
true,
skill,
Some(exists),
true
));
Expand All @@ -551,31 +539,19 @@ 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(
true,
true,
false,
true,
skill,
Some(missing),
false
));
Expand All @@ -585,7 +561,6 @@ mod tests {
true,
false,
true,
skill,
Some(exists),
false
));
Expand All @@ -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…
Expand All @@ -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]
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand Down
Loading