Repository navigation
Closed
Description
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):
- Frontend performs optimistic UI update
- Tauri command calls
NodeService→SurrealStore SurrealStore.emit_event()broadcastsDomainEvent(no client_id)DomainEventForwarderreceives event and forwards to Tauri frontend- Frontend receives its own change back → state corruption/race conditions
Evidence:
- "Sibling not found" errors during rapid Enter + Shift+Tab operations (Fix Shift+Tab outdent failing with 'Sibling not found' error after rapid node creation #662)
- TauriSync events firing for changes made by the same client
- Browser mode (
dev-proxy.rs) has workingclient_idfiltering, Tauri mode does not
Root Cause:
emit_event()is called directly inSurrealStore(data layer) with no client context- No mechanism to pass
client_idthrough the call chain DomainEventForwarderforwards ALL events blindly
Proposed Solution
Move event emission to the NodeService layer with client_id awareness:
Target Architecture:
- Each client (Tauri, MCP, proxy, cloud sync) has a unique
client_id NodeServicemethods accept optionalclient_idparameter (or use context)- Events are emitted at
NodeServicelevel withsource_client_id DomainEventForwarderfilters events matching subscriber'sclient_id
Key Design Decision:
NodeServiceshould 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 fromSurrealStore - Add event emission to
NodeServiceafter successful operations - Keep
SurrealStore.event_txbroadcast channel for now (or move to NodeService)
Phase 2: Add Client Context
- Create
ClientContexttype withclient_id: String - Add
with_client()or similar method toNodeServicefor setting context - Update
DomainEventstruct to includesource_client_id: Option<String>
Phase 3: Update Event Consumers
- Update
DomainEventForwarderto accept Tauri'sclient_id - Filter events where
source_client_id == subscriber_client_id - Re-enable
DomainEventForwarderindb.rs
Phase 4: Fix Layering Violations
- Audit Tauri commands for direct
SurrealStorecalls - Route all operations through
NodeService get_all_edgesinnodes.rs:890is only current violation (read-only, low priority)
Acceptance Criteria
- Events emitted at
NodeServicelayer, notSurrealStore -
DomainEventincludessource_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 (needssource_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
- Merge NodeOperations into NodeService to eliminate architectural duplication #676 (establishes single unified NodeService layer where events should be emitted)
Related Issues
- Fix Shift+Tab outdent failing with 'Sibling not found' error after rapid node creation #662 (fix Shift+Tab outdent race condition - blocked by this)
- Merge NodeOperations into NodeService to eliminate architectural duplication #676 (merge NodeOperations into NodeService - prerequisite)
- Architecture: Migrate to backend-originated event emission #226 (closed - backend-originated events already implemented)
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)
Metadata
Metadata
Assignees
Labels
No labels
Activity
Fix outdent race condition with pending move tracking and stale parentId
Fix Shift+Tab outdent failing with 'Sibling not found' error after ra…
malibio commentedon Nov 28, 2025
Test Baseline (Starting Work)
Frontend: ✅ 728 tests passing
Backend: ✅ 532 tests passing (3 ignored)
No test failures at start of work.
WIP: Phase 1 - Add source_client_id to DomainEvent struct
malibio commentedon Nov 28, 2025
Phase 1 Complete - Handoff Notes
Work Completed
✅ Phase 1: DomainEvent Infrastructure
source_client_id: Option<String>to all 6 DomainEvent variantsRecommended Implementation Path
After analyzing the codebase, I recommend NOT moving event emission to NodeService (as originally proposed). Here's why:
Current Working Architecture:
Simpler Solution (Recommended):
client_id: Option<String>parameter to SurrealStore methodsWhy NOT move to NodeService?
Next Steps (Phases 2-4 Combined)
1. Add client_id to SurrealStore method signatures
Example:
2. Update emit_event() calls to pass client_id
Replace all current
source_client_id: Nonewith the passed parameter:3. Update DomainEventForwarder with filtering
Add Tauri's client_id to DomainEventForwarder and filter:
4. Thread client_id from Tauri commands
Update all Tauri commands to generate/pass client_id:
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 logicpackages/desktop-app/src-tauri/src/commands/db.rs- Pass Tauri client_id throughpackages/desktop-app/src-tauri/src/commands/nodes.rs- Pass client_id throughTesting Strategy
Branch Status
feature/issue-665-domain-event-client-filteringcc @malibio - Phase 1 infrastructure is solid. Recommend simplified approach above for remaining work.
WIP: Phase 2 - Add event emission to NodeService layer
WIP: Phase 2 - Add event emission to NodeService layer
WIP: Phase 3 - Add client context to NodeService
WIP: Phase 4 - Update DomainEventForwarder with client_id filtering
WIP: Phase 5 - Update Tauri commands to pass client_id
WIP: Phase 1 - Add source_client_id to DomainEvent struct
WIP: Phase 2 - Add event emission to NodeService layer
WIP: Phase 2 - Add event emission to NodeService layer
WIP: Phase 3 - Add client context to NodeService
WIP: Phase 4 - Update DomainEventForwarder with client_id filtering
WIP: Phase 5 - Update Tauri commands to pass client_id
Rearchitect Domain Events to Support source_client_id Filtering (#684)
Fix Shift+Tab outdent failing with 'Sibling not found' error after ra…
Rearchitect Domain Events to Support source_client_id Filtering (#684)
Fix Shift+Tab outdent failing with 'Sibling not found' error after ra…
Rearchitect Domain Events to Support source_client_id Filtering (#684)
Fix Shift+Tab outdent failing with 'Sibling not found' error after ra…
Rearchitect Domain Events to Support source_client_id Filtering (#684)
Remove GitHub issue-number references from code comments (#1669)