fix(session): prefer longer source in load_transcript to prevent legacy truncation#3221
Closed
Mibayy wants to merge 1 commit into
Closed
fix(session): prefer longer source in load_transcript to prevent legacy truncation#3221Mibayy wants to merge 1 commit into
Mibayy wants to merge 1 commit into
Conversation
…cy truncation
When a long-lived session pre-dates SQLite storage (e.g. sessions
created before the DB layer was introduced, or after a clean
deployment that reset the DB), _flush_messages_to_session_db only
writes the *new* messages from the current turn to SQLite — it skips
messages already present in conversation_history, assuming they are
already persisted.
That assumption fails for legacy JSONL-only sessions:
Turn N (first after DB migration):
load_transcript(id) → SQLite: 0 → falls back to JSONL: 994 ✓
_flush_messages_to_session_db: skip first 994, write 2 new → SQLite: 2
Turn N+1:
load_transcript(id) → SQLite: 2 → returns immediately ✗
Agent sees 2 messages of history instead of 996
The same pattern causes the reported symptom: session JSON truncated
to 4 messages (_save_session_log writes agent.messages which only has
2 history + 2 new = 4).
Fix: always load both sources and return whichever is longer. For a
fully-migrated session SQLite will always be ≥ JSONL, so there is no
regression. For a legacy session that hasn't been bootstrapped yet,
JSONL wins and the full history is restored.
Closes NousResearch#3212
Contributor
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.
Summary
Closes #3212
Fixes conversation context silently truncating to 4 messages after a gateway restart for sessions with long JSONL history.
Root Cause
_flush_messages_to_session_dbdeliberately skips messages already inconversation_historyto avoid duplicate writes (the #860 fix). The assumption is that those messages are already persisted in SQLite. That assumption is false for:Exact failure trace for the reporter's session (994 JSONL messages):
This matches the reporter's exact evidence: JSONL 994 ✅, session JSON 4 ❌.
Fix
load_transcriptnow loads both SQLite and JSONL, then returns whichever has more messages.For a fully-migrated session, SQLite will always be ≥ JSONL (JSONL is append-only and stops being written once SQLite takes over). There is no regression for normal sessions.
The JSONL read is a sequential scan; for very large transcripts it adds a small I/O cost, but
load_transcriptis called once per gateway turn and the file is already in the OS page cache for active sessions. The trade-off is acceptable.Note on a related bug
This is a separate fix from #3210 / PR #3220 (cached agent
session_idmismatch after session reset). That PR covers the case where a session reset causes the agent to write to the wrong session. This PR covers the case where a legacy JSONL session is never bootstrapped into SQLite, causing subsequent turns to see only the most recent 2 messages.Tests
1453 gateway tests + 456 session tests pass. No regressions.