From 2a596dec514528374f3086355630c0f56c3ca3e8 Mon Sep 17 00:00:00 2001 From: teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 8 Jun 2026 06:37:50 -0700 Subject: [PATCH] fix(agent): clear _session_messages in AIAgent.close() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit close() is the hard teardown for true session boundaries (/new, /reset, session expiry). It already closes the OpenAI client and child agents but left the conversation-history list intact. Mirror the soft-eviction path (_release_evicted_agent_soft clears _session_messages) so a held reference to a closed agent — e.g. a draining background task — doesn't pin tens of MB of tool outputs until the agent object itself is collected. --- run_agent.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/run_agent.py b/run_agent.py index c6cc1e21581a..015cf7d2b239 100644 --- a/run_agent.py +++ b/run_agent.py @@ -3087,6 +3087,17 @@ def close(self) -> None: except Exception: pass + # 6. Free conversation history. Mirrors _release_evicted_agent_soft's + # soft-eviction clear — close() is the hard teardown for true session + # boundaries (/new, /reset, session expiry), so the message list won't + # be reused. Drops the reference proactively rather than waiting for + # the agent object itself to be collected, which matters when a caller + # still holds the closed agent (e.g. a draining background task). + try: + self._session_messages = [] + except Exception: + pass + def _hydrate_todo_store(self, history: List[Dict[str, Any]]) -> None: """ Recover todo state from conversation history.