clm-kernel 0.0.19

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
Documentation
#![allow(unused_imports)]
use super::*;

pub fn get_connection(pillar: &str, clm_root: &str) -> Result<Connection> {
    let db_path = get_db_path(pillar, clm_root);
    // Ensure parent dir exists (only if not in-memory URI starting with file:)
    let db_path_str = db_path.to_string_lossy();
    if !db_path_str.starts_with("file:") {
        if let Some(parent) = db_path.parent() {
            let _ = fs::create_dir_all(parent);
        }
    } else {
        // Shared memory database. Cache connection in thread-local to ensure persistence
        let path_str = db_path_str.to_string();
        let already_registered = IN_MEMORY_CONNS.with(|conns| conns.borrow().contains_key(&path_str));
        if !already_registered
            && let Ok(keep_alive_conn) = Connection::open_with_flags(
                &db_path,
                rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
                    | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
                    | rusqlite::OpenFlags::SQLITE_OPEN_URI,
            ) {
                let _ = keep_alive_conn.busy_timeout(std::time::Duration::from_millis(10000));
                let _ = keep_alive_conn.pragma_update(None, "journal_mode", "WAL");
                let _ = keep_alive_conn.pragma_update(None, "synchronous", "NORMAL");

                let table_exists: bool = keep_alive_conn
                    .query_row(
                        "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='card';",
                        [],
                        |row| row.get(0),
                    )
                    .unwrap_or(0) > 0;
                if !table_exists {
                    let project_path = Path::new(clm_root);
                    let mut schema_file = project_path.join("types").join("schemas").join("mcard_schema.sql");
                    if !schema_file.exists() {
                        schema_file = project_path.join("schema").join("mcard_schema.sql");
                    }
                    if !schema_file.exists() {
                        schema_file = project_path.join("..").join("schema").join("mcard_schema.sql");
                    }
                    if schema_file.exists()
                        && let Ok(schema_sql) = fs::read_to_string(&schema_file) {
                            let sql_to_use = if schema_sql.contains("CREATE TABLE") {
                                &schema_sql
                            } else {
                                CANONICAL_SCHEMA
                            };
                            let _ = keep_alive_conn.execute_batch(sql_to_use);
                        }
                    let _ = keep_alive_conn.execute("ALTER TABLE card ADD COLUMN g_time TEXT;", []);
                }
                IN_MEMORY_CONNS.with(|conns| {
                    conns.borrow_mut().insert(path_str, keep_alive_conn);
                });
            }
    }

    let db_path_display = db_path.display().to_string();
    for path in [
        db_path_display.clone(),
        format!("{}-shm", db_path_display),
        format!("{}-wal", db_path_display),
    ] {
        let Ok(meta) = fs::metadata(&path) else { continue };
        let mut perms = meta.permissions();
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let current_mode = perms.mode();
            if current_mode & 0o200 == 0 {
                perms.set_mode(current_mode | 0o664);
                let _ = fs::set_permissions(&path, perms);
            }
        }
        // On Windows this clears only FILE_ATTRIBUTE_READONLY (left behind by an interrupted
        // read-only tape session); it does not widen ACLs the way it would on Unix.
        #[cfg(windows)]
        if perms.readonly() {
            #[allow(clippy::permissions_set_readonly_false)]
            perms.set_readonly(false);
            let _ = fs::set_permissions(&path, perms);
        }
    }

    let conn = Connection::open_with_flags(
        &db_path,
        rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
            | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
            | rusqlite::OpenFlags::SQLITE_OPEN_URI,
    )?;
    // Enable busy timeout to avoid lock contention
    conn.busy_timeout(std::time::Duration::from_millis(10000))?;

    // Enable WAL mode and normal synchronous mode persistently on the connection
    let _ = conn.pragma_update(None, "journal_mode", "WAL");
    let _ = conn.pragma_update(None, "synchronous", "NORMAL");

    // Check if the card table exists, if not, auto-initialize from schema
    let table_exists: bool = conn
        .query_row(
            "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='card';",
            [],
            |row| row.get(0),
        )
        .unwrap_or(0) > 0;
    if !table_exists {
        let _ = conn.execute_batch(CANONICAL_SCHEMA);
    }

    let _ = conn.execute("ALTER TABLE card ADD COLUMN g_time TEXT;", []);
    Ok(conn)
}

pub fn init_tridb(clm_root: &str) -> Result<()> {
    let project_path = Path::new(clm_root);
    let mut schema_file = project_path.join("types").join("schemas").join("mcard_schema.sql");
    if !schema_file.exists() {
        schema_file = project_path.join("..").join("types").join("schemas").join("mcard_schema.sql");
    }

    let schema_sql = if schema_file.exists() {
        let content = fs::read_to_string(&schema_file).unwrap_or_else(|_| CANONICAL_SCHEMA.to_string());
        if content.contains("CREATE TABLE") {
            content
        } else {
            CANONICAL_SCHEMA.to_string()
        }
    } else {
        CANONICAL_SCHEMA.to_string()
    };

    let mut version = "unknown".to_string();
    for line in schema_sql.lines() {
        if line.contains("Version:") {
            if let Some(pos) = line.find("Version:") {
                let suffix = &line[pos + 8..].trim();
                let parts: Vec<&str> = suffix.split_whitespace().collect();
                if !parts.is_empty() {
                    version = parts[0].trim_matches(|c: char| !c.is_numeric() && c != '.').to_string();
                }
            }
            break;
        }
    }

    eprintln!("[INIT_TRIDB] Canonical Schema: {}", schema_file.display());
    eprintln!("[INIT_TRIDB] Schema version:   {}", version);
    eprintln!();

    let pillars = vec!["agent_identities", "content_memory", "execution_log"];

    for pillar in pillars {
        let conn = get_connection(pillar, clm_root)
            .map_err(|e| ClmError::Evaluation(format!("Failed to open DB for {}: {}", pillar, e)))?;

        // Enable WAL mode
        let _ = conn.pragma_update(None, "journal_mode", "WAL");

        // Split schema into separate statements (SQLite executescript equivalent)
        // Since sqlite3 handles multiple statements, we can run them in a transaction
        conn.execute_batch(&schema_sql)
            .map_err(|e| ClmError::Evaluation(format!("Failed to apply schema for {}: {}", pillar, e)))?;

        // Validate tables exist
        let mut stmt = conn
            .prepare("SELECT name FROM sqlite_master WHERE type='table';")?;
        let table_rows = stmt
            .query_map([], |row| row.get::<_, String>(0))?;

        let mut has_card = false;
        let mut has_registry = false;
        let mut has_history = false;

        for name in table_rows.flatten() {
            match name.as_str() {
                "card" => has_card = true,
                "handle_registry" => has_registry = true,
                "handle_history" => has_history = true,
                _ => {}
            }
        }

        if !has_card || !has_registry || !has_history {
            return Err(ClmError::Evaluation(format!("DB {} is missing required tables", pillar)));
        }
        
        // Optional: Schema registration check for self-evolution
        let schema_handle = format!("db:schema:{}:{}", pillar, version);
        let schema_hash = crate::fs_utils::compute_sha256_hex(schema_sql.as_bytes());
        let _ = handle_register_batch(pillar, clm_root, &schema_handle, &crate::hash::prefixed(&schema_hash), &schema_sql);
    }

    // Load the type lattice eagerly so type validation is always available.
    // This mirrors the Python TypeRegistry which loads on construction.
    // The lattice is stored in a global OnceLock for zero-cost repeated access.
    let mut lattice = crate::type_lattice::TypeLattice::new();
    lattice.load_registry(clm_root);
    eprintln!("[INIT_TRIDB] Type lattice loaded: {} types registered", lattice.registry.len());

    Ok(())
}

pub fn sql_query_parameterized(
    pillar: &str,
    clm_root: &str,
    query: &str,
    params_vec: Vec<String>,
    as_json: bool,
) -> Result<String> {
    let conn = get_connection(pillar, clm_root)?;
    
    // Ensure table structure is present (fallback just in case)
    let _ = conn.execute("SELECT 1 FROM card LIMIT 1;", []);
    
    let query_trimmed = query.trim();
    let is_batch = query_trimmed.contains("BEGIN;") 
        || query_trimmed.contains("begin;")
        || query_trimmed.contains("COMMIT;")
        || query_trimmed.contains("commit;")
        || (query_trimmed.contains(';') && !query_trimmed.ends_with(';'))
        || query_trimmed.matches(';').count() > 1;

    if is_batch {
        conn.execute_batch(query)?;
        if as_json {
            return Ok("[]".to_string());
        } else {
            return Ok(String::new());
        }
    }

    let mut stmt = conn.prepare(query)?;
    let col_count = stmt.column_count();
    let col_names: Vec<String> = stmt
        .column_names()
        .into_iter()
        .map(|s| s.to_string())
        .collect();

    // Convert Vec<String> to rusqlite params
    let params_ref: Vec<&dyn rusqlite::ToSql> = params_vec
        .iter()
        .map(|s| s as &dyn rusqlite::ToSql)
        .collect();

    let mut rows = stmt.query(params_ref.as_slice())?;
    
    if as_json {
        let mut results = Vec::new();
        while let Some(row) = rows.next()? {
            let mut row_map = serde_json::Map::new();
            for i in 0..col_count {
                let name = &col_names[i];
                let value_ref = row.get_ref(i)?;
                let json_val = match value_ref {
                    rusqlite::types::ValueRef::Null => serde_json::Value::Null,
                    rusqlite::types::ValueRef::Integer(n) => serde_json::Value::Number(n.into()),
                    rusqlite::types::ValueRef::Real(f) => {
                        if let Some(num) = serde_json::Number::from_f64(f) {
                            serde_json::Value::Number(num)
                        } else {
                            serde_json::Value::Null
                        }
                    }
                    rusqlite::types::ValueRef::Text(t) => {
                        let text = String::from_utf8_lossy(t).into_owned();
                        serde_json::Value::String(text)
                    }
                    rusqlite::types::ValueRef::Blob(b) => {
                        let text = String::from_utf8_lossy(b).into_owned();
                        serde_json::Value::String(text)
                    }
                };
                row_map.insert(name.clone(), json_val);
            }
            results.push(serde_json::Value::Object(row_map));
        }
        Ok(serde_json::Value::Array(results).to_string())
    } else {
        let mut results = Vec::new();
        while let Some(row) = rows.next()? {
            let mut row_parts = Vec::new();
            for i in 0..col_count {
                let val_ref = row.get_ref(i)?;
                let s = match val_ref {
                    rusqlite::types::ValueRef::Null => "".to_string(),
                    rusqlite::types::ValueRef::Integer(n) => n.to_string(),
                    rusqlite::types::ValueRef::Real(f) => f.to_string(),
                    rusqlite::types::ValueRef::Text(t) => String::from_utf8_lossy(t).into_owned(),
                    rusqlite::types::ValueRef::Blob(b) => String::from_utf8_lossy(b).into_owned(),
                };
                row_parts.push(s);
            }
            results.push(row_parts.join("|"));
        }
        Ok(results.join("\n"))
    }
}

/// Pre-compiled statement query for card content by hash.
pub fn get_card_cached(conn: &Connection, hash: &str) -> Result<Option<String>> {
    let mut stmt = conn.prepare_cached("SELECT content FROM card WHERE hash = ?1")?;
    let mut rows = stmt.query(params![hash])?;
    if let Some(row) = rows.next()? {
        let val: rusqlite::types::Value = row.get(0)?;
        let content = match val {
            rusqlite::types::Value::Text(s) => s,
            rusqlite::types::Value::Blob(b) => {
                String::from_utf8(b).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
            }
            _ => "".to_string(),
        };
        Ok(Some(content))
    } else {
        Ok(None)
    }
}

/// Pre-compiled statement check if a card exists by hash.
pub fn card_exists_cached(conn: &Connection, hash: &str) -> Result<bool> {
    let mut stmt = conn.prepare_cached("SELECT count(*) FROM card WHERE hash = ?1")?;
    let count: i64 = stmt.query_row(params![hash], |row| row.get(0))?;
    Ok(count > 0)
}

/// Pre-compiled statement resolution of handle to current hash.
pub fn resolve_handle_hash_cached(conn: &Connection, handle: &str) -> Result<Option<String>> {
    let mut stmt = conn.prepare_cached("SELECT current_hash FROM handle_registry WHERE handle = ?1")?;
    let res: Option<String> = stmt.query_row(params![handle], |row| row.get(0)).ok();
    Ok(res)
}

/// Pre-compiled statement resolution of handle to card content.
pub fn resolve_handle_content_cached(conn: &Connection, handle: &str) -> Result<Option<String>> {
    if let Some(hash) = resolve_handle_hash_cached(conn, handle)? {
        get_card_cached(conn, &hash)
    } else {
        Ok(None)
    }
}