Skip to content

Commit 5eac01d

Browse files
committed
[M1.2.1] Design FlatBuffers schema for Mimi messages
- Comprehensive protocol definition with 100+ message types - Trace context & distributed tracing support (correlation IDs) - Message types for all modules: Beatrice (intent), Liliana (mood/persona), Mimi (routing), Pandora (memory), Odlaguna (gating), Ryzu (execution) - Budget-aware routing metadata (Priority, ComplexityEstimate, GatingTier) - Unified Message envelope with type-safe unions - Zero-copy deserialization via FlatBuffers - Rust bindings with MessageBuilder, validation, and unit tests - C++ code generation script - Schema documentation with integration guide
1 parent 9c4d51b commit 5eac01d

8 files changed

Lines changed: 1036 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ log = "0.4"
2626
env_logger = "0.11"
2727
num_cpus = "1.16"
2828
tokio-test = "0.4"
29+
flatbuffers = "24.3"
30+
flatbuffers-build = "24.3"
2931

3032
[profile.dev]
3133
opt-level = 0

build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::path::PathBuf;
2+
3+
fn main() {
4+
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
5+
6+
// Compile FlatBuffers schemas to Rust
7+
flatbuffers_build::compile_flatbuffers_files(
8+
&["proto/schema.fbs", "proto/metadata.fbs"],
9+
&["proto/"],
10+
&out_dir,
11+
)
12+
.expect("Failed to compile FlatBuffers schemas");
13+
14+
println!("cargo:rerun-if-changed=proto/");
15+
}

docs/M1.2.1-FLATBUFFERS-SCHEMA.md

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
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

proto/metadata.fbs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace MiMi.Protocol;
2+
3+
table RuntimeMetrics {
4+
start_time_ms: uint64;
5+
end_time_ms: uint64;
6+
duration_ms: uint32;
7+
cpu_percent: float;
8+
memory_mb: uint32;
9+
tokens_input: uint32;
10+
tokens_output: uint32;
11+
}
12+
13+
table BudgetSnapshot {
14+
tier1_used: uint32;
15+
tier1_limit: uint32;
16+
tier2_used: uint32;
17+
tier2_limit: uint32;
18+
tier3_used: uint32;
19+
tier3_limit: uint32;
20+
timestamp_ms: uint64;
21+
}

0 commit comments

Comments
 (0)