feat(assets): cursor-based pagination on GET /api/assets
Summary
- Adds
afterquery parameter andnext_cursorresponse field onGET /api/assets - Cursors are opaque to the client; sort direction is baked into the cursor so it can't be misapplied across
asc/descrequests
Contract
Cursor format (encoded as base64url, no padding):
JSON{"s": sort_field, "v": value, "id": id, "o": order}
The o field binds the cursor to the sort direction it was minted under — replaying a desc cursor against an asc request fails with INVALID_CURSOR rather than silently walking the wrong direction. o is mandatory on every payload.
Time values are Unix microseconds (UTC) — microsecond precision matches PostgreSQL's timestamp so a stored timestamp round-trips through the cursor exactly, with no risk of dropping rows in the same millisecond bucket.
Sort fields supported: created_at, updated_at, name, size. Passing after with sort=last_access_time returns 400 INVALID_CURSOR.
| Scenario | Behavior |
|---|---|
| Cursor sort field ≠ request sort | 400 INVALID_CURSOR |
| Cursor order ≠ request order | 400 INVALID_CURSOR |
| Malformed / oversized cursor | 400 INVALID_CURSOR |
| Last page reached | next_cursor omitted, has_more false |
| Asset deleted between pages | Subsequent cursors still valid; skip missing rows |
Implementation
- New
app/assets/services/cursor.py— opaque cursor encode/decode with wire-format caps (1024 encoded / 512 value / 128 id bytes; value cap matches theAssetReference.namecolumn) - Query layer adds a keyset WHERE clause (
field <op> v OR (field = v AND id <op> cursor_id)) and a secondaryORDER BY idfor stable tiebreak - Handler maps
InvalidCursorError→ 400INVALID_CURSOR
Tests
- Unit tests on the cursor module (
tests-unit/assets_test/services/test_cursor.py):- Encode/decode round-trip across sort field types (string, int, time, unicode)
- Microsecond precision preserved through the wire
- UTC normalization for time values
- All invalid-cursor branches: oversized cursor, not base64, not JSON, empty/oversized id, oversized value, unsupported sort field, non-dict payload, missing/mismatched order
- Per-field-cap encoding fits within the wire cap
- Byte-identity wire-format fixtures so silent drift fails the test
- Integration tests on the live HTTP endpoint (
tests-unit/assets_test/test_list_cursor.py):- Full multi-page cursor walk returns items in order with no duplicates
- Invalid cursor → 400
- Sort/cursor mismatch → 400
next_cursoromitted when no more results- Pagination stable across deletes of already-returned rows
- Existing list-filter tests pass unchanged
OpenAPI
Spec updated in openapi.yaml — after param on /api/assets, next_cursor on ListAssetsResponse.
[!NOTE]
Reviews paused
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
- [ ] ▶️ Resume reviews
- [ ] 🔍 Trigger review
📝 Walkthrough
Walkthrough
This PR adds opaque keyset cursor pagination to GET /api/assets: a new cursor module implements encode/decode and validation (with InvalidCursorError), request/response schemas gain after/next_cursor, DB queries accept typed cursor inputs and use (sort_col,id) keyset filters, the service layer decodes/validates cursors and mints next-cursors via over-fetch, the API route maps InvalidCursorError to HTTP 400, OpenAPI is updated, and unit/integration tests cover primitives and end-to-end behavior.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 38.10% 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 'feat(assets): cursor-based pagination on GET /api/assets' clearly and concisely summarizes the main change—adding cursor-based pagination to the assets API endpoint. |
| 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 | The pull request description clearly explains the purpose (cursor-based pagination), technical details (opaque base64url encoding, keyset pagination with tiebreaker), and how it relates to the changeset across multiple files. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
Comment @coderabbitai help to get the list of available commands and usage tips.