refactor(server): share event stream encoding#36484
Open
kitlangton wants to merge 2 commits into
Open
Conversation
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.
What
Encode public server events once before delivering them to connected TUI and API clients.
Previously, every
/api/eventconnection independently schema-encoded, JSON-serialized, and SSE-framed the same event. The new Server feed performs that work once and offers the same immutable frame string to each connection's independently bounded queue.Disposable encoding benchmark for 1,000 representative 8 KiB public events, using one warmup and nine measured runs:
The benchmark isolated schema, JSON, and SSE encoding. It was intentionally not committed because it did not exercise sockets or the complete HTTP stack.
How
packages/server/src/event-feed.tsinstalls one global Core observer, filters public events once, encodes one complete SSE frame, and offers it to per-connectionQueue.droppingqueues.packages/server/src/handlers/event.tskeeps the raw HttpApi route, connection-specificserver.connected, heartbeat, headers, and wire contract unchanged.packages/core/src/event.tsremoves the deadliveBoundedhelper after its only production caller moved to the Server feed.specs/v2/event-stream-architecture.mdrecords the ownership boundary, rejected PubSub design, lifecycle, capacity semantics, and benchmark results.Scope
EventLoggerbehavior are unchanged.Last-Event-ID, byte-based capacity, or shared PubSub cursor eviction.Testing
bun run testinpackages/server: 5 passed, including exact SSE frame compatibility, encode-once fan-out, public filtering, independent overflow, and encoding failure isolation.bun typecheckinpackages/server: passed.bun run test test/event.test.tsinpackages/core: 51 passed.bun typecheckinpackages/core: passed.bun run test test/embedded.test.tsinpackages/sdk-next: 7 passed against the real embedded router and generated client.packages/core/src/event.ts.EventLoggercleanup was removed from scope.Additional unrelated failures observed:
packages/clientPromise tests expect the staleserver.mcpgenerated group while the current client exposesmcp; event Effect and Promise stream tests pass.packages/opencodeHTTP SDK tests return existing 500s in unrelated session/prompt scenarios.Flow
sequenceDiagram participant Core as EventV2 participant Feed as Server EventFeed participant A as TUI A queue participant B as TUI B queue Core->>Feed: publish one public payload Feed->>Feed: schema + JSON + SSE encode once Feed->>A: offer shared frame reference Feed->>B: offer shared frame reference alt A is full Feed--xA: fail only A with overflow end B-->>B: continue streaming in order