Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion hermes_cli/oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,15 @@ def _run_agent(
agent.stream_delta_callback = None
agent.tool_gen_callback = None

return agent.chat(prompt) or ""
try:
return agent.chat(prompt) or ""
finally:
try:
messages = getattr(agent, "_session_messages", None)
if hasattr(agent, "shutdown_memory_provider"):
agent.shutdown_memory_provider(messages if isinstance(messages, list) else None)
except Exception:
pass


def _oneshot_clarify_callback(question: str, choices=None) -> str:
Expand Down
8 changes: 6 additions & 2 deletions plugins/memory/honcho/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,10 +1415,14 @@ def shutdown(self) -> None:
for t in (self._prefetch_thread, self._sync_thread):
if t and t.is_alive():
t.join(timeout=5.0)
# Flush any remaining messages
# Flush remaining messages and shut down manager-owned background threads.
if self._manager and not (self._init_thread and self._init_thread.is_alive() and not self._session_initialized):
try:
self._manager.flush_all()
shutdown = getattr(self._manager, "shutdown", None)
if callable(shutdown):
shutdown()
else:
self._manager.flush_all()
except Exception:
pass

Expand Down
14 changes: 13 additions & 1 deletion plugins/memory/honcho/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def __init__(
# one source of truth; see __init__.py _do_session_init for the prewarm.
self._context_cache: dict[str, dict] = {}
self._prefetch_cache_lock = threading.Lock()
self._context_prefetch_threads: list[threading.Thread] = []
self._context_prefetch_threads_lock = threading.Lock()
self._dialectic_reasoning_level: str = (
config.dialectic_reasoning_level if config else "low"
)
Expand Down Expand Up @@ -546,7 +548,14 @@ def flush_all(self) -> None:
break

def shutdown(self) -> None:
"""Gracefully shut down the async writer thread."""
"""Gracefully shut down the async writer and context prefetch threads."""
with self._context_prefetch_threads_lock:
threads = list(self._context_prefetch_threads)
self._context_prefetch_threads = []
for t in threads:
if t.is_alive():
t.join(timeout=10)

if self._async_queue is not None and self._async_thread is not None:
self.flush_all()
self._async_queue.put(_ASYNC_SHUTDOWN)
Expand Down Expand Up @@ -676,6 +685,9 @@ def _run():
self.set_context_result(session_key, result)

t = threading.Thread(target=_run, name="honcho-context-prefetch", daemon=True)
with self._context_prefetch_threads_lock:
self._context_prefetch_threads = [x for x in self._context_prefetch_threads if x.is_alive()]
self._context_prefetch_threads.append(t)
t.start()

def set_context_result(self, session_key: str, result: dict[str, str]) -> None:
Expand Down