Skip to content

fix(cli): flush run parts after json stream idle#31483

Closed
maojindao55 wants to merge 2 commits into
anomalyco:devfrom
maojindao55:fix/run-json-flush-parts
Closed

fix(cli): flush run parts after json stream idle#31483
maojindao55 wants to merge 2 commits into
anomalyco:devfrom
maojindao55:fix/run-json-flush-parts

Conversation

@maojindao55

@maojindao55 maojindao55 commented Jun 9, 2026

Copy link
Copy Markdown

Summary

Fixes #31482.

  • Track parts emitted by opencode run's event stream.
  • After non-interactive session.prompt / session.command completes and the event loop reaches idle, flush any parts from the returned assistant message that were not emitted by the SSE stream.
  • This keeps run --format json from exiting with missing text / step_finish events when session.status idle is observed before the final part updates are processed.

Context

Issue #31482 reports a reproducible case with opencode 1.16.2 where:

opencode -m volcengine-plan/kimi-k2.6 run --format json \
  --session <existing-session-with-tool-history> \
  "only output OK"

The assistant response and step finish were persisted in ~/.local/share/opencode/opencode.db, but stdout was empty or only contained step_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 final message.part.updated events are handled, the process can finish before emitting the response parts. The blocking session.prompt / session.command response already contains the assistant message parts, so this patch uses that response as a final precise flush source.

Testing

  • git diff --check
  • npm exec --yes bun@1.3.14 -- install --frozen-lockfile
  • npm exec --yes bun@1.3.14 -- run --cwd packages/opencode build --single --skip-install --skip-embed-web-ui
  • Built binary smoke test: opencode --version returned 0.0.0-fix/run-json-flush-parts-202606090832
  • Manual real-model checks with the compiled binary:
    • new run --format json session emitted step_start, text, and step_finish
    • resumed run --format json --session ... emitted step_start, text, and step_finish

Note: the current dev build could not resume the original 1.16.2 local session because it returned Session not found for that legacy session. The fix was validated against a current dev session created by the compiled binary.

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Jun 9, 2026
@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

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.

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions

github-actions Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Jun 9, 2026
@github-actions github-actions Bot closed this Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opencode run --format json --session writes assistant text to DB but emits empty stdout for an existing session

1 participant