diff --git a/plugins/memory/honcho/__init__.py b/plugins/memory/honcho/__init__.py index ea24ed064f..8bf9676e57 100644 --- a/plugins/memory/honcho/__init__.py +++ b/plugins/memory/honcho/__init__.py @@ -24,6 +24,7 @@ from typing import Any, Callable, Dict, List, Optional from agent.memory_manager import sanitize_context from agent.memory_provider import TRIVIAL_PROMPT_RE, MemoryProvider, is_trivial_prompt +from plugins.memory.honcho.client import spawn_context_thread from tools.registry import tool_error logger = logging.getLogger(__name__) @@ -467,9 +468,8 @@ class HonchoMemoryProvider(MemoryProvider): self._manager = None logger.warning("Honcho background session init failed: %s", e) - self._init_thread = threading.Thread( - target=_run, - daemon=True, + self._init_thread = spawn_context_thread( + _run, name="honcho-session-init", ) self._init_thread.start() @@ -546,9 +546,8 @@ class HonchoMemoryProvider(MemoryProvider): self._dialectic_empty_streak += 1 self._prefetch_thread_started_at = time.monotonic() - prewarm_thread = threading.Thread( - target=_prewarm_dialectic, - daemon=True, + prewarm_thread = spawn_context_thread( + _prewarm_dialectic, name="honcho-prewarm-dialectic", ) prewarm_thread.start() @@ -776,9 +775,7 @@ class HonchoMemoryProvider(MemoryProvider): except Exception as e: logger.debug("Honcho first-turn base context failed: %s", e) - _bt = threading.Thread( - target=_fetch_base, daemon=True, name="honcho-base-first" - ) + _bt = spawn_context_thread(_fetch_base, name="honcho-base-first") _bt.start() _base_wait = ( max(0.0, first_turn_base_deadline - time.monotonic()) @@ -853,8 +850,8 @@ class HonchoMemoryProvider(MemoryProvider): self._dialectic_empty_streak += 1 self._prefetch_thread_started_at = time.monotonic() - first_turn_thread = threading.Thread( - target=_run_first_turn, daemon=True, name="honcho-prefetch-first" + first_turn_thread = spawn_context_thread( + _run_first_turn, name="honcho-prefetch-first" ) first_turn_thread.start() self._prefetch_thread = first_turn_thread @@ -1009,9 +1006,7 @@ class HonchoMemoryProvider(MemoryProvider): self._dialectic_empty_streak += 1 self._prefetch_thread_started_at = time.monotonic() - prefetch_thread = threading.Thread( - target=_run, daemon=True, name="honcho-prefetch" - ) + prefetch_thread = spawn_context_thread(_run, name="honcho-prefetch") prefetch_thread.start() self._prefetch_thread = prefetch_thread @@ -1416,9 +1411,7 @@ class HonchoMemoryProvider(MemoryProvider): if self._sync_thread and self._sync_thread.is_alive(): self._sync_thread.join(timeout=5.0) - self._sync_thread = threading.Thread( - target=_sync, daemon=True, name="honcho-sync" - ) + self._sync_thread = spawn_context_thread(_sync, name="honcho-sync") self._sync_thread.start() def on_memory_write( @@ -1451,7 +1444,7 @@ class HonchoMemoryProvider(MemoryProvider): except Exception as e: logger.debug("Honcho memory mirror failed: %s", e) - t = threading.Thread(target=_write, daemon=True, name="honcho-memwrite") + t = spawn_context_thread(_write, name="honcho-memwrite") t.start() def on_session_end(self, messages: List[Dict[str, Any]]) -> None: diff --git a/plugins/memory/honcho/client.py b/plugins/memory/honcho/client.py index 7440f5b8bf..45dc7e26ea 100644 --- a/plugins/memory/honcho/client.py +++ b/plugins/memory/honcho/client.py @@ -889,6 +889,33 @@ _client_slot_timeouts: dict[tuple, float] = {} _client_slots_lock = _threading.Lock() +def spawn_context_thread( + target, + *, + name: str, + daemon: bool = True, + args: tuple = (), +) -> "_threading.Thread": + """Spawn a thread that inherits the caller's contextvars. + + Profile isolation in multi-profile processes is a ContextVar + (set_hermes_home_override); plain threading.Thread targets start with an + EMPTY context, so any ambient resolution on the thread + (resolve_config_path, resolve_active_host, get_hermes_home) silently + lands on the default profile. Copying the caller's context at spawn time + makes the thread see the profile scope it was created under. + """ + import contextvars + + ctx = contextvars.copy_context() + t = _threading.Thread( + target=lambda: ctx.run(target, *args), + name=name, + daemon=daemon, + ) + return t + + def _credential_fingerprint(config: HonchoClientConfig | None) -> str: """Stable identity for the credential a client will be built with. diff --git a/plugins/memory/honcho/session.py b/plugins/memory/honcho/session.py index 0d53f54f0e..efaa986333 100644 --- a/plugins/memory/honcho/session.py +++ b/plugins/memory/honcho/session.py @@ -12,7 +12,7 @@ from datetime import datetime from pathlib import Path from typing import Any, Callable, TYPE_CHECKING -from plugins.memory.honcho.client import get_honcho_client +from plugins.memory.honcho.client import get_honcho_client, spawn_context_thread from plugins.memory.honcho.oauth import redact_tokens as _redact_tokens if TYPE_CHECKING: @@ -779,10 +779,9 @@ class HonchoSessionManager: return with self._async_thread_lock: if self._async_thread is None or not self._async_thread.is_alive(): - self._async_thread = threading.Thread( - target=self._async_writer_loop, + self._async_thread = spawn_context_thread( + self._async_writer_loop, name="honcho-async-writer", - daemon=True, ) self._async_thread.start() @@ -932,7 +931,7 @@ class HonchoSessionManager: if result: self.set_context_result(session_key, result) - t = threading.Thread(target=_run, name="honcho-context-prefetch", daemon=True) + t = spawn_context_thread(_run, name="honcho-context-prefetch") t.start() def set_context_result(self, session_key: str, result: dict[str, str]) -> None: