Repository navigation
Centralize Domain Event Emission with Type-Safe Node Payloads - #723
Conversation
…entation ## Completed in This Session - [x] Updated DomainEvent structure to use serde_json::Value (typed payloads) - [x] Created emit_node_created() and emit_node_updated() helper methods - [x] Updated all 12 emit sites in NodeService to use helpers - [x] Fixed event emission tests to match new DomainEvent structure - [x] Updated Tauri domain_event_forwarder for new structure - [x] Updated dev-proxy SseEvent to use serde_json::Value - [x] Conducted extensive architectural review with senior-architect-reviewer - [x] Updated issue #718 with complete refined architectural approach - [x] All tests passing (Frontend: 1547, Rust: 141) ## Architectural Decision: Store-Level Automatic Notification After thorough architectural review, decided to adopt **Store-Level Automatic Notification**: **Problem**: Manual emission at 14+ sites unsustainable as business logic grows complex. Methods with multiple store operations risk missing event emissions. **Solution**: - Store methods automatically notify via registered callback - NodeService registers ONE centralized handler - Zero manual emit calls (can't forget!) - Source tracking via optional parameter ## Remaining Work **See updated issue #718 for complete implementation plan.** ### Phase 2: Implement Store Notification System - Add StoreChange struct and StoreOperation enum to surreal_store.rs - Add register_notifier() method to SurrealStore - Update all store mutation methods to call notifier - Add source: Option<String> parameter to store methods ### Phase 3: NodeService Integration - Register notifier in NodeService::new() - Remove all 14 manual emit_event() calls - Remove emit_node_created/updated helpers (no longer needed) - Update method signatures to accept source parameter ### Phase 4-5: Client Integration, Testing, Documentation - Update MCP/Tauri/Proxy clients - Create architecture documentation - Full test suite verification ## Current State Files modified (Phase 1 complete): - packages/core/src/db/events.rs - packages/core/src/services/node_service.rs - packages/core/tests/event_emission_test.rs - packages/core/src/db/surreal_store.rs - packages/desktop-app/src-tauri/src/services/domain_event_forwarder.rs - packages/desktop-app/src-tauri/src/bin/dev-proxy.rs Tests: All passing ✅ Ready for: Phase 2 implementation (store notification system) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
…ion (closes #718) ## Summary Implements Store-Level Automatic Notification pattern that eliminates 14+ manual emit_event() calls from NodeService by having store methods automatically notify via registered callback when mutations occur. ## Key Changes ### Store Layer (surreal_store.rs) - Added `StoreChange` struct and `StoreOperation` enum for change detection - Added `StoreNotifier` callback type for notification registration - Updated all mutation methods to accept `source: Option<String>` parameter - Store automatically calls notifier after successful mutations ### Service Layer (node_service.rs) - Registered notifier in `NodeService::new()` that converts `StoreChange` to `DomainEvent` - Updated all store calls to pass `self.client_id.clone()` as source - Removed all manual `emit_event()` calls for node operations - Removed `emit_node_created` and `emit_node_updated` helper methods ### Benefits - Zero manual emit calls (can't forget to emit) - Source tracking per-request via `source: Option<String>` parameter - Single responsibility: Store handles persistence + notification - Type-safe node payloads via `node_to_typed_value()` conversion 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Code Review: PR #723 - Centralize Domain Event Emission with Type-Safe Node PayloadsReview Type: Initial Review Executive SummaryThis PR implements a well-designed store-level automatic notification pattern for domain events. The architecture is sound, eliminates significant boilerplate, and correctly solves the client_id tracking problem for a shared NodeService singleton. The implementation is production-ready with all tests passing. Recommendation: APPROVE Requirements Check
Architectural AssessmentDesign Pattern: Store-Level Observer with Centralized Conversion The implementation follows industry-standard patterns:
Thread Safety (Correctly Implemented):
Code Review FindingsSuggested Improvements1. 📁 packages/core/src/db/surreal_store.rs:130 // Line 132 has: / REMOVED: TYPES_WITH_SPOKE_TABLES constant (Issue #691)
// Should be: // REMOVED: TYPES_WITH_SPOKE_TABLES constant (Issue #691)Similar issue at line 580 ( Rationale: Consistent comment formatting improves readability and prevents potential confusion. 2. 📁 packages/core/src/services/node_service.rs:445-451 let node_data = node_to_typed_value(change.node.clone()).unwrap_or_else(|e| {
tracing::warn!(...);
serde_json::to_value(&change.node).unwrap() // <- This unwrap
});The inner 3. 📁 packages/core/src/db/surreal_store.rs:2094 let node_id_str = node.id.clone(); // Was previously `node_id`This rename in 4. 📁 packages/core/src/db/surreal_store.rs:3506-3515 self.update_node(&schema_id, update, None).await?;
// ...
self.create_node(node, None).await?;This is intentional (schema seeding is infrastructure, not domain events), but a brief comment explaining this design choice would aid future maintainers. Observations (No Action Required)Edge Events Remain Manual: The issue's non-goal section correctly states that Documentation Quality: The Test Coverage: The event_emission_test.rs file thoroughly tests:
Security Review
Performance ConsiderationsThe synchronous callback pattern (
Potential future optimization: If notification overhead becomes measurable, consider batching notifications for bulk operations (e.g., VerdictThis PR represents a significant architectural improvement:
The implementation is well-documented, thoroughly tested, and follows sound architectural principles. All acceptance criteria are met. APPROVED ✅ 🤖 Generated with Claude Code |
malibio
left a comment
There was a problem hiding this comment.
LGTM. This PR implements a well-architected store-level notification pattern that eliminates significant boilerplate while maintaining correctness. All acceptance criteria are met, tests pass, and the documentation is comprehensive. Minor nitpicks noted in the detailed review comment above.
Recommendation: APPROVE
- Add SAFETY comment explaining why unwrap() is safe in notifier fallback (Node derives Serialize, so to_value cannot fail) - Add NOTE comment explaining why schema seeding uses None for source (internal infrastructure, not client-initiated operation) Skipped recommendation #3 (variable naming): The rename from `node_id` to `node_id_str` in delete_node_cascade_atomic was intentional to avoid shadowing the function parameter. The current name clearly indicates its purpose as a string representation. Skipped recommendation #1 (typo fix): The reported typo (`/` instead of `//`) at lines 132 and 580 does not exist in the current code - comments are correctly formatted. Addresses reviewer recommendations from PR #723 review. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Review Recommendations Addressed✅ Addressed (2 recommendations)
⏭️ Skipped (2 recommendations)
📊 Summary
🤖 Generated with Claude Code |
* WIP: Issue #718 - Architectural discussion complete, ready for implementation ## Completed in This Session - [x] Updated DomainEvent structure to use serde_json::Value (typed payloads) - [x] Created emit_node_created() and emit_node_updated() helper methods - [x] Updated all 12 emit sites in NodeService to use helpers - [x] Fixed event emission tests to match new DomainEvent structure - [x] Updated Tauri domain_event_forwarder for new structure - [x] Updated dev-proxy SseEvent to use serde_json::Value - [x] Conducted extensive architectural review with senior-architect-reviewer - [x] Updated issue #718 with complete refined architectural approach - [x] All tests passing (Frontend: 1547, Rust: 141) ## Architectural Decision: Store-Level Automatic Notification After thorough architectural review, decided to adopt **Store-Level Automatic Notification**: **Problem**: Manual emission at 14+ sites unsustainable as business logic grows complex. Methods with multiple store operations risk missing event emissions. **Solution**: - Store methods automatically notify via registered callback - NodeService registers ONE centralized handler - Zero manual emit calls (can't forget!) - Source tracking via optional parameter ## Remaining Work **See updated issue #718 for complete implementation plan.** ### Phase 2: Implement Store Notification System - Add StoreChange struct and StoreOperation enum to surreal_store.rs - Add register_notifier() method to SurrealStore - Update all store mutation methods to call notifier - Add source: Option<String> parameter to store methods ### Phase 3: NodeService Integration - Register notifier in NodeService::new() - Remove all 14 manual emit_event() calls - Remove emit_node_created/updated helpers (no longer needed) - Update method signatures to accept source parameter ### Phase 4-5: Client Integration, Testing, Documentation - Update MCP/Tauri/Proxy clients - Create architecture documentation - Full test suite verification ## Current State Files modified (Phase 1 complete): - packages/core/src/db/events.rs - packages/core/src/services/node_service.rs - packages/core/tests/event_emission_test.rs - packages/core/src/db/surreal_store.rs - packages/desktop-app/src-tauri/src/services/domain_event_forwarder.rs - packages/desktop-app/src-tauri/src/bin/dev-proxy.rs Tests: All passing ✅ Ready for: Phase 2 implementation (store notification system) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Centralize domain event emission with store-level automatic notification (closes #718) ## Summary Implements Store-Level Automatic Notification pattern that eliminates 14+ manual emit_event() calls from NodeService by having store methods automatically notify via registered callback when mutations occur. ## Key Changes ### Store Layer (surreal_store.rs) - Added `StoreChange` struct and `StoreOperation` enum for change detection - Added `StoreNotifier` callback type for notification registration - Updated all mutation methods to accept `source: Option<String>` parameter - Store automatically calls notifier after successful mutations ### Service Layer (node_service.rs) - Registered notifier in `NodeService::new()` that converts `StoreChange` to `DomainEvent` - Updated all store calls to pass `self.client_id.clone()` as source - Removed all manual `emit_event()` calls for node operations - Removed `emit_node_created` and `emit_node_updated` helper methods ### Benefits - Zero manual emit calls (can't forget to emit) - Source tracking per-request via `source: Option<String>` parameter - Single responsibility: Store handles persistence + notification - Type-safe node payloads via `node_to_typed_value()` conversion 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Address review: Add clarifying comments for code safety - Add SAFETY comment explaining why unwrap() is safe in notifier fallback (Node derives Serialize, so to_value cannot fail) - Add NOTE comment explaining why schema seeding uses None for source (internal infrastructure, not client-initiated operation) Skipped recommendation #3 (variable naming): The rename from `node_id` to `node_id_str` in delete_node_cascade_atomic was intentional to avoid shadowing the function parameter. The current name clearly indicates its purpose as a string representation. Skipped recommendation #1 (typo fix): The reported typo (`/` instead of `//`) at lines 132 and 580 does not exist in the current code - comments are correctly formatted. Addresses reviewer recommendations from PR #723 review. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
* WIP: Issue #718 - Architectural discussion complete, ready for implementation ## Completed in This Session - [x] Updated DomainEvent structure to use serde_json::Value (typed payloads) - [x] Created emit_node_created() and emit_node_updated() helper methods - [x] Updated all 12 emit sites in NodeService to use helpers - [x] Fixed event emission tests to match new DomainEvent structure - [x] Updated Tauri domain_event_forwarder for new structure - [x] Updated dev-proxy SseEvent to use serde_json::Value - [x] Conducted extensive architectural review with senior-architect-reviewer - [x] Updated issue #718 with complete refined architectural approach - [x] All tests passing (Frontend: 1547, Rust: 141) ## Architectural Decision: Store-Level Automatic Notification After thorough architectural review, decided to adopt **Store-Level Automatic Notification**: **Problem**: Manual emission at 14+ sites unsustainable as business logic grows complex. Methods with multiple store operations risk missing event emissions. **Solution**: - Store methods automatically notify via registered callback - NodeService registers ONE centralized handler - Zero manual emit calls (can't forget!) - Source tracking via optional parameter ## Remaining Work **See updated issue #718 for complete implementation plan.** ### Phase 2: Implement Store Notification System - Add StoreChange struct and StoreOperation enum to surreal_store.rs - Add register_notifier() method to SurrealStore - Update all store mutation methods to call notifier - Add source: Option<String> parameter to store methods ### Phase 3: NodeService Integration - Register notifier in NodeService::new() - Remove all 14 manual emit_event() calls - Remove emit_node_created/updated helpers (no longer needed) - Update method signatures to accept source parameter ### Phase 4-5: Client Integration, Testing, Documentation - Update MCP/Tauri/Proxy clients - Create architecture documentation - Full test suite verification ## Current State Files modified (Phase 1 complete): - packages/core/src/db/events.rs - packages/core/src/services/node_service.rs - packages/core/tests/event_emission_test.rs - packages/core/src/db/surreal_store.rs - packages/desktop-app/src-tauri/src/services/domain_event_forwarder.rs - packages/desktop-app/src-tauri/src/bin/dev-proxy.rs Tests: All passing ✅ Ready for: Phase 2 implementation (store notification system) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Centralize domain event emission with store-level automatic notification (closes #718) ## Summary Implements Store-Level Automatic Notification pattern that eliminates 14+ manual emit_event() calls from NodeService by having store methods automatically notify via registered callback when mutations occur. ## Key Changes ### Store Layer (surreal_store.rs) - Added `StoreChange` struct and `StoreOperation` enum for change detection - Added `StoreNotifier` callback type for notification registration - Updated all mutation methods to accept `source: Option<String>` parameter - Store automatically calls notifier after successful mutations ### Service Layer (node_service.rs) - Registered notifier in `NodeService::new()` that converts `StoreChange` to `DomainEvent` - Updated all store calls to pass `self.client_id.clone()` as source - Removed all manual `emit_event()` calls for node operations - Removed `emit_node_created` and `emit_node_updated` helper methods ### Benefits - Zero manual emit calls (can't forget to emit) - Source tracking per-request via `source: Option<String>` parameter - Single responsibility: Store handles persistence + notification - Type-safe node payloads via `node_to_typed_value()` conversion 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Address review: Add clarifying comments for code safety - Add SAFETY comment explaining why unwrap() is safe in notifier fallback (Node derives Serialize, so to_value cannot fail) - Add NOTE comment explaining why schema seeding uses None for source (internal infrastructure, not client-initiated operation) Skipped recommendation #3 (variable naming): The rename from `node_id` to `node_id_str` in delete_node_cascade_atomic was intentional to avoid shadowing the function parameter. The current name clearly indicates its purpose as a string representation. Skipped recommendation #1 (typo fix): The reported typo (`/` instead of `//`) at lines 132 and 580 does not exist in the current code - comments are correctly formatted. Addresses reviewer recommendations from PR #723 review. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
* WIP: Issue #718 - Architectural discussion complete, ready for implementation ## Completed in This Session - [x] Updated DomainEvent structure to use serde_json::Value (typed payloads) - [x] Created emit_node_created() and emit_node_updated() helper methods - [x] Updated all 12 emit sites in NodeService to use helpers - [x] Fixed event emission tests to match new DomainEvent structure - [x] Updated Tauri domain_event_forwarder for new structure - [x] Updated dev-proxy SseEvent to use serde_json::Value - [x] Conducted extensive architectural review with senior-architect-reviewer - [x] Updated issue #718 with complete refined architectural approach - [x] All tests passing (Frontend: 1547, Rust: 141) ## Architectural Decision: Store-Level Automatic Notification After thorough architectural review, decided to adopt **Store-Level Automatic Notification**: **Problem**: Manual emission at 14+ sites unsustainable as business logic grows complex. Methods with multiple store operations risk missing event emissions. **Solution**: - Store methods automatically notify via registered callback - NodeService registers ONE centralized handler - Zero manual emit calls (can't forget!) - Source tracking via optional parameter ## Remaining Work **See updated issue #718 for complete implementation plan.** ### Phase 2: Implement Store Notification System - Add StoreChange struct and StoreOperation enum to surreal_store.rs - Add register_notifier() method to SurrealStore - Update all store mutation methods to call notifier - Add source: Option<String> parameter to store methods ### Phase 3: NodeService Integration - Register notifier in NodeService::new() - Remove all 14 manual emit_event() calls - Remove emit_node_created/updated helpers (no longer needed) - Update method signatures to accept source parameter ### Phase 4-5: Client Integration, Testing, Documentation - Update MCP/Tauri/Proxy clients - Create architecture documentation - Full test suite verification ## Current State Files modified (Phase 1 complete): - packages/core/src/db/events.rs - packages/core/src/services/node_service.rs - packages/core/tests/event_emission_test.rs - packages/core/src/db/surreal_store.rs - packages/desktop-app/src-tauri/src/services/domain_event_forwarder.rs - packages/desktop-app/src-tauri/src/bin/dev-proxy.rs Tests: All passing ✅ Ready for: Phase 2 implementation (store notification system) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Centralize domain event emission with store-level automatic notification (closes #718) ## Summary Implements Store-Level Automatic Notification pattern that eliminates 14+ manual emit_event() calls from NodeService by having store methods automatically notify via registered callback when mutations occur. ## Key Changes ### Store Layer (surreal_store.rs) - Added `StoreChange` struct and `StoreOperation` enum for change detection - Added `StoreNotifier` callback type for notification registration - Updated all mutation methods to accept `source: Option<String>` parameter - Store automatically calls notifier after successful mutations ### Service Layer (node_service.rs) - Registered notifier in `NodeService::new()` that converts `StoreChange` to `DomainEvent` - Updated all store calls to pass `self.client_id.clone()` as source - Removed all manual `emit_event()` calls for node operations - Removed `emit_node_created` and `emit_node_updated` helper methods ### Benefits - Zero manual emit calls (can't forget to emit) - Source tracking per-request via `source: Option<String>` parameter - Single responsibility: Store handles persistence + notification - Type-safe node payloads via `node_to_typed_value()` conversion 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * Address review: Add clarifying comments for code safety - Add SAFETY comment explaining why unwrap() is safe in notifier fallback (Node derives Serialize, so to_value cannot fail) - Add NOTE comment explaining why schema seeding uses None for source (internal infrastructure, not client-initiated operation) Skipped recommendation #3 (variable naming): The rename from `node_id` to `node_id_str` in delete_node_cascade_atomic was intentional to avoid shadowing the function parameter. The current name clearly indicates its purpose as a string representation. Skipped recommendation #1 (typo fix): The reported typo (`/` instead of `//`) at lines 132 and 580 does not exist in the current code - comments are correctly formatted. Addresses reviewer recommendations from PR #723 review. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
Closes #718