Skip to content

fix(hindsight): flush buffered turns on session end to prevent silent data loss - #36988

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/hindsight-session-end-flush
Closed

fix(hindsight): flush buffered turns on session end to prevent silent data loss#36988
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/hindsight-session-end-flush

Conversation

@liuhao1024

Copy link
Copy Markdown
Contributor

What does this PR do?

Flushes buffered turns in HindsightMemoryProvider.on_session_end() to prevent silent data loss when a session ends before hitting the retain_every_n_turns modulo boundary.

Related Issue

Fixes #36216

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Changes Made

  • plugins/memory/hindsight/__init__.py: Added on_session_end() method that flushes _session_turns via the writer queue before session teardown. Mirrors the existing flush logic in on_session_switch() but without resetting session state (the session is ending, not rotating).
  • tests/plugins/memory/test_hindsight_provider.py: Added TestSessionEndBufferFlush with 4 tests: buffered turns flushed, empty buffer no-op, state preservation during flush, skip during shutdown.

How to Test

  1. Set retain_every_n_turns: 12 in Hindsight config
  2. Start a conversation and exchange 8 turns (fewer than 12)
  3. Exit the CLI or let the session expire via gateway idle timeout
  4. Check Hindsight bank — the 8 turns should now be retained (previously they were silently dropped)
  5. Run pytest tests/plugins/memory/test_hindsight_provider.py::TestSessionEndBufferFlush -xvs to verify the regression tests pass

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/plugins/memory/test_hindsight_provider.py -x and all 105 tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture or workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A
  • I've updated tool descriptions/schemas if I changed tool behavior — or N/A

Code Intelligence

  • Analyzed: plugins/memory/hindsight/__init__.pyHindsightMemoryProvider.on_session_end (new method), on_session_switch (reference pattern), sync_turn (buffer logic), shutdown (teardown sequence)
  • Blast radius: LOW — single new method on a plugin provider, no changes to existing methods or base class
  • Related patterns: on_session_switch (lines 1586-1712) uses identical flush logic; shutdown (line 1714) drains the same writer queue; MemoryManager.on_session_end (agent/memory_manager.py:477) is the caller

… data loss

When retain_every_n_turns > 1, a session that ends before hitting the
modulo boundary (CLI exit, idle timeout, /reset) silently drops all
buffered turns. The on_session_switch hook already handles this for
mid-session rotations, but on_session_end was inherited as a no-op from
the base MemoryProvider class despite being declared in plugin.yaml.

Add on_session_end() that flushes _session_turns via the same writer
queue used by on_session_switch, preserving FIFO ordering with any
still-queued retains from the same session.

Fixes NousResearch#36216
@alt-glitch alt-glitch added type/bug Something isn't working comp/plugins Plugin system and bundled plugins tool/memory Memory tool and memory providers P3 Low — cosmetic, nice to have labels Jun 2, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #36219 (and earlier #28845) — all three implement HindsightMemoryProvider.on_session_end() to flush buffered _session_turns through the writer queue at teardown, and all fix #36216. Consolidating on one PR would avoid redundant review.

@liuhao1024

Copy link
Copy Markdown
Contributor Author

Acknowledged — #36219 looks more complete. Closing this as duplicate.

@liuhao1024

Copy link
Copy Markdown
Contributor Author

Confirmed — closing as duplicate of #36219, which has a more complete implementation. As noted earlier, all three PRs (#36219, #28845, this one) implement HindsightMemoryProvider.on_session_end() to flush buffered turns, and #36219 is the most comprehensive.

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 tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Hindsight drops buffered turns on session end when retain_every_n_turns > 1

2 participants