ensure_agent_sdk.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #!/usr/bin/env python3
  2. """SessionStart bootstrap: ensure claude_agent_sdk is importable for the
  3. agentic commit reviewer.
  4. If claude_agent_sdk already imports in the current python3, this is a no-op.
  5. Otherwise it creates a venv at ~/.claude/security/agent-sdk-venv and installs
  6. the SDK there. security_reminder_hook.py prepends that venv's site-packages to
  7. sys.path before attempting the SDK import, so the venv is used as a
  8. fallback only when the system install is missing.
  9. The venv lives under ~/.claude/security/ (same dir the plugin already uses
  10. for per-session state) so it persists across plugin updates — rebuilding
  11. on every update is 30-60s of wasted work for a package that changes far
  12. less often than the plugin does.
  13. """
  14. from __future__ import annotations
  15. import importlib.util
  16. import json
  17. import os
  18. import subprocess
  19. import sys
  20. import time
  21. from pathlib import Path
  22. # Outcome codes for the sdk_bootstrap metric. Values are stable for telemetry.
  23. NOOP_SYSTEM = 0 # claude_agent_sdk already importable in system python
  24. NOOP_VENV = 1 # venv already built and SDK imports from it
  25. BUILT = 2 # venv created + SDK pip-installed this run
  26. BUILD_FAILED = 3 # venv create or pip install raised/timed out
  27. # Outcome 4 was previously SKIP_WIN32; retired now that the consumer glob in
  28. # llm.py also matches Windows venv layout (Lib/site-packages). Don't reuse the
  29. # value — telemetry rows from older plugin builds still emit 4.
  30. SKIP_SENTINEL = 5 # another SessionStart is currently building
  31. HOOK_PY_INCOMPATIBLE = 6 # hook interpreter is <3.10 — SDK syntax can't load
  32. # here no matter how the venv was built. See #2071.
  33. def _sdk_on_syspath() -> bool:
  34. # find_spec is ~10ms; actually importing the SDK pulls in
  35. # transitive deps and costs ~800ms — too heavy for a
  36. # per-SessionStart no-op check that most sessions hit.
  37. try:
  38. return importlib.util.find_spec("claude_agent_sdk") is not None
  39. except Exception:
  40. return False
  41. def _plugin_version_int() -> int:
  42. # Same encoding as security_reminder_hook._read_plugin_version_int so
  43. # metrics rows from both hooks join on pv.
  44. try:
  45. p = Path(__file__).parent.parent / ".claude-plugin" / "plugin.json"
  46. v = json.loads(p.read_text())["version"]
  47. major, minor, patch = (int(x) for x in v.split(".")[:3])
  48. return major * 10000 + minor * 100 + patch
  49. except Exception:
  50. return 0
  51. def main() -> tuple[int, str, str]:
  52. """Run the bootstrap. Returns (outcome, err_phase, err_kind).
  53. err_phase / err_kind are non-empty only on BUILD_FAILED — they let
  54. telemetry split bootstrap failures by root cause.
  55. """
  56. # Honesty check (fixes the misleading NOOP_VENV in #2071): the SDK
  57. # requires Python >=3.10 and uses 3.10+ syntax (match statements,
  58. # PEP 604 unions). On a 3.9 hook interpreter we CANNOT import it no
  59. # matter how the venv was built — llm.py runs in this same interpreter
  60. # and the syntax-level import will SyntaxError. macOS ships 3.9.6 as
  61. # the default `python3` and `/usr/bin` precedes Homebrew in PATH, so
  62. # this case is the default state for a large share of macOS users.
  63. #
  64. # sg-python.sh now prefers python3.10+ binaries so most users won't
  65. # reach this branch; the fallback to 3.9 is preserved for the
  66. # pattern-warning hooks that don't need the SDK. Reporting
  67. # HOOK_PY_INCOMPATIBLE here:
  68. # (a) avoids 30-60s of wasted pip install,
  69. # (b) avoids the lie where the venv_py probe says NOOP_VENV but the
  70. # consumer import fails, and
  71. # (c) gives telemetry a clean bucket to size the affected fleet.
  72. if sys.version_info < (3, 10):
  73. return (
  74. HOOK_PY_INCOMPATIBLE,
  75. "hook_py",
  76. f"py_{sys.version_info[0]}.{sys.version_info[1]}",
  77. )
  78. if _sdk_on_syspath():
  79. return NOOP_SYSTEM, "", ""
  80. state_dir = Path(
  81. os.environ.get("SECURITY_WARNINGS_STATE_DIR")
  82. or os.path.expanduser("~/.claude/security")
  83. )
  84. venv = state_dir / "agent-sdk-venv"
  85. # Windows venvs put the interpreter at Scripts\python.exe; POSIX uses bin/python.
  86. if sys.platform == "win32":
  87. venv_py = venv / "Scripts" / "python.exe"
  88. else:
  89. venv_py = venv / "bin" / "python"
  90. # Another SessionStart (concurrent CC instance, same plugin) may already
  91. # be building. The sentinel lives NEXT TO the venv, not inside it —
  92. # `python -m venv --clear` wipes the target dir's contents, so an
  93. # in-venv sentinel would be deleted the instant we create the venv.
  94. # Stale sentinels (>5min) from a SIGKILL'd build are ignored.
  95. sentinel = state_dir / "agent-sdk-venv.building"
  96. if sentinel.exists():
  97. try:
  98. if time.time() - sentinel.stat().st_mtime < 300:
  99. return SKIP_SENTINEL, "", ""
  100. sentinel.unlink(missing_ok=True)
  101. except OSError:
  102. return SKIP_SENTINEL, "", ""
  103. # If a venv already exists and its python can import the SDK, done.
  104. if venv_py.exists():
  105. try:
  106. r = subprocess.run(
  107. [str(venv_py), "-c", "import claude_agent_sdk"],
  108. capture_output=True, timeout=10,
  109. )
  110. if r.returncode == 0:
  111. return NOOP_VENV, "", ""
  112. except Exception:
  113. pass # broken venv; rebuild below
  114. err_phase = ""
  115. err_kind = ""
  116. we_own_sentinel = False
  117. try:
  118. state_dir.mkdir(parents=True, exist_ok=True)
  119. # O_EXCL makes the sentinel an atomic lock — if two SessionStarts
  120. # race past the exists() check above, only one creates it.
  121. try:
  122. os.close(os.open(sentinel, os.O_CREAT | os.O_EXCL | os.O_WRONLY))
  123. except FileExistsError:
  124. return SKIP_SENTINEL, "", ""
  125. we_own_sentinel = True
  126. err_phase = "venv"
  127. subprocess.run(
  128. [sys.executable, "-m", "venv", "--clear", str(venv)],
  129. capture_output=True, timeout=60, check=True,
  130. )
  131. # Some machines route pip through a private registry; we
  132. # don't pass --index-url here so we inherit that default. Outside
  133. # the user's machine, pip's own default registry applies — that's the same
  134. # exposure the user would have running `pip install` themselves, so
  135. # we're not widening the supply-chain surface.
  136. #
  137. # --prefer-binary: on ARM64 Windows, pip's default resolver picks a
  138. # `cryptography` version with no published binary wheel and tries to
  139. # build from source, which needs Rust/Cargo (almost never present
  140. # on user machines). The build fails and the whole bootstrap returns
  141. # BUILD_FAILED. A binary wheel exists on PyPI for an adjacent
  142. # version (`cryptography-46.0.3-cp311-abi3-win_arm64.whl`);
  143. # --prefer-binary tells pip to pick it. Cross-platform safe: no-op
  144. # on platforms where the latest version already has a wheel.
  145. err_phase = "pip"
  146. subprocess.run(
  147. [str(venv_py), "-m", "pip", "install", "--quiet",
  148. "--disable-pip-version-check", "--prefer-binary",
  149. "claude-agent-sdk"],
  150. capture_output=True, timeout=120, check=True,
  151. )
  152. return BUILT, "", ""
  153. except subprocess.CalledProcessError as e:
  154. # Capture a stderr fingerprint so telemetry can split BUILD_FAILED by
  155. # root cause (no-network, package-not-found, dns-fail, etc.).
  156. # Categorize first, then keep a short raw tail for the long tail of
  157. # unexpected modes.
  158. stderr_b = e.stderr or b""
  159. if isinstance(stderr_b, bytes):
  160. stderr_str = stderr_b.decode("utf-8", errors="replace")
  161. else:
  162. stderr_str = str(stderr_b)
  163. s = stderr_str.lower()
  164. if "no matching distribution" in s or "could not find a version" in s:
  165. err_kind = "pip_no_match"
  166. elif "name or service not known" in s or "name resolution" in s \
  167. or "nodename nor servname" in s or "temporary failure in name" in s:
  168. err_kind = "dns_fail"
  169. elif "connection refused" in s or "connection reset" in s:
  170. err_kind = "conn_refused"
  171. elif "ssl" in s and ("verify" in s or "certificate" in s):
  172. err_kind = "ssl_verify"
  173. elif "permission denied" in s or "read-only file system" in s:
  174. err_kind = "perm_denied"
  175. elif "no module named pip" in s or "no module named ensurepip" in s:
  176. err_kind = "no_pip"
  177. elif "no space left" in s or "disk quota" in s:
  178. err_kind = "disk_full"
  179. elif "proxy" in s and ("authent" in s or "tunnel" in s or "407" in s):
  180. err_kind = "proxy_auth"
  181. elif "timeout" in s or "timed out" in s:
  182. err_kind = "stderr_timeout"
  183. else:
  184. # First 60 chars of the last non-empty stderr line — bounded to
  185. # stay inside CC's metric value-length budget. Real failure modes
  186. # we haven't categorized show up here as a low-cardinality bucket.
  187. tail = next(
  188. (ln.strip() for ln in reversed(stderr_str.splitlines()) if ln.strip()),
  189. "",
  190. )[:60]
  191. err_kind = f"other:{tail}" if tail else "other"
  192. return BUILD_FAILED, err_phase, err_kind
  193. except subprocess.TimeoutExpired:
  194. return BUILD_FAILED, err_phase, "subprocess_timeout"
  195. except Exception as e:
  196. return BUILD_FAILED, err_phase, f"exc:{type(e).__name__}"
  197. finally:
  198. # Only remove the sentinel if THIS process created it. The
  199. # FileExistsError path above means another process owns the lock;
  200. # unconditionally unlinking here would delete its sentinel and let
  201. # a third concurrent SessionStart `venv --clear` over the in-flight
  202. # build.
  203. if we_own_sentinel:
  204. sentinel.unlink(missing_ok=True)
  205. def _maybe_emit_user_notice(outcome: int, pv: int) -> str | None:
  206. """Return a one-time user-visible notice when the agentic reviewer is
  207. in a persistent broken state on this machine, or None if we've already
  208. shown the notice for this plugin version (or shouldn't show one).
  209. The marker file is plugin-version-keyed: a future plugin update can
  210. re-notify if behavior changes (e.g. we ship out-of-process SDK in v3
  211. and want to tell affected users it's fixed). Failures to write the
  212. marker degrade to "skip the notice this session" so we don't spam
  213. every SessionStart on a read-only home dir.
  214. Currently only HOOK_PY_INCOMPATIBLE qualifies. BUILD_FAILED is
  215. intentionally excluded — it covers transient causes (network failure,
  216. pip registry hiccup, in-flight rebuild) where the next session may
  217. succeed and a permanent notice would mislead.
  218. """
  219. if outcome != HOOK_PY_INCOMPATIBLE:
  220. return None
  221. try:
  222. state_dir = Path(
  223. os.environ.get("SECURITY_WARNINGS_STATE_DIR")
  224. or os.path.expanduser("~/.claude/security")
  225. )
  226. marker = state_dir / f".agentic_unavailable_notice_v{pv or 0}"
  227. if marker.exists():
  228. return None
  229. state_dir.mkdir(parents=True, exist_ok=True)
  230. # Write timestamp + Python version so the marker is self-documenting
  231. # if a user goes looking. O_EXCL would be racier with no real win
  232. # (two concurrent SessionStarts both showing the notice once is fine).
  233. marker.write_text(
  234. f"{time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())} "
  235. f"py={sys.version_info[0]}.{sys.version_info[1]}\n"
  236. )
  237. except OSError:
  238. return None
  239. return (
  240. f"⚠ security-guidance plugin: the cross-file commit reviewer "
  241. f"(layer 3 of 3 — catches IDOR, auth-bypass, cross-file SSRF) "
  242. f"is unavailable in this environment. It requires Python ≥3.10, "
  243. f"but the hook is running on "
  244. f"{sys.version_info[0]}.{sys.version_info[1]}.\n\n"
  245. f"Pattern checks and the single-shot LLM diff review are still "
  246. f"active. To enable the deeper reviewer, install Python 3.10+ "
  247. f"(e.g. `brew install python` on macOS) and restart Claude Code.\n\n"
  248. f"This notice is shown once per plugin version. "
  249. f"See: github.com/anthropics/claude-plugins-official/issues/2071"
  250. )
  251. if __name__ == "__main__":
  252. # Tell the harness this is async — venv create + pip install can take
  253. # 30-60s on a cold cache, well past the default sync hook timeout.
  254. # SessionStart runs before the user's first prompt; doing this in the
  255. # background means the first commit-review of the session usually finds
  256. # the venv ready.
  257. print(json.dumps({"async": True, "asyncTimeout": 180000}), flush=True)
  258. t0 = time.perf_counter()
  259. try:
  260. outcome, err_phase, err_kind = main()
  261. except Exception as exc:
  262. outcome, err_phase, err_kind = (
  263. BUILD_FAILED, "main", f"exc:{type(exc).__name__}"
  264. )
  265. # CC's async-hook registry scans stdout line-by-line after process exit
  266. # and takes the FIRST non-{"async":...} JSON line as the hook response;
  267. # its `metrics` key is forwarded to the hook metrics event on the
  268. # next attachments pass. Must be a single line — the registry splits on
  269. # \n and json-parses each independently. Values must be bool|number OR
  270. # short strings (CC accepts string metric values if they're not
  271. # null). Stay inside the 10-key emit cap.
  272. metrics: dict[str, object] = {
  273. "sdk_bootstrap": outcome,
  274. "sdk_bootstrap_ms": round((time.perf_counter() - t0) * 1000),
  275. }
  276. if err_kind:
  277. # Truncate defensively; categorized values are <40 chars but the
  278. # `other:<tail>` mode could be longer. err_phase may be empty for
  279. # pre-venv failures (state_dir.mkdir perm-denied, sentinel O_EXCL
  280. # raising a non-FileExistsError OSError) — emit as "pre" so the
  281. # err_kind isn't silently dropped.
  282. metrics["sdk_bootstrap_phase"] = (err_phase or "pre")[:16]
  283. metrics["sdk_bootstrap_err"] = err_kind[:96]
  284. pv = _plugin_version_int()
  285. if pv:
  286. metrics["pv"] = pv
  287. response: dict[str, object] = {"metrics": metrics}
  288. # One-time user-visible notice when the agentic reviewer is dead on
  289. # arrival. Uses hookSpecificOutput.additionalContext (SessionStart's
  290. # supported channel for surfacing text to both the model and the user)
  291. # plus systemMessage as a belt-and-suspenders. Marker-file-gated so
  292. # this fires exactly once per plugin version per install — see
  293. # _maybe_emit_user_notice.
  294. notice = _maybe_emit_user_notice(outcome, pv)
  295. if notice:
  296. response["hookSpecificOutput"] = {
  297. "hookEventName": "SessionStart",
  298. "additionalContext": notice,
  299. }
  300. response["systemMessage"] = notice
  301. print(json.dumps(response), flush=True)