Skip to main content

lean_ctx/core/providers/
mod.rs

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