aingle_cortex 0.6.3

Córtex API - REST/GraphQL/SPARQL interface for AIngle semantic graphs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
// Copyright 2019-2026 Apilium Technologies OÜ. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR Commercial

//! The shared application state for the Córtex API server.

use aingle_graph::GraphDB;
use aingle_logic::RuleEngine;
use std::path::Path;
use std::sync::Arc;
use ineru::IneruMemory;
use tokio::sync::RwLock;

#[cfg(feature = "auth")]
use crate::auth::UserStore;
use crate::proofs::ProofStore;
use crate::rest::audit::AuditLog;

/// The shared state accessible by all API handlers.
///
/// This struct uses `Arc` and `RwLock` to provide safe, concurrent access
/// to the application's core components like the database and logic engine.
#[derive(Clone)]
pub struct AppState {
    /// A thread-safe reference to the graph database.
    pub graph: Arc<RwLock<GraphDB>>,
    /// A thread-safe reference to the logic and validation engine.
    pub logic: Arc<RwLock<RuleEngine>>,
    /// The Ineru dual-memory system (STM + LTM with consolidation).
    pub memory: Arc<RwLock<IneruMemory>>,
    /// The event broadcaster for sending real-time updates to WebSocket subscribers.
    pub broadcaster: Arc<EventBroadcaster>,
    /// The store for managing and verifying zero-knowledge proofs.
    pub proof_store: Arc<ProofStore>,
    /// Manager for temporary sandbox namespaces used by skill verification.
    pub sandbox_manager: Arc<SandboxManager>,
    /// Audit log for tracking API actions.
    pub audit_log: Arc<RwLock<AuditLog>>,
    /// The user store for authentication and authorization.
    ///
    /// This field is only available if the `auth` feature is enabled.
    #[cfg(feature = "auth")]
    pub user_store: Arc<UserStore>,
    /// P2P manager for multi-node triple synchronization.
    #[cfg(feature = "p2p")]
    pub p2p: Option<Arc<crate::p2p::manager::P2pManager>>,
    /// Write-Ahead Log for clustering.
    #[cfg(feature = "cluster")]
    pub wal: Option<Arc<aingle_wal::WalWriter>>,
    /// Raft consensus instance for cluster coordination.
    #[cfg(feature = "cluster")]
    pub raft: Option<openraft::Raft<aingle_raft::CortexTypeConfig, std::sync::Arc<aingle_raft::state_machine::CortexStateMachine>>>,
    /// This node's ID in the Raft cluster.
    #[cfg(feature = "cluster")]
    pub cluster_node_id: Option<u64>,
    /// Shared secret for authenticating internal cluster RPCs.
    #[cfg(feature = "cluster")]
    pub cluster_secret: Option<String>,
    /// TLS server config for encrypting inter-node communication.
    #[cfg(feature = "cluster")]
    pub tls_server_config: Option<Arc<rustls::ServerConfig>>,
    /// This node's author identity for DAG actions.
    #[cfg(feature = "dag")]
    pub dag_author: Option<aingle_graph::NodeId>,
    /// Per-author monotonic sequence counter for DAG actions.
    #[cfg(feature = "dag")]
    pub dag_seq_counter: std::sync::Arc<std::sync::atomic::AtomicU64>,
    /// Ed25519 signing key for DAG actions (mandatory in production).
    #[cfg(feature = "dag")]
    pub dag_signing_key: Option<std::sync::Arc<aingle_graph::dag::DagSigningKey>>,
}

impl AppState {
    /// Creates a new `AppState` with an in-memory graph database.
    /// This is useful for testing or development environments.
    pub fn new() -> crate::error::Result<Self> {
        let graph = GraphDB::memory()?;
        let logic = RuleEngine::new();
        let memory = IneruMemory::agent_mode();

        #[cfg(feature = "auth")]
        let user_store = {
            let store = Arc::new(UserStore::new());
            // Initialize a default admin user for convenience.
            let _ = store.init_default_admin();
            store
        };

        Ok(Self {
            graph: Arc::new(RwLock::new(graph)),
            logic: Arc::new(RwLock::new(logic)),
            memory: Arc::new(RwLock::new(memory)),
            broadcaster: Arc::new(EventBroadcaster::new()),
            proof_store: Arc::new(ProofStore::new()),
            sandbox_manager: Arc::new(SandboxManager::new()),
            audit_log: Arc::new(RwLock::new(AuditLog::default())),
            #[cfg(feature = "auth")]
            user_store,
            #[cfg(feature = "p2p")]
            p2p: None,
            #[cfg(feature = "cluster")]
            wal: None,
            #[cfg(feature = "cluster")]
            raft: None,
            #[cfg(feature = "cluster")]
            cluster_node_id: None,
            #[cfg(feature = "cluster")]
            cluster_secret: None,
            #[cfg(feature = "cluster")]
            tls_server_config: None,
            #[cfg(feature = "dag")]
            dag_author: None,
            #[cfg(feature = "dag")]
            dag_seq_counter: std::sync::Arc::new(std::sync::atomic::AtomicU64::new(1)),
            #[cfg(feature = "dag")]
            dag_signing_key: None,
        })
    }

    /// Creates a new `AppState` with a pre-configured `GraphDB` instance.
    pub fn with_graph(graph: GraphDB) -> Self {
        let logic = RuleEngine::new();
        let memory = IneruMemory::agent_mode();

        #[cfg(feature = "auth")]
        let user_store = {
            let store = Arc::new(UserStore::new());
            // Initialize a default admin user.
            let _ = store.init_default_admin();
            store
        };

        Self {
            graph: Arc::new(RwLock::new(graph)),
            logic: Arc::new(RwLock::new(logic)),
            memory: Arc::new(RwLock::new(memory)),
            broadcaster: Arc::new(EventBroadcaster::new()),
            proof_store: Arc::new(ProofStore::new()),
            sandbox_manager: Arc::new(SandboxManager::new()),
            audit_log: Arc::new(RwLock::new(AuditLog::default())),
            #[cfg(feature = "auth")]
            user_store,
            #[cfg(feature = "p2p")]
            p2p: None,
            #[cfg(feature = "cluster")]
            wal: None,
            #[cfg(feature = "cluster")]
            raft: None,
            #[cfg(feature = "cluster")]
            cluster_node_id: None,
            #[cfg(feature = "cluster")]
            cluster_secret: None,
            #[cfg(feature = "cluster")]
            tls_server_config: None,
            #[cfg(feature = "dag")]
            dag_author: None,
            #[cfg(feature = "dag")]
            dag_seq_counter: std::sync::Arc::new(std::sync::atomic::AtomicU64::new(1)),
            #[cfg(feature = "dag")]
            dag_signing_key: None,
        }
    }

    /// Creates a new `AppState` with a file-backed audit log.
    pub fn with_audit_path(path: std::path::PathBuf) -> crate::error::Result<Self> {
        let graph = GraphDB::memory()?;
        let logic = RuleEngine::new();
        let memory = IneruMemory::agent_mode();

        #[cfg(feature = "auth")]
        let user_store = {
            let store = Arc::new(UserStore::new());
            let _ = store.init_default_admin();
            store
        };

        Ok(Self {
            graph: Arc::new(RwLock::new(graph)),
            logic: Arc::new(RwLock::new(logic)),
            memory: Arc::new(RwLock::new(memory)),
            broadcaster: Arc::new(EventBroadcaster::new()),
            proof_store: Arc::new(ProofStore::new()),
            sandbox_manager: Arc::new(SandboxManager::new()),
            audit_log: Arc::new(RwLock::new(AuditLog::with_path(10_000, path))),
            #[cfg(feature = "auth")]
            user_store,
            #[cfg(feature = "p2p")]
            p2p: None,
            #[cfg(feature = "cluster")]
            wal: None,
            #[cfg(feature = "cluster")]
            raft: None,
            #[cfg(feature = "cluster")]
            cluster_node_id: None,
            #[cfg(feature = "cluster")]
            cluster_secret: None,
            #[cfg(feature = "cluster")]
            tls_server_config: None,
            #[cfg(feature = "dag")]
            dag_author: None,
            #[cfg(feature = "dag")]
            dag_seq_counter: std::sync::Arc::new(std::sync::atomic::AtomicU64::new(1)),
            #[cfg(feature = "dag")]
            dag_signing_key: None,
        })
    }

    /// Creates a new `AppState` with a configurable database path and optional audit log.
    ///
    /// - `":memory:"` — volatile in-memory storage.
    /// - Any other path — Sled-backed persistent storage.
    pub fn with_db_path(
        db_path: &str,
        audit_log_path: Option<std::path::PathBuf>,
    ) -> crate::error::Result<Self> {
        let graph = if db_path == ":memory:" {
            GraphDB::memory()?
        } else {
            // Ensure the parent directory exists
            if let Some(parent) = Path::new(db_path).parent() {
                std::fs::create_dir_all(parent).ok();
            }
            GraphDB::sled(db_path)?
        };

        let logic = RuleEngine::new();

        // Load Ineru snapshot if available next to the graph database
        let memory = if db_path != ":memory:" {
            let snapshot_path = Path::new(db_path)
                .parent()
                .unwrap_or(Path::new("."))
                .join("ineru.snapshot");
            if snapshot_path.exists() {
                match IneruMemory::load_from_file(&snapshot_path) {
                    Ok(mem) => {
                        log::info!("Loaded Ineru snapshot from {}", snapshot_path.display());
                        mem
                    }
                    Err(e) => {
                        log::warn!("Failed to load Ineru snapshot: {}. Starting fresh.", e);
                        IneruMemory::agent_mode()
                    }
                }
            } else {
                IneruMemory::agent_mode()
            }
        } else {
            IneruMemory::agent_mode()
        };

        let audit_log = if let Some(path) = audit_log_path {
            AuditLog::with_path(10_000, path)
        } else {
            AuditLog::default()
        };

        // Create ProofStore — persistent if using Sled, in-memory otherwise.
        // Uses a separate sled DB path (sibling to graph) to avoid lock contention.
        let proof_store = if db_path != ":memory:" {
            let proof_db_path = Path::new(db_path)
                .parent()
                .unwrap_or(Path::new("."))
                .join("proofs.sled");
            let proof_db_str = proof_db_path.to_string_lossy();
            match ProofStore::with_sled(&proof_db_str) {
                Ok(ps) => {
                    log::info!("ProofStore using Sled backend at {}", proof_db_str);
                    Arc::new(ps)
                }
                Err(e) => {
                    log::warn!("Failed to open Sled ProofStore: {}. Falling back to in-memory.", e);
                    Arc::new(ProofStore::new())
                }
            }
        } else {
            Arc::new(ProofStore::new())
        };

        #[cfg(feature = "auth")]
        let user_store = {
            let store = Arc::new(UserStore::new());
            let _ = store.init_default_admin();
            store
        };

        Ok(Self {
            graph: Arc::new(RwLock::new(graph)),
            logic: Arc::new(RwLock::new(logic)),
            memory: Arc::new(RwLock::new(memory)),
            broadcaster: Arc::new(EventBroadcaster::new()),
            proof_store,
            sandbox_manager: Arc::new(SandboxManager::new()),
            audit_log: Arc::new(RwLock::new(audit_log)),
            #[cfg(feature = "auth")]
            user_store,
            #[cfg(feature = "p2p")]
            p2p: None,
            #[cfg(feature = "cluster")]
            wal: None,
            #[cfg(feature = "cluster")]
            raft: None,
            #[cfg(feature = "cluster")]
            cluster_node_id: None,
            #[cfg(feature = "cluster")]
            cluster_secret: None,
            #[cfg(feature = "cluster")]
            tls_server_config: None,
            #[cfg(feature = "dag")]
            dag_author: None,
            #[cfg(feature = "dag")]
            dag_seq_counter: std::sync::Arc::new(std::sync::atomic::AtomicU64::new(1)),
            #[cfg(feature = "dag")]
            dag_signing_key: None,
        })
    }


    /// Flushes the graph database and saves the Ineru memory snapshot to disk.
    ///
    /// This should be called before shutdown or binary updates to ensure
    /// no data is lost.
    pub async fn flush(&self, snapshot_dir: Option<&Path>) -> crate::error::Result<()> {
        // Flush graph database
        {
            let graph = self.graph.read().await;
            graph.flush()?;
        }

        // Flush proof store
        if let Err(e) = self.proof_store.flush() {
            log::warn!("Failed to flush proof store: {}", e);
        }

        // Save Ineru memory snapshot
        if let Some(dir) = snapshot_dir {
            let snapshot_path = dir.join("ineru.snapshot");
            let memory = self.memory.read().await;
            if let Err(e) = memory.save_to_file(&snapshot_path) {
                log::warn!("Failed to save Ineru snapshot: {}", e);
            } else {
                log::info!("Ineru snapshot saved to {}", snapshot_path.display());
            }
        }

        Ok(())
    }

    /// Returns an internal Cortex client configured for same-process access.
    ///
    /// This client calls the Cortex REST API and can be used by host functions
    /// to bridge WASM zome code with the semantic graph.
    pub fn cortex_client(&self) -> crate::client::CortexInternalClient {
        crate::client::CortexInternalClient::default_client()
    }

    /// Gathers and returns statistics about the graph and connected clients.
    pub async fn stats(&self) -> GraphStats {
        let graph = self.graph.read().await;
        let stats = graph.stats();
        GraphStats {
            triple_count: stats.triple_count,
            subject_count: stats.subject_count,
            predicate_count: stats.predicate_count,
            object_count: stats.object_count,
            connected_clients: self.broadcaster.client_count(),
        }
    }
}

impl Default for AppState {
    fn default() -> Self {
        Self::new().expect("Failed to create default AppState with in-memory graph")
    }
}

/// A serializable struct containing statistics about the graph database.
#[derive(Debug, Clone, serde::Serialize)]
pub struct GraphStats {
    /// The total number of triples in the graph.
    pub triple_count: usize,
    /// The number of unique subjects.
    pub subject_count: usize,
    /// The number of unique predicates.
    pub predicate_count: usize,
    /// The number of unique objects.
    pub object_count: usize,
    /// The number of currently connected WebSocket clients.
    pub connected_clients: usize,
}

/// A broadcaster for sending real-time `Event`s to WebSocket subscribers.
pub struct EventBroadcaster {
    /// The underlying `tokio::sync::broadcast` sender.
    sender: tokio::sync::broadcast::Sender<Event>,
    /// An atomic counter for the number of connected clients.
    client_count: std::sync::atomic::AtomicUsize,
}

impl EventBroadcaster {
    /// Creates a new `EventBroadcaster`.
    pub fn new() -> Self {
        let (sender, _) = tokio::sync::broadcast::channel(1024);
        Self {
            sender,
            client_count: std::sync::atomic::AtomicUsize::new(0),
        }
    }

    /// Subscribes to the broadcast channel to receive events.
    /// This also increments the client count.
    pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<Event> {
        self.client_count
            .fetch_add(1, std::sync::atomic::Ordering::SeqCst);
        self.sender.subscribe()
    }

    /// Decrements the client count when a client unsubscribes.
    pub fn unsubscribe(&self) {
        // Use fetch_update to prevent underflow wrapping to usize::MAX.
        let _ = self.client_count.fetch_update(
            std::sync::atomic::Ordering::SeqCst,
            std::sync::atomic::Ordering::SeqCst,
            |current| current.checked_sub(1),
        );
    }

    /// Broadcasts an `Event` to all active subscribers.
    pub fn broadcast(&self, event: Event) {
        let _ = self.sender.send(event);
    }

    /// Returns the number of currently connected clients.
    pub fn client_count(&self) -> usize {
        self.client_count.load(std::sync::atomic::Ordering::SeqCst)
    }
}

impl Default for EventBroadcaster {
    fn default() -> Self {
        Self::new()
    }
}

/// Defines the types of real-time events sent to WebSocket clients.
#[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "type", content = "data")]
pub enum Event {
    /// Sent when a new triple is added to the graph.
    TripleAdded {
        hash: String,
        subject: String,
        predicate: String,
        object: serde_json::Value,
    },
    /// Sent when a triple is deleted from the graph.
    TripleDeleted { hash: String },
    /// Sent after a validation operation is completed.
    ValidationCompleted {
        hash: String,
        valid: bool,
        proof_hash: Option<String>,
    },
    /// Sent to a client immediately after it connects.
    Connected { client_id: String },
    /// A heartbeat message to keep the connection alive.
    Ping,
}

impl Event {
    /// Serializes the event to a JSON string.
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).unwrap_or_default()
    }
}

// ---------------------------------------------------------------------------
// Sandbox Manager
// ---------------------------------------------------------------------------

/// Entry for a sandbox namespace with TTL.
struct SandboxEntry {
    namespace: String,
    created_at: std::time::Instant,
    ttl: std::time::Duration,
}

/// Manager for temporary sandbox namespaces used by skill verification.
///
/// Sandboxes are isolated graph namespaces with a time-to-live (TTL).
/// After TTL expiration, the sandbox should be cleaned up.
pub struct SandboxManager {
    entries: RwLock<std::collections::HashMap<String, SandboxEntry>>,
}

impl SandboxManager {
    /// Creates a new, empty `SandboxManager`.
    pub fn new() -> Self {
        Self {
            entries: RwLock::new(std::collections::HashMap::new()),
        }
    }

    /// Creates a new sandbox entry with the given ID, namespace, and TTL.
    pub async fn create(&self, id: String, namespace: String, ttl_seconds: u64) {
        let entry = SandboxEntry {
            namespace,
            created_at: std::time::Instant::now(),
            ttl: std::time::Duration::from_secs(ttl_seconds),
        };
        let mut entries = self.entries.write().await;
        entries.insert(id, entry);
    }

    /// Removes a sandbox by ID, returning the namespace if found.
    pub async fn remove(&self, id: &str) -> Option<String> {
        let mut entries = self.entries.write().await;
        entries.remove(id).map(|e| e.namespace)
    }

    /// Returns the namespace for a sandbox if it exists and hasn't expired.
    pub async fn get(&self, id: &str) -> Option<String> {
        let entries = self.entries.read().await;
        entries.get(id).and_then(|e| {
            if e.created_at.elapsed() < e.ttl {
                Some(e.namespace.clone())
            } else {
                None
            }
        })
    }

    /// Returns a list of all expired sandbox IDs for cleanup.
    pub async fn expired(&self) -> Vec<String> {
        let entries = self.entries.read().await;
        entries
            .iter()
            .filter(|(_, e)| e.created_at.elapsed() >= e.ttl)
            .map(|(id, _)| id.clone())
            .collect()
    }
}

impl Default for SandboxManager {
    fn default() -> Self {
        Self::new()
    }
}