Skip to content

The agent that grows with you

fix(skills): treat an unreadable target as occupied and undo with rmdir

kshitijk4poor958ee4c5aa5f
diff --git a/tests/tools/test_skill_create_occupied_directory.py b/tests/tools/test_skill_create_occupied_directory.py
index 6121aee9995..34f8f973b87 100644
--- a/tests/tools/test_skill_create_occupied_directory.py
+++ b/tests/tools/test_skill_create_occupied_directory.py
@@ -1,6 +1,8 @@
"""Ownership contracts for creating skills in occupied paths."""
from contextlib import contextmanager
+import os
+import sys
from unittest.mock import patch
from tools.skill_manager_tool import _create_skill
@@ -25,6 +27,11 @@ def _skill_dir(tmp_path):
yield
+def _can_make_unreadable_dir() -> bool:
+ # chmod 0 is ignored for root and has no directory-listing effect on Windows.
+ return sys.platform != "win32" and hasattr(os, "geteuid") and os.geteuid() != 0
+
+
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"
@@ -39,7 +46,19 @@ def test_occupied_directory_is_preserved_when_scan_blocks(tmp_path):
empty.mkdir()
empty_result = _create_skill("empty-skill", VALID_CONTENT)
+ # A directory create cannot even list is somebody's: refuse cleanly, never raise.
+ unreadable_result = None
+ if _can_make_unreadable_dir():
+ unreadable = tmp_path / "unreadable-skill"
+ unreadable.mkdir()
+ unreadable.chmod(0)
+ try:
+ unreadable_result = _create_skill("unreadable-skill", VALID_CONTENT)
+ finally:
+ unreadable.chmod(0o700)
+
assert result["success"] is False
+ assert "Choose another name" in result["error"]
assert data.read_bytes() == b"keep me"
assert target.is_dir()
assert not (target / "SKILL.md").exists()
@@ -48,6 +67,11 @@ def test_occupied_directory_is_preserved_when_scan_blocks(tmp_path):
assert empty.is_dir()
assert not any(empty.iterdir())
+ if unreadable_result is not None:
+ assert unreadable_result["success"] is False
+ assert "Choose another name" in unreadable_result["error"]
+ assert not (unreadable / "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):
diff --git a/tools/skill_manager_tool.py b/tools/skill_manager_tool.py
index 2b6875055aa..f26e343d95a 100644
--- a/tools/skill_manager_tool.py
+++ b/tools/skill_manager_tool.py
@@ -428,11 +428,16 @@ def _create_skill(name: str, content: str, category: str = None) -> Dict[str, An
if existing := _find_skill(name):
return _err(f"A skill named '{name}' already exists at {existing['path']}.")
skill_dir = _resolve_skill_dir(name, category)
- occupied_error = (f"Cannot create skill '{name}': refusing to adopt or delete an existing path "
- f"at {skill_dir}.")
+ occupied_error = (f"Cannot create skill '{name}': {skill_dir} already exists and is not an empty "
+ "directory. Choose another name, or move/remove that path and retry.")
# Only a directory create made (or an EMPTY leftover, e.g. from an earlier create whose
# SKILL.md write failed) may be used; anything with an entry is someone else's and is refused.
- if skill_dir.is_symlink() or (skill_dir.exists() and (not skill_dir.is_dir() or any(skill_dir.iterdir()))):
+ try:
+ occupied = skill_dir.is_symlink() or (
+ skill_dir.exists() and (not skill_dir.is_dir() or any(skill_dir.iterdir())))
+ except OSError: # unreadable / unstat-able (permissions, ACL): never adopt, never delete
+ occupied = True
+ if occupied:
return _err(occupied_error)
from hermes_constants import assert_named_profile_home_live
assert_named_profile_home_live(skill_dir)
@@ -447,10 +452,12 @@ def _create_skill(name: str, content: str, category: str = None) -> Dict[str, An
atomic_write_text(skill_md, content, preserve_mode=True, create_mode=0o644)
if scan_error := _security_scan_skill(skill_dir):
# Undo only what this call wrote: the SKILL.md, plus the directory if create made it.
- # An adopted (empty) directory stays in place — never rmtree a dir create did not make.
+ # An adopted (empty) directory stays in place. rmdir (not rmtree) so a file some other
+ # process dropped in meanwhile is never deleted — create wrote nothing but SKILL.md.
skill_md.unlink(missing_ok=True)
if created_dir:
- shutil.rmtree(skill_dir, ignore_errors=True)
+ with suppress(OSError):
+ skill_dir.rmdir()
return _err(scan_error)
root = _skills_dir() # display relative under the profile dir; absolute under skills.create_dir
display = skill_dir.relative_to(root) if skill_dir.is_relative_to(root) else skill_dir