Skip to content

The agent that grows with you

refactor(skills): one same-root rank for skill_view and the catalog

kshitijk4poor905ef4efdf9d
diff --git a/agent/skill_utils.py b/agent/skill_utils.py
index f045427ed5b..a76c5cc1d24 100644
--- a/agent/skill_utils.py
+++ b/agent/skill_utils.py
@@ -459,6 +459,13 @@ def provably_same_skill(skill_mds) -> bool:
return False
+def skill_candidate_rank(skill_md, root) -> Tuple[bool, int]:
+ """Same-root order of identical copies: a real SKILL.md beats a legacy flat ``<name>.md``,
+ then the shallower path wins (shared by skill_view and :func:`resolve_skill_catalog`)."""
+ skill_md = Path(skill_md)
+ return (skill_md.name != "SKILL.md", len(skill_md.relative_to(root).parts))
+
+
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
@@ -495,8 +502,8 @@ def resolve_skill_catalog(entries: List[Dict[str, Any]]) -> List[Dict[str, Any]]
winner: Dict[str, Optional[int]] = {}
for alias, idxs in owners.items():
won, _ = pick_skill_candidate([
- (out[j]["tier"], str(out[j]["root"]), (False, len(PurePath(out[j]["relative_path"]).parts) + 1),
- out[j]["path"]) for j in idxs])
+ (out[j]["tier"], str(out[j]["root"]), skill_candidate_rank(out[j]["path"], out[j]["root"]), out[j]["path"])
+ for j in idxs])
winner[alias] = None if won is None else idxs[won]
for i, e in enumerate(out):
name, rel = str(e["name"]), e["relative_path"]
diff --git a/tools/skills_tool.py b/tools/skills_tool.py
index bce35724689..bdd512fa639 100644
--- a/tools/skills_tool.py
+++ b/tools/skills_tool.py
@@ -489,18 +489,12 @@ def _owning_search_dir(skill_md: Path, all_dirs) -> Optional[Path]:
return max(owners, key=lambda d: len(d.parts), default=None)
-def _rank_same_root_candidate(candidate, root: Path) -> tuple:
- """Real SKILL.md beats a legacy flat ``<name>.md``, then the shallower path wins."""
- _skill_dir, skill_md = candidate
- return (skill_md.name != "SKILL.md", len(skill_md.relative_to(root).parts))
-
-
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 AMBIGUOUS_SKILL_PREFIX, TIER_PROJECT, pick_skill_candidate
+ from agent.skill_utils import AMBIGUOUS_SKILL_PREFIX, TIER_PROJECT, pick_skill_candidate, skill_candidate_rank
all_dirs = [d for _t, d in roots]
if not all_dirs:
return _fail(
@@ -510,7 +504,7 @@ def _locate_skill(name: str, local_category_name: Optional[str], roots):
if len(candidates) > 1:
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])
+ (tier_of[root], str(root), skill_candidate_rank(c[1], 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 > "