ensure_agent_sdk.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. # Shared state-dir resolver: SECURITY_WARNINGS_STATE_DIR → CLAUDE_CONFIG_DIR/security
  23. # → ~/.claude/security. See _base.state_dir for resolution precedence. Re-aliased
  24. # here to match the existing local name (state_dir was already a local var in
  25. # main() and _maybe_emit_user_notice).
  26. from _base import state_dir as _resolve_state_dir
  27. # Outcome codes for the sdk_bootstrap metric. Values are stable for telemetry.
  28. NOOP_SYSTEM = 0 # claude_agent_sdk already importable in system python
  29. NOOP_VENV = 1 # venv already built and SDK imports from it
  30. BUILT = 2 # venv created + SDK pip-installed this run
  31. BUILD_FAILED = 3 # venv create or pip install raised/timed out
  32. # Outcome 4 was previously SKIP_WIN32; retired now that the consumer glob in
  33. # llm.py also matches Windows venv layout (Lib/site-packages). Don't reuse the
  34. # value — telemetry rows from older plugin builds still emit 4.
  35. SKIP_SENTINEL = 5 # another SessionStart is currently building
  36. HOOK_PY_INCOMPATIBLE = 6 # hook interpreter is <3.10 — SDK syntax can't load
  37. # here no matter how the venv was built. See #2071.
  38. # --target fallback: when `python -m venv` can't bootstrap pip (ensurepip
  39. # missing — Debian python3-venv not installed, or a python.org/pyenv build
  40. # without ensurepip), fall back to `pip install --target <dir>` which needs
  41. # only the system pip, not venv/ensurepip. Telemetry (v2.0.4 sdk_has_pip
  42. # probe) confirmed ~95% of venv_ensurepip_fail users HAVE pip, so this
  43. # recovers the agentic reviewer for them instead of degrading to pattern +
  44. # single-shot review. See #2154 follow-up.
  45. BUILT_TARGET = 7 # venv ensurepip failed → SDK pip-installed via --target
  46. NOOP_TARGET = 8 # --target libs already present and importable
  47. # Phase + err-kind integer encoding for sdk_bootstrap_phase / sdk_bootstrap_err.
  48. #
  49. # Earlier versions emitted these as STRINGS (e.g. "pip", "dns_fail"). CC's
  50. # plugin-metrics pipeline silently drops plugin-emitted string values —
  51. # only `bool|finite-number` plugin metrics reach BigQuery. (CC-core
  52. # metrics like `subscription_type` are exempt because they're injected
  53. # downstream of plugin validation.) Confirmed empirically: 185K
  54. # BUILD_FAILED rows in BQ had `sdk_bootstrap_phase`/`sdk_bootstrap_err`
  55. # = NULL despite the Python code emitting them. This left ~28K
  56. # BUILD_FAILED sessions/day with no diagnostic split — flying blind on
  57. # the real failure modes (pip-no-match vs dns-fail vs ssl-verify etc.).
  58. #
  59. # Fix: encode as small integers per the maps below. Values are
  60. # APPEND-ONLY for telemetry stability. Reserve 99 as the "unknown /
  61. # uncategorized" bucket so an unmapped err_kind (e.g., a new exception
  62. # type) still emits a non-zero signal.
  63. SDK_BOOTSTRAP_PHASE_CODES = {
  64. "pre": 1, # pre-venv (state_dir.mkdir, sentinel open)
  65. "venv": 2, # python -m venv --clear
  66. "pip": 3, # pip install
  67. "main": 4, # uncaught exception above main()
  68. "pip_target": 5, # `pip install --target` fallback (venv ensurepip failed)
  69. }
  70. SDK_BOOTSTRAP_ERR_CODES = {
  71. "pip_no_match": 1,
  72. "dns_fail": 2,
  73. "conn_refused": 3,
  74. "ssl_verify": 4,
  75. "perm_denied": 5,
  76. "no_pip": 6,
  77. "disk_full": 7,
  78. "proxy_auth": 8,
  79. "stderr_timeout": 9, # pip stderr containing "timeout"/"timed out"
  80. "subprocess_timeout": 10, # subprocess.TimeoutExpired (>120s)
  81. # Venv-stage specific categories added after PR #2112 telemetry surfaced
  82. # 2,406 phase=2/err=99 sessions in the first 3h of v2.0.1 — venv phase
  83. # failing in ways the original pip-flavored patterns didn't catch. These
  84. # all split out of what was previously collapsing to _uncategorized.
  85. "venv_ensurepip_fail": 11, # Debian/Ubuntu missing python3-venv;
  86. # stderr mentions ensurepip non-zero exit
  87. # or "ensurepip is not available"
  88. "venv_path_too_long": 12, # Windows MAX_PATH (260) or POSIX
  89. # ENAMETOOLONG — venv writes deep paths
  90. # under state_dir/agent-sdk-venv/Lib/...
  91. "venv_no_module": 13, # `python3 -m venv` itself missing — "No
  92. # module named 'venv'" / "No module named venv"
  93. "venv_already_exists": 14, # Errno 17 / "file exists" — sentinel race
  94. # past O_EXCL or stale dir survived --clear
  95. "venv_setup_failed": 15, # Generic "virtual environment was not
  96. # created successfully" — catches the long
  97. # tail of venv setup failures that don't
  98. # match a more specific category above
  99. # 16–98 reserved for future categories; APPEND-ONLY.
  100. # 99 catches everything else (including "exc:<TypeName>" and "other:<tail>"
  101. # — the original string is debug-loggable but the integer is what makes
  102. # it to telemetry). For the "other:" tail, `sdk_bootstrap_stderr_sig`
  103. # carries a bounded integer hash so we can still distinguish patterns
  104. # in BQ aggregation.
  105. "_uncategorized": 99,
  106. }
  107. # Exception-type encoding for the "exc:<TypeName>" err_kinds (the generic
  108. # `except Exception` path — venv/pip raised a Python exception rather than
  109. # a CalledProcessError with categorizable stderr).
  110. #
  111. # #2154 telemetry surfaced that the dominant remaining venv BUILD_FAILED
  112. # bucket (phase=venv, err=99) is ~99% `exc:` with stderr_sig=NULL — i.e.
  113. # exceptions, not stderr-bearing subprocess failures — so the stderr_sig
  114. # hash couldn't distinguish them. This maps the exception TYPE to a stable
  115. # code so BQ can tell FileNotFoundError (python/venv binary missing) from
  116. # PermissionError (read-only home) from a bare OSError, etc.
  117. #
  118. # All the FileNotFoundError/PermissionError/etc. entries are OSError
  119. # subclasses, so they ALSO carry an errno (see _encode_errno) — the type
  120. # code gives the Python class, errno gives the OS-level cause. APPEND-ONLY.
  121. SDK_BOOTSTRAP_EXC_CODES = {
  122. "FileNotFoundError": 1, # interpreter/venv path component missing
  123. "PermissionError": 2, # read-only home, sandboxed FS
  124. "NotADirectoryError": 3,
  125. "IsADirectoryError": 4,
  126. "FileExistsError": 5, # (sentinel race is handled separately; this
  127. # is FileExistsError from elsewhere in venv)
  128. "OSError": 6, # bare OSError — errno carries the real cause
  129. "BlockingIOError": 7,
  130. "BrokenPipeError": 8,
  131. "ConnectionError": 9,
  132. "TimeoutError": 10, # distinct from subprocess.TimeoutExpired
  133. "InterruptedError": 11,
  134. "MemoryError": 12,
  135. "UnicodeDecodeError": 13,
  136. "ValueError": 14,
  137. "RuntimeError": 15,
  138. # 16–98 reserved; APPEND-ONLY.
  139. "_other_exc": 99, # an exception type not in this map
  140. }
  141. def _encode_phase(s):
  142. """Map err_phase string to its telemetry integer code, or 0 if unset.
  143. Empty/None → 0 lets `if encoded:` cleanly skip emission. Per
  144. SDK_BOOTSTRAP_PHASE_CODES, valid codes are 1-4."""
  145. return SDK_BOOTSTRAP_PHASE_CODES.get((s or "").strip(), 0)
  146. def _encode_err_kind(s):
  147. """Map err_kind string to its telemetry integer code, or 0 if unset.
  148. Direct hits use the static map; "exc:<X>" and "other:<tail>" both
  149. collapse to _uncategorized (99) — the raw string survives in debug
  150. logs, only the integer reaches BQ."""
  151. s = (s or "").strip()
  152. if not s:
  153. return 0
  154. if s in SDK_BOOTSTRAP_ERR_CODES:
  155. return SDK_BOOTSTRAP_ERR_CODES[s]
  156. # Prefix matches for the catch-all categories
  157. if s.startswith("exc:") or s.startswith("other:") or s == "other":
  158. return SDK_BOOTSTRAP_ERR_CODES["_uncategorized"]
  159. # Unknown string — still emit as uncategorized rather than dropping
  160. return SDK_BOOTSTRAP_ERR_CODES["_uncategorized"]
  161. def _encode_stderr_sig(err_kind):
  162. """Bounded integer hash of the stderr tail captured in "other:<tail>"
  163. err_kinds. Lets us distinguish patterns INSIDE the _uncategorized
  164. (code 99) bucket without unbounded cardinality.
  165. Returns 0 for non-"other:" err_kinds (so the field auto-omits from
  166. emit_metrics on categorized failures — see the emit block in main()).
  167. Strategy: take the tail's first ~30 chars (post-lowercase, post-trim),
  168. SHA-1, fold the first 2 bytes to 0–999. Different stderr messages
  169. cluster into different buckets; same stderr always maps to the same
  170. bucket. Cardinality is bounded at 1000, well below any "high
  171. cardinality" alarm — and a real failure mode typically produces
  172. near-identical stderr across thousands of machines, so 1000 buckets
  173. is comfortably wide.
  174. Why first ~30 chars: stderr like "ERROR: Command failed: <full
  175. path>" varies the tail wildly (paths) but the categorization signal
  176. is in the leading words. Dropping the suffix focuses the hash on
  177. the discriminative part.
  178. """
  179. if not err_kind or not err_kind.startswith("other:"):
  180. return 0
  181. import hashlib
  182. tail = err_kind[len("other:"):].strip().lower()[:30]
  183. if not tail:
  184. return 0
  185. h = hashlib.sha1(tail.encode("utf-8", errors="replace")).digest()
  186. return int.from_bytes(h[:2], "big") % 1000
  187. def _encode_exc_kind(err_kind):
  188. """Map an "exc:<TypeName>[:errno]" err_kind to its exception-type code
  189. (SDK_BOOTSTRAP_EXC_CODES). Returns 0 for non-exc err_kinds (so the
  190. sdk_bootstrap_exc field auto-omits on stderr/categorized failures).
  191. Unmapped exception types → 99 (_other_exc)."""
  192. if not err_kind or not err_kind.startswith("exc:"):
  193. return 0
  194. # "exc:OSError:28" → "OSError"; "exc:RuntimeError" → "RuntimeError"
  195. name = err_kind[len("exc:"):].split(":", 1)[0].strip()
  196. if not name:
  197. return 0
  198. return SDK_BOOTSTRAP_EXC_CODES.get(name, SDK_BOOTSTRAP_EXC_CODES["_other_exc"])
  199. def _encode_errno(err_kind):
  200. """Extract the OS errno from an "exc:<TypeName>:<errno>" err_kind.
  201. OSError-family exceptions embed their errno (ENOENT=2, EACCES=13,
  202. ENOSPC=28, …) — the OS-level cause is far more actionable than the
  203. Python class alone. Returns 0 when absent/non-numeric (field omitted)."""
  204. if not err_kind or not err_kind.startswith("exc:"):
  205. return 0
  206. parts = err_kind.split(":")
  207. if len(parts) < 3:
  208. return 0
  209. try:
  210. return int(parts[2])
  211. except (ValueError, IndexError):
  212. return 0
  213. def _probe_has_pip() -> bool:
  214. """True iff the current interpreter can run pip (`-m pip --version`).
  215. Probed only on the venv_ensurepip_fail path (see __main__), NOT on the
  216. happy path — it's an extra subprocess we only want when diagnosing a
  217. failure. The result decides whether a `pip install --target` fallback
  218. (Option A) is even viable for this machine: ensurepip/venv missing but
  219. pip present → --target would work; pip also missing → it wouldn't, and
  220. the user needs a system package (python3-venv / a complete Python)."""
  221. try:
  222. r = subprocess.run(
  223. [sys.executable, "-m", "pip", "--version"],
  224. capture_output=True, timeout=10,
  225. )
  226. return r.returncode == 0
  227. except Exception:
  228. return False
  229. def _pip_err_from_stderr(stderr_b):
  230. """Categorize a pip-install stderr into a known err_kind (the pip subset
  231. of SDK_BOOTSTRAP_ERR_CODES). Used by the --target fallback; mirrors the
  232. pip branches of main()'s inline categorizer. Kept as a sibling rather
  233. than extracting main()'s chain (which also has venv-phase branches) to
  234. avoid disturbing the working venv categorization."""
  235. if isinstance(stderr_b, bytes):
  236. s = stderr_b.decode("utf-8", errors="replace")
  237. else:
  238. s = str(stderr_b or "")
  239. low = s.lower()
  240. if "no matching distribution" in low or "could not find a version" in low:
  241. return "pip_no_match"
  242. if ("name or service not known" in low or "name resolution" in low
  243. or "nodename nor servname" in low or "temporary failure in name" in low):
  244. return "dns_fail"
  245. if "connection refused" in low or "connection reset" in low:
  246. return "conn_refused"
  247. if "ssl" in low and ("verify" in low or "certificate" in low):
  248. return "ssl_verify"
  249. if "permission denied" in low or "read-only file system" in low:
  250. return "perm_denied"
  251. if "no module named pip" in low or "no module named ensurepip" in low:
  252. return "no_pip"
  253. if "no space left" in low or "disk quota" in low:
  254. return "disk_full"
  255. if "proxy" in low and ("authent" in low or "tunnel" in low or "407" in low):
  256. return "proxy_auth"
  257. if "timeout" in low or "timed out" in low:
  258. return "stderr_timeout"
  259. tail = next((ln.strip() for ln in reversed(s.splitlines()) if ln.strip()), "")[:60]
  260. return f"other:{tail}" if tail else "other"
  261. def _target_dir(state_dir) -> Path:
  262. return Path(state_dir) / "agent-sdk-libs"
  263. def _target_sdk_importable(state_dir) -> bool:
  264. """True iff the --target libs dir has an importable claude_agent_sdk,
  265. probed with THIS interpreter (the one llm.py will import it from) and the
  266. target dir prepended to sys.path. Cheap dir-check first to avoid a
  267. subprocess on the common no-target path."""
  268. target = _target_dir(state_dir)
  269. if not (target / "claude_agent_sdk").is_dir():
  270. return False
  271. try:
  272. r = subprocess.run(
  273. [sys.executable, "-c",
  274. "import sys; sys.path.insert(0, sys.argv[1]); import claude_agent_sdk",
  275. str(target)],
  276. capture_output=True, timeout=10,
  277. )
  278. return r.returncode == 0
  279. except Exception:
  280. return False
  281. def _build_via_target(state_dir) -> tuple[int, str, str]:
  282. """Fallback install when `python -m venv` can't bootstrap pip (ensurepip
  283. missing — Debian python3-venv absent, or a python.org/pyenv build without
  284. ensurepip). `pip install --target <dir>` needs only the system pip, not
  285. venv/ensurepip. v2.0.4 telemetry (sdk_has_pip) confirmed ~95% of
  286. venv_ensurepip_fail users have pip. The consumer (llm.py) adds this flat
  287. dir to sys.path. Returns (outcome, err_phase, err_kind).
  288. --upgrade so a stale/partial target dir from a prior failed attempt
  289. doesn't make pip refuse; --prefer-binary mirrors the venv path's wheel
  290. preference (ARM64 Windows cryptography)."""
  291. target = _target_dir(state_dir)
  292. try:
  293. subprocess.run(
  294. [sys.executable, "-m", "pip", "install",
  295. "--target", str(target), "--upgrade",
  296. "--disable-pip-version-check", "--prefer-binary",
  297. "claude-agent-sdk"],
  298. capture_output=True, timeout=120, check=True,
  299. )
  300. return BUILT_TARGET, "", ""
  301. except subprocess.CalledProcessError as e:
  302. return BUILD_FAILED, "pip_target", _pip_err_from_stderr(e.stderr)
  303. except subprocess.TimeoutExpired:
  304. return BUILD_FAILED, "pip_target", "subprocess_timeout"
  305. except Exception as e:
  306. errno = getattr(e, "errno", None)
  307. if isinstance(errno, int):
  308. return BUILD_FAILED, "pip_target", f"exc:{type(e).__name__}:{errno}"
  309. return BUILD_FAILED, "pip_target", f"exc:{type(e).__name__}"
  310. def _sdk_on_syspath() -> bool:
  311. # find_spec is ~10ms; actually importing the SDK pulls in
  312. # transitive deps and costs ~800ms — too heavy for a
  313. # per-SessionStart no-op check that most sessions hit.
  314. try:
  315. return importlib.util.find_spec("claude_agent_sdk") is not None
  316. except Exception:
  317. return False
  318. def _plugin_version_int() -> int:
  319. # Same encoding as security_reminder_hook._read_plugin_version_int so
  320. # metrics rows from both hooks join on pv.
  321. try:
  322. p = Path(__file__).parent.parent / ".claude-plugin" / "plugin.json"
  323. v = json.loads(p.read_text())["version"]
  324. major, minor, patch = (int(x) for x in v.split(".")[:3])
  325. return major * 10000 + minor * 100 + patch
  326. except Exception:
  327. return 0
  328. def main() -> tuple[int, str, str]:
  329. """Run the bootstrap. Returns (outcome, err_phase, err_kind).
  330. err_phase / err_kind are non-empty only on BUILD_FAILED — they let
  331. telemetry split bootstrap failures by root cause.
  332. """
  333. # Honesty check (fixes the misleading NOOP_VENV in #2071): the SDK
  334. # requires Python >=3.10 and uses 3.10+ syntax (match statements,
  335. # PEP 604 unions). On a 3.9 hook interpreter we CANNOT import it no
  336. # matter how the venv was built — llm.py runs in this same interpreter
  337. # and the syntax-level import will SyntaxError. macOS ships 3.9.6 as
  338. # the default `python3` and `/usr/bin` precedes Homebrew in PATH, so
  339. # this case is the default state for a large share of macOS users.
  340. #
  341. # sg-python.sh now prefers python3.10+ binaries so most users won't
  342. # reach this branch; the fallback to 3.9 is preserved for the
  343. # pattern-warning hooks that don't need the SDK. Reporting
  344. # HOOK_PY_INCOMPATIBLE here:
  345. # (a) avoids 30-60s of wasted pip install,
  346. # (b) avoids the lie where the venv_py probe says NOOP_VENV but the
  347. # consumer import fails, and
  348. # (c) gives telemetry a clean bucket to size the affected fleet.
  349. if sys.version_info < (3, 10):
  350. return (
  351. HOOK_PY_INCOMPATIBLE,
  352. "hook_py",
  353. f"py_{sys.version_info[0]}.{sys.version_info[1]}",
  354. )
  355. if _sdk_on_syspath():
  356. return NOOP_SYSTEM, "", ""
  357. state_dir = Path(_resolve_state_dir())
  358. venv = state_dir / "agent-sdk-venv"
  359. # Windows venvs put the interpreter at Scripts\python.exe; POSIX uses bin/python.
  360. if sys.platform == "win32":
  361. venv_py = venv / "Scripts" / "python.exe"
  362. else:
  363. venv_py = venv / "bin" / "python"
  364. # Another SessionStart (concurrent CC instance, same plugin) may already
  365. # be building. The sentinel lives NEXT TO the venv, not inside it —
  366. # `python -m venv --clear` wipes the target dir's contents, so an
  367. # in-venv sentinel would be deleted the instant we create the venv.
  368. # Stale sentinels (>5min) from a SIGKILL'd build are ignored.
  369. sentinel = state_dir / "agent-sdk-venv.building"
  370. if sentinel.exists():
  371. try:
  372. if time.time() - sentinel.stat().st_mtime < 300:
  373. return SKIP_SENTINEL, "", ""
  374. sentinel.unlink(missing_ok=True)
  375. except OSError:
  376. return SKIP_SENTINEL, "", ""
  377. # If a venv already exists and its python can import the SDK, done.
  378. if venv_py.exists():
  379. try:
  380. r = subprocess.run(
  381. [str(venv_py), "-c", "import claude_agent_sdk"],
  382. capture_output=True, timeout=10,
  383. )
  384. if r.returncode == 0:
  385. return NOOP_VENV, "", ""
  386. except Exception:
  387. pass # broken venv; rebuild below
  388. # If a prior run installed the SDK via the --target fallback (ensurepip
  389. # path), reuse it. Only reached when there's no working venv, so healthy
  390. # NOOP_VENV users never pay for this probe.
  391. if _target_sdk_importable(state_dir):
  392. return NOOP_TARGET, "", ""
  393. err_phase = ""
  394. err_kind = ""
  395. we_own_sentinel = False
  396. try:
  397. state_dir.mkdir(parents=True, exist_ok=True)
  398. # O_EXCL makes the sentinel an atomic lock — if two SessionStarts
  399. # race past the exists() check above, only one creates it.
  400. try:
  401. os.close(os.open(sentinel, os.O_CREAT | os.O_EXCL | os.O_WRONLY))
  402. except FileExistsError:
  403. return SKIP_SENTINEL, "", ""
  404. we_own_sentinel = True
  405. err_phase = "venv"
  406. subprocess.run(
  407. [sys.executable, "-m", "venv", "--clear", str(venv)],
  408. capture_output=True, timeout=60, check=True,
  409. )
  410. # Some machines route pip through a private registry; we
  411. # don't pass --index-url here so we inherit that default. Outside
  412. # the user's machine, pip's own default registry applies — that's the same
  413. # exposure the user would have running `pip install` themselves, so
  414. # we're not widening the supply-chain surface.
  415. #
  416. # --prefer-binary: on ARM64 Windows, pip's default resolver picks a
  417. # `cryptography` version with no published binary wheel and tries to
  418. # build from source, which needs Rust/Cargo (almost never present
  419. # on user machines). The build fails and the whole bootstrap returns
  420. # BUILD_FAILED. A binary wheel exists on PyPI for an adjacent
  421. # version (`cryptography-46.0.3-cp311-abi3-win_arm64.whl`);
  422. # --prefer-binary tells pip to pick it. Cross-platform safe: no-op
  423. # on platforms where the latest version already has a wheel.
  424. err_phase = "pip"
  425. subprocess.run(
  426. [str(venv_py), "-m", "pip", "install", "--quiet",
  427. "--disable-pip-version-check", "--prefer-binary",
  428. "claude-agent-sdk"],
  429. capture_output=True, timeout=120, check=True,
  430. )
  431. return BUILT, "", ""
  432. except subprocess.CalledProcessError as e:
  433. # Capture a stderr fingerprint so telemetry can split BUILD_FAILED by
  434. # root cause (no-network, package-not-found, dns-fail, etc.).
  435. # Categorize first, then keep a short raw tail for the long tail of
  436. # unexpected modes.
  437. stderr_b = e.stderr or b""
  438. if isinstance(stderr_b, bytes):
  439. stderr_str = stderr_b.decode("utf-8", errors="replace")
  440. else:
  441. stderr_str = str(stderr_b)
  442. s = stderr_str.lower()
  443. # Venv-specific patterns checked FIRST — they overlap with some pip
  444. # patterns (e.g. "no module named ensurepip" could match no_pip OR
  445. # venv_ensurepip_fail; the venv-stage interpretation is the right
  446. # one when err_phase=="venv"). Order is venv-most-specific →
  447. # pip-historical → generic.
  448. if err_phase == "venv" and (
  449. "ensurepip is not available" in s
  450. or ("ensurepip" in s and "returned non-zero" in s)
  451. or "the virtual environment was not created" in s and "ensurepip" in s
  452. ):
  453. err_kind = "venv_ensurepip_fail"
  454. elif err_phase == "venv" and (
  455. "[errno 36]" in s
  456. or "file name too long" in s
  457. or "path too long" in s
  458. ):
  459. err_kind = "venv_path_too_long"
  460. elif err_phase == "venv" and (
  461. "no module named venv" in s
  462. or "no module named 'venv'" in s
  463. ):
  464. err_kind = "venv_no_module"
  465. elif err_phase == "venv" and (
  466. "[errno 17]" in s
  467. or ("file exists" in s and "venv" in s)
  468. ):
  469. err_kind = "venv_already_exists"
  470. elif "no matching distribution" in s or "could not find a version" in s:
  471. err_kind = "pip_no_match"
  472. elif "name or service not known" in s or "name resolution" in s \
  473. or "nodename nor servname" in s or "temporary failure in name" in s:
  474. err_kind = "dns_fail"
  475. elif "connection refused" in s or "connection reset" in s:
  476. err_kind = "conn_refused"
  477. elif "ssl" in s and ("verify" in s or "certificate" in s):
  478. err_kind = "ssl_verify"
  479. elif "permission denied" in s or "read-only file system" in s:
  480. err_kind = "perm_denied"
  481. elif "no module named pip" in s or "no module named ensurepip" in s:
  482. err_kind = "no_pip"
  483. elif "no space left" in s or "disk quota" in s:
  484. err_kind = "disk_full"
  485. elif "proxy" in s and ("authent" in s or "tunnel" in s or "407" in s):
  486. err_kind = "proxy_auth"
  487. elif "timeout" in s or "timed out" in s:
  488. err_kind = "stderr_timeout"
  489. elif err_phase == "venv" and (
  490. "virtual environment was not created" in s
  491. or "error: command" in s and "venv" in s
  492. ):
  493. # Generic venv-setup catch-all — matched AFTER the more specific
  494. # venv patterns above so we don't shadow them, but BEFORE the
  495. # other: fallback so generic venv setup failures get their own
  496. # bucket instead of polluting the long-tail signature space.
  497. err_kind = "venv_setup_failed"
  498. else:
  499. # First 60 chars of the last non-empty stderr line — bounded to
  500. # stay inside CC's metric value-length budget. Real failure modes
  501. # we haven't categorized show up here as a low-cardinality bucket.
  502. tail = next(
  503. (ln.strip() for ln in reversed(stderr_str.splitlines()) if ln.strip()),
  504. "",
  505. )[:60]
  506. err_kind = f"other:{tail}" if tail else "other"
  507. # venv couldn't bootstrap pip (ensurepip missing) but pip itself may
  508. # work — fall back to a flat `pip install --target`. Only this one
  509. # category falls through; every other venv/pip failure is terminal.
  510. # The finally block unlinks our sentinel first (so the target build
  511. # isn't blocked by it); _build_via_target does the target install.
  512. if err_kind == "venv_ensurepip_fail":
  513. if we_own_sentinel:
  514. sentinel.unlink(missing_ok=True)
  515. we_own_sentinel = False
  516. return _build_via_target(state_dir)
  517. return BUILD_FAILED, err_phase, err_kind
  518. except subprocess.TimeoutExpired:
  519. return BUILD_FAILED, err_phase, "subprocess_timeout"
  520. except Exception as e:
  521. # Embed errno for OSError-family exceptions ("exc:OSError:28") so
  522. # telemetry can decode the OS-level cause (ENOENT/EACCES/ENOSPC/…),
  523. # not just the Python class. #2154 follow-up: this is the dominant
  524. # remaining venv BUILD_FAILED bucket. See _encode_exc_kind/_encode_errno.
  525. errno = getattr(e, "errno", None)
  526. if isinstance(errno, int):
  527. return BUILD_FAILED, err_phase, f"exc:{type(e).__name__}:{errno}"
  528. return BUILD_FAILED, err_phase, f"exc:{type(e).__name__}"
  529. finally:
  530. # Only remove the sentinel if THIS process created it. The
  531. # FileExistsError path above means another process owns the lock;
  532. # unconditionally unlinking here would delete its sentinel and let
  533. # a third concurrent SessionStart `venv --clear` over the in-flight
  534. # build.
  535. if we_own_sentinel:
  536. sentinel.unlink(missing_ok=True)
  537. def _maybe_emit_user_notice(outcome: int, pv: int) -> str | None:
  538. """Return a one-time user-visible notice when the agentic reviewer is
  539. in a persistent broken state on this machine, or None if we've already
  540. shown the notice for this plugin version (or shouldn't show one).
  541. The marker file is plugin-version-keyed: a future plugin update can
  542. re-notify if behavior changes (e.g. we ship out-of-process SDK in v3
  543. and want to tell affected users it's fixed). Failures to write the
  544. marker degrade to "skip the notice this session" so we don't spam
  545. every SessionStart on a read-only home dir.
  546. Currently only HOOK_PY_INCOMPATIBLE qualifies. BUILD_FAILED is
  547. intentionally excluded — it covers transient causes (network failure,
  548. pip registry hiccup, in-flight rebuild) where the next session may
  549. succeed and a permanent notice would mislead.
  550. """
  551. if outcome != HOOK_PY_INCOMPATIBLE:
  552. return None
  553. try:
  554. state_dir = Path(_resolve_state_dir())
  555. marker = state_dir / f".agentic_unavailable_notice_v{pv or 0}"
  556. if marker.exists():
  557. return None
  558. state_dir.mkdir(parents=True, exist_ok=True)
  559. # Write timestamp + Python version so the marker is self-documenting
  560. # if a user goes looking. O_EXCL would be racier with no real win
  561. # (two concurrent SessionStarts both showing the notice once is fine).
  562. marker.write_text(
  563. f"{time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())} "
  564. f"py={sys.version_info[0]}.{sys.version_info[1]}\n"
  565. )
  566. except OSError:
  567. return None
  568. return (
  569. f"⚠ security-guidance plugin: the cross-file commit reviewer "
  570. f"(layer 3 of 3 — catches IDOR, auth-bypass, cross-file SSRF) "
  571. f"is unavailable in this environment. It requires Python ≥3.10, "
  572. f"but the hook is running on "
  573. f"{sys.version_info[0]}.{sys.version_info[1]}.\n\n"
  574. f"Pattern checks and the single-shot LLM diff review are still "
  575. f"active. To enable the deeper reviewer, install Python 3.10+ "
  576. f"(e.g. `brew install python` on macOS) and restart Claude Code.\n\n"
  577. f"This notice is shown once per plugin version. "
  578. f"See: github.com/anthropics/claude-plugins-official/issues/2071"
  579. )
  580. if __name__ == "__main__":
  581. # Tell the harness this is async — venv create + pip install can take
  582. # 30-60s on a cold cache, well past the default sync hook timeout.
  583. # SessionStart runs before the user's first prompt; doing this in the
  584. # background means the first commit-review of the session usually finds
  585. # the venv ready.
  586. print(json.dumps({"async": True, "asyncTimeout": 180000}), flush=True)
  587. t0 = time.perf_counter()
  588. try:
  589. outcome, err_phase, err_kind = main()
  590. except Exception as exc:
  591. outcome, err_phase, err_kind = (
  592. BUILD_FAILED, "main", f"exc:{type(exc).__name__}"
  593. )
  594. # CC's async-hook registry scans stdout line-by-line after process exit
  595. # and takes the FIRST non-{"async":...} JSON line as the hook response;
  596. # its `metrics` key is forwarded to the hook metrics event on the
  597. # next attachments pass. Must be a single line — the registry splits on
  598. # \n and json-parses each independently.
  599. #
  600. # IMPORTANT — values must be bool|finite-number. The validation comment
  601. # has historically said "or short strings" but that was wrong: CC's
  602. # plugin-metrics pipeline silently drops plugin-emitted string values.
  603. # Stay inside the 10-key emit cap.
  604. metrics: dict[str, object] = {
  605. "sdk_bootstrap": outcome,
  606. "sdk_bootstrap_ms": round((time.perf_counter() - t0) * 1000),
  607. }
  608. if err_kind:
  609. # Encode phase + err_kind as integer codes (see
  610. # SDK_BOOTSTRAP_PHASE_CODES / SDK_BOOTSTRAP_ERR_CODES). Earlier
  611. # versions emitted these as strings and CC dropped them — restoring
  612. # the diagnostic split that 28K BUILD_FAILED/day need to triage by
  613. # root cause. err_phase defaults to "pre" when empty (pre-venv
  614. # failure path, e.g. state_dir.mkdir perm-denied).
  615. metrics["sdk_bootstrap_phase"] = _encode_phase(err_phase or "pre")
  616. metrics["sdk_bootstrap_err"] = _encode_err_kind(err_kind)
  617. # For "other:<tail>" (encoded err==99), emit a bounded integer
  618. # hash of the stderr tail so BQ can distinguish patterns inside
  619. # the _uncategorized bucket without unbounded cardinality. Zero
  620. # when err_kind is categorized — the schema reader treats 0 as
  621. # "no signal", matching the absence convention.
  622. sig = _encode_stderr_sig(err_kind)
  623. if sig:
  624. metrics["sdk_bootstrap_stderr_sig"] = sig
  625. # Exception-type + errno for the "exc:" bucket (the dominant
  626. # remaining venv BUILD_FAILED mode per #2154 telemetry). Both
  627. # auto-omit (0) on stderr/categorized failures.
  628. exc = _encode_exc_kind(err_kind)
  629. if exc:
  630. metrics["sdk_bootstrap_exc"] = exc
  631. exc_errno = _encode_errno(err_kind)
  632. if exc_errno:
  633. metrics["sdk_bootstrap_errno"] = exc_errno
  634. # venv_ensurepip_fail (code 11) is the top categorizable venv
  635. # failure, and telemetry shows it's NOT just Debian — macOS has the
  636. # most distinct affected users. Probe whether this interpreter has
  637. # pip so we know if a `pip install --target` fallback (Option A)
  638. # would actually help, vs the user needing a system package. Probed
  639. # only here (not on the happy path) to avoid an extra subprocess
  640. # per healthy session.
  641. if _encode_err_kind(err_kind) == 11:
  642. metrics["sdk_has_pip"] = _probe_has_pip()
  643. # Interpreter version (major*100 + minor, e.g. 309 / 312), emitted on
  644. # every bootstrap. Disambiguates the macOS cohort (Apple 3.9 vs a 3.10+
  645. # with broken ensurepip) for both venv_ensurepip_fail AND
  646. # HOOK_PY_INCOMPATIBLE (whose "py_3.9" err_kind otherwise collapses to
  647. # err=99, losing the version). Cheap — no subprocess, just sys.version_info.
  648. metrics["sdk_hook_py"] = sys.version_info[0] * 100 + sys.version_info[1]
  649. pv = _plugin_version_int()
  650. if pv:
  651. metrics["pv"] = pv
  652. response: dict[str, object] = {"metrics": metrics}
  653. # One-time user-visible notice when the agentic reviewer is dead on
  654. # arrival. Uses hookSpecificOutput.additionalContext (SessionStart's
  655. # supported channel for surfacing text to both the model and the user)
  656. # plus systemMessage as a belt-and-suspenders. Marker-file-gated so
  657. # this fires exactly once per plugin version per install — see
  658. # _maybe_emit_user_notice.
  659. notice = _maybe_emit_user_notice(outcome, pv)
  660. if notice:
  661. response["hookSpecificOutput"] = {
  662. "hookEventName": "SessionStart",
  663. "additionalContext": notice,
  664. }
  665. response["systemMessage"] = notice
  666. print(json.dumps(response), flush=True)