Skip to content
64 changes: 52 additions & 12 deletions src/specify_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,19 @@ def download_template_from_github(ai_assistant: str, download_dir: Path, *, scri
}
return zip_path, metadata

def download_and_extract_template(project_path: Path, ai_assistant: str, script_type: str, is_current_dir: bool = False, *, verbose: bool = True, tracker: StepTracker | None = None, client: httpx.Client = None, debug: bool = False, github_token: str = None) -> Path:
def download_and_extract_template(
project_path: Path,
ai_assistant: str,
script_type: str,
is_current_dir: bool = False,
*,
skip_legacy_codex_prompts: bool = False,
verbose: bool = True,
tracker: StepTracker | None = None,
client: httpx.Client = None,
debug: bool = False,
github_token: str = None,
) -> Path:
Comment thread
RbBtSn0w marked this conversation as resolved.
"""Download the latest release and extract it to create a new project.
Returns project_path. Uses tracker if provided (with keys: fetch, download, extract, cleanup)
"""
Comment thread
RbBtSn0w marked this conversation as resolved.
Expand Down Expand Up @@ -1019,6 +1031,10 @@ def download_and_extract_template(project_path: Path, ai_assistant: str, script_
console.print("[cyan]Found nested directory structure[/cyan]")

for item in source_dir.iterdir():
# Codex skills mode should not materialize legacy prompt files
# from older template archives.
if skip_legacy_codex_prompts and ai_assistant == "codex" and item.name == ".codex":
continue
Comment thread
RbBtSn0w marked this conversation as resolved.
Outdated
dest_path = project_path / item.name
if item.is_dir():
if dest_path.exists():
Expand Down Expand Up @@ -1069,6 +1085,11 @@ def download_and_extract_template(project_path: Path, ai_assistant: str, script_
elif verbose:
console.print("[cyan]Flattened nested directory structure[/cyan]")

if skip_legacy_codex_prompts and ai_assistant == "codex":
Comment thread
RbBtSn0w marked this conversation as resolved.
legacy_codex_dir = project_path / ".codex"
if legacy_codex_dir.is_dir():
shutil.rmtree(legacy_codex_dir, ignore_errors=True)
Comment thread
RbBtSn0w marked this conversation as resolved.

except Exception as e:
if tracker:
tracker.error("extract", str(e))
Expand Down Expand Up @@ -1994,7 +2015,18 @@ def init(

if use_github:
with httpx.Client(verify=local_ssl_context) as local_client:
download_and_extract_template(project_path, selected_ai, selected_script, here, verbose=False, tracker=tracker, client=local_client, debug=debug, github_token=github_token)
download_and_extract_template(
project_path,
selected_ai,
selected_script,
here,
skip_legacy_codex_prompts=(selected_ai == "codex" and ai_skills),
verbose=False,
tracker=tracker,
client=local_client,
debug=debug,
github_token=github_token,
)
else:
scaffold_ok = scaffold_from_core_pack(project_path, selected_ai, selected_script, here, tracker=tracker)
if not scaffold_ok:
Expand All @@ -2013,7 +2045,6 @@ def init(
if not here and project_path.exists():
shutil.rmtree(project_path)
raise typer.Exit(1)

# For generic agent, rename placeholder directory to user-specified path
if selected_ai == "generic" and ai_commands_dir:
placeholder_dir = project_path / ".speckit" / "commands"
Expand All @@ -2033,16 +2064,25 @@ def init(
if ai_skills:
if selected_ai in NATIVE_SKILLS_AGENTS:
skills_dir = _get_skills_dir(project_path, selected_ai)
if not _has_bundled_skills(project_path, selected_ai):
raise RuntimeError(
f"Expected bundled agent skills in {skills_dir.relative_to(project_path)}, "
"but none were found. Re-run with an up-to-date template."
)
if tracker:
tracker.start("ai-skills")
tracker.complete("ai-skills", f"bundled skills → {skills_dir.relative_to(project_path)}")
bundled_found = _has_bundled_skills(project_path, selected_ai)
if bundled_found:
if tracker:
tracker.start("ai-skills")
tracker.complete("ai-skills", f"bundled skills → {skills_dir.relative_to(project_path)}")
else:
console.print(f"[green]✓[/green] Using bundled agent skills in {skills_dir.relative_to(project_path)}/")
Comment thread
RbBtSn0w marked this conversation as resolved.
else:
console.print(f"[green]✓[/green] Using bundled agent skills in {skills_dir.relative_to(project_path)}/")
# Compatibility fallback: convert command templates to skills
# when an older template archive does not include native skills.
# This keeps `specify init --here --ai codex --ai-skills` usable
# in repos that already contain unrelated skills under .agents/skills.
fallback_ok = install_ai_skills(project_path, selected_ai, tracker=tracker)
if not fallback_ok:
raise RuntimeError(
f"Expected bundled agent skills in {skills_dir.relative_to(project_path)}, "
"but none were found and fallback conversion failed. "
"Re-run with an up-to-date template."
)
else:
skills_ok = install_ai_skills(project_path, selected_ai, tracker=tracker)

Expand Down
18 changes: 9 additions & 9 deletions tests/test_ai_skills.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,8 @@ def fake_download(project_path, *args, **kwargs):
mock_skills.assert_not_called()
assert (target / ".agents" / "skills" / "speckit-specify" / "SKILL.md").exists()

def test_codex_native_skills_missing_fails_clearly(self, tmp_path):
"""Codex native skills init should fail if bundled skills are missing."""
def test_codex_native_skills_missing_falls_back_then_fails_cleanly(self, tmp_path):
"""Codex should attempt fallback conversion when bundled skills are missing."""
from typer.testing import CliRunner

runner = CliRunner()
Expand All @@ -730,7 +730,7 @@ def test_codex_native_skills_missing_fails_clearly(self, tmp_path):
with patch("specify_cli.download_and_extract_template", lambda *args, **kwargs: None), \
patch("specify_cli.ensure_executable_scripts"), \
patch("specify_cli.ensure_constitution_from_template"), \
patch("specify_cli.install_ai_skills") as mock_skills, \
patch("specify_cli.install_ai_skills", return_value=False) as mock_skills, \
patch("specify_cli.is_git_repo", return_value=False), \
patch("specify_cli.shutil.which", return_value="/usr/bin/codex"):
result = runner.invoke(
Expand All @@ -739,11 +739,12 @@ def test_codex_native_skills_missing_fails_clearly(self, tmp_path):
)

assert result.exit_code == 1
mock_skills.assert_not_called()
mock_skills.assert_called_once()
assert "Expected bundled agent skills" in result.output
assert "fallback conversion failed" in result.output
Comment thread
RbBtSn0w marked this conversation as resolved.

def test_codex_native_skills_ignores_non_speckit_skill_dirs(self, tmp_path):
"""Non-spec-kit SKILL.md files should not satisfy Codex bundled-skills validation."""
"""Non-spec-kit SKILL.md files should trigger fallback conversion, not hard-fail."""
from typer.testing import CliRunner

runner = CliRunner()
Expand All @@ -757,17 +758,16 @@ def fake_download(project_path, *args, **kwargs):
with patch("specify_cli.download_and_extract_template", side_effect=fake_download), \
patch("specify_cli.ensure_executable_scripts"), \
patch("specify_cli.ensure_constitution_from_template"), \
patch("specify_cli.install_ai_skills") as mock_skills, \
patch("specify_cli.install_ai_skills", return_value=True) as mock_skills, \
patch("specify_cli.is_git_repo", return_value=False), \
patch("specify_cli.shutil.which", return_value="/usr/bin/codex"):
result = runner.invoke(
app,
["init", str(target), "--ai", "codex", "--ai-skills", "--script", "sh", "--no-git"],
)

assert result.exit_code == 1
mock_skills.assert_not_called()
assert "Expected bundled agent skills" in result.output
assert result.exit_code == 0
mock_skills.assert_called_once()

Comment thread
RbBtSn0w marked this conversation as resolved.
def test_commands_preserved_when_skills_fail(self, tmp_path):
"""If skills fail, commands should NOT be removed (safety net)."""
Expand Down