|
| 1 | +# M1.2.1: FlatBuffers Message Schema |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document describes the comprehensive FlatBuffers schema (`proto/schema.fbs`) that defines all inter-module message types for the MiMi cognitive architecture. The schema is the **single source of truth** for message serialization/deserialization across Rust and C++ components communicating via the Zenoh Message Bus. |
| 6 | + |
| 7 | +## Design Principles |
| 8 | + |
| 9 | +### 1. Zero-Copy Deserialization |
| 10 | +- All messages use FlatBuffers for direct, zero-copy access to binary data |
| 11 | +- No intermediate allocation or unpacking needed |
| 12 | +- Enables sub-millisecond deserialization latency |
| 13 | + |
| 14 | +### 2. Type Safety & Versioning |
| 15 | +- Enum-based message types prevent runtime type confusion |
| 16 | +- Protocol version field enables backward compatibility |
| 17 | +- Union types ensure type-safe polymorphism |
| 18 | + |
| 19 | +### 3. Trace Context & Observability |
| 20 | +- Every message carries `TraceContext` for distributed tracing |
| 21 | +- Correlation IDs link requests across the entire processing pipeline |
| 22 | +- Latency tracking per message hop |
| 23 | + |
| 24 | +### 4. Budget-Aware Routing |
| 25 | +- Priority levels (CRITICAL, HIGH, MEDIUM, LOW) for gating system |
| 26 | +- Complexity estimates guide routing decisions (Tier 1/2/3) |
| 27 | +- Token cost tracking for budget management |
| 28 | + |
| 29 | +### 5. Semantic Integrity |
| 30 | +- Tone profiles capture emotional/voice metadata (Liliana) |
| 31 | +- Mood state embedded in responses |
| 32 | +- Security flags from Odlaguna gate included in messages |
| 33 | + |
| 34 | +## Schema Structure |
| 35 | + |
| 36 | +### Core Enumerations |
| 37 | + |
| 38 | +#### `IntentType` (Beatrice Classification) |
| 39 | +- QUERY: Information request |
| 40 | +- ACTION: Task execution request |
| 41 | +- SKILL_CREATION: System-generated skill |
| 42 | +- RESPONSE: Reply to prior message |
| 43 | +- ERROR: Error notification |
| 44 | +- EVENT: System event (state change) |
| 45 | +- CONTROL: Administrative control |
| 46 | + |
| 47 | +#### `ComplexityEstimate` (Gating System) |
| 48 | +- TRIVIAL: Cache hit, <50ms (Tier 1 Liliana) |
| 49 | +- SOCIAL: Greeting/small-talk (Tier 1 Liliana) |
| 50 | +- SIMPLE: Known skill, <200ms (Tier 2 Beatrice+Echidna) |
| 51 | +- MODERATE: Skill + light reasoning (Tier 2) |
| 52 | +- COMPLEX: Full cognitive pipeline (Tier 3) |
| 53 | + |
| 54 | +#### `GatingTier` (Routing Decision) |
| 55 | +- TIER_1_REFLEX: Liliana cache + template |
| 56 | +- TIER_2_AUTOMATED: Beatrice + Echidna skill |
| 57 | +- TIER_3_COGNITIVE: Full pipeline (Priscilla + Pandora + Echidna + LLM) |
| 58 | + |
| 59 | +#### `ResponseStatus` (Execution Outcome) |
| 60 | +- SUCCESS: Task completed successfully |
| 61 | +- PARTIAL: Partial completion (timeout, partial results) |
| 62 | +- FAILURE: Task failed |
| 63 | +- TIMEOUT: Execution timeout exceeded |
| 64 | +- REJECTED: Rejected by Odlaguna gate |
| 65 | + |
| 66 | +#### `CacheLayer` (Liliana Cache Classification) |
| 67 | +- NONE: Cache miss |
| 68 | +- EXACT_MATCH: Exact cache hit |
| 69 | +- SEMANTIC: Semantic similarity match |
| 70 | +- TEMPLATE: Template-based response |
| 71 | + |
| 72 | +### Message Types |
| 73 | + |
| 74 | +#### User Input (Beatrice → Mimi) |
| 75 | +``` |
| 76 | +UserInput { |
| 77 | + user_message: string, // Raw user text |
| 78 | + user_id: string, // User identifier |
| 79 | + session_id: string, // Conversation session |
| 80 | + channel: string // Input channel (cli, http, etc.) |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +#### Intent Classification (Beatrice → Mimi/Liliana) |
| 85 | +``` |
| 86 | +IntentClassified { |
| 87 | + intent_type: IntentType, |
| 88 | + confidence: float, // [0,1] classification confidence |
| 89 | + entity_tags: [string], // Extracted entities |
| 90 | + is_social: bool, |
| 91 | + estimated_complexity: ComplexityEstimate, |
| 92 | + raw_intent_text: string |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### Liliana Response (Liliana → Odlaguna) |
| 97 | +``` |
| 98 | +LilianaResponse { |
| 99 | + response: string, |
| 100 | + mood: Mood, // Current mood state |
| 101 | + confidence: float, // Response appropriateness confidence |
| 102 | + tone_profile: ToneProfile, // Voice/personality metadata |
| 103 | + cached_response: CachedResponse, |
| 104 | + fallback_metadata: string // JSON fallback hint |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +#### Task Execution (Mimi → Ryzu) |
| 109 | +``` |
| 110 | +TaskExecution { |
| 111 | + task_id: string, |
| 112 | + skill_id: string, |
| 113 | + skill_name: string, |
| 114 | + priority: Priority, |
| 115 | + parameters: [byte], // Serialized skill params |
| 116 | + timeout_ms: uint32, |
| 117 | + routing_decision: RoutingDecision |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +#### Execution Result (Ryzu → Mimi) |
| 122 | +``` |
| 123 | +ExecutionResult { |
| 124 | + task_id: string, |
| 125 | + success: bool, |
| 126 | + status: ResponseStatus, |
| 127 | + output: string, |
| 128 | + error: string, |
| 129 | + execution_time_ms: uint32, |
| 130 | + tokens_consumed: uint32 // For budget tracking |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +#### Memory Update (Mimi → Pandora) |
| 135 | +``` |
| 136 | +MemoryUpdate { |
| 137 | + update_id: string, |
| 138 | + entity_id: string, // Neo4j node ID |
| 139 | + action: string, // create, update, link, delete |
| 140 | + entity_type: string, // concept, skill, interaction |
| 141 | + properties: string, // JSON |
| 142 | + heatmap_decay: float, // Thermal decay [0,1] |
| 143 | + retention_priority: Priority |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +#### Odlaguna Gate (Odlaguna → Liliana) |
| 148 | +``` |
| 149 | +OdalgunaGate { |
| 150 | + message_id: string, |
| 151 | + allowed: bool, |
| 152 | + modifications: [Modification], // Suggested changes |
| 153 | + security_flags: SecurityFlags, |
| 154 | + confidence: float, // Gate confidence [0,1] |
| 155 | + latency_ms: uint32 |
| 156 | +} |
| 157 | +``` |
| 158 | + |
| 159 | +#### Error Report (Any Module → Odlaguna) |
| 160 | +``` |
| 161 | +ErrorReport { |
| 162 | + error_id: string, |
| 163 | + severity: Priority, |
| 164 | + module: string, // Where error occurred |
| 165 | + error_message: string, |
| 166 | + error_type: string, // Classification |
| 167 | + stack_trace: string, // Debug-only |
| 168 | + context_json: string // Additional context |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +### Unified Message Envelope |
| 173 | + |
| 174 | +All messages wrap their payload in a `Message` type: |
| 175 | + |
| 176 | +``` |
| 177 | +Message { |
| 178 | + version: uint32, // Protocol version |
| 179 | + trace: TraceContext, // Distributed tracing |
| 180 | + body_type: MessageBodyType, // Discriminant |
| 181 | + body: MessageBody, // Union of all message types |
| 182 | + routing_hint: string // Target topic/route hint |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +**Root type:** `Message` |
| 187 | + |
| 188 | +## Code Generation |
| 189 | + |
| 190 | +### Rust |
| 191 | + |
| 192 | +**Build Script** (`build.rs`): |
| 193 | +```rust |
| 194 | +flatbuffers_build::compile_flatbuffers_files( |
| 195 | + &["proto/schema.fbs", "proto/metadata.fbs"], |
| 196 | + &["proto/"], |
| 197 | + &out_dir, |
| 198 | +).expect("Failed to compile FlatBuffers schemas"); |
| 199 | +``` |
| 200 | + |
| 201 | +**Output:** Rust bindings generated to `$OUT_DIR/mimi_protocol.rs` |
| 202 | + |
| 203 | +**Usage:** |
| 204 | +```rust |
| 205 | +use mimi::protocol::*; |
| 206 | +use flatbuffers::FlatBufferBuilder; |
| 207 | + |
| 208 | +let mut builder = FlatBufferBuilder::new(1024); |
| 209 | +let mut msg_builder = builders::MessageBuilder::new(&mut builder); |
| 210 | +let intent = msg_builder.create_intent_classified( |
| 211 | + IntentType::QUERY, |
| 212 | + 0.85, |
| 213 | + false, |
| 214 | + ComplexityEstimate::SIMPLE |
| 215 | +); |
| 216 | +``` |
| 217 | + |
| 218 | +**Validation:** |
| 219 | +```rust |
| 220 | +use mimi::protocol::validation::*; |
| 221 | + |
| 222 | +validate_intent_classified(&intent)?; |
| 223 | +validate_mood(&mood)?; |
| 224 | +``` |
| 225 | + |
| 226 | +### C++ |
| 227 | + |
| 228 | +**Generation Script** (`scripts/generate_cpp_bindings.py`): |
| 229 | +```bash |
| 230 | +python3 scripts/generate_cpp_bindings.py |
| 231 | +``` |
| 232 | + |
| 233 | +**Output:** C++ headers generated to `proto/generated/cpp/` |
| 234 | + |
| 235 | +**Usage:** |
| 236 | +```cpp |
| 237 | +#include "proto/generated/cpp/schema_generated.h" |
| 238 | +using namespace MiMi::Protocol; |
| 239 | + |
| 240 | +// Access zero-copy data |
| 241 | +auto message = GetMessage(buffer); |
| 242 | +auto body = message->body_as_UserInput(); |
| 243 | +``` |
| 244 | +
|
| 245 | +## Integration with Zenoh Topics |
| 246 | +
|
| 247 | +Messages are published/subscribed on topic hierarchies mapped to message types: |
| 248 | +
|
| 249 | +| Message Type | Zenoh Topic | Direction | |
| 250 | +|---|---|---| |
| 251 | +| UserInput | `beatrice/input` | Beatrice → Mimi | |
| 252 | +| IntentClassified | `beatrice/intent` | Beatrice → Liliana/Mimi | |
| 253 | +| LilianaResponse | `liliana/response` | Liliana → Odlaguna | |
| 254 | +| TaskExecution | `mimi/task/execute` | Mimi → Ryzu | |
| 255 | +| ExecutionResult | `mimi/task/result` | Ryzu → Mimi | |
| 256 | +| MemoryUpdate | `pandora/memory/update` | Mimi → Pandora | |
| 257 | +| OdalgunaGate | `odlaguna/gate` | Odlaguna → Liliana | |
| 258 | +| ErrorReport | `sys/error` | Any → Odlaguna | |
| 259 | +| SkillPublish | `echidna/skill/publish` | Echidna → Odlaguna | |
| 260 | +| EventNotification | `sys/events` | Any → Bus | |
| 261 | +
|
| 262 | +## Latency & Performance Targets |
| 263 | +
|
| 264 | +| Operation | Budget | |
| 265 | +|---|---| |
| 266 | +| Serialization (Rust) | < 100μs | |
| 267 | +| Deserialization (zero-copy) | < 50μs | |
| 268 | +| Zenoh broker latency | < 1ms | |
| 269 | +| End-to-end message hop | < 5ms | |
| 270 | +
|
| 271 | +## Backward Compatibility |
| 272 | +
|
| 273 | +Protocol version field enables future evolution: |
| 274 | +- Version mismatch detection |
| 275 | +- Graceful downgrade/upgrade paths |
| 276 | +- Schema evolution without breaking existing deployments |
| 277 | +
|
| 278 | +## Testing |
| 279 | +
|
| 280 | +Unit tests included in `src/protocol.rs`: |
| 281 | +
|
| 282 | +```bash |
| 283 | +cargo test --lib protocol::tests |
| 284 | +``` |
| 285 | + |
| 286 | +Tests validate: |
| 287 | +- Message builder correctness |
| 288 | +- Mood/intent validation |
| 289 | +- Round-trip serialization |
| 290 | +- Boundary conditions |
| 291 | + |
| 292 | +## Future Extensions |
| 293 | + |
| 294 | +Reserved for M1.3+: |
| 295 | +- Compression options (for large skill binaries) |
| 296 | +- Streaming message batching |
| 297 | +- End-to-end encryption metadata |
0 commit comments