fix(memory): send only incremental turns in Hindsight append mode - #25763
Closed
estridell wants to merge 1 commit into
Closed
fix(memory): send only incremental turns in Hindsight append mode#25763estridell wants to merge 1 commit into
estridell wants to merge 1 commit into
Conversation
…When Hindsight >=0.5.0 returns update_mode='append' from the version\nprobe, sync_turn() was still sending the full accumulated session on\nevery retain. This creates quadratic token usage — turn 1, then turns\n1+2, then turns 1+2+3 — because _session_turns is never cleared.\n\nWith append-mode documents, the correct behavior is to send only the\nturns buffered since the last retain. The server appends them to the\nexisting document, so prior turns don't need to be re-sent.\n\nLegacy overwrite mode (no update_mode) is unchanged: full-session\nsnapshots are still required so the document replaces prior content.\n\n_session_turns itself is NOT cleared — it must remain populated so\non_session_switch() can still flush partial buffers when\nretain_every_n_turns > 1. Instead, a _last_retained_turn_count\ncounter tracks how many turns have already been sent.\n\nFixes the token-cost half of the append-mode integration gap introduced\nin 3082fa0 (feat: probe API for update_mode='append' support).\n
This was referenced May 17, 2026
This was referenced May 24, 2026
Contributor
|
This looks implemented on current Evidence:
|
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.
What does this PR do?
Fixes quadratic token usage in
HindsightMemoryProvider.sync_turn()when the Hindsight API supportsupdate_mode="append"(>=0.5.0).The bug:
sync_turn()sends the full accumulated_session_turnson every retain. With append-mode documents, this means turn 1 is sent N times, turn 2 is sent N-1 times, etc. — O(n²) token usage that burns through API credits on long sessions.The fix: Track how many turns have already been retained (
_last_retained_turn_count). In append mode, send only_session_turns[_last_retained_turn_count:](the new turns since last retain). Legacy overwrite mode is unchanged — full-session snapshots are still required._session_turnsitself is NOT cleared, soon_session_switch()can still flush partial buffers whenretain_every_n_turns > 1.Related Issue
Fixes the token-cost half of the append-mode integration gap introduced in 3082fa0 (feat: probe API for update_mode='append' support, dedupe across processes).
Type of Change
Changes Made
plugins/memory/hindsight/__init__.py: Add_last_retained_turn_countcounter, use it to slice only new turns in append mode, reset in__init__/initialize()/on_session_switch()tests/plugins/memory/test_hindsight_provider.py: Rename existing test to clarify it covers legacy overwrite mode, add new testtest_sync_turn_append_mode_sends_only_new_buffered_turnsthat verifies incremental sendsHow to Test
pytest tests/plugins/memory/test_hindsight_provider.py -q— 97 tests passpytest tests -q -k 'hindsight'— 102 tests pass, 1 skippedtest_sync_turn_append_mode_sends_only_new_buffered_turnsspecifically verifies that the second retain does NOT contain turns from the first retainChecklist
fix(memory): ...)pytest tests/ -qand all tests passNotes
The
on_session_switch()flush in append mode will re-send some already-retained turns (redundant but safe). This is the correct tradeoff — data safety > perfect efficiency. The flush code is upstream and should not be touched in this PR.