Skip to content

Rearchitect Domain Events to Support source_client_id Filtering #665

Closed

Description

@malibio

Overview

The current domain event architecture emits events from SurrealStore (data layer) without any client identification, causing feedback loops when Tauri receives its own events back during optimistic UI updates. This needs to be rearchitected so events are emitted at the NodeService layer with source_client_id awareness.

Problem Statement

Current Architecture (Broken):

  1. Frontend performs optimistic UI update
  2. Tauri command calls NodeService → SurrealStore
  3. SurrealStore.emit_event() broadcasts DomainEvent (no client_id)
  4. DomainEventForwarder receives event and forwards to Tauri frontend
  5. Frontend receives its own change back → state corruption/race conditions

Evidence:

Root Cause:

  • emit_event() is called directly in SurrealStore (data layer) with no client context
  • No mechanism to pass client_id through the call chain
  • DomainEventForwarder forwards ALL events blindly

Proposed Solution

Move event emission to the NodeService layer with client_id awareness:

Target Architecture:

  1. Each client (Tauri, MCP, proxy, cloud sync) has a unique client_id
  2. NodeService methods accept optional client_id parameter (or use context)
  3. Events are emitted at NodeService level with source_client_id
  4. DomainEventForwarder filters events matching subscriber's client_id

Key Design Decision:

  • NodeService should be the shared service layer for ALL clients
  • Data layer (SurrealStore) remains unaware of clients
  • All entry points (Tauri commands, MCP handlers, HTTP proxy) go through NodeService

Implementation Approach

Phase 1: Refactor Event Emission Location

  • Remove emit_event() calls from SurrealStore
  • Add event emission to NodeService after successful operations
  • Keep SurrealStore.event_tx broadcast channel for now (or move to NodeService)

Phase 2: Add Client Context

  • Create ClientContext type with client_id: String
  • Add with_client() or similar method to NodeService for setting context
  • Update DomainEvent struct to include source_client_id: Option<String>

Phase 3: Update Event Consumers

  • Update DomainEventForwarder to accept Tauri's client_id
  • Filter events where source_client_id == subscriber_client_id
  • Re-enable DomainEventForwarder in db.rs

Phase 4: Fix Layering Violations

  • Audit Tauri commands for direct SurrealStore calls
  • Route all operations through NodeService
  • get_all_edges in nodes.rs:890 is only current violation (read-only, low priority)

Acceptance Criteria

  • Events emitted at NodeService layer, not SurrealStore
  • DomainEvent includes source_client_id: Option<String>
  • Tauri frontend does not receive its own events back
  • MCP client events are properly identified and filterable
  • Rapid Enter + Shift+Tab no longer causes "Sibling not found" errors
  • Browser mode (dev-proxy.rs) continues to work
  • Code passes bun run quality:fix
  • Tests pass (bun run test:all)

Technical Specifications

Reference Files

  • SurrealStore: packages/core/src/db/surreal_store.rs - Current event emission location
  • DomainEvent: packages/core/src/db/events.rs - Event types (needs source_client_id)
  • NodeService: packages/core/src/services/node_service.rs - Target event emission location
  • DomainEventForwarder: packages/desktop-app/src-tauri/src/services/domain_event_forwarder.rs - Needs filtering
  • db.rs: packages/desktop-app/src-tauri/src/commands/db.rs - DomainEventForwarder currently disabled
  • dev-proxy.rs: packages/desktop-app/src-tauri/src/bin/dev-proxy.rs - Working client_id example

Current emit_event Locations in SurrealStore

Lines: 975, 1206, 1207, 1443, 1615, 1722, 1753, 1840, 2943, 2948, 3010, 3031

Browser Mode Pattern (Reference)

// dev-proxy.rs has working client_id filtering
pub enum SseEvent {
    NodeCreated {
        node_id: String,
        node_data: Node,
        client_id: Option<String>,  // ← This is what we need
    },
    // ...
}

// Filter logic
if let (Some(this_client), Some(event_client)) = (&client_id, event_client_id) {
    if this_client == event_client {
        return None; // Filter out own events
    }
}

Dependencies

Related Issues

Non-Goals

  • Changing the broadcast channel mechanism (tokio broadcast is fine)
  • Adding client_id to every database operation signature
  • Real-time collaborative editing (future scope)

Activity

  1. malibio commented on Nov 28, 2025

    @malibio
    CollaboratorAuthor

    Test Baseline (Starting Work)

    Frontend: ✅ 728 tests passing
    Backend: ✅ 532 tests passing (3 ignored)

    No test failures at start of work.

  2. self-assigned this
    on Nov 28, 2025
  3. malibio commented on Nov 28, 2025

    @malibio
    CollaboratorAuthor

    Phase 1 Complete - Handoff Notes

    Work Completed

    ✅ Phase 1: DomainEvent Infrastructure

    • Added source_client_id: Option<String> to all 6 DomainEvent variants
    • Updated 12 emit_event() call sites in SurrealStore (temporarily with None)
    • Updated DomainEventForwarder to destructure new event format
    • Fixed event_type() helper and all test pattern matching
    • Tests Status: ✅ Backend: 516 passing, ✅ Frontend: 728 passing

    Recommended Implementation Path

    After analyzing the codebase, I recommend NOT moving event emission to NodeService (as originally proposed). Here's why:

    Current Working Architecture:

    Tauri Commands → NodeService → SurrealStore → emit_event() → DomainEventForwarder → Frontend
    

    Simpler Solution (Recommended):

    1. Add client_id: Option<String> parameter to SurrealStore methods
    2. Thread client_id from Tauri commands through NodeService to SurrealStore
    3. Pass client_id to emit_event() calls (already have infrastructure from Phase 1)
    4. Filter in DomainEventForwarder based on source_client_id

    Why NOT move to NodeService?

    • SurrealStore broadcast channel is working perfectly
    • dev-proxy.rs emits at HTTP handler level (NOT NodeService), suggesting this pattern works
    • Moving events to NodeService requires refactoring NodeService to own the channel
    • Current layering (data layer emits, business layer doesn't care) is clean

    Next Steps (Phases 2-4 Combined)

    1. Add client_id to SurrealStore method signatures

    Example:

    pub async fn create_node(&self, node: Node, client_id: Option<String>) -> Result<Node>
    pub async fn update_node(&self, id: &str, update: NodeUpdate, client_id: Option<String>) -> Result<Node>
    // ... etc for all CRUD methods

    2. Update emit_event() calls to pass client_id

    Replace all current source_client_id: None with the passed parameter:

    self.emit_event(DomainEvent::NodeCreated {
        node: node.clone(),
        source_client_id: client_id, // ← Pass through
    });

    3. Update DomainEventForwarder with filtering

    Add Tauri's client_id to DomainEventForwarder and filter:

    pub struct DomainEventForwarder {
        store: Arc<SurrealStore>,
        app: AppHandle,
        client_id: String, // ← Add this
    }
    
    // In forward_event():
    if let Some(source) = source_client_id {
        if source == self.client_id {
            return; // Skip own events
        }
    }

    4. Thread client_id from Tauri commands

    Update all Tauri commands to generate/pass client_id:

    // In db.rs or similar
    static TAURI_CLIENT_ID: OnceLock<String> = OnceLock::new();
    
    // Pass to NodeService, which passes to SurrealStore

    5. Update NodeService to accept and forward client_id

    Add client_id: Option<String> parameter to all NodeService CRUD methods and forward to SurrealStore.

    Files to Modify (Remaining Work)

    • packages/core/src/db/surreal_store.rs - Add client_id param to all CRUD methods (~15 methods)
    • packages/core/src/services/node_service.rs - Add client_id param, forward to store (~20 methods)
    • packages/desktop-app/src-tauri/src/services/domain_event_forwarder.rs - Add filtering logic
    • packages/desktop-app/src-tauri/src/commands/db.rs - Pass Tauri client_id through
    • packages/desktop-app/src-tauri/src/commands/nodes.rs - Pass client_id through
    • All Tauri command files - Thread client_id parameter

    Testing Strategy

    1. Unit tests: Already passing, may need minor updates for new signatures
    2. Integration test: Create test that verifies filtering works (subscribe with client_id, verify own events filtered)
    3. Manual test: Rapid Enter + Shift+Tab should no longer cause "Sibling not found" errors (Fix Shift+Tab outdent failing with 'Sibling not found' error after rapid node creation #662)

    Branch Status

    • Branch: feature/issue-665-domain-event-client-filtering
    • Commit: a8e748c (Phase 1 complete)
    • Tests: ✅ All passing
    • Ready for: Phase 2-4 implementation

    cc @malibio - Phase 1 infrastructure is solid. Recommend simplified approach above for remaining work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions