The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(skills): resolve the frontmatter name _find_skill ignores
liuhao102419b5b411ab4c
diff --git a/tests/tools/test_skill_manager_tool.py b/tests/tools/test_skill_manager_tool.pyindex 9370adfecd0..c08dc0bce6b 100644--- a/tests/tools/test_skill_manager_tool.py+++ b/tests/tools/test_skill_manager_tool.py@@ -74,6 +74,20 @@ description: Use when deploying multi-region Kubernetes clusters with custom CNIStep 1."""+# Directory name deliberately differs from the frontmatter name — the+# mismatch scenario from #99609. Nothing at creation time forces the two+# to agree, and skills_list displays the frontmatter name.+MISMATCHED_NAME_CONTENT = """\+---+name: keeta-eng-conduct+description: Frontmatter name differs from the directory name.+---++# Mismatched Name Skill++Step 1: Do the thing under the other name.+"""+# ---------------------------------------------------------------------------# _validate_name@@ -164,6 +178,16 @@ class TestCreateSkill:assert result["success"] is Falseassert "already exists" in result["error"]+ def test_create_rejects_name_colliding_with_existing_frontmatter_name(self, tmp_path):+ """A new skill's directory name must not collide with an existing+ skill's displayed (frontmatter) name — skills_list dedups by that+ name, so the new skill would be silently hidden."""+ with _skill_dir(tmp_path):+ _create_skill("eng-conduct", MISMATCHED_NAME_CONTENT)+ result = _create_skill("keeta-eng-conduct", VALID_SKILL_CONTENT)+ assert result["success"] is False+ assert "already exists" in result["error"]+def test_create_rejects_category_traversal(self, tmp_path):skills_dir = tmp_path / "skills"skills_dir.mkdir()@@ -295,6 +319,53 @@ class TestEditSkill:assert found is not Noneassert found["path"] == tmp_path / "mlops" / "my-skill"+ def test_find_skill_accepts_frontmatter_name(self, tmp_path):+ """The name ``skills list`` displays must resolve in skill_manage.++ skills_list shows the frontmatter ``name:`` when it diverges from+ the directory name, but ``_find_skill`` matched only the directory+ name — so every mutating action called with the displayed name+ failed with a misleading "not found in active profile" error while+ the skill sat visibly enabled in the correct profile (#99609).+ """+ with _skill_dir(tmp_path):+ _create_skill(+ "eng-conduct", MISMATCHED_NAME_CONTENT, category="example-category"+ )+ found = _find_skill("keeta-eng-conduct")+ assert found is not None+ assert found["path"] == tmp_path / "example-category" / "eng-conduct"++ def test_edit_existing_skill_by_frontmatter_name(self, tmp_path):+ with _skill_dir(tmp_path):+ _create_skill(+ "eng-conduct", MISMATCHED_NAME_CONTENT, category="example-category"+ )+ result = _edit_skill("keeta-eng-conduct", VALID_SKILL_CONTENT_2)+ assert result["success"] is True, result.get("error")+ content = (tmp_path / "example-category" / "eng-conduct" / "SKILL.md").read_text()+ assert "Updated description" in content++ def test_find_skill_directory_name_wins_over_foreign_frontmatter(self, tmp_path):+ """One skill's frontmatter name must not shadow another skill's dir.++ The conflicting pair is written directly to disk: _create_skill's+ duplicate check now also consults the frontmatter fallback, so it+ refuses to create a skill whose directory name collides with an+ existing skill's frontmatter name — the same collision+ skills_list's seen-names dedup would silently hide.+ """+ claiming = tmp_path / "real-target"+ claiming.mkdir()+ (claiming / "SKILL.md").write_text(VALID_SKILL_CONTENT)+ claimed = tmp_path / "test-skill"+ claimed.mkdir()+ (claimed / "SKILL.md").write_text(MISMATCHED_NAME_CONTENT)+ with _skill_dir(tmp_path):+ found = _find_skill("test-skill")+ assert found is not None+ assert found["path"] == claimed+def test_edit_invalid_content_rejected(self, tmp_path):with _skill_dir(tmp_path):_create_skill("my-skill", VALID_SKILL_CONTENT)diff --git a/tools/skill_manager_tool.py b/tools/skill_manager_tool.pyindex 1dc0f1db4ff..18b0c3cd2de 100644--- a/tools/skill_manager_tool.py+++ b/tools/skill_manager_tool.py@@ -215,12 +215,44 @@ def _iter_skill_dirs(root: Path):yield skill_md.parent+def _read_frontmatter_name(skill_md: Path) -> Optional[str]:+ """Read a SKILL.md's frontmatter ``name:`` — the name skills_list displays.++ Fail-quiet on unreadable files: the fallback lookup in ``_find_skill``+ must never break the directory-name match, and an unreadable SKILL.md+ simply has no second name to offer. The value is truncated with the+ same ``MAX_NAME_LENGTH`` budget skills_list applies when displaying it.+ """+ try:+ from agent.skill_utils import parse_frontmatter++ content = skill_md.read_text(encoding="utf-8-sig", errors="replace")[:4000]+ frontmatter, _ = parse_frontmatter(content)+ except OSError:+ logger.debug("frontmatter read failed for %s", skill_md, exc_info=True)+ return None+ name = frontmatter.get("name")+ if isinstance(name, str):+ return name[:MAX_NAME_LENGTH]+ return None++def _find_skill(name: str) -> Optional[Dict[str, Any]]:"""Find a skill (local skills dir, then skills.external_dirs) -> ``{"path": Path}`` | None.Accepts the bare dir name (``axolotl``; matches category-nested skills too) and thecategorized relative path (``mlops/axolotl``) — the two forms skill_view resolves. The- categorized form matches RELATIVE to the local root only (relative_to raises for external dirs)."""+ categorized form matches RELATIVE to the local root only (relative_to raises for external dirs).++ As a last resort the frontmatter ``name:`` is matched too, so the name ``skills list``+ and the dashboard display (frontmatter name wins over the directory name there)+ resolves everywhere instead of failing with a misleading "not found in active+ profile" error when the two names diverge. Directory-name matches keep priority —+ a frontmatter match is only considered after the whole scan found no+ directory/categorized match, so one skill's frontmatter cannot shadow another+ skill's directory. A display name held by two or more distinct skills resolves to+ nothing: like skill_view's same-tier collision refusal, refusing to guess beats+ silently mutating the wrong skill; the directory name still resolves."""from agent.skill_utils import get_all_skills_dirslocal_root = Noneif "/" in name or "\\" in name:@@ -231,6 +263,8 @@ def _find_skill(name: str) -> Optional[Dict[str, Any]]:"skills dir resolve failed; categorized lookups fall back to the unresolved path",exc_info=True)local_root = _skills_dir()+ display_matches: List[Path] = []+ seen_display: set = set()for skills_dir in get_all_skills_dirs():if not skills_dir.exists():continue@@ -242,6 +276,20 @@ def _find_skill(name: str) -> Optional[Dict[str, Any]]:if (resolved.is_relative_to(local_root)and resolved.relative_to(local_root).as_posix() == name): # POSIX formreturn {"path": skill_dir}+ if _read_frontmatter_name(skill_dir / "SKILL.md") == name:+ key = skill_dir / "SKILL.md"+ with suppress(Exception):+ key = key.resolve()+ if key not in seen_display:+ seen_display.add(key)+ display_matches.append(skill_dir)+ if len(display_matches) == 1:+ return {"path": display_matches[0]}+ if display_matches:+ logger.warning(+ "Skill display name '%s' is ambiguous (%d skills claim it: %s) — refusing to guess; "+ "resolve it by directory name instead",+ name, len(display_matches), "; ".join(str(p) for p in display_matches))return None