Skip to content

Conversation

@jchuder
Copy link

@jchuder jchuder commented Jan 9, 2026

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
Contributor

github-actions bot commented Jan 9, 2026

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
Contributor

github-actions bot commented Jan 9, 2026

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

No duplicate PRs found

@jchuder jchuder force-pushed the fix/session-messages-sort-by-time branch from a5c3f07 to 384c171 Compare January 9, 2026 05:18
@fwang fwang requested a review from adamdotdevin as a code owner January 9, 2026 06:32
@fwang fwang force-pushed the fix/session-messages-sort-by-time branch from 659712c to e41f5a3 Compare January 9, 2026 14:26
@fwang fwang force-pushed the fix/session-messages-sort-by-time branch from e41f5a3 to 3c2d868 Compare January 9, 2026 14:33
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.

opencode export outputs messages in wrong order for long sessions

3 participants