fix(streaming): detect truncated streams and complete promptly on [DONE] - #170
Open
Fahad090NP wants to merge 1 commit into
Open
fix(streaming): detect truncated streams and complete promptly on [DONE]#170Fahad090NP wants to merge 1 commit into
Fahad090NP wants to merge 1 commit into
Conversation
The shared streaming engine only ended a response on connection close or cancellation, ignoring OpenCode's data: [DONE] terminator. This left the request hanging until the idle timeout when the gateway kept the connection alive after [DONE] (user cancelled silently), and treated a connection that closed without [DONE] (proxy reset, upstream crash, truncated payload) as a successful empty response. - parseServerSentEvent now fires an onDone callback on data: [DONE] and no longer passes it to the extractor. - engine breaks the read loop as soon as [DONE] arrives (prompt completion instead of waiting on a lingering socket). - isStreamTruncated throws a clear OpenCodeRequestError when the stream ended with neither [DONE] nor a captured finish_reason after content was already received, instead of silently succeeding. - unit tests added in src/test/sse.test.ts.
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.
Summary
The shared streaming engine (
src/transports/engine.ts) only ended a response stream on connection close or user cancellation, ignoring OpenCode'sdata: [DONE]terminator (confirmed intmp/opencode-dev/.../server/transport/ws.ts, which enqueuesdata: [DONE]\n\nthen closes). This produced two silent failure modes that looked like "the model stopped working":[DONE], the read loop never sawdoneand sat until the 2-minute idle timeout (user cancelled manually, no error shown).[DONE](proxy reset, upstream crash, truncated payload, VPN/firewall cut),reader.read()returneddonewith no[DONE]and nofinish_reason; the engine treated it as a successful empty response.Fix
src/transports/sse.ts—parseServerSentEventnow takes anonDonecallback fired ondata: [DONE](and[DONE]is no longer passed to the extractor).src/transports/engine.ts— tracksstreamFlags.sawDoneand breaks the read loop as soon as[DONE]arrives (prompt completion instead of waiting on a lingering socket); after the loop,isStreamTruncatedthrows a clearOpenCodeRequestErrorwhen the stream ended with neither[DONE]nor a capturedfinish_reasonafter content was already received, instead of silently succeeding.src/transports/sse.ts—isStreamTruncatedis a small pure helper (exported, unit-tested).src/test/sse.test.ts— unit tests for[DONE]handling andisStreamTruncated.Verification
npm run lint(editorconfig, eslint, markdown, prettier, shellcheck, typecheck, unit tests) — green.src/test/sse.test.ts— new tests pass.Builds on the resilience work in #169 (history-trim + transient fetch-retry).