Skip to content

fix(streaming): prevent bursty Responses SSE delivery - #123

Merged
lidge-jun merged 2 commits into
lidge-jun:mainfrom
Wibias:codex/fix-responses-sse-coalescing-pr
Jul 14, 2026
Merged

lidge-jun merged 2 commits into
lidge-jun:mainfrom
Wibias:codex/fix-responses-sse-coalescing-pr

Conversation

@Wibias

@Wibias Wibias commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Problem

Issue #114 remained partially unresolved after #115. The upstream Accept-Encoding: identity change addressed compressed provider responses, but the Codex Desktop app-server could still receive the final answer as one burst when the bridge processed many adapter events in a single JavaScript turn.

The observable symptom was:

  • direct curl requests streamed progressively;
  • the real Codex app-server received hundreds of item/agentMessage/delta notifications within roughly 30 ms;
  • the proxy's SSE frames were semantically correct, but the HTTP layer could coalesce same-turn enqueues before flushing them downstream.

Root cause

bridgeToResponsesSSE consumes an async adapter event burst inside the ReadableStream.start() task. When the upstream response body supplied many parsed text deltas without an event-loop gap, the bridge called controller.enqueue() repeatedly in the same turn. Bun then exposed the queued frames to the downstream client as one large delivery.

This explains why the previous compression fix could pass a simple curl reproduction while the full Codex request shape still appeared buffered.

Fix

  • Track whether the bridge emitted output during the current turn.
  • Detect when the next adapter event arrives before the scheduled macrotask boundary.
  • Yield once with a zero-delay timer so Bun can flush queued SSE bytes before processing the next same-turn event.
  • Preserve normal provider-paced streams: when the upstream already waits between events, the scheduled timer overlaps that wait and adds no intentional fixed pacing.

Regression coverage

tests/bridge-live-delivery.test.ts starts a real Bun HTTP server, feeds the bridge 40 synchronous text_delta events, reads the response through fetch(), and asserts that deltas arrive across multiple reads rather than one end burst.

Verification

  • Focused bridge, SSE, header-policy, and openai-chat suites: 83 passed.
  • bun x tsc --noEmit: passed.
  • git diff --check: passed.
  • Full suite rerun: 2,398 passed, 3 skipped, 1 unrelated failure.
  • Remaining unrelated failure: Windows cannot create the symlink used by tests/claude-agents-inject.test.ts:92 (EPERM) without symlink privileges.

Risk and rollout

The change only affects bursty streaming delivery and keeps event ordering, terminal handling, cancellation, and heartbeat behavior unchanged. It may increase the number of downstream flush opportunities for providers that deliver many deltas in one response-body read, which is the intended behavior for interactive streaming.

Please retest with the real macOS Codex Desktop app-server path from issue #114.

Yield between same-turn bridge events so Bun can flush SSE frames before a synchronous upstream burst is coalesced into one downstream delivery. Add a real HTTP regression test for issue #114.
@lidge-jun

Copy link
Copy Markdown
Owner

Reviewed; the approach looks sound. Macrotask-boundary yielding in bridgeToResponsesSSE splits synchronous adapter bursts without reordering, benefits all routed providers, and leaves native Responses passthrough unaffected.

Before this leaves draft, we would like:

  1. The real macOS Codex Desktop app-server end-to-end retest referenced in OpenRouter (openai-chat adapter): streaming delivers tokens in batches, not live #114.
  2. A burst-duration measurement for large buffered replays. setTimeout(0) per emitted event could stretch replay of hundreds of queued events.
  3. A stronger regression test. tests/bridge-live-delivery.test.ts currently only asserts that deltas span more than one read; please assert multiple delivery groups or bound the largest group.

@Wibias

Wibias commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the measurable/regression-test requests in commit ca8426c.

  • tests/bridge-live-delivery.test.ts now records delivery groups and asserts the largest group is no more than 75% of the 40-event burst, rather than only checking that there are two reads.
  • Added a 200-event synchronous replay timing test with a 5-second upper bound. On the current Windows host, the test completed in approximately 317 ms wall time.
  • The focused bridge/provider suite is green: 84 passed, 0 failed; typecheck and git diff --check also pass.

The requested real macOS Codex Desktop app-server retest remains environment-blocked here because this runner is Windows and has no macOS app-server binary. The PR remains draft pending that owner-side macOS validation against the production Codex Desktop path described in #114.

@lidge-jun
lidge-jun marked this pull request as ready for review July 14, 2026 07:34
@lidge-jun
lidge-jun merged commit cdfa753 into lidge-jun:main Jul 14, 2026
6 checks passed
@Wibias
Wibias deleted the codex/fix-responses-sse-coalescing-pr branch July 15, 2026 17:59
agentHits pushed a commit to agentHits/opencodex that referenced this pull request Sep 17, 2026
…coalescing-pr

fix(streaming): prevent bursty Responses SSE delivery
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.

2 participants