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
42 changes: 33 additions & 9 deletions src/kiro_crew/doctor_checks/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<tool>.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.

Expand All @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
40 changes: 28 additions & 12 deletions src/kiro_crew/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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``.

Expand All @@ -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]:
Expand Down
126 changes: 126 additions & 0 deletions test/test_cli_doctor_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ["<sandbox>", *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.

Expand Down
Loading