summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/_base.py
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-27 21:08:09 +0000
committerClaude <noreply@anthropic.com>2026-10-27 21:08:09 +0000
commit06d1f63e308370e0df03e9fc323f42ae1c4f5a06 (patch)
tree883f8ada4ae85f62c61ad97826f937455fc07f0b /plugins/security-guidance/hooks/_base.py
parent89a2c8b4356767441800d8b6799bd3ee11e63a51 (diff)
parentf8c5e7a1dcb71b10cb74586224b5401f3de5db28 (diff)
Merge branch 'main' into mhegazy/sg-review-sessions-not-savedmhegazy/sg-review-sessions-not-saved
Diffstat (limited to 'plugins/security-guidance/hooks/_base.py')
-rw-r--r--plugins/security-guidance/hooks/_base.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/plugins/security-guidance/hooks/_base.py b/plugins/security-guidance/hooks/_base.py
index e5959e68..28b189a4 100644
--- a/plugins/security-guidance/hooks/_base.py
+++ b/plugins/security-guidance/hooks/_base.py
@@ -212,6 +212,7 @@ _USAGE = {
# reported it manually.
"http_err_last": 0, # most recent HTTP error code this invocation
"http_err_count": 0, # total HTTP errors (4xx + 5xx + network)
+ "net_err": 0, # why the last call got no HTTP response, 0 if it got one
# For the usage log line only: model ids as recorded (the requested alias
# on the Agent SDK path), and whether any call had no price-table entry.
"models": [],
@@ -300,6 +301,33 @@ def _record_http_error(status):
_USAGE["http_err_count"] += 1
+def _record_net_error(code):
+ """Record why an LLM API call got no HTTP response: the packed code from
+ llm._net_error_code. `api_error` -1 cannot tell a certificate failure
+ from a refused connection or a timeout, and a failure the HTTP client
+ raises outside its own error type (a default TLS context that cannot be
+ built, a connection closed before the reply) has no `api_error` at all.
+ Emitted as `net_err` on the row of a hook whose last call failed this
+ way; a later call clears it."""
+ try:
+ c = int(code)
+ except (TypeError, ValueError):
+ return
+ if c > 0:
+ with _USAGE_LOCK:
+ _USAGE["net_err"] = c
+
+
+def _clear_net_error():
+ with _USAGE_LOCK:
+ _USAGE["net_err"] = 0
+
+
+def _net_error_metric():
+ with _USAGE_LOCK:
+ return _USAGE.get("net_err", 0)
+
+
# Set by main() and the review handlers: which review this invocation runs
# and for which Claude Code session, so the usage log line can be attributed.
_USAGE_CONTEXT = {"session_id": "", "review": "stop"}