security-guidance: agent-SDK venv never rebuilds after an interpreter change — find_spec staleness check cannot see ABI breaks in compiled deps
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
- On a machine where
sg-python.shPass 2 resolvespython3to 3.14, let SessionStart build the venv. It getslib/python3.14/site-packageswith_pydantic_core.cpython-314-darwin.so. - Install
[email protected](or any version in the Pass 1 list) for unrelated reasons.sg-python.shnow execs 3.11. - Every Stop/commit review logs, once per turn:
agentic_review: SDK unavailable (No module named 'pydantic_core._pydantic_core'); falling back- Restarting sessions never fixes it.
ensure_agent_sdk.pyreturnsNOOP_VENVbecausefind_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)
- Pin the venv to its interpreter. Record the building interpreter's
X.Y(orsys.implementation.cache_tag) in a sidecar next to the venv, and rebuild with--clearwhen the resolved interpreter no longer matches. Cheap — no import needed, just a string compare, so thefind_specfast path is preserved. - 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.shPass 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
- security-guidance 2.0.7
- macOS 26.x (Darwin 25.6.0), arm64
- Homebrew
[email protected]/[email protected]present; venv built by 3.14, hooks resolving to 3.11
Source: anthropics/claude-plugins-official