diff --git a/crates/goose/src/session/session_manager.rs b/crates/goose/src/session/session_manager.rs index a11e4b1273..bb8e5eacf9 100644 --- a/crates/goose/src/session/session_manager.rs +++ b/crates/goose/src/session/session_manager.rs @@ -1082,7 +1082,10 @@ impl SessionStorage { async fn get_conversation(&self, session_id: &str) -> Result { let pool = self.pool().await?; let rows = sqlx::query_as::<_, (String, String, i64, Option, Option)>( - "SELECT role, content_json, created_timestamp, metadata_json, message_id FROM messages WHERE session_id = ? ORDER BY timestamp", + // Order by created_timestamp, then by id to break ties. created_timestamp is in seconds, + // so messages created in the same second (e.g., tool request and response) need to + // maintain their insertion order via the auto-increment id. + "SELECT role, content_json, created_timestamp, metadata_json, message_id FROM messages WHERE session_id = ? ORDER BY created_timestamp, id", ) .bind(session_id) .fetch_all(pool)