The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(honcho): stop classifying bare '401' digits as auth errors; redact session-side auth logs
Erosikab1414baa0954
diff --git a/plugins/memory/honcho/session.py b/plugins/memory/honcho/session.pyindex 367b6e58f15..8e8db7e3119 100644--- a/plugins/memory/honcho/session.py+++ b/plugins/memory/honcho/session.py@@ -12,6 +12,7 @@ from datetime import datetimefrom typing import Any, TYPE_CHECKINGfrom plugins.memory.honcho.client import get_honcho_client+from plugins.memory.honcho.oauth import _redact_tokensif TYPE_CHECKING:from honcho import Honcho@@ -32,26 +33,38 @@ class HonchoAuthError(RuntimeError):"""-# The SDK raises different exception classes per transport, but an auth failure always carries one of these markers.+# Text fallback for transports without a status attribute. Conservative on+# purpose: a false positive spends a refresh-token rotation, and a lost+# rotation response can permanently revoke the grant. A missed auth error+# only costs one un-recovered call. "authentication failed" (not bare+# "authentication") so auth-infrastructure outage messages stay transient._AUTH_ERROR_MARKERS = ("invalid or expired access token",- "authentication",+ "authentication failed","unauthorized",- "401",)+# A 401 in text counts only with HTTP context ("HTTP 401", "status 401"), never as a bare number.+_HTTP_401_RE = re.compile(r"\b(?:http|status(?:[ _]code)?\s*[:=]?)\s*401\b")+def _is_auth_error(exc: BaseException) -> bool:status = getattr(exc, "status_code", None) or getattr(exc, "status", None)if status == 401:return True+ # The transport reported a concrete non-auth status; trust it over text.+ if isinstance(status, int) and status not in (0, 401):+ return Falsetext = str(exc).lower()+ if _HTTP_401_RE.search(text):+ return Truereturn any(marker in text for marker in _AUTH_ERROR_MARKERS)def _auth_error_message(exc: BaseException) -> str:return (- f"Honcho rejected our credentials and a forced token refresh did not recover: {exc}. "+ "Honcho rejected our credentials and a forced token refresh did not "+ f"recover: {_redact_tokens(str(exc))}. ""Re-authenticate with 'hermes honcho setup'.")@@ -204,13 +217,14 @@ class HonchoSessionManager:return self._honchodef _record_auth_failure(self, exc: BaseException) -> None:+ detail = _redact_tokens(str(exc))if self._auth_failure is None:logger.error("Honcho authentication failed and token refresh did not recover; ""memory sync and recall are paused until the user re-authenticates: %s",- exc,+ detail,)- self._auth_failure = str(exc)+ self._auth_failure = detaildef _clear_auth_failure(self) -> None:if self._auth_failure is not None:@@ -566,7 +580,7 @@ class HonchoSessionManager:raiselogger.warning("Honcho message sync hit an auth error; forcing token "- "refresh and retrying once: %s", e,+ "refresh and retrying once: %s", _redact_tokens(str(e)),)if not self._force_reauth():raise@@ -820,7 +834,7 @@ class HonchoSessionManager:raiselogger.warning("Honcho dialectic query hit an auth error; forcing token "- "refresh and retrying once: %s", e,+ "refresh and retrying once: %s", _redact_tokens(str(e)),)if not self._force_reauth():self._record_auth_failure(e)diff --git a/tests/honcho_plugin/test_auth_recovery.py b/tests/honcho_plugin/test_auth_recovery.pyindex ef26f1e6552..c9b147e9bec 100644--- a/tests/honcho_plugin/test_auth_recovery.py+++ b/tests/honcho_plugin/test_auth_recovery.py@@ -222,6 +222,32 @@ class TestAuthErrorDetection:assert not _is_auth_error(Exception("connection reset by peer"))assert not _is_auth_error(Exception("HTTP 500 internal error"))+ def test_bare_401_digits_are_not_auth_errors(self):+ """A false positive spends a token rotation and can revoke the grant;+ digits appearing in latency figures, request ids, or identifiers must+ never classify as auth failures."""+ for msg in (+ "Rate limited, retry after 4010 ms",+ "500 Internal Server Error (request id req-4012ab)",+ "connection timeout to workspace ws-401-prod",+ "peer 401k-planning not found",+ ):+ assert not _is_auth_error(Exception(msg)), msg++ def test_401_with_http_context_matches(self):+ assert _is_auth_error(Exception("HTTP 401"))+ assert _is_auth_error(Exception("status 401"))+ assert _is_auth_error(Exception("status_code: 401"))+ assert _is_auth_error(Exception("401 Unauthorized"))++ def test_concrete_non_auth_status_wins_over_text(self):+ exc = Exception("authentication failed")+ exc.status = 429+ assert not _is_auth_error(exc)++ def test_bare_authentication_word_is_not_enough(self):+ assert not _is_auth_error(Exception("authentication service unreachable"))+# ---------------------------------------------------------------------------# session: dialectic 401 recovery@@ -495,6 +521,13 @@ class TestAuthNotice:mgr.dialectic_query("k", "q")assert mgr.pop_auth_notice() is None+ def test_recorded_failure_and_notice_redact_token_values(self):+ mgr = _make_manager(_FlakyPeer(failures=0))+ mgr._record_auth_failure(Exception("rejected token hch-at-secretvalue99"))+ notice = mgr.pop_auth_notice()+ assert "secretvalue99" not in notice+ assert "hch-at-[redacted]" in notice+def test_provider_prefetch_injects_notice_once(self):class _FakeManager:def __init__(self):