Skip to content

fix: Add cloud infrastructure for user message routing#213

Merged
khaliqgant merged 2 commits into
mainfrom
fix/cross-machine-user-routing
Jan 18, 2026
Merged

fix: Add cloud infrastructure for user message routing#213
khaliqgant merged 2 commits into
mainfrom
fix/cross-machine-user-routing

Conversation

@khaliqgant

Copy link
Copy Markdown
Member

Summary

Completes the cross-machine user routing feature by adding missing cloud infrastructure. The previous daemon-side fix (PR #212, commit 41d7b4f) was correct but incomplete - the cloud server lacked the necessary components to actually discover and deliver messages to users.

Root Cause Analysis

Not a Regression - Missing Feature

Investigation via git log and git blame revealed this is not a regression:

Date Commit Description
Dec 30, 2025 ba37864 Cloud-daemon linking added - agents only
Jan 1, 2026 37996c0 Cross-machine integration completed - agents only
Jan 17, 2026 41d7b4f First user routing fix - daemon-side only
Jan 18, 2026 126e1f8 This fix - cloud infrastructure

The original cross-machine routing (commits ba37864, 37996c0) was designed exclusively for agent-to-agent communication. User routing was never implemented:

  1. /api/daemons/agents returned allAgents but never allUsers
  2. No presence tracking accessible to API routes
  3. No mechanism to deliver messages to cloud-connected users

Why First Fix (41d7b4f) Didn't Work

The daemon-side code was correct:

  • Added isRemoteUser() method
  • Router checks for remote users
  • Cloud-sync expects allUsers from API

But the cloud server never returned allUsers because:

  • onlineUsers Map was local to WebSocket handler in server.ts
  • API routes in daemons.ts couldn't access it
  • No message delivery mechanism for daemonId: 'cloud'

What This Fix Does

New Services

  1. PresenceRegistry (src/cloud/services/presence-registry.ts)

    • Shared module for tracking online users
    • Accessible to both WebSocket handlers and API routes
    • Exports getOnlineUsersForDiscovery() in RemoteAgent format
  2. CloudMessageBus (src/cloud/services/cloud-message-bus.ts)

    • Event-based pub/sub for message delivery
    • API routes publish, WebSocket handlers subscribe
    • Delivers messages to user's presence WebSocket connections

Updated Components

  1. /api/daemons/agents endpoint - Now returns allUsers alongside allAgents

  2. /api/daemons/message endpoint - Special handling for targetDaemonId === 'cloud'

  3. server.ts - Registers/unregisters users with presence registry, subscribes to message bus

Message Flow

Agent → Daemon → POST /api/daemons/message (daemonId=cloud)
  → CloudMessageBus.sendToUser()
  → wssPresence WebSocket
  → User Dashboard

Test Coverage

Added 23 new tests:

  • PresenceRegistry (17 tests): registration, unregistration, lastSeen updates, online status, discovery format
  • CloudMessageBus (6 tests): event emission, metadata, multiple subscribers, independent delivery

Files Changed

  • src/cloud/services/presence-registry.ts (new)
  • src/cloud/services/cloud-message-bus.ts (new)
  • src/cloud/services/presence-registry.test.ts (new)
  • src/cloud/services/cloud-message-bus.test.ts (new)
  • src/cloud/services/index.ts (exports)
  • src/cloud/api/daemons.ts (allUsers + cloud routing)
  • src/cloud/server.ts (presence registry + message bus)

Test Plan

  • All existing tests pass (99 tests)
  • New service tests pass (23 tests)
  • TypeScript compiles without errors
  • Manual test: Agent → User message delivery via cloud dashboard

🤖 Generated with Claude Code

Agent Relay and others added 2 commits January 18, 2026 08:36
The previous fix (41d7b4f) added daemon-side support for remote users,
but the cloud server was missing critical infrastructure:

1. Added PresenceRegistry service - shared module that tracks online
   users, accessible to both WebSocket handler and API routes

2. Added CloudMessageBus service - event-based pub/sub for delivering
   messages to cloud users via their WebSocket connections

3. Updated /api/daemons/agents to return allUsers from presence registry
   so daemons can discover cloud-connected users

4. Updated /api/daemons/message to handle daemonId='cloud' case,
   delivering messages to users via CloudMessageBus

5. Updated server.ts to register/unregister users with presence registry
   and subscribe to CloudMessageBus for WebSocket delivery

Message flow for cloud users:
Agent -> Daemon -> POST /api/daemons/message (daemonId=cloud)
-> CloudMessageBus.sendToUser() -> Presence WebSocket -> User Dashboard

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Add test coverage for the new cloud infrastructure:

- PresenceRegistry tests (17 tests):
  - User registration and unregistration
  - Last seen updates
  - Online status checks
  - getOnlineUsersForDiscovery() RemoteAgent format

- CloudMessageBus tests (6 tests):
  - Event emission for user messages
  - Metadata inclusion
  - Multiple subscribers
  - Independent user message delivery

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@my-senior-dev-pr-review

Copy link
Copy Markdown

🤖 My Senior Dev — Analysis Complete

👤 For @khaliqgant

📁 Expert in src/dashboard/react-components/ (10 edits) • ⚡ 69th PR this month

View your contributor analytics →


📊 7 files reviewed • 3 high risk • 2 need attention

🚨 High Risk:

  • src/cloud/api/daemons.ts — Contains critical message routing logic with potential security risks and performance issues.

⚠️ Needs Attention:

  • src/cloud/api/daemons.ts — Critical for message handling and user tracking; concerns about performance under load need examination.

🚀 Open Interactive Review →

The full interface unlocks features not available in GitHub:

  • 💬 AI Chat — Ask questions on any file, get context-aware answers
  • 🔍 Smart Hovers — See symbol definitions and usage without leaving the diff
  • 📚 Code Archeology — Understand how files evolved over time (/archeology)
  • 🎯 Learning Insights — See how this PR compares to similar changes

💬 Chat here: @my-senior-dev explain this change — or try @chaos-monkey @security-auditor @optimizer @skeptic @junior-dev

📖 View all 12 personas & slash commands

You can interact with me by mentioning @my-senior-dev in any comment:

In PR comments or on any line of code:

  • Ask questions about the code or PR
  • Request explanations of specific changes
  • Get suggestions for improvements

Slash commands:

  • /help — Show all available commands
  • /archeology — See the history and evolution of changed files
  • /profile — Performance analysis and suggestions
  • /expertise — Find who knows this code best
  • /personas — List all available AI personas

AI Personas (mention to get their perspective):

Persona Focus
@chaos-monkey 🐵 Edge cases & failure scenarios
@skeptic 🤨 Challenge assumptions
@optimizer Performance & efficiency
@security-auditor 🔒 Security vulnerabilities
@accessibility-advocate Inclusive design
@junior-dev 🌱 Simple explanations
@tech-debt-collector 💳 Code quality & shortcuts
@ux-champion 🎨 User experience
@devops-engineer 🚀 Deployment & scaling
@documentation-nazi 📚 Documentation gaps
@legacy-whisperer 🏛️ Working with existing code
@test-driven-purist Testing & TDD

For the best experience, view this PR on myseniordev.com — includes AI chat, file annotations, and interactive reviews.

@khaliqgant khaliqgant merged commit f2ed968 into main Jan 18, 2026
8 checks passed
@khaliqgant khaliqgant deleted the fix/cross-machine-user-routing branch January 18, 2026 08:59
khaliqgant pushed a commit that referenced this pull request Jan 18, 2026
Records completed work on cross-machine user message routing.
- Added cloud infrastructure (PresenceRegistry, CloudMessageBus)
- Created PR #213 with 23 comprehensive tests
- Identified as missing feature (not regression)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
khaliqgant pushed a commit that referenced this pull request Jan 18, 2026
Records completed work on cross-machine user message routing.
- Added cloud infrastructure (PresenceRegistry, CloudMessageBus)
- Created PR #213 with 23 comprehensive tests
- Identified as missing feature (not regression)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
khaliqgant pushed a commit that referenced this pull request Jan 18, 2026
Records completed work on cross-machine user message routing.
- Added cloud infrastructure (PresenceRegistry, CloudMessageBus)
- Created PR #213 with 23 comprehensive tests
- Identified as missing feature (not regression)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
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.

1 participant