Skip to content

feat(session): bi-directional cursor-based pagination (#6548)#8535

Open
CasualDeveloper wants to merge 1 commit into
anomalyco:devfrom
CasualDeveloper:feat/6548-message-pagination
Open

feat(session): bi-directional cursor-based pagination (#6548)#8535
CasualDeveloper wants to merge 1 commit into
anomalyco:devfrom
CasualDeveloper:feat/6548-message-pagination

Conversation

@CasualDeveloper
Copy link
Copy Markdown
Contributor

@CasualDeveloper CasualDeveloper commented Jan 14, 2026

Issue for this PR

Closes #6548

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds bidirectional cursor pagination for session messages across the server, app, TUI, experimental HttpApi routes, and generated SDK/OpenAPI types.

This keeps large sessions usable by loading bounded message windows, using RFC 8288 Link headers for older/newer pages, and preserving revert/restore correctness through server-backed revert preview metadata when boundary messages are outside the loaded page.

How did you verify your code works?

Verified locally with package-local typechecks and targeted tests:

  • ./packages/sdk/js/script/build.ts
  • bun typecheck in packages/opencode, packages/app, and packages/sdk/js
  • bun test --preload ./happydom.ts ./src/context/revert-page.test.ts in packages/app
  • bun test --timeout 120000 test/server/httpapi-session.test.ts in packages/opencode
  • bun test --timeout 120000 test/server/session-messages.test.ts in packages/opencode, with and without auth env
  • bun test --timeout 120000 test/server/session-select.test.ts in packages/opencode, with and without auth env
  • bun test --timeout 120000 test/session/messages-pagination.test.ts test/cli/tui/pagination-helpers.test.ts test/util/parse-link-header.test.ts in packages/opencode
  • git diff --check dev..HEAD

Screenshots / recordings

Not included. This changes session loading/scroll behavior rather than visual styling; the behavior is covered by the targeted tests above.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate Found

PR #6656: "session: paginate message loading"

This is not a duplicate in the traditional sense—it's a more complete implementation that builds upon and replaces the earlier approach.

@CasualDeveloper CasualDeveloper changed the title feat(session): add bi-directional cursor-based pagination for messages feat(session): bi-directional cursor-based pagination (#6548) Jan 14, 2026
@CasualDeveloper
Copy link
Copy Markdown
Contributor Author

@rekram1-node hi Aiden, would appreciate your feedback on how I implemented pagination, per your suggestion! :)

@ry2009
Copy link
Copy Markdown
Contributor

ry2009 commented Jan 14, 2026

Glad I could help kickstart this, I have been a little busy to continue my OSS journey right now. I haven't personally looked through the code in depth (just did a skim) but 1k line changes seems like a lot for one PR (might be better to split it if you can) but once again maybe the code is super clean and simple -- I just haven't sifted through deeply lol. Great work & good luck closing this!

@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch 6 times, most recently from 4f57718 to afe9c53 Compare January 15, 2026 06:35
@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch 3 times, most recently from 4bd8bf6 to 34eea60 Compare January 15, 2026 17:21
@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch from 34eea60 to 2c70c15 Compare January 17, 2026 05:23
@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch from 2c70c15 to adec748 Compare January 17, 2026 10:15
@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch from adec748 to 01e84ef Compare January 17, 2026 10:46
@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch from 01e84ef to ea25515 Compare January 18, 2026 18:59
@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch from ea25515 to 4cb0df1 Compare January 18, 2026 19:23
@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch 3 times, most recently from 34ac66b to 7e43217 Compare February 18, 2026 12:11
@CasualDeveloper
Copy link
Copy Markdown
Contributor Author

CasualDeveloper commented Feb 18, 2026

PR updated to work with new sqlite-backed message storage in latest OpenCode dev commit (currently 6cd3a59, several commits newer then v1.2.6)

@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch from 7e43217 to 6deeeb6 Compare February 18, 2026 14:13
@alberti42
Copy link
Copy Markdown
Contributor

I don't know if this is useful feedback, but I love the new feature. I have been running with it for the past several days. It never failed even once. But I see there were still edge cases not covered. I am looking forward to this getting upstream. ❤️

@alberti42
Copy link
Copy Markdown
Contributor

Is there any update on this PR? Where is it hanging? I use it daily. Thank you, @CasualDeveloper, for this super useful extension. So often, I need to fork from the first message..

@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch from d12668b to fb9cc15 Compare March 8, 2026 18:50
@CasualDeveloper
Copy link
Copy Markdown
Contributor Author

Hi @alberti42, I think this one has been hanging because it's a big change and unfortunately I haven't had an opportunity to sit down with the maintainers to follow up and go over the work in this feature (I got swamped with real job stuff lol)

@alberti42
Copy link
Copy Markdown
Contributor

Yeah, I totally understand. Same here; sometimes there is really no time for fun projects like this one. Thank you a lot for the super important contribution.

The current implementation is no longer compatible with the main branch, right? I tried to rebase, but there were several conflicts. So, I stopped updating while waiting for this feature to be properly handled. I think some of the maintainers should help you or take over because this is a central must-have feature.

@CasualDeveloper CasualDeveloper force-pushed the feat/6548-message-pagination branch 2 times, most recently from 1c805dd to a86cd02 Compare March 27, 2026 07:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds robust bi-directional, cursor-based pagination for session messages across the server/API, SDK, app, and TUI to make long sessions navigable while keeping message memory bounded.

Changes:

  • Implement before / after / oldest cursor pagination for session messages and emit RFC 8288 Link headers.
  • Update SDK types/client to support new pagination params + header-based navigation.
  • Wire TUI (and app prefetch/sync) to use boundary-aware loading with a capped in-memory message window; add/adjust regression tests.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
packages/sdk/openapi.json Adds after/oldest query params and documents Link pagination response header.
packages/sdk/js/src/v2/gen/types.gen.ts Regenerates request types to include after/oldest for session messages.
packages/sdk/js/src/v2/gen/sdk.gen.ts Regenerates SDK client to send after/oldest query params.
packages/opencode/src/util/link-header.ts Adds RFC 8288 Link header parsing utilities for rel URLs + query param extraction.
packages/opencode/src/session/message-v2.ts Implements bidirectional cursor pagination in storage layer (before/after/oldest) and updates streaming to use new cursors.
packages/opencode/src/session/index.ts Adjusts DB import set (minor refactor).
packages/opencode/src/server/routes/session.ts Extends /session/:sessionID/message to validate new params and return RFC 8288 Link headers for prev/next pages.
packages/opencode/src/cli/cmd/tui/util/pagination.ts Adds TUI pagination helper utilities (ordering, insertion, eviction, edge hints, error normalization).
packages/opencode/src/cli/cmd/tui/routes/session/index.tsx Updates TUI session view to support boundary-aware loading and new scroll behavior, plus minor UI tweaks.
packages/opencode/src/cli/cmd/tui/context/sync.tsx Implements cursor-based message paging state, capped message windowing (500), and load older/newer/jump behaviors using Link headers.
packages/opencode/src/cli/cmd/tui/context/keybind.tsx Hardens keybind parsing for non-string config values.
packages/opencode/test/util/parse-link-header.test.ts Adds unit tests for new Link header parsing utilities.
packages/opencode/test/cli/tui/pagination-helpers.test.ts Adds unit tests for TUI pagination helper/edge-state logic.
packages/opencode/test/session/messages-pagination.test.ts Reworks session pagination regression tests for new cursor semantics and ordering rules.
packages/opencode/test/server/session-messages.test.ts Updates server API tests to validate Link header pagination (prev/next), param validation, and limit boundary behavior.
packages/opencode/test/server/session-select.test.ts Refactors request/auth helper usage for endpoint tests.
packages/app/src/pages/layout.tsx Switches app-side prefetch cursor extraction from X-Next-Cursor to parsing Link header (rel=prev).
packages/app/src/context/sync.tsx Switches app-side sync cursor extraction from X-Next-Cursor to parsing Link header (rel=prev).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/opencode/src/server/instance/session.ts Outdated
Comment thread packages/opencode/src/server/instance/session.ts Outdated
Comment thread packages/opencode/src/session/index.ts Outdated
Comment thread packages/opencode/src/cli/cmd/tui/routes/session/index.tsx Outdated
Comment thread packages/opencode/src/cli/cmd/tui/routes/session/index.tsx Outdated
Comment thread packages/opencode/src/cli/cmd/tui/routes/session/index.tsx
Comment thread packages/opencode/test/server/session-select.test.ts Outdated
Comment thread packages/opencode/test/server/session-select.test.ts Outdated
Comment thread packages/opencode/test/server/session-messages.test.ts Outdated
Comment thread packages/opencode/test/session/messages-pagination.test.ts Outdated
@CasualDeveloper
Copy link
Copy Markdown
Contributor Author

Fork updated to sync with dev (as of cb1a500).

@Davids048
Copy link
Copy Markdown

Hi, thank you very much for building this feature. It is very important for long context works. Is there any update on the progress of this feature?

@CasualDeveloper
Copy link
Copy Markdown
Contributor Author

Hi, thank you very much for building this feature. It is very important for long context works. Is there any update on the progress of this feature?

for now i've just been keeping updated in my local fork of opencode, regularly rebasing and refactoring as needed to make it work on dev

you might need to manually set up my fork as a source and pull the same way if you really needed it

@alberti42
Copy link
Copy Markdown
Contributor

@Davids048 I am doing exactly this, and I am very thankful that @CasualDeveloper keeps rebasing. I have a few other branches of mine with fixes, and usually I rebase them every time there is an update from this feature.

I am not sure why OpenCode does not help here to push it through. It is nearly a dealbreaker for me not being able to get back to the beginning of a given chat.

They probably have many other projects they are currently following.

@CasualDeveloper
Copy link
Copy Markdown
Contributor Author

@alberti42 thanks for the compliment! :D

i think it's probably because the feature is so huge and the team is busy with refactoring a lot of stuff everywhere else lol

Add bidirectional cursor pagination with Link headers across the server, app, TUI, and experimental HttpApi surfaces. Keep large-history revert and restore flows correct with server-backed revert previews, bounded client windows, and paged boundary hydration.
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.

[FEATURE]: Paginated message loading for long sessions

6 participants