diff options
| author | Mohamed Hegazy <8000722+mhegazy@users.noreply.github.com> | 2026-10-20 13:27:28 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-10-20 13:27:28 -0700 |
| commit | 7bcf8f3757d158e023b98c151d8091cbf130db91 (patch) | |
| tree | 39c6290b24268c366c2506cc66fb9a230b0185cd /plugins/security-guidance/hooks | |
| parent | 62bb122f3052d547bdc4e5e2d0e4029fdcb2e8e5 (diff) | |
| parent | 1eee6df1f032b8719fecdd987273d908d168f467 (diff) | |
Merge pull request #6812 from anthropics/mhegazy/sg-bootstrap-spawn-cwd
security-guidance: run SDK bootstrap subprocesses from the state dir
Diffstat (limited to 'plugins/security-guidance/hooks')
| -rw-r--r-- | plugins/security-guidance/hooks/_base.py | 11 | ||||
| -rw-r--r-- | plugins/security-guidance/hooks/ensure_agent_sdk.py | 30 |
2 files changed, 30 insertions, 11 deletions
diff --git a/plugins/security-guidance/hooks/_base.py b/plugins/security-guidance/hooks/_base.py index 1daa6642..eecae8b5 100644 --- a/plugins/security-guidance/hooks/_base.py +++ b/plugins/security-guidance/hooks/_base.py @@ -33,11 +33,12 @@ def state_dir(): """ explicit = os.environ.get("SECURITY_WARNINGS_STATE_DIR") if explicit: - return os.path.expanduser(explicit) - cc_config = os.environ.get("CLAUDE_CONFIG_DIR") - if cc_config: - return os.path.expanduser(os.path.join(cc_config, "security")) - return os.path.expanduser("~/.claude/security") + d = explicit + elif os.environ.get("CLAUDE_CONFIG_DIR"): + d = os.path.join(os.environ["CLAUDE_CONFIG_DIR"], "security") + else: + d = "~/.claude/security" + return os.path.abspath(os.path.expanduser(d)) # Debug log file. Lives under the plugin state dir (default ~/.claude/security/) diff --git a/plugins/security-guidance/hooks/ensure_agent_sdk.py b/plugins/security-guidance/hooks/ensure_agent_sdk.py index 059f3825..c1d59ba7 100644 --- a/plugins/security-guidance/hooks/ensure_agent_sdk.py +++ b/plugins/security-guidance/hooks/ensure_agent_sdk.py @@ -29,6 +29,24 @@ from pathlib import Path # main() and _maybe_emit_user_notice). from _base import portable_output, state_dir as _resolve_state_dir, write_hook_output + +def _spawn_cwd() -> str: + """Working directory for every Python this file starts. + + `python -c` and `python -m` put the working directory first on the import + path, and hooks run in the user's repository, so a repository file named + `claude_agent_sdk.py`, `venv.py` or `pip.py` would be imported in place + of the real module. The state dir is the plugin's own (the venv is built + there, so the interpreter can reach it); the plugin folder is the + fallback when it can't be created. + """ + d = _resolve_state_dir() + try: + os.makedirs(d, exist_ok=True) + return d + except OSError: + return str(Path(__file__).resolve().parent.parent) + # Outcome codes for the sdk_bootstrap metric. Values are stable for telemetry. NOOP_SYSTEM = 0 # claude_agent_sdk already importable in system python NOOP_VENV = 1 # venv already built and SDK imports from it @@ -311,7 +329,7 @@ def _probe_has_pip() -> bool: try: r = subprocess.run( [sys.executable, "-m", "pip", "--version"], - capture_output=True, timeout=10, + capture_output=True, timeout=10, cwd=_spawn_cwd(), ) return r.returncode == 0 except Exception: @@ -409,7 +427,7 @@ def _target_sdk_importable(state_dir) -> bool: [sys.executable, "-c", "import sys; sys.path.insert(0, sys.argv[1]); import claude_agent_sdk", str(target)], - capture_output=True, timeout=10, + capture_output=True, timeout=10, cwd=_spawn_cwd(), ) return r.returncode == 0 except Exception: @@ -434,7 +452,7 @@ def _build_via_target(state_dir) -> tuple[int, str, str]: "--target", str(target), "--upgrade", "--disable-pip-version-check", "--prefer-binary", "--no-cache-dir", "claude-agent-sdk"], - capture_output=True, timeout=120, check=True, + capture_output=True, timeout=120, check=True, cwd=_spawn_cwd(), ) return BUILT_TARGET, "", "" except subprocess.CalledProcessError as e: @@ -534,7 +552,7 @@ def main() -> tuple[int, str, str]: try: r = subprocess.run( [str(venv_py), "-c", "import claude_agent_sdk"], - capture_output=True, timeout=10, + capture_output=True, timeout=10, cwd=_spawn_cwd(), ) if r.returncode == 0: return NOOP_VENV, "", "" @@ -570,7 +588,7 @@ def main() -> tuple[int, str, str]: err_phase = "venv" subprocess.run( [sys.executable, "-m", "venv", "--clear", str(venv)], - capture_output=True, timeout=60, check=True, + capture_output=True, timeout=60, check=True, cwd=_spawn_cwd(), ) # Some machines route pip through a private registry; we # don't pass --index-url here so we inherit that default. Outside @@ -594,7 +612,7 @@ def main() -> tuple[int, str, str]: [str(venv_py), "-m", "pip", "install", "--quiet", "--disable-pip-version-check", "--prefer-binary", "--no-cache-dir", "claude-agent-sdk"], - capture_output=True, timeout=120, check=True, + capture_output=True, timeout=120, check=True, cwd=_spawn_cwd(), ) return BUILT, "", "" except subprocess.CalledProcessError as e: |
