-
Notifications
You must be signed in to change notification settings - Fork 361
fix: prevent deletion of instance-level skills via agent API #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jamiepine
merged 2 commits into
spacedriveapp:main
from
l33t0:fix/skills-remove-instance-guard
Mar 12, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -174,13 +174,29 @@ impl SkillSet { | |||||
|
|
||||||
| /// Remove a skill by name. | ||||||
| /// | ||||||
| /// Only workspace-level skills can be removed via this method. Instance-level | ||||||
| /// skills are shared across all agents and must not be deleted through the | ||||||
| /// per-agent API. | ||||||
| /// | ||||||
| /// Returns the base directory path if the skill was found and removed. | ||||||
| pub async fn remove(&mut self, name: &str) -> anyhow::Result<Option<PathBuf>> { | ||||||
| let skill = match self.skills.remove(&name.to_lowercase()) { | ||||||
| let key = name.to_lowercase(); | ||||||
| let skill = match self.skills.get(&key) { | ||||||
| Some(s) => s, | ||||||
| None => return Ok(None), | ||||||
| }; | ||||||
|
|
||||||
| if skill.source == SkillSource::Instance { | ||||||
| anyhow::bail!( | ||||||
| "cannot remove instance-level skill '{}' via the agent API; \ | ||||||
| instance skills are shared across all agents and must be \ | ||||||
| removed from the instance skills directory directly", | ||||||
| name | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
| let skill = self.skills.remove(&key).unwrap(); | ||||||
|
|
||||||
| // Remove the skill directory from disk | ||||||
| if skill.base_dir.exists() { | ||||||
| tokio::fs::remove_dir_all(&skill.base_dir) | ||||||
|
|
@@ -516,4 +532,77 @@ mod tests { | |||||
| let prompt = empty_set.render_worker_skills(&[], &engine).unwrap(); | ||||||
| assert!(prompt.is_empty()); | ||||||
| } | ||||||
|
|
||||||
| fn make_skill(name: &str, source: SkillSource) -> Skill { | ||||||
| Skill { | ||||||
| name: name.into(), | ||||||
| description: format!("{name} skill"), | ||||||
| file_path: PathBuf::from(format!("/skills/{name}/SKILL.md")), | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: hardcoding
Suggested change
|
||||||
| base_dir: PathBuf::from(format!("/tmp/test-skills-{}", uuid::Uuid::new_v4())), | ||||||
| content: format!("# {name}"), | ||||||
| source, | ||||||
| source_repo: None, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[tokio::test] | ||||||
| async fn remove_instance_skill_is_rejected() { | ||||||
| let mut set = SkillSet::default(); | ||||||
| set.skills.insert( | ||||||
| "my-skill".into(), | ||||||
| make_skill("my-skill", SkillSource::Instance), | ||||||
| ); | ||||||
|
|
||||||
| let result = set.remove("my-skill").await; | ||||||
| assert!(result.is_err()); | ||||||
| let msg = result.unwrap_err().to_string(); | ||||||
| assert!( | ||||||
| msg.contains("instance-level skill"), | ||||||
| "unexpected error: {msg}" | ||||||
| ); | ||||||
|
|
||||||
| // Skill should still be in the set (not removed) | ||||||
| assert!(set.skills.contains_key("my-skill")); | ||||||
| } | ||||||
|
|
||||||
| #[tokio::test] | ||||||
| async fn remove_workspace_skill_succeeds() { | ||||||
| let mut set = SkillSet::default(); | ||||||
| let skill = make_skill("my-skill", SkillSource::Workspace); | ||||||
| // Don't create the directory on disk - remove should still return the path | ||||||
| let expected_dir = skill.base_dir.clone(); | ||||||
| set.skills.insert("my-skill".into(), skill); | ||||||
|
|
||||||
| let result = set.remove("my-skill").await; | ||||||
| assert!(result.is_ok()); | ||||||
| let path = result.unwrap(); | ||||||
| assert_eq!(path, Some(expected_dir)); | ||||||
| assert!(!set.skills.contains_key("my-skill")); | ||||||
| } | ||||||
|
|
||||||
| #[tokio::test] | ||||||
| async fn remove_nonexistent_skill_returns_none() { | ||||||
| let mut set = SkillSet::default(); | ||||||
| let result = set.remove("nonexistent").await; | ||||||
| assert!(result.is_ok()); | ||||||
| assert!(result.unwrap().is_none()); | ||||||
| } | ||||||
|
|
||||||
| #[tokio::test] | ||||||
| async fn remove_is_case_insensitive() { | ||||||
| let mut set = SkillSet::default(); | ||||||
| set.skills.insert( | ||||||
| "my-skill".into(), | ||||||
| make_skill("my-skill", SkillSource::Instance), | ||||||
| ); | ||||||
|
|
||||||
| let result = set.remove("MY-SKILL").await; | ||||||
| assert!(result.is_err()); | ||||||
| assert!( | ||||||
| result | ||||||
| .unwrap_err() | ||||||
| .to_string() | ||||||
| .contains("instance-level skill") | ||||||
| ); | ||||||
| } | ||||||
| } | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String-matching on
anyhowtext to decide 403 feels brittle. Since you already loaded theSkillSet, you can checkskill.sourcedirectly and keep the status mapping independent of the exact error message.