relay-knowledge 1.1.14

Graph-database-based knowledge graph project.
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
//! Retrieval request planning and optional derived backend adapters.
//!
//! Retrieval owns query-shape validation and budgets before the application
//! service asks storage and derived indexes for data.

use std::time::Duration;
use std::{error::Error, fmt};

pub mod provider;
mod rerank;
pub(crate) mod terms;

use crate::domain::{
    FreshnessPolicy, GraphVersion, IndexKind, IndexStatus, RerankDiagnostics, RerankMode,
    RetrievalBackendState, RetrievalBackendStatus, RetrievalHit, RetrieverSource,
};

pub const LOCAL_SEMANTIC_MODEL: &str = "relay-local-token-semantic-v1";
pub const LOCAL_VECTOR_MODEL: &str = "relay-local-hash-ann-v1";
pub const LOCAL_RERANK_MODEL: &str = "relay-local-deterministic-rerank-v1";
pub const LOCAL_VECTOR_DIMENSION: u32 = 16;
pub const DEFAULT_EMBEDDING_BATCH_SIZE: usize = 32;
pub const DEFAULT_EMBEDDING_TIMEOUT: Duration = Duration::from_secs(30);
pub const DEFAULT_EMBEDDING_MAX_CONCURRENCY: usize = 4;
pub const DEFAULT_RERANK_TIMEOUT: Duration = Duration::from_millis(100);
pub const DEFAULT_RERANK_CANDIDATE_MULTIPLIER: usize = 4;
pub const DEFAULT_RERANK_MAX_CANDIDATES: usize = 64;

/// Validated retrieval request with bounded result count.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RetrievalPlan {
    pub query: String,
    pub source_scope: Option<String>,
    pub limit: usize,
    pub freshness: FreshnessPolicy,
}

/// Configured owner of a semantic or vector read model.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReadModelBackendMode {
    Local,
    External,
    Disabled,
}

/// Remote LLM provider family used for embedding calls.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EmbeddingProviderKind {
    OpenAiCompatible,
    Echo,
}

impl EmbeddingProviderKind {
    /// Parses a stable environment/config value.
    pub fn parse(value: &str) -> Result<Self, EmbeddingProviderKindError> {
        match value.trim().to_ascii_lowercase().as_str() {
            "openai_compatible" => Ok(Self::OpenAiCompatible),
            "echo" => Ok(Self::Echo),
            other => Err(EmbeddingProviderKindError {
                value: other.to_owned(),
            }),
        }
    }

    /// Stable configuration label.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::OpenAiCompatible => "openai_compatible",
            Self::Echo => "echo",
        }
    }
}

/// Invalid embedding provider kind supplied by runtime configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EmbeddingProviderKindError {
    pub value: String,
}

impl fmt::Display for EmbeddingProviderKindError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "embedding provider '{}' must be openai_compatible or echo",
            self.value
        )
    }
}

impl Error for EmbeddingProviderKindError {}

impl ReadModelBackendMode {
    /// Parses a stable environment/config value.
    pub fn parse(value: &str) -> Result<Self, ReadModelBackendModeError> {
        match value.trim().to_ascii_lowercase().as_str() {
            "local" => Ok(Self::Local),
            "external" => Ok(Self::External),
            "disabled" => Ok(Self::Disabled),
            other => Err(ReadModelBackendModeError {
                value: other.to_owned(),
            }),
        }
    }

    /// Stable configuration label.
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Local => "local",
            Self::External => "external",
            Self::Disabled => "disabled",
        }
    }
}

/// Invalid read model backend mode supplied by runtime configuration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadModelBackendModeError {
    pub value: String,
}

impl fmt::Display for ReadModelBackendModeError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "retrieval backend '{}' must be local, external, or disabled",
            self.value
        )
    }
}

impl Error for ReadModelBackendModeError {}

/// Model metadata used by semantic/vector refresh workers and diagnostics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadModelMetadata {
    pub name: String,
    pub dimension: u32,
}

/// Runtime configuration for post-fusion result reranking.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RerankConfig {
    pub mode: RerankMode,
    pub model: Option<String>,
    pub timeout: Duration,
    pub candidate_multiplier: usize,
    pub max_candidates: usize,
}

impl RerankConfig {
    /// Uses the built-in deterministic reranker on an expanded local candidate pool.
    pub fn local() -> Self {
        Self {
            mode: RerankMode::Local,
            model: Some(LOCAL_RERANK_MODEL.to_owned()),
            timeout: DEFAULT_RERANK_TIMEOUT,
            candidate_multiplier: DEFAULT_RERANK_CANDIDATE_MULTIPLIER,
            max_candidates: DEFAULT_RERANK_MAX_CANDIDATES,
        }
    }

    /// Returns the storage candidate budget required before final truncation.
    pub fn candidate_limit(&self, requested_limit: usize) -> usize {
        let truncation_probe_limit = requested_limit.saturating_add(1);
        if self.mode == RerankMode::Disabled {
            return truncation_probe_limit;
        }

        let expanded = requested_limit
            .saturating_mul(self.candidate_multiplier)
            .max(truncation_probe_limit);
        expanded.min(self.max_candidates.max(truncation_probe_limit))
    }

    /// Applies the configured reranking policy to post-fusion candidates.
    pub fn rerank(
        &self,
        query: &str,
        hits: Vec<RetrievalHit>,
    ) -> (Vec<RetrievalHit>, RerankDiagnostics) {
        rerank::rerank_hits(query, hits, self)
    }
}

/// Runtime configuration for a remote embedding provider.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteEmbeddingConfig {
    pub provider: EmbeddingProviderKind,
    pub base_url: String,
    pub api_key: String,
    pub batch_size: usize,
    pub timeout: Duration,
    pub max_concurrency: usize,
}

impl RemoteEmbeddingConfig {
    /// Returns a URL label that is safe to expose in diagnostics.
    pub fn redacted_base_url(&self) -> String {
        redacted_url(&self.base_url)
    }
}

/// Runtime read model configuration shared by refresh and retrieval status.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReadModelBackendConfig {
    pub semantic_mode: ReadModelBackendMode,
    pub vector_mode: ReadModelBackendMode,
    pub semantic_model: ReadModelMetadata,
    pub vector_model: ReadModelMetadata,
    pub image_model: ReadModelMetadata,
    pub remote_embedding: Option<RemoteEmbeddingConfig>,
    pub rerank: RerankConfig,
}

impl ReadModelBackendConfig {
    /// Uses the built-in deterministic read models.
    pub fn local() -> Self {
        Self {
            semantic_mode: ReadModelBackendMode::Local,
            vector_mode: ReadModelBackendMode::Local,
            semantic_model: ReadModelMetadata {
                name: LOCAL_SEMANTIC_MODEL.to_owned(),
                dimension: LOCAL_VECTOR_DIMENSION,
            },
            vector_model: ReadModelMetadata {
                name: LOCAL_VECTOR_MODEL.to_owned(),
                dimension: LOCAL_VECTOR_DIMENSION,
            },
            image_model: ReadModelMetadata {
                name: "relay-local-image-hash-v1".to_owned(),
                dimension: LOCAL_VECTOR_DIMENSION,
            },
            remote_embedding: None,
            rerank: RerankConfig::local(),
        }
    }

    /// Returns whether local index refresh should maintain an index family.
    pub fn refreshes_index(&self, kind: IndexKind) -> bool {
        match kind {
            IndexKind::Bm25 => true,
            IndexKind::Semantic => self.semantic_mode != ReadModelBackendMode::Disabled,
            IndexKind::Vector => self.vector_mode != ReadModelBackendMode::Disabled,
        }
    }

    /// Returns read-model retrievers that must not execute for a request.
    pub fn disabled_retriever_sources(&self) -> Vec<RetrieverSource> {
        let mut disabled = Vec::new();
        if self.semantic_mode == ReadModelBackendMode::Disabled {
            disabled.push(RetrieverSource::Semantic);
        }
        if self.vector_mode == ReadModelBackendMode::Disabled {
            disabled.push(RetrieverSource::Vector);
        }

        disabled
    }
}

fn redacted_url(value: &str) -> String {
    let trimmed = value.trim();
    let Some((scheme, rest)) = trimmed.split_once("://") else {
        return trimmed.to_owned();
    };
    let authority = rest.split('/').next().unwrap_or(rest);
    let host = authority
        .rsplit_once('@')
        .map_or(authority, |(_, host)| host);
    if host.is_empty() {
        return scheme.to_owned();
    }

    format!("{scheme}://{host}")
}

/// Builds semantic/vector backend status from configured read models and index cursors.
pub fn read_model_backend_statuses(
    plan: &RetrievalPlan,
    graph_version: GraphVersion,
    indexes: &[IndexStatus],
    config: &ReadModelBackendConfig,
) -> Vec<RetrievalBackendStatus> {
    [
        (
            RetrieverSource::Semantic,
            IndexKind::Semantic,
            config.semantic_mode,
            &config.semantic_model,
        ),
        (
            RetrieverSource::Vector,
            IndexKind::Vector,
            config.vector_mode,
            &config.vector_model,
        ),
    ]
    .into_iter()
    .map(|(source, kind, mode, metadata)| {
        read_model_backend_status(source, kind, mode, metadata, plan, graph_version, indexes)
    })
    .collect()
}

fn read_model_backend_status(
    source: RetrieverSource,
    kind: IndexKind,
    mode: ReadModelBackendMode,
    metadata: &ReadModelMetadata,
    plan: &RetrievalPlan,
    graph_version: GraphVersion,
    indexes: &[IndexStatus],
) -> RetrievalBackendStatus {
    if mode == ReadModelBackendMode::Disabled {
        return RetrievalBackendStatus {
            source,
            state: RetrievalBackendState::Unavailable,
            scope_post_filter: plan.source_scope.is_some(),
            indexed_graph_version: None,
            reason: Some(format!(
                "{} read model disabled by configuration",
                source.as_str()
            )),
        };
    }

    let Some(index) = indexes.iter().find(|status| status.kind == kind) else {
        return RetrievalBackendStatus {
            source,
            state: RetrievalBackendState::Unavailable,
            scope_post_filter: plan.source_scope.is_some(),
            indexed_graph_version: None,
            reason: Some(format!("{} index metadata is unavailable", source.as_str())),
        };
    };
    let stale = index.is_stale_for(graph_version);
    let reason = if stale {
        format!(
            "{} read model index is stale at graph version {} while graph is {}; configured {} backend model={} dimension={}",
            source.as_str(),
            index.indexed_graph_version.get(),
            graph_version.get(),
            mode.as_str(),
            metadata.name,
            metadata.dimension
        )
    } else {
        format!(
            "{} read model available through {} backend model={} dimension={}",
            source.as_str(),
            mode.as_str(),
            metadata.name,
            metadata.dimension
        )
    };

    RetrievalBackendStatus {
        source,
        state: if stale {
            RetrievalBackendState::Degraded
        } else {
            RetrievalBackendState::Available
        },
        scope_post_filter: plan.source_scope.is_some(),
        indexed_graph_version: Some(index.indexed_graph_version),
        reason: Some(reason),
    }
}

impl RetrievalPlan {
    /// Validates query text and result limits.
    pub fn new(
        query: impl Into<String>,
        source_scope: Option<String>,
        limit: usize,
        freshness: FreshnessPolicy,
    ) -> Result<Self, RetrievalPlanError> {
        let query = query.into();
        let trimmed = query.trim();
        if trimmed.is_empty() {
            return Err(RetrievalPlanError::EmptyQuery);
        }

        let limit = match limit {
            1..=50 => limit,
            0 => return Err(RetrievalPlanError::ZeroLimit),
            _ => return Err(RetrievalPlanError::LimitTooLarge { max: 50 }),
        };

        Ok(Self {
            query: trimmed.to_owned(),
            source_scope,
            limit,
            freshness,
        })
    }
}

/// Retrieval planning error mapped to stable API errors by application.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RetrievalPlanError {
    EmptyQuery,
    ZeroLimit,
    LimitTooLarge { max: usize },
}

impl fmt::Display for RetrievalPlanError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyQuery => write!(formatter, "query must not be empty"),
            Self::ZeroLimit => write!(formatter, "limit must be greater than zero"),
            Self::LimitTooLarge { max } => write!(formatter, "limit must be {max} or less"),
        }
    }
}

impl Error for RetrievalPlanError {}

#[cfg(test)]
#[path = "mod_tests.rs"]
mod tests;