hawdb-core 0.8.0

An embedded Rust graph database for local data-plane workloads
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Host-injectable scoring for graph-augmented retrieval.
//!
//! The spec is defined here, in the lowest crate that both the query engine and
//! the host-facing contracts can depend on, because the scoring operator runs in
//! the executor while the request contract lives above the search crate.

use super::ScoringCombination;
use std::fmt;

/// Typed, host-injectable rerank scoring.
///
/// A spec is a weighted sum of features multiplied by exponential decay
/// factors. The engine evaluates it one candidate at a time; a plan carries
/// only [`ScoringSpec::shape_fingerprint`], so changing weights never
/// invalidates a cached plan.
#[derive(Debug, Clone, PartialEq)]
pub struct ScoringSpec {
    pub terms: Vec<ScoringTerm>,
    pub decay: Vec<DecayTerm>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct ScoringTerm {
    pub weight: f64,
    pub feature: ScoreFeature,
}

/// Exponential decay `0.5^(age / half_life)` over a distance or timestamp
/// feature, clamped to `[min_factor, 1]` and multiplied into the combined
/// score.
#[derive(Debug, Clone, PartialEq)]
pub struct DecayTerm {
    pub feature: ScoreFeature,
    pub half_life: f64,
    pub min_factor: f64,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ScoreFeature {
    /// Ranking score returned by the search projection.
    SearchScore,
    /// Graph-side seed score for the same canonical node.
    GraphSeedScore,
    /// Bounded graph distance from the seed (`0` for the seed itself).
    HopDistance,
    /// Numeric canonical node property.
    NodeProperty(String),
    /// Canonical timestamp property, aged against the request clock.
    TimestampProperty(String),
}

/// Feature values the engine can supply for one candidate.
pub trait ScoringFeatureSource {
    fn search_score(&self) -> Option<f64>;
    fn graph_seed_score(&self) -> Option<f64>;
    fn hop_distance(&self) -> Option<usize>;
    fn numeric_property(&self, property: &str) -> Option<f64>;
    fn timestamp_millis(&self, property: &str) -> Option<u64>;

    /// Borrowed, explicitly returned metadata for a host's cohort policy, such
    /// as an exact reason/tie key. It does not establish retrieval provenance.
    fn returned_value(&self, _column: &str) -> Option<&crate::Value> {
        None
    }
}

/// One candidate's evaluation, carrying per-term provenance.
#[derive(Debug, Clone, PartialEq)]
pub struct ScoringEvaluation {
    pub combined_score: f64,
    /// Contribution of each [`ScoringSpec::terms`] entry, in spec order.
    pub term_contributions: Vec<f64>,
    /// Factor of each [`ScoringSpec::decay`] entry, in spec order.
    pub decay_factors: Vec<f64>,
    /// Features the source could not supply; they neither add nor multiply.
    pub missing_features: Vec<ScoreFeature>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScoringSpecError {
    NoTerms,
    NonFiniteWeight,
    NegativeWeight,
    NonPositiveHalfLife,
    InvalidMinFactor,
    /// A timestamp feature carries no rank value of its own.
    TimestampTermNotAllowed,
    /// Only `HopDistance` and `TimestampProperty` define an age to decay.
    UnsupportedDecayFeature,
}

impl fmt::Display for ScoringSpecError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let text = match self {
            Self::NoTerms => "a scoring spec needs at least one term",
            Self::NonFiniteWeight => "scoring term weights must be finite",
            Self::NegativeWeight => "scoring term weights must not be negative",
            Self::NonPositiveHalfLife => "decay half-lives must be positive and finite",
            Self::InvalidMinFactor => "decay floors must lie in [0, 1]",
            Self::TimestampTermNotAllowed => {
                "a timestamp property is a decay feature, not an additive term"
            }
            Self::UnsupportedDecayFeature => {
                "only hop distance and timestamp properties define an age to decay"
            }
        };
        formatter.write_str(text)
    }
}

impl std::error::Error for ScoringSpecError {}

impl ScoringSpec {
    /// Weighted sum over the two retriever scores.
    pub fn weighted_scores(search_weight: f64, graph_seed_weight: f64) -> Self {
        Self {
            terms: vec![
                ScoringTerm {
                    weight: search_weight,
                    feature: ScoreFeature::SearchScore,
                },
                ScoringTerm {
                    weight: graph_seed_weight,
                    feature: ScoreFeature::GraphSeedScore,
                },
            ],
            decay: Vec::new(),
        }
    }

    pub fn validate(&self) -> Result<(), ScoringSpecError> {
        if self.terms.is_empty() {
            return Err(ScoringSpecError::NoTerms);
        }
        for term in &self.terms {
            if !term.weight.is_finite() {
                return Err(ScoringSpecError::NonFiniteWeight);
            }
            if term.weight < 0.0 {
                return Err(ScoringSpecError::NegativeWeight);
            }
            if matches!(term.feature, ScoreFeature::TimestampProperty(_)) {
                return Err(ScoringSpecError::TimestampTermNotAllowed);
            }
        }
        for decay in &self.decay {
            if !decay.half_life.is_finite() || decay.half_life <= 0.0 {
                return Err(ScoringSpecError::NonPositiveHalfLife);
            }
            if !decay.min_factor.is_finite() || !(0.0..=1.0).contains(&decay.min_factor) {
                return Err(ScoringSpecError::InvalidMinFactor);
            }
            if !matches!(
                decay.feature,
                ScoreFeature::HopDistance | ScoreFeature::TimestampProperty(_)
            ) {
                return Err(ScoringSpecError::UnsupportedDecayFeature);
            }
        }
        Ok(())
    }

    /// Feature shape without weights: the identity that belongs in a plan
    /// fingerprint or cache key.
    pub fn shape_fingerprint(&self) -> String {
        let terms = self
            .terms
            .iter()
            .map(|term| score_feature_name(&term.feature))
            .collect::<Vec<_>>()
            .join(",");
        let decay = self
            .decay
            .iter()
            .map(|decay| score_feature_name(&decay.feature))
            .collect::<Vec<_>>()
            .join(",");
        format!("terms=[{terms}] decay=[{decay}]")
    }

    /// Whether this spec reads canonical node properties. Callers use it to
    /// avoid loading a node record for specs that only need retriever scores.
    pub fn needs_canonical_node_properties(&self) -> bool {
        self.terms
            .iter()
            .any(|term| matches!(term.feature, ScoreFeature::NodeProperty(_)))
            || self.decay.iter().any(|decay| {
                matches!(
                    decay.feature,
                    ScoreFeature::NodeProperty(_) | ScoreFeature::TimestampProperty(_)
                )
            })
    }

    /// Evaluates one candidate. Missing features contribute nothing and are
    /// reported instead of silently ranking as zero-valued hits.
    pub fn evaluate(
        &self,
        source: &impl ScoringFeatureSource,
        reference_time_millis: u64,
    ) -> ScoringEvaluation {
        let mut missing_features = Vec::new();
        let mut term_contributions = Vec::with_capacity(self.terms.len());
        let mut decay_factors = Vec::with_capacity(self.decay.len());
        let combined_score = self.evaluate_into(
            source,
            reference_time_millis,
            ScoringCombination::WeightedSum,
            &mut |value| term_contributions.push(value),
            &mut |value| decay_factors.push(value),
            &mut |feature: &ScoreFeature| missing_features.push(feature.clone()),
        );
        ScoringEvaluation {
            combined_score,
            term_contributions,
            decay_factors,
            missing_features,
        }
    }

    /// Evaluates the same score without allocating a per-row diagnostic report.
    ///
    /// Validate the specification before execution. Missing features retain
    /// the same neutral behavior as [`Self::evaluate`]; use that method when
    /// the caller needs feature provenance and individual contributions.
    pub fn evaluate_score(
        &self,
        source: &impl ScoringFeatureSource,
        reference_time_millis: u64,
    ) -> f64 {
        self.evaluate_into(
            source,
            reference_time_millis,
            ScoringCombination::WeightedSum,
            &mut |_| {},
            &mut |_| {},
            &mut |_| {},
        )
    }

    pub(super) fn evaluate_into(
        &self,
        source: &impl ScoringFeatureSource,
        reference_time_millis: u64,
        combination: ScoringCombination,
        term_contribution: &mut impl FnMut(f64),
        decay_factor: &mut impl FnMut(f64),
        missing_feature: &mut impl FnMut(&ScoreFeature),
    ) -> f64 {
        let mut combined_score = match combination {
            ScoringCombination::WeightedSum => 0.0,
            ScoringCombination::WeightedProduct => 1.0,
        };
        for term in &self.terms {
            let value = match self.term_value(&term.feature, source) {
                Some(value) => value,
                None => {
                    missing_feature(&term.feature);
                    match combination {
                        ScoringCombination::WeightedSum => 0.0,
                        ScoringCombination::WeightedProduct => {
                            term_contribution(1.0);
                            continue;
                        }
                    }
                }
            };
            let contribution = match combination {
                ScoringCombination::WeightedSum => term.weight * value,
                ScoringCombination::WeightedProduct => value.powf(term.weight),
            };
            match combination {
                ScoringCombination::WeightedSum => combined_score += contribution,
                ScoringCombination::WeightedProduct => combined_score *= contribution,
            }
            term_contribution(contribution);
        }
        for decay in &self.decay {
            let age = match self.decay_age(&decay.feature, source, reference_time_millis) {
                Some(age) => age,
                None => {
                    missing_feature(&decay.feature);
                    decay_factor(1.0);
                    continue;
                }
            };
            let factor = 0.5f64
                .powf(age / decay.half_life)
                .clamp(decay.min_factor, 1.0);
            combined_score *= factor;
            decay_factor(factor);
        }
        combined_score
    }

    fn term_value(
        &self,
        feature: &ScoreFeature,
        source: &impl ScoringFeatureSource,
    ) -> Option<f64> {
        match feature {
            ScoreFeature::SearchScore => source.search_score().filter(|v| v.is_finite()),
            ScoreFeature::GraphSeedScore => source.graph_seed_score().filter(|v| v.is_finite()),
            ScoreFeature::HopDistance => source
                .hop_distance()
                .map(|hops| u32::try_from(hops).map_or(f64::from(u32::MAX), f64::from)),
            ScoreFeature::NodeProperty(property) => {
                source.numeric_property(property).filter(|v| v.is_finite())
            }
            // Decay-only features carry no additive value.
            ScoreFeature::TimestampProperty(_) => None,
        }
    }

    fn decay_age(
        &self,
        feature: &ScoreFeature,
        source: &impl ScoringFeatureSource,
        reference_time_millis: u64,
    ) -> Option<f64> {
        match feature {
            ScoreFeature::HopDistance => source.hop_distance().map(|hops| hops as f64),
            ScoreFeature::TimestampProperty(property) => {
                let timestamp = source.timestamp_millis(property)?;
                let age_millis = reference_time_millis.saturating_sub(timestamp);
                Some(age_millis as f64 / 1_000.0)
            }
            _ => None,
        }
    }
}

fn score_feature_name(feature: &ScoreFeature) -> String {
    match feature {
        ScoreFeature::SearchScore => "search_score".to_string(),
        ScoreFeature::GraphSeedScore => "graph_seed_score".to_string(),
        ScoreFeature::HopDistance => "hop_distance".to_string(),
        ScoreFeature::NodeProperty(property) => format!("node_property:{property}"),
        ScoreFeature::TimestampProperty(property) => format!("timestamp:{property}"),
    }
}

#[cfg(test)]
mod tests {
    use super::{
        DecayTerm, ScoreFeature, ScoringFeatureSource, ScoringSpec, ScoringSpecError, ScoringTerm,
    };
    use std::collections::BTreeMap;

    #[derive(Default)]
    struct StubFeatures {
        search_score: Option<f64>,
        graph_seed_score: Option<f64>,
        hop_distance: Option<usize>,
        numeric: BTreeMap<String, f64>,
        timestamps: BTreeMap<String, u64>,
    }

    impl ScoringFeatureSource for StubFeatures {
        fn search_score(&self) -> Option<f64> {
            self.search_score
        }

        fn graph_seed_score(&self) -> Option<f64> {
            self.graph_seed_score
        }

        fn hop_distance(&self) -> Option<usize> {
            self.hop_distance
        }

        fn numeric_property(&self, property: &str) -> Option<f64> {
            self.numeric.get(property).copied()
        }

        fn timestamp_millis(&self, property: &str) -> Option<u64> {
            self.timestamps.get(property).copied()
        }
    }

    fn term(weight: f64, feature: ScoreFeature) -> ScoringTerm {
        ScoringTerm { weight, feature }
    }

    fn decay(feature: ScoreFeature, half_life: f64, min_factor: f64) -> DecayTerm {
        DecayTerm {
            feature,
            half_life,
            min_factor,
        }
    }

    #[test]
    fn scoring_spec_combines_weighted_features() {
        let spec = ScoringSpec {
            terms: vec![
                term(0.5, ScoreFeature::SearchScore),
                term(2.0, ScoreFeature::NodeProperty("pagerank".to_string())),
            ],
            decay: Vec::new(),
        };
        let source = StubFeatures {
            search_score: Some(4.0),
            numeric: BTreeMap::from([("pagerank".to_string(), 3.0)]),
            ..StubFeatures::default()
        };
        let evaluation = spec.evaluate(&source, 0);
        assert_eq!(evaluation.combined_score, 0.5 * 4.0 + 2.0 * 3.0);
        assert_eq!(evaluation.term_contributions, vec![2.0, 6.0]);
        assert!(evaluation.missing_features.is_empty());
        assert_eq!(spec.evaluate_score(&source, 0), evaluation.combined_score);
    }

    #[test]
    fn scoring_spec_reports_missing_features_instead_of_ranking_them_zero() {
        let spec = ScoringSpec {
            terms: vec![
                term(1.0, ScoreFeature::SearchScore),
                term(1.0, ScoreFeature::GraphSeedScore),
            ],
            decay: Vec::new(),
        };
        let source = StubFeatures {
            search_score: Some(2.0),
            ..StubFeatures::default()
        };
        let evaluation = spec.evaluate(&source, 0);
        assert_eq!(evaluation.combined_score, 2.0);
        assert_eq!(spec.evaluate_score(&source, 0), evaluation.combined_score);
        assert_eq!(
            evaluation.missing_features,
            vec![ScoreFeature::GraphSeedScore]
        );
    }

    #[test]
    fn hop_decay_multiplies_and_holds_its_floor() {
        let spec = ScoringSpec {
            terms: vec![term(1.0, ScoreFeature::SearchScore)],
            decay: vec![decay(ScoreFeature::HopDistance, 1.0, 0.25)],
        };
        let evaluate = |hops: usize| {
            spec.evaluate(
                &StubFeatures {
                    search_score: Some(8.0),
                    hop_distance: Some(hops),
                    ..StubFeatures::default()
                },
                0,
            )
        };
        assert_eq!(evaluate(0).combined_score, 8.0);
        assert_eq!(evaluate(1).combined_score, 4.0);
        assert_eq!(evaluate(2).combined_score, 2.0);
        assert_eq!(evaluate(9).combined_score, 2.0);
        assert_eq!(evaluate(2).decay_factors, vec![0.25]);
    }

    #[test]
    fn timestamp_decay_ages_against_the_request_clock() {
        let spec = ScoringSpec {
            terms: vec![term(1.0, ScoreFeature::SearchScore)],
            decay: vec![decay(
                ScoreFeature::TimestampProperty("updated_at".to_string()),
                600.0,
                0.0,
            )],
        };
        let aged = StubFeatures {
            search_score: Some(10.0),
            timestamps: BTreeMap::from([("updated_at".to_string(), 400_000)]),
            ..StubFeatures::default()
        };
        // 600 seconds old against a 600-second half-life.
        assert_eq!(spec.evaluate(&aged, 1_000_000).combined_score, 5.0);
        let future = StubFeatures {
            search_score: Some(10.0),
            timestamps: BTreeMap::from([("updated_at".to_string(), 2_000_000)]),
            ..StubFeatures::default()
        };
        assert_eq!(spec.evaluate(&future, 1_000_000).combined_score, 10.0);
    }

    #[test]
    fn scoring_spec_validation_rejects_unusable_shapes() {
        let missing = StubFeatures::default();
        assert_eq!(
            ScoringSpec {
                terms: Vec::new(),
                decay: Vec::new(),
            }
            .validate(),
            Err(ScoringSpecError::NoTerms)
        );
        assert_eq!(
            ScoringSpec {
                terms: vec![term(-1.0, ScoreFeature::SearchScore)],
                decay: Vec::new(),
            }
            .validate(),
            Err(ScoringSpecError::NegativeWeight)
        );
        assert_eq!(
            ScoringSpec {
                terms: vec![term(f64::NAN, ScoreFeature::SearchScore)],
                decay: Vec::new(),
            }
            .validate(),
            Err(ScoringSpecError::NonFiniteWeight)
        );
        assert_eq!(
            ScoringSpec {
                terms: vec![term(1.0, ScoreFeature::SearchScore)],
                decay: vec![decay(ScoreFeature::HopDistance, 0.0, 0.0)],
            }
            .validate(),
            Err(ScoringSpecError::NonPositiveHalfLife)
        );
        assert_eq!(
            ScoringSpec {
                terms: vec![term(1.0, ScoreFeature::SearchScore)],
                decay: vec![decay(ScoreFeature::HopDistance, 1.0, 1.5)],
            }
            .validate(),
            Err(ScoringSpecError::InvalidMinFactor)
        );
        assert_eq!(
            ScoringSpec {
                terms: vec![term(1.0, ScoreFeature::TimestampProperty("t".to_string()))],
                decay: Vec::new(),
            }
            .validate(),
            Err(ScoringSpecError::TimestampTermNotAllowed)
        );
        assert_eq!(
            ScoringSpec {
                terms: vec![term(1.0, ScoreFeature::SearchScore)],
                decay: vec![decay(ScoreFeature::SearchScore, 1.0, 0.0)],
            }
            .validate(),
            Err(ScoringSpecError::UnsupportedDecayFeature)
        );
        assert!(ScoringSpec::weighted_scores(1.0, 1.0).validate().is_ok());
        // A property that is merely absent at runtime is valid, and reported
        // through `missing_features` instead of failing the request.
        assert!(ScoringSpec {
            terms: vec![term(1.0, ScoreFeature::NodeProperty("absent".to_string()))],
            decay: Vec::new(),
        }
        .validate()
        .is_ok());
        assert!(missing.numeric_property("absent").is_none());
        assert!(ScoringSpecError::NoTerms
            .to_string()
            .contains("at least one term"));
    }

    #[test]
    fn scoring_shape_fingerprint_ignores_weights() {
        let lightness = ScoringSpec {
            terms: vec![
                term(0.1, ScoreFeature::SearchScore),
                term(0.2, ScoreFeature::HopDistance),
            ],
            decay: vec![decay(ScoreFeature::HopDistance, 30.0, 0.1)],
        };
        let heavy = ScoringSpec {
            terms: vec![
                term(9.0, ScoreFeature::SearchScore),
                term(5.0, ScoreFeature::HopDistance),
            ],
            decay: vec![decay(ScoreFeature::HopDistance, 1.0, 0.9)],
        };
        assert_eq!(lightness.shape_fingerprint(), heavy.shape_fingerprint());
        assert!(!lightness.needs_canonical_node_properties());
        let property = ScoringSpec {
            terms: vec![term(
                1.0,
                ScoreFeature::NodeProperty("pagerank".to_string()),
            )],
            decay: Vec::new(),
        };
        assert!(property.needs_canonical_node_properties());
    }
}