-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(api): sort /memories newest-first before pagination #995
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
Open
costajohnt
wants to merge
1
commit into
rohitg00:main
Choose a base branch
from
costajohnt:fix/990-memories-latest-sort-order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+50
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { readFileSync } from "node:fs"; | ||
|
|
||
| // api::memories returned rows in KV-insertion order (oldest first), so | ||
| // `?latest=true&limit=N` served the N OLDEST "latest" memories and the viewer | ||
| // showed stale data once the corpus exceeded the limit (#990). The handler now | ||
| // sorts newest-first (createdAt desc, fallback updatedAt) before it slices, | ||
| // mirroring the viewer-side fix that already landed for the Memories tab | ||
| // (#674/#701). | ||
| // | ||
| // Like the other api::memories tests in this repo, this asserts on the handler | ||
| // source: the SDK-registered handler is not invoked in isolation here, so the | ||
| // guard is that the sort exists, uses the agreed keys, and runs BEFORE the | ||
| // slice (slicing an unsorted list is the bug). | ||
| describe("api::memories sorts newest first before pagination (#990)", () => { | ||
| const api = readFileSync("src/triggers/api.ts", "utf-8"); | ||
| const start = api.indexOf('registerFunction("api::memories"'); | ||
| const end = api.indexOf('config: { api_path: "/agentmemory/memories"'); | ||
| const handler = api.slice(start, end); | ||
|
|
||
| it("isolates the api::memories handler source", () => { | ||
| expect(start).toBeGreaterThan(-1); | ||
| expect(end).toBeGreaterThan(start); | ||
| }); | ||
|
|
||
| it("sorts filtered memories before the offset/limit slice", () => { | ||
| const sortIdx = handler.search(/filtered\.sort\(/); | ||
| const sliceIdx = handler.search(/filtered\.slice\(offset/); | ||
| expect(sortIdx).toBeGreaterThan(-1); | ||
| expect(sliceIdx).toBeGreaterThan(-1); | ||
| // Slicing before sorting is the #990 bug — the sort must come first. | ||
| expect(sortIdx).toBeLessThan(sliceIdx); | ||
| }); | ||
|
|
||
| it("sorts on createdAt desc with updatedAt fallback (matches the #701 viewer fix)", () => { | ||
| expect(handler).toMatch(/a\.createdAt \|\| a\.updatedAt/); | ||
| expect(handler).toMatch(/b\.createdAt \|\| b\.updatedAt/); | ||
| expect(handler).toMatch(/localeCompare/); | ||
| }); | ||
| }); | ||
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.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert the comparator direction explicitly.
This still passes if the implementation flips to
aKey.localeCompare(bKey), which would reintroduce oldest-first ordering. Check forbKey.localeCompare(aKey)(or evaluate the comparator on sample timestamps) so the regression actually locks newest-first behavior.Suggested tightening
it("sorts on createdAt desc with updatedAt fallback (matches the `#701` viewer fix)", () => { expect(handler).toMatch(/a\.createdAt \|\| a\.updatedAt/); expect(handler).toMatch(/b\.createdAt \|\| b\.updatedAt/); - expect(handler).toMatch(/localeCompare/); + expect(handler).toMatch(/bKey\.localeCompare\(aKey\)/); });📝 Committable suggestion
🤖 Prompt for AI Agents