fix: drain pending events before breaking on session idle in JSON format mode#31446
Open
jangel97 wants to merge 1 commit into
Open
fix: drain pending events before breaking on session idle in JSON format mode#31446jangel97 wants to merge 1 commit into
jangel97 wants to merge 1 commit into
Conversation
…mat mode The loop() function in opencode run --format json breaks immediately when it receives a session.status = idle event. In containerized environments, this event races ahead of text and step-finish part events in the SSE pipeline, causing incomplete JSONL output (only step_start emitted). Track active step lifecycle with a counter. When idle arrives while steps are still open, defer the break and drain remaining events with a 3s safety timeout.
4 tasks
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 #31435
Type of change
What does this PR do?
opencode run --format jsonexits immediately whensession.status = idlefires, but in containerized/CI environments the idle event races ahead oftextandstep-finishpart events in the SSE pipeline. The result is truncated JSONL output — onlystep_startis emitted whiletextandstep_finishare lost.The fix tracks active steps with a counter (
activeSteps). When idle arrives while steps are still open, it defers the break (pendingIdle = true) and continues draining events until allstep-finishevents arrive. A 3-second safety timeout ensures the loop doesn't hang if astep-finishis never delivered.I understand why this works: the idle event is published by the Runner's
onIdlecallback after the processor finishes, but it travels through a different path (session status → PubSub) than part events (LLM stream → processor → PubSub). In containerized environments with higher event delivery latency, idle consistently wins the race. By deferring the break and waiting for the step lifecycle to complete, we guarantee all parts are consumed before exiting.How did you verify your code works?
Tested in three configurations:
bun run --conditions=node src/index.ts run --format json -m google-vertex-anthropic/claude-sonnet-4@20250514 "Say hello"— all 3 events emitted (step_start, text, step_finish)OPENCODE_EXPERIMENTAL=true— all 3 events emittedstep_startemitted, confirming the race conditionAlso verified:
opencode run "prompt") works unchangedScreenshots / recordings
Before fix (container): only
step_startemittedAfter fix (container): all events emitted
Checklist