Skip to content

fix(providers): SSE data-prefix and final-event edge cases - #3

Merged
dirtysouthalpha merged 1 commit into
mainfrom
fix/sse-edge-cases
Jul 26, 2026
Merged

dirtysouthalpha merged 1 commit into
mainfrom
fix/sse-edge-cases

Conversation

@dirtysouthalpha

Copy link
Copy Markdown
Owner

Ports two real SSE parsing bugs' fixes from the agentic-hardening lineage onto current main, where both were still present:

  1. Parser required data: (with space) → dropped every event from providers emitting data:{...} (e.g. Gemini). Now accepts both forms.
  2. Final event without a trailing newline was left unprocessed in the buffer → dropped the usage record → 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.

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.
@dirtysouthalpha
dirtysouthalpha merged commit 90fb302 into main Jul 26, 2026
1 check passed
@dirtysouthalpha
dirtysouthalpha deleted the fix/sse-edge-cases branch July 26, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant