Skip to content
Closed
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions tests/tools/test_skills_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,31 @@ def test_bundle_content_hash_mixed_matches_on_disk(self, tmp_path):

assert bundle_content_hash(bundle) == content_hash(skill_dir)

def test_bundle_content_hash_matches_on_disk_when_file_and_dir_share_prefix(self, tmp_path):
"""Regression: file-vs-dir prefix collisions must not change hash ordering."""
from tools.skills_guard import content_hash

bundle = SkillBundle(
name="demo-skill",
files={
"SKILL.md": "same content\n",
"references/styles.md": "top-level style index\n",
"references/styles/blueprint.md": "blueprint\n",
},
source="github",
identifier="owner/repo/demo-skill",
trust_level="community",
)
skill_dir = tmp_path / "demo-skill"
skill_dir.mkdir()
(skill_dir / "SKILL.md").write_text("same content\n")
(skill_dir / "references").mkdir()
(skill_dir / "references" / "styles.md").write_text("top-level style index\n")
(skill_dir / "references" / "styles").mkdir()
(skill_dir / "references" / "styles" / "blueprint.md").write_text("blueprint\n")

assert bundle_content_hash(bundle) == content_hash(skill_dir)

def test_reports_update_when_remote_hash_differs(self):
lock = MagicMock()
lock.list_installed.return_value = [{
Expand Down
31 changes: 22 additions & 9 deletions tools/skills_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,15 +776,28 @@ def content_hash(skill_path: Path) -> str:
"""
h = hashlib.sha256()
if skill_path.is_dir():
for f in sorted(skill_path.rglob("*")):
if f.is_file():
try:
rel = f.relative_to(skill_path).as_posix()
h.update(rel.encode("utf-8"))
h.update(b"\x00")
h.update(f.read_bytes())
except OSError:
continue
file_entries = []
for f in skill_path.rglob("*"):
if not f.is_file():
continue
try:
file_entries.append((f.relative_to(skill_path).as_posix(), f))
except OSError:
continue

# Keep ordering symmetric with ``bundle_content_hash()``, which sorts
# by relative POSIX path strings. Sorting ``Path`` objects directly can
# disagree with pure string ordering when a skill has both a file and a
# nested directory sharing the same prefix (e.g. ``references/styles.md``
# and ``references/styles/blueprint.md``), producing false update
# detections for unchanged skills.
for rel, f in sorted(file_entries, key=lambda item: item[0]):
try:
h.update(rel.encode("utf-8"))
h.update(b"\x00")
h.update(f.read_bytes())
except OSError:
continue
elif skill_path.is_file():
h.update(skill_path.read_bytes())
return f"sha256:{h.hexdigest()[:16]}"
Expand Down