fix(agent): rewind flush cursor exactly when repair compacts before the cursor

Follow-up to the #44837 clamp: a min() clamp only fixes cursor overshoot
past the new end of the list. When repair_message_sequence drops/merges
messages at indexes below the cursor, the clamp leaves the cursor pointing
past unflushed rows and the turn-end flush silently skips them.

Extract repair_message_sequence_with_cursor(): snapshot the flushed prefix
by object identity before repair, then recompute the cursor as the count
of surviving flushed messages. Falls back to the clamp when no snapshot is
available. Keeps the safety guard in _flush_messages_to_session_db.

Adds targeted tests for overshoot, before-cursor compaction, no-repair,
bare-agent, and the flush guard.
This commit is contained in:
Teknium
2026-06-12 16:29:01 -07:00
parent 5d0408d9fe
commit 8905ee6b8a
3 changed files with 143 additions and 9 deletions
+5 -9
View File
@@ -595,21 +595,17 @@ def run_conversation(
# landed after an orphan tool result). Most providers return
# empty content on malformed sequences, which would otherwise
# retrigger the empty-retry loop indefinitely.
repaired_seq = agent._repair_message_sequence(messages)
# repair_message_sequence_with_cursor also recomputes the SessionDB
# flush cursor (_last_flushed_db_idx) when repair compacts the list,
# so the turn-end flush doesn't skip the assistant/tool chain (#44837).
from agent.agent_runtime_helpers import repair_message_sequence_with_cursor
repaired_seq = repair_message_sequence_with_cursor(agent, messages)
if repaired_seq > 0:
request_logger.info(
"Repaired %s message-alternation violations before request (session=%s)",
repaired_seq,
agent.session_id or "-",
)
# Clamp the SessionDB flush cursor after compaction. If repair
# merged or dropped messages, _last_flushed_db_idx may now point
# past the new end of `messages`, causing turn-end flush to skip
# the assistant/tool chain entirely (#44837).
if hasattr(agent, "_last_flushed_db_idx"):
agent._last_flushed_db_idx = min(
agent._last_flushed_db_idx, len(messages)
)
api_messages = []
for idx, msg in enumerate(messages):