fix(providers): SSE data-prefix and final-event edge cases - #3
Merged
Merged
Conversation
Ports two SSE parsing fixes from the agentic-hardening lineage onto the
current v3.x stream parser, where both bugs were still present.
1. The parser required `data: ` with a space and used slice(6), so every
event from providers that emit `data:{...}` (e.g. Gemini's
OpenAI-compatible mode) was silently dropped. Now accepts `data:` with or
without the spec-optional space.
2. A stream that ended without a trailing newline left its last event in
`buffer`, which the loop never processed. On many providers the final
event carries the `usage` record, so cost/token accounting was broken.
Now flushes the decoder and processes the trailing line after the loop.
Parse logic extracted into a `handleData`/`handleLine` closure so it runs both
in the loop and on the final buffer. Added tests/sse-edge-cases.test.ts
covering both cases plus the well-formed baseline (3 passing).
The 5 unrelated pre-existing Windows-env failures (bwrap sandbox, read-only
atomic write, secrets-file crypto) are untouched by this change.
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.
Ports two real SSE parsing bugs' fixes from the
agentic-hardeninglineage onto current main, where both were still present:data:(with space) → dropped every event from providers emittingdata:{...}(e.g. Gemini). Now accepts both forms.usagerecord → broke cost/token accounting. Now flushed after the loop.Adds tests/sse-edge-cases.test.ts (3 passing). Typecheck clean. The 5 pre-existing Windows-env test failures (bwrap/atomic-write/secrets) are unrelated and untouched.