Skip to content

fix(cli): ensure on_session_end hook fires on all exit paths - #4038

Closed
kelsia14 wants to merge 1 commit into
NousResearch:mainfrom
kelsia14:fix-cli-graceful-shutdown
Closed

fix(cli): ensure on_session_end hook fires on all exit paths#4038
kelsia14 wants to merge 1 commit into
NousResearch:mainfrom
kelsia14:fix-cli-graceful-shutdown

Conversation

@kelsia14

Copy link
Copy Markdown
Contributor

Problem

The on_session_end plugin hook was not firing on abrupt CLI exits, causing sessions to remain unindexed in FTS5. This affected three scenarios:

  1. Ctrl+C (KeyboardInterrupt): Hook not called, session orphaned in JSON without SQLite index
  2. SSH disconnect (BrokenPipeError): Not captured, process died without cleanup
  3. SIGTERM/SIGHUP (kill/terminal hangup): No signal handlers, instant death

Solution

All exit paths now converge to the same finally block that invokes on_session_end.

Changes

  • finally block: Added invoke_hook("on_session_end", ...) with interrupted=True
  • except tuple: Added BrokenPipeError alongside KeyboardInterrupt
  • Signal handlers: SIGHUP/SIGTERM now raise KeyboardInterrupt() to trigger cleanup

Flow

All exit paths (Ctrl+C, SSH drop, SIGTERM, SIGHUP, normal exit) now execute the same cleanup sequence including the on_session_end hook.

Testing

  • pytest tests/hermes_cli/ -q: 755 passed
  • pytest tests/test_plugins.py -q: 19 passed
  • Manual: Verified hook fires on Ctrl+C with test plugin

Impact

Plugins can now reliably cleanup and sync session state on any exit type, fixing the "orphaned JSON sessions" issue.

@kelsia14
kelsia14 force-pushed the fix-cli-graceful-shutdown branch from 3d10fc4 to dffc19a Compare March 30, 2026 20:51
@kelsia14
kelsia14 deleted the fix-cli-graceful-shutdown branch April 2, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant