fix(gateway): reset _last_flushed_db_idx on cached-agent reuse#44354
Closed
liuhao1024 wants to merge 1 commit into
Closed
fix(gateway): reset _last_flushed_db_idx on cached-agent reuse#44354liuhao1024 wants to merge 1 commit into
liuhao1024 wants to merge 1 commit into
Conversation
When the gateway reuses a cached AIAgent across turns, _init_cached_agent_for_turn() reset _api_call_count but left _last_flushed_db_idx stale from the previous turn. This caused _flush_messages_to_session_db() to compute flush_from from the stale cursor rather than the new turn's conversation_history length, silently skipping the assistant reply and producing consecutive-user transcript rows that corrupt context replay on subsequent turns. Reset _last_flushed_db_idx to 0 at the start of every cached-agent turn (regardless of interrupt depth) so the flush cursor is always aligned with the current turn's history boundary. Fixes NousResearch#44327
Collaborator
Contributor
Author
|
Thanks for the flag @alt-glitch. Confirmed — #32760 has the same one-line fix ( |
This was referenced Jun 11, 2026
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?
Resets
_last_flushed_db_idxon cachedAIAgentreuse so_flush_messages_to_session_dbrecomputes its flush offset from the new turn'sconversation_historylength instead of carrying a stale cursor from the previous turn. Without this, the assistant reply is silently skipped and the transcript accumulates consecutiveuserrows, corrupting context replay on subsequent turns.Related Issue
Fixes #44327
Type of Change
Changes Made
gateway/run.py: Addedagent._last_flushed_db_idx = 0to_init_cached_agent_for_turn()so the SessionDB flush cursor is reset on every cached-agent turn, regardless of interrupt depth.tests/gateway/test_agent_cache.py: Added 3 tests verifying the flush cursor reset (fresh turn, interrupt-recursive turn, idempotent zero case).How to Test
messagestable (sqlite3 ~/.hermes/state.db "SELECT role, content FROM messages WHERE session_id='<id>'").userrows that corrupt context replay.pytest tests/gateway/test_agent_cache.py -v -k flush_cursor— all 3 new tests should pass.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/ACode Intelligence
_init_cached_agent_for_turn(callers: 3 in gateway/run.py)_flush_messages_to_session_db(uses_last_flushed_db_idxin flush_from computation)_last_flushed_db_idxis also reset by scaffolding-drop clamps (State.db loses assistant responses when _drop_trailing_empty_response_scaffolding causes _last_flushed_db_idx to overshoot len(messages) #31507) and compression-reset paths (Context compaction loses assistant messages and merges user follow-ups #43066); this fix covers the missing gateway cached-agent path