[PR #7425] fix(session): sort messages by time.created instead of alphabetical order #12396

Open
opened 2026-02-16 18:17:18 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7425

State: open
Merged: No


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
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7425 **State:** open **Merged:** No --- ### 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
yindo added the pull-request label 2026-02-16 18:17:18 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12396