Skip to content

fix(pagination): upgrade cursor encoding and SQL filtering for identi… - #1382

Closed
simar2411 wants to merge 1 commit into
knoxiboy:mainfrom
simar2411:fix/cursor-pagination-skips-1335
Closed

fix(pagination): upgrade cursor encoding and SQL filtering for identi…#1382
simar2411 wants to merge 1 commit into
knoxiboy:mainfrom
simar2411:fix/cursor-pagination-skips-1335

Conversation

@simar2411

@simar2411 simar2411 commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

User description

…cal createdAt timestamps (#1335)

Description

Related Issue

Closes #

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Documentation update (README, guides, comments)
  • Style / UI change (no logic change)
  • Code refactor (no behavior change)
  • Test addition or update
  • Breaking change (fix or feature that would cause existing functionality to change)

Screenshots (if UI change)

Before After
(screenshot) (screenshot)

How Has This Been Tested?

  • Tested locally with npm run dev
  • Verified on mobile viewport (375px)
  • Verified on desktop viewport (1440px)

Checklist

  • I have tested my changes locally (npm run dev)
  • My code follows the existing code style (TypeScript, Tailwind, no any types)
  • I have not introduced unrelated changes (each PR should address one issue)
  • I have added comments where necessary
  • My branch is up to date with main
  • I have linked the related issue above
  • Screenshots are included (if this is a UI change)

CodeAnt-AI Description

Prevent cursor pagination from skipping items with identical creation times

What Changed

  • Cursor pagination now uses both creation time and item ID to continue exactly after the last item, including when several items share the same timestamp
  • Existing comma-, pipe-, and timestamp-only cursors continue to work
  • Invalid or incomplete cursors safely fall back without causing request errors
  • Added coverage for cursor compatibility, timestamp aliases, and invalid values

Impact

✅ Fewer skipped items across paginated feeds
✅ Reliable pagination for identical creation timestamps
✅ Safer handling of legacy and invalid cursors

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

Summary by CodeRabbit

  • Bug Fixes
    • Improved cursor-based pagination reliability for doubt listings.
    • Added support for legacy cursors and timestamp-only cursors.
    • Improved handling of malformed or invalid cursor values.
    • Preserved accurate ordering when multiple items share the same timestamp.

@vercel

vercel Bot commented Aug 10, 2026

Copy link
Copy Markdown

@Kritika200520 is attempting to deploy a commit to the Karan Mani Tripathi 's projects Team on Vercel.

A member of the Team first needs to authorize it.

@codeant-ai

codeant-ai Bot commented Aug 10, 2026

Copy link
Copy Markdown

🤖 CodeAnt AI — Review Status

Status Commit Started (UTC) Finished (UTC)
✅ Reviewed your PR 9b0f2f8 Aug 10, 2026 · 16:56 16:58

@codeant-ai

codeant-ai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@github-actions github-actions Bot added gssoc'26 GSSoC program issue level:intermediate Intermediate level task type:bug Bug fix type:docs Documentation update labels Aug 10, 2026
@codeant-ai codeant-ai Bot added the size:M This PR changes 30-99 lines, ignoring generated files label Aug 10, 2026
@github-actions github-actions Bot added size/m review-needed and removed size:M This PR changes 30-99 lines, ignoring generated files labels Aug 10, 2026
@github-actions
github-actions Bot requested a review from knoxiboy August 10, 2026 16:57
@github-actions

Copy link
Copy Markdown

@coderabbitai review
@codeantai review

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

No new commits to review since the last review.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: ce1eb34f-8dec-4864-bcea-8b599ed3ed66

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

The pagination cursor contract now supports comma-separated encoding, legacy pipe-separated cursors, and timestamp-only cursors. The doubts API applies composite or timestamp-only keyset boundaries based on the decoded cursor.

Changes

Cursor pagination

Layer / File(s) Summary
Cursor contract and decoding
src/lib/pagination.ts, src/__tests__/lib/pagination.test.ts
DecodedCursor now exposes timestamp and a nullable id. Encoding uses commas, while decoding accepts comma, pipe, and timestamp-only formats. Tests cover valid aliases and malformed dates, IDs, and payloads.
Doubts API keyset filtering
src/app/api/doubts/route.ts
The route reads timestamp with a createdAt fallback. It applies (createdAt, id) filtering when an ID exists and a timestamp-only boundary otherwise.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related issues

  • knoxiboy/DoubtDesk issue 1335: The cursor format and doubts-route filtering changes address timestamp–ID composite pagination and ordering for identical createdAt values.

Possibly related PRs

Suggested labels: backend

Suggested reviewers: madsysharma

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main changes to cursor encoding and SQL filtering for identical timestamps.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

@github-actions[bot] I will review pull request #1382. I am replying to the latest comment only because the full earlier comment chain is unavailable.

⚠️ Action not completed

Already reviewed.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

const cursorRequested = searchParams.has("cursor");
const decodedCursor = decodeCursor(searchParams.get("cursor"));
const cursorCreatedAt = decodedCursor?.createdAt ?? null;
const cursorCreatedAt = decodedCursor?.timestamp ?? decodedCursor?.createdAt ?? null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Malformed non-empty cursors decode to null, making cursorCreatedAt null, but useCursor remains true because it depends only on the presence of the cursor parameter. The request therefore silently returns the first cursor-mode page and may issue a new cursor instead of falling back to offset pagination as documented. Require a valid decoded cursor before enabling cursor mode, or explicitly reject invalid cursors. [api mismatch]

Severity Level: Major ⚠️
- ⚠️ Malformed cursor requests select the wrong pagination mode.
- ❌ Pinned-first ordering is bypassed for affected requests.
- ⚠️ Clients may receive an unexpected replacement cursor.

Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** src/app/api/doubts/route.ts
**Line:** 158:158
**Comment:**
	*Api Mismatch: Malformed non-empty cursors decode to `null`, making `cursorCreatedAt` null, but `useCursor` remains true because it depends only on the presence of the `cursor` parameter. The request therefore silently returns the first cursor-mode page and may issue a new cursor instead of falling back to offset pagination as documented. Require a valid decoded cursor before enabling cursor mode, or explicitly reject invalid cursors.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

lt(doubtsTable.id, cursorId),
),
)
: lt(doubtsTable.createdAt, cursorCreatedAt)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Legacy timestamp-only cursors produce cursorId === null, so this branch filters with only createdAt < cursorCreatedAt. Because cursor ordering is (createdAt DESC, id DESC), every row sharing the boundary timestamp is skipped, including rows that should follow the legacy cursor according to the id tiebreaker. Legacy cursors should either be migrated/rejected or handled with a defined inclusive boundary strategy. [off-by-one]

Severity Level: Major ⚠️
- ❌ Legacy pagination can omit doubts sharing timestamps.
- ⚠️ Users may never see skipped feed rows.
- ⚠️ Results differ from composite keyset ordering.

Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** src/app/api/doubts/route.ts
**Line:** 253:253
**Comment:**
	*Off By One: Legacy timestamp-only cursors produce `cursorId === null`, so this branch filters with only `createdAt < cursorCreatedAt`. Because cursor ordering is `(createdAt DESC, id DESC)`, every row sharing the boundary timestamp is skipped, including rows that should follow the legacy cursor according to the id tiebreaker. Legacy cursors should either be migrated/rejected or handled with a defined inclusive boundary strategy.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib/pagination.ts`:
- Around line 42-53: Update decodeCursor() to reject normalized invalid calendar
dates by requiring createdAt.toISOString() to exactly match the raw timestamp
string for both composite cursors using iso and legacy timestamp-only cursors
using decoded. Preserve the existing invalid-date, integer-id, and empty-id
validation, and add regression coverage for values such as
2026-02-30T00:00:00.270Z.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 46566d2b-28cb-4702-aa16-cf9d178a20aa

📥 Commits

Reviewing files that changed from the base of the PR and between cc15691 and 9b0f2f8.

📒 Files selected for processing (3)
  • src/__tests__/lib/pagination.test.ts
  • src/app/api/doubts/route.ts
  • src/lib/pagination.ts

Comment thread src/lib/pagination.ts
Comment on lines +42 to +53
const createdAt = new Date(iso);
const id = Number(idRaw);
if (Number.isNaN(createdAt.getTime()) || !Number.isInteger(id) || idRaw.trim() === "") {
return null;
}
return { createdAt, timestamp: createdAt, id };
}

// Support legacy single-field timestamp cursors without an id component
const createdAt = new Date(decoded);
if (!Number.isNaN(createdAt.getTime())) {
return { createdAt, timestamp: createdAt, id: null };

Copy link
Copy Markdown
Contributor

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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Verify that invalid-calendar-date regression coverage exists after the fix.
rg -n -C 3 '2026-02-30|invalid date' src/__tests__/lib/pagination.test.ts

Repository: knoxiboy/DoubtDesk

Length of output: 468


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Files:"
git ls-files | rg '(^|/)pagination(\.test)?\.ts$|(^|/)route\.ts$|doubts' || true

echo
echo "pagination outline:"
ast-grep outline src/lib/pagination.ts --view expanded || true

echo
echo "pagination.ts lines 1-120:"
cat -n src/lib/pagination.ts | sed -n '1,120p'

echo
echo "pagination.test.ts relevant tests:"
cat -n src/__tests__/lib/pagination.test.ts | sed -n '1,140p'

echo
echo "cursor consumers:"
rg -n -C 4 'decodeCursor|timestamp|createdAt' src | sed -n '1,200p'

echo
echo "Behavioral probe for invalid calendar dates in available runtime:"
node - <<'JS'
const samples = [
  "2026-02-30T00:00:00.270Z",
  "2026-02-30T23:59:59.999Z",
  "2026-01-32T00:00:00.000Z",
];
for (const s of samples) {
  const d = new Date(s);
  console.log(`${s} => ${d.getTime()} ISO=${d.toJSON()} timeString=${d.toString()}`);
}
JS

Repository: knoxiboy/DoubtDesk

Length of output: 23410


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Cursor consumers around decodeCursor calls:"
rg -n -C 8 'decodeCursor\(' src | sed -n '1,240p'

echo
echo "Relevant pagination tests end:"
cat -n src/__tests__/lib/pagination.test.ts | sed -n '71,120p'

Repository: knoxiboy/DoubtDesk

Length of output: 9435


Reject normalized invalid calendar dates.

decodeCursor() currently accepts "2026-02-30T00:00:00.000Z" because new Date() normalizes it to March 2, 2026. The route then uses that later boundary in the (createdAt, id) keyset predicate, which can skip valid rows. Reject timestamps whose toISOString() differs from the raw decoded date string for both composite and legacy timestamp-only cursors, and add regression cases like "2026-02-30T00:00:00.270Z".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/pagination.ts` around lines 42 - 53, Update decodeCursor() to reject
normalized invalid calendar dates by requiring createdAt.toISOString() to
exactly match the raw timestamp string for both composite cursors using iso and
legacy timestamp-only cursors using decoded. Preserve the existing invalid-date,
integer-id, and empty-id validation, and add regression coverage for values such
as 2026-02-30T00:00:00.270Z.

@simar2411 simar2411 closed this Aug 11, 2026
@github-actions github-actions Bot removed gssoc'26 GSSoC program issue level:intermediate Intermediate level task type:bug Bug fix type:docs Documentation update size/m review-needed labels Aug 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants