fix SQL query to fetch latest messages instead of earliest#1013
fix SQL query to fetch latest messages instead of earliest#1013omeraplak merged 6 commits intoVoltAgent:mainfrom
Conversation
🦋 Changeset detectedLatest commit: b98f802 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
📝 WalkthroughWalkthroughThis pull request changes message retrieval SQL across four database adapter packages to return recent messages: three packages wrap the inner SELECT in a subquery (inner ORDER BY created_at DESC, outer ORDER BY created_at ASC) and Supabase queries DESC then reverses the result array. A changeset marks the four packages for minor version bumps. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Important Action Needed: IP Allowlist UpdateIf your organization protects your Git platform with IP whitelisting, please add the new CodeRabbit IP address to your allowlist:
Failure to add the new IP will result in interrupted reviews. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
hey @fengyun99 , thank you so much. Can you fix the supabase test? |



PR Checklist
Please check if your PR fulfills the following requirements:
Bugs / Features
What is the current behavior?
When contextLimit is set to 2, the system incorrectly retrieves the oldest two messages from the conversation history instead of the most recent two. This causes the model to miss relevant context from the latest interactions.
For example, with the following sequence:
The model correctly recalls the user's name (from message #1) but fails to recall the user's interest in games (from message #2), because only messages #1 and #2 are fetched—but due to incorrect SQL ordering, it actually fetches messages #1 and #2 as the first two, not the last two before the current query. In effect, when the third message arrives, the retrieved context should be messages #1 (the earliest message), but due to wrong sort order (ASC instead of DESC), it returns outdated or misordered context.
What is the new behavior?
The SQL query used to fetch conversation history now correctly orders messages by timestamp in descending order and limits to the most recent contextLimit entries before the current message. This ensures that the model receives the latest relevant context.
Now, in the above example, the third query will correctly receive the second messages (in proper recency order), allowing the model to answer “what I like to do” not "What is my name"
fixes (issue)
Notes for reviewers
The fix involves updating the ORDER BY clause in the SQL query used by memory adapters to sort by timestamp in DESC order before applying LIMIT.
Verified locally with SQLite and PostgreSQL adapters using the provided test sequence.
Note: The D1 and Supabase database adapters have been updated with the corrected query logic, but have not been manually tested due to environment constraints. The change is syntactically consistent with other adapters and follows the same pattern, so it should work as expected—but extra attention during review or CI validation is appreciated.
Summary by cubic
Fixes message fetching to return the most recent messages instead of the earliest, so contextLimit provides the latest context before the current message. Applies to Cloudflare D1, libsql, Postgres, and Supabase while keeping chronological output.
Bug Fixes
Migration
Written for commit b98f802. Summary will update on new commits.
Summary by CodeRabbit
Bug Fixes
Chores