The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(skills): refuse to adopt an occupied directory on create
joaomarcos63e30069fa6f
diff --git a/tests/tools/test_skill_create_occupied_directory.py b/tests/tools/test_skill_create_occupied_directory.pynew file mode 100644index 00000000000..2c05a394be9--- /dev/null+++ b/tests/tools/test_skill_create_occupied_directory.py@@ -0,0 +1,104 @@+"""Ownership contracts for creating skills in occupied paths."""++from contextlib import contextmanager+from unittest.mock import patch++from tools.skill_manager_tool import _create_skill+++VALID_CONTENT = """\+---+name: new-skill+description: Use when testing skill creation.+---++# New Skill++Create the thing.+"""+++@contextmanager+def _skill_dir(tmp_path):+ with patch("tools.skill_manager_tool.SKILLS_DIR", tmp_path), \+ patch("agent.skill_utils.get_all_skills_dirs", return_value=[tmp_path]):+ yield+++def test_occupied_directory_is_preserved_when_scan_blocks(tmp_path):+ with _skill_dir(tmp_path), patch("tools.skill_manager_tool._security_scan_skill", return_value="blocked"):+ target = tmp_path / "new-skill"+ target.mkdir()+ data = target / "data.bin"+ data.write_bytes(b"keep me")+ result = _create_skill("new-skill", VALID_CONTENT)++ assert result["success"] is False+ assert data.read_bytes() == b"keep me"+ assert target.is_dir()+ assert not (target / "SKILL.md").exists()+++def test_occupied_directory_is_preserved_when_scan_does_not_block(tmp_path):+ with _skill_dir(tmp_path), patch("tools.skill_manager_tool._security_scan_skill", return_value=None):+ target = tmp_path / "new-skill"+ target.mkdir()+ data = target / "data.bin"+ data.write_bytes(b"keep me")+ result = _create_skill("new-skill", VALID_CONTENT)++ assert result["success"] is False+ assert data.read_bytes() == b"keep me"+ assert target.is_dir()+ assert not (target / "SKILL.md").exists()+++def test_preexisting_skill_file_is_preserved_when_index_does_not_find_it(tmp_path):+ with _skill_dir(tmp_path), patch("tools.skill_manager_tool._find_skill", return_value=None):+ target = tmp_path / "new-skill"+ target.mkdir()+ skill_md = target / "SKILL.md"+ skill_md.write_bytes(b"original")+ result = _create_skill("new-skill", VALID_CONTENT)++ assert result["success"] is False+ assert skill_md.read_bytes() == b"original"+++def test_fresh_directory_is_removed_when_scan_blocks(tmp_path):+ with _skill_dir(tmp_path), patch("tools.skill_manager_tool._security_scan_skill", return_value="blocked"):+ result = _create_skill("new-skill", VALID_CONTENT)++ assert result["success"] is False+ assert not (tmp_path / "new-skill").exists()+++def test_fresh_directory_is_created_when_scan_does_not_block(tmp_path):+ with _skill_dir(tmp_path), patch("tools.skill_manager_tool._security_scan_skill", return_value=None):+ result = _create_skill("new-skill", VALID_CONTENT)++ assert result["success"] is True+ assert (tmp_path / "new-skill" / "SKILL.md").exists()+++def test_category_directory_collision_is_refused(tmp_path):+ with _skill_dir(tmp_path), patch("tools.skill_manager_tool._security_scan_skill", return_value=None):+ category = tmp_path / "category"+ category.mkdir()+ nested = category / "nested-skill.md"+ nested.write_bytes(b"nested")+ result = _create_skill("category", VALID_CONTENT)++ assert result["success"] is False+ assert nested.read_bytes() == b"nested"+ assert not (category / "SKILL.md").exists()+++def test_file_occupying_skill_path_is_not_deleted(tmp_path):+ with _skill_dir(tmp_path):+ target = tmp_path / "new-skill"+ target.write_bytes(b"do not delete")+ result = _create_skill("new-skill", VALID_CONTENT)++ assert result["success"] is False+ assert target.read_bytes() == b"do not delete"diff --git a/tools/skill_manager_tool.py b/tools/skill_manager_tool.pyindex ffbe713a520..7ad8c893c8a 100644--- a/tools/skill_manager_tool.py+++ b/tools/skill_manager_tool.py@@ -428,8 +428,16 @@ def _create_skill(name: str, content: str, category: str = None) -> Dict[str, Anif existing := _find_skill(name):return _err(f"A skill named '{name}' already exists at {existing['path']}.")skill_dir = _resolve_skill_dir(name, category)- from hermes_constants import mkdir_under_hermes_home- mkdir_under_hermes_home(skill_dir)+ occupied_error = (f"Cannot create skill '{name}': refusing to adopt or delete an existing path "+ f"at {skill_dir}.")+ if skill_dir.exists() or skill_dir.is_symlink():+ return _err(occupied_error)+ from hermes_constants import assert_named_profile_home_live+ assert_named_profile_home_live(skill_dir)+ try:+ skill_dir.mkdir(parents=True, exist_ok=False)+ except FileExistsError:+ return _err(occupied_error)skill_md = skill_dir / "SKILL.md"atomic_write_text(skill_md, content, preserve_mode=True, create_mode=0o644)if scan_error := _security_scan_skill(skill_dir):