fix: resolve silent agent termination on stream drop#9589
Closed
Varuu-0 wants to merge 4 commits intoKilo-Org:mainfrom
Closed
fix: resolve silent agent termination on stream drop#9589Varuu-0 wants to merge 4 commits intoKilo-Org:mainfrom
Varuu-0 wants to merge 4 commits intoKilo-Org:mainfrom
Conversation
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (7 files)
Reviewed by gpt-5.5-20260423 · 1,392,325 tokens |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 313b3ba3c0
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
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.
Context
This PR addresses a critical bug where Kilo agents would prematurely stop generating text mid-response without any error shown to the user. This silent failure occurs when the upstream Server-Sent Events (SSE) connection drops unexpectedly before a proper terminal event or
finish_reasonis received from the provider. Previously, the system defaulted to an "other" finish reason, which the agent loop interpreted as a successful completion, skipping the error handling and retry logic entirely.fixes
#8476
#9544
Implementation
To fix this, the system must properly distinguish between a clean generation completion and a premature stream termination:
openai-compatible-chat-language-model.tsandopenai-responses-language-model.tsto actively track whether a terminal finish chunk or reason was successfully received. If the stream flushes prematurely, they now emit anunknownfinish reason instead of defaulting toother.map-openai-compatible-finish-reason.tsandmap-openai-responses-finish-reason.tsto map unrecognized states tounknowninstead ofother.processor.ts) to intercept theunknownfinish reason immediately after the stream drains. If no tool parts were executed, it throws a retryableMessageV2.APIError("Stream terminated prematurely without a finish reason"), which triggers the existingEffect.retrypipeline.prompt.tsto explicitly excludeotheras a valid completion signal, ensuring no ambiguous states can cause a silent loop exit.Screenshots
openai-compatible-chat-language-model.tstypescript<br/>let finishReason = {<br/> unified: "other",<br/> raw: undefined<br/>}<br/>typescript<br/>let finishReason = { ... }<br/>let receivedFinishReason = false;<br/><br/>/* in flush() */<br/>if (!receivedFinishReason && !isFirstChunk) {<br/> finishReason = { unified: "unknown", raw: undefined }<br/>}<br/>openai-responses-language-model.tstypescript<br/>let finishReason = {<br/> unified: "other",<br/> raw: undefined<br/>}<br/>typescript<br/>let finishReason = { ... }<br/>let receivedFinishChunk = false;<br/><br/>/* in flush() */<br/>if (!receivedFinishChunk && responseId !== null) {<br/> finishReason = { unified: "unknown", raw: undefined }<br/>}<br/>processor.tstypescript<br/>yield* stream.pipe(<br/> Stream.tap(...),<br/> Stream.takeUntil(...),<br/> Stream.runDrain<br/>)<br/><br/><br/>typescript<br/>yield* stream.pipe(...)<br/><br/>if (ctx.assistantMessage.finish === "unknown") {<br/> const parts = MessageV2.parts(ctx.assistantMessage.id)<br/> if (!parts.some(p => p.type === "tool")) {<br/> yield* Effect.fail(new MessageV2.APIError({<br/> message: "Stream terminated prematurely...",<br/> isRetryable: true<br/> }).toObject())<br/> }<br/>}<br/>prompt.tstypescript<br/>const finished = handle.message.finish &&<br/> !["tool-calls", "unknown"].includes(handle.message.finish)<br/>typescript<br/>const finished = handle.message.finish &&<br/> !["tool-calls", "unknown", "other"].includes(handle.message.finish)<br/>typescript<br/>default:<br/> return "other"<br/>typescript<br/>default:<br/> return "unknown"<br/>How to Test
finish_reasonchunk.Get in Touch
Discord @varuu