relay_knowledge/domain/code/staleness/mod.rs
1//! Defines query-time staleness priority and source-verification policy.
2
3use serde::{Deserialize, Serialize};
4
5/// Per-file staleness hint attached to retrieval hits at query time.
6///
7/// Encodes the freshness relationship between the indexed graph snapshot and
8/// the live file state. Query-time freshness diagnostics can distinguish an
9/// answer served from an older completed scope while a matching refresh task is
10/// still pending from a scope that is simply stale.
11///
12/// New variants may be added in future releases; match exhaustively or use a
13/// wildcard to remain forward-compatible.
14#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
15#[serde(rename_all = "snake_case", tag = "state")]
16#[non_exhaustive]
17pub enum StalenessHint {
18 Fresh,
19 /// A matching index task is queued, running, or retrying for this query.
20 PendingIndex {},
21 /// Indexed snapshot is older than the latest file modification.
22 Stale {},
23}
24
25impl StalenessHint {
26 pub fn requires_source_verification(&self) -> bool {
27 !matches!(self, StalenessHint::Fresh)
28 }
29
30 pub fn should_replace(&self, current: Option<&Self>) -> bool {
31 current.is_none_or(|current| self.priority() > current.priority())
32 }
33
34 fn priority(&self) -> u8 {
35 match self {
36 StalenessHint::Fresh => 0,
37 StalenessHint::Stale {} => 1,
38 StalenessHint::PendingIndex {} => 2,
39 }
40 }
41}
42
43#[cfg(test)]
44mod mod_tests;