Skip to content

fix(gateway): fire on_session_finalize on idle expiry (salvage #13756)#15132

Merged
teknium1 merged 2 commits into
mainfrom
hermes/hermes-37dfb080
Apr 24, 2026
Merged

fix(gateway): fire on_session_finalize on idle expiry (salvage #13756)#15132
teknium1 merged 2 commits into
mainfrom
hermes/hermes-37dfb080

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Salvage of #13756 by @dimitrovi onto current main with a regression test added.

Closes #14981.

What this PR does

When _session_expiry_watcher sweeps a session that has aged past its reset policy (idle timeout, scheduled reset), it now fires on_session_finalize — matching the contract already honored by /new, /reset, CLI shutdown, and gateway stop. Before this PR, the expiry path flushed memories and evicted the agent silently, so plugins using on_session_finalize never saw their final-pass extraction opportunity for idle-expired sessions.

How

In _session_expiry_watcher, right after _async_flush_memories completes for an expired session, invoke hermes_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

Validation

Before After
/new fires on_session_finalize
/reset fires hook
CLI shutdown fires hook
Gateway stop() fires hook for each active agent
Idle expiry fires hook ✗ (silent)

Test 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

Stefan Dimitrov and others added 2 commits April 24, 2026 05:29
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.
@teknium1 teknium1 merged commit 25465fd into main Apr 24, 2026
11 of 12 checks passed
@teknium1 teknium1 deleted the hermes/hermes-37dfb080 branch April 24, 2026 12:40
@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/gateway Gateway runner, session dispatch, delivery comp/plugins Plugin system and bundled plugins labels Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery comp/plugins Plugin system and bundled plugins P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: The on_session_finalize hook is not being fired when gateway sessions expire due to configured idle time.

2 participants