Skip to content

Commit 00c5a85

Browse files
committed
fix(agent/opencode): accept clean-exit runs when OpenCode v2 omits step_finish
OpenCode v2 `run --format json` frequently ends the stream after the final text event without emitting step_finish (upstream: anomalyco/opencode#53207; Multica: multica-ai#8892). The daemon's terminal-signal guard then fails the run and discards a completed answer. Scope the rescue to the openStep guard only: when the run ended mid-step at EOF but the CLI process exited 0 and text output was produced, accept the run as completed (with a Warn log). The continuation and void-step guards keep failing closed, as do runs with a non-zero exit or an error event. Verified locally: go build ./pkg/agent and the OpenCode unit tests pass.
1 parent ace7655 commit 00c5a85

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

‎server/pkg/agent/opencode.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ func (b *opencodeBackend) Execute(ctx context.Context, prompt string, opts ExecO
351351
// the process exit detail so a mid-step crash still surfaces the
352352
// signal / exit code that killed it.
353353
scanResult.errMsg = fmt.Sprintf("%s; opencode exited with error: %v", scanResult.errMsg, exitErr)
354+
} else if exitErr == nil && scanResult.stepStillOpen && strings.TrimSpace(scanResult.output) != "" {
355+
// Some OpenCode 2.x providers complete the answer but never emit
356+
// step_finish in --format json mode (observed with
357+
// @ai-sdk/openai-compatible gateways whose streams carry no
358+
// finish/usage chunk): the CLI exits 0 with the full text on the
359+
// stream, and only the terminal-signal guard marks the run
360+
// failed. A clean exit plus produced text is positive evidence
361+
// this run finished, so accept it instead of discarding a
362+
// completed answer. Scoped to the openStep guard: continuation
363+
// and void-step failures keep failing closed.
364+
b.cfg.Logger.Warn("opencode: stream ended mid-step without step_finish, but the process exited cleanly with output; accepting the run as completed")
365+
scanResult.status = "completed"
366+
scanResult.errMsg = ""
354367
} else if writeErr != nil && !scanResult.sawTerminalSignal {
355368
// A failed prompt write is only benign once the run is PROVEN to have
356369
// finished: OpenCode reads stdin to EOF before it does any work, so a
@@ -409,6 +422,7 @@ type eventResult struct {
409422
sessionID string
410423
usage TokenUsage // accumulated token usage across all steps
411424
noTerminalSignal bool // guard fired: the stream ended without evidence the run actually finished
425+
stepStillOpen bool // the openStep guard fired specifically (stream ended mid-step), as opposed to the continuation / void-step guards
412426
// sawTerminalSignal is positive evidence that the run actually finished: a
413427
// step_finish closed the last step with no continuation pending and with
414428
// something to show for it. It is NOT the negation of noTerminalSignal — a
@@ -551,12 +565,14 @@ func (b *opencodeBackend) processEvents(r io.Reader, ch chan<- Message) eventRes
551565
// `opencode run` exited without emitting an error event. Fail closed on
552566
// that structural evidence rather than reporting a false-green completion.
553567
noTerminalSignal := false
568+
stepStillOpen := false
554569
if finalStatus == "completed" {
555570
switch {
556571
case openStep:
557572
finalStatus = "failed"
558573
finalError = "opencode stream ended without a terminal signal (step still open at EOF)"
559574
noTerminalSignal = true
575+
stepStillOpen = true
560576
case awaitingContinuation:
561577
finalStatus = "failed"
562578
finalError = "opencode stream ended without a terminal signal (last step required a continuation that never started)"
@@ -575,6 +591,7 @@ func (b *opencodeBackend) processEvents(r io.Reader, ch chan<- Message) eventRes
575591
sessionID: sessionID,
576592
usage: usage,
577593
noTerminalSignal: noTerminalSignal,
594+
stepStillOpen: stepStillOpen,
578595
sawTerminalSignal: sawStepFinish && !noTerminalSignal,
579596
}
580597
}

0 commit comments

Comments
 (0)