Skip to main content

lean_ctx/core/providers/
provider_trait.rs

1use super::{ProviderItem, ProviderResult};
2
3/// Plugin-ready trait for external context providers.
4///
5/// A ContextProvider connects LeanCTX to external data sources (issue trackers,
6/// CI systems, documentation, etc.) and returns structured context that flows
7/// through the standard compression and IR pipeline.
8///
9/// The existing GitLab provider implements this interface pattern.
10/// Future plugins will register implementations dynamically via the provider
11/// framework contract.
12pub trait ContextProvider: Send + Sync {
13    /// Unique identifier for this provider (e.g. "gitlab", "github", "jira").
14    fn id(&self) -> &'static str;
15
16    /// Human-readable display name.
17    fn display_name(&self) -> &'static str;
18
19    /// Returns the actions this provider supports (e.g. "issues", "mrs", "pipelines").
20    fn supported_actions(&self) -> &[&str];
21
22    /// Execute a provider action and return structured results.
23    fn execute(&self, action: &str, params: &ProviderParams) -> Result<ProviderResult, String>;
24
25    /// Stable, credential-bound identity of a read-only resource. A name alone
26    /// does not authorize reuse; providers without this contract remain unbound.
27    fn reuse_binding(&self, _action: &str, _params: &ProviderParams) -> Option<String> {
28        None
29    }
30
31    /// Fetch current evidence for one previously observed item. Never implement
32    /// this with a cached result or a project-only permission probe.
33    fn reacquire_item(
34        &self,
35        _action: &str,
36        _params: &ProviderParams,
37        _item_id: &str,
38    ) -> Result<ProviderResult, String> {
39        Err("provider does not support current item authorization".into())
40    }
41
42    /// TTL for caching results from this provider (in seconds).
43    fn cache_ttl_secs(&self) -> u64 {
44        120
45    }
46
47    /// What this connector actually guarantees (strategy package 2026-10-14,
48    /// doc 17). The default claims nothing it cannot prove: it lists, and it
49    /// cannot re-check a stored item, so its content stays personal-local and
50    /// is withheld from governed reuse.
51    fn descriptor(&self) -> ConnectorDescriptor {
52        ConnectorDescriptor::list_only("token or none")
53    }
54
55    /// Whether this provider requires authentication.
56    fn requires_auth(&self) -> bool {
57        true
58    }
59
60    /// Check if the provider is configured and ready to serve requests.
61    fn is_available(&self) -> bool;
62}
63
64/// How far a connector supports one capability. `Partial` always names its
65/// limit in the descriptor's `notes`.
66#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
67#[serde(rename_all = "snake_case")]
68pub enum CapabilityLevel {
69    Supported,
70    Partial,
71    Unsupported,
72}
73
74/// The connector contract of doc 17: what a source connector can do, how it
75/// fails and what it may write. Declared by the connector, rendered into the
76/// capability matrix and consulted before governed or shared use.
77#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
78pub struct ConnectorDescriptor {
79    /// How the principal is bound (token, OAuth data source, none).
80    pub principal_binding: &'static str,
81    pub search: CapabilityLevel,
82    pub list: CapabilityLevel,
83    pub get: CapabilityLevel,
84    pub delta: CapabilityLevel,
85    /// Re-check, with the principal's own credential, that a stored item is
86    /// still readable (`reacquire_item`).
87    pub revalidate_acl: CapabilityLevel,
88    /// Detect that a stored item changed since it was read.
89    pub revalidate_revision: CapabilityLevel,
90    pub delete_event: CapabilityLevel,
91    /// Source text is data, never instructions, whatever its detectors say.
92    pub content_trust: &'static str,
93    /// Mutations of the source: none. Writes belong to the host application.
94    pub write_surface: &'static str,
95    /// Error, timeout, rate limit or lost access: what the gateway does.
96    pub failure_policy: &'static str,
97    pub notes: &'static str,
98}
99
100impl ConnectorDescriptor {
101    pub(crate) const UNTRUSTED: &'static str = "untrusted data";
102    pub(crate) const NO_WRITES: &'static str = "none";
103    pub(crate) const FAIL_CLOSED: &'static str = "deny; no stale or cached answer";
104
105    pub(crate) fn list_only(principal_binding: &'static str) -> Self {
106        Self {
107            principal_binding,
108            search: CapabilityLevel::Unsupported,
109            list: CapabilityLevel::Supported,
110            get: CapabilityLevel::Unsupported,
111            delta: CapabilityLevel::Unsupported,
112            revalidate_acl: CapabilityLevel::Unsupported,
113            revalidate_revision: CapabilityLevel::Unsupported,
114            delete_event: CapabilityLevel::Unsupported,
115            content_trust: Self::UNTRUSTED,
116            write_surface: Self::NO_WRITES,
117            failure_policy: Self::FAIL_CLOSED,
118            notes: "no item re-check: personal-local use only, withheld from governed reuse",
119        }
120    }
121
122    /// Governed or shared reuse of a stored item needs a current ACL check.
123    pub fn allows_shared_reuse(&self) -> bool {
124        self.revalidate_acl == CapabilityLevel::Supported
125    }
126}
127
128/// Parameters passed to a provider action.
129#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
130pub struct ProviderParams {
131    pub project: Option<String>,
132    pub state: Option<String>,
133    pub limit: Option<usize>,
134    pub query: Option<String>,
135    pub id: Option<String>,
136}
137
138/// A context packet produced by a provider, ready for the IR pipeline.
139#[derive(Debug, Clone)]
140pub struct ContextPacket {
141    pub provider_id: String,
142    pub action: String,
143    pub items: Vec<ProviderItem>,
144    pub token_count_raw: usize,
145    pub token_count_compressed: usize,
146    pub cache_hit: bool,
147}