Repository navigation
fix(kanban): validate --skill against the worker profile's skill registry at create time #44101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7613,6 +7613,152 @@ def _resolve_hermes_argv() -> list[str]: | |
| return _module_hermes_argv() | ||
|
|
||
|
|
||
| def _kanban_worker_skill_available(hermes_home: Optional[str]) -> bool: | ||
| """True if the bundled ``kanban-worker`` skill resolves for the home the | ||
| spawned worker will run under. | ||
|
|
||
| The dispatcher injects ``--skills kanban-worker`` into every worker. When | ||
| the worker activates a profile (``hermes -p <name>``), its ``SKILLS_DIR`` | ||
| becomes ``<profile_home>/skills`` — which on many profiles does NOT contain | ||
| the bundled skill (it ships in the *default* root home, not every | ||
| profile-scoped skills dir). Preloading a missing skill is fatal at CLI | ||
| startup (``ValueError: Unknown skill(s): kanban-worker``), aborting the | ||
| worker before the agent loop runs. Gate the flag on actual resolvability; | ||
| the kanban lifecycle contract is still injected via ``KANBAN_GUIDANCE``, so | ||
| omitting the flag only drops the supplementary pattern library. | ||
| """ | ||
| from pathlib import Path as _Path | ||
|
|
||
| # An unset HERMES_HOME means the worker falls back to the default root | ||
| # home (``~/.hermes``), which ships the bundled skill. | ||
| base = _Path(hermes_home) if hermes_home else (_Path.home() / ".hermes") | ||
| skills_root = base / "skills" | ||
| if not skills_root.is_dir(): | ||
| return False | ||
| # Canonical bundled location first (cheap), then a bounded scan for | ||
| # profiles that have it nested elsewhere. | ||
| if (skills_root / "devops" / "kanban-worker" / "SKILL.md").is_file(): | ||
| return True | ||
| try: | ||
| for skill_md in skills_root.rglob("kanban-worker/SKILL.md"): | ||
| if skill_md.is_file(): | ||
| return True | ||
| except OSError: | ||
| pass | ||
| return False | ||
|
|
||
|
|
||
| def _skill_search_roots(hermes_home: Optional[str]) -> list: | ||
| """Skill directories a worker spawned under *hermes_home* would search. | ||
|
|
||
| Mirrors the roots ``skill_view()`` scans: ``<home>/skills`` plus any | ||
| ``skills.external_dirs`` from that home's ``config.yaml``. The config is | ||
| read directly (not via the in-process loader) because the validating CLI | ||
| may be running under a *different* HERMES_HOME than the worker will. | ||
| """ | ||
| from pathlib import Path as _Path | ||
|
|
||
| base = _Path(hermes_home) if hermes_home else (_Path.home() / ".hermes") | ||
| roots = [] | ||
| skills_root = base / "skills" | ||
| if skills_root.is_dir(): | ||
| roots.append(skills_root) | ||
| config_path = base / "config.yaml" | ||
| if config_path.is_file(): | ||
| try: | ||
| import yaml | ||
|
|
||
| with open(config_path, "r", encoding="utf-8") as fh: | ||
| cfg = yaml.safe_load(fh) or {} | ||
| external = (cfg.get("skills") or {}).get("external_dirs") or [] | ||
| for raw in external: | ||
| if not isinstance(raw, str) or not raw.strip(): | ||
| continue | ||
| ext = _Path(os.path.expandvars(raw)).expanduser() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relative |
||
| if ext.is_dir(): | ||
| roots.append(ext) | ||
| except Exception: | ||
| # Unreadable/malformed config — validate against <home>/skills | ||
| # only rather than failing the create. | ||
| pass | ||
| return roots | ||
|
|
||
|
|
||
| _SKILL_FRONTMATTER_NAME = re.compile(r"^name:\s*['\"]?([^'\"\n]+?)['\"]?\s*$", re.MULTILINE) | ||
|
|
||
|
|
||
| def unresolvable_task_skills( | ||
| skills: Iterable[str], | ||
| hermes_home: Optional[str] = None, | ||
| ) -> list: | ||
| """Return the subset of *skills* that won't resolve for a worker under | ||
| *hermes_home* (``None`` → the default root home). | ||
|
|
||
| Best-effort mirror of ``skill_view()``'s lookup so ``kanban create`` can | ||
| reject tasks whose force-loaded skills would crash the worker at startup | ||
| (``ValueError: Unknown skill(s): ...``) and burn the retry budget (#44072). | ||
|
|
||
| Deliberately conservative — a name is only reported when *every* cheap | ||
| strategy misses: directory named ``<name>`` containing ``SKILL.md`` (at | ||
| the root or nested), relative-path form ``cat/name``, legacy flat | ||
| ``<name>.md``, and frontmatter ``name:`` aliases. Names this function | ||
| can't reliably check cross-profile are given the benefit of the doubt: | ||
| plugin-qualified ``namespace:skill`` names (plugin registries are | ||
| per-home state) and the built-in ``kanban-worker`` (the dispatcher | ||
| already gates it on resolvability and drops it when absent). | ||
| """ | ||
| roots = _skill_search_roots(hermes_home) | ||
| missing = [] | ||
| seen = set() | ||
| for raw in skills or (): | ||
| name = (raw or "").strip() | ||
| if not name or name in seen: | ||
| continue | ||
| seen.add(name) | ||
| if name == "kanban-worker" or ":" in name: | ||
| continue | ||
| if not roots: | ||
| # No skills dir at all — every concrete name is unresolvable. | ||
| missing.append(name) | ||
| continue | ||
| if not _skill_resolves(name, roots): | ||
| missing.append(name) | ||
| return missing | ||
|
|
||
|
|
||
| def _skill_resolves(name: str, roots: list) -> bool: | ||
| """True if *name* matches any skill under any of *roots*.""" | ||
| for root in roots: | ||
| direct = root / name | ||
| try: | ||
| if (direct / "SKILL.md").is_file(): | ||
| return True | ||
| if direct.with_suffix(".md").is_file(): | ||
| return True | ||
| except (OSError, ValueError): | ||
| continue | ||
| # Bare-name lookups: nested dir name, frontmatter alias, flat .md. | ||
| if "/" in name or "\\" in name: | ||
| continue | ||
| try: | ||
| for skill_md in root.rglob("SKILL.md"): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the shared indexed-skill iteration semantics here: raw |
||
| if skill_md.parent.name == name: | ||
| return True | ||
| try: | ||
| head = skill_md.read_text(encoding="utf-8", errors="replace")[:4096] | ||
| except OSError: | ||
| continue | ||
| match = _SKILL_FRONTMATTER_NAME.search(head) | ||
| if match and match.group(1).strip() == name: | ||
| return True | ||
| for flat_md in root.rglob(f"{name}.md"): | ||
| if flat_md.name != "SKILL.md" and flat_md.is_file(): | ||
| return True | ||
| except OSError: | ||
| continue | ||
| return False | ||
|
|
||
|
|
||
| def _worker_terminal_timeout_env( | ||
| max_runtime_seconds: Optional[int], | ||
| current_timeout: Optional[str], | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| """Regression tests for #44072: ``kanban create --skill`` must validate | ||
| skill names at dispatch time. | ||
|
|
||
| Workers preload ``--skill`` values at CLI startup and crash with | ||
| ``ValueError: Unknown skill(s): <name>`` on unknown names — burning the | ||
| task's retry budget and completing with an empty result. ``kanban create`` | ||
| now rejects tasks whose force-loaded skills won't resolve for the worker's | ||
| profile home, before the task is enqueued. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from hermes_cli import kanban as kc | ||
| from hermes_cli import kanban_db as kb | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def kanban_home(tmp_path, monkeypatch): | ||
| home = tmp_path / ".hermes" | ||
| home.mkdir() | ||
| monkeypatch.setenv("HERMES_HOME", str(home)) | ||
| monkeypatch.setattr(Path, "home", lambda: tmp_path) | ||
| kb.init_db() | ||
| return home | ||
|
|
||
|
|
||
| def _install_skill(root: Path, rel_dir: str, frontmatter_name: str | None = None) -> None: | ||
| skill_dir = root / "skills" / rel_dir | ||
| skill_dir.mkdir(parents=True, exist_ok=True) | ||
| name = frontmatter_name or rel_dir.rsplit("/", 1)[-1] | ||
| (skill_dir / "SKILL.md").write_text( | ||
| f"---\nname: {name}\ndescription: test skill\n---\n\n# {name}\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
|
|
||
| def _task_count() -> int: | ||
| with kb.connect() as conn: | ||
| return len(kb.list_tasks(conn)) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # unresolvable_task_skills (unit) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_unknown_skill_reported(kanban_home): | ||
| _install_skill(kanban_home, "devops/real-skill") | ||
| missing = kb.unresolvable_task_skills( | ||
| ["real-skill", "no-such-skill"], str(kanban_home) | ||
| ) | ||
| assert missing == ["no-such-skill"] | ||
|
|
||
|
|
||
| def test_relative_path_form_resolves(kanban_home): | ||
| _install_skill(kanban_home, "devops/nested-skill") | ||
| assert kb.unresolvable_task_skills( | ||
| ["devops/nested-skill"], str(kanban_home) | ||
| ) == [] | ||
|
|
||
|
|
||
| def test_frontmatter_name_alias_resolves(kanban_home): | ||
| # Dir is "short-alias" but the frontmatter name is what skills_list | ||
| # exposes — skill_view accepts it, so the validator must too. | ||
| _install_skill(kanban_home, "cat/short-alias", frontmatter_name="long-public-name") | ||
| assert kb.unresolvable_task_skills( | ||
| ["long-public-name"], str(kanban_home) | ||
| ) == [] | ||
|
|
||
|
|
||
| def test_legacy_flat_md_resolves(kanban_home): | ||
| flat = kanban_home / "skills" / "misc" | ||
| flat.mkdir(parents=True) | ||
| (flat / "oldstyle.md").write_text("# oldstyle\n", encoding="utf-8") | ||
| assert kb.unresolvable_task_skills(["oldstyle"], str(kanban_home)) == [] | ||
|
|
||
|
|
||
| def test_kanban_worker_and_plugin_names_get_benefit_of_doubt(kanban_home): | ||
| # kanban-worker is gated by the dispatcher itself (dropped when absent, | ||
| # never fatal); plugin registries are per-home state we can't inspect | ||
| # cross-profile — neither may be rejected here. | ||
| (kanban_home / "skills").mkdir() | ||
| assert kb.unresolvable_task_skills( | ||
| ["kanban-worker", "someplugin:some-skill"], str(kanban_home) | ||
| ) == [] | ||
|
|
||
|
|
||
| def test_external_dirs_from_config_are_searched(kanban_home, tmp_path): | ||
| ext = tmp_path / "external-skills" | ||
| (ext / "shipped-elsewhere").mkdir(parents=True) | ||
| (ext / "shipped-elsewhere" / "SKILL.md").write_text( | ||
| "---\nname: shipped-elsewhere\n---\n", encoding="utf-8" | ||
| ) | ||
| (kanban_home / "skills").mkdir() | ||
| (kanban_home / "config.yaml").write_text( | ||
| f"skills:\n external_dirs:\n - {ext.as_posix()}\n", encoding="utf-8" | ||
| ) | ||
| assert kb.unresolvable_task_skills( | ||
| ["shipped-elsewhere"], str(kanban_home) | ||
| ) == [] | ||
|
|
||
|
|
||
| def test_missing_skills_root_rejects_concrete_names(kanban_home): | ||
| # No <home>/skills at all → preloading any skill is fatal at worker | ||
| # startup ("Skills directory does not exist yet"). | ||
| assert kb.unresolvable_task_skills(["anything"], str(kanban_home)) == ["anything"] | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # kanban create wiring (end-to-end via run_slash) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_create_rejects_unknown_skill(kanban_home): | ||
| _install_skill(kanban_home, "devops/real-skill") | ||
| out = kc.run_slash("create 'doomed task' --skill some-unknown-skill") | ||
| assert "unknown skill(s)" in out.lower() | ||
| assert "some-unknown-skill" in out | ||
| assert "Created" not in out | ||
| assert _task_count() == 0 | ||
|
|
||
|
|
||
| def test_create_accepts_installed_skill(kanban_home): | ||
| _install_skill(kanban_home, "devops/real-skill") | ||
| out = kc.run_slash("create 'good task' --skill real-skill") | ||
| assert "Created" in out | ||
| with kb.connect() as conn: | ||
| task = kb.list_tasks(conn)[0] | ||
| assert list(task.skills) == ["real-skill"] | ||
|
|
||
|
|
||
| def test_create_validates_against_assignee_profile_home(kanban_home): | ||
| # Skill installed ONLY in the assignee profile's home — the root home | ||
| # doesn't have it. Create must validate against the profile the worker | ||
| # will actually run under, not the creator's home. | ||
| profile_home = kanban_home / "profiles" / "coder" | ||
| _install_skill(profile_home, "devops/profile-only-skill") | ||
| out = kc.run_slash( | ||
| "create 'profile task' --assignee coder --skill profile-only-skill" | ||
| ) | ||
| assert "Created" in out | ||
|
|
||
| # And the inverse: the skill is in the creator's root home but NOT in | ||
| # the assignee's profile home — exactly the failure in #44072. | ||
| _install_skill(kanban_home, "devops/root-only-skill") | ||
| out = kc.run_slash( | ||
| "create 'doomed profile task' --assignee coder --skill root-only-skill" | ||
| ) | ||
| assert "unknown skill(s)" in out.lower() | ||
| assert "root-only-skill" in out | ||
|
|
||
|
|
||
| def test_create_without_skills_skips_validation(kanban_home): | ||
| # No --skill → no validation, even with no skills dir present. | ||
| out = kc.run_slash("create 'plain task' --assignee alice") | ||
| assert "Created" in out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This rejects a mixed valid/invalid list, but current main intentionally skips unknown entries when at least one requested skill loads (
cli.py:15933-15947,018009bc). Please preserve that behavior and reject only the all-unresolvable case.