fix: deduplicate SQLite session writes via _last_flushed_db_idx#870
Open
teyrebaz33 wants to merge 1 commit intoNousResearch:mainfrom
Open
fix: deduplicate SQLite session writes via _last_flushed_db_idx#870teyrebaz33 wants to merge 1 commit intoNousResearch:mainfrom
teyrebaz33 wants to merge 1 commit intoNousResearch:mainfrom
Conversation
_flush_messages_to_session_db() was called from 15+ exit paths in run_conversation(), each time re-flushing all messages from conversation_history start. Combined with _log_msg_to_db() and gateway's append_to_transcript(), messages were written 3-4x, inflating token usage by the same factor. Track _last_flushed_db_idx on the agent instance so each flush only writes truly new messages. Uses getattr() with fallback to len(conversation_history) for backward compatibility with existing sessions that have no prior state. Closes NousResearch#860
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.
Closes #860
Problem
_flush_messages_to_session_db()was called from 15+ exit paths inrun_conversation(), each time re-flushing all messages fromlen(conversation_history). Combined with_log_msg_to_db()and the gateway'sappend_to_transcript(), messages were written 3–4x to SQLite — causing proportionally inflated token usage on every API call.Fix (Option A from issue)
Track
_last_flushed_db_idxon the agent instance. Each flush call starts from the last written index instead of fromlen(conversation_history), so only truly new messages are written.Uses
getattr()fallback for backward compatibility with existing agent instances.Testing
3 new tests in
TestFlushMessagesDeduplication:len(conversation_history)Full suite: 2863 passed.