Skip to content

[BUG] [v0.1.0] load_session_at_index() appends loaded messages to existing conversation instead of replacing them #163

@climax-dev-1

Description

@climax-dev-1

Bug Description

load_session_at_index() in cortex-tui/src/runner/event_loop/mouse.rs loads a session from history but never clears the existing messages in app_state.messages before appending the loaded session's messages. This causes messages from the previous session to remain visible, with the newly loaded session's messages appended after them.

Location

src/cortex-tui/src/runner/event_loop/mouse.rs, lines 361–384

Root Cause

The function calls self.app_state.load_session(session_id) which only sets the session ID and switches to AppView::Session — it does not clear self.app_state.messages. Then the loop on line 368 calls self.app_state.add_message(message) for each message in the loaded session, appending them to whatever messages were already present.

fn load_session_at_index(&mut self, idx: usize) -> Result<()> {
    if let Some(session) = self.app_state.session_history.get(idx) {
        let session_id = session.id;
        self.app_state.load_session(session_id);  // does NOT clear messages

        let session_id_str = session_id.to_string();
        if let Ok(loaded_session) = CortexSession::load(&session_id_str) {
            for msg in loaded_session.messages() {
                // Appends on top of existing messages!
                self.app_state.add_message(message);
            }
            self.cortex_session = Some(loaded_session);
        }
    }
    Ok(())
}

Reproduction Steps

  1. Start a conversation and exchange several messages with the AI
  2. Click on a different session in the session history sidebar
  3. Observe that the UI now shows the old conversation's messages followed by the loaded session's messages

Expected Behavior

When loading a session from history, the existing messages should be cleared first so only the loaded session's messages are displayed.

Fix

Add self.app_state.clear_messages() (or equivalent) before the message loading loop:

fn load_session_at_index(&mut self, idx: usize) -> Result<()> {
    if let Some(session) = self.app_state.session_history.get(idx) {
        let session_id = session.id;
        self.app_state.load_session(session_id);

        let session_id_str = session_id.to_string();
        if let Ok(loaded_session) = CortexSession::load(&session_id_str) {
            self.app_state.clear_messages();  // Clear before loading
            for msg in loaded_session.messages() {
                // ...
            }
        }
    }
    Ok(())
}

Impact

Users who click on sessions in the sidebar will see a corrupted conversation view with messages from multiple sessions mixed together. This makes session history navigation unreliable.

Version

v0.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions