Skip to content

fix: repair Cloudflare device cursor pagination#1941

Merged
riderx merged 3 commits into
mainfrom
codex/fix-cloudflare-device-cursor
Apr 24, 2026
Merged

fix: repair Cloudflare device cursor pagination#1941
riderx merged 3 commits into
mainfrom
codex/fix-cloudflare-device-cursor

Conversation

@riderx
Copy link
Copy Markdown
Member

@riderx riderx commented Apr 24, 2026

Summary (AI generated)

  • Fix Cloudflare /private/devices cursor pagination so nextCursor advances after per-device grouping
  • Add a regression test covering ascending and descending cursor queries on the Analytics Engine path

Motivation (AI generated)

The Cloudflare/workerd implementation applied cursor filtering before the per-device aggregation, which could repeat the same device on the next page and strand later rows behind a non-advancing cursor.

Business Impact (AI generated)

This removes a production pagination defect in device administration flows, avoiding duplicate-page loops, unreachable devices, and wasted operational work in exports or analytics workflows.

Test Plan (AI generated)

  • bunx eslint supabase/functions/_backend/utils/cloudflare.ts tests/cloudflare-device-pagination.unit.test.ts
  • bunx vitest run tests/cloudflare-device-pagination.unit.test.ts tests/cloudflare-datetime.unit.test.ts

Generated with AI

Summary by CodeRabbit

  • Refactor

    • Reorganized device query construction by extracting helper functions for query building, cursor pagination filtering, and sort order handling. Removed debug logging from the main device fetch function.
  • Tests

    • Added comprehensive unit tests for the new pagination query builder, validating cursor-based pagination behavior across different sort orders.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 24, 2026

Warning

Rate limit exceeded

@riderx has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 4 minutes and 57 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 4 minutes and 57 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3da24146-9f8a-4fc6-b807-1b48d70892aa

📥 Commits

Reviewing files that changed from the base of the PR and between 7f8644c and b41ce6c.

📒 Files selected for processing (2)
  • supabase/functions/_backend/utils/cloudflare.ts
  • tests/cloudflare-device-pagination.unit.test.ts
📝 Walkthrough

Walkthrough

The pull request refactors query construction in readDevicesCF by extracting query-building logic into three new helper functions: buildReadDevicesCFQuery for SQL generation, buildReadDevicesCFCursorFilter for pagination logic using updated_at and device_id, and getReadDevicesCFOrder for sort handling. Tests validate cursor pagination behavior.

Changes

Cohort / File(s) Summary
Query Building Refactoring
supabase/functions/_backend/utils/cloudflare.ts
Extracted inline Analytics Engine SQL construction into buildReadDevicesCFQuery. Refactored cursor pagination logic into buildReadDevicesCFCursorFilter using updated_at and device_id. Centralized sort handling in getReadDevicesCFOrder. Removed two logging calls for deviceIds and search.
Pagination Unit Tests
tests/cloudflare-device-pagination.unit.test.ts
Added two test cases validating buildReadDevicesCFQuery cursor pagination: one for descending updated_at order with WHERE (updated_at < ...) predicate, another for ascending order with WHERE (updated_at > ...) predicate.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

A rabbit hops through queries neat,
Extracting helpers, code more sweet,
Cursor filters sorted right,
With updated_at, pagination's bright! 🐰

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% 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
Title check ✅ Passed The title clearly and specifically describes the main change: fixing Cloudflare device cursor pagination, which directly aligns with the changeset's core purpose.
Description check ✅ Passed The description includes a summary and test plan (both AI-generated), but lacks comprehensive coverage of all template sections and manual testing evidence.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-cloudflare-device-cursor

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

@codspeed-hq
Copy link
Copy Markdown
Contributor

codspeed-hq Bot commented Apr 24, 2026

Merging this PR will not alter performance

✅ 28 untouched benchmarks


Comparing codex/fix-cloudflare-device-cursor (b41ce6c) with main (633aeea)

Open in CodSpeed

@riderx riderx marked this pull request as ready for review April 24, 2026 09:46
@riderx
Copy link
Copy Markdown
Member Author

riderx commented Apr 24, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 24, 2026

✅ Actions performed

Review triggered.

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
tests/cloudflare-device-pagination.unit.test.ts (1)

5-5: Prefer it.concurrent() for these independent unit tests.

Both cases are pure query-string assertions and can run safely in parallel.

♻️ Suggested change
-  it('applies descending cursor pagination after device grouping', () => {
+  it.concurrent('applies descending cursor pagination after device grouping', () => {
@@
-  it('applies ascending cursor pagination after device grouping', () => {
+  it.concurrent('applies ascending cursor pagination after device grouping', () => {

As per coding guidelines, "Use it.concurrent() instead of it() when possible to run tests in parallel within the same file, maximizing parallelism for faster CI/CD".

Also applies to: 22-22

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/cloudflare-device-pagination.unit.test.ts` at line 5, Replace the two
independent unit tests that use it(...) with it.concurrent(...) so they run in
parallel; specifically change the test with the description "applies descending
cursor pagination after device grouping" and the other standalone test at the
second occurrence in this file to use it.concurrent(...) instead of it(...),
preserving the test bodies and assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@supabase/functions/_backend/utils/cloudflare.ts`:
- Around line 708-714: The cloudlog calls currently emit raw params.deviceIds
and params.search; instead redact or summarize these sensitive fields before
logging—modify the block that calls cloudlog (referencing cloudlog,
params.deviceIds, params.search and c.get('requestId')) to log non-sensitive
representations such as deviceIds.length or a hashed/first-N-masked sample and a
truncated or sanitized summary of params.search (e.g., "<redacted>" or first 20
chars + ellipsis), ensuring no raw identifiers or user input are written to logs
while preserving useful context like the requestId.

In `@tests/cloudflare-device-pagination.unit.test.ts`:
- Around line 13-20: Add explicit assertions to the test that verify the
secondary tie-breaker on device_id is present in the cursor predicate: after
computing query, assert that the query string contains the `AND device_id >`
clause (e.g., `AND device_id > '...') and that this clause appears after the
cursor predicate (use the existing `cursorIndex` / `groupByIndex` logic if
helpful). Update both occurrences noted in the comment (the current block around
the `groupByIndex`/`cursorIndex` assertions and the analogous checks at lines
30–31) so the test fails if the `device_id` tie-breaker is omitted.

---

Nitpick comments:
In `@tests/cloudflare-device-pagination.unit.test.ts`:
- Line 5: Replace the two independent unit tests that use it(...) with
it.concurrent(...) so they run in parallel; specifically change the test with
the description "applies descending cursor pagination after device grouping" and
the other standalone test at the second occurrence in this file to use
it.concurrent(...) instead of it(...), preserving the test bodies and
assertions.
🪄 Autofix (Beta)

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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0ff7a171-d834-4d7b-8aa7-63cda2424fb7

📥 Commits

Reviewing files that changed from the base of the PR and between 633aeea and 7f8644c.

📒 Files selected for processing (2)
  • supabase/functions/_backend/utils/cloudflare.ts
  • tests/cloudflare-device-pagination.unit.test.ts

Comment thread supabase/functions/_backend/utils/cloudflare.ts
Comment thread tests/cloudflare-device-pagination.unit.test.ts
@sonarqubecloud
Copy link
Copy Markdown

@riderx riderx merged commit c260278 into main Apr 24, 2026
16 checks passed
@riderx riderx deleted the codex/fix-cloudflare-device-cursor branch April 24, 2026 12:25
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.

1 participant