Skip to content

fix(session): use index-based message ordering and harden prompt loop exit#17256

Open
sjawhar wants to merge 1 commit intoanomalyco:devfrom
sjawhar:fix/session-message-ordering
Open

fix(session): use index-based message ordering and harden prompt loop exit#17256
sjawhar wants to merge 1 commit intoanomalyco:devfrom
sjawhar:fix/session-message-ordering

Conversation

@sjawhar
Copy link

@sjawhar sjawhar commented Mar 12, 2026

Issue for this PR

Closes #11863

Type of change

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

What does this PR do?

When client and server clocks diverge, message IDs don't sort in creation order. Several places in the frontend use > / >= on IDs to determine message boundaries (restore, fork, timeline queuing), which breaks under clock skew. The prompt loop's shouldExitLoop also fails open when parentID is undefined.

This PR:

  • Replaces lexicographic ID comparisons with findIndex/indexOf on already-sorted arrays
  • Adds a sortMessages utility to packages/util (sort by time.created → role rank → id)
  • Guards shouldExitLoop against undefined parentID — exits instead of looping

How did you verify your code works?

  • bun typecheck passes in packages/opencode, packages/app, packages/util
  • bun test test/session/prompt.test.ts — 14 pass including new undefined-parentID case
  • bun test in packages/util — 3 pass including new sortMessages ordering tests
  • Added skewed-messages.ts fixture with factory helpers for clock-skewed message IDs

Screenshots / recordings

N/A — logic change, no UI modifications.

Checklist

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

… exit

Replace lexicographic ID comparisons with position-based lookups in
session restore, fork, and timeline queuing to handle clock-skewed
message IDs correctly.

Add sortMessages utility (time.created → role rank → id) to
packages/util for consistent message ordering across frontend and
backend.

Guard shouldExitLoop against missing parentID — fail-safe to exit
rather than loop indefinitely when assistant parentID is undefined.

Add skewed-message test fixtures and extend prompt/revert/session
test suites for the new ordering logic.
@sjawhar sjawhar requested a review from adamdotdevin as a code owner March 12, 2026 23:27
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 12, 2026
@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 12, 2026
@github-actions
Copy link
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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.

Web UI: client-generated message IDs cause infinite processing loop when client/server clocks diverge

1 participant