khive-runtime 0.13.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
/// Test 2: `wal_pages` reflects a real
/// checkpoint observation after writes, deterministically forced via a
/// direct `checkpoint_once` call rather than waiting on the async
/// periodic task.
#[tokio::test]
async fn metrics_snapshot_wal_pages_reflects_recent_write() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("metrics_wal_test.db");
    let pool = Arc::new(
        ConnectionPool::new(khive_db::PoolConfig {
            path: Some(path),
            ..khive_db::PoolConfig::for_test()
        })
        .expect("pool open"),
    );

    {
        let writer = pool.try_writer().expect("writer");
        writer
            .conn()
            .execute_batch(
                "CREATE TABLE t (x INTEGER); \
                     INSERT INTO t VALUES (1); \
                     INSERT INTO t VALUES (2);",
            )
            .expect("seed writes");
    }

    let dedicated_conn = pool
        .open_standalone_writer()
        .expect("open dedicated checkpoint connection");
    khive_db::checkpoint_once(
        &pool,
        &dedicated_conn,
        &CheckpointConfig::default(),
        &mut khive_db::checkpoint::TruncateState::default(),
    )
    .expect("checkpoint_once must observe on a healthy dedicated connection");

    let dispatcher = MockDispatch {
        namespace: "local".to_string(),
        config_id: "cfg-wal".to_string(),
        dispatch_calls: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        pool: Some(pool),
        dispatch_err: None,
    };

    let snapshot = build_metrics_snapshot(&dispatcher);
    assert!(
        snapshot.wal_pages.is_some(),
        "wal_pages must be observed after a real checkpoint tick, got {snapshot:?}"
    );
    assert_eq!(snapshot.wal_log_frames, snapshot.wal_pages);
    assert!(snapshot.wal_checkpointed_frames.is_some());
    assert!(snapshot.wal_pending_frames.is_some());
    assert!(snapshot.wal_physical_bytes.is_some());
    assert!(snapshot.wal_observed_at_unix_ms.is_some());
    assert_eq!(snapshot.wal_checkpoint_stores.len(), 1);
    assert_eq!(snapshot.wal_checkpoint_stores[0].store_id, "main");
    assert_eq!(snapshot.wal_checkpoint_stores[0].timing.ticks, 1);
    // The snapshot carries the checkpoint-pressure fields read-only
    // (no mutation path reachable through `MetricsSnapshot`/`DaemonRequestFrame`);
    // an observed tick (not a skip) must report a zero-length skip streak.
    assert_eq!(
        snapshot.wal_checkpoint_consecutive_skips, 0,
        "an observed (non-skipped) tick must report zero consecutive skips, got {snapshot:?}"
    );
}

#[derive(Clone)]
struct CheckpointMetricsDispatch {
    main: Option<Arc<ConnectionPool>>,
    secondaries: Vec<Arc<ConnectionPool>>,
}

#[async_trait]
impl DaemonDispatch for CheckpointMetricsDispatch {
    fn plan(&self, _ops: &str) -> String {
        panic!("metrics must not plan")
    }
    async fn dispatch(
        &self,
        _ops: String,
        _presentation: Option<String>,
        _presentation_per_op: Option<Vec<Option<String>>>,
        _format: Option<String>,
        _format_per_op: Option<Vec<Option<String>>>,
        _from_wire: bool,
        _identity: Option<RequestIdentity>,
    ) -> Result<String, String> {
        panic!("metrics must not dispatch")
    }
    async fn warm_all(&self) {}
    fn namespace(&self) -> &str {
        "local"
    }
    fn config_id(&self) -> &str {
        "checkpoint-metrics"
    }
    fn pool_for_checkpoint(&self) -> Option<Arc<ConnectionPool>> {
        self.main.clone()
    }
    fn secondary_pools_for_checkpoint(&self) -> Vec<Arc<ConnectionPool>> {
        self.secondaries.clone()
    }
}

#[tokio::test]
#[serial(checkpoint_skip_metrics)]
async fn metrics_checkpoint_timing_keeps_stores_separate_and_scrapes_read_only() {
    let dir = tempfile::tempdir().unwrap();
    let mut pools = Vec::new();
    for label in ["primary", "secondary"] {
        let directory = dir.path().join(label);
        std::fs::create_dir(&directory).unwrap();
        let pool = Arc::new(
            ConnectionPool::new(khive_db::PoolConfig {
                path: Some(directory.join("same.db")),
                ..khive_db::PoolConfig::for_test()
            })
            .unwrap(),
        );
        pool.try_writer()
            .unwrap()
            .conn()
            .execute_batch("CREATE TABLE t (x INTEGER); INSERT INTO t VALUES (1);")
            .unwrap();
        pools.push(pool);
    }
    let dispatcher = CheckpointMetricsDispatch {
        main: Some(Arc::clone(&pools[0])),
        secondaries: vec![Arc::clone(&pools[1])],
    };
    let before = build_metrics_snapshot(&dispatcher);
    assert_eq!(before.wal_checkpoint_stores.len(), 2);
    assert!(before
        .wal_checkpoint_stores
        .iter()
        .all(|store| store.timing.ticks == 0));
    for (index, pool) in pools.iter().enumerate() {
        let conn = pool.open_standalone_writer().unwrap();
        for _ in 0..=index {
            khive_db::checkpoint_once(
                pool,
                &conn,
                &CheckpointConfig {
                    truncate_high_water_pages: u64::MAX,
                    ..CheckpointConfig::default()
                },
                &mut khive_db::checkpoint::TruncateState::default(),
            )
            .unwrap();
        }
    }
    let mut request = base_request_frame("checkpoint-metrics");
    request.metrics_only = true;
    let snapshot = round_trip(dispatcher.clone(), &request)
        .await
        .metrics
        .unwrap();
    let stores = &snapshot.wal_checkpoint_stores;
    assert_eq!(stores.len(), 2);
    assert_eq!(stores[0].store_id, "main");
    assert_eq!(stores[0].role, "main");
    assert_eq!(stores[1].store_id, "secondary:0");
    assert_eq!(stores[1].role, "secondary");
    for store in stores {
        assert_eq!(
            store.database.as_deref(),
            Some("same.db"),
            "wire label must omit directories"
        );
        assert!(store.timing.elapsed_us_max <= store.timing.elapsed_us_sum);
    }
    assert_eq!(stores[0].timing.ticks, 1);
    assert_eq!(stores[1].timing.ticks, 2);
    let again = build_metrics_snapshot(&dispatcher);
    assert_eq!(
        again.wal_checkpoint_stores, *stores,
        "scraping must not checkpoint"
    );
    let secondary_only = build_metrics_snapshot(&CheckpointMetricsDispatch {
        main: None,
        secondaries: vec![Arc::clone(&pools[1])],
    });
    assert_eq!(secondary_only.wal_checkpoint_stores.len(), 1);
    assert_eq!(
        secondary_only.wal_checkpoint_stores[0].store_id,
        "secondary:0"
    );
    assert_eq!(secondary_only.wal_checkpoint_stores[0].timing.ticks, 2);
    assert!(build_metrics_snapshot(&CheckpointMetricsDispatch {
        main: None,
        secondaries: vec![]
    })
    .wal_checkpoint_stores
    .is_empty());
}

#[test]
fn metrics_checkpoint_timing_serde_is_additive_and_round_trips() {
    let snapshot = MetricsSnapshot {
        wal_checkpoint_stores: vec![CheckpointStoreMetrics {
            store_id: "secondary:0".into(),
            role: "secondary".into(),
            database: Some("memory.db".into()),
            timing: khive_db::checkpoint::CheckpointTiming {
                ticks: 7,
                elapsed_us_sum: 123,
                elapsed_us_max: 50,
                busy_ticks: 2,
                error_ticks: 1,
            },
        }],
        ..MetricsSnapshot::default()
    };
    let wire = serde_json::to_value(&snapshot).unwrap();
    let store = &wire["wal_checkpoint_stores"][0];
    assert_eq!(
        store,
        &serde_json::json!({
            "store_id": "secondary:0", "role": "secondary", "database": "memory.db",
            "ticks": 7, "elapsed_us_sum": 123, "elapsed_us_max": 50, "busy_ticks": 2, "error_ticks": 1,
        })
    );
    assert_eq!(
        serde_json::from_value::<MetricsSnapshot>(wire.clone()).unwrap(),
        snapshot
    );
    let mut old_wire = wire.clone();
    old_wire
        .as_object_mut()
        .unwrap()
        .remove("wal_checkpoint_stores");
    let old = serde_json::from_value::<MetricsSnapshot>(old_wire).unwrap();
    assert!(
        old.wal_checkpoint_stores.is_empty(),
        "old snapshot must default the new vector"
    );
    let partial = serde_json::from_value::<CheckpointStoreMetrics>(serde_json::json!({
        "store_id": "main", "role": "main"
    }))
    .unwrap();
    assert_eq!(
        partial.timing,
        khive_db::checkpoint::CheckpointTiming::default()
    );
    assert_eq!(partial.database, None);
    #[derive(serde::Deserialize)]
    struct LegacyMetrics {
        wal_pages: Option<u64>,
        open_tx_count: usize,
    }
    let legacy: LegacyMetrics = serde_json::from_value(wire).unwrap();
    assert_eq!(legacy.wal_pages, None);
    assert_eq!(legacy.open_tx_count, 0);
}

#[tokio::test]
async fn metrics_snapshot_exposes_decomposed_writer_stages() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join("metrics_writer_stage_test.db");
    let pool = Arc::new(
        ConnectionPool::new(khive_db::PoolConfig {
            path: Some(path),
            ..khive_db::PoolConfig::for_test()
        })
        .expect("pool open"),
    );
    {
        let writer = pool.try_writer().unwrap();
        writer
            .conn()
            .execute_batch("CREATE TABLE t (id INTEGER PRIMARY KEY)")
            .unwrap();
    }
    let handle = pool
        .writer_task_handle()
        .unwrap()
        .expect("file-backed default writer task");
    handle
        .send(|conn| {
            std::thread::sleep(std::time::Duration::from_millis(30));
            conn.execute("INSERT INTO t VALUES (1)", [])
                .map_err(|error| khive_storage::error::StorageError::Pool {
                    operation: "metrics_writer_stage_test".into(),
                    message: error.to_string(),
                })
        })
        .await
        .unwrap();

    let dispatcher = MockDispatch {
        namespace: "local".to_string(),
        config_id: "cfg-writer-stages".to_string(),
        dispatch_calls: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        pool: Some(pool),
        dispatch_err: None,
    };
    let snapshot = build_metrics_snapshot(&dispatcher);
    assert!(snapshot.write_last_queue_wait_micros.is_some());
    assert!(snapshot.write_last_transaction_acquire_micros.is_some());
    assert!(snapshot.write_last_commit_micros.is_some());
    assert!(
        snapshot.write_last_body_micros >= Some(25_000),
        "synthetic delay must be attributed to the body: {snapshot:?}"
    );
    assert!(snapshot.write_last_total_micros >= snapshot.write_last_body_micros);
    assert!(snapshot.write_last_observed_at_unix_ms.is_some());
}

/// Test 3: the tx-pin oracle. The registry is process-global, so an
/// unrelated transaction can depart between snapshots and exactly offset
/// this test's registration. Keep an owned handle live and assert the
/// resulting count floor instead of comparing two points in time.
#[test]
#[serial(tx_registry)]
fn metrics_snapshot_reflects_open_transaction_registry() {
    let dispatcher = MockDispatch {
        namespace: "local".to_string(),
        config_id: "cfg-tx".to_string(),
        dispatch_calls: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        pool: None,
        dispatch_err: None,
    };

    let departing_handle = khive_storage::tx_registry::register(Some(
        "daemon_metrics_snapshot_departing_test_tx".to_string(),
    ));
    let before = build_metrics_snapshot(&dispatcher).open_tx_count;
    assert!(before >= 1);

    let handle = khive_storage::tx_registry::register(Some(
        "daemon_metrics_snapshot_owned_test_tx".to_string(),
    ));
    drop(departing_handle);

    let during = build_metrics_snapshot(&dispatcher);
    assert!(
        during.open_tx_count >= 1,
        "open_tx_count must reflect the live owned transaction despite registry churn: \
             churn_baseline={before} during={}",
        during.open_tx_count
    );
    assert!(
        during.oldest_pinned_tx_micros.is_some(),
        "oldest_pinned_tx_micros must be Some while a transaction is open"
    );

    drop(handle);
    assert!(
        !khive_storage::tx_registry::snapshot()
            .iter()
            .any(|(_, label)| label.as_deref() == Some("daemon_metrics_snapshot_owned_test_tx")),
        "the owned registry entry must disappear when its handle is dropped"
    );
}

/// Test 4: write-queue depth is flag-gated
/// on `PoolConfig::write_queue_enabled` (the `KHIVE_WRITE_QUEUE=1`
/// setting), never on a specific depth value (racy under concurrency).
#[tokio::test]
async fn metrics_snapshot_write_queue_depth_flag_gated() {
    let dir = tempfile::tempdir().expect("tempdir");

    let enabled_pool = Arc::new(
        ConnectionPool::new(khive_db::PoolConfig {
            path: Some(dir.path().join("wq_enabled.db")),
            write_queue_enabled: Some(true),
            ..khive_db::PoolConfig::for_test()
        })
        .expect("pool open"),
    );
    let enabled_dispatcher = MockDispatch {
        namespace: "local".to_string(),
        config_id: "cfg-wq-on".to_string(),
        dispatch_calls: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        pool: Some(enabled_pool),
        dispatch_err: None,
    };
    let snapshot_on = build_metrics_snapshot(&enabled_dispatcher);
    assert!(
        snapshot_on.write_queue_depth.is_some(),
        "write_queue_depth must be Some when write_queue_enabled=true, got {snapshot_on:?}"
    );
    assert!(snapshot_on.write_queue_capacity.is_some());

    let disabled_pool = Arc::new(
        ConnectionPool::new(khive_db::PoolConfig {
            path: Some(dir.path().join("wq_disabled.db")),
            write_queue_enabled: Some(false),
            ..khive_db::PoolConfig::for_test()
        })
        .expect("pool open"),
    );
    let disabled_dispatcher = MockDispatch {
        namespace: "local".to_string(),
        config_id: "cfg-wq-off".to_string(),
        dispatch_calls: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        pool: Some(disabled_pool),
        dispatch_err: None,
    };
    let snapshot_off = build_metrics_snapshot(&disabled_dispatcher);
    assert!(
        snapshot_off.write_queue_depth.is_none(),
        "write_queue_depth must be None when write_queue_enabled=false, got {snapshot_off:?}"
    );
    assert!(snapshot_off.write_queue_capacity.is_none());

    // No pool at all (in-memory/poolless dispatcher): also None.
    let no_pool_dispatcher = MockDispatch {
        namespace: "local".to_string(),
        config_id: "cfg-no-pool".to_string(),
        dispatch_calls: Arc::new(std::sync::atomic::AtomicUsize::new(0)),
        pool: None,
        dispatch_err: None,
    };
    let snapshot_no_pool = build_metrics_snapshot(&no_pool_dispatcher);
    assert!(snapshot_no_pool.write_queue_depth.is_none());
    assert!(snapshot_no_pool.write_queue_capacity.is_none());
}