fix(ai): keep conversation records in the order they were written - #1622
Open
Andy-Sverdlov-LucaNet wants to merge 2 commits into
Open
Andy-Sverdlov-LucaNet wants to merge 2 commits into
Andy-Sverdlov-LucaNet wants to merge 2 commits into
Conversation
A turn's user and assistant records are inserted back to back once the stream ends, and TIMESTAMP columns are stored at whole-second precision, so the pair almost always shares one created_at. Ordering by that column alone leaves the tie to the database, which returns the answer before its question often enough to notice, both in the conversation views and in the context built for the next turn. The same tie exposed the prompt: the detail endpoints substitute the topic for the first record, and when the answer sorted first it was the answer that got overwritten while the stored prompt appeared in full a row later. Order by created_at then id so insertion order breaks the tie, and substitute the topic for the first user record rather than for whichever record happens to come first.
The repository test writes a conversation's records with one shared timestamp and checks they come back in insertion order. The service test hands GetConversationDetail records with the answer first and checks that the topic replaces the opening question rather than the answer.
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.
Fixes #1621
A turn's user and assistant records are inserted back to back once the stream ends, and
TIMESTAMPcolumns are stored at whole-second precision, so the pair almost always shares onecreated_at. Ordering by that column alone leaves the tie to the database, which returns the answer before its question often enough to notice, both in the conversation views and in the context built for the next turn.The same tie exposed the prompt: the detail endpoints substitute the topic for the first record, and when the answer sorted first it was the answer that got overwritten while the stored prompt appeared in full a row later.
Order by
created_atthenidso insertion order breaks the tie, and substitute the topic for the first user record rather than for whichever record happens to come first.Read side only, no migration. Existing conversations display correctly once deployed.
Tests: a repository test in
repo_testwrites records with one shared timestamp and checks insertion order; a service test handsGetConversationDetailrecords with the answer first and checks the topic replaces the opening question rather than the answer.