Skip to main content

clm_rust_core/layer2/
db.rs

1use crate::layer0::vocabulary::{HANDLE_FILE_MEMDB};
2use std::fs;
3use std::io::{Read, Write};
4use std::net::{TcpStream, Shutdown};
5use std::path::{Path, PathBuf};
6use std::time::Duration;
7use rusqlite::{params, Connection};
8use std::cell::RefCell;
9use std::collections::HashMap;
10use crate::error::{ClmError, StorageErrorCode, Result};
11
12thread_local! {
13    pub static TEST_SANDBOX_ACTIVE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
14    pub static TEST_SANDBOX_DB_ID: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
15    pub static TEST_SANDBOX_DEPTH: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
16    static IN_MEMORY_CONNS: RefCell<HashMap<String, Connection>> = RefCell::new(HashMap::new());
17}
18
19pub fn clear_in_memory_connections() {
20    IN_MEMORY_CONNS.with(|conns| conns.borrow_mut().clear());
21}
22
23/// Canonical MCard schema (v3.0.3) embedded at compile-time as single source of truth (SSOT).
24pub const CANONICAL_SCHEMA: &str = include_str!("../../types/schemas/mcard_schema.sql");
25
26#[cfg(any(feature = "native", feature = "db"))]
27use std::sync::{Arc, Mutex, OnceLock};
28#[cfg(any(feature = "native", feature = "db"))]
29use r2d2::Pool;
30#[cfg(any(feature = "native", feature = "db"))]
31use r2d2_sqlite::SqliteConnectionManager;
32
33#[cfg(any(feature = "native", feature = "db"))]
34#[path = "db/migrations.rs"]
35pub mod migrations;
36
37#[cfg(any(feature = "native", feature = "db"))]
38#[path = "db/compaction.rs"]
39pub mod compaction;
40
41/// High-Concurrency Dual-Pool SQLite Manager.
42///
43/// Provides a dedicated single writer connection alongside an `r2d2` multi-reader
44/// connection pool configured with WAL mode, normal synchronous mode, and busy timeouts.
45#[cfg(any(feature = "native", feature = "db"))]
46#[derive(Clone)]
47pub struct DbPillarPool {
48    writer: Arc<Mutex<Connection>>,
49    reader_pool: Pool<SqliteConnectionManager>,
50    db_path: PathBuf,
51}
52
53#[cfg(any(feature = "native", feature = "db"))]
54impl DbPillarPool {
55    /// Initializes a dual-pool manager for the given pillar database path.
56    pub fn new(pillar: &str, clm_root: &str) -> Result<Self> {
57        let normalized = get_pillar_filename(pillar);
58        let db_path = get_db_path(&normalized, clm_root);
59        let db_path_str = db_path.to_string_lossy();
60
61        if !db_path_str.starts_with(crate::layer0::vocabulary::URI_SCHEME_FILE)
62            && let Some(parent) = db_path.parent() {
63                let _ = fs::create_dir_all(parent);
64            }
65
66        #[cfg(unix)]
67        if !db_path_str.starts_with(crate::layer0::vocabulary::URI_SCHEME_FILE) {
68            use std::os::unix::fs::PermissionsExt;
69            if let Ok(meta) = fs::metadata(&db_path) {
70                let current_mode = meta.permissions().mode();
71                if current_mode & 0o200 == 0 {
72                    let mut perms = meta.permissions();
73                    perms.set_mode(current_mode | 0o664);
74                    let _ = fs::set_permissions(&db_path, perms);
75                }
76            }
77            for ext in &["-shm", "-wal"] {
78                let aux_path = format!("{}{}", db_path.display(), ext);
79                if let Ok(meta) = fs::metadata(&aux_path) {
80                    let current_mode = meta.permissions().mode();
81                    if current_mode & 0o200 == 0 {
82                        let mut perms = meta.permissions();
83                        perms.set_mode(current_mode | 0o664);
84                        let _ = fs::set_permissions(&aux_path, perms);
85                    }
86                }
87            }
88        }
89
90        // 1. Initialize Writer Connection with WAL pragma & run migrations
91        let mut writer_conn = Connection::open_with_flags(
92            &db_path,
93            rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
94                | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
95                | rusqlite::OpenFlags::SQLITE_OPEN_URI,
96        )?;
97        writer_conn.busy_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_BUSY_TIMEOUT_MS)))?;
98        let _ = writer_conn.pragma_update(None, "journal_mode", "WAL");
99        let _ = writer_conn.pragma_update(None, "synchronous", "NORMAL");
100        let _ = writer_conn.pragma_update(None, "foreign_keys", "ON");
101
102        // Apply versioned schema migrations on the writer connection
103        let _ = migrations::apply_migrations(&mut writer_conn, &normalized);
104
105        crate::layer2::record_sqlite_guard::install(&writer_conn)?;
106
107        // 2. Initialize Reader Pool
108        let manager = SqliteConnectionManager::file(&db_path)
109            .with_flags(
110                rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
111                    | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
112                    | rusqlite::OpenFlags::SQLITE_OPEN_URI,
113            )
114            .with_init(|conn| {
115                let _ = conn.busy_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_BUSY_TIMEOUT_MS)));
116                let _ = conn.pragma_update(None, "journal_mode", "WAL");
117                let _ = conn.pragma_update(None, "synchronous", "NORMAL");
118
119                crate::layer2::record_sqlite_guard::install(conn)?;
120                conn.pragma_update(None, "query_only", true)
121            });
122
123        let reader_pool = Pool::builder()
124            .max_size(16)
125            .min_idle(Some(2))
126            .connection_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_BUSY_TIMEOUT_MS)))
127            .build(manager)
128            .map_err(|e| {
129                ClmError::storage(
130                    StorageErrorCode::DatabaseUnavailable,
131                    format!("Failed to build SQLite reader connection pool for {}: {}", normalized, e),
132                )
133            })?;
134
135        Ok(Self {
136            writer: Arc::new(Mutex::new(writer_conn)),
137            reader_pool,
138            db_path,
139        })
140    }
141
142    /// Returns an `Arc<Mutex<Connection>>` handle to the dedicated writer connection.
143    pub fn writer(&self) -> Arc<Mutex<Connection>> {
144        self.writer.clone()
145    }
146
147    /// Acquires a connection from the multi-reader pool without locking the writer.
148    pub fn reader(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
149        self.reader_pool.get().map_err(|e| {
150            ClmError::storage(
151                StorageErrorCode::DatabaseUnavailable,
152                format!("Failed to acquire connection from reader pool: {}", e),
153            )
154        })
155    }
156
157    /// Returns the underlying database path or URI.
158    pub fn db_path(&self) -> &Path {
159        &self.db_path
160    }
161}
162
163#[cfg(any(feature = "native", feature = "db"))]
164static SHARED_POOLS: OnceLock<dashmap::DashMap<String, Arc<DbPillarPool>>> = OnceLock::new();
165
166#[cfg(any(feature = "native", feature = "db"))]
167static SHARED_CONNECTIONS: OnceLock<dashmap::DashMap<String, Arc<Mutex<Connection>>>> = OnceLock::new();
168
169#[cfg(any(feature = "native", feature = "db"))]
170pub fn get_pillar_pool(pillar: &str, clm_root: &str) -> Result<Arc<DbPillarPool>> {
171    let normalized = get_pillar_filename(pillar);
172    let db_path = get_db_path(&normalized, clm_root);
173    let key = format!("{}:{}", normalized, db_path.to_string_lossy());
174    let map = SHARED_POOLS.get_or_init(dashmap::DashMap::new);
175    if let Some(entry) = map.get(&key) {
176        return Ok(entry.clone());
177    }
178    let pool = Arc::new(DbPillarPool::new(&normalized, clm_root)?);
179    map.insert(key, pool.clone());
180    Ok(pool)
181}
182
183#[cfg(any(feature = "native", feature = "db"))]
184pub fn get_writer_connection(pillar: &str, clm_root: &str) -> Result<Arc<Mutex<Connection>>> {
185    let pool = get_pillar_pool(pillar, clm_root)?;
186    Ok(pool.writer())
187}
188
189#[cfg(any(feature = "native", feature = "db"))]
190pub fn get_reader_connection(pillar: &str, clm_root: &str) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
191    let pool = get_pillar_pool(pillar, clm_root)?;
192    pool.reader()
193}
194
195#[cfg(any(feature = "native", feature = "db"))]
196pub fn get_shared_connection(pillar: &str, clm_root: &str) -> std::result::Result<Arc<Mutex<Connection>>, String> {
197    get_writer_connection(pillar, clm_root).map_err(|e| e.to_string())
198}
199
200#[cfg(any(feature = "native", feature = "db"))]
201pub fn clear_shared_connections() {
202    if let Some(map) = SHARED_CONNECTIONS.get() {
203        map.clear();
204    }
205    if let Some(pools) = SHARED_POOLS.get() {
206        pools.clear();
207    }
208}
209
210pub fn set_sandbox_depth(depth: usize) {
211    TEST_SANDBOX_DEPTH.with(|d| d.set(depth));
212}
213
214pub fn thread_local_depth_get() -> usize {
215    TEST_SANDBOX_DEPTH.with(|d| d.get())
216}
217
218pub fn thread_local_depth_increment() -> usize {
219    TEST_SANDBOX_DEPTH.with(|d| {
220        let c = d.get() + 1;
221        d.set(c);
222        c
223    })
224}
225
226pub fn thread_local_depth_decrement() {
227    TEST_SANDBOX_DEPTH.with(|d| {
228        let c = d.get();
229        if c > 0 {
230            d.set(c - 1);
231        }
232    })
233}
234
235pub struct DatabaseSandboxGuard {
236    prev_active: bool,
237    prev_db_id: Option<String>,
238}
239
240impl DatabaseSandboxGuard {
241    pub fn activate(db_id: &str) -> Self {
242        let prev_active = TEST_SANDBOX_ACTIVE.with(|active| active.get());
243        let prev_db_id = TEST_SANDBOX_DB_ID.with(|id| id.borrow().clone());
244        
245        TEST_SANDBOX_ACTIVE.with(|active| active.set(true));
246        TEST_SANDBOX_DB_ID.with(|id| {
247            *id.borrow_mut() = Some(db_id.to_string());
248        });
249        
250        DatabaseSandboxGuard {
251            prev_active,
252            prev_db_id,
253        }
254    }
255}
256
257impl Drop for DatabaseSandboxGuard {
258    fn drop(&mut self) {
259        TEST_SANDBOX_ACTIVE.with(|active| active.set(self.prev_active));
260        TEST_SANDBOX_DB_ID.with(|id| {
261            *id.borrow_mut() = self.prev_db_id.clone();
262        });
263        clear_in_memory_connections();
264    }
265}
266
267
268pub fn get_db_path(pillar: &str, clm_root: &str) -> PathBuf {
269    if crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_MEM_TRIDB).unwrap_or_default() == "1" {
270        return PathBuf::from(format!("{HANDLE_FILE_MEMDB}{}?mode=memory&cache=shared", get_pillar_filename(pillar)));
271    }
272    let is_sandbox = TEST_SANDBOX_ACTIVE.with(|active| active.get());
273    if is_sandbox {
274        let db_id = TEST_SANDBOX_DB_ID.with(|id| {
275            id.borrow().clone().unwrap_or_else(|| {
276                format!("{:?}", std::thread::current().id())
277            })
278        });
279        let depth = TEST_SANDBOX_DEPTH.with(|d| d.get());
280        let depth_suffix = if depth > 0 { format!("_depth_{}", depth) } else { "".to_string() };
281        PathBuf::from(format!("{HANDLE_FILE_MEMDB}{}_{}{}?mode=memory&cache=shared", get_pillar_filename(pillar), db_id, depth_suffix))
282    } else {
283        let zero_fs = crate::utils::is_zero_fs();
284        if zero_fs {
285            let snap_dir = crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_MEMBROKER_SNAP_DIR)
286                .unwrap_or_else(|_| "/tmp".to_string());
287            let snap_path = if cfg!(windows) && (snap_dir == "/tmp" || snap_dir == "\\tmp") {
288                std::env::temp_dir()
289            } else {
290                PathBuf::from(&snap_dir)
291            };
292            snap_path.join(format!("{}.db", get_pillar_filename(pillar)))
293        } else {
294            let fname = get_pillar_filename(pillar);
295            let db_dir = Path::new(clm_root).join("db");
296            let target_path = db_dir.join(format!("{}.db", fname));
297            // Backward-compatibility: if mcard_state requested but content_memory.db exists, fall back
298            if fname == "mcard_state" && !target_path.exists() && db_dir.join("content_memory.db").exists() {
299                db_dir.join("content_memory.db")
300            } else if fname == "sovereign_identity" && !target_path.exists() && db_dir.join("agent_identities.db").exists() {
301                db_dir.join("agent_identities.db")
302            } else {
303                target_path
304            }
305        }
306    }
307}
308
309fn get_pillar_filename(pillar: &str) -> String {
310    match pillar {
311        "mcard_state" | "mcard_state.db" => "mcard_state".to_string(),
312        "sovereign_identity" | "sovereign_identity.db" => "sovereign_identity".to_string(),
313        "agent_identity" | crate::layer0::vocabulary_env::PILLAR_AGENT_IDENTITIES => crate::layer0::vocabulary_env::PILLAR_AGENT_IDENTITIES.to_string(),
314        crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY | "content" | "memory" => crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY.to_string(),
315        crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG | "execution" | "log" => crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG.to_string(),
316        "evolution" | "evol" => {
317            let now = chrono::Local::now();
318            let iso_week = chrono::Datelike::iso_week(&now);
319            format!("pkc_evol_{:04}_w{:02}", iso_week.year(), iso_week.week())
320        }
321        _ => crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY.to_string(),
322    }
323}
324
325/// Check if the membroker backend is active (CLM_DB_BACKEND=membroker and CLM_ZERO_FS=true).
326pub fn is_membroker_backend() -> bool {
327    crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_DB_BACKEND).unwrap_or_default() == "membroker"
328        && crate::utils::is_zero_fs()
329}
330
331/// Send a SQL statement to the Python membroker via TCP (framed CLMQ protocol).
332/// Returns the response body on success.
333pub fn membroker_send(pillar: &str, sql: &str) -> Result<String> {
334    let port: u16 = crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_MEMBROKER_PORT)
335        .unwrap_or_else(|_| "43740".to_string())
336        .parse()
337        .unwrap_or(43740);
338
339    let addr = format!("127.0.0.1:{}", port);
340    let timeout = Duration::from_secs(5);
341    let socket_addr: std::net::SocketAddr = addr
342        .parse()
343        .map_err(|e| ClmError::Evaluation(format!("Invalid membroker address: {}", e)))?;
344
345    let mut stream = TcpStream::connect_timeout(&socket_addr, timeout)
346        .map_err(|e| ClmError::Evaluation(format!("membroker connect failed: {}", e)))?;
347    stream.set_read_timeout(Some(timeout))?;
348    stream.set_write_timeout(Some(timeout))?;
349
350    let body = sql.as_bytes();
351    let header = format!("CLMQ\t{}\t{}\t\n", pillar, body.len());
352    stream.write_all(header.as_bytes())?;
353    stream.write_all(body)?;
354    stream.shutdown(Shutdown::Write)?;
355
356    let mut response = Vec::new();
357    stream.read_to_end(&mut response)?;
358    let resp_str = String::from_utf8_lossy(&response);
359
360    if resp_str.starts_with("CLMR\tOK\t") {
361        if let Some(pos) = resp_str.find('\n') {
362            Ok(resp_str[pos + 1..].to_string())
363        } else {
364            Ok(String::new())
365        }
366    } else {
367        Err(ClmError::Evaluation(format!("membroker error: {}", resp_str)))
368    }
369}
370
371/// Sync a single INSERT/REPLACE statement to the membroker if the backend is active.
372/// Silently skips if membroker is not available or not in use.
373pub fn membroker_sync(pillar: &str, sql: &str) {
374    if !is_membroker_backend() {
375        return;
376    }
377    let _ = membroker_send(pillar, sql);
378}
379
380
381#[path = "db_connection.rs"]
382mod db_connection;
383#[allow(unused_imports)]
384pub use db_connection::*;
385#[path = "db_handles.rs"]
386mod db_handles;
387#[allow(unused_imports)]
388pub use db_handles::*;
389#[cfg(test)]
390#[path = "db_tests.rs"]
391mod tests;
392
393
394
395