fix: Request end up prematurely because of client disconection - #2
Merged
Conversation
hcastc00
force-pushed
the
fix/finish-reason
branch
from
September 9, 2026 11:11
51d2ff4 to
b06d16a
Compare
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.
Retain streaming usage and add privacy-conscious diagnostics
Summary
Streaming completions could reach clients before trailing provider usage was
consumed, allowing clients that close on
finish_reasonor[DONE]to interruptusage collection. The OpenAI-compatible parser also missed usage on some delta
shapes. Successful requests could consequently reach metering without token
counts, while
generation completedlogs gave no indication of the missing usage.This change retains usage through stream finalization and adds diagnostics to
trace provider receipt, parser forwarding, and metering outcomes without logging
prompt or response content.
Changes
finish_reasonand[DONE],with a five-second finalization timeout. Request cancellation still cancels
upstream work; finalization also includes the metering call triggered at EOF.
failures, and metering acceptance or failure with correlation IDs and usage
counts. Metering acceptance does not prove a nonzero credit deduction.
[DONE], bare EOF, cancellation, parser anomalies, and usage forwarding.Debug logs add chunk metadata for usage and finish signals.
are not silently dropped during bursts.
Privacy
Diagnostics omit raw SSE, prompts, generated text, tool arguments, and credentials.
Request summaries use fixed metadata keys, numeric/boolean settings, and counts;
tool names, stop strings, schemas, and arbitrary stream options are omitted.
Generation and HTTP error logs omit error messages and arbitrary upstream metadata,
which may echo private request contents, while retaining numeric upstream status.
Unknown finish reasons are logged as
unknown. Correlation IDs remain visible.Validation
go test ./...passes, including privacy regression tests for request summaries,provider errors that echo prompts, tool arguments, and unexpected finish reasons.
trailing usage, and providers that omit usage.
correlation fields, severity, and preservation of accounting outcomes.
git diff --checkpasses.Limits
The timeout bounds finalization but cannot recover usage a provider never sends.
Missing usage is reported without replaying requests or estimating charges.
Historical missing usage and existing logs are not repaired. Log delivery and
retention still depend on deployment, and disabling sampling increases log volume.
See streaming usage diagnostics for event
meanings, Loki queries, and how to distinguish possible provider omission from an
interrupted stream.