From 11a102d4b8f7eba2faf5636083c5a1ca826eb0ba Mon Sep 17 00:00:00 2001 From: WOLIKIMCHENG <35391914+WOLIKIMCHENG@users.noreply.github.com> Date: Thu, 19 Nov 2026 15:04:29 +0800 Subject: [PATCH] fix(workflows): isolate init step process state Run workflow init in a child process to keep concurrent project paths and output independent while preserving CLI behavior and adapter loading. Assisted-by: Codex CLI (model: gpt-6.1-sol, autonomous) --- .../workflows/step/init/__init__.py | 118 ++---- .../integrations/test_installed_adapters.py | 71 ++++ tests/test_workflows.py | 392 ++++++++++++++++-- tests/workflows/test_composition_execution.py | 172 ++++++++ 4 files changed, 655 insertions(+), 98 deletions(-) diff --git a/src/specify_cli/workflows/step/init/__init__.py b/src/specify_cli/workflows/step/init/__init__.py index 5dc04484a9..8685508a22 100644 --- a/src/specify_cli/workflows/step/init/__init__.py +++ b/src/specify_cli/workflows/step/init/__init__.py @@ -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 ( @@ -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``. @@ -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) @@ -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: @@ -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, @@ -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..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..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..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) diff --git a/tests/specify_cli/integrations/test_installed_adapters.py b/tests/specify_cli/integrations/test_installed_adapters.py index 46d6072225..1bbbe1e056 100644 --- a/tests/specify_cli/integrations/test_installed_adapters.py +++ b/tests/specify_cli/integrations/test_installed_adapters.py @@ -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): diff --git a/tests/test_workflows.py b/tests/test_workflows.py index 56baaa7b16..3f7565a1c7 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -3546,9 +3546,362 @@ def _force_gate_stdin(monkeypatch, *, tty: bool): monkeypatch.setattr(gate_module, "sys", _FakeSys(tty=tty)) +def _isolate_init_home(monkeypatch, tmp_path_factory): + """Keep child-process auth, trust and catalog state in fixture-owned homes.""" + home = tmp_path_factory.mktemp("init-home") + for key in ("HOME", "USERPROFILE", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"): + monkeypatch.setenv(key, str(home)) + for key in ("SPECKIT_INTEGRATION_DEFAULT", "SPECKIT_INTEGRATION_CATALOG_URL"): + monkeypatch.delenv(key, raising=False) + + +def _gate_init_launch(monkeypatch, before): + """Gate the real CLI boundary on both sides of the cwd-isolation fix.""" + from specify_cli.workflows.step import init + + if hasattr(init, "subprocess"): + original = init.subprocess.run + + def run(args, **kwargs): + if "init" in args: + before(args[args.index("init"):]) + return original(args, **kwargs) + + monkeypatch.setattr(init.subprocess, "run", run) + else: + from typer.testing import CliRunner + + original = CliRunner.invoke + + def invoke(self, app, args, **kwargs): + before(args) + return original(self, app, args, **kwargs) + + monkeypatch.setattr(CliRunner, "invoke", invoke) + + +def _assert_init_scaffold(root, script="sh"): + """Require generated files from real init, rather than a mocked mkdir.""" + from specify_cli._assets import get_speckit_version + + options = json.loads((root / ".specify/init-options.json").read_text(encoding="utf-8")) + assert options["speckit_version"] == get_speckit_version() + assert (root / ".specify/memory/constitution.md").is_file() + assert list((root / ".specify/workflows").glob("*/workflow.yml")) + directory = {"sh": "bash", "ps": "powershell", "py": "python"}[script] + assert list((root / ".specify/scripts" / directory).iterdir()) + assert (root / ".github/skills/speckit-specify/SKILL.md").is_file() + + +@pytest.fixture +def init_empty_catalog(tmp_path, tmp_path_factory, monkeypatch): + from tests.specify_cli.integrations.test_installed_adapters import server, tls_certificate + + certificate = tls_certificate.__wrapped__(tmp_path_factory) + fixture = server.__wrapped__(tmp_path_factory.mktemp("init-catalog"), monkeypatch, certificate) + catalog = next(fixture) + try: + (catalog.root / "catalog.json").write_text( + json.dumps({"schema_version": "1.0", "integrations": {}}), encoding="utf-8" + ) + directory = tmp_path / ".specify" + directory.mkdir() + (directory / "integration-catalogs.yml").write_text(yaml.safe_dump({ + "catalogs": [{"name": "empty", "url": f"{catalog.url}/catalog.json", + "install_allowed": False}], + }), encoding="utf-8") + yield catalog + finally: + fixture.close() + + class TestInitStep: """Test the init step type.""" + @pytest.fixture(autouse=True) + def isolated_home(self, monkeypatch, tmp_path_factory): + _isolate_init_home(monkeypatch, tmp_path_factory) + + @pytest.mark.parametrize("separate_roots", [False, True]) + def test_relative_projects_survive_interleaved_init(self, tmp_path, monkeypatch, separate_roots): + from concurrent.futures import ThreadPoolExecutor + from threading import Event + + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + launch = tmp_path / "launch" + launch.mkdir() + roots = [tmp_path / "root-a", tmp_path / ("root-b" if separate_roots else "root-a")] + for root in set(roots): + root.mkdir() + monkeypatch.chdir(launch) + streams = (sys.stdin, sys.stdout, sys.stderr) + entered = {name: Event() for name in ("alpha", "beta")} + release = {name: Event() for name in entered} + + def before(argv): + name = argv[1] + entered[name].set() + assert release[name].wait(30), f"Timed out releasing {name}" + + _gate_init_launch(monkeypatch, before) + step = InitStep() # The registry shares one instance across fan-out items. + contexts = [StepContext(project_root=str(root)) for root in roots] + configs = [{"id": name, "project": name, "script": "sh"} for name in entered] + pool = ThreadPoolExecutor(max_workers=2) + try: + first = pool.submit(step.execute, configs[0], contexts[0]) + assert entered["alpha"].wait(30) + second = pool.submit(step.execute, configs[1], contexts[1]) + assert entered["beta"].wait(30) + release["alpha"].set() + a = first.result(timeout=60) # Includes the old cwd-restoring finally. + release["beta"].set() + b = second.result(timeout=60) + final_cwd = Path.cwd() + finally: + for event in release.values(): + event.set() + pool.shutdown(wait=True) + os.chdir(launch) # Test coordinator cleanup only, after all workers exit. + + assert [a.status, b.status] == [StepStatus.COMPLETED] * 2, [a.error, b.error] + for root, name, result in zip(roots, entered, (a, b)): + _assert_init_scaffold(root / name) + assert name in result.output["stdout"] + assert result.output["stderr"] == "" + assert not (launch / name).exists() + assert [ctx.project_root for ctx in contexts] == [str(root) for root in roots] + assert final_cwd == launch + assert (sys.stdin, sys.stdout, sys.stderr) == streams + + @pytest.mark.parametrize("root_kind", ["absolute", "relative", "omitted"]) + @pytest.mark.parametrize("target", [{"here": True, "project": "ignored"}, {"project": "."}, {}]) + def test_captures_base_once_without_restoring_external_cwd( + self, tmp_path, monkeypatch, root_kind, target + ): + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + launch, root, moved = [tmp_path / name for name in ("launch", "root", "moved")] + for path in (launch, root, moved): + path.mkdir() + monkeypatch.chdir(launch) + base = {"absolute": str(root), "relative": "../root", "omitted": None}[root_kind] + expected = launch if base is None else root + streams = (sys.stdin, sys.stdout, sys.stderr) + environment = dict(os.environ) + _gate_init_launch(monkeypatch, lambda argv: os.chdir(moved)) + context = StepContext(project_root=base) + result = InitStep().execute({"id": "init", "script": "sh", **target}, context) + assert result.status == StepStatus.COMPLETED, result.error + _assert_init_scaffold(expected) + assert not (moved / ".specify").exists() + assert Path.cwd() == moved + assert (sys.stdin, sys.stdout, sys.stderr) == streams + assert dict(os.environ) == environment + assert context.project_root == base + + @pytest.mark.parametrize("target_kind", ["nested", "absolute", "parent"]) + @pytest.mark.parametrize("script", [None, "sh", "ps", "py"]) + def test_real_target_and_script_matrix(self, tmp_path, target_kind, script): + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + root = tmp_path / "root" + root.mkdir() + target = root / "nested/demo" if target_kind == "nested" else tmp_path / "space 项目" + project = {"absolute": str(target), "nested": "nested/demo", "parent": "../space 项目"}[target_kind] + config = {"id": "init", "project": project} + if script: + config["script"] = script + result = InitStep().execute(config, StepContext(project_root=str(root))) + assert result.status == StepStatus.COMPLETED, result.error + _assert_init_scaffold(target, script or ("ps" if os.name == "nt" else "sh")) + assert result.output["argv"][1] == project + + def test_force_preserves_unrelated_content_and_bundled_preset(self, tmp_path): + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + sentinel = tmp_path / "keep.bin" + sentinel.write_bytes(b"unrelated\x00bytes") + result = InitStep().execute( + {"id": "init", "here": True, "force": True, "preset": "lean", "script": "py"}, + StepContext(project_root=str(tmp_path)), + ) + assert result.status == StepStatus.COMPLETED, result.error + assert sentinel.read_bytes() == b"unrelated\x00bytes" + _assert_init_scaffold(tmp_path, "py") + assert (tmp_path / ".specify/presets/lean/preset.yml").is_file() + + def test_relative_root_preserves_symlink_parent_semantics(self, tmp_path, monkeypatch): + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + launch = tmp_path / "launch" + destination = tmp_path / "destination" + launch.mkdir() + (destination / "nested").mkdir(parents=True) + try: + (launch / "alias").symlink_to(destination / "nested", target_is_directory=True) + except OSError as exc: + pytest.skip(f"Directory symlinks unavailable: {exc}") + monkeypatch.chdir(launch) + context = StepContext(project_root="alias/..") + result = InitStep().execute({"id": "init", "project": "demo", "script": "sh"}, context) + assert result.status == StepStatus.COMPLETED, result.error + _assert_init_scaffold(destination / "demo") + assert not (launch / "demo").exists() + assert context.project_root == "alias/.." + + def test_explicit_integration_and_quoted_options_override_defaults(self, tmp_path, monkeypatch): + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + monkeypatch.setenv("SPECKIT_INTEGRATION_DEFAULT", "gemini") + result = InitStep().execute( + {"id": "init", "here": "{{ inputs.yes }}", "integration": "generic", + "integration_options": '--commands-dir "agent commands"', "script": "py"}, + StepContext(project_root=str(tmp_path), default_integration="copilot", inputs={"yes": True}), + ) + assert result.status == StepStatus.COMPLETED, result.error + assert result.output["integration"] == "generic" + assert (tmp_path / "agent commands/speckit.specify.md").is_file() + + @pytest.mark.parametrize("kind", [ + "missing-root", "file-root", "existing-target", "invalid-script", "missing-traversal", + ]) + def test_real_failures_preserve_parent_state(self, tmp_path, kind): + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + root = tmp_path / "root" + root.mkdir() + config = {"id": "init", "project": "demo", "script": "sh"} + if kind == "missing-root": + root = tmp_path / "missing" + elif kind == "file-root": + root = tmp_path / "file" + root.write_bytes(b"keep") + elif kind == "existing-target": + (root / "demo").mkdir() + (root / "demo/keep").write_bytes(b"keep") + elif kind == "missing-traversal": + config["project"] = "missing/../escape" + else: + config["script"] = "invalid" + state = (os.getcwd(), sys.stdin, sys.stdout, sys.stderr, dict(os.environ)) + result = InitStep().execute(config, StepContext(project_root=str(root))) + assert result.status == StepStatus.FAILED + assert result.error and result.error != "SystemExit: 1" + assert result.output["stderr"].strip() + assert state == (os.getcwd(), sys.stdin, sys.stdout, sys.stderr, dict(os.environ)) + assert not (tmp_path / "escape").exists() + assert not list(tmp_path.rglob("init-options.json")) + if kind == "existing-target": + assert (root / "demo/keep").read_bytes() == b"keep" + + def test_pins_interpreter_source_and_child_environment(self, tmp_path, monkeypatch): + import subprocess + import specify_cli + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step import init + + root = tmp_path / "root" + root.mkdir() + (root / "specify_cli.py").write_text("raise RuntimeError('shadow imported')", encoding="utf-8") + decoy = tmp_path / "bin" + decoy.mkdir() + for name in ("specify", "specify.cmd"): + path = decoy / name + path.write_text("exit 97\n", encoding="utf-8") + path.chmod(0o755) + monkeypatch.setenv("PATH", str(decoy) + os.pathsep + os.environ.get("PATH", "")) + original = subprocess.run + launches = [] + + def run(args, **kwargs): + if "init" in args: + launches.append((args, kwargs)) + return original(args, **kwargs) + + monkeypatch.setattr(subprocess, "run", run) + environment = dict(os.environ) + result = init.InitStep().execute( + {"id": "init", "project": "demo", "script": "py"}, StepContext(project_root=str(root)) + ) + assert result.status == StepStatus.COMPLETED, result.error + _assert_init_scaffold(root / "demo", "py") + assert len(launches) == 1 + args, options = launches[0] + assert args[0] == sys.executable + assert args[1:3] == ["-P", "-c"] + assert args[4] == str(Path(specify_cli.__file__).resolve().parent.parent) + assert options["cwd"] == str(root) + assert options["stdin"] == subprocess.DEVNULL + assert options["capture_output"] is True and options["check"] is False + assert options["encoding"] == "utf-8" + assert options["env"] is not os.environ + assert options["env"]["PYTHONIOENCODING"] == "utf-8" + assert dict(os.environ) == environment + + @pytest.mark.parametrize("stdout,stderr,expected", [ + ("diagnostic\n", "", "diagnostic"), ("out", "real error\n", "real error\n"), + ("", "", ""), ("", "Traceback\nRuntimeError: boom inside init", "Traceback\nRuntimeError: boom inside init"), + ]) + def test_child_failure_streams(self, tmp_path, monkeypatch, stdout, stderr, expected): + import subprocess + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + monkeypatch.setattr(subprocess, "run", lambda *a, **k: subprocess.CompletedProcess(a, 3, stdout, stderr)) + result = InitStep().execute({"id": "init", "project": "demo"}, StepContext(project_root=str(tmp_path))) + assert result.status == StepStatus.FAILED + assert result.output["stdout"] == stdout + assert result.output["stderr"] == expected + assert result.error == (expected.strip() or stdout.strip() or "specify init exited with code 3.") + + def test_launch_failure_is_actionable(self, tmp_path, monkeypatch): + import subprocess + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + def fail(*args, **kwargs): + raise OSError("fixture launch denied") + + monkeypatch.setattr(subprocess, "run", fail) + result = InitStep().execute({"id": "init", "project": "demo"}, StepContext(project_root=str(tmp_path))) + assert result.status == StepStatus.FAILED + assert str(tmp_path) in result.error and "fixture launch denied" in result.error + + def test_nul_project_argument_returns_failed_result(self, tmp_path): + """Real subprocess argument rejection must stay within the step result.""" + from specify_cli.workflows.base import StepContext, StepStatus + from specify_cli.workflows.step.init import InitStep + + step = InitStep() + config = {"id": "init", "project": "bad\x00name", "integration": "copilot", "script": "sh"} + context = StepContext(project_root=str(tmp_path)) + assert step.validate(config) == [] + state = (os.getcwd(), sys.stdin, sys.stdout, sys.stderr, dict(os.environ)) + try: + result = step.execute(config, context) + finally: + assert state == (os.getcwd(), sys.stdin, sys.stdout, sys.stderr, dict(os.environ)) + assert context.project_root == str(tmp_path) + assert list(tmp_path.iterdir()) == [] + + assert result.status == StepStatus.FAILED + assert result.output["exit_code"] == 1 + assert result.output["argv"][1] == "bad\x00name" + assert result.output["stdout"] == "" + assert result.error == result.output["stderr"] + assert "Cannot run specify init" in result.error + assert str(tmp_path) in result.error + assert "embedded null" in result.error.lower() + def test_docstring_lists_every_valid_script_type(self): # The `script` field docstring must not contradict the step's own # VALID_SCRIPT_TYPES (which includes 'py'); validate() accepts all three. @@ -3681,7 +4034,7 @@ def test_project_name_creates_subdirectory(self, tmp_path): assert result.status == StepStatus.COMPLETED assert (tmp_path / "demo" / ".specify").is_dir() - def test_invalid_integration_fails(self, tmp_path): + def test_invalid_integration_fails(self, tmp_path, init_empty_catalog): from specify_cli.workflows.step.init import InitStep from specify_cli.workflows.base import StepContext, StepStatus @@ -3700,7 +4053,7 @@ def test_invalid_integration_fails(self, tmp_path): assert result.output["exit_code"] != 0 assert result.error is not None - def test_failed_init_surfaces_inits_own_message(self, tmp_path): + def test_failed_init_surfaces_inits_own_message(self, tmp_path, init_empty_catalog): """A failing init step must report init's diagnostics, not 'SystemExit: 1'. `typer.Exit(n)` — how `specify init` reports every ordinary failure — @@ -3743,42 +4096,29 @@ def test_failed_init_surfaces_inits_own_message(self, tmp_path): assert "no-such-agent" in stderr or "Unknown" in stderr, stderr def test_unexpected_crash_still_reports_its_exception(self, monkeypatch, tmp_path): - """The branch's original purpose is preserved for a genuine crash. - - Only `SystemExit` is now excluded; any other exception escaping the - runner must still be surfaced, since nothing else would describe it. - """ + """Preserve exception details emitted by an unexpectedly failing child.""" + import subprocess from specify_cli.workflows.step.init import InitStep - import typer.testing - - class _Result: - exit_code = 1 - output = "" - stderr = "" - exception = RuntimeError("boom inside init") + original = subprocess.run - class _Runner: - def __init__(self, *a, **k): - pass - - def invoke(self, *a, **k): - return _Result() - - monkeypatch.setattr(typer.testing, "CliRunner", _Runner) - - from specify_cli.workflows.base import StepContext + def crash(args, **kwargs): + return original( + [sys.executable, "-P", "-c", "raise RuntimeError('boom inside init')"], **kwargs + ) + monkeypatch.setattr(subprocess, "run", crash) _code, _stdout, stderr = InitStep()._run_init( - ["init", "demo"], StepContext(project_root=str(tmp_path)) + ["init", "demo"], str(tmp_path) ) assert "RuntimeError: boom inside init" in stderr - def test_non_empty_current_dir_without_force_fails_fast(self, tmp_path): + def test_non_empty_current_dir_without_force_fails_fast(self, tmp_path, monkeypatch): from specify_cli.workflows.step.init import InitStep from specify_cli.workflows.base import StepContext, StepStatus (tmp_path / "existing.txt").write_text("data") + _gate_init_launch(monkeypatch, lambda argv: pytest.fail("Non-empty preflight must not launch init")) step = InitStep() ctx = StepContext( diff --git a/tests/workflows/test_composition_execution.py b/tests/workflows/test_composition_execution.py index 33c746c498..cbe5d0a9ba 100644 --- a/tests/workflows/test_composition_execution.py +++ b/tests/workflows/test_composition_execution.py @@ -24,6 +24,178 @@ def definition(name, steps, **fields): ) +@pytest.fixture +def init_home(monkeypatch, tmp_path_factory): + from tests.test_workflows import _isolate_init_home + + _isolate_init_home(monkeypatch, tmp_path_factory) + + +def test_init_step_fan_out_keeps_targets_and_streams_isolated(tmp_path, monkeypatch, init_home): + import os + import sys + from tests.test_workflows import _assert_init_scaffold, _gate_init_launch + + root, launch = tmp_path / "root", tmp_path / "launch" + root.mkdir() + launch.mkdir() + monkeypatch.chdir(launch) + streams = (sys.stdin, sys.stdout, sys.stderr) + barrier = threading.Barrier(2, timeout=30) + _gate_init_launch(monkeypatch, lambda argv: barrier.wait()) + workflow = definition("init-fan", [{ + "id": "spread", "type": "fan-out", "items": ["alpha", "beta"], "max_concurrency": 2, + "step": {"id": "bootstrap", "type": "init", "project": "{{ item }}", "script": "sh"}, + }]) + engine = WorkflowEngine(root) + assert engine.validate(workflow) == [] + try: + state = engine.execute(workflow) + final_cwd = os.getcwd() + finally: + barrier.abort() + os.chdir(launch) # execute joins its fan-out workers before returning. + assert state.status == RunStatus.COMPLETED, state.error + saved = RunState.load(state.run_id, root) + assert saved.step_results == state.step_results + results = saved.step_results["spread"]["output"]["results"] + assert [result["project"] for result in results] == ["alpha", "beta"] + for result in results: + _assert_init_scaffold(root / result["project"]) + assert result["exit_code"] == 0 + assert result["stderr"] == "" + assert not (launch / result["project"]).exists() + assert final_cwd == str(launch) + assert (sys.stdin, sys.stdout, sys.stderr) == streams + + +def test_init_step_does_not_capture_concurrent_observer(tmp_path, monkeypatch, init_home, capsys): + import contextlib + import os + import subprocess + import sys + from tests.test_workflows import _assert_init_scaffold + from specify_cli.workflows.step import init + + root, launch = tmp_path / "root", tmp_path / "launch" + root.mkdir() + launch.mkdir() + (launch / "sentinel").write_text("ambient", encoding="utf-8") + (root / "sentinel").write_text("wrong root", encoding="utf-8") + monkeypatch.chdir(launch) + streams = (sys.stdin, sys.stdout, sys.stderr) + active, observed = threading.Event(), threading.Event() + readings = [] + + def wait_for_observer(): + active.set() + assert observed.wait(30), "Observer did not finish" + + if hasattr(init, "subprocess"): + communicate = subprocess.Popen.communicate + + def wait(self, *args, **kwargs): + if "init" in self.args: + wait_for_observer() + return communicate(self, *args, **kwargs) + + monkeypatch.setattr(subprocess.Popen, "communicate", wait) + else: + from typer.testing import CliRunner + + isolation = CliRunner.isolation + + @contextlib.contextmanager + def isolated(self, *args, **kwargs): + with isolation(self, *args, **kwargs) as value: + wait_for_observer() # Legacy streams have already been replaced. + yield value + + monkeypatch.setattr(CliRunner, "isolation", isolated) + + class Observer(StepBase): + type_key = "init-observer" + + def execute(self, config, context): + try: + assert active.wait(30), "Init did not reach its real boundary" + from pathlib import Path + + readings.append(Path("sentinel").read_text(encoding="utf-8")) + readings.append((sys.stdin, sys.stdout, sys.stderr)) + print("observer-out-token") + print("observer-err-token", file=sys.stderr) + return StepResult(StepStatus.COMPLETED) + finally: + observed.set() + + monkeypatch.setitem(STEP_REGISTRY, "init-observer", Observer()) + workflow = definition("observe-init", [{ + "id": "spread", "type": "fan-out", "items": [True, False], "max_concurrency": 2, + "step": {"id": "route", "type": "if", "condition": "{{ item }}", "then": [ + {"id": "bootstrap", "type": "init", "project": "demo", "script": "sh"}, + ], "else": [{"id": "observe", "type": "init-observer"}]}, + }]) + engine = WorkflowEngine(root) + assert engine.validate(workflow) == [] + try: + state = engine.execute(workflow) + final_cwd = os.getcwd() + finally: + observed.set() + os.chdir(launch) + captured = capsys.readouterr() + assert state.status == RunStatus.COMPLETED, state.error + assert readings == ["ambient", streams] + assert "observer-out-token" in captured.out + assert "observer-err-token" in captured.err + persisted = RunState.load(state.run_id, root) + assert "observer-out-token" not in json.dumps(persisted.execution) + assert "observer-err-token" not in json.dumps(persisted.execution) + _assert_init_scaffold(root / "demo") + assert final_cwd == str(launch) + assert (sys.stdin, sys.stdout, sys.stderr) == streams + + +def test_init_step_resume_preserves_target_and_completed_replay(tmp_path, monkeypatch, init_home, probe): + from tests.test_workflows import _assert_init_scaffold + from specify_cli.workflows.step.init import InitStep + + root, launch = tmp_path / "root", tmp_path / "launch" + root.mkdir() + launch.mkdir() + (root / "keep").write_bytes(b"keep") + monkeypatch.chdir(launch) + workflow = definition("resume-init", [ + {"id": "bootstrap", "type": "init", "here": True, "script": "sh", "force": "{{ inputs.force }}"}, + {"id": "wait", "type": "probe", "await": True}, + ], inputs={"force": {"type": "boolean", "default": False}, + "approve": {"type": "boolean", "default": False}}) + engine = WorkflowEngine(root) + assert engine.validate(workflow) == [] + failed = engine.execute(workflow) + assert failed.status == RunStatus.FAILED + assert "force: true" in failed.error + saved = RunState.load(failed.run_id, root) + assert saved.step_results["bootstrap"]["status"] == "failed" + paused = WorkflowEngine(root).resume(saved.run_id, {"force": True}) + assert paused.status == RunStatus.PAUSED, paused.error + _assert_init_scaffold(root) + assert (root / "keep").read_bytes() == b"keep" + files = [root / ".specify/init-options.json", root / ".github/skills/speckit-specify/SKILL.md"] + before = [(path.read_bytes(), path.stat().st_mtime_ns) for path in files] + + def unexpected(*args, **kwargs): + pytest.fail("Completed init must be replayed without executing") + + monkeypatch.setattr(InitStep, "execute", unexpected) + complete = WorkflowEngine(root).resume(paused.run_id, {"approve": True}) + assert complete.status == RunStatus.COMPLETED, complete.error + assert [(path.read_bytes(), path.stat().st_mtime_ns) for path in files] == before + assert RunState.load(complete.run_id, root).step_results["bootstrap"] == paused.step_results["bootstrap"] + assert not (launch / ".specify").exists() + + def install(root, child, enabled=True): from specify_cli.workflows.catalog import WorkflowRegistry