Skip to main content

feagi_services/impls/
runtime_service_impl.rs

1// Copyright 2025 Neuraville Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4/*!
5Runtime control service implementation.
6
7Provides control over the FEAGI burst engine runtime.
8
9Copyright 2025 Neuraville Inc.
10Licensed under the Apache License, Version 2.0
11*/
12
13use std::sync::Arc;
14
15use ahash::AHashSet;
16use async_trait::async_trait;
17use feagi_npu_burst_engine::BurstLoopRunner;
18use feagi_structures::genomic::cortical_area::CorticalID;
19use parking_lot::RwLock;
20use tracing::{debug, info, warn};
21
22use crate::traits::runtime_service::ManualStimulationMode;
23use crate::traits::RuntimeService;
24use crate::types::{RuntimeStatus, ServiceError, ServiceResult};
25
26/// Default implementation of RuntimeService
27///
28/// Wraps the BurstLoopRunner and provides async interface for runtime control.
29pub struct RuntimeServiceImpl {
30    burst_runner: Arc<RwLock<BurstLoopRunner>>,
31    paused: Arc<RwLock<bool>>,
32}
33
34impl RuntimeServiceImpl {
35    /// Create a new RuntimeServiceImpl
36    pub fn new(burst_runner: Arc<RwLock<BurstLoopRunner>>) -> Self {
37        Self {
38            burst_runner,
39            paused: Arc::new(RwLock::new(false)),
40        }
41    }
42}
43
44#[async_trait]
45impl RuntimeService for RuntimeServiceImpl {
46    async fn start(&self) -> ServiceResult<()> {
47        info!(target: "feagi-services", "Starting burst engine");
48
49        let mut runner = self.burst_runner.write();
50
51        runner
52            .start()
53            .map_err(|e| ServiceError::InvalidState(e.to_string()))?;
54
55        // Clear paused flag
56        *self.paused.write() = false;
57
58        Ok(())
59    }
60
61    async fn stop(&self) -> ServiceResult<()> {
62        info!(target: "feagi-services", "Stopping burst engine");
63
64        let mut runner = self.burst_runner.write();
65        runner
66            .stop_strict()
67            .map_err(|e| ServiceError::Backend(format!("Failed to stop burst engine: {}", e)))?;
68
69        // Clear paused flag
70        *self.paused.write() = false;
71
72        Ok(())
73    }
74
75    async fn pause(&self) -> ServiceResult<()> {
76        info!(target: "feagi-services", "Pausing burst engine");
77
78        let runner = self.burst_runner.read();
79        if !runner.is_running() {
80            return Err(ServiceError::InvalidState(
81                "Burst engine is not running".to_string(),
82            ));
83        }
84
85        // Set paused flag (actual pause implementation depends on burst loop design)
86        *self.paused.write() = true;
87
88        // TODO: Implement actual pause mechanism in BurstLoopRunner
89        // For now, we just track the paused state
90        warn!(target: "feagi-services", "Pause not yet implemented in BurstLoopRunner - using flag only");
91
92        Ok(())
93    }
94
95    async fn resume(&self) -> ServiceResult<()> {
96        info!(target: "feagi-services", "Resuming burst engine");
97
98        let paused = *self.paused.read();
99        if !paused {
100            return Err(ServiceError::InvalidState(
101                "Burst engine is not paused".to_string(),
102            ));
103        }
104
105        // Clear paused flag
106        *self.paused.write() = false;
107
108        // TODO: Implement actual resume mechanism in BurstLoopRunner
109        warn!(target: "feagi-services", "Resume not yet implemented in BurstLoopRunner - using flag only");
110
111        Ok(())
112    }
113
114    async fn step(&self) -> ServiceResult<()> {
115        info!(target: "feagi-services", "Executing single burst step");
116
117        let runner = self.burst_runner.read();
118        if runner.is_running() {
119            return Err(ServiceError::InvalidState(
120                "Cannot step while burst engine is running in continuous mode".to_string(),
121            ));
122        }
123
124        // TODO: Implement single-step execution in BurstLoopRunner
125        warn!(target: "feagi-services", "Single-step execution not yet implemented in BurstLoopRunner");
126
127        Err(ServiceError::NotImplemented(
128            "Single-step execution not yet implemented".to_string(),
129        ))
130    }
131
132    async fn get_status(&self) -> ServiceResult<RuntimeStatus> {
133        let runner = self.burst_runner.read();
134        let is_running = runner.is_running();
135        let burst_count = runner.get_burst_count();
136        let is_paused = *self.paused.read();
137
138        // Note: Some metrics not yet available from BurstLoopRunner
139        // - current_rate_hz: Would require tracking actual execution rate
140        // - last_burst_neuron_count: Not tracked by BurstLoopRunner
141        // - avg_burst_time_ms: Not tracked by BurstLoopRunner
142        Ok(RuntimeStatus {
143            is_running,
144            is_paused,
145            frequency_hz: runner.get_frequency(),
146            burst_count,
147            current_rate_hz: if is_running {
148                runner.get_frequency()
149            } else {
150                0.0
151            },
152            last_burst_neuron_count: 0, // Not yet tracked
153            avg_burst_time_ms: 0.0,     // Not yet tracked
154        })
155    }
156
157    async fn set_frequency(&self, frequency_hz: f64) -> ServiceResult<()> {
158        if frequency_hz <= 0.0 {
159            return Err(ServiceError::InvalidInput(
160                "Frequency must be greater than 0".to_string(),
161            ));
162        }
163
164        info!(target: "feagi-services", "Setting burst frequency to {} Hz", frequency_hz);
165
166        let mut runner = self.burst_runner.write();
167        runner.set_frequency(frequency_hz);
168
169        Ok(())
170    }
171
172    async fn get_burst_count(&self) -> ServiceResult<u64> {
173        let runner = self.burst_runner.read();
174        Ok(runner.get_burst_count())
175    }
176
177    async fn reset_burst_count(&self) -> ServiceResult<()> {
178        info!(target: "feagi-services", "Resetting burst count");
179
180        // TODO: Implement burst count reset in BurstLoopRunner
181        warn!(target: "feagi-services", "Burst count reset not yet implemented in BurstLoopRunner");
182
183        Err(ServiceError::NotImplemented(
184            "Burst count reset not yet implemented".to_string(),
185        ))
186    }
187
188    async fn register_motor_subscriptions(
189        &self,
190        agent_id: &str,
191        cortical_ids: Vec<String>,
192        rate_hz: f64,
193    ) -> ServiceResult<()> {
194        if rate_hz <= 0.0 {
195            return Err(ServiceError::InvalidInput(
196                "Motor rate must be greater than 0".to_string(),
197            ));
198        }
199
200        let cortical_set: AHashSet<String> = cortical_ids.into_iter().collect();
201        let runner = self.burst_runner.read();
202        runner
203            .register_motor_subscriptions_with_rate(agent_id.to_string(), cortical_set, rate_hz)
204            .map_err(|e| ServiceError::InvalidInput(e.to_string()))
205    }
206
207    async fn register_visualization_subscriptions(
208        &self,
209        agent_id: &str,
210        rate_hz: f64,
211    ) -> ServiceResult<()> {
212        if rate_hz <= 0.0 {
213            return Err(ServiceError::InvalidInput(
214                "Visualization rate must be greater than 0".to_string(),
215            ));
216        }
217
218        let runner = self.burst_runner.read();
219        runner
220            .register_visualization_subscriptions_with_rate(agent_id.to_string(), rate_hz)
221            .map_err(|e| ServiceError::InvalidInput(e.to_string()))
222    }
223
224    fn unregister_motor_subscriptions(&self, agent_id: &str) {
225        let runner = self.burst_runner.read();
226        runner.unregister_motor_subscriptions(agent_id);
227    }
228
229    fn unregister_visualization_subscriptions(&self, agent_id: &str) {
230        let runner = self.burst_runner.read();
231        runner.unregister_visualization_subscriptions(agent_id);
232    }
233
234    async fn get_fcl_snapshot(&self) -> ServiceResult<Vec<(u64, f32)>> {
235        let runner = self.burst_runner.read();
236        let fcl_data = runner.get_fcl_snapshot();
237
238        // Convert NeuronId (u32) to u64
239        let result = fcl_data
240            .iter()
241            .map(|(neuron_id, potential)| (neuron_id.0 as u64, *potential))
242            .collect();
243
244        Ok(result)
245    }
246
247    async fn get_fcl_snapshot_with_cortical_idx(&self) -> ServiceResult<Vec<(u64, u32, f32)>> {
248        let runner = self.burst_runner.read();
249        let npu = runner.get_npu();
250
251        // CRITICAL: Acquire lock ONCE and do BOTH operations (FCL snapshot + cortical_idx lookup)
252        // Previous code acquired lock twice: once for get_fcl_snapshot(), once for cortical_idx
253        let lock_start = std::time::Instant::now();
254        let thread_id = std::thread::current().id();
255        debug!("[NPU-LOCK] RUNTIME-SERVICE: Thread {:?} attempting NPU lock for get_fcl_snapshot_with_cortical_idx at {:?}", thread_id, lock_start);
256        let result: Vec<(u64, u32, f32)> = {
257            // Acquire lock ONCE for both FCL snapshot and cortical_idx lookup
258            let npu_lock = npu.lock().unwrap();
259            let lock_acquired = std::time::Instant::now();
260            let lock_wait = lock_acquired.duration_since(lock_start);
261            debug!(
262                "[NPU-LOCK] RUNTIME-SERVICE: Thread {:?} acquired lock after {:.2}ms wait for get_fcl_snapshot_with_cortical_idx",
263                thread_id,
264                lock_wait.as_secs_f64() * 1000.0
265            );
266
267            // STRICT: Resolve cortical_idx without fallbacks (memory neurons are handled explicitly).
268            let fcl_data = npu_lock
269                .get_last_fcl_snapshot_with_cortical_idx()
270                .map_err(|e| {
271                    ServiceError::Internal(format!("Failed to resolve FCL cortical_idx: {e}"))
272                })?;
273            debug!(
274                "[NPU-LOCK] RUNTIME-SERVICE: Thread {:?} got FCL snapshot ({} neurons) with cortical_idx",
275                thread_id,
276                fcl_data.len()
277            );
278
279            fcl_data
280                .into_iter()
281                .map(|(neuron_id, cortical_idx, potential)| {
282                    (neuron_id.0 as u64, cortical_idx, potential)
283                })
284                .collect()
285        }; // Lock released here
286        let lock_released = std::time::Instant::now();
287        let _lock_hold_duration = lock_released.duration_since(lock_start);
288        debug!("[NPU-LOCK] RUNTIME-SERVICE: Thread {:?} RELEASED NPU lock after get_fcl_snapshot_with_cortical_idx (total: {:.2}ms wait + {:.2}ms hold, {} neurons)", 
289            thread_id,
290            lock_released.duration_since(lock_start).as_secs_f64() * 1000.0,
291            lock_released.duration_since(lock_start).as_secs_f64() * 1000.0,
292            result.len());
293
294        Ok(result)
295    }
296
297    async fn get_fire_queue_sample(
298        &self,
299    ) -> ServiceResult<
300        std::collections::HashMap<u32, (Vec<u32>, Vec<u32>, Vec<u32>, Vec<u32>, Vec<f32>)>,
301    > {
302        let mut runner = self.burst_runner.write();
303
304        match runner.get_fire_queue_sample() {
305            Some(sample) => {
306                // Convert AHashMap to std::HashMap for service layer compatibility
307                let result: std::collections::HashMap<_, _> = sample.into_iter().collect();
308                Ok(result)
309            }
310            None => Ok(std::collections::HashMap::new()),
311        }
312    }
313
314    async fn get_fire_ledger_configs(&self) -> ServiceResult<Vec<(u32, usize)>> {
315        debug!("[NPU-LOCK] RUNTIME-SERVICE: get_fire_ledger_configs() called - this acquires NPU lock!");
316        let runner = self.burst_runner.read();
317        let configs = runner.get_fire_ledger_configs();
318        Ok(configs)
319    }
320
321    async fn configure_fire_ledger_window(
322        &self,
323        cortical_idx: u32,
324        window_size: usize,
325    ) -> ServiceResult<()> {
326        let mut runner = self.burst_runner.write();
327        runner
328            .configure_fire_ledger_window(cortical_idx, window_size)
329            .map_err(|e| {
330                ServiceError::Internal(format!("Failed to configure fire ledger window: {e}"))
331            })?;
332
333        info!(target: "feagi-services", "Configured Fire Ledger window for area {}: {} bursts",
334            cortical_idx, window_size);
335
336        Ok(())
337    }
338
339    async fn get_fcl_sampler_config(&self) -> ServiceResult<(f64, u32)> {
340        let runner = self.burst_runner.read();
341        Ok(runner.get_fcl_sampler_config())
342    }
343
344    async fn set_fcl_sampler_config(
345        &self,
346        frequency: Option<f64>,
347        consumer: Option<u32>,
348    ) -> ServiceResult<()> {
349        let runner = self.burst_runner.read();
350        runner.set_fcl_sampler_config(frequency, consumer);
351        Ok(())
352    }
353
354    async fn get_area_fcl_sample_rate(&self, area_id: u32) -> ServiceResult<f64> {
355        let runner = self.burst_runner.read();
356        Ok(runner.get_area_fcl_sample_rate(area_id))
357    }
358
359    async fn set_area_fcl_sample_rate(&self, area_id: u32, sample_rate: f64) -> ServiceResult<()> {
360        if sample_rate <= 0.0 || sample_rate > 1000.0 {
361            return Err(ServiceError::InvalidInput(
362                "Sample rate must be between 0 and 1000 Hz".to_string(),
363            ));
364        }
365
366        let runner = self.burst_runner.read();
367        runner.set_area_fcl_sample_rate(area_id, sample_rate);
368
369        info!(target: "feagi-services", "Set FCL sample rate for area {} to {}Hz", area_id, sample_rate);
370        Ok(())
371    }
372
373    async fn inject_sensory_by_coordinates(
374        &self,
375        cortical_id: &str,
376        xyzp_data: &[(u32, u32, u32, f32)],
377        mode: ManualStimulationMode,
378    ) -> ServiceResult<usize> {
379        // Parse cortical ID from base64 string
380        let cortical_id_typed = CorticalID::try_from_base_64(cortical_id).map_err(|e| {
381            ServiceError::InvalidInput(format!("Invalid cortical ID format: {}", e))
382        })?;
383
384        // Get NPU from burst runner
385        let runner = self.burst_runner.read();
386        let npu = runner.get_npu();
387
388        // Inject using NPU's service layer method
389        let lock_start = std::time::Instant::now();
390        debug!(
391            "[NPU-LOCK] RUNTIME-SERVICE: Acquiring lock for manual stimulation ({} coordinates) - THIS CAN BLOCK BURST LOOP!",
392            xyzp_data.len()
393        );
394        let injected_count = {
395            let mut npu_lock = npu
396                .lock()
397                .map_err(|e| ServiceError::Backend(format!("Failed to lock NPU: {}", e)))?;
398            let lock_wait = lock_start.elapsed();
399            debug!(
400                "[NPU-LOCK] RUNTIME-SERVICE: Lock acquired for manual stimulation (waited {:.2}ms)",
401                lock_wait.as_secs_f64() * 1000.0
402            );
403            let result = match mode {
404                ManualStimulationMode::Candidate => {
405                    npu_lock.inject_sensory_xyzp_by_id(&cortical_id_typed, xyzp_data)
406                }
407                ManualStimulationMode::ForceFire => {
408                    npu_lock.inject_force_fire_by_coordinates(&cortical_id_typed, xyzp_data)
409                }
410            };
411            let lock_hold_duration = lock_start.elapsed();
412            debug!(
413                "[NPU-LOCK] RUNTIME-SERVICE: Releasing lock after manual stimulation (held for {:.2}ms)",
414                lock_hold_duration.as_secs_f64() * 1000.0
415            );
416            result
417        };
418
419        if injected_count == 0 && !xyzp_data.is_empty() {
420            warn!(target: "feagi-services",
421                "No neurons found for injection: cortical_id={}, coordinates={}",
422                cortical_id, xyzp_data.len());
423        } else if injected_count > 0 {
424            info!(target: "feagi-services",
425                "Injected {} neurons into FCL for cortical area {}",
426                injected_count, cortical_id);
427        }
428
429        Ok(injected_count)
430    }
431}