Skip to main content

lean_ctx/core/providers/
mod.rs

1pub mod cache;
2pub mod config;
3pub mod config_provider;
4pub mod connection;
5pub mod credentials;
6pub mod github;
7pub mod gitlab;
8pub mod hardened_http;
9pub mod health;
10pub mod init;
11pub mod jira;
12pub mod jira_oauth;
13pub mod mcp_bridge;
14pub mod postgres;
15pub mod provenance;
16pub mod provider_trait;
17pub mod registry;
18pub mod scaffold;
19pub(crate) mod selected_gitlab;
20pub(crate) mod snapshot_projection;
21
22pub use provider_trait::{ContextPacket, ContextProvider, ProviderParams};
23pub use registry::{ProviderRegistry, global_registry};
24
25use serde::{Deserialize, Serialize};
26
27use crate::core::evidence::Claim;
28
29/// Intern a string to a process-global `&'static str`, leaking each *unique* value at
30/// most once. Provider constructors run per `ctx_provider`/`ctx_preload` call, so a
31/// naive `Box::leak` per construction leaked unboundedly; interning bounds the leak to
32/// the finite set of distinct provider ids/names/actions.
33pub(crate) fn intern(s: String) -> &'static str {
34    use std::collections::HashSet;
35    use std::sync::{Mutex, OnceLock};
36    static POOL: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
37    let pool = POOL.get_or_init(|| Mutex::new(HashSet::new()));
38    let mut guard = pool
39        .lock()
40        .unwrap_or_else(std::sync::PoisonError::into_inner);
41    if let Some(&existing) = guard.get(s.as_str()) {
42        return existing;
43    }
44    let leaked: &'static str = Box::leak(s.into_boxed_str());
45    guard.insert(leaked);
46    leaked
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct ProviderResult {
51    pub provider: String,
52    pub resource_type: String,
53    pub items: Vec<ProviderItem>,
54    pub total_count: Option<usize>,
55    pub truncated: bool,
56}
57
58#[derive(Debug, Clone, Default, Serialize, Deserialize)]
59pub struct ProviderItem {
60    pub id: String,
61    pub title: String,
62    pub state: Option<String>,
63    pub author: Option<String>,
64    pub created_at: Option<String>,
65    pub updated_at: Option<String>,
66    pub url: Option<String>,
67    pub labels: Vec<String>,
68    pub body: Option<String>,
69    /// Attributable evidence distilled from this item (confidence + source).
70    /// Empty for plain records; populated by research/extraction providers.
71    #[serde(default, skip_serializing_if = "Vec::is_empty")]
72    pub claims: Vec<Claim>,
73}
74
75impl ProviderResult {
76    pub fn format_compact(&self) -> String {
77        let mut out = self.compact_header(self.items.len());
78        for item in &self.items {
79            out.push_str(&item.format_compact());
80        }
81        out
82    }
83
84    /// The list line of [`Self::format_compact`] for `count` listed items.
85    pub(crate) fn compact_header(&self, count: usize) -> String {
86        format!(
87            "{} {} ({}{}):\n",
88            self.provider,
89            self.resource_type,
90            count,
91            if self.truncated { "+" } else { "" }
92        )
93    }
94}
95
96impl ProviderItem {
97    /// The lines of one item in [`ProviderResult::format_compact`]: all it
98    /// shows of the item, so it can be admitted as that item's source.
99    pub(crate) fn format_compact(&self) -> String {
100        let state = self.state.as_deref().unwrap_or("");
101        let labels = if self.labels.is_empty() {
102            String::new()
103        } else {
104            format!(" [{}]", self.labels.join(","))
105        };
106        let mut out = format!("  #{} {} ({}){}\n", self.id, self.title, state, labels);
107        for claim in &self.claims {
108            out.push_str("    ▸ ");
109            out.push_str(&claim.render());
110            out.push('\n');
111        }
112        out
113    }
114}