Skip to content

fix(hindsight): implement on_session_end to flush buffered turns at session exit - #16697

Closed
fayenix wants to merge 1 commit into
NousResearch:mainfrom
chiefmojo:fix/hindsight-on-session-end
Closed

fix(hindsight): implement on_session_end to flush buffered turns at session exit#16697
fayenix wants to merge 1 commit into
NousResearch:mainfrom
chiefmojo:fix/hindsight-on-session-end

Conversation

@fayenix

@fayenix fayenix commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes the Part B gap from #15165 ("Gateway restart drops session memory").

sync_turn() batches retention every N turns. Turns accumulated since the last batch boundary were previously lost when the session ended (gateway restart, idle timeout, /reset, /new). The plugin.yaml declared the on_session_end hook but the implementation was never written.

What this does

Implements on_session_end(messages) that:

  1. Skips if auto_retain disabled or no client
  2. Waits for in-flight sync thread (10s timeout)
  3. Calculates remaining turns since last batch boundary
  4. Flushes only remaining turns via synchronous aretain_batch
  5. Handles errors gracefully (logs, never raises)

Tests

8 new tests in TestOnSessionEnd covering: flush, auto_retain skip, aligned no-op, zero turns, missing client, in-flight sync, error resilience, session tags.

All 89 existing + 8 new = 97 tests pass.

Related

…ession exit

sync_turn() batches retention every N turns. Turns accumulated since the
last batch boundary were previously lost when the session ended (gateway
restart, idle timeout, /reset, /new). This left users with empty recall
on the first turn after any session boundary.

on_session_end now forces a final synchronous retention of any remaining
buffered turns, preserving conversation context across session restarts.

Fixes the Part B gap from #15165 — the plugin YAML declared the hook
but the implementation was never written.

Tests cover: flush of buffered turns, auto_retain skip, already-aligned
no-op, zero turns, missing client, in-flight sync wait, error resilience,
and session tag inclusion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/plugins Plugin system and bundled plugins P3 Low — cosmetic, nice to have type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Gateway restart drops session memory — shutdown_memory_provider receives empty messages

3 participants