clm-kernel 0.0.23

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
use crate::layer0::vocabulary::{HANDLE_FILE_MEMDB};
use std::fs;
use std::io::{Read, Write};
use std::net::{TcpStream, Shutdown};
use std::path::{Path, PathBuf};
use std::time::Duration;
use rusqlite::{params, Connection};
use std::cell::RefCell;
use std::collections::HashMap;
use crate::error::{ClmError, StorageErrorCode, Result};

thread_local! {
    pub static TEST_SANDBOX_ACTIVE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
    pub static TEST_SANDBOX_DB_ID: std::cell::RefCell<Option<String>> = const { std::cell::RefCell::new(None) };
    pub static TEST_SANDBOX_DEPTH: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
    static IN_MEMORY_CONNS: RefCell<HashMap<String, Connection>> = RefCell::new(HashMap::new());
}

pub fn clear_in_memory_connections() {
    IN_MEMORY_CONNS.with(|conns| conns.borrow_mut().clear());
}

/// Canonical MCard schema (v3.0.3) embedded at compile-time as single source of truth (SSOT).
pub const CANONICAL_SCHEMA: &str = include_str!("../../types/schemas/mcard_schema.sql");

#[cfg(any(feature = "native", feature = "db"))]
use std::sync::{Arc, Mutex, OnceLock};
#[cfg(any(feature = "native", feature = "db"))]
use r2d2::Pool;
#[cfg(any(feature = "native", feature = "db"))]
use r2d2_sqlite::SqliteConnectionManager;

#[cfg(any(feature = "native", feature = "db"))]
#[path = "db/migrations.rs"]
pub mod migrations;

#[cfg(any(feature = "native", feature = "db"))]
#[path = "db/compaction.rs"]
pub mod compaction;

/// High-Concurrency Dual-Pool SQLite Manager.
///
/// Provides a dedicated single writer connection alongside an `r2d2` multi-reader
/// connection pool configured with WAL mode, normal synchronous mode, and busy timeouts.
#[cfg(any(feature = "native", feature = "db"))]
#[derive(Clone)]
pub struct DbPillarPool {
    writer: Arc<Mutex<Connection>>,
    reader_pool: Pool<SqliteConnectionManager>,
    db_path: PathBuf,
}

#[cfg(any(feature = "native", feature = "db"))]
impl DbPillarPool {
    /// Initializes a dual-pool manager for the given pillar database path.
    pub fn new(pillar: &str, clm_root: &str) -> Result<Self> {
        let normalized = get_pillar_filename(pillar);
        let db_path = get_db_path(&normalized, clm_root);
        let db_path_str = db_path.to_string_lossy();

        if !db_path_str.starts_with(crate::layer0::vocabulary::URI_SCHEME_FILE)
            && let Some(parent) = db_path.parent() {
                let _ = fs::create_dir_all(parent);
            }

        #[cfg(unix)]
        if !db_path_str.starts_with(crate::layer0::vocabulary::URI_SCHEME_FILE) {
            use std::os::unix::fs::PermissionsExt;
            if let Ok(meta) = fs::metadata(&db_path) {
                let current_mode = meta.permissions().mode();
                if current_mode & 0o200 == 0 {
                    let mut perms = meta.permissions();
                    perms.set_mode(current_mode | 0o664);
                    let _ = fs::set_permissions(&db_path, perms);
                }
            }
            for ext in &["-shm", "-wal"] {
                let aux_path = format!("{}{}", db_path.display(), ext);
                if let Ok(meta) = fs::metadata(&aux_path) {
                    let current_mode = meta.permissions().mode();
                    if current_mode & 0o200 == 0 {
                        let mut perms = meta.permissions();
                        perms.set_mode(current_mode | 0o664);
                        let _ = fs::set_permissions(&aux_path, perms);
                    }
                }
            }
        }

        // 1. Initialize Writer Connection with WAL pragma & run migrations
        let mut writer_conn = Connection::open_with_flags(
            &db_path,
            rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
                | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
                | rusqlite::OpenFlags::SQLITE_OPEN_URI,
        )?;
        writer_conn.busy_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_BUSY_TIMEOUT_MS)))?;
        let _ = writer_conn.pragma_update(None, "journal_mode", "WAL");
        let _ = writer_conn.pragma_update(None, "synchronous", "NORMAL");
        let _ = writer_conn.pragma_update(None, "foreign_keys", "ON");

        // Apply versioned schema migrations on the writer connection
        let _ = migrations::apply_migrations(&mut writer_conn, &normalized);

        crate::layer2::record_sqlite_guard::install(&writer_conn)?;

        // 2. Initialize Reader Pool
        let manager = SqliteConnectionManager::file(&db_path)
            .with_flags(
                rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
                    | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
                    | rusqlite::OpenFlags::SQLITE_OPEN_URI,
            )
            .with_init(|conn| {
                let _ = conn.busy_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_BUSY_TIMEOUT_MS)));
                let _ = conn.pragma_update(None, "journal_mode", "WAL");
                let _ = conn.pragma_update(None, "synchronous", "NORMAL");

                crate::layer2::record_sqlite_guard::install(conn)?;
                conn.pragma_update(None, "query_only", true)
            });

        let reader_pool = Pool::builder()
            .max_size(16)
            .min_idle(Some(2))
            .connection_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_BUSY_TIMEOUT_MS)))
            .build(manager)
            .map_err(|e| {
                ClmError::storage(
                    StorageErrorCode::DatabaseUnavailable,
                    format!("Failed to build SQLite reader connection pool for {}: {}", normalized, e),
                )
            })?;

        Ok(Self {
            writer: Arc::new(Mutex::new(writer_conn)),
            reader_pool,
            db_path,
        })
    }

    /// Returns an `Arc<Mutex<Connection>>` handle to the dedicated writer connection.
    pub fn writer(&self) -> Arc<Mutex<Connection>> {
        self.writer.clone()
    }

    /// Acquires a connection from the multi-reader pool without locking the writer.
    pub fn reader(&self) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
        self.reader_pool.get().map_err(|e| {
            ClmError::storage(
                StorageErrorCode::DatabaseUnavailable,
                format!("Failed to acquire connection from reader pool: {}", e),
            )
        })
    }

    /// Returns the underlying database path or URI.
    pub fn db_path(&self) -> &Path {
        &self.db_path
    }
}

#[cfg(any(feature = "native", feature = "db"))]
static SHARED_POOLS: OnceLock<dashmap::DashMap<String, Arc<DbPillarPool>>> = OnceLock::new();

#[cfg(any(feature = "native", feature = "db"))]
static SHARED_CONNECTIONS: OnceLock<dashmap::DashMap<String, Arc<Mutex<Connection>>>> = OnceLock::new();

#[cfg(any(feature = "native", feature = "db"))]
pub fn get_pillar_pool(pillar: &str, clm_root: &str) -> Result<Arc<DbPillarPool>> {
    let normalized = get_pillar_filename(pillar);
    let db_path = get_db_path(&normalized, clm_root);
    let key = format!("{}:{}", normalized, db_path.to_string_lossy());
    let map = SHARED_POOLS.get_or_init(dashmap::DashMap::new);
    if let Some(entry) = map.get(&key) {
        return Ok(entry.clone());
    }
    let pool = Arc::new(DbPillarPool::new(&normalized, clm_root)?);
    map.insert(key, pool.clone());
    Ok(pool)
}

#[cfg(any(feature = "native", feature = "db"))]
pub fn get_writer_connection(pillar: &str, clm_root: &str) -> Result<Arc<Mutex<Connection>>> {
    let pool = get_pillar_pool(pillar, clm_root)?;
    Ok(pool.writer())
}

#[cfg(any(feature = "native", feature = "db"))]
pub fn get_reader_connection(pillar: &str, clm_root: &str) -> Result<r2d2::PooledConnection<SqliteConnectionManager>> {
    let pool = get_pillar_pool(pillar, clm_root)?;
    pool.reader()
}

#[cfg(any(feature = "native", feature = "db"))]
pub fn get_shared_connection(pillar: &str, clm_root: &str) -> std::result::Result<Arc<Mutex<Connection>>, String> {
    get_writer_connection(pillar, clm_root).map_err(|e| e.to_string())
}

#[cfg(any(feature = "native", feature = "db"))]
pub fn clear_shared_connections() {
    if let Some(map) = SHARED_CONNECTIONS.get() {
        map.clear();
    }
    if let Some(pools) = SHARED_POOLS.get() {
        pools.clear();
    }
}

pub fn set_sandbox_depth(depth: usize) {
    TEST_SANDBOX_DEPTH.with(|d| d.set(depth));
}

pub fn thread_local_depth_get() -> usize {
    TEST_SANDBOX_DEPTH.with(|d| d.get())
}

pub fn thread_local_depth_increment() -> usize {
    TEST_SANDBOX_DEPTH.with(|d| {
        let c = d.get() + 1;
        d.set(c);
        c
    })
}

pub fn thread_local_depth_decrement() {
    TEST_SANDBOX_DEPTH.with(|d| {
        let c = d.get();
        if c > 0 {
            d.set(c - 1);
        }
    })
}

pub struct DatabaseSandboxGuard {
    prev_active: bool,
    prev_db_id: Option<String>,
}

impl DatabaseSandboxGuard {
    pub fn activate(db_id: &str) -> Self {
        let prev_active = TEST_SANDBOX_ACTIVE.with(|active| active.get());
        let prev_db_id = TEST_SANDBOX_DB_ID.with(|id| id.borrow().clone());
        
        TEST_SANDBOX_ACTIVE.with(|active| active.set(true));
        TEST_SANDBOX_DB_ID.with(|id| {
            *id.borrow_mut() = Some(db_id.to_string());
        });
        
        DatabaseSandboxGuard {
            prev_active,
            prev_db_id,
        }
    }
}

impl Drop for DatabaseSandboxGuard {
    fn drop(&mut self) {
        TEST_SANDBOX_ACTIVE.with(|active| active.set(self.prev_active));
        TEST_SANDBOX_DB_ID.with(|id| {
            *id.borrow_mut() = self.prev_db_id.clone();
        });
        clear_in_memory_connections();
    }
}


pub fn get_db_path(pillar: &str, clm_root: &str) -> PathBuf {
    if crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_MEM_TRIDB).unwrap_or_default() == "1" {
        return PathBuf::from(format!("{HANDLE_FILE_MEMDB}{}?mode=memory&cache=shared", get_pillar_filename(pillar)));
    }
    let is_sandbox = TEST_SANDBOX_ACTIVE.with(|active| active.get());
    if is_sandbox {
        let db_id = TEST_SANDBOX_DB_ID.with(|id| {
            id.borrow().clone().unwrap_or_else(|| {
                format!("{:?}", std::thread::current().id())
            })
        });
        let depth = TEST_SANDBOX_DEPTH.with(|d| d.get());
        let depth_suffix = if depth > 0 { format!("_depth_{}", depth) } else { "".to_string() };
        PathBuf::from(format!("{HANDLE_FILE_MEMDB}{}_{}{}?mode=memory&cache=shared", get_pillar_filename(pillar), db_id, depth_suffix))
    } else {
        let zero_fs = crate::utils::is_zero_fs();
        if zero_fs {
            let snap_dir = crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_MEMBROKER_SNAP_DIR)
                .unwrap_or_else(|_| "/tmp".to_string());
            let snap_path = if cfg!(windows) && (snap_dir == "/tmp" || snap_dir == "\\tmp") {
                std::env::temp_dir()
            } else {
                PathBuf::from(&snap_dir)
            };
            snap_path.join(format!("{}.db", get_pillar_filename(pillar)))
        } else {
            let fname = get_pillar_filename(pillar);
            let db_dir = Path::new(clm_root).join("db");
            let target_path = db_dir.join(format!("{}.db", fname));
            // Backward-compatibility: if mcard_state requested but content_memory.db exists, fall back
            if fname == "mcard_state" && !target_path.exists() && db_dir.join("content_memory.db").exists() {
                db_dir.join("content_memory.db")
            } else if fname == "sovereign_identity" && !target_path.exists() && db_dir.join("agent_identities.db").exists() {
                db_dir.join("agent_identities.db")
            } else {
                target_path
            }
        }
    }
}

fn get_pillar_filename(pillar: &str) -> String {
    match pillar {
        "mcard_state" | "mcard_state.db" => "mcard_state".to_string(),
        "sovereign_identity" | "sovereign_identity.db" => "sovereign_identity".to_string(),
        "agent_identity" | "agent_identities" => "agent_identities".to_string(),
        crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY | "content" | "memory" => crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY.to_string(),
        crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG | "execution" | "log" => crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG.to_string(),
        "evolution" | "evol" => {
            let now = chrono::Local::now();
            let iso_week = chrono::Datelike::iso_week(&now);
            format!("pkc_evol_{:04}_w{:02}", iso_week.year(), iso_week.week())
        }
        _ => crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY.to_string(),
    }
}

/// Check if the membroker backend is active (CLM_DB_BACKEND=membroker and CLM_ZERO_FS=true).
pub fn is_membroker_backend() -> bool {
    crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_DB_BACKEND).unwrap_or_default() == "membroker"
        && crate::utils::is_zero_fs()
}

/// Send a SQL statement to the Python membroker via TCP (framed CLMQ protocol).
/// Returns the response body on success.
pub fn membroker_send(pillar: &str, sql: &str) -> Result<String> {
    let port: u16 = crate::layer0::environment::var(crate::layer0::vocabulary_env::ENV_CLM_MEMBROKER_PORT)
        .unwrap_or_else(|_| "43740".to_string())
        .parse()
        .unwrap_or(43740);

    let addr = format!("127.0.0.1:{}", port);
    let timeout = Duration::from_secs(5);
    let socket_addr: std::net::SocketAddr = addr
        .parse()
        .map_err(|e| ClmError::Evaluation(format!("Invalid membroker address: {}", e)))?;

    let mut stream = TcpStream::connect_timeout(&socket_addr, timeout)
        .map_err(|e| ClmError::Evaluation(format!("membroker connect failed: {}", e)))?;
    stream.set_read_timeout(Some(timeout))?;
    stream.set_write_timeout(Some(timeout))?;

    let body = sql.as_bytes();
    let header = format!("CLMQ\t{}\t{}\t\n", pillar, body.len());
    stream.write_all(header.as_bytes())?;
    stream.write_all(body)?;
    stream.shutdown(Shutdown::Write)?;

    let mut response = Vec::new();
    stream.read_to_end(&mut response)?;
    let resp_str = String::from_utf8_lossy(&response);

    if resp_str.starts_with("CLMR\tOK\t") {
        if let Some(pos) = resp_str.find('\n') {
            Ok(resp_str[pos + 1..].to_string())
        } else {
            Ok(String::new())
        }
    } else {
        Err(ClmError::Evaluation(format!("membroker error: {}", resp_str)))
    }
}

/// Sync a single INSERT/REPLACE statement to the membroker if the backend is active.
/// Silently skips if membroker is not available or not in use.
pub fn membroker_sync(pillar: &str, sql: &str) {
    if !is_membroker_backend() {
        return;
    }
    let _ = membroker_send(pillar, sql);
}


#[path = "db_connection.rs"]
mod db_connection;
#[allow(unused_imports)]
pub use db_connection::*;
#[path = "db_handles.rs"]
mod db_handles;
#[allow(unused_imports)]
pub use db_handles::*;
#[cfg(test)]
#[path = "db_tests.rs"]
mod tests;