fix: await event loop in non-interactive opencode run#29132
Open
hardes11 wants to merge 1 commit into
Open
Conversation
In non-interactive mode, the event processing loop was started with fire-and-forget (Promise.catch but not awaited). When the prompt() call returned its HTTP acknowledgment, execute() returned immediately, and the Effect framework's cleanup disposed the in-process server, killing the SSE stream before the model finished generating. Only the step_start event was emitted (it arrives before prompt() returns). All text, tool_use, and step_finish events were lost because the stream was torn down mid-generation. Fix: store the loop promise and await it after the prompt/command call completes. This keeps the process alive until the session reaches idle and all events have been processed. Tested: `opencode run 'what is 2+2?' --format json` now correctly emits step_start + text + step_finish events (previously only step_start).
This was referenced May 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #26855 (originally reported as #29131, which was a duplicate)
Type of change
What does this PR do?
Fixes
opencode run --format jsonexiting before the event stream completes. In non-interactive mode,loop()was called fire-and-forget (.catch(), not awaited). Whenprompt()returns the HTTP ack,execute()returns, the Effect framework disposes the in-process server, and the SSE stream dies before the model finishes generating.Only
step_startwas emitted.textandstep_finishwere lost.The change stores the loop promise and awaits it after the prompt/command call, keeping the process alive until the session reaches
idle.How did you verify your code works?
bun run build --single --skip-embed-web-ui→ smoke test passedopencode run "what is 2+2?" --format jsonemits onlystep_startstep_start,text(with "4"),step_finishopencode run "search arxiv for..." --format json— tool_use events now appearbun turbo typecheck, 15/15 packages)Checklist