Skip to content

fix(control-plane): dedupe presence list by participantId#622

Merged
ColeMurray merged 2 commits into
ColeMurray:mainfrom
donnfelker:upstream-presence-dedupe-by-participant
May 17, 2026
Merged

fix(control-plane): dedupe presence list by participantId#622
ColeMurray merged 2 commits into
ColeMurray:mainfrom
donnfelker:upstream-presence-dedupe-by-participant

Conversation

@donnfelker
Copy link
Copy Markdown
Contributor

@donnfelker donnfelker commented May 11, 2026

Fixes a bug where a prompt engineer would show up twice (or more) based upon how many tabs they had open. This change ensures that they only show up once in the list of prompt engineers.

Summary

  • The presence list returned one entry per WebSocket client, so a single user with multiple connections (e.g. two browser tabs) appeared as multiple participants - rendering duplicate avatars in the UI and producing React duplicate-key warnings.
  • Dedupe getPresenceList() output by participantId: any active socket marks the participant active, and the most recent lastSeen across sockets wins.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed duplicate presence entries for the same participant when connected from multiple tabs/devices; presence now shows a single entry per participant.
    • Presence now uses the most recent activity timestamp and treats any active connection as active for the participant, preventing premature removals.
  • Tests
    • Added integration and unit tests validating presence deduplication and correct presence_update vs presence_leave behavior.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 11, 2026

📝 Walkthrough

Walkthrough

getPresenceList() now deduplicates presence entries by participantId, merging status as "active" if any socket is active and using the most recent lastSeen. Durable-object close logic refreshes presence when other sockets remain. Unit and integration tests verify these behaviors.

Changes

Presence List Deduplication

Layer / File(s) Summary
Core Implementation
packages/control-plane/src/session/presence-service.ts
getPresenceList() builds a Map keyed by participantId to merge multiple sockets per participant. Status is "active" if any connection is active; lastSeen is the maximum timestamp across connections.
DurableObject websocket close handling
packages/control-plane/src/session/durable-object.ts
On WebSocket close, check for other authenticated sockets for the same participant: broadcast a presence refresh if others exist, otherwise emit presence_leave.
Unit tests for deduping
packages/control-plane/src/session/presence-service.test.ts
Adds two tests: one asserts a participant with multiple sockets is deduped into one presence entry with merged status and lastSeen; the other asserts distinct participants remain separate.
Integration tests for presence messages
packages/control-plane/test/integration/websocket-client.test.ts
Adds two integration tests that assert closing one of multiple sockets triggers presence_update (not presence_leave) and closing the sole socket broadcasts presence_leave with the leaving userId.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I’m a rabbit on the network trail,
Tabs may hop but I count as hale.
When sockets double and messages blend,
I stitch the lastSeen and call a friend.
One participant, whole at the end. 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: deduplicating the presence list by participantId to fix the bug where users with multiple connections appear multiple times.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Owner

@ColeMurray ColeMurray left a comment

Choose a reason for hiding this comment

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

[Automated Review] Requesting changes — see inline comment for the symmetric presence_leave bug not addressed by this PR. The dedupe fix here is correct, but its mirror image on disconnect partially undoes the user-visible improvement.

Comment thread packages/control-plane/src/session/presence-service.ts
When a participant holds multiple WebSocket connections (e.g. two
browser tabs) and one disconnects, the previous code broadcast
presence_leave with the user's userId. The web client filters all
entries by userId on presence_leave, so the user would disappear
from every viewer's UI even though they were still connected on
another tab — partially undoing the dedupe-by-participantId fix.

Check whether any other authenticated socket still belongs to the
same participantId before deciding which message to send. If yes,
broadcast a deduped presence_update via broadcastPresence(). If
not, fall back to the existing presence_leave path.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/control-plane/test/integration/websocket-client.test.ts (1)

275-278: ⚡ Quick win

Extract timeoutMs into a named constant with unit.

Both new tests restate 2000. Please define one local constant (for example, PRESENCE_MESSAGE_TIMEOUT_MS) and reuse it.

As per coding guidelines, "Define each default timeout value exactly once as a named constant and import it everywhere rather than restating literal values" and "Do not use bare timeout variable names; always encode the unit (seconds for Python, milliseconds for TypeScript) in the variable name".

Also applies to: 300-303

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/control-plane/test/integration/websocket-client.test.ts` around
lines 275 - 278, Introduce a named constant for the timeout (e.g.,
PRESENCE_MESSAGE_TIMEOUT_MS = 2000) and replace the hard-coded 2000 in the
collectMessages call(s) with that constant; update both occurrences (the
collector created for tab1.ws and the similar call around lines 300-303) so they
use PRESENCE_MESSAGE_TIMEOUT_MS to encode milliseconds and avoid repeating the
literal.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/control-plane/test/integration/websocket-client.test.ts`:
- Around line 275-278: Introduce a named constant for the timeout (e.g.,
PRESENCE_MESSAGE_TIMEOUT_MS = 2000) and replace the hard-coded 2000 in the
collectMessages call(s) with that constant; update both occurrences (the
collector created for tab1.ws and the similar call around lines 300-303) so they
use PRESENCE_MESSAGE_TIMEOUT_MS to encode milliseconds and avoid repeating the
literal.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: df0ede4c-837e-4dc5-9c7d-c026a5eb9fff

📥 Commits

Reviewing files that changed from the base of the PR and between c595f69 and 2cb1f94.

📒 Files selected for processing (2)
  • packages/control-plane/src/session/durable-object.ts
  • packages/control-plane/test/integration/websocket-client.test.ts

@ColeMurray ColeMurray merged commit a1d1975 into ColeMurray:main May 17, 2026
18 checks passed
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.

2 participants