Skip to content

fix(agent): clear _session_messages in AIAgent.close() - #42123

Merged
teknium1 merged 1 commit into
mainfrom
fix/close-clears-session-messages
Jun 8, 2026
Merged

fix(agent): clear _session_messages in AIAgent.close()#42123
teknium1 merged 1 commit into
mainfrom
fix/close-clears-session-messages

Conversation

@teknium1

@teknium1 teknium1 commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Summary

AIAgent.close() now clears _session_messages, so the conversation-history list is freed at hard teardown instead of waiting for the agent object itself to be garbage-collected.

Why

close() is the hard teardown for true session boundaries (/new, /reset, session expiry). It already kills background procs, terminal sandboxes, browser daemons, child agents, and the OpenAI client — but left the message list intact. The soft-eviction path (_release_evicted_agent_soft, fixed in 3d029a53e) already clears _session_messages; this closes the matching gap on the hard path. Matters when a caller still holds a reference to the closed agent (e.g. a draining background task), which would otherwise pin tens of MB of tool outputs.

Changes

  • run_agent.py: clear self._session_messages as step 6 of close(), guarded.

Validation

  • py_compile clean. One added block, no behavior change beyond freeing the list at a point where the session is already over.

Follow-up to 3d029a53e / #18438.

Infographic

aiagent-close-free-history

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.
@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

🔎 Lint report: fix/close-clears-session-messages vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 10397 on HEAD, 10397 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 5430 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

@liuhao1024

Copy link
Copy Markdown
Contributor

✅ Verified — _session_messages cleared in AIAgent.close() for memory leak prevention

Reviewed the diff (run_agent.py).

  • Placement confirmed: The _session_messages = [] assignment is added at the end of close(), after all existing teardown steps (tool cleanup, event hooks, etc.). This mirrors the soft-eviction path in _release_evicted_agent_soft which already clears the list.
  • Safety: Wrapped in try/except Exception: pass, consistent with the existing teardown pattern — a failure here must not prevent other cleanup from running.
  • Use case: When a caller holds a reference to a closed agent (e.g., a draining background task), the message list is freed proactively rather than waiting for GC of the agent object.

The fix is correct and minimal. No issues found.

@teknium1
teknium1 merged commit 5e06c9f into main Jun 8, 2026
23 checks passed
@teknium1
teknium1 deleted the fix/close-clears-session-messages branch June 8, 2026 14:03
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels Jun 8, 2026
a249169329-cpu pushed a commit to a249169329-cpu/hermes-agent that referenced this pull request Jun 8, 2026
…42123)

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.
changman pushed a commit to changman/hermes-agent that referenced this pull request Jun 10, 2026
…42123)

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.
alt-glitch pushed a commit that referenced this pull request Jun 14, 2026
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.
davidgut1982 pushed a commit to davidgut1982/hermes-agent that referenced this pull request Jun 17, 2026
…42123)

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.
T02200059 pushed a commit to T02200059/hermes-agent that referenced this pull request Jun 18, 2026
…42123)

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.
waefrebeorn pushed a commit to waefrebeorn/slermes that referenced this pull request Jul 2, 2026
…42123)

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.
santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
…42123)

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.
donbowman pushed a commit to donbowman/hermes-agent that referenced this pull request Jul 13, 2026
…42123)

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.
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
…42123)

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.
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
…42123)

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.
melon-xf added a commit to melon-xf/hermes-agent that referenced this pull request Sep 3, 2026
…42123)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants