Skip to content

feat(encounters): ephemeral Co-DM presence on the encounter SSE stream (#2209) - #2210

Merged
AwsomeFox merged 2 commits into
mainfrom
copilot/backlog-2209-encounter-presence
Aug 11, 2026
Merged

AwsomeFox merged 2 commits into
mainfrom
copilot/backlog-2209-encounter-presence

Conversation

@AwsomeFox

Copy link
Copy Markdown
Owner

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.presence snapshot 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.
  • New CampaignEvent variant encounter.presence carrying 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 in src/); 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.
  • REST surface on EncountersController: GET/POST/DELETE /encounters/:id/presence. POST declares + heartbeats (silent on a same-activity heartbeat so it does not spam the stream) and returns the snapshot; DELETE leaves; GET reads.
  • Secrecy / role filtering (AC4, AC5): 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 hidden encounter's existence never leaks. Non-DMs also 404 on the declare/read routes (same hidden gate as GET /encounters/:id). userId is the membership-roster identity space (already visible to every member), so the frame carries nothing role-dependent.
  • Presence is transport-level state (like an SSE keepalive): not persisted, not audited. A restart drops everyone; a reconnect re-declares (AC3).
  • campaign-event-projection classifies encounter.presence as broadcast-safe (role-dependent secrecy is enforced upstream at emit, not by projection — same pattern as dice.rolled).

Acceptance criteria

  • Server-side presence registry tracks (userId, encounterId, activity: viewing|editing).
  • Presence broadcast as a campaign event on the existing SSE stream on join/leave/heartbeat-expire.
  • Presence expires on heartbeat timeout (lazy sweep); reconnect restores it (re-declare re-broadcasts).
  • Role-filtered: visible only to same-campaign members (SSE membership gate + campaign-scoped stream).
  • No leak on hidden prep: hidden-encounter presence is DM-only; non-DMs 404 and never receive the frame.
  • Server tests cover join/leave/expire lifecycle, role filtering, and SSE delivery.

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/server tests: new encounter-presence.e2e-spec.ts (18/18 ✅), campaign-event-projection.spec.ts (9/9 ✅), sse.e2e-spec.ts ✅.

Note: some encounters.e2e-spec.ts concurrency tests (read ECONNRESET / socket hang up) flake locally under heavy concurrent agent-session load on this machine (AGENTS.md documents this exact hazard). They are non-deterministic connection resets in unmodified combat/turn/AoE tests, unrelated to this additive change; CI runs in isolation.

#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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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/schema with encounter presence types and a new CampaignEvent variant encounter.presence.
  • Add EncounterPresenceService (in-memory registry + TTL-based lazy expiry + role-routed SSE emission) and wire it into the encounters module/controller via GET/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.

Comment thread packages/schema/src/index.ts Resolved

@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: 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.

@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: 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".

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.

Co-DM presence: ephemeral authenticated presence on the encounter SSE stream (#816 slice 1)

2 participants