Skip to content

Experience-Driven Black-Box Cost Control for Coding Agents

Find Claude Code when `claude` is not on PATH; kill the probe's tree

mbrownnycnyc0e3c970ab426
diff --git a/tests/test_live_core.py b/tests/test_live_core.py
index e3b483d..5e50741 100644
--- a/tests/test_live_core.py
+++ b/tests/test_live_core.py
@@ -868,6 +868,21 @@ def test_claude_side_calls_stay_out_of_the_persons_history_where_the_cli_allows(
assert sidecall.claude_flags("old") == []
+def test_claude_cli_falls_back_to_the_native_install_when_not_on_path(monkeypatch, tmp_path):
+ import os
+ from tokeneconomizer.plugin import sidecall
+ monkeypatch.setattr(sidecall.shutil, "which", lambda name: None)
+ monkeypatch.setattr(sidecall.os.path, "expanduser",
+ lambda p: p.replace("~", str(tmp_path), 1))
+ assert sidecall.claude_cli() == "claude"
+ exe = tmp_path / ".local" / "bin" / ("claude.exe" if os.name == "nt" else "claude")
+ exe.parent.mkdir(parents=True)
+ exe.write_text("")
+ assert sidecall.claude_cli() == str(exe)
+ monkeypatch.setattr(sidecall.shutil, "which", lambda name: "/opt/bin/claude")
+ assert sidecall.claude_cli() == "/opt/bin/claude"
+
+
# `pi --print --mode json` on Pi 1.2.0 through OpenRouter, trimmed: the header, the system and
# user messages, the reply (a thinking block, then the text), and the run's end.
PI_JSON_RUN = [
diff --git a/tokeneconomizer/plugin/cli.py b/tokeneconomizer/plugin/cli.py
index 7f8fb75..864bc43 100644
--- a/tokeneconomizer/plugin/cli.py
+++ b/tokeneconomizer/plugin/cli.py
@@ -626,7 +626,8 @@ def cmd_doctor(args):
ok = False
import shutil as _sh
# Only the agents installed here: a Codex-only machine has no Claude Code catalog to probe.
- agents = [h for h in ("claude", "codex", "copilot", "opencode", "pi") if _sh.which(h)]
+ agents = [h for h in ("claude", "codex", "copilot", "opencode", "pi")
+ if _sh.which(sidecall.claude_cli() if h == "claude" else h)]
print(f"agents "
f"{', '.join(agents) or 'none of claude, codex, copilot, opencode, pi on PATH'}")
for h in agents:
diff --git a/tokeneconomizer/plugin/sidecall.py b/tokeneconomizer/plugin/sidecall.py
index 3594a3e..292edac 100644
--- a/tokeneconomizer/plugin/sidecall.py
+++ b/tokeneconomizer/plugin/sidecall.py
@@ -41,6 +41,7 @@ or the loop guard is (the judge and the nudge writer).
import json
import os
import re
+import shutil
import subprocess
import tempfile
@@ -107,12 +108,26 @@ def resume(host, root=None):
return False
+def claude_cli():
+ """The Claude Code executable to start: `claude` on PATH (with PATHEXT on Windows, so a
+ `claude.cmd` shim too), else the native installer's `~/.local/bin/claude` -- which is often
+ not on PATH, a shell reaching it through an alias or a profile function that a subprocess
+ does not see. Falls back to the bare name, so the error stays the CLI-not-found one."""
+ found = shutil.which("claude")
+ if found:
+ return found
+ exe = os.path.expanduser(os.path.join("~", ".local", "bin",
+ "claude.exe" if os.name == "nt" else "claude"))
+ return exe if os.path.isfile(exe) else "claude"
+
+
_FLAGS = {}
-def claude_flags(cli="claude"):
+def claude_flags(cli=None):
"""`--no-session-persistence` when this Claude Code has it: a side call or a measurement is
not a session of the person's, and should not appear in their history (`claude --resume`)."""
+ cli = cli or claude_cli()
if cli not in _FLAGS:
try:
help_text = subprocess.run([cli, "--help"], capture_output=True,
@@ -124,7 +139,8 @@ def claude_flags(cli="claude"):
return list(_FLAGS[cli])
-def claude_call(prompt, model="haiku", cli="claude", timeout=TIMEOUT_S):
+def claude_call(prompt, model="haiku", cli=None, timeout=TIMEOUT_S):
+ cli = cli or claude_cli()
cmd = [cli, "-p", "--output-format", "json", "--max-turns", "1", "--tools", "",
"--model", model or "haiku"] + claude_flags(cli)
with tempfile.TemporaryDirectory(prefix="te-side-") as cwd:
diff --git a/tokeneconomizer/plugin/toolcheck.py b/tokeneconomizer/plugin/toolcheck.py
index 9aff29e..1d0de7d 100644
--- a/tokeneconomizer/plugin/toolcheck.py
+++ b/tokeneconomizer/plugin/toolcheck.py
@@ -11,6 +11,7 @@
import json
import os
import shutil
+import signal
import subprocess
import tempfile
import threading
@@ -19,7 +20,7 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from . import adapter as live_adapter
from .denylist import CODEX_TOGGLES, write_json as _write_json
-from .sidecall import claude_flags
+from .sidecall import claude_cli, claude_flags
def verify_copilot(repo_root, servers, cli="copilot"):
@@ -54,16 +55,17 @@ def verify_codex(repo_root, capabilities, cli="codex", timeout=30):
return state or None
-def _init_tools(extra_args, cli="claude", timeout=60):
+def _init_tools(extra_args, cli=None, timeout=60):
"""Tool list from a `claude -p` session's init event; the process is killed right after it,
before the model is asked anything."""
+ cli = cli or claude_cli()
env = dict(os.environ, TE_SIDECALL="1")
cmd = [cli, "-p", "Reply with OK", "--output-format", "stream-json", "--verbose",
"--model", "haiku", "--max-turns", "1"] + claude_flags(cli) + list(extra_args)
- with tempfile.TemporaryDirectory(prefix="te-probe-") as cwd:
+ with tempfile.TemporaryDirectory(prefix="te-probe-", ignore_cleanup_errors=True) as cwd:
p = subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, encoding="utf-8", errors="replace", env=env,
- cwd=cwd)
+ cwd=cwd, start_new_session=os.name != "nt")
tools, version = None, None
deadline = time.time() + timeout
try:
@@ -78,11 +80,24 @@ def _init_tools(extra_args, cli="claude", timeout=60):
if time.time() > deadline:
break
finally:
- p.kill()
- p.wait()
+ _kill_tree(p)
return tools, version
+def _kill_tree(p):
+ """Kill the probe and what it started. The MCP servers a session launches outlive a plain
+ `kill()`, and on Windows they keep the probe's temporary directory, their cwd, in use."""
+ if os.name == "nt":
+ subprocess.run(["taskkill", "/T", "/F", "/PID", str(p.pid)], capture_output=True)
+ else:
+ try:
+ os.killpg(p.pid, signal.SIGKILL)
+ except OSError:
+ pass
+ p.kill()
+ p.wait()
+
+
class _Capture(BaseHTTPRequestHandler):
"""A chat-completions endpoint that records the request and answers "OK"."""
def log_message(self, *a):
@@ -189,7 +204,7 @@ def refresh_catalog(state_root, cli=None, host="claude"):
return path, sorted(tools)
if host != "claude":
raise RuntimeError(f"no catalog probe for {host}")
- tools, version = _init_tools([], cli=cli or "claude")
+ tools, version = _init_tools([], cli=cli)
if not tools:
raise RuntimeError("could not read the tool list from a `claude -p` init event")
path = live_adapter.catalog_path(state_root, "claude")
@@ -198,7 +213,8 @@ def refresh_catalog(state_root, cli=None, host="claude"):
return path, sorted(tools)
-def _prompt_tokens(extra_args, cli="claude", timeout=180):
+def _prompt_tokens(extra_args, cli=None, timeout=180):
+ cli = cli or claude_cli()
env = dict(os.environ, TE_SIDECALL="1")
cmd = [cli, "-p", "Reply with exactly: OK", "--output-format", "json", "--model", "haiku",
"--max-turns", "1"] + claude_flags(cli) + list(extra_args)
@@ -253,7 +269,7 @@ def measure(tools, cli=None, host="claude"):
"saved_chars_per_request": size(full) - size(lean),
"saved_tokens_per_request_est": (size(full) - size(lean)) // 4,
"probe_cost_usd": 0.0}
- cli = cli or "claude"
+ cli = cli or claude_cli()
full, c1 = _prompt_tokens([], cli=cli)
lean, c2 = _prompt_tokens(["--disallowed-tools", ",".join(tools)], cli=cli)
return {"tools": list(tools), "prompt_tokens_full": full, "prompt_tokens_lean": lean,