Skip to content

The agent that grows with you

perf(cli): release a retired agent off the command path

kshitijk4poorfe2a47c7f264
diff --git a/hermes_cli/cli_agent_setup_mixin.py b/hermes_cli/cli_agent_setup_mixin.py
index 2119c21dadc..dbb47f1ea16 100644
--- a/hermes_cli/cli_agent_setup_mixin.py
+++ b/hermes_cli/cli_agent_setup_mixin.py
@@ -233,11 +233,14 @@ def _resume_panel_colors() -> tuple:
def _retire_agent(cli) -> None:
"""Drop ``cli.agent`` so the next turn rebuilds it, releasing its LLM clients first: the Codex
app-server child (and MCP descendants) belongs to the instance, so ``self.agent = None`` alone
- orphans it for the CLI process lifetime (#72548). Session tool state is kept (soft release)."""
+ orphans it for the CLI process lifetime (#72548). Session tool state is kept (soft release).
+ The release drains and shuts down memory providers (seconds), so it runs off the command path;
+ non-daemon, so a normal exit still waits for it."""
agent = cli.agent
- if agent is not None and hasattr(agent, "release_clients"):
- agent.release_clients()
cli.agent = None
+ if agent is not None and hasattr(agent, "release_clients"):
+ from agent.memory_provider import spawn_context_thread
+ spawn_context_thread(agent.release_clients, name="cli-agent-retire", daemon=False).start()
class CLIAgentSetupMixin:
diff --git a/tests/hermes_cli/test_cli_retire_agent.py b/tests/hermes_cli/test_cli_retire_agent.py
index 1c69e555f3b..bd7c8db347d 100644
--- a/tests/hermes_cli/test_cli_retire_agent.py
+++ b/tests/hermes_cli/test_cli_retire_agent.py
@@ -7,6 +7,7 @@ from types import SimpleNamespace
from unittest.mock import patch
from hermes_cli.cli_commands_mixin import CLICommandsMixin
+from tests.waiting import wait_until
class _FakeAgent:
@@ -24,4 +25,4 @@ def test_reasoning_command_releases_old_agent_clients_before_rebuild():
CLICommandsMixin._handle_reasoning_command(stub, "/reasoning high")
assert stub.reasoning_config == {"enabled": True, "effort": "high"}
assert stub.agent is None
- assert agent.release_calls == 1
+ wait_until(lambda: agent.release_calls == 1, what="the retired agent's clients to be released")