maple_runtime/runtime_core/
runtime.rs1use crate::allocator::AttentionAllocator;
4use crate::config::RuntimeConfig;
5use crate::fabrics::{CouplingFabric, PresenceFabric};
6use crate::invariants::InvariantGuard;
7use crate::runtime_core::{ContinuityProof, ProfileManager, ResonatorHandle, ResonatorRegistry};
8use crate::scheduler::ResonanceScheduler;
9use crate::telemetry::RuntimeTelemetry;
10use crate::temporal::TemporalCoordinator;
11use crate::types::*;
12use std::sync::Arc;
13use tokio::sync::RwLock;
14
15#[derive(Clone)]
22pub struct MapleRuntime {
23 inner: Arc<RuntimeInner>,
24}
25
26struct RuntimeInner {
27 resonator_registry: Arc<ResonatorRegistry>,
31 profile_manager: Arc<ProfileManager>,
32
33 presence_fabric: Arc<PresenceFabric>,
37 coupling_fabric: Arc<CouplingFabric>,
38 attention_allocator: Arc<AttentionAllocator>,
39
40 #[allow(dead_code)]
52 invariant_guard: Arc<InvariantGuard>,
53 temporal_coordinator: Arc<TemporalCoordinator>,
60 scheduler: Arc<ResonanceScheduler>,
61
62 telemetry: Arc<RuntimeTelemetry>,
66
67 shutdown: Arc<RwLock<bool>>,
71}
72
73impl MapleRuntime {
74 pub async fn bootstrap(config: RuntimeConfig) -> Result<Self, BootstrapError> {
100 tracing::info!("Bootstrapping MAPLE Resonance Runtime");
101
102 tracing::debug!("Phase 1: Initializing safety infrastructure");
104 let invariant_guard = Arc::new(InvariantGuard::new(&config.invariants));
105
106 tracing::debug!("Phase 2: Initializing temporal coordination");
108 let temporal_coordinator = Arc::new(TemporalCoordinator::new(&config.temporal));
109
110 tracing::debug!("Phase 3: Initializing resonance infrastructure");
112 let attention_allocator = Arc::new(AttentionAllocator::new(&config.attention));
113 let presence_fabric = Arc::new(PresenceFabric::new(&config.presence));
114 let coupling_fabric = Arc::new(CouplingFabric::new(
115 &config.coupling,
116 Arc::clone(&attention_allocator),
117 ));
118
119 tracing::debug!("Phase 4: Cognitive pipeline (placeholder)");
121
122 tracing::debug!("Phase 5: Initializing Resonator management");
124 let profile_manager = Arc::new(ProfileManager::new(&config.profiles));
125 let resonator_registry = Arc::new(ResonatorRegistry::new(&config.registry));
126
127 tracing::debug!("Phase 6: Initializing scheduler and telemetry");
129 let scheduler = Arc::new(ResonanceScheduler::new(&config.scheduling));
130 let telemetry = Arc::new(RuntimeTelemetry::new(&config.telemetry));
131
132 let inner = RuntimeInner {
133 resonator_registry,
134 profile_manager,
135 presence_fabric,
136 coupling_fabric,
137 attention_allocator,
138 invariant_guard,
139 temporal_coordinator,
140 scheduler,
141 telemetry,
142 shutdown: Arc::new(RwLock::new(false)),
143 };
144
145 tracing::info!("MAPLE Resonance Runtime bootstrapped successfully");
146
147 Ok(Self {
148 inner: Arc::new(inner),
149 })
150 }
151
152 pub async fn shutdown(&self) -> Result<(), ShutdownError> {
160 tracing::info!("Shutting down MAPLE Resonance Runtime");
161
162 let mut shutdown = self.inner.shutdown.write().await;
163 if *shutdown {
164 tracing::warn!("Runtime already shut down");
165 return Ok(());
166 }
167 *shutdown = true;
168 drop(shutdown);
169
170 tracing::debug!("Step 1: Stopping new commitments");
172
173 tracing::debug!("Step 2: Waiting for active commitments");
175
176 tracing::debug!("Step 3: Persisting Resonator continuity");
178 self.inner
179 .resonator_registry
180 .persist_all_continuity()
181 .await
182 .map_err(|e| ShutdownError::PersistenceError(e.to_string()))?;
183
184 tracing::debug!("Step 4: Persisting coupling topology");
186 self.inner
187 .coupling_fabric
188 .persist_topology()
189 .await
190 .map_err(|e| ShutdownError::PersistenceError(e.to_string()))?;
191
192 tracing::debug!("Step 5: Flushing telemetry");
194 self.inner.telemetry.flush().await;
195
196 tracing::info!("MAPLE Resonance Runtime shutdown complete");
197 Ok(())
198 }
199
200 pub async fn is_shutting_down(&self) -> bool {
202 *self.inner.shutdown.read().await
203 }
204
205 pub async fn register_resonator(
219 &self,
220 spec: ResonatorSpec,
221 ) -> Result<ResonatorHandle, RegistrationError> {
222 if self.is_shutting_down().await {
223 return Err(RegistrationError::InvalidSpec(
224 "Runtime is shutting down".to_string(),
225 ));
226 }
227
228 self.inner
230 .profile_manager
231 .validate_spec(&spec)
232 .map_err(|e| RegistrationError::ProfileValidation(e))?;
233
234 let identity = self
239 .inner
240 .resonator_registry
241 .create_identity(&spec.identity)
242 .await?;
243
244 self.inner
246 .presence_fabric
247 .initialize_presence(&identity, &spec.presence)
248 .await
249 .map_err(|e| RegistrationError::InvalidSpec(e.to_string()))?;
250
251 self.inner
253 .attention_allocator
254 .allocate_budget(&identity, &spec.attention)
255 .await
256 .map_err(|e| RegistrationError::InvalidSpec(e.to_string()))?;
257
258 self.inner
260 .coupling_fabric
261 .register(&identity)
262 .await
263 .map_err(|e| RegistrationError::InvalidSpec(e.to_string()))?;
264
265 let handle = ResonatorHandle::new(identity, self.clone());
267
268 self.inner.telemetry.resonator_registered(&handle);
270
271 tracing::info!("Registered Resonator: {}", identity);
272
273 Ok(handle)
274 }
275
276 pub async fn resume_resonator(
281 &self,
282 continuity_proof: ContinuityProof,
283 ) -> Result<ResonatorHandle, ResumeError> {
284 if self.is_shutting_down().await {
285 return Err(ResumeError::StateRestorationFailed(
286 "Runtime is shutting down".to_string(),
287 ));
288 }
289
290 let record = self
292 .inner
293 .resonator_registry
294 .verify_continuity(&continuity_proof)
295 .await?;
296
297 let identity = record.identity;
299
300 self.inner
302 .presence_fabric
303 .restore_presence(&identity, &record.presence_state)
304 .await
305 .map_err(|e| ResumeError::StateRestorationFailed(e.to_string()))?;
306
307 self.inner
309 .attention_allocator
310 .restore_budget(&identity, &record.attention_state)
311 .await
312 .map_err(|e| ResumeError::StateRestorationFailed(e.to_string()))?;
313
314 self.inner
316 .coupling_fabric
317 .restore_couplings(&identity, &record.couplings)
318 .await
319 .map_err(|e| ResumeError::StateRestorationFailed(e.to_string()))?;
320
321 let handle = ResonatorHandle::new(identity, self.clone());
324
325 self.inner.telemetry.resonator_resumed(&handle);
326
327 tracing::info!("Resumed Resonator: {}", identity);
328
329 Ok(handle)
330 }
331
332 pub fn presence_fabric(&self) -> &Arc<PresenceFabric> {
337 &self.inner.presence_fabric
338 }
339
340 pub fn coupling_fabric(&self) -> &Arc<CouplingFabric> {
341 &self.inner.coupling_fabric
342 }
343
344 pub fn attention_allocator(&self) -> &Arc<AttentionAllocator> {
345 &self.inner.attention_allocator
346 }
347
348 pub fn temporal_coordinator(&self) -> &Arc<TemporalCoordinator> {
349 &self.inner.temporal_coordinator
350 }
351
352 pub fn scheduler(&self) -> &Arc<ResonanceScheduler> {
353 &self.inner.scheduler
354 }
355
356 pub fn telemetry(&self) -> &Arc<RuntimeTelemetry> {
357 &self.inner.telemetry
358 }
359}
360
361#[derive(Debug, Clone)]
363pub struct ResonatorSpec {
364 pub identity: ResonatorIdentitySpec,
366
367 pub profile: ResonatorProfile,
369
370 pub capabilities: Vec<CapabilitySpec>,
372
373 pub presence: PresenceConfig,
375
376 pub attention: AttentionBudgetSpec,
378
379 pub initial_memory: Option<MemorySnapshot>,
381
382 pub coupling_affinity: CouplingAffinitySpec,
384}
385
386impl Default for ResonatorSpec {
387 fn default() -> Self {
388 Self {
389 identity: ResonatorIdentitySpec::default(),
390 profile: ResonatorProfile::default(),
391 capabilities: Vec::new(),
392 presence: PresenceConfig::default(),
393 attention: AttentionBudgetSpec::default(),
394 initial_memory: None,
395 coupling_affinity: CouplingAffinitySpec::default(),
396 }
397 }
398}
399
400#[derive(Debug, Clone)]
402pub struct ResonatorIdentitySpec {
403 pub name: Option<String>,
405
406 pub metadata: std::collections::HashMap<String, String>,
408}
409
410impl Default for ResonatorIdentitySpec {
411 fn default() -> Self {
412 Self {
413 name: None,
414 metadata: std::collections::HashMap::new(),
415 }
416 }
417}
418
419#[derive(Debug, Clone)]
421pub struct CapabilitySpec {
422 pub name: String,
423 pub version: String,
424}
425
426#[derive(Debug, Clone)]
428pub struct MemorySnapshot {
429 pub data: Vec<u8>,
430}