Skip to content

feat(launch-intents): stream session_ready events to the bot via SSE - #66

Merged
hugoalves-dev merged 1 commit into
mainfrom
hugo/github-issue-64-8d1efc
Nov 22, 2026
Merged

hugoalves-dev merged 1 commit into
mainfrom
hugo/github-issue-64-8d1efc

Conversation

@hugoalves-dev

Copy link
Copy Markdown
Owner

Summary

  • New GET /api/launch-intents/events SSE stream under the existing launch-intents:bot policy (service bearer). Anonymous, wrong-token and device callers are rejected.
  • Frames: connected (first, sent after subscribing, retry: 5000), session_ready {"intentId"} with a sequence id, and heartbeat every 20s (configurable via LaunchIntents:EventsHeartbeatSeconds).
  • LaunchIntentService.CompleteShareAsync publishes session_ready only after SaveChangesAsync commits. 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 by LaunchIntents:EventsMaxSubscribers (default 8, then 503). There is no per-client DB poll or timer beyond the heartbeat delay.
  • /pending and every other route are unchanged and stay the source of truth for reconciliation.
  • Docs: SSE contract, race-safe bootstrap (wait for connected, then reconcile /pending once), single-instance limits, and rollout order in docs/discord-activity-launch-contract.md.

Tests

LaunchIntentEventsTests (10 new) covers auth rejection, one session_ready after 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

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-22T23:41:35.260887Z d24711b PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +49 to +51
**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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Comment on lines +70 to +73
if (broadcaster.SubscriberCount >= Math.Max(1, options.Value.EventsMaxSubscribers))
return Results.Json(new { code = "too_many_subscribers" }, statusCode: StatusCodes.Status503ServiceUnavailable);

var subscription = broadcaster.Subscribe();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@hugoalves-dev
hugoalves-dev merged commit 5874055 into main Nov 22, 2026
5 checks passed
@hugoalves-dev
hugoalves-dev deleted the hugo/github-issue-64-8d1efc branch November 22, 2026 23:53
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.

feat(launch-intents): stream authenticated session-ready events via SSE instead of polling

1 participant