Skip to content

Commit 977a329

Browse files
committed
fix(presets): render shared skill dirs once; match the command's registration
Two findings from the review of 1bcd341: - A surviving composer can record several agents for one physical skills directory (codex and agy share .agents/skills). Its primary skill was refreshed once per recorded agent on the owner route, so the last one's renderer won, and the winner route kept the first recorded one. Group the survivor's destinations by directory and pick the renderer once: the active agent when it uses that directory, otherwise the first recorded agent in sorted order. The alias pass uses the same rule, so a primary skill and its alias agree. - A layer under the owner counted as registered in a historical directory when its pack had any record for an agent resolving there. After a partial registration, another command's record admitted a layer of this command that was never written there. Require this command's skill name in registered_skills, or its command name in a native skill agent's registered_commands; the same for extensions. The legacy fallbacks are unchanged. Assisted-by: Claude Code (model: Claude Mythos 5.2, autonomous)
1 parent ce821bb commit 977a329

2 files changed

Lines changed: 212 additions & 16 deletions

File tree

‎src/specify_cli/presets/_manager_skills.py‎

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ def apply_to_dir(
451451
# filtered out; otherwise compose from the owner's layers
452452
# that belong here.
453453
registered_here = self._layers_registered_in(
454-
layers[owner_idx:], skills_dir, active_ai, active_skills_dir
454+
layers[owner_idx:], skills_dir, active_ai, active_skills_dir,
455+
cmd_name,
455456
)
456457
# A project override wins the command for every agent, so the
457458
# owner's composition is not what belongs there: let the
@@ -507,7 +508,8 @@ def apply_to_dir(
507508
if layer["path"].is_relative_to(owner_dir)
508509
)
509510
owner_layers = self._layers_registered_in(
510-
layers[owner_idx:], skills_dir, active_ai, active_skills_dir
511+
layers[owner_idx:], skills_dir, active_ai, active_skills_dir,
512+
cmd_name,
511513
)
512514
composed = resolver.resolve_content(
513515
cmd_name, "command", layers=owner_layers
@@ -650,8 +652,15 @@ def _reconcile_preset_aliases(
650652

651653
for skills_dir, group in groups.items():
652654
agents = group["agents"]
655+
# Same choice as the primary skill of that directory.
653656
agent = (
654-
active_ai if active_ai in agents else sorted(agents)[0]
657+
active_ai
658+
if active_ai in agents
659+
or (
660+
skills_dir == active_skills_dir
661+
and active_ai in registrar.AGENT_CONFIGS
662+
)
663+
else sorted(agents)[0]
655664
)
656665
agent_config = registrar.AGENT_CONFIGS[agent]
657666
# Rendering follows the single-active rule outside a removal
@@ -728,7 +737,8 @@ def _reconcile_preset_aliases(
728737
# its primary skill is.
729738
shadowed = alias_shadowed and not removal_pass
730739
owner_layers = self._layers_registered_in(
731-
layers[owner_idx:], skills_dir, active_ai, active_skills_dir
740+
layers[owner_idx:], skills_dir, active_ai, active_skills_dir,
741+
primary,
732742
)
733743
composed = resolver.resolve_content(
734744
primary, "command", layers=owner_layers
@@ -818,16 +828,19 @@ def _layers_registered_in(
818828
skills_dir: Path,
819829
active_ai: Optional[str],
820830
active_skills_dir: Optional[Path],
831+
cmd_name: str,
821832
) -> List[Dict[str, Any]]:
822-
"""Drop pack layers that were never registered in ``skills_dir``.
833+
"""Drop pack layers of ``cmd_name`` never registered in ``skills_dir``.
823834
824835
Presets are registered for the active agent only (#2948), so a preset
825836
installed while another agent was active has no output in a
826837
historical agent's directory and must not be composed into one. A
827-
preset counts as registered here when its ``registered_skills`` names
828-
an agent that resolves to this directory, or its
829-
``registered_commands`` names a native skill agent that does: only
830-
for those is a registered command a file in the skills directory. A
838+
preset counts as registered here when its ``registered_skills`` lists
839+
this command's skill for an agent that resolves to this directory, or
840+
its ``registered_commands`` lists the command for a native skill
841+
agent that does: only for those is a registered command a file in
842+
the skills directory. Another command of the same pack recorded
843+
there says nothing about this one (a registration can be partial). A
831844
command-backed agent's entry records command files elsewhere, and
832845
its skills directory can be another agent's (Amp's resolves to
833846
Codex's ``.agents/skills``). The active directory is never filtered,
@@ -847,6 +860,16 @@ def _layers_registered_in(
847860
except ImportError:
848861
agent_configs = {}
849862

863+
wanted = {
864+
"registered_skills": set(self._skill_names_for_command(cmd_name)),
865+
"registered_commands": {cmd_name},
866+
}
867+
868+
def lists_command(key: str, names: Any) -> bool:
869+
return isinstance(names, list) and any(
870+
isinstance(name, str) and name in wanted[key] for name in names
871+
)
872+
850873
def writes_skills(agent: Any) -> bool:
851874
config = agent_configs.get(agent) if isinstance(agent, str) else None
852875
return bool(config) and config.get("extension") == "/SKILL.md"
@@ -879,7 +902,7 @@ def is_registered(pack_id: str) -> bool:
879902
for key in ("registered_skills", "registered_commands"):
880903
recorded = meta.get(key)
881904
if isinstance(recorded, dict) and any(
882-
names
905+
lists_command(key, names)
883906
and (key == "registered_skills" or writes_skills(agent))
884907
and resolves_here(agent)
885908
for agent, names in recorded.items()
@@ -903,7 +926,9 @@ def is_registered(pack_id: str) -> bool:
903926
if isinstance(meta, dict) and not meta.get("registered_skills"):
904927
commands = meta.get("registered_commands")
905928
if isinstance(commands, dict) and not any(
906-
names and writes_skills(agent) and resolves_here(agent)
929+
lists_command("registered_commands", names)
930+
and writes_skills(agent)
931+
and resolves_here(agent)
907932
for agent, names in commands.items()
908933
):
909934
continue
@@ -1026,6 +1051,12 @@ def _surviving_owned_skills(
10261051
provenance for legacy flat lists) and keeps the names whose ``SKILL.md``
10271052
still carries that preset's ``preset:<id>`` marker, as
10281053
``(skills_dir, agent, owner_id, command_name, skill_name)`` tuples.
1054+
1055+
Several agents can record one physical directory (agy/codex/zed all
1056+
use ``.agents/skills``), so each file is returned once, with the
1057+
agent to render it: the active agent when it uses that directory,
1058+
otherwise the first recorded agent in sorted order, as
1059+
``_unregister_skills`` and the alias pass choose.
10291060
"""
10301061
registrar = self._command_registrar()
10311062
affected = {
@@ -1044,6 +1075,7 @@ def _surviving_owned_skills(
10441075
)
10451076
if not isinstance(recorded, dict):
10461077
continue
1078+
groups: Dict[Path, Dict[str, List[str]]] = {}
10471079
for agent, names in recorded.items():
10481080
if not isinstance(agent, str) or not isinstance(names, list):
10491081
continue
@@ -1054,9 +1086,24 @@ def _surviving_owned_skills(
10541086
)
10551087
if skills_dir is None:
10561088
continue
1089+
group = groups.setdefault(skills_dir, {"agents": [], "names": []})
1090+
group["agents"].append(agent)
10571091
for name in names:
1058-
if name not in affected:
1059-
continue
1092+
if (
1093+
isinstance(name, str)
1094+
and name in affected
1095+
and name not in group["names"]
1096+
):
1097+
group["names"].append(name)
1098+
for skills_dir, group in groups.items():
1099+
agents = group["agents"]
1100+
agent = (
1101+
active_ai
1102+
if active_ai
1103+
and (active_ai in agents or skills_dir == active_skills_dir)
1104+
else sorted(agents)[0]
1105+
)
1106+
for name in group["names"]:
10601107
skill_file = skills_dir / name / "SKILL.md"
10611108
if not self._validate_skill_subdir(
10621109
skill_file.parent, create=False, skills_root=skills_dir

‎tests/specify_cli/presets/test_manager_skills.py‎

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,12 +2737,18 @@ def test_remove_base_does_not_compose_other_agents_layer_into_historical_dir(
27372737
assert "Low body" not in content and "Base preset body" not in content
27382738
assert "Core custom body" in content and "Middle body" in content
27392739

2740-
@pytest.mark.parametrize("extension_agent", ["codex", "claude"])
2740+
@pytest.mark.parametrize(
2741+
"extension_agent,unrelated_claude_command",
2742+
[("codex", False), ("codex", True), ("claude", False)],
2743+
ids=["codex", "codex-unrelated-claude-command", "claude"],
2744+
)
27412745
def test_historical_skill_uses_only_registered_extension(
2742-
self, project_dir, temp_dir, extension_agent
2746+
self, project_dir, temp_dir, extension_agent, unrelated_claude_command
27432747
):
27442748
"""Removing a base must not introduce another agent's extension
2745-
into a historical skill or alias; a registered base must survive."""
2749+
into a historical skill or alias; a registered base must survive.
2750+
Another command of the extension recorded for Claude (a partial
2751+
registration) does not register this one there."""
27462752
self._write_init_options(project_dir, ai=extension_agent, ai_skills=True)
27472753
for folder in (".agents", ".claude"):
27482754
(project_dir / folder / "skills").mkdir(parents=True)
@@ -2771,6 +2777,16 @@ def test_historical_skill_uses_only_registered_extension(
27712777
extension_agent: ["speckit.foreign.run"]
27722778
}
27732779
assert extensions.registry.get("foreign")["registered_skills"] == []
2780+
if unrelated_claude_command:
2781+
extensions.registry.update(
2782+
"foreign",
2783+
{
2784+
"registered_commands": {
2785+
extension_agent: ["speckit.foreign.run"],
2786+
"claude": ["speckit.foreign.other"],
2787+
}
2788+
},
2789+
)
27742790

27752791
self._write_init_options(project_dir, ai="claude", ai_skills=True)
27762792
self._create_skill(project_dir / ".claude" / "skills", "speckit-foreign-run")
@@ -3016,6 +3032,139 @@ def test_rescaffold_keeps_other_presets_alias_named_like_baseless_command(
30163032

30173033
assert target.read_text(encoding="utf-8") == installed
30183034

3035+
@pytest.mark.parametrize(
3036+
"active,expected,unexpected",
3037+
[
3038+
("codex", "$speckit-plan", "/speckit-plan"),
3039+
("agy", "/speckit-plan", "$speckit-plan"),
3040+
("claude", "/speckit-plan", "$speckit-plan"),
3041+
],
3042+
ids=["codex-active", "agy-active", "shared-dir-historical"],
3043+
)
3044+
@pytest.mark.parametrize("middle", [False, True], ids=["winner", "owner-refresh"])
3045+
def test_shared_dir_survivor_skill_rendered_once_per_directory(
3046+
self, project_dir, temp_dir, active, expected, unexpected, middle
3047+
):
3048+
"""Codex and agy share ``.agents/skills``, and the surviving composer
3049+
records both. Its primary skill is one file, so it is refreshed once:
3050+
with the active agent's renderer when that agent uses the directory,
3051+
otherwise with the first recorded agent in sorted order (agy), the
3052+
same choice the alias pass and preset removal make. ``middle`` adds a
3053+
higher preset registered only for another directory, which sends the
3054+
refresh through the owner route instead of the winner route."""
3055+
self._write_init_options(project_dir, ai="codex", ai_skills=True)
3056+
shared = project_dir / ".agents" / "skills"
3057+
self._create_skill(shared, "speckit-custom")
3058+
self._write_core_custom(project_dir)
3059+
manager = PresetManager(project_dir)
3060+
install = functools.partial(self._install_alias_layer, manager, temp_dir)
3061+
install("base-preset", "Base preset body", "replace", 10, alias=False)
3062+
install("append-preset", "Run `__SPECKIT_COMMAND_PLAN__`.", "append", 5)
3063+
self._write_init_options(project_dir, ai="agy", ai_skills=True)
3064+
manager.register_enabled_presets_for_agent("agy")
3065+
self._write_init_options(project_dir, ai="codex", ai_skills=True)
3066+
manager.register_enabled_presets_for_agent("codex")
3067+
recorded = manager.registry.get("append-preset").get("registered_skills", {})
3068+
assert {"codex", "agy"} <= set(recorded)
3069+
if middle or active == "claude":
3070+
self._write_init_options(project_dir, ai="claude", ai_skills=True)
3071+
self._create_skill(project_dir / ".claude" / "skills", "speckit-custom")
3072+
if middle:
3073+
install("top-preset", "Top body", "append", 3, alias=False)
3074+
self._write_init_options(project_dir, ai=active, ai_skills=True)
3075+
3076+
assert manager.remove("base-preset") is True
3077+
3078+
for name in ("speckit-custom", "speckit-custom-short"):
3079+
text = (shared / name / "SKILL.md").read_text(encoding="utf-8")
3080+
assert "Core custom body" in text and "Base preset body" not in text
3081+
assert "Top body" not in text
3082+
assert expected in text and unexpected not in text
3083+
3084+
@pytest.mark.parametrize("core", [False, True], ids=["no-fallback", "core-fallback"])
3085+
def test_remove_base_requires_this_commands_registration_in_historical_dir(
3086+
self, project_dir, temp_dir, core
3087+
):
3088+
"""``low`` provides ``speckit.custom`` and ``speckit.other`` and is
3089+
installed while Codex is active. A partial registration then records
3090+
only its unrelated ``speckit.other`` skill for Claude. That record
3091+
must not admit its ``speckit.custom`` layer into Claude's skill and
3092+
alias when the base under Claude's composer is removed."""
3093+
self._write_init_options(project_dir, ai="claude", ai_skills=True)
3094+
self._create_skill(project_dir / ".claude" / "skills", "speckit-custom")
3095+
if core:
3096+
self._write_core_custom(project_dir)
3097+
manager = PresetManager(project_dir)
3098+
install = functools.partial(self._install_alias_layer, manager, temp_dir)
3099+
install("base-preset", "Base preset body", "replace", 10, alias=False)
3100+
install("middle-preset", "Middle body", "append", 7)
3101+
claude_skill = project_dir / ".claude" / "skills" / "speckit-custom" / "SKILL.md"
3102+
claude_alias = self._alias_skill(project_dir)
3103+
self._write_init_options(project_dir, ai="codex", ai_skills=True)
3104+
manager.register_enabled_presets_for_agent("codex")
3105+
low = self._create_multi_command_preset(
3106+
temp_dir, "low-preset", ["speckit.custom", "speckit.other"]
3107+
)
3108+
(low / "commands" / "speckit.custom.md").write_text(
3109+
"---\ndescription: low\n---\n\nLow body\n", encoding="utf-8"
3110+
)
3111+
manager.install_from_directory(low, "0.1.5", priority=12)
3112+
meta = manager.registry.get("low-preset")
3113+
assert "claude" not in meta["registered_skills"]
3114+
assert "claude" not in meta["registered_commands"]
3115+
manager.registry.update(
3116+
"low-preset",
3117+
{
3118+
"registered_skills": {
3119+
**meta["registered_skills"], "claude": ["speckit-other"],
3120+
},
3121+
"registered_commands": {
3122+
**meta["registered_commands"], "claude": ["speckit.other"],
3123+
},
3124+
},
3125+
)
3126+
3127+
assert manager.remove("base-preset") is True
3128+
3129+
for output in (claude_skill, claude_alias):
3130+
if not core:
3131+
assert not output.exists()
3132+
continue
3133+
content = output.read_text(encoding="utf-8")
3134+
assert "Low body" not in content and "Base preset body" not in content
3135+
assert "Core custom body" in content and "Middle body" in content
3136+
3137+
def test_shared_dir_outputs_use_active_renderer_when_only_another_agent_recorded(
3138+
self, project_dir, temp_dir
3139+
):
3140+
"""The presets were registered while agy was active. Codex uses the
3141+
same ``.agents/skills`` and is active now, without having been
3142+
recorded. The primary skill and its alias are refreshed in Codex's
3143+
directory, so both use Codex's command-reference syntax."""
3144+
self._write_init_options(project_dir, ai="agy", ai_skills=True)
3145+
shared = project_dir / ".agents" / "skills"
3146+
self._create_skill(shared, "speckit-custom")
3147+
self._write_core_custom(project_dir)
3148+
manager = PresetManager(project_dir)
3149+
install = functools.partial(self._install_alias_layer, manager, temp_dir)
3150+
install("base-preset", "Base preset body", "replace", 10, alias=False)
3151+
install("append-preset", "Run `__SPECKIT_COMMAND_PLAN__`.", "append", 5)
3152+
meta = manager.registry.get("append-preset")
3153+
assert set(meta["registered_skills"]) == {"agy"}
3154+
assert set(meta["registered_commands"]) == {"agy"}
3155+
for name in ("speckit-custom", "speckit-custom-short"):
3156+
assert "/speckit-plan" in (shared / name / "SKILL.md").read_text(
3157+
encoding="utf-8"
3158+
)
3159+
self._write_init_options(project_dir, ai="codex", ai_skills=True)
3160+
3161+
assert manager.remove("base-preset") is True
3162+
3163+
for name in ("speckit-custom", "speckit-custom-short"):
3164+
text = (shared / name / "SKILL.md").read_text(encoding="utf-8")
3165+
assert "Core custom body" in text and "Base preset body" not in text
3166+
assert "$speckit-plan" in text and "/speckit-plan" not in text
3167+
30193168
def test_alias_refresh_rewrites_the_same_file(self, project_dir, temp_dir):
30203169
"""The alias is refreshed in place, as command registration always
30213170
wrote it: the same inode keeps its owner, ACL and mode."""

0 commit comments

Comments
 (0)