- Fix native module FFI architecture (segfault issues)
- Research and implement proper FFI patterns
- Update Research Log module to new architecture
- Document build requirements to prevent future issues
- FFI Architecture Research: ✅ Studied best practices from cargo, neovim, etc.
- Message-Based Design: ✅ Designed unified JSON message protocol
- Minimal Test Module: ✅ Created and validated FFI architecture
- Research Log Rewrite: ✅ Completely rewrote with message-based FFI
- Tests Passing: ✅ All FFI tests passing - NO MORE SEGFAULTS!
- Build Documentation: ✅ Added mandatory build requirements to master plan
FFI ARCHITECTURE FIXED: The fundamental issue of passing trait objects through FFI is resolved. Native modules now use the same message-based approach as WASM modules.
Research Findings:
- Trait objects (
dyn ResearchModule) cannot safely cross FFI boundaries - Fat pointers (16 bytes) are not C-compatible
- Best practice: Use C-compatible function pointers with opaque handles
- Message passing with JSON is safe and extensible
Design Decision: Unified message-based architecture
// Simple C-compatible interface
#[no_mangle]
pub extern "C" fn create_module() -> *mut c_void
#[no_mangle]
pub extern "C" fn handle_message(
module: *mut c_void,
message_type: *const c_char,
payload: *const c_char
) -> *mut c_charCreated: modules/test-minimal/
- Validates FFI architecture
- Tests all message types
- Includes null safety checks
- All tests passing
Changes Made:
- Removed all trait dependencies
- Implemented message handlers for all types
- Supports commands, queries, mutations, events
- Maintains internal state only
- Simplified dependencies (no rp-core, rp-modules, etc.)
Key Code Structure:
fn handle_message(&mut self, msg_type: &str, payload: Value) -> Value {
match msg_type {
"init" => self.handle_init(payload),
"command" => self.handle_command(payload),
"query" => self.handle_query(payload),
"mutation" => self.handle_mutation(payload),
"event" => self.handle_event(payload),
_ => error_response
}
}Added to Master Plan:
- MANDATORY BUILD REQUIREMENTS section
- Clear instructions for CARGO_BUILD_JOBS=1
- Timeout specifications
- Common pitfalls to avoid
Created Tests:
minimal_ffi_test.rs- Validates basic FFIresearch_log_ffi_test.rs- Comprehensive module testing- Both passing with new architecture
Module System (85% Complete)
├── Native Modules
│ ├── Loading ✅
│ ├── FFI Interface ✅ FIXED!
│ ├── Message Handling ✅
│ └── Test Coverage ✅
├── WASM Modules
│ ├── Loading ✅
│ ├── C-Style Exports ✅
│ ├── Command Execution ✅
│ └── Message Alignment ⚠️ (needs update)
└── Module SDK
├── Structure ✅
├── Message Protocol ✅
├── Implementation ❌ (next priority)
└── Documentation ❌ (needed)
-
Message Types:
init: Initialize modulecommand: Execute module operationsquery: Read Layer 1 datamutation: Modify Layer 1 dataevent: System notifications
-
Response Format:
{ "status": "success|error", "data": {...}, // for success "error": "...", // for errors }
CRITICAL: Always use these settings:
export CARGO_BUILD_JOBS=1 # Prevent resource exhaustion
timeout: 600000ms (10 min) # For builds
timeout: 300000ms (5 min) # For tests- First builds take 5-10 minutes (NORMAL)
- 200+ crates compile due to heavy dependencies
- Xorg high CPU is X11 forwarding, not actual load
- System is NOT hanging, just compiling
- No Trait Objects: Only C-compatible types
- Opaque Pointers: Module owns data, host has handle
- JSON Messages: All communication serialized
- Memory Safety: Clear ownership, proper cleanup
- Modules build to:
target/release/libMODULE_NAME.so - NOT in module's own target directory (workspace takes precedence)
- Tests look in workspace target directory
/modules/test-minimal/- New minimal FFI test module/modules/research-log/src/lib.rs- Completely rewritten/crates/rp-modules/tests/minimal_ffi_test.rs- New test/crates/rp-modules/tests/research_log_ffi_test.rs- New testRESEARCHPROCESS_GPS_MASTER_PLAN_v3.8_2025_08_01_1831_EEST.md- Updated
- WASM modules need message format alignment
- ModuleLoader needs update for new architecture
- Module SDK needs implementation
-
Update WASM Module Message Format:
- Align with native module protocol
- Ensure consistent JSON structure
- Test interoperability
-
Implement Module SDK:
- Message builder helpers
- Response parser utilities
- Common patterns library
-
Update ModuleLoader:
- Remove trait object handling
- Implement message routing
- Add proper error handling
-
Create Module Documentation:
- FFI pattern guide
- Message protocol spec
- Example module template
-
Memory Sanitizer Testing:
- Run with ASAN/MSAN
- Verify no memory leaks
- Check boundary conditions
Module System Completion:
- Align WASM module message format
- Create module SDK with helpers
- Update ModuleLoader for messages
- Write module developer guide
- Test with memory sanitizers
- Create example module template
Remember:
- ALWAYS use CARGO_BUILD_JOBS=1
- ALWAYS use extended timeouts
- NO_FALLBACK_POLICY compliance
- Test everything thoroughly
- Master Plan:
RESEARCHPROCESS_GPS_MASTER_PLAN_v3.8_2025_08_01_1831_EEST.md - Build Guide:
docs/development/BUILD_PERFORMANCE_GUIDE_2025_08_01_1230_EEST.md - Previous Handover:
COMPREHENSIVE_HANDOVER_2025_08_01_1717_EEST.md
# Always build with single job
CARGO_BUILD_JOBS=1 cargo build --release
# Test with proper timeout
CARGO_BUILD_JOBS=1 cargo test --test test_name -- --nocapture
# Check for running builds
ps aux | grep cargo- FFI is Hard: But message passing makes it manageable
- Build Times: Accept them, plan for them
- Testing Matters: Comprehensive tests caught all issues
- Documentation Critical: Prevents repeating mistakes
- Safety: No unsafe memory access
- Uniformity: Same protocol for all modules
- Extensibility: Easy to add new message types
- Debugging: All messages can be logged/traced
- Start with WASM alignment (should be quick)
- SDK is mostly boilerplate - use macros
- ModuleLoader changes are straightforward
- Documentation can use test module as example
This handover documents the successful implementation of the message-based FFI architecture, eliminating the segfault issues and providing a solid foundation for the module system.