fix(honcho): propagate contextvars to all plugin background threads
Profile isolation is a ContextVar; plain threading.Thread targets start with an empty context, so the plugin's nine daemon threads (session init, prewarm, first-turn base/prefetch, prefetch, sync, memwrite, async writer, context prefetch) resolved ambient state — config path, active host, hermes home, oauth token paths — against the DEFAULT profile whenever they ran under a routed profile's turn. Adds spawn_context_thread(), which copies the caller's context at spawn time so the thread sees the profile scope it was created under, and routes every plugin thread spawn through it. Defense-in-depth under the bound-config work: even ambient resolution on these threads now lands on the right profile. The copy_context approach follows the gateway's own _run_in_executor_with_context pattern; #81401 applied it to the init thread, this extends it to all nine spawns. Co-authored-by: angel12 <angel12@users.noreply.github.com>
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user