-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(webapp): use composite keyset cursor for run pagination #3852
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
matt-aitken
wants to merge
1
commit into
main
Choose a base branch
from
fix/runs-cursor-keyset-pagination
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.
+472
−30
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| area: webapp | ||
| type: fix | ||
| --- | ||
|
|
||
| Fix run pagination that could duplicate or skip runs: the query orders by `(created_at, run_id)` but the cursor cut on `run_id` alone, which diverges when run_id order doesn't match created_at order (e.g. bulk replay re-processing runs). Cursors now encode the composite key as an opaque token and cut on the matching tuple; legacy bare-run_id cursors stay supported for in-flight pagination. |
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
47 changes: 47 additions & 0 deletions
47
apps/webapp/app/services/runsRepository/runsCursor.server.ts
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,47 @@ | ||
| /** | ||
| * Cursor encoding for keyset pagination over `(created_at, run_id)`. | ||
| * | ||
| * The list query orders by the composite key `(created_at, run_id)`, so a sound | ||
| * cursor must carry BOTH components — cutting on `run_id` alone re-includes and | ||
| * skips rows whenever `run_id` order diverges from `created_at` order. | ||
| * | ||
| * A cursor is an opaque URL-safe base64 token wrapping `{ c: createdAtMs, r: | ||
| * runId }`. Cursors are server-issued (the SDK just echoes | ||
| * `pagination.next`/`previous` back), so this format needs no client update. | ||
| * | ||
| * Legacy cursors were the bare internal run_id (a cuid). They are detected by | ||
| * decode failure: a cuid base64-decodes to non-JSON bytes, so it falls through | ||
| * to `{ kind: "legacy" }` and the old (knowingly unsound) `run_id`-only | ||
| * predicate. In-flight legacy cursors keep working and drain naturally. | ||
| */ | ||
|
|
||
| import { z } from "zod"; | ||
|
|
||
| export type DecodedRunsCursor = | ||
| | { kind: "composite"; createdAt: number; runId: string } | ||
| | { kind: "legacy"; runId: string }; | ||
|
|
||
| // `c` = created_at (ms since epoch), `r` = run_id. Short keys keep the token small. | ||
| const CompositeCursor = z.object({ | ||
| c: z.number().int(), | ||
| r: z.string().min(1), | ||
| }); | ||
|
|
||
| export function encodeRunsCursor(createdAtMs: number, runId: string): string { | ||
| return Buffer.from(JSON.stringify({ c: createdAtMs, r: runId })).toString("base64url"); | ||
| } | ||
|
|
||
| export function decodeRunsCursor(cursor: string): DecodedRunsCursor { | ||
| try { | ||
| const parsed = CompositeCursor.safeParse( | ||
| JSON.parse(Buffer.from(cursor, "base64url").toString("utf8")) | ||
| ); | ||
| if (parsed.success) { | ||
| return { kind: "composite", createdAt: parsed.data.c, runId: parsed.data.r }; | ||
| } | ||
| } catch { | ||
| // JSON.parse threw — not a composite cursor. | ||
| } | ||
|
|
||
| return { kind: "legacy", runId: cursor }; | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
🚩 Prisma orderBy still uses id:desc while ClickHouse uses (created_at, run_id)
At
clickhouseRunsRepository.server.ts:182, the Prisma query orders byid: 'desc'while the ClickHouse query orders by(created_at DESC, run_id DESC). Sinceidandrun_idare the same field (Postgres PK = ClickHouse run_id), this isid DESCvs(created_at DESC, id DESC). Whencreated_atdiffers between rows, the Prisma ordering may not match the ClickHouse page ordering. This was a pre-existing issue (same orderBy existed before this PR) and the runs are displayed in a list where the frontend likely re-sorts or the difference is negligible, but it's worth noting.(Refers to lines 181-183)
Was this helpful? React with 👍 or 👎 to provide feedback.