fix(hindsight): implement on_session_end to flush buffered turns at session exit - #28845
Open
fayenix wants to merge 3 commits into
Open
fix(hindsight): implement on_session_end to flush buffered turns at session exit#28845fayenix wants to merge 3 commits into
fayenix wants to merge 3 commits into
Conversation
…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 NousResearch#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.
13 tasks
teknium1
reviewed
Jun 15, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks for chasing this; the premise is real on current main. Hindsight still buffers turns in sync_turn() until retain_every_n_turns and has no provider-specific on_session_end() on origin/main (plugins/memory/hindsight/__init__.py:1525; git grep only finds sync_turn() and on_session_switch()).
Problems
- The PR waits on
self._sync_thread.join(timeout=10.0)before flushing (plugins/memory/hindsight/__init__.py:1512), but current Hindsight uses_sync_threadas a legacy alias for the long-lived writer thread (plugins/memory/hindsight/__init__.py:559-561,:957-978). That makes normal session-end flushing wait for the timeout instead of just draining queued work. - The flush writes directly to
self._document_idwithout_resolve_retain_target()orupdate_mode(plugins/memory/hindsight/__init__.py:1557). Current main’s retain path may use stablesession_idplusupdate_mode='append'(plugins/memory/hindsight/__init__.py:1054-1073,:1530-1537), and legacy overwrite mode needs full-session content rather than only the modulo remainder. - The added tests don’t cover that append/legacy split, document_id selection, or writer-queue ordering.
Suggested changes
- Mirror
sync_turn()’s current retain-target rules inon_session_end()and use_last_retained_turn_countfor append-mode deltas. - Enqueue the final flush through the writer queue rather than joining the writer thread directly.
Automated hermes-sweeper review.
…sion-end # Conflicts: # tests/plugins/memory/test_hindsight_provider.py
1 task
19 tasks
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.
Summary
The hindsight memory provider buffers turns in-memory during a session but never flushes them when the session ends cleanly. This means buffered turns are silently lost if the gateway shuts down gracefully (no crash, no interrupt).
Changes
on_session_end()on the HindsightProvider to flush all buffered turns to the summary store when the gateway calls it during shutdown.Testing
pytest tests/plugins/memory/test_hindsight_provider.pyRelated
Resubmission of #16697 (closed without merge — commits preserved locally, re-cherry-picked onto current main).