Skip to main content

clm_rust_core/layer2/
db_connection.rs

1#![allow(unused_imports)]
2use super::*;
3
4pub fn get_connection(pillar: &str, clm_root: &str) -> Result<Connection> {
5    let db_path = get_db_path(pillar, clm_root);
6    // Ensure parent dir exists (only if not in-memory URI starting with file:)
7    let db_path_str = db_path.to_string_lossy();
8    if !db_path_str.starts_with(crate::layer0::vocabulary::URI_SCHEME_FILE) {
9        if let Some(parent) = db_path.parent() {
10            let _ = fs::create_dir_all(parent);
11        }
12    } else {
13        // Shared memory database. Cache connection in thread-local to ensure persistence
14        let path_str = db_path_str.to_string();
15        let already_registered = IN_MEMORY_CONNS.with(|conns| conns.borrow().contains_key(&path_str));
16        if !already_registered
17            && let Ok(keep_alive_conn) = Connection::open_with_flags(
18                &db_path,
19                rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
20                    | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
21                    | rusqlite::OpenFlags::SQLITE_OPEN_URI,
22            ) {
23                let _ = keep_alive_conn.busy_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_BUSY_TIMEOUT_MS)));
24                let _ = keep_alive_conn.pragma_update(None, "journal_mode", "WAL");
25                let _ = keep_alive_conn.pragma_update(None, "synchronous", "NORMAL");
26
27                let table_exists: bool = keep_alive_conn
28                    .query_row(
29                        "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='card';",
30                        [],
31                        |row| row.get(0),
32                    )
33                    .unwrap_or(0) > 0;
34                if !table_exists {
35                    let project_path = Path::new(clm_root);
36                    let mut schema_file = project_path.join("types").join("schemas").join("mcard_schema.sql");
37                    if !schema_file.exists() {
38                        schema_file = project_path.join("schema").join("mcard_schema.sql");
39                    }
40                    if !schema_file.exists() {
41                        schema_file = project_path.join("..").join("schema").join("mcard_schema.sql");
42                    }
43                    if schema_file.exists()
44                        && let Ok(schema_sql) = fs::read_to_string(&schema_file) {
45                            let sql_to_use = if schema_sql.contains("CREATE TABLE") {
46                                &schema_sql
47                            } else {
48                                CANONICAL_SCHEMA
49                            };
50                            let _ = keep_alive_conn.execute_batch(sql_to_use);
51                        }
52                    let _ = keep_alive_conn.execute("ALTER TABLE card ADD COLUMN g_time TEXT;", []);
53                }
54                IN_MEMORY_CONNS.with(|conns| {
55                    conns.borrow_mut().insert(path_str, keep_alive_conn);
56                });
57            }
58    }
59
60    let db_path_display = db_path.display().to_string();
61    for path in [
62        db_path_display.clone(),
63        format!("{}-shm", db_path_display),
64        format!("{}-wal", db_path_display),
65    ] {
66        let Ok(meta) = fs::metadata(&path) else { continue };
67        let mut perms = meta.permissions();
68        #[cfg(unix)]
69        {
70            use std::os::unix::fs::PermissionsExt;
71            let current_mode = perms.mode();
72            if current_mode & 0o200 == 0 {
73                perms.set_mode(current_mode | 0o664);
74                let _ = fs::set_permissions(&path, perms);
75            }
76        }
77        // On Windows this clears only FILE_ATTRIBUTE_READONLY (left behind by an interrupted
78        // read-only tape session); it does not widen ACLs the way it would on Unix.
79        #[cfg(windows)]
80        if perms.readonly() {
81            #[allow(clippy::permissions_set_readonly_false)]
82            perms.set_readonly(false);
83            let _ = fs::set_permissions(&path, perms);
84        }
85    }
86
87    let conn = Connection::open_with_flags(
88        &db_path,
89        rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
90            | rusqlite::OpenFlags::SQLITE_OPEN_CREATE
91            | rusqlite::OpenFlags::SQLITE_OPEN_URI,
92    )?;
93    // Enable busy timeout to avoid lock contention
94    conn.busy_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_BUSY_TIMEOUT_MS)))?;
95
96    // Enable WAL mode and normal synchronous mode persistently on the connection
97    let _ = conn.pragma_update(None, "journal_mode", "WAL");
98    let _ = conn.pragma_update(None, "synchronous", "NORMAL");
99
100    // Check if the card table exists, if not, auto-initialize from schema
101    let table_exists: bool = conn
102        .query_row(
103            "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='card';",
104            [],
105            |row| row.get(0),
106        )
107        .unwrap_or(0) > 0;
108    if !table_exists {
109        let _ = conn.execute_batch(CANONICAL_SCHEMA);
110    }
111
112    let _ = conn.execute("ALTER TABLE card ADD COLUMN g_time TEXT;", []);
113    crate::layer2::record_sqlite_guard::install(&conn)?;
114    Ok(conn)
115}
116
117pub fn init_tridb(clm_root: &str) -> Result<()> {
118    let project_path = Path::new(clm_root);
119    let mut schema_file = project_path.join("types").join("schemas").join("mcard_schema.sql");
120    if !schema_file.exists() {
121        schema_file = project_path.join("..").join("types").join("schemas").join("mcard_schema.sql");
122    }
123
124    let schema_sql = if schema_file.exists() {
125        let content = fs::read_to_string(&schema_file).unwrap_or_else(|_| CANONICAL_SCHEMA.to_string());
126        if content.contains("CREATE TABLE") {
127            content
128        } else {
129            CANONICAL_SCHEMA.to_string()
130        }
131    } else {
132        CANONICAL_SCHEMA.to_string()
133    };
134
135    let mut version = "unknown".to_string();
136    for line in schema_sql.lines() {
137        if line.contains("Version:") {
138            if let Some(pos) = line.find("Version:") {
139                let suffix = &line[pos + 8..].trim();
140                let parts: Vec<&str> = suffix.split_whitespace().collect();
141                if !parts.is_empty() {
142                    version = parts[0].trim_matches(|c: char| !c.is_numeric() && c != '.').to_string();
143                }
144            }
145            break;
146        }
147    }
148
149    eprintln!("[INIT_TRIDB] Canonical Schema: {}", schema_file.display());
150    eprintln!("[INIT_TRIDB] Schema version:   {}", version);
151    eprintln!();
152
153    let pillars = vec![crate::layer0::vocabulary_env::PILLAR_AGENT_IDENTITIES, crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG];
154
155    for pillar in pillars {
156        let conn = get_connection(pillar, clm_root)
157            .map_err(|e| ClmError::Evaluation(format!("Failed to open DB for {}: {}", pillar, e)))?;
158
159        // Enable WAL mode
160        let _ = conn.pragma_update(None, "journal_mode", "WAL");
161
162        // Split schema into separate statements (SQLite executescript equivalent)
163        // Since sqlite3 handles multiple statements, we can run them in a transaction
164        conn.execute_batch(&schema_sql)
165            .map_err(|e| ClmError::Evaluation(format!("Failed to apply schema for {}: {}", pillar, e)))?;
166
167        // Validate tables exist
168        let mut stmt = conn
169            .prepare("SELECT name FROM sqlite_master WHERE type='table';")?;
170        let table_rows = stmt
171            .query_map([], |row| row.get::<_, String>(0))?;
172
173        let mut has_card = false;
174        let mut has_registry = false;
175        let mut has_history = false;
176
177        for name in table_rows.flatten() {
178            match name.as_str() {
179                "card" => has_card = true,
180                "handle_registry" => has_registry = true,
181                "handle_history" => has_history = true,
182                _ => {}
183            }
184        }
185
186        if !has_card || !has_registry || !has_history {
187            return Err(ClmError::Evaluation(format!("DB {} is missing required tables", pillar)));
188        }
189        
190        // Optional: Schema registration check for self-evolution
191        let schema_handle = format!("db:schema:{}:{}", pillar, version);
192        let schema_hash = crate::fs_utils::compute_sha256_hex(schema_sql.as_bytes());
193        let _ = handle_register_batch(pillar, clm_root, &schema_handle, &format!("{}:{}", crate::layer0::vocabulary::HASH_ALGORITHM_SHA256, schema_hash), &schema_sql);
194    }
195
196    // Load the type lattice eagerly so type validation is always available.
197    // This mirrors the Python TypeRegistry which loads on construction.
198    // The lattice is stored in a global OnceLock for zero-cost repeated access.
199    let mut lattice = crate::type_lattice::TypeLattice::new();
200    lattice.load_registry(clm_root);
201    eprintln!("[INIT_TRIDB] Type lattice loaded: {} types registered", lattice.registry.len());
202
203    Ok(())
204}
205
206pub fn sql_query_parameterized(
207    pillar: &str,
208    clm_root: &str,
209    query: &str,
210    params_vec: Vec<String>,
211    as_json: bool,
212) -> Result<String> {
213    let conn = get_connection(pillar, clm_root)?;
214    
215    // Ensure table structure is present (fallback just in case)
216    let _ = conn.execute("SELECT 1 FROM card LIMIT 1;", []);
217    
218    let query_trimmed = query.trim();
219    let is_batch = query_trimmed.contains("BEGIN;") 
220        || query_trimmed.contains("begin;")
221        || query_trimmed.contains("COMMIT;")
222        || query_trimmed.contains("commit;")
223        || (query_trimmed.contains(';') && !query_trimmed.ends_with(';'))
224        || query_trimmed.matches(';').count() > 1;
225
226    if is_batch {
227        return Err(ClmError::Evaluation("SQL batches are not supported by the read-only query API".into()));
228    }
229
230    let mut stmt = conn.prepare(query)?;
231    if !stmt.readonly() {
232        return Err(ClmError::Evaluation("SQL mutations require a verified storage writer".into()));
233    }
234    let col_count = stmt.column_count();
235    let col_names: Vec<String> = stmt
236        .column_names()
237        .into_iter()
238        .map(|s| s.to_string())
239        .collect();
240
241    // Convert Vec<String> to rusqlite params
242    let params_ref: Vec<&dyn rusqlite::ToSql> = params_vec
243        .iter()
244        .map(|s| s as &dyn rusqlite::ToSql)
245        .collect();
246
247    let mut rows = stmt.query(params_ref.as_slice())?;
248    
249    if as_json {
250        let mut results = Vec::new();
251        while let Some(row) = rows.next()? {
252            let mut row_map = serde_json::Map::new();
253            for i in 0..col_count {
254                let name = &col_names[i];
255                let value_ref = row.get_ref(i)?;
256                let json_val = match value_ref {
257                    rusqlite::types::ValueRef::Null => serde_json::Value::Null,
258                    rusqlite::types::ValueRef::Integer(n) => serde_json::Value::Number(n.into()),
259                    rusqlite::types::ValueRef::Real(f) => {
260                        if let Some(num) = serde_json::Number::from_f64(f) {
261                            serde_json::Value::Number(num)
262                        } else {
263                            serde_json::Value::Null
264                        }
265                    }
266                    rusqlite::types::ValueRef::Text(t) => {
267                        let text = String::from_utf8_lossy(t).into_owned();
268                        serde_json::Value::String(text)
269                    }
270                    rusqlite::types::ValueRef::Blob(b) => {
271                        let text = String::from_utf8_lossy(b).into_owned();
272                        serde_json::Value::String(text)
273                    }
274                };
275                row_map.insert(name.clone(), json_val);
276            }
277            results.push(serde_json::Value::Object(row_map));
278        }
279        Ok(serde_json::Value::Array(results).to_string())
280    } else {
281        let mut results = Vec::new();
282        while let Some(row) = rows.next()? {
283            let mut row_parts = Vec::new();
284            for i in 0..col_count {
285                let val_ref = row.get_ref(i)?;
286                let s = match val_ref {
287                    rusqlite::types::ValueRef::Null => "".to_string(),
288                    rusqlite::types::ValueRef::Integer(n) => n.to_string(),
289                    rusqlite::types::ValueRef::Real(f) => f.to_string(),
290                    rusqlite::types::ValueRef::Text(t) => String::from_utf8_lossy(t).into_owned(),
291                    rusqlite::types::ValueRef::Blob(b) => String::from_utf8_lossy(b).into_owned(),
292                };
293                row_parts.push(s);
294            }
295            results.push(row_parts.join("|"));
296        }
297        Ok(results.join("\n"))
298    }
299}
300
301/// Verified exact-byte UTF-8 materialization; cached identity alone cannot authorize a read.
302pub use crate::layer2::record_store::read_text as get_card_cached;
303
304/// Pre-compiled statement check if a card exists by hash.
305pub fn card_exists_cached(conn: &Connection, hash: &str) -> Result<bool> {
306    let mut stmt = conn.prepare_cached("SELECT count(*) FROM card WHERE hash = ?1")?;
307    let count: i64 = stmt.query_row(params![hash], |row| row.get(0))?;
308    Ok(count > 0)
309}
310
311/// Pre-compiled statement resolution of handle to current hash.
312pub fn resolve_handle_hash_cached(conn: &Connection, handle: &str) -> Result<Option<String>> {
313    let mut stmt = conn.prepare_cached("SELECT current_hash FROM handle_registry WHERE handle = ?1")?;
314    let res: Option<String> = stmt.query_row(params![handle], |row| row.get(0)).ok();
315    if let Some(hash) = &res {
316        if crate::layer2::record_store::read_record(conn, hash)?.is_none() {
317            return Err(ClmError::storage(StorageErrorCode::CorruptedContent, "handle points to missing card"));
318        }
319    }
320    Ok(res)
321}
322
323/// Pre-compiled statement resolution of handle to card content.
324pub fn resolve_handle_content_cached(conn: &Connection, handle: &str) -> Result<Option<String>> {
325    if let Some(hash) = resolve_handle_hash_cached(conn, handle)? {
326        get_card_cached(conn, &hash)
327    } else {
328        Ok(None)
329    }
330}
331