opencode: Potential race condition for displaying streamed messages in the TUI as one single message
I have tried troubleshooting to fix myself but at this point it is best to confirm whether you (others) are able to reproduce similarly this issue I am having. Basically, at different times, especially with the very first message in the conversation, the very end of a message response stream (typically a punctuation like . or ?) gets pushed over into its own new message in the TUI. See the following screenshot:
I have narrowed it down to if I comment out this line of code text = undefined then it works as expected (see below).
Line to comment out:
Resulting updated CORRECT response in one single message:
The problem though is that I have noticed other side effects by commenting out this line of code so I do not think it is the solution. It seems like this is all part of some sort of race condition.
Let me know if you have any questions? Thanks!
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 1
- Comments: 23 (14 by maintainers)
Awesome work @0ximjosh !!
Especially thanks for my sanity that I wasn’t crazy 🤣
P.S. LLMs suck at trying to troubleshoot/debug race conditions! Haha
I am also using ai-sdk/openai-compatible, but i also saw this with “@openrouter/ai-sdk-provider”. I can confirm this is a race condition.
onStepFinishandonFinishare triggering before all thetext-deltachunks have been read byresult.fullStreamin the file you mentioned in the issue desc. Attached are the end of the logs from a session i got this to triggerNote how we see
stop message finishandstop step finishprint before the last text-delta. We can see this exaggerated in the session json fileHere, note the two
"type": "text"inparts. This is specifically from this chunk of codewhere we can see why commenting out
text = undefinedsemi-fixes the issue (albeit with side affects) since that would not reset text whenonStepFinishtriggers prematurely. I havent tried it yet, but @thdxr is there a reason we arent doing the parsing inonChunk? from the docs it seems the async function toonChunkwill block processing untill the promise of the current chunk is resolved. If we want to go down that path i can whip that up.I now also have a way to reproduce this, just middle-man the stream and make the usage token send at the same time as the chunks before it. Im even able to get it to improperly split up to 2-4 tokens if i send enough at the same time.
Im using my companies api layer and infra to test this so unfortunately i don’t have any code i can share for others to reproduce easily, but im more than happy to work on / test a fix for this. If i have time later this week and this hasnt been fixed i can spin up a server with my middleman people can use to test / repro this bug.
I tried reproducing the issue using GitHub Copilot as the provider, and it happened only once. When testing with Google as the provider, I couldn’t reproduce the issue. I’m also on WSL2, i can test on a bare metal linux if needed.
I have also had this using the github-copilot provider for sonnet 4. It puts out messages after like
?I guess I had this while using the Google api also. Could check it and let you know about this
@adamdotdevin @GitMurf I have had this happen quite consistently using GitHub copilot as a provider. Mostly using the copilot OpenAi models. Most of the time I prompt on the beginning of the session to avoid this and it sometimes works. But this always does happen to me in a new session, same way shown by @GitMurf. This never happens when I use my other provider (only one I use besides copilot is google provider)