-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Fix out of order messages #7472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,6 +103,10 @@ impl Conversation { | |
| self.0.clear(); | ||
| } | ||
|
|
||
| pub fn sort_by_created(&mut self) { | ||
| self.0.sort_by_key(|m| m.created); | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about making Conversation always keep them sorted? this method could be private and called on every mutable method |
||
| pub fn filtered_messages<F>(&self, filter: F) -> Vec<Message> | ||
| where | ||
| F: Fn(&MessageMetadata) -> bool, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1082,7 +1082,7 @@ impl SessionStorage { | |||||
| async fn get_conversation(&self, session_id: &str) -> Result<Conversation> { | ||||||
| let pool = self.pool().await?; | ||||||
| let rows = sqlx::query_as::<_, (String, String, i64, Option<String>, Option<String>)>( | ||||||
| "SELECT role, content_json, created_timestamp, metadata_json, message_id FROM messages WHERE session_id = ? ORDER BY timestamp", | ||||||
| "SELECT role, content_json, created_timestamp, metadata_json, message_id FROM messages WHERE session_id = ? ORDER BY created_timestamp", | ||||||
|
||||||
| "SELECT role, content_json, created_timestamp, metadata_json, message_id FROM messages WHERE session_id = ? ORDER BY created_timestamp", | |
| "SELECT role, content_json, created_timestamp, metadata_json, message_id FROM messages WHERE session_id = ? ORDER BY created_timestamp, rowid", |
Copilot
AI
Feb 24, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This query now orders by created_timestamp but the schema only indexes timestamp, so fetching a long conversation may require a full sort; consider adding an index (ideally composite on (session_id, created_timestamp)) to keep this query efficient.
Copilot
AI
Feb 24, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are existing tests in this file, but this ordering change isn’t covered; adding a regression test that inserts messages with non-monotonic created_timestamp values and asserts get_conversation() returns them sorted would help prevent this from regressing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorting the entire conversation on every loop iteration is O(n log n) and will get more expensive as history grows; consider only calling
sort_by_created()when you actually inserted an out-of-order message (e.g., after tool-pair summarization) or when the newest message’screatedis older than the current last message.