fix: await event loop in non-interactive opencode run#29132
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 matches a bug we keep hitting. We run opencode headless ( It's actually a re-regression: #26955 added this await, then #27371 dropped it while reworking exit handling (moved the exit code onto On testing: the One small suggestion: early-returning on |
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