fix(agent): clamp flush_from to len(messages) after repair shortens the list#24326
Closed
ryptotalent wants to merge 1 commit into
Closed
fix(agent): clamp flush_from to len(messages) after repair shortens the list#24326ryptotalent wants to merge 1 commit into
ryptotalent wants to merge 1 commit into
Conversation
…he list _repair_message_sequence() shortens the in-memory messages list in-place (merging consecutive user messages, dropping stray tool results), but _flush_messages_to_session_db() still uses len(conversation_history) — the pre-repair length — as the starting offset. When repair removes N messages, flush_from can exceed len(messages), so messages[flush_from:] returns an empty list and the current turn is silently dropped from SessionDB. Gateway integrations that create a fresh AIAgent per inbound message are most affected, because they rely on SessionDB for conversation continuity across requests. Fix: clamp flush_from with min(..., len(messages)) so the slice never overflows. This also handles stale _last_flushed_db_idx values after repair. Closes NousResearch#24187
19 tasks
Contributor
|
Resolved by #46071. This PR targets the same overflow of |
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.
Problem
_repair_message_sequence()shortens the in-memorymessageslist in-place (merging consecutive user messages, dropping stray tool results). However,_flush_messages_to_session_db()still useslen(conversation_history)— the pre-repair length — as the starting offset (start_idx).When repair removes N messages from the historical portion:
start_idx = len(conversation_history)reflects the old, longer lengthflush_from = max(start_idx, self._last_flushed_db_idx)can exceedlen(messages)messages[flush_from:]returns[]— the current user turn and assistant response are silently not persisted to SessionDBGateway integrations that create a fresh
AIAgentper inbound message (Telegram, Discord, etc.) rely on SessionDB for conversation continuity across requests. A dropped turn means the agent "forgets" what just happened.Fix
Clamp
flush_fromwithmin(..., len(messages))so the slice never overflows:This also correctly handles stale
_last_flushed_db_idxvalues after repair shortens the list.Testing
ast.parse()passes_repair_message_sequencemerges messages in the historical portion, then observe that_flush_messages_to_session_dbnow correctly persists the current turnCloses #24187