Repository navigation
feat(encounters): ephemeral Co-DM presence on the encounter SSE stream (#2209) - #2210
Conversation
#2209) Add the data-foundation slice of Co-DM presence (#816 slice 1): an ephemeral, authenticated, in-memory registry of which members are on an encounter's live surface, broadcast as an `encounter.presence` snapshot on the existing campaign-event SSE stream. UI rendering is a subsequent slice. Schema (@campfire/schema) - EncounterPresenceActivity ('viewing' | 'editing'), EncounterPresenceEntry, EncounterPresenceSnapshot, EncounterPresenceDeclare request body. - New CampaignEvent variant `encounter.presence` carrying a full snapshot (campaignId, encounterId, members[]) so a reconnecting/late-joining client reconciles by replacing its local set — there is no persisted history to refetch. Server - EncounterPresenceService: in-memory registry keyed by (encounterId, userId), kept alive by declare/heartbeat, lazily reaped on TTL expiry (no background timer — the server module has none). Emits a snapshot on join / activity change / leave / heartbeat-expire. - REST surface on EncountersController: GET/POST/DELETE /encounters/:id/presence. POST declares+heartbeats (silent on a same-activity heartbeat) and returns the snapshot; DELETE leaves; GET reads. - Secrecy: a hidden encounter's presence is routed DM-only via an audience predicate at emit time (hidden re-read fresh, mirroring emitEncounterEvent), so a non-DM never receives the frame and the encounter's existence never leaks. Presence is transport-level state (like an SSE keepalive): not persisted, not audited. Restart drops everyone; reconnect re-declares. - campaign-event-projection classifies `encounter.presence` as broadcast-safe (role-dependent secrecy is enforced upstream at emit, not by projection). Tests - encounter-presence.e2e-spec.ts: join/leave/expire lifecycle, heartbeat-no-op, activity change, reconnect restore, hidden-encounter role filtering (404 + DM- only delivery), campaign isolation, multi-participant snapshots, strict-body validation. 18 cases. - campaign-event-projection.spec.ts: sample for the new frame. Closes #2209.
There was a problem hiding this comment.
Pull request overview
Adds an ephemeral, authenticated encounter-presence signal (viewing/editing) backed by an in-memory server registry and broadcast as a new encounter.presence campaign-event over the existing SSE stream, with REST endpoints and server-side tests.
Changes:
- Extend
@campfire/schemawith encounter presence types and a newCampaignEventvariantencounter.presence. - Add
EncounterPresenceService(in-memory registry + TTL-based lazy expiry + role-routed SSE emission) and wire it into the encounters module/controller viaGET/POST/DELETE /encounters/:id/presence. - Add/extend server tests covering lifecycle (join/leave/expire), secrecy/role filtering for hidden encounters, and SSE delivery.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/schema/src/index.ts | Adds presence schemas and the encounter.presence campaign-event variant. |
| apps/server/src/modules/encounters/encounter-presence.service.ts | Implements the in-memory presence registry, TTL expiry sweep, and SSE snapshot emission. |
| apps/server/src/modules/encounters/encounters.controller.ts | Exposes REST endpoints to read/declare/leave presence and wires them to the service. |
| apps/server/src/modules/encounters/encounters.dto.ts | Adds DTO for the declare/heartbeat request body. |
| apps/server/src/modules/encounters/encounters.module.ts | Registers EncounterPresenceService in the encounters module providers. |
| apps/server/src/modules/events/campaign-event-projection.ts | Marks encounter.presence as broadcast-safe (with upstream audience routing). |
| apps/server/test/encounter-presence.e2e-spec.ts | Adds end-to-end coverage for presence lifecycle, secrecy, and SSE delivery. |
| apps/server/test/unit/campaign-event-projection.spec.ts | Adds a projection sample for the new event type. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5decbba57c
ℹ️ 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".
…pshot - DELETE /encounters/:id/presence now applies the same hidden-encounter visibility gate as declare (isVisibleTo + 404), so a non-DM member cannot probe encounter IDs via a leave request. - EncounterPresenceSnapshot now includes campaignId (matching the PR description), and readSnapshot/leave capture it before bucket cleanup. - Role re-eval at emit time: the hidden gate re-reads the encounter's hidden column fresh (currentHidden), consistent with the existing emitEncounterEvent pattern. The SSE audience predicate is membership- time, which is the same correctness level every other encounter.* event uses — re-evaluating per-event would be a cross-cutting change for the entire SSE layer, out of scope for this slice.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2b2ee9c5e2
ℹ️ 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".
Closes #2209 — #816 slice 1.
What this adds
The data-foundation slice of Co-DM presence: an ephemeral, authenticated, in-memory registry of which members are on a running encounter's live surface, broadcast as an
encounter.presencesnapshot on the existing campaign-event SSE stream. UI rendering is out of scope (subsequent slice).Design
Schema (
@campfire/schema)EncounterPresenceActivity(viewing|editing),EncounterPresenceEntry,EncounterPresenceSnapshot,EncounterPresenceDeclare.CampaignEventvariantencounter.presencecarrying a full snapshot (campaignId,encounterId,members[]). A snapshot (not a delta) means a reconnecting or late-joining client reconciles by replacing its local set — there is no persisted presence history to refetch.Server (
apps/server)EncounterPresenceService: in-memory registry keyed by(encounterId, userId), kept alive by declare/heartbeat, lazily reaped on TTL expiry. The server module intentionally has no background timers (none exist anywhere insrc/); expiry is reaped on the next registry access, which is also when the leave frame is emitted — the only time that emit matters is when an observer is watching. Emits a snapshot on join / activity change / explicit leave / heartbeat-expire.EncountersController:GET/POST/DELETE /encounters/:id/presence.POSTdeclares + heartbeats (silent on a same-activity heartbeat so it does not spam the stream) and returns the snapshot;DELETEleaves;GETreads.hiddenre-read fresh, mirroringemitEncounterEvent), so a non-DM never receives the frame and the hidden encounter's existence never leaks. Non-DMs also 404 on the declare/read routes (same hidden gate asGET /encounters/:id).userIdis the membership-roster identity space (already visible to every member), so the frame carries nothing role-dependent.campaign-event-projectionclassifiesencounter.presenceas broadcast-safe (role-dependent secrecy is enforced upstream at emit, not by projection — same pattern asdice.rolled).Acceptance criteria
(userId, encounterId, activity: viewing|editing).Out of scope (subsequent slices)
UI rendering, advisory intent, warnings/leases, tab-close/sleep detection beyond SSE disconnect.
Validation
npm run typecheck✅npm run lint✅ (no new warnings in changed files)npm run check:mcp-catalog✅ (277 tools — presence is a human/SSE feature; MCP has no SSE stream, so no MCP surface is required for this ephemeral transport state)npm run check:i18n✅apps/servertests: newencounter-presence.e2e-spec.ts(18/18 ✅),campaign-event-projection.spec.ts(9/9 ✅),sse.e2e-spec.ts✅.