Skip to content
Merged
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
32 changes: 18 additions & 14 deletions src/specify_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@
# MAS-specific imports for stack-aware initialization
from .mas import (
MasStack,
example_stack_init_command,
format_valid_stack_ids,
get_mas_stack,
write_stack_memory_files,
example_stack_init_command as example_stack_init_command,
format_valid_stack_ids as format_valid_stack_ids,
get_mas_stack as get_mas_stack,
write_stack_memory_files as write_stack_memory_files,
)
from .stack_skills import (
DEFAULT_INSTALL_TIMEOUT,
external_skills_to_init_payload,
install_stack_external_skills,
pending_external_skills_payload,
render_external_skills_failure_panel,
render_external_skills_summary_panel,
resolve_stack_external_skills,
write_external_skills_status,
DEFAULT_INSTALL_TIMEOUT as DEFAULT_INSTALL_TIMEOUT,
external_skills_to_init_payload as external_skills_to_init_payload,
install_stack_external_skills as install_stack_external_skills,
pending_external_skills_payload as pending_external_skills_payload,
render_external_skills_failure_panel as render_external_skills_failure_panel,
render_external_skills_summary_panel as render_external_skills_summary_panel,
resolve_stack_external_skills as resolve_stack_external_skills,
write_external_skills_status as write_external_skills_status,
)

app = typer.Typer(
Expand Down Expand Up @@ -491,11 +491,15 @@ def ensure_specify_gitignored(project_path: Path, tracker: StepTracker | None =
tracker.complete("gitignore", "already ignored")
return

from .integrations._file_changes import write_text

separator = "" if content == "" or content.endswith(("\n", "\r")) else "\n"
gitignore.write_text(f"{content}{separator}{entry}\n", encoding="utf-8")
write_text(gitignore, f"{content}{separator}{entry}\n")
detail = "updated"
else:
gitignore.write_text(f"{entry}\n", encoding="utf-8")
from .integrations._file_changes import write_text

write_text(gitignore, f"{entry}\n")
detail = "created"

if tracker:
Expand Down
1 change: 1 addition & 0 deletions src/specify_cli/bundles/_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def _run_init(integration: str, *, script_type: str, offline: bool = False) -> N
github_token=None,
offline=offline,
preset=None,
stack="cakephp2-mysql",
integration=integration,
integration_options=None,
extensions=None,
Expand Down
69 changes: 67 additions & 2 deletions src/specify_cli/command_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
resolve_default_init_integration,
)
from ._assets import (
_locate_bundled_preset,
_locate_bundled_workflow,
get_speckit_version,
)
Expand Down Expand Up @@ -383,7 +382,6 @@ def init(
# Lazy imports to avoid circular dependency — __init__.py imports this module
from . import (
_install_shared_infra_or_exit,
_print_cli_warning,
ensure_executable_scripts,
ensure_specify_gitignored,
ensure_constitution_from_template,
Expand Down Expand Up @@ -838,6 +836,21 @@ def init(
install_mas_stack_presets(project_path, mas_stack)
tracker.complete("mas-presets", " + ".join(mas_stack.preset_composition))

# Preset registration rewrites wrapped commands after the
# integration manifest snapshot. Re-hash only the files whose
# bytes changed so a fresh init is not reported as modified.
from .integrations._helpers import _resync_manifest_after_registration

_resync_manifest_after_registration(
manifest,
resolved_integration.key,
unmodified_files=set(manifest.files),
continuing=(
"MAS presets were installed, but integration manifest"
" hashes may be stale."
),
)

# Install external skills from stack
tracker.start("stack-skills")
ai_skills_enabled = bool(init_opts.get("ai_skills"))
Expand Down Expand Up @@ -1126,31 +1139,50 @@ def _persist_external_skills_progress(results: list) -> None:
)

codex_skill_mode = selected_ai == "codex" and _is_skills_integration
zcode_skill_mode = selected_ai == "zcode" and _is_skills_integration
claude_skill_mode = selected_ai == "claude" and _is_skills_integration
kimi_skill_mode = selected_ai == "kimi"
mcode_skill_mode = selected_ai == "mcode" and _is_skills_integration
agy_skill_mode = selected_ai == "agy" and _is_skills_integration
trae_skill_mode = selected_ai == "trae"
cursor_agent_skill_mode = (
selected_ai == "cursor-agent" and _is_skills_integration
)
copilot_skill_mode = selected_ai == "copilot" and _is_skills_integration
devin_skill_mode = selected_ai == "devin"
zed_skill_mode = selected_ai == "zed" and _is_skills_integration
muse_skill_mode = selected_ai == "muse" and _is_skills_integration
grok_skill_mode = selected_ai == "grok" and _is_skills_integration
dsh_skill_mode = selected_ai == "dsh" and _is_skills_integration
bob_skill_mode = selected_ai == "bob" and _is_skills_integration
native_skill_mode = (
codex_skill_mode
or zcode_skill_mode
or claude_skill_mode
or kimi_skill_mode
or mcode_skill_mode
or agy_skill_mode
or trae_skill_mode
or cursor_agent_skill_mode
or copilot_skill_mode
or devin_skill_mode
or zed_skill_mode
or muse_skill_mode
or grok_skill_mode
or dsh_skill_mode
or bob_skill_mode
)

if codex_skill_mode:
steps_lines.append(
f"{step_num}. Start Codex in this project directory; spec-kit skills were installed to [cyan].agents/skills[/cyan]"
)
step_num += 1
if zcode_skill_mode:
steps_lines.append(
f"{step_num}. Start ZCode in this project directory; spec-kit skills were installed to [cyan].zcode/skills[/cyan]"
)
step_num += 1
if claude_skill_mode:
steps_lines.append(
f"{step_num}. Start Claude in this project directory; spec-kit skills were installed to [cyan].claude/skills[/cyan]"
Expand All @@ -1166,6 +1198,36 @@ def _persist_external_skills_progress(results: list) -> None:
f"{step_num}. Start Devin in this project directory; spec-kit skills were installed to [cyan].devin/skills[/cyan]"
)
step_num += 1
if zed_skill_mode:
steps_lines.append(
f"{step_num}. Start Zed in this project directory; spec-kit skills were installed to [cyan].agents/skills[/cyan]"
)
step_num += 1
if muse_skill_mode:
steps_lines.append(
f"{step_num}. Start Muse Code in this project directory; spec-kit skills were installed to [cyan].agents/skills[/cyan]"
)
step_num += 1
if grok_skill_mode:
steps_lines.append(
f"{step_num}. Start Grok Build in this project directory; spec-kit skills were installed to [cyan].grok/skills[/cyan]"
)
step_num += 1
if dsh_skill_mode:
steps_lines.append(
f"{step_num}. Start DSH ([cyan]dsh web[/cyan]) in this project directory; spec-kit skills were installed to [cyan].dsh/skills[/cyan]"
)
step_num += 1
if bob_skill_mode:
steps_lines.append(
f"{step_num}. Start Bob in this project directory; spec-kit skills were installed to [cyan].bob/skills[/cyan]"
)
step_num += 1
if mcode_skill_mode:
steps_lines.append(
f"{step_num}. Start MiniMax Code in this project directory; spec-kit skills were installed to [cyan].minimax/skills[/cyan]"
)
step_num += 1
usage_label = "skills" if native_skill_mode else "slash commands"

invoke_prefix = _invoke_prefix_for_integration(
Expand Down Expand Up @@ -1200,6 +1262,9 @@ def _display_cmd(name: str) -> str:
steps_lines.append(
f" {step_num}.5 [cyan]{_display_cmd('implement')}[/] - Execute implementation"
)
steps_lines.append(
f" {step_num}.6 [cyan]{_display_cmd('converge')}[/] - Assess the codebase and append remaining work as tasks"
)

steps_panel = Panel(
"\n".join(steps_lines),
Expand Down
4 changes: 3 additions & 1 deletion src/specify_cli/mas.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,10 @@ def write_stack_memory_files(project_root: Path, stack: MasStack) -> None:
"stack-context.md": stack.render_stack_context_md(),
"language-policy.md": render_language_policy_md(),
}
from .integrations._file_changes import write_text

for filename, content in files.items():
(memory_dir / filename).write_text(content, encoding="utf-8")
write_text(memory_dir / filename, content)


def valid_stack_ids() -> tuple[str, ...]:
Expand Down
113 changes: 113 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import os
import re
import shlex
import shutil
import subprocess
import sys
from contextvars import ContextVar
from pathlib import Path

import pytest
Expand Down Expand Up @@ -152,6 +154,117 @@ def _isolate_auth_config(monkeypatch):
monkeypatch.setattr(_auth_http, "_config_cache", None)


# Stack shipped on MAS main. Upstream tests call ``specify init`` without
# ``--stack``; the fork rejects that. Injection is test-only and does not
# change the CLI. Tests that assert the missing-stack rejection opt out.
UPSTREAM_INIT_STACK = "cakephp2-mysql"
# Files a stack-aware init adds on top of the upstream project tree when the
# suite supplies ``cakephp2-mysql``. Inventory tests append this list.
MAS_INIT_EXTRA_FILES = [
".gitignore",
".specify/memory/language-policy.md",
".specify/memory/security-guidelines.md",
".specify/memory/stack-context.md",
".specify/memory/stack.md",
".specify/presets/.registry",
".specify/presets/mas-core/.composed/speckit.checklist.md",
".specify/presets/mas-core/.composed/speckit.clarify.md",
".specify/presets/mas-core/.composed/speckit.plan.md",
".specify/presets/mas-core/.composed/speckit.specify.md",
".specify/presets/mas-core/.composed/speckit.tasks.md",
".specify/presets/mas-core/commands/speckit.checklist.md",
".specify/presets/mas-core/commands/speckit.clarify.md",
".specify/presets/mas-core/commands/speckit.plan.md",
".specify/presets/mas-core/commands/speckit.specify.md",
".specify/presets/mas-core/commands/speckit.tasks.md",
".specify/presets/mas-core/preset.yml",
".specify/presets/mas-core/stack-skills.yml",
".specify/presets/mas-core/templates/constitution-template.md",
".specify/presets/mas-core/templates/plan-template.md",
".specify/presets/mas-core/templates/spec-template-append.md",
".specify/presets/mas-core/templates/tasks-template.md",
".specify/presets/mas-stack-cakephp2-mysql/preset.yml",
".specify/presets/mas-stack-cakephp2-mysql/stack-skills.yml",
".specify/presets/mas-stack-cakephp2-mysql/templates/mas-stack-cakephp2-mysql.md",
]
_SKIP_DEFAULT_STACK_TESTS = frozenset({
"test_init_rejects_missing_stack_before_side_effects",
})
_INJECT_UPSTREAM_INIT_STACK: ContextVar[bool] = ContextVar(
"inject_upstream_init_stack", default=True
)


def _with_upstream_init_stack(args):
"""Return ``args`` with a valid MAS ``--stack`` when ``init`` omitted one.

``--preset`` is left untouched: the fork treats it as mutually exclusive
with ``--stack``. ``--help`` is left untouched so help snapshots stay stable.
"""
if args is None:
return args
as_string = isinstance(args, str)
try:
tokens = shlex.split(args) if as_string else list(args)
except (TypeError, ValueError):
return args
if not tokens or tokens[0] != "init":
return args
if "--help" in tokens or "-h" in tokens:
return args
if "--stack" not in tokens and "--preset" not in tokens:
tokens = [tokens[0], "--stack", UPSTREAM_INIT_STACK, *tokens[1:]]
# Upstream init does not opt out of stack skill installs. When npx is on
# PATH those installs reach the network and do not exercise the assertion.
# Tests of the skill installer keep the real flag.
if "--skip-external-skills" not in tokens and "--preset" not in tokens:
tokens = [*tokens, "--skip-external-skills"]
injected = tokens
if as_string:
return shlex.join(injected)
if isinstance(args, tuple):
return tuple(injected)
return injected


@pytest.fixture(scope="session", autouse=True)
def _supply_stack_to_upstream_init():
"""Give upstream ``specify init`` invocations a stack from main.

The patch is session-scoped so module-scoped project templates, which
initialize before function fixtures, receive it too. MAS tests that
already pass ``--stack`` are unchanged. Tests that assert the missing
stack, or that install external skills, turn injection off.
"""
from typer.testing import CliRunner

original = CliRunner.invoke

def invoke(self, cli, args=None, *invoke_args, **invoke_kwargs):
if _INJECT_UPSTREAM_INIT_STACK.get():
args = _with_upstream_init_stack(args)
return original(self, cli, args, *invoke_args, **invoke_kwargs)

CliRunner.invoke = invoke
yield
CliRunner.invoke = original


@pytest.fixture(autouse=True)
def _skip_upstream_init_stack_injection(request):
if request.node.originalname in _SKIP_DEFAULT_STACK_TESTS:
token = _INJECT_UPSTREAM_INIT_STACK.set(False)
yield
_INJECT_UPSTREAM_INIT_STACK.reset(token)
return
if getattr(request.node, "path", None) and request.node.path.name == "test_stack_external_skills.py":
token = _INJECT_UPSTREAM_INIT_STACK.set(False)
yield
_INJECT_UPSTREAM_INIT_STACK.reset(token)
return
yield


@pytest.fixture(autouse=True)
def _strip_specify_env(monkeypatch):
"""Drop any inherited SPECIFY_* vars for every test.
Expand Down
3 changes: 2 additions & 1 deletion tests/integrations/test_integration_base_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from specify_cli.integrations import INTEGRATION_REGISTRY, get_integration
from specify_cli.integrations.base import MarkdownIntegration
from specify_cli.integrations.manifest import IntegrationManifest
from tests.conftest import MAS_INIT_EXTRA_FILES


class MarkdownIntegrationTests:
Expand Down Expand Up @@ -262,7 +263,7 @@ def _expected_files(self, script_variant: str) -> list[str]:



return sorted(files)
return sorted([*files, *MAS_INIT_EXTRA_FILES])

def test_complete_file_inventory_sh(self, tmp_path):
"""Every file produced by specify init --integration <key> --script sh."""
Expand Down
3 changes: 2 additions & 1 deletion tests/integrations/test_integration_base_skills.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from specify_cli.integrations import INTEGRATION_REGISTRY, get_integration
from specify_cli.integrations.base import SkillsIntegration
from specify_cli.integrations.manifest import IntegrationManifest
from tests.conftest import MAS_INIT_EXTRA_FILES


class SkillsIntegrationTests:
Expand Down Expand Up @@ -520,7 +521,7 @@ def _expected_files(self, script_variant: str) -> list[str]:
".specify/workflows/speckit/workflow.yml",
".specify/workflows/workflow-registry.json",
]
return sorted(files)
return sorted([*files, *MAS_INIT_EXTRA_FILES])

def test_complete_file_inventory_sh(self, tmp_path):
"""Every file produced by specify init --integration <key> --script sh."""
Expand Down
3 changes: 2 additions & 1 deletion tests/integrations/test_integration_base_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from specify_cli.integrations import INTEGRATION_REGISTRY, get_integration
from specify_cli.integrations.base import TomlIntegration
from specify_cli.integrations.manifest import IntegrationManifest
from tests.conftest import MAS_INIT_EXTRA_FILES


class TomlIntegrationTests:
Expand Down Expand Up @@ -527,7 +528,7 @@ def _expected_files(self, script_variant: str) -> list[str]:



return sorted(files)
return sorted([*files, *MAS_INIT_EXTRA_FILES])

def test_complete_file_inventory_sh(self, tmp_path):
"""Every file produced by specify init --integration <key> --script sh."""
Expand Down
Loading
Loading