From ea2d8b19609a5fcd38f52e409f91751f044657bc Mon Sep 17 00:00:00 2001 From: Aditya Chandrajit Deore <170283215+AdityaxDeore@users.noreply.github.com> Date: Sat, 5 Dec 2026 06:41:28 +0000 Subject: [PATCH 1/3] feat(events): warn at install when the hook interpreter is missing Generated event hooks start the dispatcher with a fixed interpreter and nothing checks it up front, so a missing interpreter fails silently inside the agent's hook runner. Add a shared availability check that resolves the hook interpreter the same way the hook command does (PATH lookup for bare names, project-relative resolution for venv paths, existence for absolute paths) and warn at install time, naming the interpreter and the remedy. Install still succeeds: the hooks may run on another machine. The Windows py -3 fallback and the workflow pre-flight from the issue are deliberately left out: generated PowerShell changes need Windows PowerShell 5.1 evidence unavailable here, and the workflow engine treats requires as advisory rather than a gate. Assisted-by: Muse Spark (model: Muse Spark 1.3, autonomous) --- src/specify_cli/events/__init__.py | 67 ++++++++++++++++++ tests/specify_cli/events/test_events.py | 93 +++++++++++++++++++++++++ 2 files changed, 160 insertions(+) diff --git a/src/specify_cli/events/__init__.py b/src/specify_cli/events/__init__.py index 66bef8154c..2c86febb01 100644 --- a/src/specify_cli/events/__init__.py +++ b/src/specify_cli/events/__init__.py @@ -4,6 +4,7 @@ - ``resolve_events`` — layered event resolution (CLI flag → YAML override → extension-declared → built-in). - ``collect_extension_events`` — scan installed extension.yml files for ``events:``. - ``install_integration_events`` / ``remove_integration_events`` — entry points called from ``IntegrationBase.setup()`` / ``teardown()``. +- ``hook_interpreter_available`` — whether the interpreter a generated hook command would use resolves on this machine (#5619). The singular CLI namespace intentionally maps to the plural Python package. ``specify_cli.events`` is an established domain API and monkeypatch boundary, @@ -1202,6 +1203,67 @@ def _resolve_interpreter_for_target(target_os: str) -> str: return "python3" +def hook_interpreter_available(interpreter: str, project_root: Path) -> bool: + """Return True when *interpreter* resolves to a runnable Python (#5619). + + *interpreter* is the string a generated hook command would use, so it + is resolved the same way the hook command does: a bare name is looked + up on ``PATH`` (on Windows, a found name must also pass the Store + stub probe, matching ``resolve_python_interpreter``); a + project-relative path is resolved against *project_root*; an absolute + path must exist. An empty string is unavailable. + """ + if not interpreter: + return False + candidate = Path(interpreter) + if candidate.is_absolute(): + return candidate.exists() + if "/" in interpreter or "\\" in interpreter: + return (project_root / interpreter).exists() + found = shutil.which(interpreter) + if found is None: + return False + if sys.platform == "win32": + from ..integrations.base import IntegrationBase + + return IntegrationBase._interpreter_runs(found) + return True + + +def _warn_if_hook_interpreter_missing( + integration: IntegrationBase, project_root: Path +) -> None: + """Warn when the generated hook commands have no interpreter to run. + + The warning is advisory: install still succeeds, because the hooks + may run on another machine (e.g. a config generated here and used on + Windows). It names the interpreter and the remedy so the failure + does not stay silent until the agent's hook runner drops it. + """ + interpreter = _resolve_interpreter(project_root) + if hook_interpreter_available(interpreter, project_root): + # ``resolve_python_interpreter`` falls back to the running + # interpreter when no venv and no bare name is on PATH; the + # generated command then embeds an absolute path that may belong + # to an ephemeral environment (a one-time ``uvx`` run), so warn + # unless a portable resolution exists. + if not ( + Path(interpreter).is_absolute() + and shutil.which("python3") is None + and shutil.which("python") is None + and not (project_root / ".venv").exists() + ): + return + print( + f"⚠️ Hook interpreter {interpreter!r} could not be resolved on " + f"this machine, so the event hooks installed for " + f"{integration.key} may fail silently. Put a Python interpreter on " + f"PATH, or create a project .venv and rerun " + f"`specify integration upgrade {integration.key}`.", + file=sys.stderr, + ) + + def _native_timeout(integration: IntegrationBase, timeout_seconds: Any) -> int: """Return the timeout in the unit the integration's native config expects. @@ -1403,6 +1465,11 @@ def install_integration_events( created: list[Path] = [] + # #5619: warn when the hook commands written below would have no + # interpreter to run on this machine. Advisory only — install + # continues, since the hooks may run elsewhere. + _warn_if_hook_interpreter_missing(integration, project_root) + # 1. Generate events.py dispatcher script (#12: validate destination first) dispatcher_dir = project_root / EVENTS_DISPATCHER_DIR dispatcher_path = dispatcher_dir / EVENTS_DISPATCHER_FILENAME diff --git a/tests/specify_cli/events/test_events.py b/tests/specify_cli/events/test_events.py index f5f7183702..88da4db5ee 100644 --- a/tests/specify_cli/events/test_events.py +++ b/tests/specify_cli/events/test_events.py @@ -3749,3 +3749,96 @@ def test_refresh_failure_preserves_existing_config(self, tmp_path): # The pre-existing config was NOT destroyed before the failure # (install handles cleanup atomically; refresh no longer pre-strips). assert config_path.read_text() == original + +# -- Hook interpreter check (#5619) ------------------------------------------ + + +class TestHookInterpreterAvailable: + """Resolution tests for the hook interpreter check: PATH command, + project-relative venv path, and absolute path (issue acceptance + criteria).""" + + def test_bare_name_on_path_is_available(self, tmp_path, monkeypatch): + from specify_cli.events import hook_interpreter_available + import shutil as _shutil + + monkeypatch.setattr(_shutil, "which", lambda name: "/usr/bin/python3") + assert hook_interpreter_available("python3", tmp_path) is True + + def test_bare_name_missing_from_path_is_unavailable(self, tmp_path, monkeypatch): + from specify_cli.events import hook_interpreter_available + import shutil as _shutil + + monkeypatch.setattr(_shutil, "which", lambda name: None) + assert hook_interpreter_available("python3", tmp_path) is False + + def test_project_venv_path_resolves_against_project_root(self, tmp_path): + from specify_cli.events import hook_interpreter_available + + venv_python = tmp_path / ".venv" / "bin" / "python" + venv_python.parent.mkdir(parents=True) + venv_python.write_text("#!/bin/sh\n", encoding="utf-8") + assert hook_interpreter_available(".venv/bin/python", tmp_path) is True + assert hook_interpreter_available(".venv/bin/python", tmp_path.parent) is False + + def test_absolute_path_must_exist(self, tmp_path): + from specify_cli.events import hook_interpreter_available + + existing = tmp_path / "python" + existing.write_text("#!/bin/sh\n", encoding="utf-8") + assert hook_interpreter_available(str(existing), tmp_path) is True + assert ( + hook_interpreter_available(str(tmp_path / "no-such-python"), tmp_path) + is False + ) + + def test_empty_interpreter_is_unavailable(self, tmp_path): + from specify_cli.events import hook_interpreter_available + + assert hook_interpreter_available("", tmp_path) is False + + +class TestInstallWarnsOnMissingHookInterpreter: + """Install warns (without failing) when the hook interpreter the + generated commands would use cannot be resolved on this machine.""" + + def _install(self, tmp_path): + integration = ClaudeIntegration() + manifest = MagicMock(spec=IntegrationManifest) + manifest.files = {} + manifest.record_file = MagicMock() + manifest.record_existing = MagicMock() + install_integration_events( + integration, + tmp_path, + manifest, + {"session_start": [{"command": "speckit.x.y"}]}, + ) + return tmp_path / ".claude/settings.json" + + def test_warns_when_no_interpreter_on_path(self, tmp_path, monkeypatch, capsys): + import shutil as _shutil + + monkeypatch.setattr(_shutil, "which", lambda name: None) + config_path = self._install(tmp_path) + # The install itself still succeeds; the warning is advisory. + assert config_path.is_file() + err = capsys.readouterr().err + assert "python" in err + assert "PATH" in err or ".venv" in err + + def test_no_warning_when_interpreter_resolves(self, tmp_path, capsys): + self._install(tmp_path) + err = capsys.readouterr().err + assert "interpreter" not in err.lower() + + def test_no_warning_when_project_venv_exists(self, tmp_path, monkeypatch, capsys): + import shutil as _shutil + + venv_python = tmp_path / ".venv" / "bin" / "python" + venv_python.parent.mkdir(parents=True) + venv_python.write_text("#!/bin/sh\n", encoding="utf-8") + monkeypatch.setattr(_shutil, "which", lambda name: None) + self._install(tmp_path) + err = capsys.readouterr().err + assert "interpreter" not in err.lower() From d29145a3d1d935612d2089c93f992377367432cf Mon Sep 17 00:00:00 2001 From: Aditya Chandrajit Deore <170283215+AdityaxDeore@users.noreply.github.com> Date: Sat, 5 Dec 2026 08:46:24 +0000 Subject: [PATCH 2/3] fix(events): warn despite an empty .venv directory An empty .venv directory suppressed the hook-interpreter fallback warning even though resolve_python_interpreter ignores the directory and still falls back to the running interpreter, so the warning depended on a directory that changed nothing about the selected interpreter. Drop the directory-existence condition. Also make two install-warning tests deterministic: pin the PATH-only resolution test to a POSIX platform (the Windows stub probe rejects the fake PATH entry) and mock a successful PATH lookup in the no-warning test (a uvx-only install otherwise warns). The per-format interpreter-selection finding is left out as a scope call; see the review-round summary on the PR. Tests: new empty-.venv test failed before (no warning) and passes after; tests/specify_cli/events/test_events.py 218 passed; events + integrations base 430 passed; ruff check clean on the changed files (pre-existing format drift left untouched). Assisted-by: Muse Spark (model: Muse Spark 1.3, autonomous) --- src/specify_cli/events/__init__.py | 10 +++++----- tests/specify_cli/events/test_events.py | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/specify_cli/events/__init__.py b/src/specify_cli/events/__init__.py index 2c86febb01..dfada340ef 100644 --- a/src/specify_cli/events/__init__.py +++ b/src/specify_cli/events/__init__.py @@ -1243,15 +1243,15 @@ def _warn_if_hook_interpreter_missing( interpreter = _resolve_interpreter(project_root) if hook_interpreter_available(interpreter, project_root): # ``resolve_python_interpreter`` falls back to the running - # interpreter when no venv and no bare name is on PATH; the - # generated command then embeds an absolute path that may belong - # to an ephemeral environment (a one-time ``uvx`` run), so warn - # unless a portable resolution exists. + # interpreter when no venv interpreter and no bare name is on + # PATH; the generated command then embeds an absolute path that + # may belong to an ephemeral environment (a one-time ``uvx`` + # run), so warn. An empty ``.venv`` directory does not change + # the selected interpreter, so it does not suppress the warning. if not ( Path(interpreter).is_absolute() and shutil.which("python3") is None and shutil.which("python") is None - and not (project_root / ".venv").exists() ): return print( diff --git a/tests/specify_cli/events/test_events.py b/tests/specify_cli/events/test_events.py index 88da4db5ee..6879d55387 100644 --- a/tests/specify_cli/events/test_events.py +++ b/tests/specify_cli/events/test_events.py @@ -3762,6 +3762,9 @@ def test_bare_name_on_path_is_available(self, tmp_path, monkeypatch): from specify_cli.events import hook_interpreter_available import shutil as _shutil + # Pin a POSIX platform so the Windows stub probe does not reject + # the fake PATH entry on Windows CI (see TestResolvePythonInterpreter). + monkeypatch.setattr("specify_cli.events.sys.platform", "linux") monkeypatch.setattr(_shutil, "which", lambda name: "/usr/bin/python3") assert hook_interpreter_available("python3", tmp_path) is True @@ -3827,11 +3830,27 @@ def test_warns_when_no_interpreter_on_path(self, tmp_path, monkeypatch, capsys): assert "python" in err assert "PATH" in err or ".venv" in err - def test_no_warning_when_interpreter_resolves(self, tmp_path, capsys): + def test_no_warning_when_interpreter_resolves(self, tmp_path, monkeypatch, capsys): + import shutil as _shutil + + # Mock a successful PATH lookup so the test does not depend on the + # runner having a system Python (e.g. a uvx-only install warns). + monkeypatch.setattr("specify_cli.events.sys.platform", "linux") + monkeypatch.setattr(_shutil, "which", lambda name: "/usr/bin/python3") self._install(tmp_path) err = capsys.readouterr().err assert "interpreter" not in err.lower() + def test_warns_when_venv_directory_is_empty(self, tmp_path, monkeypatch, capsys): + import shutil as _shutil + + (tmp_path / ".venv").mkdir() + monkeypatch.setattr(_shutil, "which", lambda name: None) + config_path = self._install(tmp_path) + assert config_path.is_file() + err = capsys.readouterr().err + assert "Hook interpreter" in err + def test_no_warning_when_project_venv_exists(self, tmp_path, monkeypatch, capsys): import shutil as _shutil From dcb65a87f1e3a09cb11de6d889929261ad315024 Mon Sep 17 00:00:00 2001 From: Aditya Chandrajit Deore <170283215+AdityaxDeore@users.noreply.github.com> Date: Sat, 5 Dec 2026 12:27:06 +0000 Subject: [PATCH 3/3] fix(events): warn when PATH holds only Windows Store stubs shutil.which finds a Store alias stub, so the fallback warning stayed silent even though neither PATH interpreter runs and the resolver had already fallen back to an absolute interpreter. Check the bare names with hook_interpreter_available, which runs the stub probe on Windows. Tests: new stub-only-PATH test failed before (no warning) and passes after; tests/specify_cli/events/ + integrations/test_base.py 431 passed; ruff check clean on the changed files (pre-existing format drift left untouched). Assisted-by: Muse Spark (model: Muse Spark 1.3, autonomous) --- src/specify_cli/events/__init__.py | 7 +++++-- tests/specify_cli/events/test_events.py | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/specify_cli/events/__init__.py b/src/specify_cli/events/__init__.py index dfada340ef..026ce4683a 100644 --- a/src/specify_cli/events/__init__.py +++ b/src/specify_cli/events/__init__.py @@ -1248,10 +1248,13 @@ def _warn_if_hook_interpreter_missing( # may belong to an ephemeral environment (a one-time ``uvx`` # run), so warn. An empty ``.venv`` directory does not change # the selected interpreter, so it does not suppress the warning. + # Check the bare names with the helper, not ``shutil.which``: + # a Windows Store stub is found by ``which`` but fails the + # helper's run probe, and must not suppress the warning. if not ( Path(interpreter).is_absolute() - and shutil.which("python3") is None - and shutil.which("python") is None + and not hook_interpreter_available("python3", project_root) + and not hook_interpreter_available("python", project_root) ): return print( diff --git a/tests/specify_cli/events/test_events.py b/tests/specify_cli/events/test_events.py index 6879d55387..de70791e66 100644 --- a/tests/specify_cli/events/test_events.py +++ b/tests/specify_cli/events/test_events.py @@ -3841,6 +3841,29 @@ def test_no_warning_when_interpreter_resolves(self, tmp_path, monkeypatch, capsy err = capsys.readouterr().err assert "interpreter" not in err.lower() + def test_warns_when_path_has_only_windows_store_stubs( + self, tmp_path, monkeypatch, capsys + ): + import shutil as _shutil + + from specify_cli.integrations.base import IntegrationBase + + monkeypatch.setattr("specify_cli.events.sys.platform", "win32") + monkeypatch.setattr( + _shutil, + "which", + lambda name: f"C:\\WindowsApps\\{name}.exe" + if name in ("python3", "python") + else None, + ) + monkeypatch.setattr( + IntegrationBase, "_interpreter_runs", staticmethod(lambda path: False) + ) + config_path = self._install(tmp_path) + assert config_path.is_file() + err = capsys.readouterr().err + assert "Hook interpreter" in err + def test_warns_when_venv_directory_is_empty(self, tmp_path, monkeypatch, capsys): import shutil as _shutil