Skip to main content

paper/core/
effective.rs

1//! Which models are in effect, and why. Precedence: local pin > remote default
2//! > built-in (spec ยง4).
3
4use super::config::Config;
5use super::models::{MMARCO_RERANKER, MULTILINGUAL_EMBEDDING};
6use super::remote_config::{Deprecation, RemoteConfig};
7use chrono::NaiveDate;
8
9/// The one place the built-in defaults live.
10pub const BUILTIN_EMBEDDER: &str = MULTILINGUAL_EMBEDDING;
11pub const BUILTIN_RERANKER: &str = MMARCO_RERANKER;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Source {
15    Pinned,
16    Remote,
17    BuiltIn,
18}
19
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct Effective {
22    pub embedder: String,
23    pub reranker: String,
24    pub embedder_source: Source,
25    pub reranker_source: Source,
26}
27
28fn pick(pin: &Option<String>, remote: Option<&String>, builtin: &str) -> (String, Source) {
29    match (pin, remote) {
30        (Some(id), _) => (id.clone(), Source::Pinned),
31        (None, Some(id)) => (id.clone(), Source::Remote),
32        (None, None) => (builtin.to_string(), Source::BuiltIn),
33    }
34}
35
36/// An expired remote config contributes nothing.
37pub fn resolve(config: &Config, remote: Option<&RemoteConfig>, today: NaiveDate) -> Effective {
38    let remote = remote.filter(|r| !r.is_expired(today));
39    let (embedder, embedder_source) = pick(
40        &config.embedder,
41        remote.and_then(|r| r.defaults.embedder.as_ref()),
42        BUILTIN_EMBEDDER,
43    );
44    let (reranker, reranker_source) = pick(
45        &config.reranker,
46        remote.and_then(|r| r.defaults.reranker.as_ref()),
47        BUILTIN_RERANKER,
48    );
49    Effective {
50        embedder,
51        reranker,
52        embedder_source,
53        reranker_source,
54    }
55}
56
57/// The saved, signature-verified remote config if it is still valid today.
58/// Reads only local files; never touches the network.
59pub fn load_live_remote(today: NaiveDate) -> Option<RemoteConfig> {
60    let dir = super::paths::Paths::new().base_dir();
61    super::remote_config::load_cached(&dir, &super::remote_config::compiled_keys(), today)
62        .filter(|r| !r.is_expired(today))
63}
64
65impl Source {
66    pub fn label(self) -> &'static str {
67        match self {
68            Source::Pinned => "pinned",
69            Source::Remote => "remote",
70            Source::BuiltIn => "built-in",
71        }
72    }
73}
74
75/// Deprecations naming the embedder or reranker actually in use.
76pub fn deprecations_in_use<'a>(
77    remote: Option<&'a RemoteConfig>,
78    effective: &Effective,
79) -> Vec<&'a Deprecation> {
80    remote
81        .into_iter()
82        .flat_map(|r| &r.deprecated)
83        .filter(|d| d.id == effective.embedder || d.id == effective.reranker)
84        .collect()
85}
86
87/// The CLI notice: one stderr line per deprecated in-use model.
88pub fn note_deprecations(remote: Option<&RemoteConfig>, effective: &Effective) {
89    for d in deprecations_in_use(remote, effective) {
90        eprintln!("note: {} is deprecated: {}", d.id, d.message);
91    }
92}