graphforge-storage 0.5.1

GraphForge StorageProvider trait and Parquet backend
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
//! Deterministic freshness policy for verified embedding generations.

use crate::{
    EmbeddingCompatibilityId, EmbeddingGenerationId, EmbeddingGenerationManifest,
    EmbeddingSourceFingerprint, EmbeddingSourceState, SearchArtifactError,
};

/// Changed-UUID percentage that makes a generation substantially stale.
pub const EMBEDDING_SUBSTANTIAL_CHANGED_PERCENT: u64 = 5;
/// Relevant committed mutation batches that make a generation substantially stale.
pub const EMBEDDING_SUBSTANTIAL_MUTATION_BATCHES: u64 = 128;

/// Durable mutation evidence observed after one generation source snapshot.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EmbeddingMutationObservation {
    current_source: EmbeddingSourceState,
    changed_distinct_uuids: u64,
    relevant_committed_batches: u64,
    structural_mutation: bool,
    scope_proven: bool,
}

impl EmbeddingMutationObservation {
    /// Validate one bounded freshness observation.
    ///
    /// # Errors
    /// Rejects changed UUID or structural evidence without a committed batch.
    pub fn new(
        current_source: EmbeddingSourceState,
        changed_distinct_uuids: u64,
        relevant_committed_batches: u64,
        structural_mutation: bool,
        scope_proven: bool,
    ) -> Result<Self, SearchArtifactError> {
        if relevant_committed_batches == 0 && (changed_distinct_uuids != 0 || structural_mutation) {
            return Err(invalid(
                "embedding mutation observation",
                "changed UUID and structural evidence require a committed relevant batch",
            ));
        }
        Ok(Self {
            current_source,
            changed_distinct_uuids,
            relevant_committed_batches,
            structural_mutation,
            scope_proven,
        })
    }

    /// Current durable dependency projection.
    #[must_use]
    pub const fn current_source(self) -> EmbeddingSourceState {
        self.current_source
    }

    /// Distinct UUIDs changed across relevant committed batches.
    #[must_use]
    pub const fn changed_distinct_uuids(self) -> u64 {
        self.changed_distinct_uuids
    }

    /// Relevant committed mutation batches since the recorded source.
    #[must_use]
    pub const fn relevant_committed_batches(self) -> u64 {
        self.relevant_committed_batches
    }

    /// Whether any relevant topology mutation occurred.
    #[must_use]
    pub const fn structural_mutation(self) -> bool {
        self.structural_mutation
    }

    /// Whether mutation scope was proven from durable metadata.
    #[must_use]
    pub const fn scope_proven(self) -> bool {
        self.scope_proven
    }
}

/// Freshness state for one verified, compatible, complete generation.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EmbeddingFreshnessState {
    /// Recorded and current dependency projections are exactly identical.
    Fresh,
    /// A bounded relevant mutation occurred below every substantial threshold.
    Stale,
    /// Ordinary search must refresh successfully before serving.
    SubstantiallyStale,
}

/// Stable reason for a non-fresh state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EmbeddingFreshnessReason {
    /// Relevant scope cannot be proven from durable metadata.
    UnprovenMutationScope,
    /// A structural space observed a relevant topology mutation.
    StructuralMutation,
    /// Any relevant mutation affected a recorded empty generation.
    EmptyGenerationMutation,
    /// At least five percent of recorded eligible UUIDs changed.
    ChangedUuidFraction,
    /// At least 128 relevant mutation batches accumulated.
    MutationBatchLimit,
    /// A known relevant mutation remains below substantial thresholds.
    RelevantMutation,
}

impl EmbeddingFreshnessReason {
    /// Stable bounded diagnostic token.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::UnprovenMutationScope => "unproven_mutation_scope",
            Self::StructuralMutation => "structural_mutation",
            Self::EmptyGenerationMutation => "empty_generation_mutation",
            Self::ChangedUuidFraction => "changed_uuid_fraction",
            Self::MutationBatchLimit => "mutation_batch_limit",
            Self::RelevantMutation => "relevant_mutation",
        }
    }
}

/// Classified freshness plus the exact current durable source state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EmbeddingFreshness {
    state: EmbeddingFreshnessState,
    reason: Option<EmbeddingFreshnessReason>,
    current_source: EmbeddingSourceState,
}

impl EmbeddingFreshness {
    /// Classified state.
    #[must_use]
    pub const fn state(self) -> EmbeddingFreshnessState {
        self.state
    }

    /// Stable reason, absent only for `fresh`.
    #[must_use]
    pub const fn reason(self) -> Option<EmbeddingFreshnessReason> {
        self.reason
    }

    /// Current durable dependency projection used for classification.
    #[must_use]
    pub const fn current_source(self) -> EmbeddingSourceState {
        self.current_source
    }
}

/// Stable observable diagnostic emitted only for an explicit forced stale read.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EmbeddingForcedStaleDiagnostic {
    /// Compatibility lineage selected by the caller.
    pub compatibility_id: EmbeddingCompatibilityId,
    /// Last complete immutable generation served.
    pub generation_id: EmbeddingGenerationId,
    /// Source fingerprint recorded by that generation.
    pub recorded_source: EmbeddingSourceFingerprint,
    /// Current durable dependency fingerprint.
    pub current_source: EmbeddingSourceFingerprint,
    /// Deterministic substantial-staleness reason.
    pub reason: EmbeddingFreshnessReason,
}

impl EmbeddingForcedStaleDiagnostic {
    /// Stable, bounded, content-free diagnostic representation.
    #[must_use]
    pub fn stable_message(self) -> String {
        format!(
            "embedding_force_stale:v1 compatibility_id={} generation_id={} recorded_source={} current_source={} reason={}",
            self.compatibility_id,
            self.generation_id,
            self.recorded_source,
            self.current_source,
            self.reason.as_str(),
        )
    }
}

/// Serving decision after compatibility and primary-data validation succeeded.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EmbeddingReadDecision {
    /// Serve the verified fresh generation.
    ServeFresh,
    /// Serve a mildly stale generation while refresh is queued.
    ServeStale {
        /// Stable non-substantial reason.
        reason: EmbeddingFreshnessReason,
    },
    /// Refresh must succeed before ordinary search may serve.
    RefreshRequired {
        /// Stable substantial-staleness reason.
        reason: EmbeddingFreshnessReason,
    },
    /// Explicitly serve the last complete substantially stale generation.
    ServeForcedStale {
        /// Stable observable forced-read diagnostic.
        diagnostic: EmbeddingForcedStaleDiagnostic,
    },
}

/// Classify one verified generation against current durable mutation evidence.
///
/// Precedence is deterministic: unproven scope, structural mutation, empty
/// generation, changed UUID fraction, mutation-batch limit, then ordinary
/// relevant mutation. Wall-clock timestamps never participate.
///
/// # Errors
/// Rejects observations that predate the generation, impossible batch counts,
/// unexplained fingerprint changes, and checked threshold arithmetic overflow.
pub fn classify_embedding_freshness(
    manifest: &EmbeddingGenerationManifest,
    observation: EmbeddingMutationObservation,
) -> Result<EmbeddingFreshness, SearchArtifactError> {
    let recorded = manifest.source();
    let current = observation.current_source;
    if current.graph_generation() < recorded.graph_generation() {
        return Err(invalid(
            "embedding freshness observation",
            "current graph generation predates the recorded generation",
        ));
    }
    let generation_delta = current.graph_generation() - recorded.graph_generation();
    if observation.relevant_committed_batches > generation_delta {
        return Err(invalid(
            "embedding freshness observation",
            "relevant batch count exceeds the committed graph generation delta",
        ));
    }
    let no_relevant_evidence = observation.relevant_committed_batches == 0
        && observation.changed_distinct_uuids == 0
        && !observation.structural_mutation
        && observation.scope_proven;
    if recorded.fingerprint() == current.fingerprint() && no_relevant_evidence {
        return Ok(EmbeddingFreshness {
            state: EmbeddingFreshnessState::Fresh,
            reason: None,
            current_source: current,
        });
    }
    if recorded.fingerprint() != current.fingerprint()
        && no_relevant_evidence
        && observation.scope_proven
    {
        return Err(invalid(
            "embedding freshness observation",
            "source fingerprint changed without a relevant mutation or unproven scope",
        ));
    }

    let reason = if !observation.scope_proven {
        EmbeddingFreshnessReason::UnprovenMutationScope
    } else if observation.structural_mutation {
        EmbeddingFreshnessReason::StructuralMutation
    } else if recorded.eligible_uuid_count() == 0 {
        EmbeddingFreshnessReason::EmptyGenerationMutation
    } else if changed_fraction_is_substantial(
        observation.changed_distinct_uuids,
        recorded.eligible_uuid_count(),
    )? {
        EmbeddingFreshnessReason::ChangedUuidFraction
    } else if observation.relevant_committed_batches >= EMBEDDING_SUBSTANTIAL_MUTATION_BATCHES {
        EmbeddingFreshnessReason::MutationBatchLimit
    } else {
        EmbeddingFreshnessReason::RelevantMutation
    };
    let state = if reason == EmbeddingFreshnessReason::RelevantMutation {
        EmbeddingFreshnessState::Stale
    } else {
        EmbeddingFreshnessState::SubstantiallyStale
    };
    Ok(EmbeddingFreshness {
        state,
        reason: Some(reason),
        current_source: current,
    })
}

/// Select the serving boundary for a classified verified generation.
#[must_use]
pub fn decide_embedding_read(
    manifest: &EmbeddingGenerationManifest,
    freshness: EmbeddingFreshness,
    force_stale: bool,
) -> EmbeddingReadDecision {
    match freshness.state {
        EmbeddingFreshnessState::Fresh => EmbeddingReadDecision::ServeFresh,
        EmbeddingFreshnessState::Stale => EmbeddingReadDecision::ServeStale {
            reason: freshness
                .reason
                .expect("a stale classification always has a reason"),
        },
        EmbeddingFreshnessState::SubstantiallyStale if force_stale => {
            EmbeddingReadDecision::ServeForcedStale {
                diagnostic: EmbeddingForcedStaleDiagnostic {
                    compatibility_id: manifest.compatibility_id(),
                    generation_id: manifest.generation_id(),
                    recorded_source: manifest.source().fingerprint(),
                    current_source: freshness.current_source.fingerprint(),
                    reason: freshness
                        .reason
                        .expect("a substantially stale classification always has a reason"),
                },
            }
        }
        EmbeddingFreshnessState::SubstantiallyStale => EmbeddingReadDecision::RefreshRequired {
            reason: freshness
                .reason
                .expect("a substantially stale classification always has a reason"),
        },
    }
}

fn changed_fraction_is_substantial(
    changed: u64,
    recorded_eligible: u64,
) -> Result<bool, SearchArtifactError> {
    let changed_percent = changed.checked_mul(100).ok_or_else(arithmetic_overflow)?;
    let threshold = recorded_eligible
        .max(1)
        .checked_mul(EMBEDDING_SUBSTANTIAL_CHANGED_PERCENT)
        .ok_or_else(arithmetic_overflow)?;
    Ok(changed_percent >= threshold)
}

fn arithmetic_overflow() -> SearchArtifactError {
    SearchArtifactError::ResourceExhausted {
        resource: "embedding_freshness_arithmetic",
        limit: u64::MAX,
    }
}

fn invalid(field: &'static str, reason: impl Into<String>) -> SearchArtifactError {
    SearchArtifactError::InvalidSelector {
        field,
        reason: reason.into(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        EmbeddingContentDigest, EmbeddingGenerationManifestInput, EmbeddingPublicationFingerprint,
    };

    fn source(generation: u64, eligible: u64, marker: u8) -> EmbeddingSourceState {
        EmbeddingSourceState::new(
            generation,
            [marker; 32],
            [marker.wrapping_add(1); 32],
            eligible,
        )
    }

    fn manifest(recorded: EmbeddingSourceState, generated_at: i64) -> EmbeddingGenerationManifest {
        EmbeddingGenerationManifest::new(EmbeddingGenerationManifestInput {
            compatibility_id: EmbeddingCompatibilityId::from_hex(&"11".repeat(32)).unwrap(),
            source: recorded,
            content_digest: EmbeddingContentDigest::from_hex(&"22".repeat(32)).unwrap(),
            vector_count: recorded.eligible_uuid_count(),
            dimension: 2,
            generated_at_micros: generated_at,
            committed_at_micros: generated_at + 1,
            publication_fingerprint: EmbeddingPublicationFingerprint::from_hex(&"33".repeat(32))
                .unwrap(),
        })
        .unwrap()
    }

    fn observation(
        current: EmbeddingSourceState,
        changed: u64,
        batches: u64,
        structural: bool,
        proven: bool,
    ) -> EmbeddingMutationObservation {
        EmbeddingMutationObservation::new(current, changed, batches, structural, proven).unwrap()
    }

    #[test]
    fn exact_source_without_mutation_evidence_is_fresh_and_time_independent() {
        let recorded = source(7, 100, 1);
        let observation = observation(recorded, 0, 0, false, true);
        let early = classify_embedding_freshness(&manifest(recorded, 10), observation).unwrap();
        let late = classify_embedding_freshness(&manifest(recorded, 9_999), observation).unwrap();
        assert_eq!(early, late);
        assert_eq!(early.state(), EmbeddingFreshnessState::Fresh);
        assert_eq!(early.reason(), None);
        assert_eq!(
            decide_embedding_read(&manifest(recorded, 10), early, true),
            EmbeddingReadDecision::ServeFresh
        );
    }

    #[test]
    fn substantial_reason_precedence_is_deterministic() {
        let recorded = source(1, 100, 1);
        let current = source(130, 100, 2);
        let cases = [
            (
                observation(current, 100, 129, true, false),
                EmbeddingFreshnessReason::UnprovenMutationScope,
            ),
            (
                observation(current, 100, 129, true, true),
                EmbeddingFreshnessReason::StructuralMutation,
            ),
            (
                observation(current, 5, 127, false, true),
                EmbeddingFreshnessReason::ChangedUuidFraction,
            ),
            (
                observation(current, 4, 128, false, true),
                EmbeddingFreshnessReason::MutationBatchLimit,
            ),
        ];
        for (observation, expected) in cases {
            let freshness =
                classify_embedding_freshness(&manifest(recorded, 10), observation).unwrap();
            assert_eq!(
                freshness.state(),
                EmbeddingFreshnessState::SubstantiallyStale
            );
            assert_eq!(freshness.reason(), Some(expected));
        }
    }

    #[test]
    fn any_relevant_change_to_an_empty_generation_is_substantial() {
        let recorded = source(1, 0, 1);
        let freshness = classify_embedding_freshness(
            &manifest(recorded, 10),
            observation(source(2, 1, 2), 1, 1, false, true),
        )
        .unwrap();
        assert_eq!(
            freshness.reason(),
            Some(EmbeddingFreshnessReason::EmptyGenerationMutation)
        );
    }

    #[test]
    fn changed_uuid_fraction_uses_exact_five_percent_boundary() {
        let recorded = source(1, 100, 1);
        let below = classify_embedding_freshness(
            &manifest(recorded, 10),
            observation(source(2, 100, 2), 4, 1, false, true),
        )
        .unwrap();
        let exact = classify_embedding_freshness(
            &manifest(recorded, 10),
            observation(source(2, 100, 2), 5, 1, false, true),
        )
        .unwrap();
        assert_eq!(below.state(), EmbeddingFreshnessState::Stale);
        assert_eq!(
            exact.reason(),
            Some(EmbeddingFreshnessReason::ChangedUuidFraction)
        );
    }

    #[test]
    fn mutation_batch_limit_uses_exact_128_boundary() {
        let recorded = source(1, 1_000, 1);
        let below = classify_embedding_freshness(
            &manifest(recorded, 10),
            observation(source(128, 1_000, 2), 0, 127, false, true),
        )
        .unwrap();
        let exact = classify_embedding_freshness(
            &manifest(recorded, 10),
            observation(source(129, 1_000, 2), 0, 128, false, true),
        )
        .unwrap();
        assert_eq!(below.state(), EmbeddingFreshnessState::Stale);
        assert_eq!(
            exact.reason(),
            Some(EmbeddingFreshnessReason::MutationBatchLimit)
        );
    }

    #[test]
    fn forced_stale_is_explicit_deterministic_and_content_free() {
        let recorded = source(1, 100, 1);
        let manifest = manifest(recorded, 10);
        let freshness = classify_embedding_freshness(
            &manifest,
            observation(source(2, 100, 2), 100, 1, false, true),
        )
        .unwrap();
        assert_eq!(
            decide_embedding_read(&manifest, freshness, false),
            EmbeddingReadDecision::RefreshRequired {
                reason: EmbeddingFreshnessReason::ChangedUuidFraction,
            }
        );
        let EmbeddingReadDecision::ServeForcedStale { diagnostic } =
            decide_embedding_read(&manifest, freshness, true)
        else {
            panic!("force must select the last complete generation");
        };
        let message = diagnostic.stable_message();
        assert_eq!(message, diagnostic.stable_message());
        assert!(message.starts_with("embedding_force_stale:v1 compatibility_id="));
        assert!(message.ends_with("reason=changed_uuid_fraction"));
        assert!(!message.contains("vector"));
        assert!(!message.contains("body"));
    }

    #[test]
    fn inconsistent_observations_and_overflow_are_structured() {
        let recorded = source(5, 100, 1);
        assert!(EmbeddingMutationObservation::new(recorded, 1, 0, false, true).is_err());
        for observation in [
            observation(source(4, 100, 2), 0, 0, false, false),
            observation(source(6, 100, 2), 0, 2, false, true),
            observation(source(6, 100, 2), 0, 0, false, true),
        ] {
            assert!(classify_embedding_freshness(&manifest(recorded, 10), observation).is_err());
        }

        let overflow_recorded = source(1, u64::MAX, 1);
        let error = classify_embedding_freshness(
            &manifest(overflow_recorded, 10),
            observation(source(2, u64::MAX, 2), u64::MAX, 1, false, true),
        )
        .unwrap_err();
        assert!(matches!(
            error,
            SearchArtifactError::ResourceExhausted {
                resource: "embedding_freshness_arithmetic",
                ..
            }
        ));
    }
}