Skip to main content

kaizen/store/sqlite/
mod.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2//! Sync SQLite store. WAL mode, schema migrations as ordered SQL strings.
3
4use crate::core::config::try_team_salt;
5use crate::core::event::{Event, EventKind, EventSource, SessionRecord, SessionStatus};
6use crate::core::trace_span::{TraceSpanKind, TraceSpanRecord};
7use crate::metrics::types::{
8    FileFact, RankedFile, RankedTool, RepoEdge, RepoSnapshotRecord, ToolSpanView,
9};
10use crate::store::event_index::index_event_derived;
11use crate::store::projector::{DEFAULT_ORPHAN_TTL_MS, Projector, ProjectorEvent};
12use crate::store::tool_span_index::{
13    clear_session_spans, rebuild_tool_spans_for_session, upsert_tool_span_record,
14};
15use crate::sync::context::SyncIngestContext;
16use crate::sync::outbound::outbound_event_from_row;
17use crate::sync::redact::redact_payload;
18use crate::sync::smart::enqueue_tool_spans_for_session;
19use anyhow::{Context, Result};
20use rusqlite::types::Value;
21use rusqlite::{
22    Connection, OpenFlags, OptionalExtension, TransactionBehavior, params, params_from_iter,
23};
24use std::cell::RefCell;
25use std::collections::{HashMap, HashSet};
26use std::path::{Path, PathBuf};
27
28pub(super) use constants::{DEFAULT_CACHE_KIB, DEFAULT_MMAP_MB, SYNTHETIC_TS_CEILING_MS};
29pub use constants::{
30    SYNC_STATE_LAST_AGENT_SCAN_MS, SYNC_STATE_LAST_AUTO_PRUNE_MS, SYNC_STATE_SEARCH_DIRTY_MS,
31};
32pub(crate) use contracts::{CaptureQualityRow, TraceSpanQualityRow};
33pub use contracts::{
34    GuidanceKind, GuidancePerfRow, GuidanceReport, InsightsStats, PruneStats, SessionFilter,
35    SessionOutcomeRow, SessionPage, SessionSampleAgg, SessionStatusRow, StoreOpenMode,
36    SummaryStats, SyncStatusSnapshot, ToolSpanSyncRow,
37};
38pub(super) use sql::{PAIN_HOTSPOTS_SQL, SESSION_SELECT, TOOL_RANK_ROWS_SQL};
39
40#[derive(Clone)]
41struct SpanTreeCacheEntry {
42    session_id: String,
43    last_event_seq: Option<u64>,
44    nodes: Vec<crate::store::span_tree::SpanNode>,
45}
46
47pub struct Store {
48    conn: Connection,
49    root: PathBuf,
50    search_writer: RefCell<Option<crate::search::PendingWriter>>,
51    span_tree_cache: RefCell<Option<SpanTreeCacheEntry>>,
52    projector: RefCell<Projector>,
53}
54
55mod artifact_windows;
56mod constants;
57mod contracts;
58mod evals;
59mod event_batch;
60mod event_extensions;
61mod event_projector;
62mod event_read;
63mod event_write;
64mod events;
65mod experiment_windows;
66mod feedback;
67mod guidance;
68mod guidance_candidates;
69mod maintenance;
70mod metrics;
71mod outbox_migration;
72mod outcomes;
73mod prompts;
74mod report_windows;
75mod reports;
76mod rows;
77mod samples;
78mod schema;
79mod session_read;
80mod session_window;
81mod sessions;
82mod sql;
83mod sync;
84#[cfg(test)]
85mod tests;
86mod tool_span_sync;
87mod tool_spans;
88mod trace_spans;
89mod visualization;
90
91pub(super) fn now_ms() -> u64 {
92    std::time::SystemTime::now()
93        .duration_since(std::time::UNIX_EPOCH)
94        .unwrap_or_default()
95        .as_millis() as u64
96}
97
98impl Store {
99    pub(crate) fn conn(&self) -> &Connection {
100        &self.conn
101    }
102
103    pub fn open(path: &Path) -> Result<Self> {
104        Self::open_with_mode(path, StoreOpenMode::ReadWrite)
105    }
106
107    pub fn open_read_only(path: &Path) -> Result<Self> {
108        Self::open_with_mode(path, StoreOpenMode::ReadOnlyQuery)
109    }
110
111    pub fn open_query(path: &Path) -> Result<Self> {
112        Self::open_with_mode(path, StoreOpenMode::ReadOnlyQuery)
113    }
114
115    pub(crate) fn open_empty(root: &Path) -> Result<Self> {
116        let conn = Connection::open_in_memory().context("open empty in-memory store")?;
117        initialize_empty(&conn)?;
118        Ok(store_from_connection(conn, root.to_path_buf()))
119    }
120
121    pub fn open_with_mode(path: &Path, mode: StoreOpenMode) -> Result<Self> {
122        prepare_parent(path, mode)?;
123        let conn = match mode {
124            StoreOpenMode::ReadWrite => Connection::open(path),
125            StoreOpenMode::ReadOnlyQuery => Connection::open_with_flags(
126                path,
127                OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX,
128            ),
129        }
130        .with_context(|| format!("open db: {}", path.display()))?;
131        schema::apply_pragmas(&conn, mode)?;
132        if mode == StoreOpenMode::ReadWrite {
133            for sql in schema::MIGRATIONS {
134                conn.execute_batch(sql)?;
135            }
136            schema::ensure_schema_columns(&conn)?;
137            outbox_migration::migrate(&conn, path.parent().unwrap_or_else(|| Path::new(".")))?;
138        }
139        let root = path
140            .parent()
141            .unwrap_or_else(|| Path::new("."))
142            .to_path_buf();
143        Ok(store_from_connection(conn, root))
144    }
145
146    pub(super) fn invalidate_span_tree_cache(&self) {
147        *self.span_tree_cache.borrow_mut() = None;
148    }
149}
150
151fn initialize_empty(conn: &Connection) -> Result<()> {
152    schema::apply_pragmas(conn, StoreOpenMode::ReadWrite)?;
153    schema::MIGRATIONS
154        .iter()
155        .try_for_each(|statement| conn.execute_batch(statement))?;
156    schema::ensure_schema_columns(conn)
157}
158
159fn store_from_connection(conn: Connection, root: PathBuf) -> Store {
160    Store {
161        conn,
162        root,
163        search_writer: RefCell::new(None),
164        span_tree_cache: RefCell::new(None),
165        projector: RefCell::new(Projector::default()),
166    }
167}
168
169fn prepare_parent(path: &Path, mode: StoreOpenMode) -> Result<()> {
170    if mode == StoreOpenMode::ReadOnlyQuery {
171        return Ok(());
172    }
173    if let Some(parent) = path.parent() {
174        std::fs::create_dir_all(parent)?;
175    }
176    Ok(())
177}
178
179impl Drop for Store {
180    fn drop(&mut self) {
181        if let Some(writer) = self.search_writer.get_mut().as_mut() {
182            let _ = writer.commit();
183        }
184    }
185}