Skip to main content

qualia_core_db/extensions/
extension_bus.rs

1use crate::extension_manifest::ExtensionManifest;
2use crate::{q_hash, NQuin};
3use std::collections::HashMap;
4use std::fs;
5use std::path::{Path, PathBuf};
6use std::sync::RwLock;
7
8/// Global registry of discovered and provisioned capability extensions.
9pub struct ExtensionBus {
10    /// Maps an extension_id to its parsed manifest
11    registered_extensions: RwLock<HashMap<String, ExtensionManifest>>,
12    /// Maps a semantic interface hash (e.g., q_hash("q42:VideoTranscode")) to a list of capable extension IDs
13    capability_index: RwLock<HashMap<u64, Vec<String>>>,
14    /// The local directory where extensions are stored
15    extensions_dir: PathBuf,
16}
17
18impl ExtensionBus {
19    /// Creates `ExtensionBus` with the given state.
20    pub fn new(extensions_dir: impl AsRef<Path>) -> Self {
21        let bus = Self {
22            registered_extensions: RwLock::new(HashMap::new()),
23            capability_index: RwLock::new(HashMap::new()),
24            extensions_dir: extensions_dir.as_ref().to_path_buf(),
25        };
26        bus.scan_and_load_extensions();
27        bus
28    }
29
30    /// Scans the extensions directory for `manifest.json` files and loads them into memory.
31    pub fn scan_and_load_extensions(&self) {
32        if !self.extensions_dir.exists() {
33            let _ = fs::create_dir_all(&self.extensions_dir);
34            return;
35        }
36
37        let mut reg_lock = self.registered_extensions.write().unwrap();
38        let mut idx_lock = self.capability_index.write().unwrap();
39
40        if let Ok(entries) = fs::read_dir(&self.extensions_dir) {
41            for entry in entries.flatten() {
42                let path = entry.path();
43                if path.is_dir() {
44                    let manifest_path = path.join("manifest.json");
45                    if manifest_path.exists() {
46                        if let Ok(json_bytes) = fs::read(&manifest_path) {
47                            if let Ok(manifest) = ExtensionManifest::from_json(&json_bytes) {
48                                println!(
49                                    "Loaded extension: {} (v{})",
50                                    manifest.display_name, manifest.version
51                                );
52
53                                // Index capabilities
54                                for cap in &manifest.capabilities {
55                                    let cap_hash = q_hash(&cap.interface);
56                                    idx_lock
57                                        .entry(cap_hash)
58                                        .or_default()
59                                        .push(manifest.extension_id.clone());
60                                }
61
62                                reg_lock.insert(manifest.extension_id.clone(), manifest);
63                            } else {
64                                eprintln!("Failed to parse manifest at {:?}", manifest_path);
65                            }
66                        }
67                    }
68                }
69            }
70        }
71    }
72
73    /// Register a new extension from an absolute path (CLI workflow)
74    pub fn register_extension_from_path(&self, manifest_path: &Path) -> Result<(), String> {
75        let json_bytes =
76            fs::read(manifest_path).map_err(|e| format!("Failed to read manifest: {}", e))?;
77        let manifest = ExtensionManifest::from_json(&json_bytes)
78            .map_err(|e| format!("Invalid manifest JSON: {}", e))?;
79
80        // Copy to local extensions dir
81        let target_dir = self.extensions_dir.join(&manifest.extension_id);
82        fs::create_dir_all(&target_dir)
83            .map_err(|e| format!("Failed to create extension dir: {}", e))?;
84        fs::write(target_dir.join("manifest.json"), &json_bytes)
85            .map_err(|e| format!("Failed to write manifest: {}", e))?;
86
87        let mut reg_lock = self.registered_extensions.write().unwrap();
88        let mut idx_lock = self.capability_index.write().unwrap();
89
90        for cap in &manifest.capabilities {
91            let cap_hash = q_hash(&cap.interface);
92            idx_lock
93                .entry(cap_hash)
94                .or_default()
95                .push(manifest.extension_id.clone());
96        }
97
98        reg_lock.insert(manifest.extension_id.clone(), manifest);
99        Ok(())
100    }
101
102    /// Retrieve all registered manifests (used by the Studio UI)
103    pub fn list_extensions(&self) -> Vec<ExtensionManifest> {
104        let lock = self.registered_extensions.read().unwrap();
105        lock.values().cloned().collect()
106    }
107
108    /// Query the bus for any extensions supporting a given semantic interface hash
109    pub fn query_capability(&self, interface_hash: u64) -> Vec<ExtensionManifest> {
110        let idx_lock = self.capability_index.read().unwrap();
111        let reg_lock = self.registered_extensions.read().unwrap();
112
113        let mut results = Vec::new();
114        if let Some(ext_ids) = idx_lock.get(&interface_hash) {
115            for id in ext_ids {
116                if let Some(manifest) = reg_lock.get(id) {
117                    results.push(manifest.clone());
118                }
119            }
120        }
121        results
122    }
123
124    /// Dispatches a task recipe to an extension over localhost RPC.
125    /// Includes Sentinel Gatekeeping logic to enforce sensitivity classification.
126    pub fn dispatch_task(
127        &self,
128        extension_id: &str,
129        input_file_path: &str,
130        pipeline_steps: Vec<serde_json::Value>,
131        sensitivity_context: u64,
132        guardianship_override: bool,
133    ) -> Result<String, String> {
134        // B6: Sentinel Gatekeeping
135        let sensitivity = sensitivity_context >> 56;
136        if sensitivity == 0x02 && !guardianship_override {
137            // Log violation to WAL
138            let violation_quin = NQuin {
139                subject: q_hash(extension_id),
140                predicate: q_hash("q42:GatekeeperViolation"),
141                object: q_hash(input_file_path),
142                context: sensitivity_context,
143                metadata: 0,
144                parity: 0, // XOR fold omitted for brevity
145            };
146            let _ = crate::wal::append_mutation(&violation_quin);
147
148            return Err("GATEKEEPER_BLOCK: Cannot send Classified (0x02) data to an extension without Guardianship override.".to_string());
149        }
150
151        let reg_lock = self.registered_extensions.read().unwrap();
152        let manifest = reg_lock.get(extension_id).ok_or("Extension not found")?;
153
154        // B5: Task Recipe Payload
155        let payload = serde_json::json!({
156            "input_file_path": input_file_path, // Zero-copy pointer to local file
157            "pipeline_steps": pipeline_steps,
158            "output_routing": "http://127.0.0.1:8080/ingest" // Loopback ingest target
159        });
160
161        // Simulate Dispatch
162        println!(
163            "Dispatching task to {} via {:?}: {}",
164            manifest.display_name, manifest.transport, payload
165        );
166
167        // In a real system, we would open a Reqwest client (LocalHttp) or NamedPipe connection here
168        Ok(format!("Task dispatched successfully to {}", extension_id))
169    }
170
171    /// B4: Provisioning Loop
172    /// Downloads a required asset (e.g., ONNX model weights) for an extension
173    /// into the extension's local data directory.
174    pub async fn provision_asset(
175        &self,
176        extension_id: &str,
177        asset_url: &str,
178        filename: &str,
179    ) -> Result<String, String> {
180        let reg_lock = self.registered_extensions.read().unwrap();
181        if !reg_lock.contains_key(extension_id) {
182            return Err("Extension not found".to_string());
183        }
184
185        let target_dir = self.extensions_dir.join(extension_id).join("assets");
186        std::fs::create_dir_all(&target_dir)
187            .map_err(|e| format!("Failed to create asset dir: {}", e))?;
188
189        let target_file = target_dir.join(filename);
190
191        println!(
192            "Provisioning asset from {} into {:?}",
193            asset_url, target_file
194        );
195
196        if let Some(file_path) = asset_url.strip_prefix("file://") {
197            let src = std::path::Path::new(file_path);
198            let bytes = std::fs::read(src)
199                .map_err(|e| format!("Failed to read asset from {}: {}", file_path, e))?;
200            std::fs::write(&target_file, bytes)
201                .map_err(|e| format!("Failed to write asset to {:?}: {}", target_file, e))?;
202        } else if std::path::Path::new(asset_url).exists() {
203            let bytes = std::fs::read(asset_url)
204                .map_err(|e| format!("Failed to read asset from {}: {}", asset_url, e))?;
205            std::fs::write(&target_file, bytes)
206                .map_err(|e| format!("Failed to write asset to {:?}: {}", target_file, e))?;
207        } else if let Some(data_content) = asset_url.strip_prefix("data:") {
208            if let Some((_header, data)) = data_content.split_once(',') {
209                let bytes = data.as_bytes();
210                std::fs::write(&target_file, bytes)
211                    .map_err(|e| format!("Failed to write asset to {:?}: {}", target_file, e))?;
212            } else {
213                return Err("Malformed data URI in asset_url".to_string());
214            }
215        } else {
216            return Err(format!(
217                "Cannot provision asset from remote URL '{}': network transport not configured; provide a local path, file:// URI, or data: URI",
218                asset_url
219            ));
220        }
221
222        Ok(format!(
223            "Asset provisioned successfully at {:?}",
224            target_file
225        ))
226    }
227}
228
229#[cfg(target_arch = "wasm32")]
230pub mod wasm_bus {
231    use serde::Serialize;
232    use std::cell::RefCell;
233    use wasm_bindgen::prelude::*;
234    use wasm_bindgen::JsCast;
235    use web_sys::{ErrorEvent, Event, MessageEvent, WebSocket};
236
237    thread_local! {
238        pub static EXTENSION_BUS: RefCell<Option<ExtensionBusState>> = RefCell::new(None);
239    }
240
241    pub struct ExtensionBusState {
242        pub ws: WebSocket,
243        pub on_open: Closure<dyn FnMut(Event)>,
244        pub on_message: Closure<dyn FnMut(MessageEvent)>,
245        pub on_error: Closure<dyn FnMut(ErrorEvent)>,
246        pub on_close: Closure<dyn FnMut(Event)>,
247        pub is_authenticated: bool,
248        pub active_token_callback: Option<Box<dyn FnMut(String)>>,
249    }
250
251    #[derive(Serialize)]
252    struct ChallengePayload {
253        pub challenge: String,
254        pub did: String,
255    }
256
257    #[derive(Serialize)]
258    struct IntentPayload {
259        pub rpc: String,
260        pub prompt: String,
261        pub graph_context: String,
262        pub signature: String,
263    }
264
265    /// Initializes the extension bus under `did`.
266    pub fn init_extension_bus(did: String) -> Result<(), JsValue> {
267        let ws = WebSocket::new("ws://127.0.0.1:4242")?;
268
269        let ws_clone = ws.clone();
270        let did_clone = did.clone();
271
272        let on_open = Closure::wrap(Box::new(move |_e: Event| {
273            let payload = ChallengePayload {
274                challenge: "did:q42".into(),
275                did: did_clone.clone(),
276            };
277            if let Ok(json) = serde_json::to_string(&payload) {
278                let _ = ws_clone.send_with_str(&json);
279            }
280        }) as Box<dyn FnMut(Event)>);
281        ws.set_onopen(Some(on_open.as_ref().unchecked_ref()));
282
283        let on_message = Closure::wrap(Box::new(move |e: MessageEvent| {
284            if let Ok(txt) = e.data().dyn_into::<js_sys::JsString>() {
285                let s: String = txt.into();
286                // Parse the response
287                if s.contains("\"authenticated\":true") {
288                    EXTENSION_BUS.with(|bus| {
289                        if let Some(state) = bus.borrow_mut().as_mut() {
290                            state.is_authenticated = true;
291                        }
292                    });
293                } else if s.contains("\"token\":") || s.contains("\"text\":") {
294                    if let Ok(v) = serde_json::from_str::<serde_json::Value>(&s) {
295                        if let Some(text) = v.get("text").and_then(|t| t.as_str()) {
296                            EXTENSION_BUS.with(|bus| {
297                                if let Some(state) = bus.borrow_mut().as_mut() {
298                                    if let Some(ref mut cb) = state.active_token_callback {
299                                        cb(text.to_string());
300                                    }
301                                }
302                            });
303                        }
304                    }
305                }
306            }
307        }) as Box<dyn FnMut(MessageEvent)>);
308        ws.set_onmessage(Some(on_message.as_ref().unchecked_ref()));
309
310        let on_error = Closure::wrap(Box::new(move |e: ErrorEvent| {
311            web_sys::console::error_1(&e.message().into());
312        }) as Box<dyn FnMut(ErrorEvent)>);
313        ws.set_onerror(Some(on_error.as_ref().unchecked_ref()));
314
315        let on_close = Closure::wrap(Box::new(move |_e: Event| {
316            EXTENSION_BUS.with(|bus| {
317                *bus.borrow_mut() = None;
318            });
319        }) as Box<dyn FnMut(Event)>);
320        ws.set_onclose(Some(on_close.as_ref().unchecked_ref()));
321
322        EXTENSION_BUS.with(|bus| {
323            *bus.borrow_mut() = Some(ExtensionBusState {
324                ws,
325                on_open,
326                on_message,
327                on_error,
328                on_close,
329                is_authenticated: false,
330                active_token_callback: None,
331            });
332        });
333
334        Ok(())
335    }
336
337    /// `true` when the bus is live.
338    pub fn is_connected() -> bool {
339        EXTENSION_BUS.with(|bus| {
340            bus.borrow()
341                .as_ref()
342                .map(|s| s.is_authenticated)
343                .unwrap_or(false)
344        })
345    }
346
347    /// Sends an intent, invoking `f` with the reply.
348    pub fn send_intent<F: FnMut(String) + 'static>(
349        prompt: &str,
350        graph_context: &str,
351        on_token: F,
352    ) -> Result<(), String> {
353        let payload = IntentPayload {
354            rpc: "infer_local_model".into(),
355            prompt: prompt.to_string(),
356            graph_context: graph_context.to_string(),
357            signature: "did:q42:active".into(),
358        };
359        let intent_json = serde_json::to_string(&payload).unwrap_or_default();
360
361        EXTENSION_BUS.with(|bus| {
362            if let Some(state) = bus.borrow_mut().as_mut() {
363                if state.is_authenticated {
364                    state.active_token_callback = Some(Box::new(on_token));
365                    state
366                        .ws
367                        .send_with_str(&intent_json)
368                        .map_err(|e| format!("{:?}", e))?;
369                    return Ok(());
370                }
371            }
372            Err("Not connected or authenticated".into())
373        })
374    }
375}
376
377#[cfg(test)]
378mod tests {
379    use super::*;
380    use crate::extension_manifest::{ExtensionSecurity, SandboxLevel, TransportProtocol};
381
382    #[tokio::test]
383    async fn provision_asset_with_data_uri() {
384        let temp_dir = tempfile::tempdir().unwrap();
385        let bus = ExtensionBus::new(temp_dir.path());
386
387        // Register dummy manifest directly
388        let manifest = ExtensionManifest {
389            extension_id: "test-ext".to_string(),
390            display_name: "Test Ext".to_string(),
391            version: "1.0.0".to_string(),
392            description: "A test extension".to_string(),
393            capabilities: Vec::new(),
394            transport: TransportProtocol::Stdio,
395            security: ExtensionSecurity {
396                sandbox_level: SandboxLevel::Strict,
397                requires_gpu: false,
398                requires_network_bind: false,
399            },
400        };
401        bus.registered_extensions
402            .write()
403            .unwrap()
404            .insert("test-ext".to_string(), manifest);
405
406        let data_url = "data:application/octet-stream,GENUINE_BINARY_PAYLOAD";
407        let res = bus
408            .provision_asset("test-ext", data_url, "weights.bin")
409            .await;
410        assert!(res.is_ok());
411
412        let target = temp_dir.path().join("test-ext").join("assets").join("weights.bin");
413        assert!(target.exists());
414        let content = std::fs::read(&target).unwrap();
415        assert_eq!(content, b"GENUINE_BINARY_PAYLOAD");
416    }
417
418    #[tokio::test]
419    async fn provision_asset_fails_closed_on_unsupported_remote_url() {
420        let temp_dir = tempfile::tempdir().unwrap();
421        let bus = ExtensionBus::new(temp_dir.path());
422
423        let manifest = ExtensionManifest {
424            extension_id: "remote-ext".to_string(),
425            display_name: "Remote Ext".to_string(),
426            version: "1.0.0".to_string(),
427            description: "A test extension".to_string(),
428            capabilities: Vec::new(),
429            transport: TransportProtocol::Stdio,
430            security: ExtensionSecurity {
431                sandbox_level: SandboxLevel::Strict,
432                requires_gpu: false,
433                requires_network_bind: false,
434            },
435        };
436        bus.registered_extensions
437            .write()
438            .unwrap()
439            .insert("remote-ext".to_string(), manifest);
440
441        let res = bus
442            .provision_asset("remote-ext", "https://example.com/model.onnx", "model.onnx")
443            .await;
444        assert!(res.is_err());
445        assert!(res.unwrap_err().contains("network transport not configured"));
446    }
447}