fix: message cursor pagination must not skip equal-timestamp rows - #16367
Open
kumarpriyanshu09 wants to merge 1 commit into
Open
kumarpriyanshu09 wants to merge 1 commit into
kumarpriyanshu09 wants to merge 1 commit into
Conversation
getMessagesByCursor filtered and sorted only by the timestamp field and encoded nextCursor as that scalar alone, so rows sharing the page-boundary timestamp were permanently skipped on the next page. Reuse the composite (sort value, _id) cursor pattern already used by conversation/shared-link pagination, keep legacy scalar cursor compatibility, and add regression coverage for equal-timestamp page boundaries. Fixes LibreChat-AI#16310
This branch has not been deployed
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.
Problem
getMessagesByCursorfiltered and sorted only by the selected timestamp field, and encodednextCursoras that scalar alone.When page 1 ended at timestamp
T, page 2 resumed withcreatedAt < T(desc) or> T(asc). Any remaining rows that also had timestampTwere permanently skipped — still in Mongo, unreachable through cursor continuation.Repro from #16310: 30 messages with the same
createdAt,pageSize=25→ page 2 returned 0 instead of the remaining 5. The old equal-timestamp test never crossed a page boundary.Solution
Reuse the composite
(sort value, _id)cursor pattern already used by conversation / shared-link pagination in data-schemas:nextCursoras base64 JSON{ primary, id }$orof “past primary” and “same primary, past_id”{ [sortField]: sortOrder, _id: sortOrder }CLIENT_MESSAGE_SELECTstrips_id, still fetch it for the tie-breaker, then remove it before returningNo route changes —
api/server/routes/messages.jsalready callsdb.getMessagesByCursor.Tests
packages/data-schemasmessage cursor suite now exercises the real method (it previously reimplemented the buggy query inline), plus:createdAtrows,pageSize=25, desc and asc → page 2 returns the remainder with no gaps/duplicatescreatedAtcursor still resumesFixes #16310