Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions src/specify_cli/presets/command_set_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,43 @@ def preset_set_priority(

# Update priority
manager.registry.update(preset_id, {"priority": priority})

# Disabled presets are deliberately excluded from resolution. Updating
# their priority must not rewrite files still owned by the active stack.
# A priority change can change the winning command or skill layer just as
# install and removal do, so rematerialize artifacts provided by this
# preset from the full priority stack.
if metadata.get("enabled", True) is not False:
try:
from ._manifest import PresetManifest

manifest = PresetManifest(manager.presets_dir / preset_id / "preset.yml")
command_names = [
template["name"]
for template in manifest.templates
if template.get("type") == "command"
]
# Presets may also provide conventional commands without declaring
# them in preset.yml. Include those names so a priority change can
# rematerialize the command they participate in.
convention_commands = manager.presets_dir / preset_id / "commands"
if convention_commands.is_dir():
command_names.extend(
path.stem for path in convention_commands.glob("*.md")
)
Comment on lines +77 to +84
Comment on lines +82 to +84
command_names = list(dict.fromkeys(command_names))
if command_names:
manager._reconcile_composed_commands(command_names)
manager._reconcile_skills(command_names)
except Exception as exc: # noqa: BLE001 - lifecycle reconciliation is best-effort
import warnings

warnings.warn(
f"Post-priority reconciliation failed for {preset_id}: {exc}. "
"Agent command files may not reflect the current priority stack.",
stacklevel=2,
)

manager.reconcile_constitution(
f"Failed to reconcile constitution after changing priority for preset {preset_id}"
)
Expand Down
184 changes: 183 additions & 1 deletion tests/specify_cli/presets/test_command_set_priority.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
)
from tests.conftest import strip_ansi
from tests.specify_cli.presets._helpers import (
PresetArtifactTestHelpers,
install_constitution_sync_preset,
install_self_test_preset,
)
from tests.specify_cli.presets._helpers import (
make_convention_constitution_preset as _make_convention_constitution_preset,
)


class TestPresetSetPriority:
class TestPresetSetPriority(PresetArtifactTestHelpers):
"""Test preset set-priority CLI command."""

def test_set_priority_changes_priority(self, project_dir, pack_dir):
Expand Down Expand Up @@ -72,6 +75,185 @@ def test_set_priority_reconciles_generated_constitution(
assert result.exit_code == 0, result.output
assert memory.read_text() == "# Convention Constitution\n"

def test_set_priority_reconciles_generated_commands(self, project_dir, temp_dir):
"""Changing priority rematerializes the winning preset command."""
from unittest.mock import patch

from typer.testing import CliRunner

from specify_cli import app

self._write_init_options(project_dir, ai="gemini", ai_skills=False)
commands_dir = project_dir / ".gemini" / "commands"
commands_dir.mkdir(parents=True)

manager = PresetManager(project_dir)
first = self._create_command_preset(
temp_dir, "priority-first", "speckit.demo", "First", "BODY-A"
)
second = self._create_command_preset(
temp_dir, "priority-second", "speckit.demo", "Second", "BODY-B"
)
manager.install_from_directory(first, "0.1.5", priority=10)
manager.install_from_directory(second, "0.1.5", priority=20)

command_file = commands_dir / "speckit.demo.toml"
assert "BODY-A" in command_file.read_text()

with patch.object(Path, "cwd", return_value=project_dir):
result = CliRunner().invoke(
app, ["preset", "set-priority", "priority-second", "1"]
)

assert result.exit_code == 0, result.output
assert "BODY-B" in command_file.read_text()
assert "BODY-A" not in command_file.read_text()

def test_set_priority_includes_convention_only_commands(self, project_dir, temp_dir):
"""A convention command omitted from the manifest still participates."""
from unittest.mock import patch

import yaml
from typer.testing import CliRunner

from specify_cli import app

self._write_init_options(project_dir, ai="gemini", ai_skills=False)
commands_dir = project_dir / ".gemini" / "commands"
commands_dir.mkdir(parents=True)
manager = PresetManager(project_dir)
first = self._create_command_preset(
temp_dir, "priority-convention", "speckit.demo", "First", "BODY-A"
)
second = self._create_command_preset(
temp_dir, "priority-declared", "speckit.demo", "Second", "BODY-B"
)
manifest_path = first / "preset.yml"
manifest = yaml.safe_load(manifest_path.read_text())
(first / "templates").mkdir()
(first / "templates" / "spec-template.md").write_text("# Spec\n")
manifest["provides"]["templates"] = [
{
"type": "template",
"name": "spec-template",
"file": "templates/spec-template.md",
}
]
manifest_path.write_text(yaml.safe_dump(manifest))
manager.install_from_directory(first, "0.1.5", priority=10)
manager.install_from_directory(second, "0.1.5", priority=20)

command_file = commands_dir / "speckit.demo.toml"
assert "BODY-A" in command_file.read_text()
with patch.object(Path, "cwd", return_value=project_dir):
result = CliRunner().invoke(
app, ["preset", "set-priority", "priority-convention", "30"]
)

assert result.exit_code == 0, result.output
assert "BODY-B" in command_file.read_text()
assert "BODY-A" not in command_file.read_text()

def test_set_priority_disabled_preset_keeps_materialized_command(
self, project_dir, temp_dir
):
"""Changing a disabled preset's priority leaves active artifacts alone."""
from unittest.mock import patch

from typer.testing import CliRunner

from specify_cli import app

self._write_init_options(project_dir, ai="gemini", ai_skills=False)
commands_dir = project_dir / ".gemini" / "commands"
commands_dir.mkdir(parents=True)
manager = PresetManager(project_dir)
preset_dir = self._create_command_preset(
temp_dir, "disabled-priority", "speckit.demo", "Disabled", "FROZEN"
)
manager.install_from_directory(preset_dir, "0.1.5")
manager.registry.update("disabled-priority", {"enabled": False})
command_file = commands_dir / "speckit.demo.toml"
assert "FROZEN" in command_file.read_text()

with patch.object(Path, "cwd", return_value=project_dir):
result = CliRunner().invoke(
app, ["preset", "set-priority", "disabled-priority", "25"]
)

assert result.exit_code == 0, result.output
assert PresetManager(project_dir).registry.get("disabled-priority")["priority"] == 25
assert "FROZEN" in command_file.read_text()
Comment on lines +177 to +186

def test_set_priority_reconciles_skill_rendering(self, project_dir, temp_dir):
"""Priority changes select the winning command when rendered as skills."""
from unittest.mock import patch

from typer.testing import CliRunner

from specify_cli import app

self._write_init_options(project_dir, ai="claude", ai_skills=True)
manager = PresetManager(project_dir)
first = self._create_command_preset(
temp_dir, "skill-priority-first", "speckit.demo", "First", "BODY-A"
)
second = self._create_command_preset(
temp_dir, "skill-priority-second", "speckit.demo", "Second", "BODY-B"
)
manager.install_from_directory(first, "0.1.5", priority=10)
manager.install_from_directory(second, "0.1.5", priority=20)
skill_file = project_dir / ".claude" / "skills" / "speckit-demo" / "SKILL.md"
assert "BODY-A" in skill_file.read_text()

with patch.object(Path, "cwd", return_value=project_dir):
result = CliRunner().invoke(
app, ["preset", "set-priority", "skill-priority-second", "1"]
)

assert result.exit_code == 0, result.output
assert "BODY-B" in skill_file.read_text()
assert "BODY-A" not in skill_file.read_text()

def test_set_priority_reconciliation_failure_keeps_priority_and_constitution(
self, project_dir, temp_dir, monkeypatch
):
"""Command refresh errors warn without rolling back priority or constitution."""
from unittest.mock import patch

import pytest
from typer.testing import CliRunner

from specify_cli import app

self._write_init_options(project_dir, ai="gemini", ai_skills=False)
(project_dir / ".gemini" / "commands").mkdir(parents=True)
manager = PresetManager(project_dir)
preset_dir = self._create_command_preset(
temp_dir, "priority-failure", "speckit.demo", "Failure", "BODY"
)
manager.install_from_directory(preset_dir, "0.1.5")
called = []

def fail_reconcile(self, command_names):
raise OSError("forced command write failure")

def reconcile_constitution(self, message):
called.append(message)

monkeypatch.setattr(PresetManager, "_reconcile_composed_commands", fail_reconcile)
monkeypatch.setattr(PresetManager, "reconcile_constitution", reconcile_constitution)
with patch.object(Path, "cwd", return_value=project_dir), pytest.warns(
UserWarning, match="forced command write failure"
):
result = CliRunner().invoke(
app, ["preset", "set-priority", "priority-failure", "2"]
)

assert result.exit_code == 0, result.output
assert PresetManager(project_dir).registry.get("priority-failure")["priority"] == 2
assert called

def test_set_priority_same_value_no_change(self, project_dir, pack_dir):
"""Test set-priority with same value shows already set message."""
from unittest.mock import patch
Expand Down