Skip to content

The agent that grows with you

fix(honcho): surface the auth notice when session init itself fails

Erosika086dc8b88083
diff --git a/plugins/memory/honcho/__init__.py b/plugins/memory/honcho/__init__.py
index 3b0fe3caca1..7be1b0b3d83 100644
--- a/plugins/memory/honcho/__init__.py
+++ b/plugins/memory/honcho/__init__.py
@@ -292,6 +292,10 @@ class HonchoMemoryProvider(MemoryProvider):
self._init_lock = threading.Lock()
self._init_error = ""
+ # Init auth failures live here because the failed manager is discarded.
+ self._init_auth_failure: Optional[str] = None
+ self._init_auth_notice_emitted = False
+
# Cron and flush contexts disable the plugin entirely.
self._cron_skipped = False
@@ -445,11 +449,19 @@ class HonchoMemoryProvider(MemoryProvider):
init_session_id = self._lazy_init_session_id or "hermes-default"
def _run() -> None:
+ from plugins.memory.honcho.session import HonchoAuthError
+
try:
self._do_session_init(cfg, init_session_id, **init_kwargs)
self._lazy_init_kwargs = None
self._lazy_init_session_id = None
self._init_error = ""
+ self._clear_init_auth_failure()
+ except HonchoAuthError as e:
+ self._init_error = str(e)
+ self._record_init_auth_failure(e)
+ self._manager = None
+ logger.warning("Honcho background session init failed: authentication rejected")
except Exception as e:
self._init_error = str(e)
self._manager = None
@@ -563,6 +575,8 @@ class HonchoMemoryProvider(MemoryProvider):
if not self._config or self._lazy_init_kwargs is None:
return False
+ from plugins.memory.honcho.session import HonchoAuthError
+
try:
self._do_session_init(
self._config,
@@ -572,13 +586,29 @@ class HonchoMemoryProvider(MemoryProvider):
# Clear lazy refs
self._lazy_init_kwargs = None
self._lazy_init_session_id = None
+ self._clear_init_auth_failure()
return self._manager is not None
+ except HonchoAuthError as e:
+ self._record_init_auth_failure(e)
+ self._manager = None
+ self._session_initialized = False
+ logger.warning("Honcho lazy session init failed: authentication rejected")
+ return False
except Exception as e:
self._manager = None
self._session_initialized = False
logger.warning("Honcho lazy session init failed: %s", e)
return False
+ def _record_init_auth_failure(self, exc: BaseException) -> None:
+ """Keep the auth detail so the one-time notice survives the manager discard."""
+ self._init_auth_failure = str(exc)
+
+ def _clear_init_auth_failure(self) -> None:
+ if self._init_auth_failure is not None:
+ self._init_auth_failure = None
+ self._init_auth_notice_emitted = False
+
def _session_ready(self) -> bool:
"""Return whether a manager/session key can be used safely.
@@ -701,7 +731,8 @@ class HonchoMemoryProvider(MemoryProvider):
timeout=max(0.0, first_turn_base_deadline - time.monotonic())
)
if not self._session_ready():
- return ""
+ # A failed auth init still owes the user the one-time notice.
+ return self._pop_auth_notice()
# First-turn mode suppresses only the base layer; dialectic is independent.
_skip_base = (
@@ -852,15 +883,19 @@ class HonchoMemoryProvider(MemoryProvider):
def _pop_auth_notice(self) -> str:
"""One-time model-facing notice that Honcho auth expired and memory is paused."""
+ msg = None
pop = getattr(self._manager, "pop_auth_notice", None)
- if not callable(pop):
- return ""
- try:
- msg = pop()
- except Exception:
- return ""
+ if callable(pop):
+ try:
+ msg = pop()
+ except Exception:
+ msg = None
if not isinstance(msg, str) or not msg:
- return ""
+ # Init failures discard the manager; the provider kept the detail.
+ if self._init_auth_failure is None or self._init_auth_notice_emitted:
+ return ""
+ self._init_auth_notice_emitted = True
+ msg = self._init_auth_failure
return (
"[Honcho memory status] Authentication with the Honcho memory backend "
"has expired and automatic token refresh failed, so memory sync and "
@@ -1460,6 +1495,10 @@ class HonchoMemoryProvider(MemoryProvider):
if self._init_thread and self._init_thread.is_alive():
return tool_error("Honcho session is still initializing; try again shortly.")
if not self._ensure_session():
+ if self._init_auth_failure:
+ return tool_error(
+ f"Honcho memory authentication failed: {self._init_auth_failure}"
+ )
return tool_error("Honcho session could not be initialized.")
if not self._manager or not self._session_key:
diff --git a/tests/honcho_plugin/test_auth_recovery.py b/tests/honcho_plugin/test_auth_recovery.py
index de751c0059b..9f0903a9162 100644
--- a/tests/honcho_plugin/test_auth_recovery.py
+++ b/tests/honcho_plugin/test_auth_recovery.py
@@ -828,3 +828,160 @@ class TestToolAuthVisibility:
out = self._provider(_Mgr()).handle_tool_call("honcho_profile", {})
assert "No profile facts" not in out
assert "authentication failed" in out
+
+
+# ---------------------------------------------------------------------------
+# initialization-time auth failures: the notice must survive the manager discard
+# ---------------------------------------------------------------------------
+
+
+def _healthy_client():
+ """A mock SDK client whose peers and sessions behave like an empty backend."""
+ client = MagicMock()
+ peer = MagicMock()
+ peer.chat.return_value = ""
+ peer.get_card.return_value = None
+ peer.context.return_value = SimpleNamespace(representation="", peer_card=[])
+ client.peer.return_value = peer
+ sdk_session = MagicMock()
+ sdk_session.context.return_value = SimpleNamespace(summary=None, messages=[])
+ client.session.return_value = sdk_session
+ return client
+
+
+def _wire_init(tmp_path, monkeypatch, client, *, recall_mode="hybrid", dead_refresh=True):
+ """Route provider initialization through a real manager backed by ``client``."""
+ from plugins.memory.honcho import client as client_mod
+ from plugins.memory.honcho import session as session_mod
+
+ path = tmp_path / "honcho.json"
+ _write(path, {"hosts": {"hermes": _host_block(expires_at=time.time() + 3600)}})
+ monkeypatch.setattr(client_mod, "resolve_config_path", lambda: path)
+ monkeypatch.setattr(client_mod, "get_honcho_client", lambda *a, **k: client)
+ monkeypatch.setattr(session_mod, "get_honcho_client", lambda *a, **k: client)
+ if dead_refresh:
+ monkeypatch.setattr(oauth, "force_refresh_token", lambda p, h: None)
+ cfg = HonchoClientConfig(
+ host="hermes", api_key="hch-at-old", enabled=True, recall_mode=recall_mode,
+ timeout=0.5, session_strategy="per-session",
+ )
+ monkeypatch.setattr(
+ client_mod.HonchoClientConfig, "from_global_config", lambda *a, **k: cfg
+ )
+ return path
+
+
+def _initialized_provider():
+ provider = HonchoMemoryProvider()
+ provider.initialize(session_id="init-auth-session")
+ if provider._init_thread:
+ provider._init_thread.join(timeout=5)
+ return provider
+
+
+class TestInitAuthFailureNotice:
+ def test_peer_setup_401_in_hybrid_mode_produces_notice(self, tmp_path, monkeypatch):
+ client = MagicMock()
+ client.peer.side_effect = Exception("HTTP 401 Unauthorized")
+ _wire_init(tmp_path, monkeypatch, client)
+ provider = _initialized_provider()
+
+ assert provider._manager is None
+ notice = provider.prefetch("what did we decide about the schema?")
+ assert "hermes honcho setup" in notice
+ assert "paused" in notice
+
+ def test_notice_is_emitted_exactly_once(self, tmp_path, monkeypatch):
+ client = MagicMock()
+ client.peer.side_effect = Exception("HTTP 401 Unauthorized")
+ _wire_init(tmp_path, monkeypatch, client)
+ provider = _initialized_provider()
+
+ assert "hermes honcho setup" in provider.prefetch("first question")
+ # Retries keep failing, but the same episode never re-arms the notice.
+ for query in ("second question", "third question"):
+ assert provider.prefetch(query) == ""
+
+ def test_dead_grant_during_session_setup_produces_notice(self, tmp_path, monkeypatch):
+ client = _healthy_client()
+ env = {}
+
+ def _session_dies(*a, **k):
+ path = env["path"]
+ block = json.loads(path.read_text())["hosts"]["hermes"]
+ cred = oauth.OAuthCredential.from_host_block(block)
+ oauth._mark_grant_dead((str(path), "hermes"), cred)
+ raise Exception("Invalid or expired access token")
+
+ client.session.side_effect = _session_dies
+ env["path"] = _wire_init(tmp_path, monkeypatch, client, dead_refresh=False)
+ provider = _initialized_provider()
+
+ assert provider._manager is None
+ assert client.peer.called # failure hit session setup, not peer setup
+ notice = provider.prefetch("what happened before the grant died?")
+ assert "hermes honcho setup" in notice
+
+ def test_tools_lazy_init_reports_auth_error(self, tmp_path, monkeypatch):
+ client = MagicMock()
+ client.peer.side_effect = Exception("Invalid or expired access token")
+ _wire_init(tmp_path, monkeypatch, client, recall_mode="tools")
+ provider = _initialized_provider()
+
+ out = provider.handle_tool_call("honcho_profile", {})
+ assert "authentication failed" in out
+ assert "could not be initialized" not in out
+
+ def test_relogin_resumes_init_and_clears_failure(self, tmp_path, monkeypatch):
+ client = _healthy_client()
+ client.peer.side_effect = Exception("HTTP 401 Unauthorized")
+ path = _wire_init(tmp_path, monkeypatch, client)
+ provider = _initialized_provider()
+ assert "hermes honcho setup" in provider.prefetch("first question")
+
+ _relogin(path)
+ client.peer.side_effect = None
+ provider.prefetch("after re-login")
+ if provider._init_thread:
+ provider._init_thread.join(timeout=5)
+
+ assert provider._session_initialized is True
+ assert provider._manager is not None
+ assert provider._init_auth_failure is None
+
+ def test_tools_relogin_resumes_without_restart(self, tmp_path, monkeypatch):
+ client = _healthy_client()
+ client.peer.side_effect = Exception("Invalid or expired access token")
+ path = _wire_init(tmp_path, monkeypatch, client, recall_mode="tools")
+ provider = _initialized_provider()
+ assert "authentication failed" in provider.handle_tool_call("honcho_profile", {})
+
+ _relogin(path)
+ client.peer.side_effect = None
+ out = provider.handle_tool_call("honcho_profile", {})
+
+ assert "authentication failed" not in out
+ assert provider._session_initialized is True
+ assert provider._init_auth_failure is None
+
+ def test_non_auth_init_timeout_fails_open_without_notice(self, tmp_path, monkeypatch):
+ client = MagicMock()
+ client.peer.side_effect = TimeoutError("request timed out")
+ _wire_init(tmp_path, monkeypatch, client, dead_refresh=False)
+ reauths = []
+ monkeypatch.setattr(oauth, "force_refresh_token", lambda p, h: reauths.append(1))
+ provider = _initialized_provider()
+
+ assert provider._manager is None
+ assert provider._init_auth_failure is None
+ assert provider.prefetch("a real question") == ""
+ assert reauths == []
+
+ def test_non_auth_tools_init_failure_keeps_generic_error(self, tmp_path, monkeypatch):
+ client = MagicMock()
+ client.peer.side_effect = TimeoutError("request timed out")
+ _wire_init(tmp_path, monkeypatch, client, recall_mode="tools", dead_refresh=False)
+ provider = _initialized_provider()
+
+ out = provider.handle_tool_call("honcho_profile", {})
+ assert "could not be initialized" in out