khive-db 0.11.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
use super::off_worker::run_checkpoint_core_off_worker;
use super::tests::{checkpoint_conn, file_pool, wait_for};
use super::{
    checkpoint_once_core, run_checkpoint_task, truncate_attempts, CheckpointConfig, TruncateState,
};
use crate::pool::ConnectionPool;
use serial_test::serial;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

/// `run_checkpoint_core_off_worker` moves the same `checkpoint_once_core`
/// call onto a blocking thread; driven on a genuinely multi-threaded
/// runtime against an identically-seeded database with TRUNCATE armed, it
/// must reach the same outcome and hand back the same escalation state as
/// calling the core directly.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[serial(checkpoint_skip_metrics, khive_walpin_sidecar_env)]
async fn checkpoint_core_off_worker_matches_the_direct_call() {
    fn seeded_pool() -> (tempfile::TempDir, Arc<ConnectionPool>) {
        let dir = tempfile::tempdir().expect("tempdir");
        let pool = file_pool(&dir.path().join("checkpoint-off-worker.db"));
        {
            let writer = pool.writer().expect("writer");
            writer
                .conn()
                .execute_batch("CREATE TABLE t (x INTEGER); INSERT INTO t VALUES (1);")
                .expect("seed WAL frames");
        }
        (dir, pool)
    }

    let config = CheckpointConfig {
        truncate_high_water_pages: 0,
        ..CheckpointConfig::default()
    };

    let (_direct_dir, direct_pool) = seeded_pool();
    let direct_conn = checkpoint_conn(&direct_pool);
    let mut direct_state = TruncateState::default();
    let direct = checkpoint_once_core(&direct_pool, &direct_conn, &config, &mut direct_state)
        .expect("direct checkpoint core");

    let (_wrapped_dir, wrapped_pool) = seeded_pool();
    let wrapped_conn = checkpoint_conn(&wrapped_pool);
    let joined = run_checkpoint_core_off_worker(
        Arc::clone(&wrapped_pool),
        wrapped_conn,
        config.clone(),
        TruncateState::default(),
    )
    .await;
    let (_conn, wrapped_state, wrapped) = joined.expect("checkpoint core did not panic");
    let wrapped = wrapped.expect("wrapped checkpoint core");

    assert!(
        direct.wal_pages.is_some(),
        "the seeded WAL must yield a PASSIVE observation on the direct path"
    );
    assert!(
        direct_state.last_attempt.is_some(),
        "the armed TRUNCATE must have been attempted on the direct path"
    );
    assert_eq!(
        direct.wal_pages, wrapped.wal_pages,
        "the offloaded call must observe the same WAL pages as the direct call"
    );
    assert_eq!(
        direct.unavailable_reason, wrapped.unavailable_reason,
        "the offloaded call must classify the PASSIVE row like the direct call"
    );
    assert_eq!(
        direct.sidecar_attribution.is_some(),
        wrapped.sidecar_attribution.is_some(),
        "the offloaded call must hand back the same attribution request shape"
    );
    assert_eq!(
        direct_state.last_attempt.is_some(),
        wrapped_state.last_attempt.is_some(),
        "the escalation state returned by the offloaded call must record the attempt"
    );
    assert_eq!(
        direct_state.consecutive_failures, wrapped_state.consecutive_failures,
        "the offloaded call must leave the same TRUNCATE failure streak"
    );
}

/// An armed TRUNCATE busy-waits inside SQLite for up to
/// `truncate_busy_timeout` while a reader pins the WAL. The checkpoint
/// task runs that call on a blocking thread, so a runtime with a single
/// worker keeps polling its other tasks meanwhile: a 10 ms ticker beside
/// the task must never see a gap anywhere near the busy timeout.
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
#[serial(checkpoint_skip_metrics, khive_walpin_sidecar_env)]
async fn truncate_busy_wait_does_not_starve_a_single_worker_runtime() {
    const TRUNCATE_BUSY: Duration = Duration::from_millis(1_000);

    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("truncate-starvation.db");
    let pool = file_pool(&path);
    {
        let writer = pool.writer().expect("writer");
        writer
            .conn()
            .execute_batch("CREATE TABLE t (x INTEGER); INSERT INTO t VALUES (1);")
            .expect("seed WAL frames");
    }
    // A read snapshot that predates the next commit: TRUNCATE has to wait
    // for it, so the busy wait below runs for its whole timeout.
    let reader = rusqlite::Connection::open(&path).expect("open reader");
    reader
        .execute_batch("BEGIN DEFERRED")
        .expect("begin read transaction");
    reader
        .query_row("SELECT COUNT(*) FROM t", [], |row| row.get::<_, i64>(0))
        .expect("materialize the read snapshot");
    {
        let writer = pool.writer().expect("writer");
        writer
            .conn()
            .execute("INSERT INTO t VALUES (2)", [])
            .expect("append a WAL frame behind the reader snapshot");
    }

    let stop = Arc::new(AtomicBool::new(false));
    let ticker_stop = Arc::clone(&stop);
    let ticker = tokio::spawn(async move {
        let mut last = Instant::now();
        let mut max_gap = Duration::ZERO;
        while !ticker_stop.load(Ordering::SeqCst) {
            tokio::time::sleep(Duration::from_millis(10)).await;
            let now = Instant::now();
            max_gap = max_gap.max(now.duration_since(last));
            last = now;
        }
        max_gap
    });
    // Let the ticker settle before the checkpoint task shares its worker.
    tokio::time::sleep(Duration::from_millis(50)).await;

    let before = truncate_attempts();
    let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(());
    let started = Instant::now();
    let task = tokio::spawn(run_checkpoint_task(
        Arc::clone(&pool),
        CheckpointConfig {
            interval: Duration::from_secs(60),
            truncate_high_water_pages: 0,
            truncate_busy_timeout: TRUNCATE_BUSY,
            ..CheckpointConfig::default()
        },
        None,
        shutdown_rx,
        true,
    ));
    let attempted = wait_for(Duration::from_secs(10), || truncate_attempts() > before).await;
    let waited = started.elapsed();
    stop.store(true, Ordering::SeqCst);
    let max_gap = ticker.await.expect("ticker task");

    shutdown_tx.send(()).expect("send shutdown signal");
    tokio::time::timeout(Duration::from_secs(5), task)
        .await
        .expect("checkpoint task should exit after shutdown")
        .expect("checkpoint task panicked");
    reader
        .execute_batch("ROLLBACK")
        .expect("release the read transaction");

    assert!(
        attempted,
        "the armed TRUNCATE attempt must complete within the deadline"
    );
    assert!(
        waited >= TRUNCATE_BUSY / 2,
        "fixture invalid: the armed TRUNCATE finished after {waited:?} without waiting out \
         its {TRUNCATE_BUSY:?} busy timeout on the pinned reader"
    );
    assert!(
        max_gap < TRUNCATE_BUSY / 2,
        "a single-worker runtime went {max_gap:?} between 10 ms ticker wakeups while the \
         TRUNCATE busy wait ran; the checkpoint call must not hold the worker thread for \
         the {TRUNCATE_BUSY:?} busy timeout"
    );
}

#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn scheduled_checkpoint_and_sidecar_recovery_bypass_an_active_floor() {
    let home = tempfile::tempdir().unwrap();
    if crate::test_process::run_in_child(|command| {
        for (key, _) in std::env::vars_os() {
            if key.to_string_lossy().starts_with("KHIVE_") {
                command.env_remove(key);
            }
        }
        command
            .env("HOME", home.path())
            .env("USERPROFILE", home.path())
            .env("KHIVE_TEST_HARNESS", "1")
            .env("KHIVE_WRITER_TIMEOUT_SINK_DIR", home.path().join("sink"))
            .env("KHIVE_WALPIN_SIDECAR", "1")
            .env("KHIVE_WALPIN_CENSUS_BUDGET_MS", "0");
    }) {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path().canonicalize().unwrap();
    let path = root.join("scheduled-floor.db");
    let locks = root.join("locks");
    let mut pool = ConnectionPool::new(crate::pool::PoolConfig {
        path: Some(path.clone()),
        volume_lock_dir: Some(locks.clone()),
        write_queue_enabled: Some(false),
        disk_guard_config: Some(
            crate::disk_guard_config::DiskGuardEnvironment::default()
                .resolve(Some(0), Some(100))
                .unwrap(),
        ),
        ..crate::pool::PoolConfig::for_test()
    })
    .unwrap();
    pool.set_test_write_admission(0, |_| Ok(0));
    pool.writer()
        .unwrap()
        .execute_batch("CREATE TABLE payload (id INTEGER); INSERT INTO payload VALUES (1)")
        .unwrap();
    let wal = path.with_extension("db-wal");
    assert!(std::fs::metadata(&wal).unwrap().len() > 0);
    let probes = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let forbid = Arc::new(std::sync::atomic::AtomicBool::new(false));
    let observed = Arc::clone(&probes);
    let forbidden = Arc::clone(&forbid);
    pool.set_test_write_admission(100, move |_| {
        observed.fetch_add(1, Ordering::SeqCst);
        assert!(
            !forbidden.load(Ordering::SeqCst),
            "recovery must not sample capacity"
        );
        Ok(0)
    });
    assert!(matches!(
        pool.writer(),
        Err(crate::SqliteError::CapacityFloor {
            available_bytes: 0,
            floor_bytes: 100,
            ..
        })
    ));
    assert_eq!(probes.load(Ordering::SeqCst), 1);
    assert!(
        locks.is_dir(),
        "ordinary admission must create its private lease directory"
    );
    std::fs::remove_dir_all(&locks).unwrap();
    probes.store(0, Ordering::SeqCst);
    forbid.store(true, Ordering::SeqCst);
    let before = pool.writer_acquisition_snapshot();
    let sidecar = crate::walpin::sidecar_dir_for(pool.canonical_path().unwrap());
    let dead_pid = 2_000_000_000;
    assert!(!crate::walpin::is_process_alive(dead_pid));
    crate::walpin::write_beacon(
        &sidecar,
        &crate::walpin::WalpinBeacon {
            pid: dead_pid,
            process_role: "session".into(),
            started_at: 1,
            sweep_interval_ms: 5_000,
        },
    )
    .unwrap();
    let dead_beacon = crate::walpin::beacon_path(&sidecar, dead_pid);
    let live_beacon = crate::walpin::beacon_path(&sidecar, std::process::id());
    assert!(dead_beacon.exists());
    let pool = Arc::new(pool);
    let config = CheckpointConfig {
        interval: Duration::from_millis(10),
        truncate_high_water_pages: 0,
        warn_pages: u64::MAX,
        high_water_pages: u64::MAX,
        ..CheckpointConfig::default()
    };
    let (shutdown, shutdown_rx) = tokio::sync::watch::channel(());
    let mut task = tokio::spawn(run_checkpoint_task(
        Arc::clone(&pool),
        config,
        None,
        shutdown_rx,
        false,
    ));
    let progressed = wait_for(Duration::from_secs(10), || {
        std::fs::metadata(&wal).is_ok_and(|m| m.len() == 0)
            && !dead_beacon.exists()
            && live_beacon.exists()
    })
    .await;
    // A healthy tick must repair a lost registration rather than permanently
    // treating its cached beacon_registered state as proof of a file.
    let removed = progressed && std::fs::remove_file(&live_beacon).is_ok();
    let recovered = removed && wait_for(Duration::from_secs(10), || live_beacon.exists()).await;
    let _ = shutdown.send(());
    let joined = tokio::time::timeout(Duration::from_secs(10), &mut task).await;
    if joined.is_err() {
        task.abort();
        let _ = task.await;
        panic!("checkpoint task did not finish shutdown");
    }
    joined.unwrap().expect("checkpoint task panicked");
    assert!(
        progressed,
        "armed scheduled TRUNCATE and dead-sidecar cleanup must run below floor"
    );
    assert!(
        recovered,
        "the same scheduled owner must restore its missing beacon below floor"
    );
    assert_eq!(probes.load(Ordering::SeqCst), 0);
    assert!(!locks.exists(), "recovery must not acquire a volume lease");
    // The task's startup ownership claim takes exactly one bounded pooled
    // checkout, below the reserve and without a lease; recovery takes no other.
    let mut expected = before;
    expected.acquisitions += 1;
    expected.pooled_acquisitions += 1;
    assert_eq!(pool.writer_acquisition_snapshot(), expected);
    assert_eq!(
        pool.reader()
            .unwrap()
            .query_row("SELECT count(*) FROM payload", [], |row| row
                .get::<_, i64>(0))
            .unwrap(),
        1
    );
}

#[tokio::test]
async fn checkpoint_once_bypasses_an_active_floor_on_its_real_dedicated_connection() {
    let home = tempfile::tempdir().unwrap();
    if crate::test_process::run_in_child(|command| {
        for (key, _) in std::env::vars_os() {
            if key.to_string_lossy().starts_with("KHIVE_") {
                command.env_remove(key);
            }
        }
        command
            .env("HOME", home.path())
            .env("USERPROFILE", home.path())
            .env("KHIVE_TEST_HARNESS", "1")
            .env("KHIVE_WRITER_TIMEOUT_SINK_DIR", home.path().join("sink"))
            .env("KHIVE_WALPIN_SIDECAR", "1")
            .env("KHIVE_WALPIN_CENSUS_BUDGET_MS", "0");
    }) {
        return;
    }
    let dir = tempfile::tempdir().unwrap();
    let root = dir.path().canonicalize().unwrap();
    let path = root.join("once-floor.db");
    let locks = root.join("locks");
    let mut pool = ConnectionPool::new(crate::pool::PoolConfig {
        path: Some(path.clone()),
        volume_lock_dir: Some(locks.clone()),
        write_queue_enabled: Some(false),
        disk_guard_config: Some(
            crate::disk_guard_config::DiskGuardEnvironment::default()
                .resolve(Some(0), Some(100))
                .unwrap(),
        ),
        ..crate::pool::PoolConfig::for_test()
    })
    .unwrap();
    pool.set_test_write_admission(0, |_| Ok(0));
    pool.writer()
        .unwrap()
        .execute_batch("CREATE TABLE payload (id INTEGER); INSERT INTO payload VALUES (1)")
        .unwrap();
    let wal = path.with_extension("db-wal");
    assert!(std::fs::metadata(&wal).unwrap().len() > 0);
    let probes = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let forbid = Arc::new(std::sync::atomic::AtomicBool::new(false));
    let observed = Arc::clone(&probes);
    let forbidden = Arc::clone(&forbid);
    pool.set_test_write_admission(100, move |_| {
        observed.fetch_add(1, Ordering::SeqCst);
        assert!(
            !forbidden.load(Ordering::SeqCst),
            "recovery must not sample capacity"
        );
        Ok(0)
    });
    assert!(matches!(
        pool.writer(),
        Err(crate::SqliteError::CapacityFloor {
            available_bytes: 0,
            floor_bytes: 100,
            ..
        })
    ));
    assert_eq!(probes.load(Ordering::SeqCst), 1);
    assert!(
        locks.is_dir(),
        "ordinary admission must create its private lease directory"
    );
    std::fs::remove_dir_all(&locks).unwrap();
    probes.store(0, Ordering::SeqCst);
    forbid.store(true, Ordering::SeqCst);
    let before = pool.writer_acquisition_snapshot();
    let mut dedicated = super::CheckpointConnection::new();
    let conn = dedicated
        .ensure_open(&pool)
        .expect("dedicated recovery open bypasses admission");
    let mut state = TruncateState::default();
    let pages = super::checkpoint_once(
        &pool,
        conn,
        &CheckpointConfig {
            truncate_high_water_pages: 0,
            ..CheckpointConfig::default()
        },
        &mut state,
    )
    .unwrap();
    assert!(pages > 0, "the PASSIVE pass must observe the seeded WAL");
    assert!(
        state.last_attempt.is_some(),
        "the threshold must actually arm TRUNCATE"
    );
    assert_eq!(std::fs::metadata(&wal).unwrap().len(), 0);
    assert_eq!(probes.load(Ordering::SeqCst), 0);
    assert!(!locks.exists());
    assert_eq!(pool.writer_acquisition_snapshot(), before);
}