Skip to content

Let the Knowledge list go past the newest 200 records - #447

Merged
abhishekgahlot2 merged 1 commit into
useagenthq:mainfrom
officialasishkumar:feature/knowledge-load-more
Nov 13, 2026
Merged

abhishekgahlot2 merged 1 commit into
useagenthq:mainfrom
officialasishkumar:feature/knowledge-load-more

Conversation

@officialasishkumar

@officialasishkumar officialasishkumar commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

By opening this pull request you agree to the Contributor License Agreement. On your first pull request a bot asks you to sign it with a one-line comment.

Fixes #211

GET /api/knowledge answered at most 200 records, pinned first and then newest first, with no cursor. Older unpinned records fell outside the Knowledge list, and the page had no way to reach them.

What changed

GET /api/knowledge pages (backend/src/knowledge/)

  • New query parameters: limit (1 to 500, default 200, the bounds listRecords already had) and cursor. The response now carries has_more and next_cursor next to records.
  • Send next_cursor back as cursor, with the same q and limit, to get the records after the page. Without a cursor the first page is the same 200 as before, so current callers and the released web client see no change.
  • listRecordPage in store.ts replaces listRecords (the route was its only caller). It keeps the order the issue asks for, pinned DESC, created_at DESC, with id DESC added so a page cut between equal timestamps is stable, and reads one row past limit to know whether another page follows. Every key sorts descending, so "after the cursor" is one row-value comparison, (pinned, created_at, id) < (cursor), which the existing knowledge_org_list index (org_id, pinned DESC, created_at DESC) serves.
  • The cursor (list-cursor.ts) is the last record's pinned, created_at as whole microseconds since the epoch, and id, as base64url JSON. Microseconds come from Postgres and go back as 'epoch' + n microseconds, the way the thread relationships cursor does it, so records created within one millisecond are neither repeated nor skipped.
  • A malformed cursor answers 400 {"error":"cursor is invalid"}. The query stays scoped to the caller's workspace, so a cursor carries nothing across workspaces.
  • The q keyword filter pages the same way.

Knowledge page (frontend/app/(library)/knowledge/)

  • The server-rendered page carries the cursor (fetchKnowledge reads next_cursor; a missing or empty one means the last page).
  • While the backend reports more, a Show older knowledge button sits under the list, styled like the Artifacts page's Show older files. It appears once every loaded row is showing, so it never sits next to the existing "Show N more" disclosure. Each click appends the next page without repeating a record already shown, and opens the loaded rows so the new ones are visible. A second click while a page loads sends nothing.
  • A failed page shows "Couldn't load older knowledge." (role="alert"). The button becomes Try again and resends the same cursor, and the records already shown stay.
  • Refreshes (after adding knowledge or an upload):
    • Before any older page is loaded, a refresh replaces the list as before and takes the newest page's cursor.
    • Once one is loaded, a refresh puts the newest page in front and keeps every other loaded record after it (mergeNewestPage). The cursor of the next older page is kept.
  • With a folder filter and more to load, the empty line reads "Nothing in Global among the loaded records." instead of "yet".
  • Search (3 or more characters) is unchanged. It searches the whole corpus on the server, and the button stays hidden while searching.

Docs: the GET /api/knowledge row in docs-site/docs/api/knowledge.mdx, and a line on Show older knowledge in docs-site/docs/product/knowledge-and-wiki.mdx.

Across a release: an old web client ignores the new fields. A new client talking to an old backend gets no next_cursor, so the button stays hidden.

One limit: once older pages are loaded, a refresh re-reads only the newest page. A record changed elsewhere that sits outside that page (pinned or unpinned in another tab, say) keeps its old state until a reload. That is the same trade-off as Show older files on the Artifacts page.

Tests

backend/test/knowledge-pages.test.ts, through the real route and database. The expected order comes from Postgres directly, not from the code under test.

  • 230 records with 2 pinned: the first page has 200, pinned first, without the oldest record. Paging gives 200 and 30, record 201 is "Record 30", the last is "Record 0", and the ids match Postgres' order with no gap or repeat
  • 40 records with three pinned, ten sharing one exact timestamp and eight one microsecond apart: pages of 1, 4 and 7 match the one-shot order
  • a q filter pages 10 matches as 4, 4, 2, newest first
  • limit is held between 1 and 500, and a non-number gives 200
  • nine malformed cursors (garbage, wrong shape, a string for pinned, a number or negative or too-large microsecond count, a non-uuid id) each answer 400
  • a cursor from another workspace lists only the caller's own records

backend/test/knowledge.test.ts moves to listRecordPage.

Frontend:

  • knowledge-gallery.interaction.test.tsx mounts KnowledgeGallery under happy-dom with a stubbed fetch:
    • two older pages appended, a repeated record dropped, the button gone at the end
    • no button on a single page
    • no button until "Show 5 more" opens the loaded rows
    • a 503 shows the alert and keeps the rows, and Try again resends the same cursor
    • a refresh before any older page takes the newest page's cursor
    • a double click sends one request
  • knowledge-api.test.ts: the request paths, a missing, empty or non-string cursor read as the last page, a failed answer thrown, mergeNewestPage and appendOlderPage

Checks with Bun 1.3.14, backend tests on pgvector/pgvector:0.8.6-pg16-bookworm:

  • cd backend && bun run test: 3639 pass, 0 fail, then the Slack suite 119 pass, 0 fail
  • bun run typecheck (root): passes
  • cd frontend && bun run test: 2012 pass, 0 fail
  • cd frontend && bun run lint: 248 warnings and 22 infos, the same as main
  • cd frontend && bun run build: passes
  • cd docs-site && bunx blume validate: no broken links
  • bun test scripts/agent-docs.test.ts: 5 pass

End to end

I ran a real backend on its own Postgres and a production build of the frontend (next start), signed in, and added 230 knowledge records, "Fact 0" (oldest) to "Fact 229", with "Fact 3" and "Fact 150" pinned.

  • /knowledge shows Pinned 2 (Fact 150, Fact 3) and All knowledge 198, the first 30 rows, and "Show 168 more". Before this change, Fact 0 to Fact 30 could not be reached on the page.
  • Show 168 more shows down to "Fact 31", with Show older knowledge under the list.
  • Show older knowledge sends one GET /api/knowledge?cursor=…. All knowledge becomes 228, the list ends at "Fact 0", and the button is gone.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

GET /api/knowledge answered at most 200 records, pinned first and then
newest first, with no cursor, so older unpinned records could never be
reached from the Knowledge page.

The list now pages:

- listRecordPage (backend/src/knowledge/store.ts) replaces listRecords,
  whose only caller was the route. It keeps pinned DESC, created_at DESC,
  adds id DESC so a page cut between equal timestamps is stable, and reads
  one row past the limit to know whether another page follows. Every key
  sorts descending, so "after the cursor" is one row-value comparison.
- GET /api/knowledge takes limit (1 to 500, default 200) and cursor, and
  answers has_more and next_cursor next to records. The cursor
  (list-cursor.ts) is the last record's pinned flag, created_at as whole
  microseconds, and id; a malformed one answers 400 "cursor is invalid".
  Without a cursor the first page is the same 200 as before.
- The Knowledge page carries the cursor from the server render. Once every
  loaded row is showing, Show older knowledge appends the next page
  without repeating a record. A failed page says so and Try again resends
  the same cursor; a second click while a page loads sends nothing. After
  an older page is loaded, a refresh puts the newest page in front and
  keeps the rest. With a folder filter and more to load, the empty line
  says "among the loaded records".

An old web client ignores the new fields; a new one talking to an old
backend gets no next_cursor and shows no button.

Docs: the GET /api/knowledge row and a line on Show older knowledge.
@abhishekgahlot2
abhishekgahlot2 merged commit 3933267 into useagenthq:main Nov 13, 2026
22 checks passed
@abhishekgahlot2

Copy link
Copy Markdown
Contributor

Merged, thanks @officialasishkumar. Marking older pages loaded before the fetch and merging the newest page once they exist keeps every record through a refresh, and loading only on click keeps it bounded. Nicely done.

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let the Knowledge list go past the newest 200 records

2 participants