Skip to content

The agent that grows with you

fix(honcho): bind config provenance so background threads stop resolving the wrong profile

Erosika8b7d0ed4c7df
diff --git a/plugins/memory/honcho/client.py b/plugins/memory/honcho/client.py
index fd2e593faf1..3b52ae7aeaa 100644
--- a/plugins/memory/honcho/client.py
+++ b/plugins/memory/honcho/client.py
@@ -461,6 +461,24 @@ class HonchoClientConfig:
# block exists or enabled was set explicitly), vs auto-enabled from a
# stray HONCHO_API_KEY env var.
explicitly_configured: bool = False
+ # Provenance: WHERE this config was resolved from, captured at resolution
+ # time (inside the caller's profile scope). Bound consumers (session
+ # manager, OAuth refresh paths) use these instead of re-resolving
+ # resolve_config_path()/get_hermes_home() later — those resolvers read a
+ # ContextVar that background threads cannot see, so re-resolution from a
+ # daemon thread silently lands on the DEFAULT profile (#69123, #74065).
+ config_path: Path | None = None
+ hermes_home: Path | None = None
+
+ def bound_config_path(self) -> Path:
+ """Return the config path this config was resolved from.
+
+ Falls back to ambient resolution only for hand-constructed configs
+ (tests, env-only setups) that carry no provenance.
+ """
+ if self.config_path is not None:
+ return self.config_path
+ return resolve_config_path()
@classmethod
def from_env(
@@ -482,6 +500,7 @@ class HonchoClientConfig:
timeout=timeout,
ai_peer=resolved_host,
enabled=bool(api_key or base_url),
+ hermes_home=get_hermes_home(),
)
@classmethod
@@ -733,6 +752,8 @@ class HonchoClientConfig:
sessions=raw.get("sessions", {}),
raw=raw,
explicitly_configured=_explicitly_configured,
+ config_path=path,
+ hermes_home=get_hermes_home(),
)
@staticmethod
@@ -857,13 +878,16 @@ class HonchoClientConfig:
_honcho_client_slot: SingletonSlot = SingletonSlot()
_cached_timeout: float | None = None
-# Memo for the honcho.json-derived timeout, keyed on the file's mtime_ns so
-# the staleness check on every get_honcho_client() call costs one stat()
-# instead of a JSON parse. mtime -1 = file absent; (None, None) = not yet
-# populated. config.yaml needs no such memo: load_config_readonly() is
-# internally cached on both the user and managed files' signatures, and a
-# bespoke key here would have to duplicate that invalidation logic.
-_honcho_json_timeout_memo: tuple[int | None, float | None] = (None, None)
+# Memo for the honcho.json-derived timeout, keyed PER CONFIG PATH on the
+# file's mtime_ns so the staleness check on every get_honcho_client() call
+# costs one stat() instead of a JSON parse. Path-keyed because multi-profile
+# processes resolve different honcho.json files — a single-slot memo would
+# thrash between profiles and return profile A's timeout for profile B.
+# mtime -1 = file absent. config.yaml needs no such memo:
+# load_config_readonly() is internally cached on both the user and managed
+# files' signatures, and a bespoke key here would have to duplicate that
+# invalidation logic.
+_honcho_json_timeout_memo: dict[str, tuple[int, float | None]] = {}
def _config_yaml_timeout() -> float | None:
@@ -884,15 +908,16 @@ def _config_yaml_timeout() -> float | None:
def _honcho_json_timeout() -> float | None:
"""Read timeout/requestTimeout from honcho.json (host block wins), memoized on mtime."""
- global _honcho_json_timeout_memo
try:
path = resolve_config_path()
+ path_key = str(path)
try:
mtime_ns: int = path.stat().st_mtime_ns
except OSError:
mtime_ns = -1
- if _honcho_json_timeout_memo[0] == mtime_ns:
- return _honcho_json_timeout_memo[1]
+ memo = _honcho_json_timeout_memo.get(path_key)
+ if memo is not None and memo[0] == mtime_ns:
+ return memo[1]
timeout = None
if mtime_ns != -1:
@@ -904,7 +929,7 @@ def _honcho_json_timeout() -> float | None:
raw.get("timeout"),
raw.get("requestTimeout"),
)
- _honcho_json_timeout_memo = (mtime_ns, timeout)
+ _honcho_json_timeout_memo[path_key] = (mtime_ns, timeout)
return timeout
except Exception:
return None
@@ -941,7 +966,11 @@ def _apply_fresh_oauth_token(config: HonchoClientConfig) -> None:
try:
from plugins.memory.honcho import oauth
- token, _ = oauth.ensure_fresh_token(resolve_config_path(), config.host)
+ # Bound path: refresh against the honcho.json this config came from,
+ # not whatever the current context resolves to. On daemon threads the
+ # ambient resolver lands on the default profile and a refresh here
+ # would persist the rotated token into the WRONG profile's file.
+ token, _ = oauth.ensure_fresh_token(config.bound_config_path(), config.host)
if token:
config.api_key = token
except Exception:
@@ -957,8 +986,13 @@ def _refresh_cached_oauth(client: "Honcho", config: HonchoClientConfig | None) -
try:
from plugins.memory.honcho import oauth
- host = config.host if config is not None else resolve_active_host()
- token, refreshed = oauth.ensure_fresh_token(resolve_config_path(), host)
+ if config is not None:
+ host = config.host
+ path = config.bound_config_path()
+ else:
+ host = resolve_active_host()
+ path = resolve_config_path()
+ token, refreshed = oauth.ensure_fresh_token(path, host)
if refreshed and token and not oauth.apply_token_to_client(client, token):
_honcho_client_slot.reset()
except Exception:
@@ -1108,7 +1142,7 @@ def get_honcho_client(config: HonchoClientConfig | None = None) -> Honcho:
def reset_honcho_client() -> None:
"""Reset the Honcho client singleton (useful for testing)."""
- global _cached_timeout, _honcho_json_timeout_memo
+ global _cached_timeout
_honcho_client_slot.reset()
_cached_timeout = None
- _honcho_json_timeout_memo = (None, None)
+ _honcho_json_timeout_memo.clear()
diff --git a/plugins/memory/honcho/session.py b/plugins/memory/honcho/session.py
index b8d2db4ef3c..0d53f54f0e3 100644
--- a/plugins/memory/honcho/session.py
+++ b/plugins/memory/honcho/session.py
@@ -9,6 +9,7 @@ import logging
import threading
from dataclasses import dataclass, field
from datetime import datetime
+from pathlib import Path
from typing import Any, Callable, TYPE_CHECKING
from plugins.memory.honcho.client import get_honcho_client
@@ -209,10 +210,15 @@ class HonchoSessionManager:
def honcho(self) -> Honcho:
"""Get the Honcho client, refreshing a near-expiry OAuth token in place.
- Routes every access through ``get_honcho_client`` (which returns the same
- cached singleton) so a long session can't outlive its 1h access token.
+ Routes every access through ``get_honcho_client`` WITH this manager's
+ bound config so a long session can't outlive its 1h access token AND
+ so background threads (async writer, prefetch, sync) acquire the
+ client for the profile this manager was built under — a bare
+ ``get_honcho_client()`` re-resolves ambient ContextVar-backed state
+ that daemon threads cannot see, migrating every access onto the
+ first-built profile's client (#69123, #74065).
"""
- self._honcho = get_honcho_client()
+ self._honcho = get_honcho_client(self._config)
return self._honcho
def _record_auth_failure(self, exc: BaseException) -> None:
@@ -238,6 +244,20 @@ class HonchoSessionManager:
self._auth_notice_emitted = True
return self._auth_failure
+ def _bound_config_path(self) -> Path:
+ """Config path for OAuth checks, bound to this manager's profile.
+
+ Falls back to ambient resolution only when the manager was built
+ without a config (tests) — on the hot path the bound path keeps
+ background threads reading THIS profile's honcho.json, not the
+ default profile the ContextVar-blind resolver would land on.
+ """
+ from plugins.memory.honcho.client import HonchoClientConfig, resolve_config_path
+
+ if isinstance(self._config, HonchoClientConfig):
+ return self._config.bound_config_path()
+ return resolve_config_path()
+
def _reauth_required(self) -> bool:
"""True when the grant is dead and only a new login can fix it.
@@ -251,12 +271,10 @@ class HonchoSessionManager:
if not oauth.any_dead_grants():
return False
- from plugins.memory.honcho.client import resolve_config_path
-
host = getattr(self._config, "host", "") or ""
if not host:
return False
- return oauth.reauth_required(resolve_config_path(), host)
+ return oauth.reauth_required(self._bound_config_path(), host)
except Exception:
return False
@@ -267,15 +285,12 @@ class HonchoSessionManager:
"""
try:
from plugins.memory.honcho import oauth
- from plugins.memory.honcho.client import (
- reset_honcho_client,
- resolve_config_path,
- )
+ from plugins.memory.honcho.client import reset_honcho_client
host = getattr(self._config, "host", "") or ""
if not host:
return False
- token = oauth.force_refresh_token(resolve_config_path(), host)
+ token = oauth.force_refresh_token(self._bound_config_path(), host)
if not token:
return False
if not oauth.apply_token_to_client(self.honcho, token):