Skip to content

The agent that grows with you

refactor(skills_hub): shared _tree_members walker (github+official), state files declare DEFAULT_PATH

Teknium3ba710048636
diff --git a/tools/skills_hub.py b/tools/skills_hub.py
index 224e3b6d817..7aecdad9697 100644
--- a/tools/skills_hub.py
+++ b/tools/skills_hub.py
@@ -208,19 +208,16 @@ def _write_index_cache(key: str, data: Any) -> None:
# ---------------------------------------------------------------------------
class _JsonStateFile:
- """A JSON file under the hub dir with a fixed empty shape."""
+ """A JSON file under the hub dir with a fixed empty shape (``EMPTY``, deep-copied
+ on every miss/corrupt read); ``DEFAULT_PATH`` is the hub path resolver."""
EMPTY: dict = {}
+ DEFAULT_PATH: Any = None
def __init__(self, path: Optional[Path] = None):
- self.path = path if path is not None else self._default_path()
-
- def _default_path(self) -> Path:
- raise NotImplementedError
+ self.path = path if path is not None else type(self).DEFAULT_PATH()
def _read(self) -> dict:
- if not self.path.exists():
- return json.loads(json.dumps(self.EMPTY))
try:
return json.loads(self.path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
@@ -235,9 +232,7 @@ class HubLockFile(_JsonStateFile):
"""skills/.hub/lock.json — provenance of installed hub skills."""
EMPTY = {"version": 1, "installed": {}}
-
- def _default_path(self) -> Path:
- return _lock_file()
+ DEFAULT_PATH = staticmethod(_lock_file)
def load(self) -> dict:
return self._read()
@@ -295,9 +290,7 @@ class TapsManager(_JsonStateFile):
"""skills/.hub/taps.json — custom GitHub repo sources."""
EMPTY = {"taps": []}
-
- def _default_path(self) -> Path:
- return _taps_file()
+ DEFAULT_PATH = staticmethod(_taps_file)
def load(self) -> List[dict]:
return self._read().get("taps", [])
@@ -323,8 +316,7 @@ class TapsManager(_JsonStateFile):
self.save(new_taps)
return True
- def list_taps(self) -> List[dict]:
- return self.load()
+ list_taps = load
def append_audit_log(action: str, skill_name: str, source: str,
@@ -360,13 +352,3 @@ def ensure_hub_dirs() -> None:
if not path.exists():
path.write_text(initial, encoding="utf-8")
-
-# ---------------------------------------------------------------------------
-# Hermes centralized index (data source for HermesIndexSource)
-# ---------------------------------------------------------------------------
-
-
-# ---------------------------------------------------------------------------
-# Source router + parallel search
-# ---------------------------------------------------------------------------
-
diff --git a/tools/skills_hub_github.py b/tools/skills_hub_github.py
index d80f8a224d1..ee54755644d 100644
--- a/tools/skills_hub_github.py
+++ b/tools/skills_hub_github.py
@@ -195,6 +195,19 @@ def _skip_bundle_file(rel_path: str) -> bool:
return base.startswith(".") or base.endswith(".pyc") or "__pycache__" in rel_path.split("/")
+def _tree_members(entries: List[dict], prefix: str):
+ """``(rel_path, item_path, is_regular_blob)`` for every git-tree entry under ``prefix``.
+
+ Symlinks (mode 120000) and non-blobs report ``is_regular_blob=False`` so callers
+ can reject a SKILL.md-linked symlink instead of silently following it.
+ """
+ for item in entries:
+ item_path = item.get("path", "")
+ if item_path.startswith(prefix):
+ regular = item.get("type") == "blob" and item.get("mode") != "120000"
+ yield item_path[len(prefix):], item_path, regular
+
+
class GitHubSource(SkillSource):
"""Fetch skills from GitHub repos via the Contents API."""
@@ -330,12 +343,8 @@ class GitHubSource(SkillSource):
"""
prefix = f"{skill_path}/"
symlinked: set = set()
- for item in entries:
- item_path = item.get("path", "")
- if not item_path.startswith(prefix):
- continue
- rel_path = item_path[len(prefix):]
- if item.get("type") != "blob" or item.get("mode") == "120000":
+ for rel_path, item_path, regular in _tree_members(entries, prefix):
+ if not regular:
symlinked.add(rel_path)
continue
if rel_path == "SKILL.md" or _skip_bundle_file(rel_path):
diff --git a/tools/skills_hub_official.py b/tools/skills_hub_official.py
index b98e1ef031c..e90993b4d63 100644
--- a/tools/skills_hub_official.py
+++ b/tools/skills_hub_official.py
@@ -5,7 +5,7 @@ from pathlib import Path, PurePosixPath
from typing import Dict, List, Optional, Tuple, Union
from agent.skill_utils import is_excluded_skill_path
-from tools.skills_hub_github import GitHubAuth, GitHubSource, _skip_bundle_file
+from tools.skills_hub_github import GitHubAuth, GitHubSource, _skip_bundle_file, _tree_members
from tools.skills_hub_models import (
SkillBundle, SkillMeta, SkillSource, _hermes_tags, _matches_query, _memo_json, _parse_frontmatter, hub,
)
@@ -221,18 +221,9 @@ class OptionalSkillSource(SkillSource):
tree = github._get_repo_tree(self.OFFICIAL_REPO)
if tree is None:
return None
- _branch, entries = tree
- prefix = f"{repo_path}/"
files: Dict[str, Union[str, bytes]] = {}
- for item in entries:
- item_path = item.get("path", "")
- if (
- item.get("type") != "blob" or item.get("mode") == "120000"
- or not item_path.startswith(prefix)
- ):
- continue
- rel_file = item_path[len(prefix):]
- if _skip_bundle_file(rel_file):
+ for rel_file, item_path, regular in _tree_members(tree[1], f"{repo_path}/"):
+ if not regular or _skip_bundle_file(rel_file):
continue
content = github._fetch_file_bytes(self.OFFICIAL_REPO, item_path)
if content is None: