Skip to content

The agent that grows with you

refactor(honcho): merge pre-build and cached OAuth refresh hooks into one _refresh_oauth

Teknium4b4502a42956
diff --git a/plugins/memory/honcho/client.py b/plugins/memory/honcho/client.py
index f9ff5ab04af..a486ac6ffaf 100644
--- a/plugins/memory/honcho/client.py
+++ b/plugins/memory/honcho/client.py
@@ -26,8 +26,8 @@ from hermes_constants import get_hermes_home
# Cache/slot cluster lives in client_cache.py; re-exported because tests and reset
# bookkeeping reach these names through this module.
from plugins.memory.honcho.client_cache import ( # noqa: F401
- _DEFAULT_HTTP_TIMEOUT, _apply_fresh_oauth_token, _client_cache_key, _client_slots, _client_slots_lock,
- _credential_fingerprint, _honcho_json_timeout_memo, _refresh_cached_oauth, _slot_for,
+ _DEFAULT_HTTP_TIMEOUT, _client_cache_key, _client_slots, _client_slots_lock, _credential_fingerprint,
+ _honcho_json_timeout_memo, _refresh_oauth, _slot_for,
)
if TYPE_CHECKING:
@@ -508,7 +508,7 @@ def get_honcho_client(config: HonchoClientConfig | None = None) -> Honcho:
slot = _slot_for(key)
cached = slot.peek()
if cached is not None:
- _refresh_cached_oauth(cached, config, slot)
+ _refresh_oauth(config, cached, slot)
refreshed = slot.peek()
if refreshed is not None:
return refreshed
@@ -518,7 +518,7 @@ def get_honcho_client(config: HonchoClientConfig | None = None) -> Honcho:
config = HonchoClientConfig.from_global_config()
# Start with a live access token rather than 401ing an hour in.
- _apply_fresh_oauth_token(config)
+ _refresh_oauth(config)
if not config.api_key and not config.base_url:
raise ValueError("Honcho API key not found. Get your API key at https://app.honcho.dev, "
diff --git a/plugins/memory/honcho/client_cache.py b/plugins/memory/honcho/client_cache.py
index c8c9169a7de..223bc6f8487 100644
--- a/plugins/memory/honcho/client_cache.py
+++ b/plugins/memory/honcho/client_cache.py
@@ -169,22 +169,13 @@ def _resolve_timeout_from_sources(config: HonchoClientConfig | None) -> float:
return timeout if timeout is not None else _DEFAULT_HTTP_TIMEOUT
-def _apply_fresh_oauth_token(config: HonchoClientConfig) -> None:
- """Refresh a near-expiry OAuth grant and point ``config.api_key`` at it. No-op for static
- keys or on failure (the first 401 triggers session.py's forced rotation). Refreshes against
- the config's BOUND path: the ambient resolver on daemon threads lands on the default profile."""
- try:
- from plugins.memory.honcho import oauth
- token, _ = oauth.ensure_fresh_token(config.bound_config_path(), config.host)
- if token:
- config.api_key = token
- except Exception:
- logger.warning("Honcho OAuth pre-build refresh failed", exc_info=True)
-
-
-def _refresh_cached_oauth(client: Honcho, config: HonchoClientConfig | None, slot: SingletonSlot | None = None) -> None:
- """Rotate the cached client's Bearer in place when its OAuth token is stale. If the in-place
- rotation can't apply (SDK shape change), the slot is reset so the next acquisition rebuilds."""
+def _refresh_oauth(config: HonchoClientConfig | None, client: Honcho | None = None, slot: SingletonSlot | None = None) -> None:
+ """Refresh a near-expiry OAuth grant. Pre-build (``client=None``): point ``config.api_key`` at the
+ fresh token so a new client doesn't 401 an hour in. Cached (``client`` given): rotate its Bearer in
+ place; if the in-place rotation can't apply (SDK shape change) reset ``slot`` so the next acquisition
+ rebuilds. No-op for static keys or on failure (the first 401 triggers session.py's forced rotation).
+ Refreshes against the config's BOUND path: the ambient resolver on daemon threads lands on the
+ default profile."""
from plugins.memory.honcho.client import resolve_active_host, resolve_config_path
try:
@@ -194,7 +185,10 @@ def _refresh_cached_oauth(client: Honcho, config: HonchoClientConfig | None, slo
else:
host, path = resolve_active_host(), resolve_config_path()
token, refreshed = oauth.ensure_fresh_token(path, host)
- if refreshed and token and not oauth.apply_token_to_client(client, token) and slot is not None:
+ if client is None:
+ if token:
+ config.api_key = token
+ elif refreshed and token and not oauth.apply_token_to_client(client, token) and slot is not None:
slot.reset()
except Exception:
- logger.warning("Honcho OAuth cached refresh failed", exc_info=True)
+ logger.warning("Honcho OAuth %s refresh failed", "pre-build" if client is None else "cached", exc_info=True)