pub(crate) mod capture;
mod clean;
mod detectors;
pub mod egress;
pub mod hud;
mod notes;
pub mod provider;
pub(crate) mod receipt_store;
pub mod recovery;
mod semantic;
pub mod stores;
use std::path::Path;
use lean_ctx_protocol::Sha256Digest;
use lean_ctx_protocol::context_gateway::{
ClassificationV1, ContextDecisionV1, ContextDispositionV1, DetectorSignalV1, GatewayModeV1,
ReasonCodeV1, TransformationKindV1, TrustLevelV1,
};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use crate::core::config::SecretDetectionConfig;
use crate::core::input_filters::FilterAction;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct ContextGatewayConfig {
pub enabled: bool,
pub mode: GatewayModeV1,
pub secrets: FilterAction,
pub pii: FilterAction,
pub injection: FilterAction,
pub classification: FilterAction,
pub max_inspected_bytes: usize,
pub detector_timeout_ms: u64,
pub hud: hud::HudPlacement,
}
pub const DEFAULT_MAX_INSPECTED_BYTES: usize = 8 * 1024 * 1024;
pub const DEFAULT_DETECTOR_TIMEOUT_MS: u64 = 2_000;
impl Default for ContextGatewayConfig {
fn default() -> Self {
Self {
enabled: true,
mode: GatewayModeV1::Developer,
secrets: FilterAction::Redact,
pii: FilterAction::Redact,
injection: FilterAction::Warn,
classification: FilterAction::Warn,
max_inspected_bytes: DEFAULT_MAX_INSPECTED_BYTES,
detector_timeout_ms: DEFAULT_DETECTOR_TIMEOUT_MS,
hud: hud::HudPlacement::Auto,
}
}
}
impl ContextGatewayConfig {
#[must_use]
pub fn enabled_effective(&self) -> bool {
if let Ok(value) = std::env::var("LEAN_CTX_CONTEXT_GATEWAY") {
return !matches!(value.trim(), "0" | "false" | "off");
}
self.enabled
}
}
#[derive(Debug, Clone)]
pub struct AdmissionPolicy {
pub gateway: ContextGatewayConfig,
pub secret_patterns: SecretDetectionConfig,
pub(crate) semantic: Option<semantic::SemanticDetector>,
}
impl AdmissionPolicy {
#[must_use]
pub fn from_config(config: &crate::core::config::Config) -> Self {
Self {
gateway: config.context_gateway.clone(),
secret_patterns: config.secret_detection.clone(),
semantic: crate::core::intelligence_runtime::semantic_detectors::detector(),
}
}
#[must_use]
pub fn builtin(gateway: ContextGatewayConfig, secret_patterns: SecretDetectionConfig) -> Self {
Self {
gateway,
secret_patterns,
semantic: None,
}
}
fn secrets_action(&self) -> FilterAction {
if self.secret_patterns.enabled {
self.gateway.secrets
} else {
FilterAction::Off
}
}
fn actions(&self) -> detectors::Actions {
detectors::Actions {
secrets: self.secrets_action(),
pii: self.gateway.pii,
injection: self.gateway.injection,
classification: self.gateway.classification,
}
}
fn budget(&self) -> detectors::Budget {
detectors::Budget {
max_bytes: self.gateway.max_inspected_bytes,
deadline: std::time::Duration::from_millis(self.gateway.detector_timeout_ms),
}
}
fn fingerprint(&self) -> Vec<u8> {
let value = serde_json::json!({
"gateway": &self.gateway,
"secret_patterns": &self.secret_patterns,
"semantic": self.semantic.is_some(),
});
serde_json::to_vec(&value).unwrap_or_default()
}
fn detectors_mandatory(&self) -> bool {
self.gateway.mode != GatewayModeV1::Developer
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct AdmissionCounts {
pub secrets: u32,
pub pii: u32,
pub injection: u32,
pub markings: u32,
pub incomplete: u32,
pub semantic: u32,
}
#[derive(Debug, Clone)]
pub struct Admission {
pub text: Option<String>,
pub classification: ClassificationV1,
pub trust: TrustLevelV1,
pub decision: ContextDecisionV1,
pub counts: AdmissionCounts,
}
impl Admission {
#[must_use]
pub fn disposition(&self) -> ContextDispositionV1 {
self.decision.disposition
}
#[must_use]
pub fn storable(&self) -> bool {
self.text.is_some() && self.classification < ClassificationV1::Restricted
}
#[must_use]
pub fn hud_line(&self) -> Option<String> {
self.hud_line_placed(true)
}
#[must_use]
pub fn hud_line_placed(&self, redaction_counts: bool) -> Option<String> {
let c = self.counts;
let mut parts = Vec::new();
let redacted = self.decision.disposition == ContextDispositionV1::AllowRedacted;
let verb = if redacted { "redacted" } else { "detected" };
let show_values = !redacted || redaction_counts;
if c.secrets > 0 && show_values {
parts.push(format!("{} secret(s) {verb}", c.secrets));
}
if c.pii > 0 && show_values {
parts.push(format!("{} PII value(s) {verb}", c.pii));
}
if c.injection > 0 {
parts.push(format!("{} prompt-injection signal(s)", c.injection));
}
if c.markings > 0 {
parts.push(format!("{} classification marking(s)", c.markings));
}
if c.incomplete > 0 {
let reasons: Vec<&str> = self
.decision
.reason_codes
.iter()
.map(ReasonCodeV1::as_str)
.filter(|code| code.starts_with("coverage.") || code.starts_with("detector."))
.collect();
parts.push(format!("not fully inspected ({})", reasons.join(", ")));
}
if c.semantic > 0 {
parts.push(format!("{} semantic finding(s)", c.semantic));
}
let semantic_gaps: Vec<&str> = self
.decision
.reason_codes
.iter()
.map(ReasonCodeV1::as_str)
.filter(|code| matches!(*code, "semantic.partial" | "semantic.unavailable"))
.collect();
if !semantic_gaps.is_empty() {
parts.push(format!(
"semantic check incomplete ({})",
semantic_gaps.join(", ")
));
}
if self.text.is_none() {
let reasons: Vec<&str> = self
.decision
.reason_codes
.iter()
.map(ReasonCodeV1::as_str)
.collect();
return Some(format!(
"[lean-ctx gateway: content withheld — {}]",
reasons.join(", ")
));
}
(!parts.is_empty()).then(|| format!("[lean-ctx gateway: {}]", parts.join(" · ")))
}
}
#[must_use]
pub fn admit(text: &str, path: Option<&Path>, policy: &AdmissionPolicy) -> Admission {
let object = content_digest(text);
let gateway = &policy.gateway;
if !gateway.enabled_effective() {
return Admission {
text: Some(text.to_owned()),
classification: gateway.mode.unclassified(),
trust: TrustLevelV1::Unknown,
decision: ContextDecisionV1 {
object,
disposition: ContextDispositionV1::Allow,
reason_codes: vec![reason("gateway.disabled")],
signals: Vec::new(),
required_transformations: Vec::new(),
},
counts: AdmissionCounts::default(),
};
}
let actions = policy.actions();
let findings = detectors::inspect(
text,
path,
&actions,
&policy.secret_patterns,
policy.budget(),
);
let mut counts = findings.counts();
let mut reasons = Vec::new();
let mut disposition = ContextDispositionV1::Allow;
for incomplete in findings.incomplete() {
reasons.push(incomplete);
if policy.detectors_mandatory() {
disposition = ContextDispositionV1::Deny;
}
}
let mut transformations = Vec::new();
for (action, hits, code) in [
(actions.secrets, counts.secrets, "secret"),
(actions.pii, counts.pii, "pii"),
(actions.injection, counts.injection, "injection"),
(actions.classification, counts.markings, "classification"),
] {
if hits == 0 {
continue;
}
let step = match action {
FilterAction::Block => {
reasons.push(reason(&format!("{code}.blocked")));
ContextDispositionV1::Deny
}
FilterAction::Redact if code == "classification" => {
reasons.push(reason(&format!("{code}.blocked")));
ContextDispositionV1::Deny
}
FilterAction::Redact => {
reasons.push(reason(&format!("{code}.redacted")));
if !transformations.contains(&TransformationKindV1::Redaction) {
transformations.push(TransformationKindV1::Redaction);
}
ContextDispositionV1::AllowRedacted
}
FilterAction::Warn => {
reasons.push(reason(&format!("{code}.flagged")));
ContextDispositionV1::Allow
}
FilterAction::Off => continue,
};
disposition = disposition.most_restrictive(step);
}
let semantic = policy
.semantic
.map(|detect| semantic::run(text, detect, &actions, gateway.max_inspected_bytes));
if let Some(outcome) = &semantic {
counts.semantic = outcome.hits;
for (step, code) in &outcome.steps {
reasons.push(code.clone());
if *step == ContextDispositionV1::AllowRedacted
&& !transformations.contains(&TransformationKindV1::Redaction)
{
transformations.push(TransformationKindV1::Redaction);
}
disposition = disposition.most_restrictive(*step);
}
if let Some(code) = &outcome.incomplete {
reasons.push(code.clone());
}
}
let mut delivered = if disposition == ContextDispositionV1::Deny {
None
} else {
let semantic_masked = semantic
.as_ref()
.filter(|outcome| !outcome.redactions.is_empty())
.map(|outcome| semantic::redact(text, &outcome.redactions));
let source = semantic_masked.as_deref().unwrap_or(text);
Some(findings.redact(source, &actions, &policy.secret_patterns))
};
if let Some(output) = &delivered
&& output != text
&& detectors::residual(output, &actions, &policy.secret_patterns)
{
reasons.push(reason("redaction.incomplete"));
disposition = ContextDispositionV1::Deny;
delivered = None;
}
let classification = findings.classification(gateway.mode, &actions);
let semantic_injection = semantic.as_ref().is_some_and(|outcome| {
outcome
.steps
.iter()
.any(|(_, code)| code.as_str().starts_with("semantic.prompt_injection"))
});
let trust = if counts.injection > 0 || semantic_injection {
TrustLevelV1::Untrusted
} else {
TrustLevelV1::Internal
};
let mut signals: Vec<DetectorSignalV1> = findings.signals();
if let Some(outcome) = semantic {
signals.push(outcome.signal);
}
Admission {
text: delivered,
classification,
trust,
decision: ContextDecisionV1 {
object,
disposition,
reason_codes: reasons,
signals,
required_transformations: transformations,
},
counts,
}
}
pub fn admit_source(
text: &str,
path: &str,
preserve_bytes: bool,
) -> Result<String, std::io::Error> {
let policy = AdmissionPolicy::from_config(&crate::core::config::Config::load_arc());
let fingerprint = policy.fingerprint();
let capture = capture::current();
if let Some(capture) = &capture {
capture.bind_policy(policy.gateway.mode, fingerprint_digest(&fingerprint));
}
let memo_key = (policy.semantic.is_none() && policy.gateway.enabled_effective())
.then(|| clean::key(&fingerprint, path, text));
if let Some(key) = &memo_key
&& let Some(decision) = clean::get(key)
{
if let Some(capture) = &capture {
capture.record(&decision, AdmissionCounts::default());
}
return Ok(text.to_owned());
}
let admission = admit(text, Some(Path::new(path)), &policy);
if let Some(capture) = &capture {
capture.record(&admission.decision, admission.counts);
}
if let Some(key) = memo_key
&& admission.disposition() == ContextDispositionV1::Allow
&& admission.decision.reason_codes.is_empty()
&& admission.text.as_deref() == Some(text)
{
clean::insert(key, admission.decision.clone());
}
let Some(admitted) = admission.text.as_deref() else {
let reasons: Vec<&str> = admission
.decision
.reason_codes
.iter()
.map(ReasonCodeV1::as_str)
.collect();
return Err(std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
format!(
"source content withheld by the context gateway ({})",
reasons.join(", ")
),
));
};
if preserve_bytes && admitted != text {
return Err(std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"source content withheld: the context gateway would rewrite a digest-bound source",
));
}
let redaction_counts = policy.gateway.hud.redaction_counts_in_band();
if let Some(line) = admission.hud_line_placed(redaction_counts) {
notes::record(admitted, line);
}
Ok(admission.text.unwrap_or_default())
}
#[must_use]
pub fn admit_uninspectable(path: Option<&Path>, bytes: u64, policy: &AdmissionPolicy) -> Admission {
let gateway = &policy.gateway;
let object = content_digest(&format!("uninspectable:{bytes}"));
let actions = policy.actions();
let signals = detectors::unsupported_signals(&actions, bytes);
let required = signals.iter().any(|signal| {
signal.coverage.kind == lean_ctx_protocol::context_gateway::CoverageKindV1::Unsupported
});
if !gateway.enabled_effective() || !required {
return admit("", path, policy);
}
let restricted_path =
path.is_some_and(|p| crate::core::io_boundary::is_secret_like(p).is_some());
let mandatory = policy.detectors_mandatory();
Admission {
text: (!mandatory).then(String::new),
classification: if restricted_path {
ClassificationV1::Restricted
} else {
gateway.mode.unclassified()
},
trust: TrustLevelV1::Unknown,
decision: ContextDecisionV1 {
object,
disposition: if mandatory {
ContextDispositionV1::Deny
} else {
ContextDispositionV1::Allow
},
reason_codes: vec![reason("coverage.unsupported_media")],
signals,
required_transformations: Vec::new(),
},
counts: AdmissionCounts {
incomplete: 1,
..AdmissionCounts::default()
},
}
}
pub fn admit_media(path: &str, bytes: u64) -> Result<Option<String>, std::io::Error> {
let policy = AdmissionPolicy::from_config(&crate::core::config::Config::load_arc());
let admission = admit_uninspectable(Some(Path::new(path)), bytes, &policy);
if let Some(capture) = capture::current() {
capture.bind_policy(
policy.gateway.mode,
fingerprint_digest(&policy.fingerprint()),
);
capture.record(&admission.decision, admission.counts);
}
if admission.text.is_none() {
return Err(std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"media withheld by the context gateway (coverage.unsupported_media)",
));
}
Ok((admission.counts.incomplete > 0)
.then(|| "[lean-ctx gateway: media not inspected (coverage.unsupported_media)]".to_owned()))
}
#[cfg(test)]
pub(crate) fn clear_clean_memo() {
clean::clear();
}
#[must_use]
pub fn note_for(admitted: &str) -> Option<String> {
notes::lookup(admitted)
}
fn reason(code: &str) -> ReasonCodeV1 {
ReasonCodeV1::new(code).expect("built-in reason codes are valid")
}
fn fingerprint_digest(fingerprint: &[u8]) -> Sha256Digest {
let digest = crate::core::agent_identity::hex_encode(&Sha256::digest(fingerprint));
Sha256Digest::new(format!("sha256:{digest}")).expect("SHA-256 digest is canonical")
}
fn content_digest(text: &str) -> Sha256Digest {
let digest = crate::core::agent_identity::hex_encode(&Sha256::digest(text.as_bytes()));
Sha256Digest::new(format!("sha256:{digest}")).expect("SHA-256 digest is canonical")
}
#[cfg(test)]
mod tests;
#[cfg(test)]
mod receipt_tests;
#[cfg(test)]
mod store_tests;
#[cfg(test)]
mod egress_tests;