fix: session_id wiped on every trim, breaking --resume

The history trimming logic cleared session_id = None whenever entries
exceeded history_size. Since the history was always at capacity, every
new message wiped the session_id immediately after storing it. This
meant --resume was never passed to Claude Code, making every turn a
fresh conversation.

Fix: preserve session_id on trim. Claude Code's --resume loads the
full conversation from its own session transcript on disk, so trimming
our local tracking entries doesn't invalidate the session.

Also adds debug logging for session_id capture/storage (temporary).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Dave
2026-03-18 11:19:00 +00:00
parent a799009720
commit 4bf01c6cca
2 changed files with 7 additions and 4 deletions

View File

@@ -881,6 +881,7 @@ async fn handle_message(
} else {
remaining
};
slog!("[matrix-bot] session_id from chat_stream: {:?}", session_id);
(reply, session_id)
}
Err(e) => {
@@ -903,6 +904,7 @@ async fn handle_message(
let conv = guard.entry(room_id).or_default();
// Store the session ID so the next turn uses --resume.
slog!("[matrix-bot] storing session_id: {:?} (was: {:?})", new_session_id, conv.session_id);
if new_session_id.is_some() {
conv.session_id = new_session_id;
}
@@ -919,13 +921,12 @@ async fn handle_message(
});
// Trim to the configured maximum, dropping the oldest entries first.
// When trimming occurs, clear the session ID so the next turn starts
// a fresh Claude Code session (the old session's context would be
// stale since we've dropped entries from our tracking).
// The session_id is preserved: Claude Code's --resume loads the full
// conversation from its own session transcript on disk, so trimming
// our local tracking doesn't affect the LLM's context.
if conv.entries.len() > ctx.history_size {
let excess = conv.entries.len() - ctx.history_size;
conv.entries.drain(..excess);
conv.session_id = None;
}
// Persist to disk so history survives server restarts.