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
118 changes: 46 additions & 72 deletions src/specify_cli/workflows/step/init/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

Runs the same scaffolding as ``specify init`` so a workflow can create
(or merge into) a project before driving the rest of the spec-driven
process. The step invokes the ``init`` command in-process and captures
its exit code and output.
process. The step invokes the ``init`` command in an isolated child
process and captures its exit code and output.
"""

from __future__ import annotations

import os
from pathlib import Path
import subprocess
import sys
from typing import Any

from specify_cli._agent_config import (
Expand All @@ -27,6 +30,15 @@
#: only pre-existing content is engine run-state.
_ENGINE_OWNED_DIRS = {".specify"}

# Pin the child to this package, independent of its cwd and PATH. The source
# parent travels as argv data, never as interpolated Python or shell source.
_INIT_BOOTSTRAP = (
"import sys; "
"sys.path.insert(0, sys.argv.pop(1)); "
"sys.argv[0] = 'specify'; "
"from specify_cli import main; main()"
)


class InitStep(StepBase):
"""Bootstrap a project, equivalent to running ``specify init``.
Expand Down Expand Up @@ -76,6 +88,12 @@ class InitStep(StepBase):
type_key = "init"

def execute(self, config: dict[str, Any], context: StepContext) -> StepResult:
# Capture one absolute base for preflight and execution. Joining a
# relative root preserves traversal/symlink spelling for CLI validation.
base = os.fspath(context.project_root) if context.project_root else os.getcwd()
if not os.path.isabs(base):
base = os.path.join(os.getcwd(), base)

project = self._resolve(config.get("project"), context)
here = self._resolve_bool(config.get("here"), context)

Expand Down Expand Up @@ -138,7 +156,6 @@ def execute(self, config: dict[str, Any], context: StepContext) -> StepResult:
# --force so init can proceed unattended.
targets_current_dir = here or not project or str(project) == "."
if targets_current_dir and not force:
base = context.project_root or os.getcwd()
has_engine_dirs = False
try:
with os.scandir(base) as it:
Expand Down Expand Up @@ -209,7 +226,7 @@ def execute(self, config: dict[str, Any], context: StepContext) -> StepResult:
if force:
argv.append("--force")

exit_code, stdout, stderr = self._run_init(argv, context)
exit_code, stdout, stderr = self._run_init(argv, base)

output: dict[str, Any] = {
"argv": argv,
Expand Down Expand Up @@ -254,77 +271,34 @@ def _resolve_bool(cls, value: Any, context: StepContext) -> bool:
return bool(resolved)

@staticmethod
def _run_init(
argv: list[str], context: StepContext
) -> tuple[int, str, str]:
"""Invoke ``specify init`` in-process and capture exit code/output.

Runs with the working directory set to ``context.project_root`` so
that ``--here`` and relative project paths target the right place.
"""
from typer.testing import CliRunner
def _run_init(argv: list[str], base: str) -> tuple[int, str, str]:
"""Run the real CLI without changing parent cwd, streams or registries."""
import specify_cli

from specify_cli import app

runner = CliRunner()

prev_cwd = os.getcwd()
if context.project_root:
try:
os.chdir(context.project_root)
except OSError as exc:
return (1, "", f"Cannot enter project root: {exc}")
try:
result = runner.invoke(app, argv, catch_exceptions=True)
finally:
try:
os.chdir(prev_cwd)
except OSError:
# Best-effort cleanup: avoid masking the init command result
# if restoring the previous working directory fails.
pass

stdout = result.output or ""
# click >= 8.2 captures stderr separately; older versions mix it into
# stdout and raise when ``result.stderr`` is accessed.
package_parent = str(Path(specify_cli.__file__).resolve().parent.parent)
child_env = os.environ.copy()
child_env["PYTHONIOENCODING"] = "utf-8"
try:
stderr = result.stderr or ""
except (ValueError, AttributeError):
# Older Click: stderr is mixed into stdout. On failure, treat
# stdout as stderr so workflows can consistently read
# steps.<id>.output.stderr for error details.
stderr = stdout if result.exit_code != 0 else ""

# Record the exception only for an UNEXPECTED crash. ``typer.Exit(n)``
# -- how ``specify init`` reports every ordinary failure -- surfaces
# through ``CliRunner`` as ``result.exception = SystemExit(n)``, so this
# branch used to fire on routine errors too. ``init`` prints its
# diagnostics through Rich to stdout, leaving ``result.stderr`` empty,
# so the synthesized "SystemExit: 1" became the whole of ``stderr`` and
# preempted ``execute``'s ``stderr.strip() or stdout.strip()`` fallback.
# Every failing init step then reported ``error: 'SystemExit: 1'`` while
# init's real message -- e.g. the list of valid integrations for a
# typo'd ``integration:`` -- sat unread in stdout.
if (
result.exit_code != 0
and result.exception is not None
and not isinstance(result.exception, SystemExit)
):
detail = f"{type(result.exception).__name__}: {result.exception}"
stderr = f"{stderr}\n{detail}".strip() if stderr else detail
elif result.exit_code != 0 and not stderr.strip():
# Ordinary failure with no real stderr: under click >= 8.2 the
# streams are separate and ``init`` prints its diagnostics through
# Rich to stdout, so ``result.stderr`` is genuinely empty. Merely
# dropping the synthesized "SystemExit: 1" would leave
# ``steps.<id>.output.stderr`` empty -- still no diagnostic for a
# downstream step to read, and still contrary to the contract above
# ("treat stdout as stderr so workflows can consistently read
# steps.<id>.output.stderr for error details"). Carry the captured
# output across, matching what the older-Click branch already does.
result = subprocess.run(
[sys.executable, "-P", "-c", _INIT_BOOTSTRAP, package_parent, *argv],
cwd=base,
stdin=subprocess.DEVNULL,
capture_output=True,
encoding="utf-8",
env=child_env,
check=False,
)
except (OSError, ValueError) as exc:
return (1, "", f"Cannot run specify init in project root {base!r}: {exc}")

stdout = result.stdout or ""
stderr = result.stderr or ""
if result.returncode != 0 and not stderr.strip():
# Ordinary init failures print Rich diagnostics to stdout. Keep
# output.stderr useful to downstream steps without inventing a
# SystemExit diagnostic or replacing a real child traceback.
stderr = stdout.strip()

return (result.exit_code, stdout, stderr)
return (result.returncode, stdout, stderr)

def validate(self, config: dict[str, Any]) -> list[str]:
errors = super().validate(config)
Expand Down
71 changes: 71 additions & 0 deletions tests/specify_cli/integrations/test_installed_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,77 @@ def snapshot(project):
}


@pytest.fixture
def init_step_environment(monkeypatch, tmp_path_factory):
from tests.test_workflows import _isolate_init_home

_isolate_init_home(monkeypatch, tmp_path_factory)


def test_init_step_reloads_trusted_target_adapter(tmp_path, server, init_step_environment):
from specify_cli.workflows.base import StepContext, StepStatus
from specify_cli.workflows.step.init import InitStep

publish(server, code=implementation(body='''
def setup(self, project_root, manifest, **kwargs):
import os
result = super().setup(project_root, manifest, **kwargs)
(project_root / "adapter-pid").write_text(str(os.getpid()))
return result
'''))
project = catalog_project(tmp_path, server)
install(project)
assert (project / "adapter-pid").read_text() == str(os.getpid())
unload_installed_integrations()
assert KEY not in INTEGRATION_REGISTRY
parent_registry = dict(INTEGRATION_REGISTRY)
result = InitStep().execute(
{"id": "bootstrap", "here": True, "force": True, "integration": KEY, "script": "py"},
StepContext(project_root=str(project)),
)
assert result.status == StepStatus.COMPLETED, result.error
assert (project / "adapter-pid").read_text() != str(os.getpid())
assert (project / ".sample-agent/skills/speckit-specify/SKILL.md").is_file()
assert (project / ".specify/memory/constitution.md").is_file()
assert (project / ".specify/workflows/speckit/workflow.yml").is_file()
assert json.loads((project / ".specify/init-options.json").read_text())["script"] == "py"
assert INTEGRATION_REGISTRY == parent_registry


@pytest.mark.parametrize("damage", ["untrusted", "tampered"])
def test_init_step_rejects_untrusted_target_adapter(tmp_path, server, damage, init_step_environment):
from specify_cli.workflows.base import StepContext, StepStatus
from specify_cli.workflows.step.init import InitStep

publish(server)
project = catalog_project(tmp_path, server)
install(project)
unload_installed_integrations()
if damage == "untrusted":
path = Path.home() / ".specify/integration-trust.json"
state = json.loads(path.read_text())
state["grants"] = []
path.write_text(json.dumps(state))
diagnostic = "no local trust decision"
else:
path = project / f".specify/integrations/packages/{KEY}/__init__.py"
path.write_text(path.read_text() + "\nraise RuntimeError('untrusted code executed')\n")
diagnostic = "has been modified"
before = snapshot(project)
trust_before = (Path.home() / ".specify/integration-trust.json").read_bytes()
result = InitStep().execute(
{"id": "bootstrap", "here": True, "force": True, "integration": KEY, "script": "py"},
StepContext(project_root=str(project)),
)
assert result.status == StepStatus.FAILED
assert diagnostic in " ".join(result.error.split())
assert "untrusted code executed" not in result.error
assert result.output["stderr"].strip()
assert snapshot(project) == before
assert (Path.home() / ".specify/integration-trust.json").read_bytes() == trust_before
assert KEY not in INTEGRATION_REGISTRY


@pytest.mark.parametrize("existing", [False, True])
@pytest.mark.parametrize("change", ["unchanged", "partial", "concurrent", "deleted"])
def test_round9_pending_write_preserves_unattributed_changes(tmp_path, server, monkeypatch, existing, change):
Expand Down
Loading
Loading