fix(gateway): fire on_session_finalize on idle expiry (salvage #13756)#15132
Merged
Conversation
Regression test for #14981. Verifies that _session_expiry_watcher fires on_session_finalize for each session swept out of the store, matching the contract documented for /new, /reset, CLI shutdown, and gateway stop. Verified the test fails cleanly on pre-fix code (hook call list missing sess-expired) and passes with the fix applied.
This was referenced Apr 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Salvage of #13756 by @dimitrovi onto current main with a regression test added.
Closes #14981.
What this PR does
When
_session_expiry_watchersweeps a session that has aged past its reset policy (idle timeout, scheduled reset), it now fireson_session_finalize— matching the contract already honored by/new,/reset, CLI shutdown, andgateway stop. Before this PR, the expiry path flushed memories and evicted the agent silently, so plugins usingon_session_finalizenever saw their final-pass extraction opportunity for idle-expired sessions.How
In
_session_expiry_watcher, right after_async_flush_memoriescompletes for an expired session, invokehermes_cli.plugins.invoke_hook("on_session_finalize", session_id=..., platform=...). Platform is parsed from the session key (agent:main:<platform>:<chat_type>:<chat_id>). The invocation is wrapped in try/except so a misbehaving plugin can't break the expiry sweep.Changes
gateway/run.pystefan@dimagents.ai→ @dimitrovitest_idle_expiry_fires_finalize_hookintests/gateway/test_session_boundary_hooks.pyValidation
/newfireson_session_finalize/resetfires hookstop()fires hook for each active agentTest verified as a real regression guard: fails cleanly on pre-fix code ("on_session_finalize was not fired during idle expiry"), passes with the fix applied.
tests/gateway/test_session_boundary_hooks.py— 6/6 pass (1 new regression test)Note on parallel PR #11410
#11410 fires
MemoryProvider.on_session_end()(a different lifecycle hook on the memory-manager layer) on expiry. That's a separate gap — distinct from the plugin-hook fix here. Leaving #11410 open for separate review.Co-authored-by: @dimitrovi