fix: SSE robustness — race, idle timeout, replay, circuit breaker (#308)#332
Merged
OneStepAt4time merged 7 commits intomainfrom Mar 27, 2026
Merged
fix: SSE robustness — race, idle timeout, replay, circuit breaker (#308)#332OneStepAt4time merged 7 commits intomainfrom
OneStepAt4time merged 7 commits intomainfrom
Conversation
Generated by Hephaestus (Aegis dev agent)
Generated by Hephaestus (Aegis dev agent)
The 1-second setTimeout after emitEnded blindly deleted whatever emitter was mapped to the session ID, which could nuke a fresh emitter created by a new subscriber during that window. Now captures the emitter reference and only deletes if it's still the same instance. Generated by Hephaestus (Aegis dev agent)
SessionEventBus.emit() now uses setImmediate to deliver events asynchronously, preventing slow SSE writes from blocking the monitor loop. Updated all test files to flush async before asserting. Generated by Hephaestus (Aegis dev agent)
Per-session ring buffer (50 events) with incrementing IDs enables SSE clients to replay missed events after reconnection via the standard Last-Event-ID header. Generated by Hephaestus (Aegis dev agent)
…te (#308) - Per-session SSE handler now reads Last-Event-ID header and replays missed events from the ring buffer - Both SSE handlers track last write time and destroy zombie connections after 90s of inactivity (checked on heartbeat tick) - Removed duplicate /sessions/:id/events route (use /v1/ prefix) Generated by Hephaestus (Aegis dev agent)
Wraps browser EventSource with exponential backoff (1s to 30s cap), 5-minute give-up timeout, and failure counter reset on successful connection. subscribeSSE and subscribeGlobalSSE now use it. Generated by Hephaestus (Aegis dev agent)
OneStepAt4time
added a commit
that referenced
this pull request
Mar 27, 2026
Buffer last 50 global SSE events so clients connecting mid-session get recent events replayed via Last-Event-ID header on the /v1/events endpoint. Mirrors the per-session ring buffer pattern from PR #332. Generated by Hephaestus (Aegis dev agent)
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.
Summary
Fixes 6 SSE/event bus robustness issues under production load:
emitEndedsetTimeout no longer deletes fresh emitters created by new subscribers during the 1s cleanup windowSessionEventBus.emit()now usessetImmediateto decouple from the monitor loop, preventing slow SSE writes from blocking pollingLast-Event-IDreplay on reconnection/sessions/:id/eventsroute; all routes now use/v1/prefixResilientEventSourcewraps browserEventSourcewith exponential backoff (1s→30s cap), 5-min give-up, and failure counter reset on successFiles changed:
src/events.ts— ring buffer, setImmediate emit, cleanup guardsrc/server.ts— Last-Event-ID replay, idle timeout, removed duplicate routedashboard/src/api/client.ts— uses ResilientEventSourcedashboard/src/api/resilient-eventsource.ts— new modulesrc/__tests__/sse-events.test.ts— ring buffer tests, async flush fixessrc/__tests__/hooks.test.ts— async flush fixessrc/__tests__/latency.test.ts— async flush fixessrc/__tests__/monitor-fixes.test.ts— async flush fixesdashboard/src/__tests__/resilient-eventsource.test.ts— new test fileTest plan
npm test— 882/882 pass (root)npm test— 9/9 pass (dashboard)npx tsc --noEmit— cleannpm run build— cleanCloses #308
Generated by Hephaestus (Aegis dev agent)