A moving list needs an ordering contract
Offset pagination can repeat or skip rows when new tasks are inserted before the current page. A history used for support or reconciliation needs predictable navigation. This guide concerns your own database-backed list, not an undocumented provider pagination feature.
What to do next
- 01
Choose a total order
Order by an immutable timestamp plus a unique task ID, or another documented immutable sequence. Include both parts in the cursor so tasks with identical timestamps still have a consistent position.
- 02
Separate refresh from continuation
Use a cursor predicate to fetch items after the last seen key. Offer a new-items indicator or explicit refresh at the top instead of injecting rows in ways that shift the page currently being reviewed.
- 03
Test concurrent inserts
Load the first page, insert several newer tasks and then fetch the next page. Assert that previously visible tasks are not repeated and older tasks are not skipped, including fixtures with equal timestamps.
A concrete example
Illustrative example: a user reviews tasks ordered by created_at and ID. Three new tasks arrive after page one. Page two continues below its saved cursor, while a separate banner offers to load the three newer items.
Before you call it ready
- Ordering includes a unique tie-breaker.
- New inserts do not shift continuation.
- Cursor data is validated by the server.
A locally executed, reproducible check
An insert shifts OFFSET pagination but a saved keyset boundary stays below the last row.
We executed this exact synthetic fixture on November 16, 2026 with Python 3.12.10 and SQLite 3.49.1. It ran locally with no network requests, account data or model generation. The recorded output below is an observation from that environment, not a platform benchmark. Copy the code into a separate Python file to repeat it in a suitable local environment; compare the mechanism and check your runtime version.
import sqlite3
c=sqlite3.connect(':memory:')
c.execute('CREATE TABLE task(id INTEGER PRIMARY KEY)')
c.executemany('INSERT INTO task VALUES(?)',[(x,) for x in (2,3,4,5)])
def ids(sql,args=()):return [r[0] for r in c.execute(sql,args)]
page1=ids('SELECT id FROM task ORDER BY id DESC LIMIT 2')
c.execute('INSERT INTO task VALUES(6)')
print('first page:',page1)
print('offset next:',ids('SELECT id FROM task ORDER BY id DESC LIMIT 2 OFFSET 2'))
print('keyset next:',ids('SELECT id FROM task WHERE id<? ORDER BY id DESC LIMIT 2',(page1[-1],)))
Recorded output and interpretation
The simplified experiment uses one unique immutable integer sort key. Real timestamp ordering needs a deterministic tie-breaker and consistent filtering. Keyset pagination alone does not freeze edits or deletions or make GET /usage a complete history.
A matching local output establishes only the illustrated library or database behavior under these inputs. It does not certify a production integration. Preserve the approved action, authorization boundaries and current AISwapLab request contract when applying the idea.
first page: [5, 4]
offset next: [4, 3]
keyset next: [3, 2]
When to stop or choose another method
Stop promising a complete task history using only a bounded recent-jobs response or an unstable offset-based view under concurrent inserts.
