maple_runtime/runtime_core/
runtime.rs1use std::sync::Arc;
4use tokio::sync::RwLock;
5use crate::types::*;
6use crate::runtime_core::{ResonatorRegistry, ProfileManager, ResonatorHandle, ContinuityProof};
7use crate::fabrics::{PresenceFabric, CouplingFabric};
8use crate::allocator::AttentionAllocator;
9use crate::invariants::InvariantGuard;
10use crate::temporal::TemporalCoordinator;
11use crate::scheduler::ResonanceScheduler;
12use crate::config::RuntimeConfig;
13use crate::telemetry::RuntimeTelemetry;
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 invariant_guard: Arc<InvariantGuard>,
52 temporal_coordinator: Arc<TemporalCoordinator>,
59 scheduler: Arc<ResonanceScheduler>,
60
61 telemetry: Arc<RuntimeTelemetry>,
65
66 shutdown: Arc<RwLock<bool>>,
70}
71
72impl MapleRuntime {
73 pub async fn bootstrap(config: RuntimeConfig) -> Result<Self, BootstrapError> {
99 tracing::info!("Bootstrapping MAPLE Resonance Runtime");
100
101 tracing::debug!("Phase 1: Initializing safety infrastructure");
103 let invariant_guard = Arc::new(InvariantGuard::new(&config.invariants));
104
105 tracing::debug!("Phase 2: Initializing temporal coordination");
107 let temporal_coordinator = Arc::new(TemporalCoordinator::new(&config.temporal));
108
109 tracing::debug!("Phase 3: Initializing resonance infrastructure");
111 let attention_allocator = Arc::new(AttentionAllocator::new(&config.attention));
112 let presence_fabric = Arc::new(PresenceFabric::new(&config.presence));
113 let coupling_fabric = Arc::new(CouplingFabric::new(
114 &config.coupling,
115 Arc::clone(&attention_allocator),
116 ));
117
118 tracing::debug!("Phase 4: Cognitive pipeline (placeholder)");
120
121 tracing::debug!("Phase 5: Initializing Resonator management");
123 let profile_manager = Arc::new(ProfileManager::new(&config.profiles));
124 let resonator_registry = Arc::new(ResonatorRegistry::new(&config.registry));
125
126 tracing::debug!("Phase 6: Initializing scheduler and telemetry");
128 let scheduler = Arc::new(ResonanceScheduler::new(&config.scheduling));
129 let telemetry = Arc::new(RuntimeTelemetry::new(&config.telemetry));
130
131 let inner = RuntimeInner {
132 resonator_registry,
133 profile_manager,
134 presence_fabric,
135 coupling_fabric,
136 attention_allocator,
137 invariant_guard,
138 temporal_coordinator,
139 scheduler,
140 telemetry,
141 shutdown: Arc::new(RwLock::new(false)),
142 };
143
144 tracing::info!("MAPLE Resonance Runtime bootstrapped successfully");
145
146 Ok(Self {
147 inner: Arc::new(inner),
148 })
149 }
150
151 pub async fn shutdown(&self) -> Result<(), ShutdownError> {
159 tracing::info!("Shutting down MAPLE Resonance Runtime");
160
161 let mut shutdown = self.inner.shutdown.write().await;
162 if *shutdown {
163 tracing::warn!("Runtime already shut down");
164 return Ok(());
165 }
166 *shutdown = true;
167 drop(shutdown);
168
169 tracing::debug!("Step 1: Stopping new commitments");
171
172 tracing::debug!("Step 2: Waiting for active commitments");
174
175 tracing::debug!("Step 3: Persisting Resonator continuity");
177 self.inner
178 .resonator_registry
179 .persist_all_continuity()
180 .await
181 .map_err(|e| ShutdownError::PersistenceError(e.to_string()))?;
182
183 tracing::debug!("Step 4: Persisting coupling topology");
185 self.inner
186 .coupling_fabric
187 .persist_topology()
188 .await
189 .map_err(|e| ShutdownError::PersistenceError(e.to_string()))?;
190
191 tracing::debug!("Step 5: Flushing telemetry");
193 self.inner.telemetry.flush().await;
194
195 tracing::info!("MAPLE Resonance Runtime shutdown complete");
196 Ok(())
197 }
198
199 pub async fn is_shutting_down(&self) -> bool {
201 *self.inner.shutdown.read().await
202 }
203
204 pub async fn register_resonator(
218 &self,
219 spec: ResonatorSpec,
220 ) -> Result<ResonatorHandle, RegistrationError> {
221 if self.is_shutting_down().await {
222 return Err(RegistrationError::InvalidSpec(
223 "Runtime is shutting down".to_string(),
224 ));
225 }
226
227 self.inner
229 .profile_manager
230 .validate_spec(&spec)
231 .map_err(|e| RegistrationError::ProfileValidation(e))?;
232
233 let identity = self
238 .inner
239 .resonator_registry
240 .create_identity(&spec.identity)
241 .await?;
242
243 self.inner
245 .presence_fabric
246 .initialize_presence(&identity, &spec.presence)
247 .await
248 .map_err(|e| RegistrationError::InvalidSpec(e.to_string()))?;
249
250 self.inner
252 .attention_allocator
253 .allocate_budget(&identity, &spec.attention)
254 .await
255 .map_err(|e| RegistrationError::InvalidSpec(e.to_string()))?;
256
257 self.inner
259 .coupling_fabric
260 .register(&identity)
261 .await
262 .map_err(|e| RegistrationError::InvalidSpec(e.to_string()))?;
263
264 let handle = ResonatorHandle::new(identity, self.clone());
266
267 self.inner.telemetry.resonator_registered(&handle);
269
270 tracing::info!("Registered Resonator: {}", identity);
271
272 Ok(handle)
273 }
274
275 pub async fn resume_resonator(
280 &self,
281 continuity_proof: ContinuityProof,
282 ) -> Result<ResonatorHandle, ResumeError> {
283 if self.is_shutting_down().await {
284 return Err(ResumeError::StateRestorationFailed(
285 "Runtime is shutting down".to_string(),
286 ));
287 }
288
289 let record = self
291 .inner
292 .resonator_registry
293 .verify_continuity(&continuity_proof)
294 .await?;
295
296 let identity = record.identity;
298
299 self.inner
301 .presence_fabric
302 .restore_presence(&identity, &record.presence_state)
303 .await
304 .map_err(|e| ResumeError::StateRestorationFailed(e.to_string()))?;
305
306 self.inner
308 .attention_allocator
309 .restore_budget(&identity, &record.attention_state)
310 .await
311 .map_err(|e| ResumeError::StateRestorationFailed(e.to_string()))?;
312
313 self.inner
315 .coupling_fabric
316 .restore_couplings(&identity, &record.couplings)
317 .await
318 .map_err(|e| ResumeError::StateRestorationFailed(e.to_string()))?;
319
320 let handle = ResonatorHandle::new(identity, self.clone());
323
324 self.inner.telemetry.resonator_resumed(&handle);
325
326 tracing::info!("Resumed Resonator: {}", identity);
327
328 Ok(handle)
329 }
330
331 pub fn presence_fabric(&self) -> &Arc<PresenceFabric> {
336 &self.inner.presence_fabric
337 }
338
339 pub fn coupling_fabric(&self) -> &Arc<CouplingFabric> {
340 &self.inner.coupling_fabric
341 }
342
343 pub fn attention_allocator(&self) -> &Arc<AttentionAllocator> {
344 &self.inner.attention_allocator
345 }
346
347 pub fn temporal_coordinator(&self) -> &Arc<TemporalCoordinator> {
348 &self.inner.temporal_coordinator
349 }
350
351 pub fn scheduler(&self) -> &Arc<ResonanceScheduler> {
352 &self.inner.scheduler
353 }
354
355 pub fn telemetry(&self) -> &Arc<RuntimeTelemetry> {
356 &self.inner.telemetry
357 }
358}
359
360#[derive(Debug, Clone)]
362pub struct ResonatorSpec {
363 pub identity: ResonatorIdentitySpec,
365
366 pub profile: ResonatorProfile,
368
369 pub capabilities: Vec<CapabilitySpec>,
371
372 pub presence: PresenceConfig,
374
375 pub attention: AttentionBudgetSpec,
377
378 pub initial_memory: Option<MemorySnapshot>,
380
381 pub coupling_affinity: CouplingAffinitySpec,
383}
384
385impl Default for ResonatorSpec {
386 fn default() -> Self {
387 Self {
388 identity: ResonatorIdentitySpec::default(),
389 profile: ResonatorProfile::default(),
390 capabilities: Vec::new(),
391 presence: PresenceConfig::default(),
392 attention: AttentionBudgetSpec::default(),
393 initial_memory: None,
394 coupling_affinity: CouplingAffinitySpec::default(),
395 }
396 }
397}
398
399#[derive(Debug, Clone)]
401pub struct ResonatorIdentitySpec {
402 pub name: Option<String>,
404
405 pub metadata: std::collections::HashMap<String, String>,
407}
408
409impl Default for ResonatorIdentitySpec {
410 fn default() -> Self {
411 Self {
412 name: None,
413 metadata: std::collections::HashMap::new(),
414 }
415 }
416}
417
418#[derive(Debug, Clone)]
420pub struct CapabilitySpec {
421 pub name: String,
422 pub version: String,
423}
424
425#[derive(Debug, Clone)]
427pub struct MemorySnapshot {
428 pub data: Vec<u8>,
429}