Skip to content

fix: prevent _last_flushed_db_idx overshoot after scaffolding pop - #31531

Closed
529349029 wants to merge 0 commit into
NousResearch:mainfrom
529349029:main
Closed

529349029 wants to merge 0 commit into
NousResearch:mainfrom
529349029:main

Conversation

@529349029

Copy link
Copy Markdown

Summary

Fix a bug where state.db loses assistant responses when _drop_trailing_empty_response_scaffolding pops trailing messages before _flush_messages_to_session_db runs, causing _last_flushed_db_idx to overshoot len(messages) and skip all writes.

Root Cause

In _flush_messages_to_session_db, flush_from = max(start_idx, self._last_flushed_db_idx). When _persist_session calls _drop_trailing_empty_response_scaffolding(messages) first, trailing messages can be popped, reducing len(messages). But _last_flushed_db_idx was already set to the old (higher) value by a previous intermediate persist call. Result: flush_from >= len(messages), messages[flush_from:] is empty, nothing written.

Fix

if flush_from >= len(messages):
    flush_from = start_idx

Evidence

Session mpjo2msf4kzhz9 in state.db: user=26, assistant=123, tool=114 (missing ~43 assistant responses). Same session in WebUI local DB: user=26, assistant=166, tool=122 (complete).

@alt-glitch alt-glitch added type/bug Something isn't working P1 High — major feature broken, no workaround comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint labels May 24, 2026
@alt-glitch

Copy link
Copy Markdown
Contributor

Competing with #31517 — both fix #31507 (_last_flushed_db_idx overshoot after scaffolding drop). This PR resets flush_from inside _flush_messages_to_session_db; #31517 clamps _last_flushed_db_idx in _persist_session (earlier in call chain) and includes regression tests.

@529349029
529349029 force-pushed the main branch 8 times, most recently from 255821f to 3fc76e9 Compare May 31, 2026 07:54
@529349029
529349029 force-pushed the main branch 5 times, most recently from 00e823f to 41fd491 Compare June 10, 2026 00:47
@529349029 529349029 closed this Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P1 High — major feature broken, no workaround type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants