Skip to content

fix(session): sort messages by time.created instead of alphabetical order - #7425

Closed
jchuder wants to merge 9 commits into
anomalyco:devfrom
jchuder:fix/session-messages-sort-by-time
Closed

fix(session): sort messages by time.created instead of alphabetical order#7425
jchuder wants to merge 9 commits into
anomalyco:devfrom
jchuder:fix/session-messages-sort-by-time

Conversation

@jchuder

@jchuderjchuder commented Jan 9, 2026

Copy link
Copy Markdown

What does this PR do?

Fixes Session.messages() to sort messages by time.created timestamp instead of relying on alphabetical message ID order.

Fixes#7426

Problem

opencode export <session_id> outputs messages in wrong order for long sessions. The first part of the conversation appears in the middle or end of the export.

Root cause: Session.messages() used result.reverse() which only reverses the alphabetical order from Storage.list(). Message IDs are random strings (e.g., msg_19b9ed* vs msg_b9946c*), so alphabetical order ≠ chronological order.

Fix

Replace result.reverse() with result.sort((a, b) => a.info.time.created - b.info.time.created) to ensure messages are always sorted by creation timestamp.

How did you verify your code works?

  1. Ran lsp_diagnostics on changed file - no errors
  2. Tested export on a session with 500+ messages - output now correctly ordered by time
  3. Verified first message in export matches the actual first message of the session

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions

Copy link
Copy Markdown
Contributor

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

No duplicate PRs found

@jchuder
jchuderforce-pushed the fix/session-messages-sort-by-time branch from a5c3f07 to 384c171CompareJanuary 9, 2026 05:18
@fwang
fwangforce-pushed the fix/session-messages-sort-by-time branch from 659712c to e41f5a3CompareJanuary 9, 2026 14:26
@fwang
fwangforce-pushed the fix/session-messages-sort-by-time branch from e41f5a3 to 3c2d868CompareJanuary 9, 2026 14:33
@github-actions

Copy link
Copy Markdown
Contributor

Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR.

Sign up for freeto 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.

opencode export outputs messages in wrong order for long sessions

3 participants

@jchuder@fwang@actions-user