Skip to content

Commit fba984a

Browse files
author
myles
committed
session: recover from dropped model streams with no terminal event
xAI/grok (and others) occasionally end a stream without a finish event — keepalives keep arriving but completion is never signaled. Stream.runDrain then never resolves, the assistant message is never finalized, and the agent loop hangs: the session sits busy forever and can't accept prompts (opencode anomalyco#11865/anomalyco#19023/anomalyco#32366; hermes-agent anomalyco#27197). byte-level chunkTimeout can't catch this since keepalive bytes keep the socket warm. Add an event-idle timeout (Stream.timeoutOrElse) on the model stream — a healthy turn emits deltas continuously, so a 60s event gap means the stream is dead. Failing routes into the existing retry/halt path, so the turn finalizes (partial content + clear error) and the loop exits. Verified against a keepalive-only mock: session recovers to idle.
1 parent b155b15 commit fba984a

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎packages/opencode/src/session/processor.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import { Database } from "@opencode-ai/core/database/database"
2727
import { Usage, type LLMEvent } from "@opencode-ai/llm"
2828

2929
const DOOM_LOOP_THRESHOLD = 3
30+
// Max gap between model stream events before the stream is considered dead.
31+
// Guards against providers that drop a stream without a terminal/finish event.
32+
const STREAM_IDLE_TIMEOUT = "60 seconds"
3033
export type Result = "compact" | "stop" | "continue"
3134

3235
export interface Handle {
@@ -637,7 +640,24 @@ const layer = Layer.effect(
637640
ctx.currentText = undefined
638641
ctx.reasoningMap = {}
639642
yield* status.set(ctx.sessionID, { type: "busy" })
640-
const stream = llm.stream(streamInput)
643+
// Guard against providers that drop a stream without a terminal
644+
// event (e.g. xAI/grok sending keepalives but no finish). A healthy
645+
// turn emits events continuously, so an event-idle gap this long
646+
// means the stream is dead. Failing here routes into the existing
647+
// retry/halt path so the turn finalizes and the loop exits, instead
648+
// of hanging the session forever.
649+
const stream = llm.stream(streamInput).pipe(
650+
Stream.timeoutOrElse({
651+
duration: STREAM_IDLE_TIMEOUT,
652+
orElse: () =>
653+
Stream.failSync(
654+
() =>
655+
new Error(
656+
`Model stream stalled: no events for ${STREAM_IDLE_TIMEOUT} (dropped stream with no terminal event)`,
657+
),
658+
),
659+
}),
660+
)
641661

642662
yield* stream.pipe(
643663
Stream.tap((event) => handleEvent(event)),

0 commit comments

Comments
 (0)