Python
Managed tools land on the persistent shell's PATH
Rohit C Prasadb86615777859
diff --git a/coworker/toolchain.py b/coworker/toolchain.pyindex 93640ea0..0a10866b 100644--- a/coworker/toolchain.py+++ b/coworker/toolchain.py@@ -33,6 +33,7 @@ import urllib.requestfrom dataclasses import dataclassfrom pathlib import Pathfrom typing import Iterable, Optional+from urllib.parse import urlparsefrom .secrets import state_dir@@ -55,6 +56,13 @@ def managed_dir() -> Path:return state_dir() / "tools"+def bin_dir() -> Path:+ """One stable dir of links to the current pinned binaries. Binaries themselves live+ in versioned dirs; this is what goes on a shell's PATH, so a tool installed mid-+ session is picked up by the already-running shell without a respawn."""+ return managed_dir() / "bin"++def _platform_key() -> str:"""`<os>_<arch>` using the naming the upstream release assets use."""system = {"darwin": "darwin", "linux": "linux", "win32": "windows"}.get(@@ -203,12 +211,16 @@ def describe(name: str) -> Optional[dict[str, str]]:dl = tool.downloads.get(_platform_key())if not dl:return None+ parsed = urlparse(dl.url)+ path_parts = [p for p in parsed.path.split("/") if p]return {"name": tool.name,"version": tool.version,"summary": tool.summary,"url": dl.url,"sha256": dl.sha256,+ # Publisher, human-readable ("github.com/aquasecurity") — for the consent card.+ "source": parsed.netloc + (f"/{path_parts[0]}" if path_parts else ""),}@@ -261,4 +273,18 @@ def install(name: str, *, timeout: int = 120) -> str:staged.chmod(staged.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)shutil.move(str(staged), str(target))+ _link_into_bin(tool, target)return str(target)+++def _link_into_bin(tool: ManagedTool, target: Path) -> None:+ """Expose the versioned binary under the stable bin dir (PATH-friendly name)."""+ link = bin_dir() / target.name+ link.parent.mkdir(parents=True, exist_ok=True)+ try:+ if link.is_symlink() or link.exists():+ link.unlink()+ link.symlink_to(target)+ except OSError:+ # Filesystems without symlinks (some Windows setups): a copy serves the same role.+ shutil.copy2(target, link)diff --git a/coworker/tools/shell.py b/coworker/tools/shell.pyindex b30ea78f..0404511d 100644--- a/coworker/tools/shell.py+++ b/coworker/tools/shell.py@@ -162,6 +162,16 @@ class LocalExecutor(Executor):shell_path = "powershell.exe" if self._is_windows else "/bin/bash"self._shell_path = shell_pathself._env = {**os.environ, **_NONINTERACTIVE_ENV, **(env or {})}+ # Managed pinned tools (toolchain.install) land under one stable bin dir; putting+ # it on PATH up front — even before anything is installed there — means a tool the+ # user approves mid-session works in THIS shell immediately, by name, no respawn.+ # Appended last: the user's own copies always win.+ from .. import toolchain++ path = self._env.get("PATH", "")+ managed_bin = str(toolchain.bin_dir())+ if managed_bin not in path.split(os.pathsep):+ self._env["PATH"] = f"{path}{os.pathsep}{managed_bin}" if path else managed_binself._spawn()def _spawn(self) -> None:diff --git a/tests/test_shell.py b/tests/test_shell.pyindex 40513647..fc7291cc 100644--- a/tests/test_shell.py+++ b/tests/test_shell.py@@ -53,6 +53,24 @@ def test_env_persists_across_calls(executor):assert "hello_world" in result["output"]+def test_managed_bin_dir_is_on_path_from_spawn(tmp_path, monkeypatch):+ """A tool the user approves mid-session must work by name in the ALREADY-running+ shell (owner-hit 2026-08-14: freshly installed trivy needed a full-path retry). The+ stable bin dir goes on PATH at spawn, before anything exists in it."""+ import os as _os++ from coworker import toolchain++ monkeypatch.setenv("COWORKER_STATE_DIR", str(tmp_path / "state"))+ ex = LocalExecutor(cwd=tmp_path, default_timeout=10)+ try:+ assert str(toolchain.bin_dir()) in ex._env["PATH"].split(_os.pathsep)+ # User's own PATH entries stay ahead — their copies always win.+ assert not ex._env["PATH"].startswith(str(toolchain.bin_dir()))+ finally:+ ex.close()++def test_exit_code_captured(executor):assert executor.run(EXIT_OK)["exit_code"] == 0assert executor.run(EXIT_FAIL)["exit_code"] == 1