Repository navigation
Share one server-sent events helper across the four streams - #434
Merged
abhishekgahlot2 merged 1 commit intoNov 10, 2026
Merged
Conversation
Contributor
|
Merged, thanks @coolestnick. One helper for all four streams means the next heartbeat or teardown fix lands everywhere at once, and closing the stream on a failed write or a cancelled body is a real improvement over what three of them did before. |
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Closes #325
The run, thread, chat and changes streams each built their own
ReadableStream, opening comment, 25 s heartbeat, header block and abort cleanup. This moves all of that into one helper,sseResponseinbackend/src/util/sse.ts, and the four routes now only write frames and hand back what to undo when the stream closes.What changed:
backend/src/util/sse.tsowns the stream, the: opencomment (with an optionalretry:), the heartbeat, the four anti-buffering headers and the teardown. It gives the routesend,sendEvent,closeand aclosedflag.runs/routes.ts(run stream and thread stream),chat/routes.tsandruns/changes-route.tsgo through it. The route logic itself is moved, not rewritten.runs/routes.tswent from 1009 to 919 lines, so its ratchet baseline is lowered to 919.backend/AGENTS.mdhas a new owner row so the next stream goes through the helper.One behaviour change to be aware of: every stream now closes, and runs its teardown, when a write fails because the client left or when the response body is cancelled. Before, only the changes stream closed on a failed write and none of the routes handled a cancelled body; they waited for the request's abort signal. Headers, frames and their order are unchanged.
Tests (
backend/src/util/sse.test.ts):retry:sendEventandsend25_000interval and thetext/event-stream; charset=utf-8header appear in only one non-test file underbackend/src. This one fails on main, where four files hold them.Checks:
bun run typecheckat the root -> okcd backend && bun run teston my own database -> 3586 pass, 3 skip, 0 fail, then the Slack suite 119 pass, 0 failbun test scripts/agent-docs.test.ts-> 5 passBefore / after
I captured all four streams with
curl -iagainst a local backend, first on main and then on this branch, and diffed the two captures. In the lines both captures share, the only differences were theDateheader and the thread stream's per-connectionepochid. The capture on main was cut off before the chat model's first text frame, so the chatdeltaframe is only in the capture from this branch. Holding the changes stream open for 27 s on this branch showed the: pingheartbeat.Changes stream, same on both:
Chat stream, same on both up to the
contextframe, thedeltaframe is from this branch only: