diff --git a/cli.py b/cli.py index 761542454..fdf625481 100644 --- a/cli.py +++ b/cli.py @@ -7617,8 +7617,10 @@ class HermesCLI: "error": _summary, } - # Start agent in background thread - agent_thread = threading.Thread(target=run_agent) + # Start agent in background thread (daemon so it cannot keep the + # process alive when the user closes the terminal tab — SIGHUP + # exits the main thread and daemon threads are reaped automatically). + agent_thread = threading.Thread(target=run_agent, daemon=True) agent_thread.start() # Monitor the dedicated interrupt queue while the agent runs. @@ -9626,6 +9628,15 @@ class HermesCLI: raise finally: self._should_exit = True + # Interrupt the agent immediately so its daemon thread stops making + # API calls and exits promptly (agent_thread is daemon, so the + # process will exit once the main thread finishes, but interrupting + # avoids wasted API calls and lets run_conversation clean up). + if self.agent and getattr(self, '_agent_running', False): + try: + self.agent.interrupt() + except Exception: + pass # Flush memories before exit (only for substantial conversations) if self.agent and self.conversation_history: try: