fix(cli): flush run parts after json stream idle#31483
Conversation
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
Please edit this PR description to address the above within 2 hours, or it will be automatically closed. If you believe this was flagged incorrectly, please let a maintainer know. |
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found one potentially related PR: Related PR:
However, it's addressing a different aspect (retrying empty streams and discarding partial parts vs. flushing non-emitted parts). The current PR (31483) appears to be the primary fix for this specific issue. No duplicate PRs found. |
|
This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window. Feel free to open a new pull request that follows our guidelines. |
Summary
Fixes #31482.
opencode run's event stream.session.prompt/session.commandcompletes and the event loop reaches idle, flush any parts from the returned assistant message that were not emitted by the SSE stream.run --format jsonfrom exiting with missingtext/step_finishevents whensession.status idleis observed before the final part updates are processed.Context
Issue #31482 reports a reproducible case with
opencode 1.16.2where:The assistant response and step finish were persisted in
~/.local/share/opencode/opencode.db, but stdout was empty or only containedstep_start. A fresh session and a simple resumed session streamed normally. That points to the non-interactive run output path, not the model/provider.The current loop exits on
session.status idle. If that idle event reaches the loop before all finalmessage.part.updatedevents are handled, the process can finish before emitting the response parts. The blockingsession.prompt/session.commandresponse already contains the assistant message parts, so this patch uses that response as a final precise flush source.Testing
git diff --checknpm exec --yes bun@1.3.14 -- install --frozen-lockfilenpm exec --yes bun@1.3.14 -- run --cwd packages/opencode build --single --skip-install --skip-embed-web-uiopencode --versionreturned0.0.0-fix/run-json-flush-parts-202606090832run --format jsonsession emittedstep_start,text, andstep_finishrun --format json --session ...emittedstep_start,text, andstep_finishNote: the current dev build could not resume the original
1.16.2local session because it returnedSession not foundfor that legacy session. The fix was validated against a current dev session created by the compiled binary.