summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/_base.py
diff options
context:
space:
mode:
authorMohamed Hegazy <mhegazy@anthropic.com>2026-10-27 20:03:52 +0000
committerClaude <noreply@anthropic.com>2026-10-27 20:03:52 +0000
commit3654d346b45836e43e013b849be19233ff0efbc3 (patch)
tree080cdfdff1e1f3df67ca9517251bbe8052890724 /plugins/security-guidance/hooks/_base.py
parent45bb3d5c14f7c20c9bdc9a30e798cf0d77467e2e (diff)
parentf8c5e7a1dcb71b10cb74586224b5401f3de5db28 (diff)
Merge remote-tracking branch 'origin/main' into mhegazy/sg-secret-rename-pairingmhegazy/sg-secret-rename-pairing
# Conflicts: # plugins/security-guidance/hooks/diffstate.py
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"}