fix(hindsight): flush buffered turns on session end - #55046
Conversation
Duplicate of #36219 — same fix (HindsightMemoryProvider.on_session_end() flushing buffered _session_turns through the writer queue, both Fixes #36216) with the identical title. #36219 is the earlier open canonical. Companion to your #55045 (the gateway-side hook this depends on). Flagging the cluster so a maintainer can pick one of the on_session_end flush PRs. |
ec8fe33 to
035baa9
Compare
|
Thanks for targeting a real data-loss path. Current main buffers sub-threshold turns in Problems
Suggested changes
This is an automated hermes-sweeper review. |
HindsightMemoryProvider accumulates turns in _session_turns and only flushes them to the API every _retain_every_n_turns turns (default 5). If the session ends (idle TTL eviction, /reset, cache eviction) before reaching the threshold, the buffered turns are silently lost. Override on_session_end() to flush any remaining _session_turns via the writer queue, using the same aretain_batch path as the periodic flush and on_session_switch flush. This ensures no conversation data is lost at session boundaries. The flush is idempotent: if _session_turns is empty, it's a no-op. The queue serializes behind any in-flight retains from sync_turn.
035baa9 to
07698a7
Compare
The periodic retain in sync_turn only sends the unretained watermark delta (_session_turns[_last_retained_turn_count:]) in append mode. The on_session_end flush was sending ALL _session_turns, which would re-ship turns already retained by the periodic flush, causing duplicate content in append-mode documents. Reorder to resolve update_mode first, then use watermark slicing for append mode. Also add early return when no new turns exist (all already retained by periodic flush). Refs: NousResearch#55046 review by teknium1
|
@teknium1 已修复 append-mode 重复 flush 问题: 修复内容 (5fc7ff2):
还需要补充测试覆盖(sub-threshold flushing、append-mode tail-only、no-op guard)——我接下来处理。 |
Cover the session-end flush lifecycle: - sub-threshold turns flushed on session end - no-op when buffer empty - no-op when auto_retain disabled - append mode: only unretained watermark delta sent - overwrite mode: full buffer sent - flush serializes behind pending periodic retains via writer queue Refs: NousResearch#55046 review by teknium1
Problem
HindsightMemoryProvideraccumulates conversation turns in_session_turnsand only flushes them to the Hindsight API every_retain_every_n_turnsturns (default 5). If a session ends (idle TTL eviction, /reset, cap enforcement) before reaching the threshold, the buffered turns are silently discarded — the message content is lost forever.The base
MemoryProvider.on_session_end()is a no-op, and Hindsight does not override it, so session boundaries never trigger a flush.Fix
Override
on_session_end(messages)to flush any remaining_session_turnsinto the Hindsight API via the samearetain_batch+ writer queue path used by the periodic flush andon_session_switchflush.The implementation:
_session_turns, then clears the buffer (re-entrancy safe)_flushjob to the existing serial writer queue, so it's ordered behind any in-flight retainsRelated
Depends on the gateway-side change in PR #55045 which calls
on_session_endbefore evicting cached agents. Without that PR, this hook would never be reached on idle TTL eviction (only on explicit /reset and the existing shutdown path).