Skip to content

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

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

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

Conversation

@maojindao55

Copy link
Copy Markdown

Issue for this PR

Fixes #31482

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

opencode run --format json can stop reading the event stream when the session reaches idle, before the final assistant parts are emitted to stdout.

This change tracks which parts were already emitted from the event stream. After session.prompt or session.command finishes, it flushes any missing parts from that call's returned assistant message. This keeps JSON output consistent without replaying older session history.

How did you verify your code works?

  • Ran git diff --check
  • Built the CLI with npm exec --yes bun@1.3.14 -- run --cwd packages/opencode build --single --skip-install --skip-embed-web-ui
  • Verified the built binary smoke test passed with version 0.0.0-fix/run-json-flush-parts-v2-202606091119
  • Ran the built binary against a real model:
    • new run --format json session emitted step_start, text, and step_finish
    • resumed run --format json --session ... emitted step_start, text, and step_finish

Screenshots / recordings

N/A, CLI-only change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@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:

Found potential duplicate:

This PR appears to address the same issue as #31505 (the current PR). Both are fixing problems with the opencode run --format json command stopping prematurely when the session reaches idle, before all assistant parts are emitted to stdout. The approach differs (draining events vs flushing parts), but they target the same root problem with JSON stream output in CLI.

@maojindao55

Copy link
Copy Markdown
Author

Re: the bot note about #31446, this fixes the same symptom but uses a different fallback.

#31446 keeps draining SSE after idle with a timeout. This PR flushes only the parts returned by the current session.prompt / session.command call after the stream loop finishes, so it does not depend on a timer and does not replay older session history.

The repro in #31482 was an existing session with prior tool history where the assistant result was persisted but stdout was empty.

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