Repository navigation
feat(launch-intents): stream session_ready events to the bot via SSE - #66
Conversation
Add GET /api/launch-intents/events under the launch-intents:bot policy.
The stream sends connected, then session_ready {intentId} after the Ready
transition commits, plus heartbeats. An in-memory broadcaster with bounded
per-subscriber buffers disconnects slow readers so they reconcile via
/pending. Document the race-safe bootstrap, single-instance limits and rollout.
Closes #64
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d24711b507
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| **Race-safe bootstrap.** On every (re)connect: wait for `connected`, then call `GET /pending` once | ||
| and process its ready, unpublished intents. Anything committed before `connected` is in that | ||
| snapshot; anything after it arrives on the stream. Duplicates are possible, so handling must be |
There was a problem hiding this comment.
Reconcile all pending batches before switching to events only
If more than 100 unpublished ready intents accumulate while the bot is disconnected, this bootstrap permanently misses the remainder: LaunchIntentService.ListRecoverableAsync orders by creation time and applies .Take(100), so one /pending request is not a complete snapshot. After the bot acknowledges those first 100, the remaining intents emit no new events because their Ready transitions already occurred before subscription. Unlike the previous polling flow, the documented events-only flow never fetches them again until another disconnect. Provide a complete reconciliation mechanism, such as paginated draining or periodic reconciliation, before instructing clients to query only once.
Useful? React with 👍 / 👎.
| if (broadcaster.SubscriberCount >= Math.Max(1, options.Value.EventsMaxSubscribers)) | ||
| return Results.Json(new { code = "too_many_subscribers" }, statusCode: StatusCodes.Status503ServiceUnavailable); | ||
|
|
||
| var subscription = broadcaster.Subscribe(); |
There was a problem hiding this comment.
Enforce the subscriber limit atomically with registration
When multiple authenticated stream requests arrive concurrently near EventsMaxSubscribers, they can all observe a count below the limit before any calls Subscribe(), and all are then admitted. The concurrent dictionary makes individual operations thread-safe but does not make this check-and-register sequence atomic, so the advertised connection/resource bound can be exceeded. Move the capacity check and registration into a single atomic broadcaster operation, and return 503 when that operation cannot reserve a slot.
Useful? React with 👍 / 👎.
Summary
GET /api/launch-intents/eventsSSE stream under the existinglaunch-intents:botpolicy (service bearer). Anonymous, wrong-token and device callers are rejected.connected(first, sent after subscribing,retry: 5000),session_ready{"intentId"}with a sequenceid, andheartbeatevery 20s (configurable viaLaunchIntents:EventsHeartbeatSeconds).LaunchIntentService.CompleteShareAsyncpublishessession_readyonly afterSaveChangesAsynccommits. Idempotent retries and rejected/failed writes emit nothing.LaunchIntentEventBroadcaster(singleton, in-memory): bounded 64-event buffer per subscriber. A slow subscriber is disconnected instead of losing events silently, so it reconnects and reconciles. Streams are capped byLaunchIntents:EventsMaxSubscribers(default 8, then 503). There is no per-client DB poll or timer beyond the heartbeat delay./pendingand every other route are unchanged and stay the source of truth for reconciliation.connected, then reconcile/pendingonce), single-instance limits, and rollout order indocs/discord-activity-launch-contract.md.Tests
LaunchIntentEventsTests(10 new) covers auth rejection, onesession_readyafter commit (none for a retry or a rejected completion), payload minimality, the detail and ack flow, heartbeats, disconnect cleanup, the subscriber cap, reconciliation after a missed event, fan-out ordering, slow-subscriber overflow, and dispose.dotnet test tests/SonicRelay.Api.IntegrationTests: 284 passed.Rollout
Deploy this first. The current polling bot keeps working. Then enable SSE in the bot (hugoalves-dev/go_discord_FrameRelay#15).
Closes #64