khive-pack-knowledge 0.10.0

Knowledge verb pack — lore corpus (atoms/domains), TF-IDF retrieval, concept registration
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use std::{future::Future, time::Duration};

use khive_storage::{capture_request_read_context, StorageError};
use serde::Serialize;
use tokio::time::Instant;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum LexicalPass {
    Full,
    #[serde(rename = "subquery_1")]
    Subquery1,
    #[serde(rename = "subquery_2")]
    Subquery2,
}

impl LexicalPass {
    pub(super) fn label(self) -> &'static str {
        match self {
            Self::Full => "full",
            Self::Subquery1 => "subquery_1",
            Self::Subquery2 => "subquery_2",
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum LexicalPhase {
    ReaderOpen,
    TermFrequency,
    PhaseARowids,
    PhaseBHydration,
    EligibilityFallback,
    NamespaceMembership,
    NamespaceExistence,
    ExactNameProbe,
}

impl LexicalPhase {
    pub(super) fn label(self) -> &'static str {
        match self {
            Self::ReaderOpen => "reader_open",
            Self::TermFrequency => "term_frequency",
            Self::PhaseARowids => "phase_a_rowids",
            Self::PhaseBHydration => "phase_b_hydration",
            Self::EligibilityFallback => "eligibility_fallback",
            Self::NamespaceMembership => "namespace_membership",
            Self::NamespaceExistence => "namespace_existence",
            Self::ExactNameProbe => "exact_name_probe",
        }
    }

    pub(super) fn public(self) -> bool {
        // Later phases depend on global-index matches before namespace filtering.
        // Even their presence, without row counts, can reveal foreign matches.
        matches!(self, Self::ReaderOpen | Self::TermFrequency)
    }
}

/// Which budget governed the read that timed out.
///
/// The lexical stage runs one read under a budget tighter than its own: the
/// rarity probe, which spends a quarter of the stage budget deciding which
/// term to query first (`rarity_probe_budget`). Its expiry and a cut candidate
/// fetch are not the same event. The probe produces an ORDERING, every term is
/// queried either way, and the fetch that follows still gets the rest of the
/// budget — so the response is complete and the optimization was skipped. A
/// cut fetch is a response that is missing rows it would otherwise have had.
///
/// Callers pass this in rather than it being inferred from the phase. Today
/// `TermFrequency` reaches the stage only from the probe, so the phase alone
/// would answer correctly, and it would go on answering correctly right up
/// until someone adds a second term-frequency read under the stage budget —
/// at which point every such read would silently be reported as a skipped
/// optimization. The bound is a property of the call, so the call states it.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum LexicalBound {
    /// The read ran under the lexical stage's own budget.
    Stage,
    /// The read ran under the rarity probe's tighter nested budget.
    OrderingProbe,
}

impl LexicalBound {
    pub(super) fn label(self) -> &'static str {
        match self {
            Self::Stage => "stage",
            Self::OrderingProbe => "ordering_probe",
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
pub(super) struct LexicalTimeout {
    pub(super) pass: LexicalPass,
    pub(super) phase: LexicalPhase,
    pub(super) stage_elapsed_ms: u64,
    pub(super) operation_elapsed_ms: u64,
    pub(super) configured_budget_ms: u64,
    pub(super) effective_budget_ms: u64,
    /// Which budget governed this read.
    pub(super) bound: LexicalBound,
    /// The budget that governed THIS read, which is the stage budget only for
    /// a `Stage`-bounded read. `configured_budget_ms` and
    /// `effective_budget_ms` both describe the STAGE at its entry, by their
    /// documented definitions, so on a probe-bounded read they name an
    /// allowance that never applied to the read being reported: a reader
    /// reconstructing the cause sees `stage_elapsed_ms=501` against
    /// `effective_budget_ms=1999` and concludes a 2000 ms budget expired at
    /// 501 ms (issue #2879).
    pub(super) read_budget_ms: u64,
}

fn millis(duration: Duration) -> u64 {
    duration.as_millis().min(u64::MAX as u128) as u64
}

pub(super) struct LexicalStage {
    pass: LexicalPass,
    started: Instant,
    configured_budget_ms: u64,
    effective_budget_ms: u64,
    /// Time spent in reads that did not time out, with each phase's read
    /// count, in the order the phases first ran. A cut read that ran for a
    /// few milliseconds says nothing about where the rest of the stage went;
    /// this does. Only the timeout log record carries it: time spent in the
    /// later phases depends on other namespaces' index matches, so it never
    /// reaches the response.
    completed: Vec<(LexicalPhase, Duration, u32)>,
    pub(super) timeout: Option<LexicalTimeout>,
}

impl LexicalStage {
    pub(super) fn new(pass: LexicalPass, started: Instant, configured: Duration) -> Self {
        let effective = capture_request_read_context()
            .deadline()
            .map(|deadline| {
                deadline
                    .async_at()
                    .saturating_duration_since(Instant::now())
            })
            .unwrap_or(configured)
            .min(configured);
        Self {
            pass,
            started,
            configured_budget_ms: millis(configured),
            effective_budget_ms: millis(effective),
            completed: Vec::new(),
            timeout: None,
        }
    }

    /// Run a read under the stage's own budget.
    ///
    /// The governing bound is the EFFECTIVE budget, not the configured one: a
    /// parent deadline tighter than the stage budget is what actually cuts the
    /// read, and `effective_budget_ms` is already defined as the smaller of the
    /// two. Passing the configured budget here would put a number on the record
    /// that never governed the read, which is the defect this field exists to
    /// remove rather than relocate.
    pub(super) async fn read<T>(
        &mut self,
        phase: LexicalPhase,
        read: impl Future<Output = Result<T, StorageError>>,
    ) -> Result<T, StorageError> {
        let stage_budget = Duration::from_millis(self.effective_budget_ms);
        self.read_bounded(phase, LexicalBound::Stage, stage_budget, read)
            .await
    }

    /// Run a read under `budget`, which the caller has already scoped as a
    /// read deadline. `budget` is recorded, not enforced: the enforcement is
    /// the caller's `scope_request_read_deadline`, and passing it here is what
    /// lets the timeout record name the allowance that actually governed the
    /// read instead of the stage's.
    pub(super) async fn read_bounded<T>(
        &mut self,
        phase: LexicalPhase,
        bound: LexicalBound,
        budget: Duration,
        read: impl Future<Output = Result<T, StorageError>>,
    ) -> Result<T, StorageError> {
        let operation_started = Instant::now();
        #[cfg(test)]
        if let Some(error) = tests::inject_timeout(self.pass, phase).await {
            self.capture_timeout(phase, bound, budget, operation_started);
            return Err(error);
        }
        let result = read.await;
        if matches!(&result, Err(StorageError::Timeout { .. })) {
            self.capture_timeout(phase, bound, budget, operation_started);
        } else {
            self.record_completed(phase, operation_started.elapsed());
        }
        result
    }

    fn record_completed(&mut self, phase: LexicalPhase, elapsed: Duration) {
        match self
            .completed
            .iter_mut()
            .find(|(seen, _, _)| *seen == phase)
        {
            Some((_, total, reads)) => {
                *total += elapsed;
                *reads += 1;
            }
            None => self.completed.push((phase, elapsed, 1)),
        }
    }

    /// `(phase, total_ms, reads)` for every phase with a completed read.
    fn completed_reads(&self) -> Vec<(&'static str, u64, u32)> {
        self.completed
            .iter()
            .map(|&(phase, total, reads)| (phase.label(), millis(total), reads))
            .collect()
    }

    fn capture_timeout(
        &mut self,
        phase: LexicalPhase,
        bound: LexicalBound,
        budget: Duration,
        operation_started: Instant,
    ) {
        let now = Instant::now();
        let detail = LexicalTimeout {
            pass: self.pass,
            phase,
            stage_elapsed_ms: millis(now.saturating_duration_since(self.started)),
            operation_elapsed_ms: millis(now.saturating_duration_since(operation_started)),
            configured_budget_ms: self.configured_budget_ms,
            effective_budget_ms: self.effective_budget_ms,
            bound,
            read_budget_ms: millis(budget),
        };
        tracing::warn!(
            pass = detail.pass.label(),
            phase = detail.phase.label(),
            bound = detail.bound.label(),
            stage_elapsed_ms = detail.stage_elapsed_ms,
            operation_elapsed_ms = detail.operation_elapsed_ms,
            configured_budget_ms = detail.configured_budget_ms,
            effective_budget_ms = detail.effective_budget_ms,
            read_budget_ms = detail.read_budget_ms,
            completed_reads = ?self.completed_reads(),
            "lexical read timed out"
        );
        self.timeout = Some(detail);
    }
}

#[cfg(test)]
pub(super) mod tests {
    use std::sync::{Arc, Mutex};

    use super::*;

    tokio::task_local! {
        static INJECT: (Vec<(LexicalPass, LexicalPhase)>, Duration);
    }

    pub(in crate::knowledge) async fn with_timeout<F: Future>(
        phases: Vec<LexicalPhase>,
        elapsed: Duration,
        future: F,
    ) -> F::Output {
        let targets = phases
            .into_iter()
            .flat_map(|phase| {
                [
                    LexicalPass::Full,
                    LexicalPass::Subquery1,
                    LexicalPass::Subquery2,
                ]
                .map(|pass| (pass, phase))
            })
            .collect();
        with_pass_timeouts(targets, elapsed, future).await
    }

    pub(in crate::knowledge) async fn with_pass_timeouts<F: Future>(
        targets: Vec<(LexicalPass, LexicalPhase)>,
        elapsed: Duration,
        future: F,
    ) -> F::Output {
        INJECT.scope((targets, elapsed), future).await
    }

    pub(super) async fn inject_timeout(
        pass: LexicalPass,
        phase: LexicalPhase,
    ) -> Option<StorageError> {
        let elapsed = INJECT
            .try_with(|(targets, elapsed)| targets.contains(&(pass, phase)).then_some(*elapsed))
            .ok()
            .flatten()?;
        tokio::time::advance(elapsed).await;
        Some(StorageError::Timeout {
            operation: "test.lexical_read".into(),
        })
    }

    #[tokio::test(start_paused = true)]
    async fn earlier_deadline_and_cancellation_keep_the_entry_allowance() {
        for cancel in [false, true] {
            let (tx, rx) = tokio::sync::watch::channel(false);
            khive_storage::scope_request_read_cancellation(
                rx,
                khive_storage::scope_request_read_deadline(Duration::from_millis(100), async {
                    tokio::time::advance(Duration::from_millis(30)).await;
                    let started = Instant::now();
                    khive_storage::scope_request_read_deadline(
                        Duration::from_millis(2000),
                        async {
                            let mut stage = LexicalStage::new(
                                LexicalPass::Full,
                                started,
                                Duration::from_millis(2000),
                            );
                            let elapsed = if cancel { 11 } else { 70 };
                            let result = stage
                                .read(LexicalPhase::TermFrequency, async {
                                    tokio::time::advance(Duration::from_millis(elapsed)).await;
                                    if cancel {
                                        tx.send(true).expect("cancel receiver alive");
                                    }
                                    khive_storage::ensure_request_read_active("test.read")
                                })
                                .await;
                            assert!(matches!(result, Err(StorageError::Timeout { .. })));
                            let detail = stage.timeout.expect("missing timeout detail");
                            assert_eq!(detail.effective_budget_ms, 70);
                            assert_eq!(detail.configured_budget_ms, 2000);
                            assert_eq!(detail.operation_elapsed_ms, elapsed);
                            assert_eq!(detail.stage_elapsed_ms, elapsed);
                            assert!(serde_json::to_value(detail).unwrap().get("cause").is_none());
                        },
                    )
                    .await;
                }),
            )
            .await;
        }
    }

    #[tokio::test(start_paused = true)]
    async fn timings_are_per_operation_and_frozen_before_downstream_work() {
        let started = Instant::now();
        khive_storage::scope_request_read_deadline(Duration::from_millis(2000), async {
            let mut stage =
                LexicalStage::new(LexicalPass::Full, started, Duration::from_millis(2000));
            stage
                .read(LexicalPhase::TermFrequency, async {
                    tokio::time::advance(Duration::from_millis(25)).await;
                    Ok(())
                })
                .await
                .unwrap();
            let result: Result<(), StorageError> = stage
                .read(LexicalPhase::TermFrequency, async {
                    tokio::time::advance(Duration::from_millis(7)).await;
                    Err(StorageError::Timeout {
                        operation: "test.read".into(),
                    })
                })
                .await;
            assert!(result.is_err());
            tokio::time::advance(Duration::from_millis(4000)).await;
            let detail = stage.timeout.expect("missing timeout detail");
            assert_eq!(detail.stage_elapsed_ms, 32);
            assert_eq!(detail.operation_elapsed_ms, 7);
            assert_eq!(detail.effective_budget_ms, 2000);
        })
        .await;
    }

    /// `(message, completed_reads)` for every event emitted while installed.
    type Records = Arc<Mutex<Vec<(String, Option<String>)>>>;

    struct TimeoutRecords(Records);

    #[derive(Default)]
    struct TimeoutRecordVisitor {
        message: String,
        completed_reads: Option<String>,
    }

    impl tracing::field::Visit for TimeoutRecordVisitor {
        fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn std::fmt::Debug) {
            match field.name() {
                "message" => self.message = format!("{value:?}"),
                "completed_reads" => self.completed_reads = Some(format!("{value:?}")),
                _ => {}
            }
        }
    }

    impl tracing::Subscriber for TimeoutRecords {
        fn enabled(&self, _: &tracing::Metadata<'_>) -> bool {
            true
        }
        fn new_span(&self, _: &tracing::span::Attributes<'_>) -> tracing::span::Id {
            tracing::span::Id::from_u64(1)
        }
        fn record(&self, _: &tracing::span::Id, _: &tracing::span::Record<'_>) {}
        fn record_follows_from(&self, _: &tracing::span::Id, _: &tracing::span::Id) {}
        fn event(&self, event: &tracing::Event<'_>) {
            let mut visitor = TimeoutRecordVisitor::default();
            event.record(&mut visitor);
            self.0
                .lock()
                .unwrap()
                .push((visitor.message, visitor.completed_reads));
        }
        fn enter(&self, _: &tracing::span::Id) {}
        fn exit(&self, _: &tracing::span::Id) {}
    }

    /// Runs the completed reads, then one read that times out after 11 ms,
    /// and returns the stage elapsed time and the logged `completed_reads`.
    async fn timeout_record_after(reads: &[(LexicalPhase, u64)]) -> (u64, Option<String>) {
        let records = Arc::new(Mutex::new(Vec::new()));
        let _subscriber = tracing::subscriber::set_default(TimeoutRecords(Arc::clone(&records)));
        let started = Instant::now();
        let stage_elapsed_ms =
            khive_storage::scope_request_read_deadline(Duration::from_millis(2000), async {
                let mut stage =
                    LexicalStage::new(LexicalPass::Full, started, Duration::from_millis(2000));
                for &(phase, ms) in reads {
                    stage
                        .read(phase, async move {
                            tokio::time::advance(Duration::from_millis(ms)).await;
                            Ok::<(), StorageError>(())
                        })
                        .await
                        .unwrap();
                }
                let result: Result<(), StorageError> = stage
                    .read(LexicalPhase::PhaseBHydration, async {
                        tokio::time::advance(Duration::from_millis(11)).await;
                        Err(StorageError::Timeout {
                            operation: "test.read".into(),
                        })
                    })
                    .await;
                assert!(result.is_err());
                let detail = stage.timeout.expect("missing timeout detail");
                assert_eq!(detail.operation_elapsed_ms, 11);
                assert!(
                    serde_json::to_value(detail)
                        .unwrap()
                        .get("completed_reads")
                        .is_none(),
                    "the response detail must not carry the breakdown"
                );
                detail.stage_elapsed_ms
            })
            .await;
        let records = records.lock().unwrap();
        let logged: Vec<_> = records
            .iter()
            .filter(|(message, _)| message == "lexical read timed out")
            .collect();
        assert_eq!(logged.len(), 1, "one timeout record: {records:?}");
        (stage_elapsed_ms, logged[0].1.clone())
    }

    #[tokio::test(start_paused = true)]
    async fn a_timeout_record_names_the_time_completed_reads_spent() {
        let (stage_elapsed_ms, completed_reads) = timeout_record_after(&[
            (LexicalPhase::ReaderOpen, 3),
            (LexicalPhase::TermFrequency, 40),
            (LexicalPhase::TermFrequency, 60),
            (LexicalPhase::PhaseARowids, 5),
            (LexicalPhase::PhaseBHydration, 900),
            (LexicalPhase::PhaseARowids, 7),
        ])
        .await;
        let expected: Vec<(&str, u64, u32)> = vec![
            ("reader_open", 3, 1),
            ("term_frequency", 100, 2),
            ("phase_a_rowids", 12, 2),
            ("phase_b_hydration", 900, 1),
        ];
        assert_eq!(completed_reads, Some(format!("{expected:?}")));
        assert_eq!(stage_elapsed_ms, 1026);
        let spent: u64 = expected.iter().map(|(_, ms, _)| ms).sum();
        assert!(spent <= stage_elapsed_ms);
    }

    #[tokio::test(start_paused = true)]
    async fn a_timeout_on_the_first_read_logs_an_empty_breakdown() {
        let (stage_elapsed_ms, completed_reads) = timeout_record_after(&[]).await;
        assert_eq!(completed_reads.as_deref(), Some("[]"));
        assert_eq!(stage_elapsed_ms, 11);
    }

    #[tokio::test]
    async fn non_timeout_errors_do_not_create_diagnostics() {
        let mut stage = LexicalStage::new(
            LexicalPass::Full,
            Instant::now(),
            Duration::from_millis(2000),
        );
        let result: Result<(), StorageError> = stage
            .read(LexicalPhase::ReaderOpen, async {
                Err(StorageError::AdmissionTimeout {
                    operation: "test.admission".into(),
                    timeout_ms: 5,
                    pool_identity: None,
                })
            })
            .await;
        assert!(matches!(
            result,
            Err(StorageError::AdmissionTimeout { timeout_ms: 5, .. })
        ));
        assert!(stage.timeout.is_none());
    }

    #[tokio::test(start_paused = true)]
    async fn expired_parent_has_zero_allowance() {
        khive_storage::scope_request_read_deadline(Duration::from_millis(5), async {
            tokio::time::advance(Duration::from_millis(8)).await;
            let started = Instant::now();
            khive_storage::scope_request_read_deadline(Duration::from_millis(2000), async {
                let mut stage =
                    LexicalStage::new(LexicalPass::Full, started, Duration::from_millis(2000));
                assert!(stage
                    .read(LexicalPhase::ReaderOpen, async {
                        khive_storage::ensure_request_read_active("test.expired")
                    })
                    .await
                    .is_err());
                let detail = stage.timeout.expect("missing expired timeout detail");
                assert_eq!(detail.effective_budget_ms, 0);
                assert_eq!(detail.configured_budget_ms, 2000);
                assert_eq!(detail.stage_elapsed_ms, 0);
                assert_eq!(detail.operation_elapsed_ms, 0);
            })
            .await;
        })
        .await;
    }
}