diff options
| author | Mohamed Hegazy <mhegazy@anthropic.com> | 2026-10-15 22:47:24 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-10-19 16:29:25 +0000 |
| commit | d2919136660832d06d5260677b256033c7dbdbda (patch) | |
| tree | 50e6ee063d4402b7117366e2f12ac6fadc435bb6 /plugins/security-guidance/hooks/sg-python.sh | |
| parent | ea10b213686c90bb1984b2016937310e05336cf3 (diff) | |
security-guidance: report how long the launcher waits for interpreter probes
sg-python.sh asks each candidate interpreter for its version before running a
hook, and waits as long as that takes. Nothing recorded how long.
The launcher now times each probe with bash builtins (no extra process) and
passes the result to the hook, which reports two keys when the probes of one
launch took a second or more together:
- launcher_probe_ms: all probes of this launch
- launcher_probe_max_ms: the slowest one
The keys go last on a row, so they never displace an existing key. Measurement
only: the launcher still waits for every probe and picks the same interpreter.
Diffstat (limited to 'plugins/security-guidance/hooks/sg-python.sh')
| -rwxr-xr-x | plugins/security-guidance/hooks/sg-python.sh | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/plugins/security-guidance/hooks/sg-python.sh b/plugins/security-guidance/hooks/sg-python.sh index f1b9d68c..97387901 100755 --- a/plugins/security-guidance/hooks/sg-python.sh +++ b/plugins/security-guidance/hooks/sg-python.sh @@ -106,7 +106,7 @@ probe() { exec "$@" -c 'import os, sys; t = (sys.argv[1:] or [""])[0]; sys.exit(3) if t and not (os.path.isfile(t) and os.access(t, os.R_OK)) else print(f"{sys.version_info[0]}.{sys.version_info[1]}")' "$SG_TARGET" 2>/dev/null } -# Use as `v=$(probe ...) || { note_unreadable "<name>"; continue; }`. +# Use as `ask ... || { note_unreadable "<name>"; continue; }` (see ask below). note_unreadable() { [ $? -eq 3 ] && sg_unreadable="$sg_unreadable|$1|" return 0 @@ -119,6 +119,49 @@ on_path() { command -v "$1" >/dev/null 2>&1 } +# How long the probes took, for the launcher_probe_max_ms / launcher_probe_ms +# metrics the hook reports. Measurement only: `ask` is `v=$(probe ...)` with a +# clock around it, and changes neither which interpreter runs nor how long a +# probe may take. EPOCHREALTIME needs bash 5; older bash (macOS /bin/bash) only +# has whole seconds, where a difference of 1 can be a few milliseconds: there +# a single probe counts from 2 s, and the total is first probe to last. +unset SG_LAUNCHER_PROBE_MS SG_LAUNCHER_PROBE_MAX_MS +sg_probe_ms=0 +sg_probe_max_ms=0 +sg_probe_first="" +sg_coarse="" +sg_clock() { + # Sets $sg_now (ms) without starting a process. + case "${EPOCHREALTIME:-}" in + [0-9]*[.,][0-9][0-9][0-9][0-9][0-9][0-9]) # the decimal point follows the locale + sg_now=${EPOCHREALTIME//[!0-9]/} + sg_now=$((10#$sg_now / 1000)) ;; + *) + sg_coarse=1 + sg_now=$((SECONDS * 1000)) ;; + esac +} +ask() { + # Sets $v; same exit status as probe. + local rc=0 t0 ms + sg_clock; t0=$sg_now + [ -n "$sg_probe_first" ] || sg_probe_first=$t0 + v=$(probe "$@") || rc=$? + sg_clock; ms=$((sg_now - t0)) + if [ -n "$sg_coarse" ]; then + [ "$ms" -ge 2000 ] || ms=0 + sg_probe_ms=$((sg_now - sg_probe_first)) + [ "$sg_probe_ms" -ge 2000 ] || sg_probe_ms=0 + elif [ "$ms" -gt 0 ]; then + sg_probe_ms=$((sg_probe_ms + ms)) + fi + [ "$ms" -le "$sg_probe_max_ms" ] || sg_probe_max_ms=$ms + if [ "$sg_probe_ms" -gt 0 ]; then + export SG_LAUNCHER_PROBE_MS=$sg_probe_ms SG_LAUNCHER_PROBE_MAX_MS=$sg_probe_max_ms + fi + return $rc +} + # True iff arg is a "M.m" version string >= 3.10. claude_agent_sdk requires # Python >= 3.10; below that, pip install fails ("No matching distribution") # and the LLM-powered review (Stop / commit / push) silently no-ops while @@ -138,7 +181,7 @@ is_sdk_compatible() { # available wins; the user doesn't have to PATH-prefer it. for cmd in "python3.13" "python3.12" "python3.11" "python3.10"; do on_path "$cmd" || continue - v=$(probe "$cmd") || { note_unreadable "$cmd"; continue; } + ask "$cmd" || { note_unreadable "$cmd"; continue; } v=${v%$'\r'} if is_sdk_compatible "$v"; then exec "$cmd" "$@" @@ -153,7 +196,7 @@ for cmd in "python3" "python" "py -3"; do # shellcheck disable=SC2086 on_path $cmd || continue # shellcheck disable=SC2086 - v=$(probe $cmd) || { note_unreadable "$cmd"; continue; } + ask $cmd || { note_unreadable "$cmd"; continue; } v=${v%$'\r'} if is_sdk_compatible "$v"; then # shellcheck disable=SC2086 @@ -212,7 +255,7 @@ sg_tried="|" for cmd in "${sg_candidates[@]}"; do case "$sg_tried" in *"|$cmd|"*) continue ;; esac sg_tried="$sg_tried$cmd|" - v=$(probe "$cmd") || { note_unreadable "$cmd"; continue; } + ask "$cmd" || { note_unreadable "$cmd"; continue; } v=${v%$'\r'} if is_sdk_compatible "$v"; then exec "$cmd" "$@" |
