fix(opencode): order messages by (time.created, id) at runLoop exit#28126
fix(opencode): order messages by (time.created, id) at runLoop exit#28126godspede wants to merge 1 commit into
Conversation
Fixes anomalyco#20699 The runLoop exit at session/prompt.ts compared messages by lex-id: 'lastUser.id < lastAssistant.id'. User message IDs are minted client-side (packages/app/src/components/prompt-input/submit.ts, TUI prompt index.tsx) and shipped to the server as input.messageID, so client/server clock skew can flip the lex comparison. When user-id sorts after assistant-id, the exit check fails and a second LLM step fires — producing the duplicate-assistant turn in anomalyco#20699. Switch to composite (time.created, id): server-set time.created as primary (no client skew), id as same-millisecond tiebreak (matches the SQL composite sort at message-v2.ts:596 and :936). Applied at both runLoop sites that compare user-vs-assistant ordering: the loop exit at ~L1675 and the system-reminder wrap at ~L1795.
|
The following comment was made by an LLM, it may be inaccurate: The search found one related PR that might be worth considering: Potential Related PR:
This PR (#28126) appears to be a distinct fix targeting the runLoop exit specifically with time-based ordering, while #24379 focused on the prompt loop with transcript position. They complement different aspects of the message ordering problem but are not exact duplicates. |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Closes #20699 (maybe also #25270 and #27928)
Type of change
What does this PR do?
Time zone suffering. The user's messageIDs are minted client-side with
Date.now() * 4096 + counter, whereas the assistant's are minted server-side, so server/client time differences can hose the ordering used for the loop exit, ultimately causing a dupe. You end up with all kinds of weird issues where people say it works fine in one environment and but dupes responses in another.How did you verify your code works?
I ran it in-browser with some misbehaving models, and saw that they now behaved as one would expect.
Screenshots / recordings
N/A
Checklist