clm_rust_core/layer2/db/
compaction.rs1#[cfg(any(feature = "native", feature = "db"))]
9use rusqlite::Connection;
10#[cfg(any(feature = "native", feature = "db"))]
11use crate::error::Result;
12
13#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
14pub struct CompactionReport {
15 pub execution_log_initial_bytes: u64,
16 pub execution_log_final_bytes: u64,
17 pub cards_pruned: usize,
18 pub handles_pruned: usize,
19 pub history_pruned: usize,
20 pub documents_optimized: bool,
21 pub vacuumed: bool,
22}
23
24#[cfg(any(feature = "native", feature = "db"))]
26pub fn prune_execution_logs(clm_root: &str, max_age_days: u64) -> Result<CompactionReport> {
27 let mut report = CompactionReport::default();
28 let db_path = crate::db::get_db_path(crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG, clm_root);
29 if !db_path.exists() {
30 return Ok(report);
31 }
32
33 report.execution_log_initial_bytes = std::fs::metadata(&db_path).map(|m| m.len()).unwrap_or(0);
34
35 let mut conn = Connection::open(&db_path)?;
36 conn.busy_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_COMPACTION_BUSY_TIMEOUT_MS)))?;
37
38 let _ = conn.execute_batch("
40 DROP TRIGGER IF EXISTS card_delete;
41 DROP TRIGGER IF EXISTS card_update;
42 PRAGMA foreign_keys = OFF;
43 ");
44
45 let tx = conn.transaction()?;
46
47 let handles_pruned = if max_age_days == 0 {
49 tx.execute(
50 "DELETE FROM handle_registry WHERE handle LIKE 'run:%' OR handle LIKE 'escalation:%' OR handle LIKE 'tests:%';",
51 [],
52 )?
53 } else {
54 tx.execute(
55 "DELETE FROM handle_registry WHERE (handle LIKE 'run:%' OR handle LIKE 'escalation:%' OR handle LIKE 'tests:%') \
62 AND handle IN (SELECT handle FROM handle_history GROUP BY handle \
63 HAVING MIN(changed_at) < datetime('now', '-' || ?1 || ' days'));",
64 rusqlite::params![max_age_days],
65 )?
66 };
67 report.handles_pruned = handles_pruned;
68
69 let history_pruned = if max_age_days == 0 {
71 tx.execute(
72 "DELETE FROM handle_history WHERE handle NOT IN (SELECT handle FROM handle_registry);",
73 [],
74 )?
75 } else {
76 tx.execute(
77 "DELETE FROM handle_history WHERE handle NOT IN (SELECT handle FROM handle_registry) OR changed_at < datetime('now', '-' || ?1 || ' days');",
78 rusqlite::params![max_age_days],
79 )?
80 };
81 report.history_pruned = history_pruned;
82
83 let cards_pruned = tx.execute(
85 "DELETE FROM card WHERE hash NOT IN (SELECT current_hash FROM handle_registry);",
86 [],
87 )?;
88 report.cards_pruned = cards_pruned;
89
90 tx.commit()?;
91
92 let has_docs: bool = conn
94 .query_row(
95 "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='documents';",
96 [],
97 |r| r.get(0),
98 )
99 .unwrap_or(0) > 0;
100
101 if has_docs {
102 let _ = conn.execute_batch("
103 DELETE FROM documents;
104 INSERT INTO documents(content) SELECT content FROM card;
105 INSERT INTO documents(documents) VALUES('optimize');
106 ");
107 report.documents_optimized = true;
108 }
109
110 let _ = conn.execute_batch("
112 CREATE TRIGGER IF NOT EXISTS card_update AFTER UPDATE ON card
113 BEGIN
114 UPDATE documents SET content = new.content
115 WHERE rowid = (SELECT rowid FROM documents WHERE content = old.content LIMIT 1);
116 END;
117
118 CREATE TRIGGER IF NOT EXISTS card_delete AFTER DELETE ON card
119 BEGIN
120 DELETE FROM documents WHERE content = old.content;
121 END;
122
123 CREATE INDEX IF NOT EXISTS idx_handle_history_prev_hash ON handle_history(previous_hash);
124 PRAGMA foreign_keys = ON;
125 ");
126
127 let _ = conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);");
129 let _ = conn.execute_batch("VACUUM;");
130 report.vacuumed = true;
131
132 drop(conn);
133
134 report.execution_log_final_bytes = std::fs::metadata(&db_path).map(|m| m.len()).unwrap_or(0);
135 Ok(report)
136}
137
138#[cfg(any(feature = "native", feature = "db"))]
140pub fn compact_all(clm_root: &str) -> Result<CompactionReport> {
141 let pillars = [crate::layer0::vocabulary_env::PILLAR_CONTENT_MEMORY, "agent_identities", crate::layer0::vocabulary_env::PILLAR_EXECUTION_LOG];
142 for pillar in &pillars {
143 let path = crate::db::get_db_path(pillar, clm_root);
144 if path.exists()
145 && let Ok(conn) = Connection::open(&path) {
146 let _ = conn.busy_timeout(std::time::Duration::from_millis(u64::from(crate::layer0::runtime_contract::SQLITE_COMPACTION_BUSY_TIMEOUT_MS)));
147 let _ = conn.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);");
148 let _ = conn.execute_batch("VACUUM;");
149 }
150 }
151 prune_execution_logs(clm_root, 0)
152}