use std::path::Path;
use std::sync::Arc;
use lean_ctx_protocol::ProtocolReference;
use lean_ctx_protocol::context_gateway::{
ClassificationV1, ContextDecisionReceiptV1, ContextDecisionV1, ContextDispositionV1,
DestinationLocalityV1, DestinationV1, ReasonCodeV1, TransformationKindV1,
};
use serde_json::Value;
use super::capture::{self, AdmissionCapture, CallIdentity, Delivered};
use super::{
AdmissionCounts, AdmissionPolicy, admit, admit_uninspectable, clean, fingerprint_digest, reason,
};
pub const PROXY_RECEIPT_KEY: &str = "<proxy>";
const STRUCTURAL_KEYS: &[&str] = &[
"model",
"role",
"type",
"id",
"tool_use_id",
"tool_call_id",
"call_id",
"name",
"cache_control",
"media_type",
"mime_type",
"mimeType",
"stop",
"stop_sequences",
"tool_choice",
"response_format",
"previous_response_id",
"status",
"detail",
"format",
];
const SEALED_KEYS: &[&str] = &["signature", "encrypted_content", "thought_signature"];
#[derive(Debug, Clone, Copy)]
pub struct EgressTarget<'a> {
pub provider: &'a str,
pub model: Option<&'a str>,
pub upstream_base: &'a str,
}
#[must_use]
pub fn destination(target: &EgressTarget<'_>) -> DestinationV1 {
let reference = |raw: &str| {
ProtocolReference::new(raw.to_owned()).ok().or_else(|| {
let digest = blake3::hash(raw.as_bytes()).to_hex();
ProtocolReference::new(digest[..32].to_owned()).ok()
})
};
DestinationV1 {
provider: reference(&target.provider.to_ascii_lowercase())
.unwrap_or_else(|| ProtocolReference::new("provider").expect("valid reference")),
model: target.model.and_then(reference),
locality: locality(target.upstream_base),
organization_managed: false,
account_ref: None,
region: None,
}
}
fn locality(upstream_base: &str) -> DestinationLocalityV1 {
let Some(host) = reqwest::Url::parse(upstream_base)
.ok()
.and_then(|url| url.host_str().map(str::to_ascii_lowercase))
else {
return DestinationLocalityV1::Unknown;
};
let bare = host.trim_start_matches('[').trim_end_matches(']');
let local = match bare.parse::<std::net::IpAddr>() {
Ok(ip) => ip.is_loopback(),
Err(_) => bare == "localhost" || bare.ends_with(".localhost"),
};
if local {
DestinationLocalityV1::Local
} else {
DestinationLocalityV1::Remote
}
}
#[derive(Debug)]
pub enum EgressBody {
Unchanged,
Rewritten(Value),
Refused(String),
}
#[derive(Debug)]
pub struct EgressOutcome {
pub body: EgressBody,
pub classification: Option<ClassificationV1>,
capture: Option<Arc<AdmissionCapture>>,
destination: DestinationV1,
}
impl EgressOutcome {
fn passthrough(destination: DestinationV1) -> Self {
Self {
body: EgressBody::Unchanged,
classification: None,
capture: None,
destination,
}
}
}
struct Walk<'a> {
policy: &'a AdmissionPolicy,
fingerprint: Vec<u8>,
capture: &'a AdmissionCapture,
remote: bool,
mandatory: bool,
refused: Option<String>,
rewritten: bool,
classification: Option<ClassificationV1>,
}
enum Verdict {
Keep,
Replace(String),
}
impl Walk<'_> {
fn record(&self, decision: &ContextDecisionV1, counts: AdmissionCounts) {
self.capture.record(decision, counts);
}
fn observe(&mut self, classification: ClassificationV1) {
self.classification = Some(
self.classification
.map_or(classification, |seen| seen.join(classification)),
);
}
fn refuse(&mut self, reasons: &[ReasonCodeV1]) {
if self.refused.is_none() {
let codes: Vec<&str> = reasons.iter().map(ReasonCodeV1::as_str).collect();
self.refused = Some(format!(
"request withheld by the context gateway ({})",
codes.join(", ")
));
}
}
fn admit_string(&mut self, text: &str, sealed: bool) -> Verdict {
if text.is_empty() {
return Verdict::Keep;
}
let key = self
.policy
.semantic
.is_none()
.then(|| clean::key(&self.fingerprint, "egress", text));
if let Some(key) = &key
&& let Some(decision) = clean::get(key)
{
self.observe(self.policy.gateway.mode.unclassified());
self.record(&decision, AdmissionCounts::default());
return Verdict::Keep;
}
let mut admission = admit(text, None, self.policy);
self.observe(admission.classification);
if self.remote
&& admission.text.is_some()
&& admission.classification >= ClassificationV1::Restricted
{
admission.text = None;
admission.decision.disposition = ContextDispositionV1::Deny;
admission
.decision
.reason_codes
.push(reason("destination.remote_restricted"));
}
if sealed
&& admission
.text
.as_deref()
.is_some_and(|delivered| delivered != text)
{
admission
.decision
.reason_codes
.push(reason("egress.sealed_content"));
admission
.decision
.required_transformations
.retain(|kind| *kind != TransformationKindV1::Redaction);
if self.mandatory {
admission.decision.disposition = ContextDispositionV1::Deny;
admission.text = None;
} else {
admission.decision.disposition = ContextDispositionV1::Allow;
admission.text = Some(text.to_owned());
}
}
self.record(&admission.decision, admission.counts);
if let Some(key) = key
&& admission.disposition() == ContextDispositionV1::Allow
&& admission.decision.reason_codes.is_empty()
&& admission.text.as_deref() == Some(text)
{
clean::insert(key, admission.decision.clone());
}
match admission.text {
Some(delivered) if delivered == text => Verdict::Keep,
Some(delivered) => {
self.rewritten = true;
Verdict::Replace(delivered)
}
None if sealed => {
self.refuse(&admission.decision.reason_codes);
Verdict::Keep
}
None => {
let codes: Vec<&str> = admission
.decision
.reason_codes
.iter()
.map(ReasonCodeV1::as_str)
.collect();
self.rewritten = true;
Verdict::Replace(format!(
"[lean-ctx gateway: content withheld — {}]",
codes.join(", ")
))
}
}
}
fn media(&mut self, bytes: usize) {
let admission = admit_uninspectable(None, bytes as u64, self.policy);
self.record(&admission.decision, admission.counts);
if admission.text.is_none() {
self.refuse(&admission.decision.reason_codes);
}
}
fn walk(&mut self, value: &mut Value, sealed: bool) {
match value {
Value::String(text) => {
if let Verdict::Replace(admitted) = self.admit_string(text, sealed) {
*text = admitted;
}
}
Value::Array(items) => {
for item in items {
self.walk(item, sealed);
}
}
Value::Object(map) => {
let sealed_block = sealed
|| map.keys().any(|key| SEALED_KEYS.contains(&key.as_str()))
|| matches!(
map.get("type").and_then(Value::as_str),
Some("redacted_thinking")
);
for (key, child) in map.iter_mut() {
let key = key.as_str();
if STRUCTURAL_KEYS.contains(&key) {
continue;
}
if SEALED_KEYS.contains(&key) || (key == "data" && sealed_block) {
continue;
}
if let Some(bytes) = media_bytes(key, child) {
self.media(bytes);
continue;
}
self.walk(child, sealed_block);
}
}
_ => {}
}
}
}
fn media_bytes(key: &str, value: &Value) -> Option<usize> {
match (key, value) {
("data", Value::String(data)) if data.len() > 256 && looks_base64(data) => Some(data.len()),
("url" | "image_url" | "file_data", Value::String(url)) if url.starts_with("data:") => {
Some(url.len())
}
_ => None,
}
}
fn looks_base64(data: &str) -> bool {
data.bytes()
.take(512)
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'+' | b'/' | b'=' | b'-' | b'_'))
}
#[must_use]
pub fn admit_request(doc: &Value, target: &EgressTarget<'_>) -> EgressOutcome {
let destination = destination(target);
let policy = AdmissionPolicy::from_config(&crate::core::config::Config::load_arc());
if !policy.gateway.enabled_effective() {
return EgressOutcome::passthrough(destination);
}
let fingerprint = policy.fingerprint();
let capture = AdmissionCapture::new();
capture.bind_policy(policy.gateway.mode, fingerprint_digest(&fingerprint));
let mut admitted = doc.clone();
let mut walk = Walk {
policy: &policy,
fingerprint,
capture: &capture,
remote: destination.locality != DestinationLocalityV1::Local,
mandatory: policy.detectors_mandatory(),
refused: None,
rewritten: false,
classification: None,
};
walk.walk(&mut admitted, false);
let body = match (walk.refused.take(), walk.rewritten) {
(Some(refusal), _) => EgressBody::Refused(refusal),
(None, true) => EgressBody::Rewritten(admitted),
(None, false) => EgressBody::Unchanged,
};
EgressOutcome {
body,
classification: walk.classification,
capture: Some(capture),
destination,
}
}
#[must_use]
pub fn admit_opaque(bytes: usize, target: &EgressTarget<'_>) -> EgressOutcome {
let destination = destination(target);
let policy = AdmissionPolicy::from_config(&crate::core::config::Config::load_arc());
if !policy.gateway.enabled_effective() || bytes == 0 {
return EgressOutcome::passthrough(destination);
}
let capture = AdmissionCapture::new();
capture.bind_policy(
policy.gateway.mode,
fingerprint_digest(&policy.fingerprint()),
);
let mut admission =
admit_uninspectable(Some(Path::new("egress.opaque")), bytes as u64, &policy);
admission
.decision
.reason_codes
.push(reason("egress.opaque_payload"));
capture.record(&admission.decision, admission.counts);
let body = if admission.text.is_some() {
EgressBody::Unchanged
} else {
EgressBody::Refused(
"request withheld by the context gateway (egress.opaque_payload)".to_owned(),
)
};
EgressOutcome {
body,
classification: None,
capture: Some(capture),
destination,
}
}
pub fn finish(
outcome: &EgressOutcome,
forwarded: Option<&[u8]>,
original_bytes: usize,
agent_id: Option<&str>,
) -> Option<ContextDecisionReceiptV1> {
let capture = outcome.capture.as_ref()?;
if capture.is_empty() {
return None;
}
capture.set_original_tokens((original_bytes / 4) as u64);
let text = forwarded.map(String::from_utf8_lossy).unwrap_or_default();
let delivered = Delivered {
text: &text,
is_error: forwarded.is_none(),
tokens: (text.len() / 4) as u64,
};
let identity = CallIdentity {
agent_id,
destination: Some(outcome.destination.clone()),
};
let receipt = capture::finish(capture, &identity, &delivered)?;
let tally = capture.security_tally();
let audit_agent = agent_id.unwrap_or("proxy");
crate::core::security_events::record("proxy", audit_agent, &tally);
match super::receipt_store::persist(
&receipt,
PROXY_RECEIPT_KEY,
capture.task_scope().as_deref(),
) {
Ok(digest) if !tally.is_empty() => {
crate::core::security_events::anchor_receipt("proxy", audit_agent, digest.hex());
}
Ok(_) => {}
Err(error) => tracing::warn!("proxy gateway receipt not persisted: {error}"),
}
Some(receipt)
}
pub async fn finish_off_runtime(
outcome: EgressOutcome,
forwarded: Option<Vec<u8>>,
original_bytes: usize,
agent_id: Option<String>,
) {
let persisted = tokio::task::spawn_blocking(move || {
finish(
&outcome,
forwarded.as_deref(),
original_bytes,
agent_id.as_deref(),
);
})
.await;
if let Err(error) = persisted {
tracing::warn!("proxy gateway receipt task failed: {error}");
}
}
#[must_use]
pub fn admitted_copy(doc: &Value, target: &EgressTarget<'_>) -> Option<Value> {
match admit_request(doc, target).body {
EgressBody::Unchanged => Some(doc.clone()),
EgressBody::Rewritten(admitted) => Some(admitted),
EgressBody::Refused(_) => None,
}
}