security-guidance: agent-SDK venv never rebuilds after an interpreter change — find_spec staleness check cannot see ABI breaks in compiled deps

Author: DanielLandiCreated Aug 29, 2026Updated Sep 27, 2026

Summary

ensure_agent_sdk.py decides whether to rebuild the agent-SDK venv with a find_spec check on claude_agent_sdk. That package is a pure-Python directory, so the spec lookup succeeds whenever the directory exists — regardless of whether the venv's compiled transitive dependencies can actually be imported by the current hook interpreter.

The result is a venv that is permanently broken and permanently reported as healthy. The agentic review degrades to regex-only for months, and sdk_bootstrap telemetry reports NOOP_VENV (1) the whole time.

Why the ABI matters here

llm.py::_inject_agent_sdk_venv_into_syspath does not execute the venv's own interpreter. It globs agent-sdk-venv/lib/python*/site-packages and injects it into the hook interpreter's sys.path. So the venv's wheels must match the ABI of whichever interpreter sg-python.sh selected — and sg-python.sh selects by probing python3.13 → 3.12 → 3.11 → 3.10, which changes as the user's Homebrew/pyenv Pythons change.

Nothing re-pins the venv when that selection moves.

Reproduction

  1. On a machine where sg-python.sh Pass 2 resolves python3 to 3.14, let SessionStart build the venv. It gets lib/python3.14/site-packages with _pydantic_core.cpython-314-darwin.so.
  2. Install [email protected] (or any version in the Pass 1 list) for unrelated reasons. sg-python.sh now execs 3.11.
  3. Every Stop/commit review logs, once per turn:
agentic_review: SDK unavailable (No module named 'pydantic_core._pydantic_core'); falling back
  1. Restarting sessions never fixes it. ensure_agent_sdk.py returns NOOP_VENV because find_spec("claude_agent_sdk") still resolves against the 3.14 tree.

Observed in the wild: a venv built 2026-05-30 under 3.14, still being injected into a 3.11 hook interpreter on 2026-08-29 — 159 SDK unavailable lines in a single day's ~/.claude/security/log.txt, and sdk_bootstrap: 1 reported on every SessionStart throughout.

Root cause

The staleness check tests the wrong thing. find_spec on a pure-Python package cannot observe an ABI break in a compiled dependency (pydantic_core, cryptography, rpds-py). _sdk_on_syspath() is documented as using find_spec deliberately, to avoid the ~800ms cost of a real import — but that trade buys a check that cannot detect the dominant failure mode.

Suggested fixes (either would close it)

  1. Pin the venv to its interpreter. Record the building interpreter's X.Y (or sys.implementation.cache_tag) in a sidecar next to the venv, and rebuild with --clear when the resolved interpreter no longer matches. Cheap — no import needed, just a string compare, so the find_spec fast path is preserved.
  2. Probe a compiled dep instead of a pure-Python one. find_spec("pydantic_core._pydantic_core") is still ~10ms and does fail on ABI mismatch.

Option 1 is more robust: it catches any compiled dep, present or future.

Worth noting the venv directory name already encodes the answer — lib/python3.14/ vs. a 3.11 hook interpreter is a one-line mismatch check against the same glob llm.py already performs.

Related

  • #4907 — sg-python.sh Pass 1 never discovers 3.14+. Same underlying fragility: the interpreter that builds the venv and the interpreter that later imports it are chosen by different rules and can drift apart.
  • #2071 — earlier interpreter-selection breakage of the same review path.

Environment

Source: anthropics/claude-plugins-official