Skip to content

Commit 310e369

Browse files
Gkrumbach07Ambient Code Botclaude
authored
fix(frontend): interleave thinking messages correctly in snapshots (#891)
## Summary - Implement stable sort in handleMessagesSnapshot that preserves original order for equal timestamps - Fixes thinking blocks stacking instead of interleaving with agent messages ## Test plan - [x] Frontend unit tests pass (471 passed) - [x] ESLint passes - [x] Build succeeds - [ ] Manual test: session with thinking, verify interleaved display on reconnect Fixes: RHOAIENG-52655 🤖 Generated with [Claude Code](https://claude.com/claude-code) **Jira:** [RHOAIENG-52655](https://issues.redhat.com/browse/RHOAIENG-52655) --------- Co-authored-by: Ambient Code Bot <bot@ambient-code.local> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e0644f2 commit 310e369

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

components/frontend/src/hooks/agui/event-handlers.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -847,14 +847,21 @@ function handleMessagesSnapshot(
847847

848848
// Sort by timestamp so messages from interleaved runs appear in
849849
// chronological order. Messages without timestamps keep their
850-
// relative position (stable sort).
850+
// relative position. Use original index as tiebreaker so that
851+
// thinking blocks with identical timestamps stay interleaved with
852+
// their corresponding agent messages.
853+
const originalOrder = new Map(filtered.map((msg, idx) => [msg.id, idx]))
851854
filtered.sort((a, b) => {
852855
const ta = a.timestamp ? new Date(a.timestamp).getTime() : null
853856
const tb = b.timestamp ? new Date(b.timestamp).getTime() : null
854-
if (ta == null && tb == null) return 0
855-
if (ta == null) return 0 // keep relative position
856-
if (tb == null) return 0
857-
return ta - tb
857+
858+
if (ta != null && tb != null) {
859+
const diff = ta - tb
860+
if (diff !== 0) return diff
861+
}
862+
863+
// Equal or missing timestamps: preserve original snapshot order
864+
return (originalOrder.get(a.id) ?? 0) - (originalOrder.get(b.id) ?? 0)
858865
})
859866
state.messages = filtered
860867

0 commit comments

Comments
 (0)