From 5e06c9ffef80fcfd88433b985baaf074674a3909 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 8 Jun 2026 07:03:39 -0700 Subject: [PATCH] fix(agent): clear _session_messages in AIAgent.close() (#42123) 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 c6cc1e21581..015cf7d2b23 100644 --- a/run_agent.py +++ b/run_agent.py @@ -3087,6 +3087,17 @@ class AIAgent: 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.