The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(skills): skill_view applies cross-directory precedence instead of refusing
Tal Orbachf525ecdb9c2c
diff --git a/agent/skill_utils.py b/agent/skill_utils.pyindex 6b10f82436c..3ddd21235dd 100644--- a/agent/skill_utils.py+++ b/agent/skill_utils.py@@ -2,6 +2,7 @@Import-light by design: no tool registry, CLI config, or provider resolution."""import ast+import hashlibimport loggingimport osimport re@@ -415,15 +416,56 @@ def display_skill_create_dir() -> str:return create_dir.as_posix() + "/"+# Cross-directory precedence, lowest tier wins: trusted project > local profile > skills.create_dir >+# skills.external_dirs. Inside ONE tier two different skills sharing a name stay ambiguous — refused,+# never guessed (59da8ec4e) — while identical copies under one root resolve to the shallowest.+TIER_PROJECT, TIER_LOCAL, TIER_CREATE_DIR, TIER_EXTERNAL = range(4)+++def get_skill_search_roots(local: Optional[Path] = None, *, include_project: bool = True) -> List[Tuple[int, Path]]:+ """``(tier, dir)`` for every skill root in precedence order — the ONE ordering the skills list,+ prompt index, slash commands, skill_view, preload and cron share. *local* overrides the profile+ skills dir (skills_tool passes its live root); that entry is kept even when missing."""+ roots = [(TIER_PROJECT, d) for d in get_project_skills_dirs()] if include_project else []+ roots.append((TIER_LOCAL, Path(local) if local is not None else get_skills_dir()))+ create_dir = get_skill_create_dir()+ if create_dir is not None and create_dir.is_dir():+ roots.append((TIER_CREATE_DIR, create_dir))+ roots += [(TIER_EXTERNAL, d) for d in get_external_skills_dirs()]+ seen: Set[Path] = set()+ return [(t, d) for t, d in roots if not (d in seen or seen.add(d))]++def get_all_skills_dirs() -> List[Path]:"""Skill dirs: local ``~/.hermes/skills/`` first, then create_dir, then external.Trusted project dirs are NOT included (higher precedence; see get_project_skills_dirs)."""- dirs = [get_skills_dir()]- create_dir = get_skill_create_dir()- if create_dir is not None and create_dir.is_dir():- dirs.append(create_dir)- dirs.extend(d for d in get_external_skills_dirs() if d not in dirs)- return dirs+ return [d for _tier, d in get_skill_search_roots(include_project=False)]+++def provably_same_skill(skill_mds) -> bool:+ """True only when every path is the SAME skill: one resolved file (symlink view) or byte-identical+ content (copy). Anything else is two different skills sharing a name, and picking one by depth+ would let ``<root>/evil`` (``name: github``) shadow the real one."""+ try:+ if len({os.path.realpath(p) for p in skill_mds}) == 1:+ return True+ return len({hashlib.sha256(Path(p).read_bytes()).hexdigest() for p in skill_mds}) == 1+ except OSError:+ return False+++def pick_skill_candidate(candidates) -> Tuple[Optional[int], List[int]]:+ """Winner index among one identifier's ``(tier, root, rank, skill_md)`` candidates, plus the+ winning tier's contender indexes. The lowest tier wins; inside it a lone candidate wins, identical+ copies under one root resolve to the strictly best ``rank``, anything else is ambiguous (None)."""+ top = min(c[0] for c in candidates)+ contenders = [i for i, c in enumerate(candidates) if c[0] == top]+ if len(contenders) > 1 and len({candidates[i][1] for i in contenders}) == 1 and provably_same_skill(+ [candidates[i][3] for i in contenders]):+ ranked = sorted(contenders, key=lambda i: candidates[i][2])+ if candidates[ranked[0]][2] != candidates[ranked[1]][2]:+ return ranked[0], contenders+ return (contenders[0] if len(contenders) == 1 else None), contenders# Project-local skills (<root>/.hermes/skills, <root>/.agents/skills; root = nearestdiff --git a/tools/skills_tool.py b/tools/skills_tool.pyindex f4052b140bb..598388f904d 100644--- a/tools/skills_tool.py+++ b/tools/skills_tool.py@@ -4,7 +4,6 @@ holding SKILL.md (YAML frontmatter + instructions) plus optional references/, tescripts/. `skills_list` returns name/description only; `skill_view` returns full content andlinked files. Sibling modules (skills_tool_setup / _plugin / _dedup) re-export here."""-import hashlibimport jsonimport loggingimport os@@ -170,25 +169,24 @@ def _is_skill_disabled(name: str, platform: str = None) -> bool:return False-def _skill_search_dirs() -> Tuple[list, list, Path]:- """(project_dirs, all_dirs, active_skills_dir); trusted project-local dirs come FIRST so- first-wins dedup / the collision resolver prefer them."""- from agent.skill_utils import get_external_skills_dirs, get_project_skills_dirs- project_dirs = list(get_project_skills_dirs())+def _skill_search_dirs() -> Tuple[List[Tuple[int, Path]], Path]:+ """(``(tier, dir)`` roots in precedence order, active_skills_dir) — the shared+ ``agent.skill_utils.get_skill_search_roots`` order with the live profile dir (dropped if absent)."""+ from agent.skill_utils import TIER_LOCAL, get_skill_search_rootsactive_skills_dir = _skills_dir()- all_dirs = project_dirs + ([active_skills_dir] if active_skills_dir.exists() else [])- all_dirs += get_external_skills_dirs()- return project_dirs, all_dirs, active_skills_dir+ roots = [(t, d) for t, d in get_skill_search_roots(active_skills_dir)+ if t != TIER_LOCAL or d.exists()]+ return roots, active_skills_dirdef _find_all_skills(*, skip_disabled: bool = False) -> List[Dict[str, Any]]:- """All skills (name, description, category) across project/local/external dirs, first-wins- by name; cached per session. ``skip_disabled=True`` ignores disabled state (config UI)."""- from agent.skill_utils import iter_project_skill_files, iter_skill_index_files+ """All skills (name, description, category) across project/local/create_dir/external dirs,+ first-wins by name; cached per session. ``skip_disabled=True`` ignores disabled state (config UI)."""+ from agent.skill_utils import TIER_PROJECT, iter_project_skill_files, iter_skill_index_filescache_key = "with_disabled" if skip_disabled else "filtered"disabled = set() if skip_disabled else _get_disabled_skill_names()- project_dirs, dirs_to_scan, _ = _skill_search_dirs()- signature = _skills_scan_signature(dirs_to_scan, disabled)+ roots, _ = _skill_search_dirs()+ signature = _skills_scan_signature([d for _t, d in roots], disabled)now = time.monotonic()cached = _SKILLS_CACHE.get(cache_key)if cached is not None and cached[0] == signature and (now - cached[1]) < _SKILLS_CACHE_TTL_SECONDS:@@ -197,8 +195,8 @@ def _find_all_skills(*, skip_disabled: bool = False) -> List[Dict[str, Any]]:return [dict(s) for s in cached[2]]skills = []seen_names: set = set()- for scan_dir in dirs_to_scan: # project dirs go through the quarantine chokepoint- _iter = iter_project_skill_files if scan_dir in project_dirs else lambda d: iter_skill_index_files(d, "SKILL.md")+ for tier, scan_dir in roots: # project dirs go through the quarantine chokepoint+ _iter = iter_project_skill_files if tier == TIER_PROJECT else lambda d: iter_skill_index_files(d, "SKILL.md")for skill_md in _iter(scan_dir):if any(part in _EXCLUDED_SKILL_DIRS for part in skill_md.parts):continue@@ -488,59 +486,49 @@ def _rank_same_root_candidate(candidate, root: Path) -> tuple:return (skill_md.name != "SKILL.md", len(skill_md.relative_to(root).parts))-def _provably_same_skill(candidates) -> bool:- """True only when every candidate is the SAME skill: one resolved SKILL.md (symlink view)- or byte-identical content (copy). Anything else is two different skills sharing a name,- and picking one by depth would let ``<root>/evil`` (``name: github``) shadow the real one."""- try:- if len({os.path.realpath(smd) for _sd, smd in candidates}) == 1:- return True- return len({hashlib.sha256(smd.read_bytes()).hexdigest() for _sd, smd in candidates}) == 1- except OSError:- return False---def _locate_skill(name: str, local_category_name: Optional[str], project_dirs: list, all_dirs):- """Unique on-disk skill for *name*: collision refusal, project-tier precedence, same-root- precedence, quarantine gate, not-found listing. ``(error_json, skill_dir, skill_md)``;- skill_md set iff no error."""+def _locate_skill(name: str, local_category_name: Optional[str], roots):+ """Unique on-disk skill for *name* over ``(tier, dir)`` *roots*: cross-tier precedence+ (project > local > create_dir > external, shadowed copies logged), same-tier collision refusal,+ same-root identical-copy ranking, quarantine gate, not-found listing. ``(error_json, skill_dir,+ skill_md)``; skill_md set iff no error."""+ from agent.skill_utils import TIER_PROJECT, pick_skill_candidate+ all_dirs = [d for _t, d in roots]if not all_dirs:return _fail("Skills directory does not exist yet. It will be created on first install."), None, Nonecandidates = _collect_skill_candidates(name, local_category_name, all_dirs)- if len(candidates) > 1 and project_dirs:- # A project skill intentionally overrides a same-named local/external skill;- # ambiguity WITHIN the project tier (two different skills) still refuses.- candidates = [c for c in candidates if _under_any(c[1], project_dirs)] or candidates+ tier_of = {d: t for t, d in roots}if len(candidates) > 1:- # The refusal below guards against one skill silently shadowing another. Copies of ONE- # skill inside a single search dir (``<root>/x`` symlink view + ``<root>/cat/x`` copy)- # shadow nothing, so rank them instead; different content, an equal-rank tie or a- # cross-tier spread still refuses.- roots = {_owning_search_dir(smd, all_dirs) for _sd, smd in candidates}- if len(roots) == 1 and None not in roots and _provably_same_skill(candidates):- root = roots.pop()- ranked = sorted(candidates, key=lambda c: _rank_same_root_candidate(c, root))- if _rank_same_root_candidate(ranked[0], root) != _rank_same_root_candidate(ranked[1], root):- logger.info("Skill '%s': %d identical same-root copies, resolved to %s (duplicates: %s)",- name, len(candidates), ranked[0][1],- "; ".join(str(smd) for _sd, smd in ranked[1:]))- candidates = [ranked[0]]+ owned = [(c, _owning_search_dir(c[1], all_dirs)) for c in candidates]+ won, contenders = pick_skill_candidate([+ (tier_of[root], str(root), _rank_same_root_candidate(c, root), c[1]) for c, root in owned])+ if won is not None:+ if dropped := [str(smd) for i, (_sd, smd) in enumerate(candidates) if i != won]:+ logger.info("Skill '%s' resolved to %s by precedence (project > local > create_dir > "+ "external_dirs, then identical same-root copies); not loaded: %s",+ name, candidates[won][1], "; ".join(dropped))+ candidates = [candidates[won]]+ else:+ candidates = [candidates[i] for i in contenders]if len(candidates) > 1:paths = [str(smd) for _, smd in candidates]+ # A root-level copy's path IS the bare name, so it can't disambiguate itself.+ load_names = sorted({_owned_relative(sd, smd, all_dirs) for sd, smd in candidates} - {name})logger.warning("Skill name collision for '%s': %d candidates — %s", name, len(candidates), "; ".join(paths))return _fail(- f"Ambiguous skill name '{name}': {len(candidates)} skills match across your local skills dir "- "and external_dirs. Refusing to guess — load one explicitly by its categorized path.",- matches=paths,- hint="Pass the full relative path instead of the bare name (e.g., 'category/skill-name'), "- "or rename one of the colliding skills so each name is unique."), None, None+ f"Ambiguous skill name '{name}': "+ + (f"use one of {', '.join(load_names)}. " if load_names else "")+ + f"{len(candidates)} different skills share this name in the same skills directory tier; "+ "refusing to guess.",+ matches=paths, load_names=load_names,+ hint="Pass the exact relative path instead of the bare name, or rename one of the "+ "colliding skills so each name is unique."), None, Noneskill_dir, skill_md = candidates[0] if candidates else (None, None)# Quarantine gate: a project-tier skill with a dangerous scan verdict must not# load even by explicit name (same chokepoint the index and skills_list use).- if skill_md is not None and project_dirs:+ if skill_md is not None and tier_of.get(_owning_search_dir(skill_md, all_dirs)) == TIER_PROJECT:from agent.skill_utils import is_quarantined_project_skill- if _under_any(skill_md, project_dirs) and is_quarantined_project_skill(skill_md):+ if is_quarantined_project_skill(skill_md):return _fail(f"Project skill '{name}' is quarantined: the security scan flagged its content as ""dangerous. It will not load until the repo's skill content changes and passes a re-scan.",@@ -553,6 +541,13 @@ def _locate_skill(name: str, local_category_name: Optional[str], project_dirs: lreturn None, skill_dir, skill_md+def _owned_relative(skill_dir: Optional[Path], skill_md: Path, all_dirs) -> str:+ """Exact load path of a candidate: its skill dir (or flat ``.md`` stem) relative to its root."""+ root = _owning_search_dir(skill_md, all_dirs)+ target = skill_dir if skill_dir is not None else skill_md.with_suffix("")+ return target.relative_to(root).as_posix() if root is not None else str(target)++def _log_security_warnings(name: str, skill_md: Path, content: str, all_dirs, active_skills_dir):"""Warn (never block) when loaded from outside the trusted dirs (project + local + external)and/or when common prompt-injection patterns appear. The check is on the RESOLVED path:@@ -590,9 +585,9 @@ def skill_view(# since `bare` is not namespace-checked.if local_category_name and (lookup_error := _skill_lookup_path_error(local_category_name)):return _fail(lookup_error, hint=_LOOKUP_HINT)- project_dirs, all_dirs, active_skills_dir = _skill_search_dirs()- error, skill_dir, skill_md = _locate_skill(- name, local_category_name, project_dirs, all_dirs)+ roots, active_skills_dir = _skill_search_dirs()+ all_dirs = [d for _t, d in roots]+ error, skill_dir, skill_md = _locate_skill(name, local_category_name, roots)if error is not None:return errortry: # read once — reused for platform check and main content