refactor: unify skill source model

This commit is contained in:
2026-07-30 12:11:25 +08:00
parent 076d87e303
commit 7d1994cf93
18 changed files with 669 additions and 527 deletions
+122 -58
View File
@@ -12,7 +12,7 @@ from unittest.mock import Mock, patch
from skiff import cli
from skiff import yaml_io
from skiff.registry import external_repo_path, external_skill_path
from skiff.catalog import catalog_repo_path, catalog_skill_path
from skiff.selector import SkillChoice, filter_choices, fit_to_width, select_skills
@@ -28,7 +28,7 @@ class SelectorTests(unittest.TestCase):
data = {
"skills": [
{
"name": "external-one",
"name": "catalog-one",
"source": "registry",
"targets": ["codex"],
}
@@ -39,24 +39,24 @@ class SelectorTests(unittest.TestCase):
def test_filter_matches_name_kind_and_description(self) -> None:
choices = [
SkillChoice("frontend-design", "external", "创建界面"),
SkillChoice("frontend-design", "catalog", "创建界面"),
SkillChoice(
"discussion-notes",
"owned",
"builtin",
"维护讨论笔记",
readonly_status="全局: codex",
),
]
self.assertEqual([c.name for c in filter_choices(choices, "front")], ["frontend-design"])
self.assertEqual([c.name for c in filter_choices(choices, "owned")], ["discussion-notes"])
self.assertEqual([c.name for c in filter_choices(choices, "builtin")], ["discussion-notes"])
self.assertEqual([c.name for c in filter_choices(choices, "界面")], ["frontend-design"])
self.assertEqual([c.name for c in filter_choices(choices, "codex")], ["discussion-notes"])
def test_selector_preserves_preselected_items(self) -> None:
choices = [
SkillChoice("already-there", "owned", installed=True),
SkillChoice("new-skill", "external"),
SkillChoice("already-there", "builtin", installed=True),
SkillChoice("new-skill", "catalog"),
]
screen = Mock()
screen.getmaxyx.return_value = (24, 100)
@@ -76,7 +76,7 @@ class SelectorTests(unittest.TestCase):
choices = [
SkillChoice(
"ack",
"owned",
"builtin",
installed=False,
readonly_status="全局: cursor,claude,codex",
)
@@ -108,7 +108,7 @@ class SelectorTests(unittest.TestCase):
choices = [
SkillChoice(
"ack",
"owned",
"builtin",
description="初始化、检查并运行 ACK",
readonly_status="全局: codex",
)
@@ -128,7 +128,7 @@ class SelectorTests(unittest.TestCase):
)
self.assertEqual(
"".join(call.args[2] for call in title_calls),
"[ ] ack owned 全局: codex",
"[ ] ack builtin 全局: codex",
)
self.assertEqual(description_call.args[2], " 初始化、检查并运行 ACK")
name_call = next(call for call in title_calls if call.args[2] == "ack")
@@ -142,8 +142,8 @@ class SelectorTests(unittest.TestCase):
"repository",
children=("waza/think", "waza/ui"),
),
SkillChoice("waza/think", "external:waza", indent=1),
SkillChoice("waza/ui", "external:waza", indent=1),
SkillChoice("waza/think", "catalog:waza", indent=1),
SkillChoice("waza/ui", "catalog:waza", indent=1),
]
screen = Mock()
screen.getmaxyx.return_value = (24, 100)
@@ -161,8 +161,8 @@ class SelectorTests(unittest.TestCase):
"repository",
children=("waza/think", "waza/ui"),
),
SkillChoice("waza/think", "external:waza", installed=True, indent=1),
SkillChoice("waza/ui", "external:waza", indent=1),
SkillChoice("waza/think", "catalog:waza", installed=True, indent=1),
SkillChoice("waza/ui", "catalog:waza", indent=1),
]
screen = Mock()
screen.getmaxyx.return_value = (24, 100)
@@ -182,17 +182,17 @@ class SelectorTests(unittest.TestCase):
first = {"repo": "https://example.test/skills.git", "ref": "main", "path": "a"}
second = {"repo": "https://example.test/skills.git", "ref": "main", "path": "b"}
self.assertEqual(external_repo_path(first), external_repo_path(second))
self.assertEqual(catalog_repo_path(first), catalog_repo_path(second))
def test_external_skill_path_rejects_checkout_escape(self) -> None:
def test_catalog_skill_path_rejects_checkout_escape(self) -> None:
entry = {
"repo": "https://example.test/skills.git",
"ref": "main",
"path": "../../outside",
}
with self.assertRaisesRegex(SystemExit, "超出外部仓库"):
external_skill_path("unsafe-skill", entry)
with self.assertRaisesRegex(SystemExit, "超出来源仓库"):
catalog_skill_path("unsafe-skill", entry)
def test_collection_discovery_ignores_symlinked_skill(self) -> None:
with tempfile.TemporaryDirectory() as temp:
@@ -210,12 +210,12 @@ class SelectorTests(unittest.TestCase):
"path": "skills",
}
with patch("skiff.registry.external_checkout_path", return_value=checkout):
from skiff.registry import discover_external_skills
with patch("skiff.catalog.catalog_checkout_path", return_value=checkout):
from skiff.catalog import discover_catalog_skills
self.assertEqual(discover_external_skills("unsafe", entry), {})
self.assertEqual(discover_catalog_skills("unsafe", entry), {})
def test_registry_clone_is_shallow(self) -> None:
def test_catalog_clone_is_shallow(self) -> None:
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
checkout = root / "checkout"
@@ -226,16 +226,16 @@ class SelectorTests(unittest.TestCase):
"path": "skills",
}
with (
patch.object(cli, "external_skill_path", return_value=skill_root),
patch.object(cli, "external_checkout_path", return_value=checkout),
patch.object(cli, "catalog_skill_path", return_value=skill_root),
patch.object(cli, "catalog_checkout_path", return_value=checkout),
patch.object(
cli,
"discover_external_skills",
"discover_catalog_skills",
side_effect=[{}, {"demo": skill_root / "demo"}],
),
patch.object(cli.subprocess, "run") as run,
):
cli._ensure_registry_fetched("demo-pack", entry)
cli._ensure_catalog_fetched("demo-pack", entry)
run.assert_called_once_with(
[
@@ -277,9 +277,9 @@ class SelectCommandTests(unittest.TestCase):
def test_project_selection_installs_new_and_records_all_selected(self) -> None:
with tempfile.TemporaryDirectory() as temp:
project = Path(temp)
external = project / "external-one"
external.mkdir()
external.joinpath("SKILL.md").write_text("---\n", encoding="utf-8")
catalog = project / "catalog-one"
catalog.mkdir()
catalog.joinpath("SKILL.md").write_text("---\n", encoding="utf-8")
args = argparse.Namespace(
agents=[["codex"]],
global_scope=False,
@@ -301,28 +301,28 @@ class SelectCommandTests(unittest.TestCase):
) -> set[str]:
selected_choices.extend(choices)
selected_scope.append(scope_label)
return {"owned-one", "external-one"}
return {"builtin-one", "catalog-one"}
with (
patch.object(cli.sys, "stdin", stdin),
patch.object(cli.sys, "stdout", stdout),
patch.object(cli, "ensure_skills_home"),
patch.object(cli, "list_owned_skills", return_value=["owned-one"]),
patch.object(cli, "skill_description", return_value="owned"),
patch.object(cli, "list_builtin_skills", return_value=["builtin-one"]),
patch.object(cli, "skill_description", return_value="builtin"),
patch.object(
cli,
"load_registry",
"load_catalog",
return_value={
"external-one": {
"catalog-one": {
"repo": "https://example.test/skills.git",
"ref": "main",
"path": "external-one",
"path": "catalog-one",
}
},
),
patch.object(cli, "_registry_skill_names", return_value=["external-one"]),
patch.object(cli, "external_skill_path", return_value=external),
patch.object(cli, "_list_fully_installed_names", return_value=["owned-one"]),
patch.object(cli, "_catalog_skill_names", return_value=["catalog-one"]),
patch.object(cli, "catalog_skill_path", return_value=catalog),
patch.object(cli, "_list_fully_installed_names", return_value=["builtin-one"]),
patch.object(
cli,
"_global_installation_note",
@@ -331,7 +331,7 @@ class SelectCommandTests(unittest.TestCase):
patch.object(
cli,
"_is_fully_installed",
side_effect=lambda name, expected, project_root, targets: name == "owned-one",
side_effect=lambda name, expected, project_root, targets: name == "builtin-one",
),
patch.object(
cli,
@@ -348,10 +348,11 @@ class SelectCommandTests(unittest.TestCase):
manifest = (project / ".skills.yaml").read_text(encoding="utf-8")
self.assertEqual(installed, ["external-one"])
self.assertIn("owned-one", manifest)
self.assertIn('name: "external-one"', manifest)
self.assertIn("source: registry", manifest)
self.assertEqual(installed, ["catalog-one"])
self.assertIn("builtin-one", manifest)
self.assertIn("source: builtin", manifest)
self.assertIn('name: "catalog-one"', manifest)
self.assertIn('source: "catalog:catalog-one"', manifest)
self.assertIn("targets:", manifest)
self.assertIn("codex", manifest)
self.assertEqual(
@@ -389,7 +390,7 @@ class SelectCommandTests(unittest.TestCase):
self.assertEqual(note, "全局: cursor;全局同名冲突: claude")
def test_select_rejects_invalid_registry_name_before_rendering(self) -> None:
def test_select_rejects_invalid_catalog_name_before_rendering(self) -> None:
args = argparse.Namespace(
agents=None,
global_scope=True,
@@ -405,10 +406,10 @@ class SelectCommandTests(unittest.TestCase):
patch.object(cli.sys, "stdin", stdin),
patch.object(cli.sys, "stdout", stdout),
patch.object(cli, "ensure_skills_home"),
patch.object(cli, "list_owned_skills", return_value=[]),
patch.object(cli, "list_builtin_skills", return_value=[]),
patch.object(
cli,
"load_registry",
"load_catalog",
return_value={"../../victim": {"repo": "https://example.test/repo.git"}},
),
patch.object(cli, "select_skills") as selector,
@@ -418,7 +419,7 @@ class SelectCommandTests(unittest.TestCase):
selector.assert_not_called()
def test_select_expands_registry_collection_choices(self) -> None:
def test_select_expands_catalog_collection_choices(self) -> None:
args = argparse.Namespace(
agents=[["codex"]],
global_scope=True,
@@ -440,10 +441,10 @@ class SelectCommandTests(unittest.TestCase):
patch.object(cli.sys, "stdin", stdin),
patch.object(cli.sys, "stdout", stdout),
patch.object(cli, "ensure_skills_home"),
patch.object(cli, "list_owned_skills", return_value=[]),
patch.object(cli, "list_builtin_skills", return_value=[]),
patch.object(
cli,
"load_registry",
"load_catalog",
return_value={
"waza": {
"repo": "https://example.test/waza.git",
@@ -454,7 +455,7 @@ class SelectCommandTests(unittest.TestCase):
),
patch.object(
cli,
"_registry_skill_names",
"_catalog_skill_names",
return_value=["think", "ui"],
),
patch.object(cli, "_list_fully_installed_names", return_value=[]),
@@ -476,9 +477,72 @@ class SelectCommandTests(unittest.TestCase):
)
self.assertEqual(selected_choices[0].children, ("waza/think", "waza/ui"))
self.assertEqual([choice.indent for choice in selected_choices[1:]], [1, 1])
self.assertEqual(installed, [("think", "registry:waza")])
self.assertEqual(installed, [("think", "catalog:waza")])
def test_select_deduplicates_local_registry_collection_from_owned(self) -> None:
def test_select_expands_custom_collection_choices(self) -> None:
args = argparse.Namespace(
agents=[["codex"]],
global_scope=True,
project=None,
yes=False,
)
stdin = Mock()
stdout = Mock()
stdin.isatty.return_value = True
stdout.isatty.return_value = True
selected_choices: list[SkillChoice] = []
installed: list[tuple[str, str | None]] = []
def choose(choices: list[SkillChoice], **_: object) -> set[str]:
selected_choices.extend(choices)
return {"company/review"}
with tempfile.TemporaryDirectory() as temp:
root = Path(temp)
review = root / "review"
release = root / "release"
review.mkdir()
release.mkdir()
with (
patch.object(cli.sys, "stdin", stdin),
patch.object(cli.sys, "stdout", stdout),
patch.object(cli, "ensure_skills_home"),
patch.object(cli, "list_builtin_skills", return_value=[]),
patch.object(cli, "load_catalog", return_value={}),
patch.object(
cli,
"load_sources",
return_value={"company": {"local_path": str(root)}},
),
patch.object(cli, "_ensure_source_fetched"),
patch.object(
cli,
"discover_source_skills",
return_value={"review": review, "release": release},
),
patch.object(cli, "_is_fully_installed", return_value=False),
patch.object(cli, "select_skills", side_effect=choose),
patch.object(
cli,
"_install_skill",
side_effect=lambda name, targets, project_root, **kwargs: installed.append(
(name, kwargs.get("source"))
),
),
):
cli.cmd_select(args)
self.assertEqual(
[choice.name for choice in selected_choices],
["company", "company/review", "company/release"],
)
self.assertEqual(
selected_choices[0].children,
("company/review", "company/release"),
)
self.assertEqual(installed, [("review", "company")])
def test_select_deduplicates_local_catalog_collection_from_builtin(self) -> None:
args = argparse.Namespace(
agents=[["codex"]],
global_scope=True,
@@ -504,11 +568,11 @@ class SelectCommandTests(unittest.TestCase):
patch.object(cli.sys, "stdout", stdout),
patch.object(cli, "SKILLS_DIR", skills_root),
patch.object(cli, "ensure_skills_home"),
patch.object(cli, "list_owned_skills", return_value=["ack"]),
patch.object(cli, "skill_description", return_value="owned ack"),
patch.object(cli, "list_builtin_skills", return_value=["ack"]),
patch.object(cli, "skill_description", return_value="builtin ack"),
patch.object(
cli,
"load_registry",
"load_catalog",
return_value={
"skills": {
"repo": "~/.skills",
@@ -517,8 +581,8 @@ class SelectCommandTests(unittest.TestCase):
}
},
),
patch.object(cli, "_registry_skill_names", return_value=["ack"]),
patch.object(cli, "external_skill_path", return_value=skills_root),
patch.object(cli, "_catalog_skill_names", return_value=["ack"]),
patch.object(cli, "catalog_skill_path", return_value=skills_root),
patch.object(cli, "_is_fully_installed", return_value=False),
patch.object(cli, "select_skills", side_effect=choose),
):
@@ -540,7 +604,7 @@ class SelectCommandTests(unittest.TestCase):
with (
patch.object(cli, "_ensure_source_fetched"),
patch.object(cli, "resolve_skill_source", return_value=(skill, "owned")),
patch.object(cli, "resolve_skill_source", return_value=(skill, "builtin")),
patch.object(
cli,
"agent_skill_dir",