From fb3a15d4fea1d1a853b39d63515589c67286cf0c Mon Sep 17 00:00:00 2001 From: Gary Jennings Date: Wed, 11 Nov 2026 19:24:24 +0000 Subject: [PATCH] fix: recognise the windows toolbox launcher in the doctor cli row On a Windows install managed by Toolbox, `kirocrew doctor` always said the `kirocrew` on PATH belonged to a different install. The Toolbox check only knew the POSIX layout: the launcher had to resolve to the shared `toolbox-exec` dispatcher, and the install had to run from under `~/.toolbox`. On Windows the launcher is `%LOCALAPPDATA%\Toolbox\bin\kirocrew.exe` and the install lives under `%LOCALAPPDATA%\Toolbox`, so neither gate passed. The row now also accepts a launcher whose directory is the Windows Toolbox bin directory, and `is_toolbox_install` also accepts an interpreter under the Windows Toolbox root. Both paths come from one new `env._windows_toolbox_root`, which `_windows_toolbox_bin_dir` now builds on. The version probe after the gates is unchanged: sandboxed, and clean only on an exact version-line match. Fixes #32803 --- src/kiro_crew/doctor_checks/install.py | 42 +++++++-- src/kiro_crew/env.py | 40 +++++--- test/test_cli_doctor_install.py | 126 +++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 21 deletions(-) diff --git a/src/kiro_crew/doctor_checks/install.py b/src/kiro_crew/doctor_checks/install.py index d654c785157b..859482a0fae6 100644 --- a/src/kiro_crew/doctor_checks/install.py +++ b/src/kiro_crew/doctor_checks/install.py @@ -263,14 +263,38 @@ def _doctor_skill_currency(issues: list[str]) -> None: _TOOLBOX_DISPATCHER = "toolbox-exec" +def _is_toolbox_launcher(on_path: str) -> bool: + """Whether *on_path* is a Toolbox launcher rather than an install's own binary. + + On POSIX every Toolbox tool name links to the shared ``toolbox-exec`` + dispatcher. On Windows Toolbox puts a ``.exe`` launcher per tool in its + bin directory (``env._windows_toolbox_bin_dir``), so there it is the directory + the PATH entry sits in that identifies it. + """ + from kiro_crew import env, platform_compat + + if os.path.basename(os.path.realpath(on_path)) == _TOOLBOX_DISPATCHER: + return True + if not platform_compat.IS_WINDOWS: + return False + bin_dir = env._windows_toolbox_bin_dir(os.path.expanduser("~"), None) + if bin_dir is None: + return False + + def canonical(path: str) -> str: + return os.path.normcase(os.path.realpath(path)) + + return canonical(os.path.dirname(os.path.abspath(on_path))) == canonical(bin_dir) + + def _toolbox_launcher_runs_this_version(on_path: str) -> bool: - """Whether *on_path* is the Toolbox dispatcher and runs this install's version. + """Whether *on_path* is a Toolbox launcher and runs this install's version. - A path comparison cannot answer this for Toolbox: ``on_path`` resolves to the - one dispatcher every tool shares, never to this install's own binary. So when - THIS install is Toolbox-managed and ``on_path`` resolves to that dispatcher, - ask it: ``--version`` run through the PATH spelling (the dispatcher reads its - argv[0]) must print exactly this install's version line. Any other answer -- + A path comparison cannot answer this for Toolbox: ``on_path`` resolves to a + launcher Toolbox owns (:func:`_is_toolbox_launcher`), never to this install's + own binary. So when THIS install is Toolbox-managed and ``on_path`` is such a + launcher, ask it: ``--version`` run through the PATH spelling (the POSIX + dispatcher reads its argv[0]) must print exactly this install's version line. Any other answer -- another version, a failed or timed-out run, unreadable output, a host that cannot build the sandbox -- is False, so the caller keeps its warning. @@ -284,7 +308,7 @@ def _toolbox_launcher_runs_this_version(on_path: str) -> bool: from kiro_crew import env, version_line - if os.path.basename(os.path.realpath(on_path)) != _TOOLBOX_DISPATCHER: + if not _is_toolbox_launcher(on_path): return False if not env.is_toolbox_install(): return False @@ -337,8 +361,8 @@ def _doctor_path_launcher() -> None: print(f" kirocrew CLI: ✅ {os.path.realpath(on_path)}") return if _toolbox_launcher_runs_this_version(on_path): - # The realpath is Toolbox's shared dispatcher, so the PATH spelling is the - # one that names the tool. + # The realpath can be Toolbox's shared dispatcher, so the PATH spelling is + # the one that names the tool. print(f" kirocrew CLI: ✅ {on_path} (Toolbox launcher, same version)") return print(" ⚠ kirocrew CLI on PATH belongs to a different install than this one.") diff --git a/src/kiro_crew/env.py b/src/kiro_crew/env.py index 0cac16e2acdb..1f0717b81249 100644 --- a/src/kiro_crew/env.py +++ b/src/kiro_crew/env.py @@ -723,14 +723,18 @@ def ensure_node(timeout: float = 180.0) -> str | None: @functools.lru_cache(maxsize=1) def is_toolbox_install() -> bool: - """Return True if the running kirocrew binary was installed via Toolbox.""" + """Return True if the running kirocrew binary was installed via Toolbox. + + Toolbox installs every tool under its root: ``~/.toolbox`` on POSIX, and on + Windows ``%LOCALAPPDATA%\\Toolbox`` (:func:`_windows_toolbox_root`). + """ exe = Path(sys.executable).resolve() - toolbox_dir = (Path.home() / ".toolbox").resolve() - try: - exe.relative_to(toolbox_dir) - return True - except ValueError: - return False + roots = [Path.home() / ".toolbox"] + if platform_compat.IS_WINDOWS and ( + windows_root := _windows_toolbox_root(os.path.expanduser("~"), None) + ): + roots.append(Path(windows_root)) + return any(exe.is_relative_to(root.resolve()) for root in roots) @functools.lru_cache(maxsize=1) @@ -778,6 +782,20 @@ def _own_kirocrew_dirs() -> list[str]: return env_own_kirocrew.own_kirocrew_dirs(_validated_bin_dir) +def _windows_toolbox_root(resolved_home: str, pinned_home: str | None) -> str | None: + """Builder Toolbox's Windows root, ``%LOCALAPPDATA%\\Toolbox``. + + The Windows counterpart of ``~/.toolbox``: its ``bin`` directory + (:func:`_windows_toolbox_bin_dir`) and the tools Toolbox installs live under it. + + ``LOCALAPPDATA`` names the account running this process, so a caller that + pinned another account's *pinned_home* gets that home's ``AppData\\Local``. + """ + local = os.environ.get("LOCALAPPDATA", "") if pinned_home is None else "" + base = local or os.path.join(resolved_home, "AppData", "Local") + return _validated_bin_dir(os.path.join(base, "Toolbox")) + + def _windows_toolbox_bin_dir(resolved_home: str, pinned_home: str | None) -> str | None: """Builder Toolbox's Windows bin dir, ``%LOCALAPPDATA%\\Toolbox\\bin``. @@ -787,12 +805,10 @@ def _windows_toolbox_bin_dir(resolved_home: str, pinned_home: str | None) -> str that edit (or by a launcher that does not read it) never sees. So the Agent Harness probe reported a Toolbox-installed Claude Code as not installed. - ``LOCALAPPDATA`` names the account running this process, so a caller that - pinned another account's *pinned_home* gets that home's ``AppData\\Local``. + It is ``bin`` under :func:`_windows_toolbox_root`, which resolves the account. """ - local = os.environ.get("LOCALAPPDATA", "") if pinned_home is None else "" - base = local or os.path.join(resolved_home, "AppData", "Local") - return _validated_bin_dir(os.path.join(base, "Toolbox", "bin")) + root = _windows_toolbox_root(resolved_home, pinned_home) + return os.path.join(root, "bin") if root else None def user_tool_bin_dirs(home: str) -> list[str]: diff --git a/test/test_cli_doctor_install.py b/test/test_cli_doctor_install.py index 8fdefe51b808..7e8be44e80d4 100644 --- a/test/test_cli_doctor_install.py +++ b/test/test_cli_doctor_install.py @@ -249,6 +249,132 @@ def test_a_launcher_that_is_not_the_dispatcher_is_not_probed( assert calls == [] and self.wrapped == [] +class TestWindowsToolboxPathLauncher: + """On Windows Toolbox puts a per-tool `kirocrew.exe` in `%LOCALAPPDATA%\\Toolbox\\bin` + and installs the tool under `%LOCALAPPDATA%\\Toolbox`, so neither the POSIX + `toolbox-exec` basename nor `~/.toolbox` identifies it. Driven on any host by + pinning `IS_WINDOWS` and `LOCALAPPDATA`, with the real `is_toolbox_install`.""" + + @pytest.fixture(autouse=True) + def _fresh_toolbox_cache(self): + from kiro_crew import env + + env.is_toolbox_install.cache_clear() + yield + env.is_toolbox_install.cache_clear() + + def _layout(self, monkeypatch, tmp_path, *, probe, windows=True, running_under_toolbox=True): + from kiro_crew import env, platform_compat + + local = tmp_path / "Local" + launcher = local / "Toolbox" / "bin" / "kirocrew.exe" + launcher.parent.mkdir(parents=True) + launcher.write_text("") + install_root = local / "Toolbox" if running_under_toolbox else tmp_path / "Programs" + install = install_root / "tools" / "kirocrew" / "9.9.9" + running = install / "bin" / "kirocrew.cmd" + running.parent.mkdir(parents=True) + running.write_text("") + python = install / "python" / "python.exe" + python.parent.mkdir(parents=True) + python.write_text("") + monkeypatch.setattr(platform_compat, "IS_WINDOWS", windows) + monkeypatch.setenv("LOCALAPPDATA", str(local)) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + monkeypatch.setattr(env.sys, "executable", str(python)) + monkeypatch.setattr(cli_doctor.shutil, "which", lambda c, **kw: str(launcher)) + monkeypatch.setattr("kiro_crew.agent._resolve_kirocrew_bin", lambda: str(running)) + self.calls: list[list[str]] = [] + + def fake_wrap(argv, mode="standard", **kwargs): + self.calls.append(list(argv)) + return ["", *argv], {"SCRUBBED": "1"}, None + + def fake_run_limited(argv, **kwargs): + returncode, stdout = probe + return subprocess.CompletedProcess(argv, returncode, stdout=stdout, stderr="") + + monkeypatch.setattr(cli_doctor.sandbox, "sandboxed_spawn_argv", fake_wrap) + monkeypatch.setattr(cli_doctor.sandbox, "run_limited", fake_run_limited) + return launcher + + def test_same_version_through_the_toolbox_bin_launcher_is_clean( + self, monkeypatch, tmp_path, capsys + ) -> None: + from kiro_crew import env, version_line + + launcher = self._layout(monkeypatch, tmp_path, probe=(0, version_line() + "\n")) + + cli_doctor._doctor_path_launcher() + + out = capsys.readouterr().out + assert f"kirocrew CLI: ✅ {launcher} (Toolbox launcher, same version)" in out + assert "different install" not in out + assert self.calls == [[str(launcher), "--version"]] + assert env.is_toolbox_install() is True + + def test_another_version_through_the_toolbox_bin_launcher_warns( + self, monkeypatch, tmp_path, capsys + ) -> None: + self._layout(monkeypatch, tmp_path, probe=(0, "kirocrew 0.0.0-other\n")) + + cli_doctor._doctor_path_launcher() + + out = capsys.readouterr().out + assert "⚠ kirocrew CLI on PATH belongs to a different install" in out + assert "kirocrew CLI: ✅" not in out + + def test_a_kirocrew_exe_outside_the_toolbox_bin_is_not_probed( + self, monkeypatch, tmp_path, capsys + ) -> None: + from kiro_crew import version_line + + self._layout(monkeypatch, tmp_path, probe=(0, version_line() + "\n")) + other = tmp_path / "Programs" / "bin" / "kirocrew.exe" + other.parent.mkdir(parents=True) + other.write_text("") + monkeypatch.setattr(cli_doctor.shutil, "which", lambda c, **kw: str(other)) + + cli_doctor._doctor_path_launcher() + + out = capsys.readouterr().out + assert "⚠ kirocrew CLI on PATH belongs to a different install" in out + assert self.calls == [] + + def test_an_install_outside_the_toolbox_root_is_not_probed( + self, monkeypatch, tmp_path, capsys + ) -> None: + from kiro_crew import env, version_line + + self._layout( + monkeypatch, + tmp_path, + probe=(0, version_line() + "\n"), + running_under_toolbox=False, + ) + + cli_doctor._doctor_path_launcher() + + out = capsys.readouterr().out + assert "⚠ kirocrew CLI on PATH belongs to a different install" in out + assert self.calls == [] + assert env.is_toolbox_install() is False + + def test_the_windows_layout_is_not_read_off_windows( + self, monkeypatch, tmp_path, capsys + ) -> None: + from kiro_crew import env, version_line + + self._layout(monkeypatch, tmp_path, probe=(0, version_line() + "\n"), windows=False) + + cli_doctor._doctor_path_launcher() + + out = capsys.readouterr().out + assert "⚠ kirocrew CLI on PATH belongs to a different install" in out + assert self.calls == [] + assert env.is_toolbox_install() is False + + class TestSourceCheckout: """`kirocrew doctor` Source Checkout section — stale/off-branch source tree.