vfa-tui 0.0.0

Enterprise-grade terminal UI for the Vanguard Frontier Agentic marketplace catalog
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! SQLite index manager — WAL mode, single-writer pattern, schema migrations.
//!
//! Implements Requirement 19 (all 9 criteria):
//! - WAL mode + `SQLITE_OPEN_NO_MUTEX` (Req 19.7, 19.9)
//! - Single-writer task via mpsc channel (Req 19.8)
//! - Sequential schema migrations preserving data (Req 19.4)
//! - In-memory fallback when file is inaccessible (Req 19.5)
//! - Stale-scan detection via `fs_mtime_at_scan` (Req 19.3)

use rusqlite::{Connection, OpenFlags};

use crate::error::TuiError;
use crate::persistence::schema::{BOOTSTRAP_SQL, MIGRATIONS};

// ---------------------------------------------------------------------------
// IndexManager
// ---------------------------------------------------------------------------

/// Manages the SQLite persistence layer.
///
/// Owns one write `Connection` (used by the writer task) and exposes
/// `read_connection()` to open additional per-thread read-only connections.
pub struct IndexManager {
    /// Path passed at open time — `None` for in-memory databases.
    db_path: Option<String>,
    /// Write connection (owned here; writer task borrows via the mpsc channel).
    write_conn: Connection,
    /// Current schema version after migration.
    pub schema_version: u32,
}

impl IndexManager {
    // -----------------------------------------------------------------------
    // Constructors
    // -----------------------------------------------------------------------

    /// Open (or create) the SQLite index at `path` with WAL mode.
    ///
    /// Uses `SQLITE_OPEN_NO_MUTEX` — thread safety is handled by Rust's
    /// ownership model (Req 19.9).  Runs schema migrations on every open
    /// (Req 19.4).
    pub fn open(path: &str) -> Result<Self, TuiError> {
        let flags = OpenFlags::SQLITE_OPEN_READ_WRITE
            | OpenFlags::SQLITE_OPEN_CREATE
            | OpenFlags::SQLITE_OPEN_NO_MUTEX;
        let conn =
            Connection::open_with_flags(path, flags).map_err(|e| TuiError::PersistenceOpen {
                path: path.to_string(),
                detail: e.to_string(),
            })?;
        // Enable WAL mode (Req 19.7).
        conn.pragma_update(None, "journal_mode", "WAL")
            .map_err(|e| TuiError::PersistenceOpen {
                path: path.to_string(),
                detail: format!("WAL pragma failed: {e}"),
            })?;
        let mut mgr = Self {
            db_path: Some(path.to_string()),
            write_conn: conn,
            schema_version: 0,
        };
        mgr.schema_version = mgr.migrate()?;
        Ok(mgr)
    }

    /// Open an in-memory database — useful for tests and as a fallback when
    /// the on-disk file is corrupt or inaccessible (Req 19.5).
    pub fn open_in_memory() -> Result<Self, TuiError> {
        let conn = Connection::open_in_memory().map_err(|e| TuiError::PersistenceOpen {
            path: ":memory:".to_string(),
            detail: e.to_string(),
        })?;
        let mut mgr = Self {
            db_path: None,
            write_conn: conn,
            schema_version: 0,
        };
        mgr.schema_version = mgr.migrate()?;
        Ok(mgr)
    }

    // -----------------------------------------------------------------------
    // Migration
    // -----------------------------------------------------------------------

    /// Run pending schema migrations sequentially, preserving existing data.
    ///
    /// Detects the current version from `schema_meta` (0 if the table doesn't
    /// yet exist), applies each migration whose target version is higher, and
    /// updates `schema_version` afterward.  Each migration runs inside its own
    /// `SAVEPOINT` so a failure leaves the database at a clean prior version
    /// rather than in an unknown intermediate state (Req 19.4).
    pub fn migrate(&self) -> Result<u32, TuiError> {
        // Bootstrap: ensure schema_meta exists before querying it.
        self.write_conn.execute_batch(BOOTSTRAP_SQL).map_err(|e| {
            TuiError::PersistenceMigration {
                from: 0,
                to: 0,
                detail: format!("bootstrap failed: {e}"),
            }
        })?;

        // Read current version (0 = fresh database).
        let current_version: u32 = self
            .write_conn
            .query_row(
                "SELECT value FROM schema_meta WHERE key = 'schema_version'",
                [],
                |row| {
                    let s: String = row.get(0)?;
                    Ok(s.parse::<u32>().unwrap_or(0))
                },
            )
            .unwrap_or(0);

        let mut version = current_version;

        for (target_version, sql) in MIGRATIONS {
            if *target_version <= version {
                continue;
            }
            // Apply this migration inside a savepoint for clean rollback.
            let sp_name = format!("migration_{target_version}");
            self.write_conn
                .execute_batch(&format!("SAVEPOINT {sp_name}"))
                .map_err(|e| TuiError::PersistenceMigration {
                    from: version,
                    to: *target_version,
                    detail: format!("savepoint failed: {e}"),
                })?;

            match self.write_conn.execute_batch(sql) {
                Ok(()) => {
                    // Update the stored version.
                    self.write_conn
                        .execute(
                            "INSERT OR REPLACE INTO schema_meta (key, value) \
                             VALUES ('schema_version', ?1)",
                            rusqlite::params![target_version.to_string()],
                        )
                        .map_err(|e| TuiError::PersistenceMigration {
                            from: version,
                            to: *target_version,
                            detail: format!("version update failed: {e}"),
                        })?;
                    self.write_conn
                        .execute_batch(&format!("RELEASE {sp_name}"))
                        .map_err(|e| TuiError::PersistenceMigration {
                            from: version,
                            to: *target_version,
                            detail: format!("release savepoint failed: {e}"),
                        })?;
                    version = *target_version;
                }
                Err(e) => {
                    let _ = self
                        .write_conn
                        .execute_batch(&format!("ROLLBACK TO {sp_name}"));
                    return Err(TuiError::PersistenceMigration {
                        from: version,
                        to: *target_version,
                        detail: e.to_string(),
                    });
                }
            }
        }

        Ok(version)
    }

    // -----------------------------------------------------------------------
    // Read connections (Req 19.8 — separate Connection per thread)
    // -----------------------------------------------------------------------

    /// Open a fresh **read-only** `Connection` for use in a caller thread.
    ///
    /// Each call returns an independent `Connection` instance, satisfying
    /// the requirement that every thread or task gets its own connection
    /// (Req 19.8, 19.9).
    ///
    /// In-memory managers share the write connection for reads (the in-memory
    /// database is not accessible from a second connection object).
    pub fn read_connection(&self) -> Result<Connection, TuiError> {
        match &self.db_path {
            Some(path) => {
                let flags = OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX;
                Connection::open_with_flags(path, flags).map_err(|e| TuiError::PersistenceQuery {
                    detail: format!("read connection failed: {e}"),
                })
            }
            None => {
                // For in-memory databases there is only one logical connection;
                // return an error so callers fall back to the write connection.
                Err(TuiError::PersistenceQuery {
                    detail: "in-memory database has no separate read connection".to_string(),
                })
            }
        }
    }

    /// Borrow the write connection for direct use (e.g. from tests or the
    /// writer task before the channel is set up).
    pub fn write_conn(&self) -> &Connection {
        &self.write_conn
    }

    // -----------------------------------------------------------------------
    // Stale-scan detection (Req 19.3)
    // -----------------------------------------------------------------------

    /// Returns `true` if the cached scan for `workspace_path` is older than
    /// `fs_mtime` (Unix seconds), meaning a re-scan is needed.
    ///
    /// Returns `true` when there is no cached entry (first scan).
    pub fn is_scan_stale(&self, workspace_path: &str, fs_mtime: i64) -> bool {
        let result: rusqlite::Result<i64> = self.write_conn.query_row(
            "SELECT fs_mtime_at_scan FROM workspace_scans WHERE workspace_path = ?1",
            rusqlite::params![workspace_path],
            |row| row.get(0),
        );
        match result {
            Ok(cached_mtime) => fs_mtime > cached_mtime,
            Err(_) => true, // No cached entry → stale by definition.
        }
    }

    // -----------------------------------------------------------------------
    // Cached scan loading (Req 19.2)
    // -----------------------------------------------------------------------

    /// Return all workspace paths that have cached scan entries.
    pub fn load_cached_scan_paths(&self) -> Vec<String> {
        let mut stmt = match self
            .write_conn
            .prepare("SELECT workspace_path FROM workspace_scans ORDER BY workspace_path")
        {
            Ok(s) => s,
            Err(_) => return vec![],
        };
        stmt.query_map([], |row| row.get::<_, String>(0))
            .map(|rows| rows.filter_map(|r| r.ok()).collect())
            .unwrap_or_default()
    }

    // -----------------------------------------------------------------------
    // Drift history (Req 10.x)
    // -----------------------------------------------------------------------

    /// Load all drift-history rows as
    /// `(workspace_path, asset_id, drift_type, resolved_at)` tuples, ordered by
    /// id. `resolved_at` is `None` for still-active drift.
    pub fn load_drift_history(&self) -> Vec<(String, String, String, Option<String>)> {
        let mut stmt = match self.write_conn.prepare(
            "SELECT workspace_path, asset_id, drift_type, resolved_at \
             FROM drift_history ORDER BY id",
        ) {
            Ok(s) => s,
            Err(_) => return vec![],
        };
        stmt.query_map([], |row| {
            Ok((
                row.get::<_, String>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, Option<String>>(3)?,
            ))
        })
        .map(|rows| rows.filter_map(|r| r.ok()).collect())
        .unwrap_or_default()
    }

    // -----------------------------------------------------------------------
    // Coverage cache (Req 3.6)
    // -----------------------------------------------------------------------

    /// Load cached per-workspace coverage scores as `(workspace_path, score)`
    /// tuples, ordered by workspace path.
    pub fn load_coverage_scores(&self) -> Vec<(String, f64)> {
        let mut stmt = match self.write_conn.prepare(
            "SELECT workspace_path, coverage_score FROM coverage_cache ORDER BY workspace_path",
        ) {
            Ok(s) => s,
            Err(_) => return vec![],
        };
        stmt.query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, f64>(1)?))
        })
        .map(|rows| rows.filter_map(|r| r.ok()).collect())
        .unwrap_or_default()
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn open_in_memory_migrates_to_latest() {
        let mgr = IndexManager::open_in_memory().expect("open_in_memory");
        assert_eq!(mgr.schema_version, 4, "should migrate to version 4");
    }

    #[test]
    fn open_in_memory_tables_exist() {
        let mgr = IndexManager::open_in_memory().expect("open_in_memory");
        let conn = mgr.write_conn();

        // workspace_scans
        conn.execute(
            "INSERT INTO workspace_scans \
             (workspace_path, workspace_name, last_scan_ts, scan_duration_ms, asset_count, fs_mtime_at_scan) \
             VALUES ('/tmp/ws', 'ws', '2025-01-01T00:00:00Z', 100, 5, 9999)",
            [],
        )
        .expect("insert workspace_scans");

        // audit_log
        conn.execute(
            "INSERT INTO audit_log \
             (timestamp, event_type, subject, details, operator, entry_hash, prev_hash) \
             VALUES ('2025-01-01T00:00:00.000Z', 'policy_evaluation', 'ws', '{}', \
                     'system', 'h1', '')",
            [],
        )
        .expect("insert audit_log");

        // gate_history
        conn.execute(
            "INSERT INTO gate_history \
             (gate_name, status, exit_code, duration_ms, timestamp, catalog_hash) \
             VALUES ('lint', 'pass', 0, 800, '2025-01-01T00:00:00Z', 'ch')",
            [],
        )
        .expect("insert gate_history");
    }

    #[test]
    fn is_scan_stale_no_entry_returns_true() {
        let mgr = IndexManager::open_in_memory().expect("open_in_memory");
        assert!(mgr.is_scan_stale("/nonexistent/ws", 0));
    }

    #[test]
    fn is_scan_stale_old_mtime_returns_false() {
        let mgr = IndexManager::open_in_memory().expect("open_in_memory");
        mgr.write_conn()
            .execute(
                "INSERT INTO workspace_scans \
                 (workspace_path, workspace_name, last_scan_ts, scan_duration_ms, asset_count, fs_mtime_at_scan) \
                 VALUES ('/ws', 'ws', '2025-01-01T00:00:00Z', 100, 0, 1000)",
                [],
            )
            .unwrap();
        // fs_mtime == 1000, same as cached → not stale
        assert!(!mgr.is_scan_stale("/ws", 1000));
        // fs_mtime == 999, older than cached → not stale
        assert!(!mgr.is_scan_stale("/ws", 999));
    }

    #[test]
    fn is_scan_stale_newer_mtime_returns_true() {
        let mgr = IndexManager::open_in_memory().expect("open_in_memory");
        mgr.write_conn()
            .execute(
                "INSERT INTO workspace_scans \
                 (workspace_path, workspace_name, last_scan_ts, scan_duration_ms, asset_count, fs_mtime_at_scan) \
                 VALUES ('/ws', 'ws', '2025-01-01T00:00:00Z', 100, 0, 1000)",
                [],
            )
            .unwrap();
        // fs_mtime == 1001 > 1000 → stale
        assert!(mgr.is_scan_stale("/ws", 1001));
    }

    #[test]
    fn migrate_is_idempotent() {
        let mgr = IndexManager::open_in_memory().expect("open_in_memory");
        // Calling migrate() again should return the same version without error.
        let v2 = mgr.migrate().expect("second migrate");
        assert_eq!(v2, 4);
    }

    #[test]
    fn open_file_based_migrates_and_is_queryable() {
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("test.db");
        let path_str = path.to_str().expect("path utf8");

        let mgr = IndexManager::open(path_str).expect("open file db");
        assert_eq!(mgr.schema_version, 4);

        // Read connection should be openable.
        let rconn = mgr.read_connection().expect("read connection");
        let v: String = rconn
            .query_row(
                "SELECT value FROM schema_meta WHERE key = 'schema_version'",
                [],
                |row| row.get(0),
            )
            .expect("query schema_version");
        assert_eq!(v, "4");
    }

    #[test]
    fn load_cached_scan_paths_empty_db() {
        let mgr = IndexManager::open_in_memory().expect("open_in_memory");
        let paths = mgr.load_cached_scan_paths();
        assert!(paths.is_empty());
    }

    #[test]
    fn load_cached_scan_paths_with_entries() {
        let mgr = IndexManager::open_in_memory().expect("open_in_memory");
        for ws in ["/ws/a", "/ws/b"] {
            mgr.write_conn()
                .execute(
                    "INSERT INTO workspace_scans \
                     (workspace_path, workspace_name, last_scan_ts, scan_duration_ms, asset_count, fs_mtime_at_scan) \
                     VALUES (?1, 'ws', '2025-01-01T00:00:00Z', 100, 0, 1000)",
                    rusqlite::params![ws],
                )
                .unwrap();
        }
        let paths = mgr.load_cached_scan_paths();
        assert_eq!(paths.len(), 2);
        assert!(paths.contains(&"/ws/a".to_string()));
        assert!(paths.contains(&"/ws/b".to_string()));
    }
}