fix(site/src/pages/AgentsPage): order chat transcript by message id by ibetitsmike · Pull Request #27620 · coder/coder · GitHub
Skip to content

fix(site/src/pages/AgentsPage): order chat transcript by message id - #27620

Merged
ibetitsmike merged 3 commits into
mainfrom
mike/chat-message-order/frontend-comparator
Jul 29, 2026
Merged

fix(site/src/pages/AgentsPage): order chat transcript by message id#27620
ibetitsmike merged 3 commits into
mainfrom
mike/chat-message-order/frontend-comparator

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Context

Follow-up to #27495 (append-order guarantee for chat_messages.id) and #27619 (prompt query ordering), both merged. This PR applies the same id ordering to the transcript the user actually sees.

Why?

buildOrderedMessageIDs in chatStore.ts sorted by created_at, which is now() and therefore shared by every row in an insert batch. It builds orderedMessageIDs, which is what the transcript renders, so it re-imposed the ordering the backend PRs remove.

The failure needs the merge path, not a plain fetch. Initial REST hydration already sorts by numeric id before reaching the store, and Array.prototype.sort is stable, so a single correctly ordered response rendered correctly. But upsertDurableMessages copies the existing message Map, appends new ids, and re-sorts. Map iteration is insertion ordered, so when a refetch or reconnect merges earlier ids into a map that already holds later ones, the stable timestamp sort faithfully preserves the wrong order.

This also makes the store consistent with useChatStore.ts and api/queries/chatMessageEdits.ts, which already sort by id.

Changes

buildOrderedMessageIDs now calls toSorted with an id comparator inlined at its single call site, and the byMessageCreatedAt helper is gone. ChatMessage.id is a number in typesGenerated.ts, backed by a Go int64, so numeric subtraction is correct.

Testing

Two vitest cases, both verified red by restoring the timestamp comparator:

  • sorts messages by id when created_at disagrees with append order returned [2,1].
  • orders merged messages by id rather than by arrival returned [3,4,1,2], the exact inversion the merge path produces.

MergedMessagesRenderInIDOrder in ChatPageContent.stories.tsx covers the same merge path through the rendered timeline.

All 334 ChatConversation unit tests and the 4 ChatPageContent storybook interaction tests pass, and tsc -p . plus biome are clean.

Opened by Mux on behalf of Mike.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d0f2c2c3da

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/prompt-query branch from 8500551 to 56f7161 Compare July 28, 2026 22:03
@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/frontend-comparator branch from d0f2c2c to 4a334b5 Compare July 28, 2026 22:03
@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/prompt-query branch from 56f7161 to d2ce637 Compare July 28, 2026 22:58
@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/frontend-comparator branch from 4a334b5 to 2a89408 Compare July 28, 2026 22:58
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2a89408243

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread site/src/pages/AgentsPage/components/ChatConversation/chatStore.ts Outdated
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9611ca53f3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread site/src/pages/AgentsPage/components/ChatPageContent.stories.tsx Outdated
@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/frontend-comparator branch from 9611ca5 to 8a028d8 Compare July 28, 2026 23:27
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 8a028d8985

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/prompt-query branch from d2ce637 to 3ee593d Compare July 29, 2026 07:05
@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/frontend-comparator branch from 8a028d8 to 1760a47 Compare July 29, 2026 07:05
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Restacked onto the rebased parents, which moved to current main and renumbered the stack's migration to 000559. No content changes here: git range-diff reports all three commits identical. Revalidated tsc --noEmit, biome, and the 50 chat store unit tests.

Mux is working on Mike's behalf.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 1760a47c68

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/prompt-query branch from 3ee593d to d974c9e Compare July 29, 2026 07:31
@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/frontend-comparator branch from 1760a47 to be431d5 Compare July 29, 2026 07:31
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: be431d5dae

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/frontend-comparator branch from be431d5 to e54aa9a Compare July 29, 2026 08:08
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: e54aa9a808

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread site/src/pages/AgentsPage/components/ChatConversation/chatStore.ts Outdated
Base automatically changed from mike/chat-message-order/prompt-query to main July 29, 2026 12:05
@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/frontend-comparator branch from e54aa9a to 543c648 Compare July 29, 2026 12:20
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Comment thread site/src/pages/AgentsPage/components/ChatConversation/chatStore.ts Outdated
buildOrderedMessageIDs sorted by created_at, which is the transaction start
time and is shared across an insert batch. Because the sort is stable, a merge
that appends newly fetched lower ids to the existing message map preserved the
map order, rendering a later batch ahead of an earlier one.
…rybook

The comparator fix only had store-level coverage. This renders a merge that
appends lower ids after higher ones, all sharing one created_at, and asserts
the rendered rows follow id order.
@ibetitsmike
ibetitsmike force-pushed the mike/chat-message-order/frontend-comparator branch from 543c648 to c3ab8f0 Compare July 29, 2026 12:35
@ibetitsmike
ibetitsmike enabled auto-merge (squash) July 29, 2026 12:37
@ibetitsmike
ibetitsmike merged commit fb30674 into main Jul 29, 2026
29 of 30 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-message-order/frontend-comparator branch July 29, 2026 12:43
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 29, 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.

2 participants