maple_runtime/lib.rs
1//! # MAPLE Resonance Runtime
2//!
3//! The foundational AI framework for Mapleverse, Finalverse, and iBank.
4//!
5//! ## Overview
6//!
7//! MAPLE (Multi-Agent Platform for Learning and Evolution) is a world-class
8//! multi-agent AI framework built entirely on **Resonance Architecture** principles.
9//!
10//! Unlike traditional agent frameworks (Google A2A, Anthropic MCP) that treat
11//! agents as isolated processes communicating via messages, MAPLE treats every
12//! entity as a **Resonator** participating in continuous, stateful **resonance**.
13//!
14//! ## Key Features
15//!
16//! - **Resonance-Native**: Built from the ground up on presence → coupling → meaning → intent → commitment → consequence
17//! - **8 Architectural Invariants**: Compile-time and runtime enforced safety guarantees
18//! - **Attention Economics**: Prevents runaway resource consumption and coercive patterns
19//! - **Commitment Accountability**: Every consequential action is attributable and auditable
20//! - **Human Agency Protection**: Architectural, not policy-based, safeguards
21//!
22//! ## Architecture
23//!
24//! ```text
25//! Traditional: Agent A --[message]--> Agent B --[message]--> Agent C
26//!
27//! MAPLE: Resonator A <==[coupling]==> Resonator B <==[coupling]==> Resonator C
28//! ↑ ↑ ↑
29//! [presence] [presence] [presence]
30//! ↓ ↓ ↓
31//! [meaning] -----------------> [meaning] -----------------> [meaning]
32//! ↓ ↓ ↓
33//! [intent] ------------------> [commitment] --------------> [consequence]
34//! ```
35//!
36//! ## Quick Start
37//!
38//! ```no_run
39//! use maple_runtime::{MapleRuntime, config::RuntimeConfig, ResonatorSpec};
40//!
41//! #[tokio::main]
42//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
43//! // Bootstrap runtime
44//! let config = RuntimeConfig::default();
45//! let runtime = MapleRuntime::bootstrap(config).await?;
46//!
47//! // Register a Resonator
48//! let spec = ResonatorSpec::default();
49//! let resonator = runtime.register_resonator(spec).await?;
50//!
51//! // Resonator is now active and can participate in resonance
52//!
53//! // Shutdown gracefully
54//! runtime.shutdown().await?;
55//! Ok(())
56//! }
57//! ```
58//!
59//! ## Platform-Specific Configurations
60//!
61//! ### Mapleverse (Pure AI Agents)
62//!
63//! ```no_run
64//! use maple_runtime::{MapleRuntime, config::mapleverse_runtime_config};
65//!
66//! # #[tokio::main]
67//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
68//! let config = mapleverse_runtime_config();
69//! let runtime = MapleRuntime::bootstrap(config).await?;
70//! # Ok(())
71//! # }
72//! ```
73//!
74//! ### Finalverse (Human-AI Coexistence)
75//!
76//! ```no_run
77//! use maple_runtime::{MapleRuntime, config::finalverse_runtime_config};
78//!
79//! # #[tokio::main]
80//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
81//! let config = finalverse_runtime_config();
82//! let runtime = MapleRuntime::bootstrap(config).await?;
83//! # Ok(())
84//! # }
85//! ```
86//!
87//! ### iBank (Autonomous Finance)
88//!
89//! ```no_run
90//! use maple_runtime::{MapleRuntime, config::ibank_runtime_config};
91//!
92//! # #[tokio::main]
93//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
94//! let config = ibank_runtime_config();
95//! let runtime = MapleRuntime::bootstrap(config).await?;
96//! # Ok(())
97//! # }
98//! ```
99//!
100//! ## The 8 Canonical Invariants
101//!
102//! These invariants are **enforced at runtime** and violations are treated as system errors:
103//!
104//! 1. **Presence precedes meaning**: A Resonator must be present before it can form or receive meaning
105//! 2. **Meaning precedes intent**: Intent cannot be formed without sufficient meaning
106//! 3. **Intent precedes commitment**: Commitments cannot be created without stabilized intent
107//! 4. **Commitment precedes consequence**: No consequence may occur without an explicit commitment
108//! 5. **Coupling is bounded by attention**: Coupling strength cannot exceed available attention
109//! 6. **Safety overrides optimization**: Safety constraints take precedence over performance
110//! 7. **Human agency cannot be bypassed**: Human Resonators must always be able to disengage
111//! 8. **Failure must be explicit**: All failures must be surfaced, never hidden
112
113#![cfg_attr(feature = "strict-docs", warn(missing_docs))]
114#![cfg_attr(not(feature = "strict-docs"), allow(missing_docs))]
115#![warn(clippy::all)]
116
117// Core modules
118pub mod allocator;
119pub mod config;
120pub mod fabrics;
121pub mod invariants;
122pub mod runtime_core;
123pub mod scheduler;
124pub mod telemetry;
125pub mod temporal;
126pub mod types;
127
128// Re-exports for convenience
129pub use runtime_core::{
130 ContinuityProof, CouplingHandle, MapleRuntime, ResonatorHandle, ResonatorIdentitySpec,
131 ResonatorSpec, ScheduleHandle,
132};
133
134pub use types::{
135 AttentionBudget, AttentionBudgetSpec, AttentionClass, Commitment, CommitmentContent,
136 CommitmentStatus, Coupling, CouplingParams, CouplingPersistence, CouplingScope, LocalTimestamp,
137 PresenceConfig, PresenceState, ResonatorId, ResonatorProfile, SymmetryType, TemporalAnchor,
138};
139
140pub use invariants::{ArchitecturalInvariant, InvariantViolation};
141
142// Error types
143pub use types::{
144 AttentionError, BootstrapError, CommitmentError, CouplingError, PresenceError,
145 RegistrationError, ResumeError, SchedulingError, ShutdownError, TemporalError,
146};
147
148#[cfg(test)]
149mod tests {
150 use super::*;
151
152 #[tokio::test]
153 async fn test_runtime_bootstrap() {
154 let config = config::RuntimeConfig::default();
155 let runtime = MapleRuntime::bootstrap(config).await;
156 assert!(runtime.is_ok());
157 }
158
159 #[tokio::test]
160 async fn test_resonator_registration() {
161 let config = config::RuntimeConfig::default();
162 let runtime = MapleRuntime::bootstrap(config).await.unwrap();
163
164 let spec = ResonatorSpec::default();
165 let result = runtime.register_resonator(spec).await;
166 assert!(result.is_ok());
167 }
168
169 #[tokio::test]
170 async fn test_mapleverse_config() {
171 let config = config::mapleverse_runtime_config();
172 let runtime = MapleRuntime::bootstrap(config).await;
173 assert!(runtime.is_ok());
174 }
175
176 #[tokio::test]
177 async fn test_finalverse_config() {
178 let config = config::finalverse_runtime_config();
179 let runtime = MapleRuntime::bootstrap(config).await;
180 assert!(runtime.is_ok());
181 }
182
183 #[tokio::test]
184 async fn test_ibank_config() {
185 let config = config::ibank_runtime_config();
186 let runtime = MapleRuntime::bootstrap(config).await;
187 assert!(runtime.is_ok());
188 }
189
190 #[tokio::test]
191 async fn test_presence_signaling() {
192 let config = config::RuntimeConfig::default();
193 let runtime = MapleRuntime::bootstrap(config).await.unwrap();
194
195 let spec = ResonatorSpec::default();
196 let resonator = runtime.register_resonator(spec).await.unwrap();
197
198 let presence = PresenceState::new();
199 let result = resonator.signal_presence(presence).await;
200 assert!(result.is_ok(), "Presence signaling failed: {:?}", result);
201
202 // A second immediate signal should still be rate-limited.
203 let result = resonator.signal_presence(PresenceState::new()).await;
204 assert!(matches!(result, Err(PresenceError::RateLimitExceeded)));
205 }
206
207 #[tokio::test]
208 async fn test_shutdown() {
209 let config = config::RuntimeConfig::default();
210 let runtime = MapleRuntime::bootstrap(config).await.unwrap();
211
212 let result = runtime.shutdown().await;
213 assert!(result.is_ok());
214 }
215}