khive-db 0.20.0

SQLite storage backend: entities, edges, notes, events, FTS5, sqlite-vec vectors.
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Per-substrate SQLite store implementations.
//!
//! Each module provides a concrete store struct implementing one or more
//! `khive-storage` capability traits against the shared connection pool.

use std::sync::Arc;

use khive_storage::error::StorageError;
use khive_storage::types::StorageResult;
use khive_storage::StorageCapability;

use crate::pool::ConnectionPool;

pub mod agents;
pub mod attachment;
pub mod blob;
#[cfg(feature = "blob-s3")]
pub mod blob_s3;
pub mod entity;
pub mod event;
pub mod graph;
pub(crate) mod index_repair;
pub mod note;
pub mod sparse;
pub mod text;
pub mod vectors;

use khive_storage::{BatchWriteErrorClass, BatchWriteRetryability};

fn validate_json_equality_paths(
    predicates: &[(String, khive_storage::SqlValue)],
    capability: StorageCapability,
    operation: &'static str,
) -> Result<(), StorageError> {
    for (path, _) in predicates {
        if !path.starts_with("$.")
            || !path[2..].split('.').all(|part| {
                !part.is_empty() && part.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
            })
        {
            return Err(StorageError::InvalidInput {
                capability,
                operation: operation.into(),
                message: format!("invalid JSON equality path: {path:?}"),
            });
        }
    }
    Ok(())
}

fn append_json_equalities(
    conditions: &mut Vec<String>,
    params: &mut Vec<Box<dyn rusqlite::types::ToSql>>,
    column: &'static str,
    predicates: &[(String, khive_storage::SqlValue)],
) {
    use khive_storage::SqlValue;
    use rusqlite::types::Value;

    for (path, value) in predicates {
        params.push(Box::new(path.clone()));
        let path_index = params.len();
        let value = match value {
            SqlValue::Null => Value::Null,
            SqlValue::Bool(value) => Value::Integer(i64::from(*value)),
            SqlValue::Integer(value) => Value::Integer(*value),
            SqlValue::Float(value) => Value::Real(*value),
            SqlValue::Text(value) => Value::Text(value.clone()),
            SqlValue::Blob(value) => Value::Blob(value.clone()),
            SqlValue::Json(value) => Value::Text(value.to_string()),
            SqlValue::Uuid(value) => Value::Text(value.to_string()),
            SqlValue::Timestamp(value) => Value::Integer(value.timestamp_micros()),
        };
        params.push(Box::new(value));
        conditions.push(format!(
            "json_extract({column}, ?{path_index}) = ?{}",
            params.len()
        ));
    }
}

/// Stable refusal classification for SQLite errors captured inside a
/// best-effort per-item batch loop.
fn classify_batch_sqlite_error(
    error: &rusqlite::Error,
) -> (BatchWriteErrorClass, BatchWriteRetryability) {
    use rusqlite::ErrorCode;

    match error.sqlite_error_code() {
        Some(ErrorCode::ConstraintViolation) => (
            BatchWriteErrorClass::Constraint,
            BatchWriteRetryability::Permanent,
        ),
        Some(ErrorCode::DatabaseBusy | ErrorCode::DatabaseLocked) => (
            BatchWriteErrorClass::Busy,
            BatchWriteRetryability::Transient,
        ),
        Some(ErrorCode::OperationInterrupted) => (
            BatchWriteErrorClass::Cancelled,
            BatchWriteRetryability::Transient,
        ),
        Some(_) => (
            BatchWriteErrorClass::Driver,
            BatchWriteRetryability::Unknown,
        ),
        None => (
            BatchWriteErrorClass::Unknown,
            BatchWriteRetryability::Unknown,
        ),
    }
}

/// Run one typed-store read through the pool's bounded reader admission.
///
/// File-backed and in-memory stores deliberately share this one route. Pool
/// exhaustion is returned as the canonical retryable `AdmissionTimeout`; a
/// cancelled request remains the non-retryable `Timeout`. There is no
/// standalone-reader fallback (ADR-165 Slice 2).
///
/// The reader permit is awaited here, before the blocking task starts, so a
/// read that has to queue holds a task and not a blocking-pool thread. The
/// permit then moves into the blocking closure, which picks its connection
/// with the slot already held.
pub(crate) async fn run_pooled_store_read<F, R>(
    pool: Arc<ConnectionPool>,
    capability: StorageCapability,
    operation: &'static str,
    read: F,
) -> StorageResult<R>
where
    F: FnOnce(&crate::ProgressConn) -> Result<R, StorageError> + Send + 'static,
    R: Send + 'static,
{
    run_pooled_store_read_with_state(pool, capability, operation, false, read).await
}

/// A typed read that changes connection-local state must pay the pristine
/// return check, including when its callback unwinds with an open transaction.
pub(crate) async fn run_stateful_pooled_store_read<F, R>(
    pool: Arc<ConnectionPool>,
    capability: StorageCapability,
    operation: &'static str,
    read: F,
) -> StorageResult<R>
where
    F: FnOnce(&crate::ProgressConn) -> Result<R, StorageError> + Send + 'static,
    R: Send + 'static,
{
    run_pooled_store_read_with_state(pool, capability, operation, true, read).await
}

struct PooledReadStateOwner<'pool> {
    guard: crate::pool::ReaderGuard<'pool>,
    stateful: bool,
}

impl Drop for PooledReadStateOwner<'_> {
    fn drop(&mut self) {
        // The scope has already removed its progress handler on return or
        // unwind. Never release an unfinished transaction as a reusable reader,
        // including the degraded reader that shares the pool's writer.
        if self.stateful && !self.guard.conn().is_autocommit() {
            self.guard.discard();
        }
    }
}

async fn run_pooled_store_read_with_state<F, R>(
    pool: Arc<ConnectionPool>,
    capability: StorageCapability,
    operation: &'static str,
    stateful: bool,
    read: F,
) -> StorageResult<R>
where
    F: FnOnce(&crate::ProgressConn) -> Result<R, StorageError> + Send + 'static,
    R: Send + 'static,
{
    let trace = khive_storage::request_context::capture_search_phase_trace();
    let _admission_phase = trace.as_ref().and_then(|trace| {
        reader_phase(capability, ReaderPhaseKind::Admission).map(|phase| trace.enter(phase))
    });
    let admission = pool.acquire_reader_admission(capability, operation).await?;
    drop(_admission_phase);
    let _blocking_phase = trace.as_ref().and_then(|trace| {
        reader_phase(capability, ReaderPhaseKind::BlockingRead).map(|phase| trace.enter(phase))
    });
    let worker_trace = trace.clone();
    crate::read_cancellation::run_declared_interruptible_read(capability, operation, move |scope| {
        let _checkout_phase = worker_trace.as_ref().and_then(|trace| {
            reader_phase(capability, ReaderPhaseKind::Checkout).map(|phase| trace.enter(phase))
        });
        let guard = pool.resolve_reader_checkout(
            capability,
            operation,
            pool.reader_with_admission(admission, || scope.should_stop()),
        )?;
        drop(_checkout_phase);
        let mut owner = PooledReadStateOwner { guard, stateful };
        if stateful {
            owner.guard.mark_dirty();
        }
        let result = scope.run_pooled_reader(&mut owner.guard, read);
        if let Err(error) = &result {
            pool.record_reader_query_error(error);
        }
        result
    })
    .await
}

#[derive(Clone, Copy)]
enum ReaderPhaseKind {
    Admission,
    Checkout,
    BlockingRead,
}

fn reader_phase(
    capability: StorageCapability,
    kind: ReaderPhaseKind,
) -> Option<khive_storage::request_context::SearchPhase> {
    use khive_storage::request_context::SearchPhase;

    Some(match (capability, kind) {
        (StorageCapability::Text, ReaderPhaseKind::Admission) => SearchPhase::TextReaderAdmission,
        (StorageCapability::Text, ReaderPhaseKind::Checkout) => SearchPhase::TextReaderCheckout,
        (StorageCapability::Text, ReaderPhaseKind::BlockingRead) => SearchPhase::TextBlockingRead,
        (StorageCapability::Vectors, ReaderPhaseKind::Admission) => {
            SearchPhase::VectorReaderAdmission
        }
        (StorageCapability::Vectors, ReaderPhaseKind::Checkout) => {
            SearchPhase::VectorReaderCheckout
        }
        (StorageCapability::Vectors, ReaderPhaseKind::BlockingRead) => {
            SearchPhase::VectorBlockingRead
        }
        (StorageCapability::Entities, ReaderPhaseKind::Admission) => {
            SearchPhase::EntityReaderAdmission
        }
        (StorageCapability::Entities, ReaderPhaseKind::Checkout) => {
            SearchPhase::EntityReaderCheckout
        }
        (StorageCapability::Entities, ReaderPhaseKind::BlockingRead) => {
            SearchPhase::EntityBlockingRead
        }
        (StorageCapability::Notes, ReaderPhaseKind::Admission) => SearchPhase::NoteReaderAdmission,
        (StorageCapability::Notes, ReaderPhaseKind::Checkout) => SearchPhase::NoteReaderCheckout,
        (StorageCapability::Notes, ReaderPhaseKind::BlockingRead) => SearchPhase::NoteBlockingRead,
        (StorageCapability::Graph, ReaderPhaseKind::Admission) => SearchPhase::GraphReaderAdmission,
        (StorageCapability::Graph, ReaderPhaseKind::Checkout) => SearchPhase::GraphReaderCheckout,
        (StorageCapability::Graph, ReaderPhaseKind::BlockingRead) => SearchPhase::GraphBlockingRead,
        _ => return None,
    })
}

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

    fn sqlite_failure(code: i32) -> rusqlite::Error {
        rusqlite::Error::SqliteFailure(rusqlite::ffi::Error::new(code), None)
    }

    #[test]
    fn sqlite_refusal_classes_distinguish_permanent_transient_and_unknown() {
        let cases = [
            (
                rusqlite::ffi::SQLITE_CONSTRAINT,
                BatchWriteErrorClass::Constraint,
                BatchWriteRetryability::Permanent,
            ),
            (
                rusqlite::ffi::SQLITE_BUSY,
                BatchWriteErrorClass::Busy,
                BatchWriteRetryability::Transient,
            ),
            (
                rusqlite::ffi::SQLITE_LOCKED,
                BatchWriteErrorClass::Busy,
                BatchWriteRetryability::Transient,
            ),
            (
                rusqlite::ffi::SQLITE_INTERRUPT,
                BatchWriteErrorClass::Cancelled,
                BatchWriteRetryability::Transient,
            ),
            (
                rusqlite::ffi::SQLITE_CORRUPT,
                BatchWriteErrorClass::Driver,
                BatchWriteRetryability::Unknown,
            ),
        ];

        for (code, expected_class, expected_retryability) in cases {
            assert_eq!(
                classify_batch_sqlite_error(&sqlite_failure(code)),
                (expected_class, expected_retryability)
            );
        }

        assert_eq!(
            classify_batch_sqlite_error(&rusqlite::Error::InvalidQuery),
            (
                BatchWriteErrorClass::Unknown,
                BatchWriteRetryability::Unknown
            )
        );
    }
}

#[cfg(test)]
mod pooled_read_admission_tests {
    use super::*;
    use crate::pool::PoolConfig;
    use std::time::Duration;

    const CHECKOUT_TIMEOUT: Duration = Duration::from_secs(3);
    const QUEUE_SETTLE: Duration = Duration::from_millis(250);
    const WAITING_READS: u64 = 3;

    /// A runtime whose blocking pool holds one thread, so a pooled read that
    /// parks it makes every other blocking task wait its turn.
    fn single_blocking_thread_runtime() -> tokio::runtime::Runtime {
        tokio::runtime::Builder::new_multi_thread()
            .worker_threads(2)
            .max_blocking_threads(1)
            .enable_all()
            .build()
            .expect("test runtime")
    }

    /// A file-backed pool with one reader permit and a bounded checkout wait.
    fn single_reader_pool(dir: &tempfile::TempDir) -> Arc<ConnectionPool> {
        let config = PoolConfig {
            path: Some(dir.path().join("pooled_read_admission.db")),
            max_readers: 1,
            checkout_timeout: CHECKOUT_TIMEOUT,
            ..PoolConfig::default()
        };
        Arc::new(ConnectionPool::new(config).expect("open the single-reader pool"))
    }

    /// Reads that must queue for the sole permit wait as tasks. The permit is
    /// held by a checked-out guard on the test task, because a read that
    /// blocked inside the runtime would itself take the only blocking thread.
    #[test]
    fn queued_pooled_reads_leave_the_blocking_pool_free_and_still_time_out() {
        let runtime = single_blocking_thread_runtime();
        runtime.block_on(async {
            let dir = tempfile::tempdir().unwrap();
            let pool = single_reader_pool(&dir);
            let held_reader = pool.reader().expect("hold the sole reader permit");

            let waiting: Vec<_> = (0..WAITING_READS)
                .map(|_| {
                    tokio::spawn(run_pooled_store_read(
                        Arc::clone(&pool),
                        StorageCapability::Entities,
                        "queued_read",
                        |_conn| Ok(()),
                    ))
                })
                .collect();
            tokio::time::sleep(QUEUE_SETTLE).await;

            let probe = tokio::time::timeout(
                Duration::from_secs(1),
                tokio::task::spawn_blocking(|| "probe ran"),
            )
            .await
            .expect("an unrelated blocking task must not wait behind queued pooled reads")
            .expect("probe task panicked");
            assert_eq!(probe, "probe ran");
            assert!(
                waiting.iter().all(|read| !read.is_finished()),
                "the probe must complete while every queued read is still waiting"
            );

            for read in waiting {
                let error = read.await.expect("read task panicked").unwrap_err();
                assert!(
                    matches!(error, StorageError::AdmissionTimeout { .. }),
                    "a queued read must fail with the retryable AdmissionTimeout, got {error:?}"
                );
            }
            drop(held_reader);
            let snapshot = pool.reader_acquisition_snapshot();
            assert_eq!(snapshot.checkout_timeouts, WAITING_READS);
            assert_eq!(snapshot.available_reader_admission_slots, 1);
        });
    }

    /// Cancelling a queued read releases it without waiting for the blocking
    /// pool or the checkout timeout, and takes no permit from the read behind it.
    #[test]
    fn cancelling_a_queued_pooled_read_returns_at_once_and_leaks_no_permit() {
        let runtime = single_blocking_thread_runtime();
        runtime.block_on(async {
            let dir = tempfile::tempdir().unwrap();
            let pool = single_reader_pool(&dir);
            let held_reader = pool.reader().expect("hold the sole reader permit");

            let surviving = tokio::spawn(run_pooled_store_read(
                Arc::clone(&pool),
                StorageCapability::Entities,
                "surviving_read",
                |_conn| Ok(()),
            ));
            // The surviving read queues first, so it is the one that would hold
            // the lone blocking thread while the cancelled read waits behind it.
            tokio::time::sleep(QUEUE_SETTLE).await;
            let (cancel_tx, cancel_rx) = tokio::sync::watch::channel(false);
            let cancelled_read = run_pooled_store_read(
                Arc::clone(&pool),
                StorageCapability::Entities,
                "cancelled_read",
                |_conn| Ok(()),
            );
            let cancelled = tokio::spawn(khive_storage::scope_request_read_cancellation(
                cancel_rx,
                cancelled_read,
            ));
            tokio::time::sleep(QUEUE_SETTLE).await;
            cancel_tx.send(true).unwrap();

            let error = tokio::time::timeout(Duration::from_secs(1), cancelled)
                .await
                .expect("a cancelled queued read waited for the blocking pool or the timeout")
                .expect("cancelled read task panicked")
                .unwrap_err();
            assert!(
                matches!(error, StorageError::Timeout { .. }),
                "a cancelled queued read must be the non-retryable Timeout, got {error:?}"
            );
            assert!(
                !surviving.is_finished(),
                "the read that was not cancelled must keep waiting for its permit"
            );

            drop(held_reader);
            surviving
                .await
                .expect("surviving read task panicked")
                .expect("the surviving read must be admitted once the permit is free");
            let snapshot = pool.reader_acquisition_snapshot();
            assert_eq!(snapshot.available_reader_admission_slots, 1);
            assert_eq!(snapshot.checkout_timeouts, 0);
        });
    }
}