Skip to content
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions design/extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ renders primary names and aliases, so integrations whose native command format
is `SKILL.md` create separate primary and alias skill directories. Non-generic
native registration writes its selected destination and may replace an
existing file; the generic integration applies its ownership checks first.
For native `SKILL.md` integrations, `registered_commands` records the primary
and alias names. The primary skill name is also refreshed by the shared
extension-skill renderer; aliases retain their alias-specific frontmatter and
receive the integration's skill post-processing. Refresh uses an ownership
allowlist so unrelated user skills remain protected.

Command-backed integrations configured to use skills take a separate
supplemental path. That path renders only primary commands as
Expand Down
10 changes: 8 additions & 2 deletions docs/extensions/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,14 @@ is_installed = registry.is_installed(extension_id: str) # bool
}
```

`registered_commands` maps integration keys to registered command names;
`registered_skills` records supplemental skill names. The installer stores
`registered_commands` maps integration keys to registered command names,
including aliases emitted by native command registration. For native
`SKILL.md` integrations, registration creates separate primary and alias
skill files and applies integration-specific skill post-processing to each.
`registered_skills` records skills written by the shared extension-skill
renderer (primary command names only); its refresh allowlist is limited to
outputs owned by that registration, preserving unrelated user skills. The
installer stores
`"local"` as raw local provenance, or a catalog object as shown above.
The CLI's normalized `--json` output uses a separate
[public source shape](cli.md#list-installed-extensions).
Expand Down
8 changes: 3 additions & 5 deletions src/specify_cli/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,12 +935,10 @@ def register_commands(

# -- Post-process for non-skills agents -----------------------
_integration = None
if agent_config["extension"] != "/SKILL.md":
from specify_cli.integrations import ( # noqa: PLC0415
get_integration,
)
from specify_cli.integrations import get_integration # noqa: PLC0415

_integration = get_integration(agent_name)
_integration = get_integration(agent_name)
if agent_config["extension"] != "/SKILL.md":
if _integration is not None:
output = _integration.post_process_command_content(output)

Expand Down
106 changes: 89 additions & 17 deletions src/specify_cli/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,7 @@ def _register_extension_skills(
*, previous_resource_hashes: Optional[dict] = None,
resource_hashes: Optional[dict] = None,
overwrite_modified: bool = False,
force_names: Set[str] | None = None,
) -> List[str]:
"""Generate SKILL.md files for extension commands as agent skills.

Expand All @@ -1580,10 +1581,11 @@ def _register_extension_skills(
link_outputs: If True, create dev-mode symlinks for rendered
skill files when supported by the OS.
force: If True, overwrite existing SKILL.md files even when they
are not dev-mode symlinks. Use in the upgrade path, where
``setup()`` has just freshly regenerated core-template skill
files and the skip guard would otherwise prevent extension
content from being layered on top.
are not dev-mode symlinks. Use only when the caller has
established ownership of the existing outputs or has freshly
regenerated them as part of an upgrade.
force_names: Optional allowlist of skill names for ``force``.
Other existing skills remain protected from overwrites.

Returns:
List of skill names that were created (for registry storage).
Expand All @@ -1605,6 +1607,7 @@ def _register_extension_skills(
"resource_hashes is required when registering commands with skill resources"
)
from .. import load_init_options
from .._invocation_style import get_invocation_prefix
from ..agents import CommandRegistrar
from ..integrations import get_integration
from ..integrations.base import IntegrationBase
Expand All @@ -1621,6 +1624,7 @@ def _register_extension_skills(
agent_config = registrar.AGENT_CONFIGS.get(selected_ai, {})
integration = get_integration(selected_ai)
ai_skills_enabled = is_ai_skills_enabled(opts)
native_skill_layout = agent_config.get("extension") == "/SKILL.md"

def _resolve_command_ref_tokens(body: str) -> str:
"""Resolve explicit command-ref tokens with the active skill style."""
Expand Down Expand Up @@ -1667,6 +1671,7 @@ def _replacement(match: re.Match[str]) -> str:
# Derive skill name from command name using the same hyphenated
# convention as hook rendering and preset skill registration.
skill_name = self._skill_name_for_command(cmd_name)
force_skill = force and (force_names is None or skill_name in force_names)

# Check if skill already exists before creating the directory
skill_subdir = skills_dir / skill_name
Expand All @@ -1693,9 +1698,9 @@ def _replacement(match: re.Match[str]) -> str:
# to be refreshed on a subsequent dev install. In the upgrade
# path (force=True) the file was just written by setup(), so
# overwriting it with the composed extension content is correct.
if not is_expected_dev_symlink and not force:
if not is_expected_dev_symlink and not force_skill:
continue
elif skill_dir_preexists and not force:
elif skill_dir_preexists and not force_skill:
# Never add files to a pre-existing user directory. Without a
# verifiable SKILL.md ownership marker, rollback/removal cannot
# distinguish our output from unrelated user artifacts.
Expand Down Expand Up @@ -1723,12 +1728,33 @@ def _replacement(match: re.Match[str]) -> str:
frontmatter = registrar._adjust_script_paths(
frontmatter, extension_id=manifest.id
)
if integration is not None and (native_skill_layout or not copy_mode):
frontmatter, body = integration.process_registered_command(
frontmatter, body, source_id=manifest.id
)
Comment on lines +1731 to +1734
# Mirror the register_commands() rewrite (#2101): resolve
# extension-relative subdir references (agents/, knowledge-base/,
# etc.) to their installed .specify/extensions/<id>/ location
# before the generic placeholder/path resolution below.
if not copy_mode:
Comment thread
Copilot marked this conversation as resolved.
Resolved
body = registrar.rewrite_extension_paths(body, manifest.id, extension_dir)
body = registrar.resolve_skill_placeholders(
selected_ai, frontmatter, body, self.project_root, extension_id=manifest.id,
rewrite_paths=not copy_mode,
)
body = _resolve_command_ref_tokens(body)
if native_skill_layout:
body = registrar._convert_argument_placeholder(
body, "$ARGUMENTS", agent_config["args"]
)
separator = (
integration.invoke_separator_for_mode(True)
if integration is not None else agent_config.get("invoke_separator", ".")
)
body = IntegrationBase.resolve_command_refs(
body, separator, get_invocation_prefix(selected_ai, True)
)
Comment on lines +1753 to +1755
else:
body = _resolve_command_ref_tokens(body)

original_desc = frontmatter.get("description", "")
description = original_desc or f"Extension command: {cmd_name}"
Expand Down Expand Up @@ -2535,7 +2561,11 @@ def install_from_directory(

active_options = load_init_options(self.project_root)
generic_active = isinstance(active_options, dict) and active_options.get("ai") == "generic"
if register_commands and manifest.commands:
validate_generic_outputs = generic_active and (
register_commands or is_ai_skills_enabled(active_options)
)
ignore_fn = self._load_extensionignore(source_dir)
if (register_commands or validate_generic_outputs) and manifest.commands:
state, state_error = try_read_integration_json(self.project_root)
if state_error is not None and generic_active:
detail = (
Expand Down Expand Up @@ -2570,7 +2600,7 @@ def install_from_directory(
"Cannot register generic extension commands: generic integration "
"and init options disagree on skills mode"
)
if register_commands and generic_active and manifest.commands:
if validate_generic_outputs and manifest.commands:
from ..integrations.generic import registration_directory

try:
Expand All @@ -2579,12 +2609,22 @@ def install_from_directory(
raise ExtensionError(f"Cannot register generic extension commands: {exc}") from exc
source_root = source_dir.resolve()
for command in manifest.commands:
source_file = (source_root / command["file"]).resolve()
relative_source = Path(command["file"])
source_file = (source_root / relative_source).resolve()
if not source_file.is_relative_to(source_root) or not source_file.is_file():
raise ExtensionError(
"Cannot register generic extension commands: missing source "
f"'{command['file']}'"
)
if ignore_fn is not None and not register_commands:
current_dir = source_dir
for part in relative_source.parts:
if part in ignore_fn(str(current_dir), [part]):
raise ExtensionError(
"Cannot register generic extension commands: ignored source "
f"'{command['file']}'"
)
current_dir = current_dir / part
try:
source_file.read_text(encoding="utf-8")
except (OSError, UnicodeDecodeError) as exc:
Expand Down Expand Up @@ -2655,7 +2695,6 @@ def install_from_directory(
# or ignored resource also fails before the old install is removed.
from ._skill_resources import resolve_skill_resources

ignore_fn = self._load_extensionignore(source_dir)
for command in manifest.commands:
if command.get("resources"):
resolve_skill_resources(
Expand Down Expand Up @@ -3210,12 +3249,37 @@ def rollback_generic_registration() -> None:

# Auto-register extension commands as agent skills when skills mode
# was used during project initialisation (feature parity).
registered_skills = self._register_extension_skills(
manifest, dest_dir, link_outputs=link_commands,
# Native integration registration can write the primary SKILL.md
# before the shared renderer runs. Refresh only names whose command
# artifacts this install actually registered, so fresh installs and
# updates produce the same content without overwriting unrelated
# user skills. Keep this in the shared install path so every source
# (local, catalog, or update archive) gets identical bookkeeping.
native_skill_names = {
self._skill_name_for_command(command_name)
for command_names in registered_commands.values()
for command_name in self._valid_name_list(command_names)
}
Comment thread
Copilot marked this conversation as resolved.
Resolved
rendered_skills = self._register_extension_skills(
manifest,
dest_dir,
link_outputs=link_commands,
previous_resource_hashes=resource_hashes,
resource_hashes=resource_hashes, overwrite_modified=force,
resource_hashes=resource_hashes,
overwrite_modified=force,
force=bool(native_skill_names),
force_names=native_skill_names,
Comment on lines +3270 to +3271
Comment thread
Copilot marked this conversation as resolved.
Resolved
Comment on lines +3270 to +3271
)
if register_commands and generic_active and manifest.commands:
# Record every file rendered here as extension-owned. Native
# command registration also tracks primary skills, while this
# ownership marker lets removal safely verify the rendered content.
registered_skills = list(dict.fromkeys(rendered_skills))
# `register_commands=False` can be used to avoid command-file
# registration while installing the generic integration in skills
# mode. The skill renderer is still responsible for producing one
# invocation artifact per declared command, so keep the manifest,
# collision, and required-output checks enabled for that path.
if validate_generic_outputs and manifest.commands:
Comment thread
Copilot marked this conversation as resolved.
Resolved
expected = set(names)
actual = set(
registered_skills if skills else registered_commands.get("generic", [])
Expand Down Expand Up @@ -3284,7 +3348,7 @@ def rollback_generic_registration() -> None:
except Exception as exc:
# Any failed commit must retire outputs before the original error
# is re-raised, including errors from hook serialization.
if register_commands and generic_active and manifest.commands:
if validate_generic_outputs and manifest.commands:
rollback_errors = []
if registry_started:
try:
Expand Down Expand Up @@ -4257,10 +4321,18 @@ def register_enabled_extensions_for_agent(
# #2948 callers only pass the active agent anyway).
if agent_name == active_agent:
try:
registered_skill_names = {
self._skill_name_for_command(command_name)
for command_name in self._valid_name_list(registered)
}
registered_skills = self._register_extension_skills(
manifest, ext_dir, force=force,
manifest, ext_dir,
previous_resource_hashes=resource_hashes,
resource_hashes=resource_hashes, overwrite_modified=overwrite_modified,
force=force or bool(registered_skill_names),
force_names=(
None if force else registered_skill_names
),
)
if agent_name == "generic" and skills_mode_active:
registered_skills = self._complete_generic_refresh(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def backup_extension_skills(skill_names, *, skills_dir=None, native=False):
manager.remove(extension_id, keep_config=True)

# 8. Install new version
_ = manager.install_from_zip(
manager.install_from_zip(
archive_path,
speckit_version,
catalog_name=update.catalog_name,
Expand Down
Loading