fix(ai-gateway): log partial response body when client disconnects mid-stream - #4890
Merged
Conversation
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Executive SummaryHoisting Notes
Files Reviewed (2 files)
Reviewed by claude-opus-5 · Input: 42 · Output: 8.6K · Cached: 902.7K Review guidance: REVIEW.md from base branch |
lambertjosh
approved these changes
Jul 30, 2026
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.
Problem
When a client disconnects mid-stream (e.g. it gives up the moment an error event arrives), the response pipeline's
cancel()callback settled the request-log capture with'response stream was cancelled'and no response body — even thoughrewriteSseStreamhad already accumulated the raw upstream chunks (including the error event) incapturedChunks. The accumulated text lived in a closure thecancel()callback couldn't reach, andsettleOncemade the empty settlement win, so theapi_request_logrow ended up withresponse = NULLand only aresponse_body_read_error.This mostly hits non-terminal error events (
{"error": ...}chunks on chat_completions,type: 'error'events on messages), where the upstream keeps the stream open briefly after the error and the client disconnects first. Terminal events ([DONE],message_stop,response.completed/incomplete/failed) were already captured becausesettleBody()runs synchronously in the same macrotask as the finalenqueue, beforecancel()can interleave.Fix
Hoist the
capturedChunksbuffer out ofrewriteSseStreaminto the scope shared by the stream'sstart()andcancel()callbacks (one line of plumbing per API kind), socancel()can settle with the partial body received so far — same as the existing upstream timeout/disconnect paths already do viasettleReadError. The buffer staysnullwhen request logging is disabled, so non-logged traffic still accumulates nothing.Not covered (intentionally)
If the client disconnects before any bytes arrive (e.g. client-side timeout while the upstream is still rate-limiting), there is still nothing to log, and an error event that arrives after the disconnect is dropped — capturing it would require keeping the upstream stream alive purely for logging after the client is gone.
Tests
undefinedpartial body when nothing was streamed.Note:
pnpm format:check/lint/typecheck --changes-onlypassed via the pre-push hook; the jest run for this file was not completed locally.