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:

Image

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:

Image

Resulting updated CORRECT response in one single message:

Image

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)

Most upvoted comments

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. onStepFinish and onFinish are triggering before all the text-delta chunks have been read by result.fullStream in the file you mentioned in the issue desc. Attached are the end of the logs from a session i got this to trigger

INFO  2025-06-24T04:10:49 +0ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=text-delta part
INFO  2025-06-24T04:10:49 +0ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +1ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=text-delta part
INFO  2025-06-24T04:10:49 +0ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +175ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=text-delta part
INFO  2025-06-24T04:10:49 +1ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +1ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +0ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=text-delta part
INFO  2025-06-24T04:10:49 +0ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +1ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=text-delta part
INFO  2025-06-24T04:10:49 +2ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +72ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=text-delta part
INFO  2025-06-24T04:10:49 +1ms service=session finishReason=stop step finish
INFO  2025-06-24T04:10:49 +1ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +0ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=text-delta part
INFO  2025-06-24T04:10:49 +0ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +1ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.part.updated publishing
INFO  2025-06-24T04:10:49 +0ms service=session reason=stop message finish
INFO  2025-06-24T04:10:49 +0ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +0ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=text-delta part
INFO  2025-06-24T04:10:49 +0ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +1ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +0ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=step-finish part
INFO  2025-06-24T04:10:49 +0ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=step-finish unhandled
INFO  2025-06-24T04:10:49 +0ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=finish part
INFO  2025-06-24T04:10:49 +0ms service=session session=ses_85fde6652ffexTSbuiHhMlHMjV type=finish unhandled
INFO  2025-06-24T04:10:49 +0ms service=bus type=storage.write publishing
INFO  2025-06-24T04:10:49 +0ms service=bus type=message.updated publishing
INFO  2025-06-24T04:10:49 +0ms service=session sessionID=ses_85fde6652ffexTSbuiHhMlHMjV unlocking
INFO  2025-06-24T04:10:49 +0ms service=server duration=3872 response
INFO  2025-06-24T04:11:39 +49167ms service=bus type=* unsubscribing
INFO  2025-06-24T04:11:39 +0ms service=server event disconnected
INFO  2025-06-24T04:11:39 +6ms service=app name=session shutdown
INFO  2025-06-24T04:11:39 +1ms service=app name=mcp shutdown

Note how we see stop message finish and stop step finish print before the last text-delta. We can see this exaggerated in the session json file

{
  "id": "msg_7a02199b4002xF5X9AoDekeOAW",
  "role": "assistant",
  "parts": [
    {
      "type": "step-start"
    },
    {
      "type": "text",
      "text": "<think>\nOkay, the user wrote \"hello world\". Let me see how to respond. Since the user didn't ask a specific question, I should check if they need help with something. The guidelines say to be concise and direct. Also, mention the help command and feedback link.\n\nI'll keep it short. Maybe just ask how I can assist them today. Include the help options as instructed. Avoid any extra text. Make sure not to use emojis unless asked. Stick to the required format.\n</think>\n\nHow can I help you today? Use `/help` for assistance"
    },
    {
      "type": "text",
      "text": "."
    }
  ],
  "metadata": {
    "assistant": {
      "system": [
        "Redacted",
        "redacted"
      ],
      "path": {
        "cwd": "/Users/josh/github/opencode",
        "root": "/Users/josh/github/opencode"
      },
      "cost": null,
      "tokens": {
        "input": null,
        "output": null,
        "reasoning": 0,
        "cache": {
          "write": 0,
          "read": 0
        }
      },
      "modelID": "deepseek-ai/DeepSeek-R1",
      "providerID": "deepseek"
    },
    "time": {
      "created": 1750738246068,
      "completed": 1750738249939
    },
    "sessionID": "ses_85fde6652ffexTSbuiHhMlHMjV",
    "tool": {}
  }
}

Here, note the two "type": "text" in parts. This is specifically from this chunk of code

          case "text-delta":
            if (!text) {
              text = {
                type: "text",
                text: value.textDelta,
              }
              next.parts.push(text)
              break
            } else text.text += value.textDelta
            break

where we can see why commenting out text = undefined semi-fixes the issue (albeit with side affects) since that would not reset text when onStepFinish triggers prematurely. I havent tried it yet, but @thdxr is there a reason we arent doing the parsing in onChunk? from the docs it seems the async function to onChunk will 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.

Image

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)