use std::collections::{BTreeMap, BTreeSet};
use std::sync::Mutex;
use std::time::Duration;
use super::wire_format::WireFormat;
pub const PREFLIGHT_HEADER: &str = "x-openlatch-preflight";
const UPSTREAM_UNREACHABLE_HEADER: &str = "x-openlatch-upstream";
pub const PREFLIGHT_TIMEOUT: Duration = Duration::from_secs(5);
const PREFLIGHT_BODY: &str =
r#"{"model":"claude-sonnet-4-5","max_tokens":1,"messages":[{"role":"user","content":"ping"}]}"#;
const PREFLIGHT_BODY_RESPONSES: &str =
r#"{"model":"gpt-5-codex","input":"preflight","max_output_tokens":16}"#;
const PREFLIGHT_BODY_CHAT_COMPLETIONS: &str = r#"{"model":"openlatch-preflight","max_tokens":1,"messages":[{"role":"user","content":"ping"}]}"#;
const PREFLIGHT_BODY_GENERATE_CONTENT: &str =
r#"{"contents":[{"role":"user","parts":[{"text":"ping"}]}]}"#;
const PREFLIGHT_BODY_OLLAMA_NATIVE: &str =
r#"{"model":"openlatch-preflight","messages":[],"stream":false}"#;
const PREFLIGHT_GOOGLE_MODEL: &str = "openlatch-preflight";
const PREFLIGHT_API_KEY: &str = "ol-preflight-not-a-key";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum Verdict {
#[default]
Pending,
Ok,
Failed(String),
}
impl Verdict {
pub fn label(&self) -> &'static str {
match self {
Self::Pending => "pending",
Self::Ok => "ok",
Self::Failed(_) => "failed",
}
}
pub fn error(&self) -> Option<&str> {
match self {
Self::Failed(e) => Some(e.as_str()),
_ => None,
}
}
pub fn is_ok(&self) -> bool {
matches!(self, Self::Ok)
}
}
#[derive(Debug, Default)]
pub struct WiringState {
wired: Mutex<BTreeMap<&'static str, bool>>,
verdict: Mutex<BTreeMap<&'static str, Verdict>>,
format: Mutex<BTreeMap<&'static str, WireFormat>>,
endpoints: std::sync::Mutex<BTreeMap<String, EndpointVerdict>>,
intercept: InterceptCell,
refusals: RefusalsCell,
interceptors: Mutex<BTreeMap<&'static str, BTreeSet<String>>>,
intercepted: Mutex<BTreeMap<&'static str, bool>>,
}
#[derive(Default)]
struct InterceptCell(crate::model_relay::InterceptSlot);
impl std::fmt::Debug for InterceptCell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let filled = self.0.read().map(|g| g.is_some()).unwrap_or(false);
f.debug_struct("InterceptCell")
.field("filled", &filled)
.finish()
}
}
#[derive(Default)]
struct RefusalsCell(std::sync::Arc<crate::model_relay::intercept::RefusalLedger>);
impl std::fmt::Debug for RefusalsCell {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RefusalsCell").finish_non_exhaustive()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EndpointVerdict {
pub code: &'static str,
pub detail: String,
}
fn lowercase_host(host: &str) -> std::borrow::Cow<'_, str> {
if host.bytes().any(|b| b.is_ascii_uppercase()) {
std::borrow::Cow::Owned(host.to_ascii_lowercase())
} else {
std::borrow::Cow::Borrowed(host)
}
}
impl WiringState {
fn read<T: Clone>(m: &Mutex<BTreeMap<&'static str, T>>) -> BTreeMap<&'static str, T> {
match m.lock() {
Ok(v) => v.clone(),
Err(poisoned) => poisoned.into_inner().clone(),
}
}
fn write<T>(
m: &Mutex<BTreeMap<&'static str, T>>,
f: impl FnOnce(&mut BTreeMap<&'static str, T>),
) {
match m.lock() {
Ok(mut v) => f(&mut v),
Err(poisoned) => f(&mut poisoned.into_inner()),
}
}
pub fn is_wired(&self, agent: &str) -> bool {
match self.wired.lock() {
Ok(v) => v.get(agent).copied().unwrap_or(false),
Err(poisoned) => poisoned.into_inner().get(agent).copied().unwrap_or(false),
}
}
pub fn set_wired(&self, agent: &'static str, wired: bool) {
Self::write(&self.wired, |m| {
m.insert(agent, wired);
});
}
pub fn set_wired_format(&self, agent: &'static str, format: WireFormat) {
Self::write(&self.format, |m| {
m.insert(agent, format);
});
}
pub fn wired_format(&self, agent: &str) -> Option<WireFormat> {
match self.format.lock() {
Ok(v) => v.get(agent).copied(),
Err(poisoned) => poisoned.into_inner().get(agent).copied(),
}
}
pub fn sole_wired_agent_for(&self, format: WireFormat) -> Option<&'static str> {
if !format.is_captured() {
return None;
}
let wired = Self::read(&self.wired);
let mut hit = None;
for (agent, agent_format) in Self::read(&self.format) {
if agent_format != format || !wired.get(agent).copied().unwrap_or(false) {
continue;
}
if hit.is_some() {
return None;
}
hit = Some(agent);
}
hit
}
pub fn set_endpoint_verdict(&self, key: &str, verdict: Option<EndpointVerdict>) {
let mut map = match self.endpoints.lock() {
Ok(m) => m,
Err(poisoned) => poisoned.into_inner(),
};
match verdict {
Some(v) => {
map.insert(key.to_string(), v);
}
None => {
map.remove(key);
}
}
}
pub fn endpoint_verdicts(&self) -> BTreeMap<String, EndpointVerdict> {
match self.endpoints.lock() {
Ok(m) => m.clone(),
Err(poisoned) => poisoned.into_inner().clone(),
}
}
pub fn verdict(&self, agent: &str) -> Verdict {
match self.verdict.lock() {
Ok(v) => v.get(agent).cloned().unwrap_or_default(),
Err(poisoned) => poisoned
.into_inner()
.get(agent)
.cloned()
.unwrap_or_default(),
}
}
pub fn set_verdict(&self, agent: &'static str, verdict: Verdict) {
Self::write(&self.verdict, |m| {
m.insert(agent, verdict);
});
}
pub fn verdicts(&self) -> BTreeMap<&'static str, Verdict> {
Self::read(&self.verdict)
}
pub fn wired_agents(&self) -> BTreeMap<&'static str, bool> {
Self::read(&self.wired)
}
pub fn seed(&self, agent: &'static str) {
Self::write(&self.wired, |m| {
m.entry(agent).or_insert(false);
});
Self::write(&self.verdict, |m| {
m.entry(agent).or_default();
});
}
pub fn intercept(&self) -> crate::model_relay::InterceptSlot {
self.intercept.0.clone()
}
pub fn refusals(&self) -> std::sync::Arc<crate::model_relay::intercept::RefusalLedger> {
self.refusals.0.clone()
}
pub fn set_interceptors(&self, declared: BTreeMap<&'static str, BTreeSet<String>>) {
match self.interceptors.lock() {
Ok(mut m) => *m = declared,
Err(poisoned) => *poisoned.into_inner() = declared,
}
}
pub fn sole_interceptor_for(&self, host: &str) -> Option<&'static str> {
let host = lowercase_host(host);
let scan = |m: &BTreeMap<&'static str, BTreeSet<String>>| {
let mut hit = None;
for (agent, hosts) in m {
if !hosts.contains(host.as_ref()) {
continue;
}
if hit.is_some() {
return None;
}
hit = Some(*agent);
}
hit
};
match self.interceptors.lock() {
Ok(m) => scan(&m),
Err(poisoned) => scan(&poisoned.into_inner()),
}
}
pub fn is_intercept_host(&self, host: &str) -> bool {
let host = lowercase_host(host);
let any = |m: &BTreeMap<&'static str, BTreeSet<String>>| {
m.values().any(|hosts| hosts.contains(host.as_ref()))
};
match self.interceptors.lock() {
Ok(m) => any(&m),
Err(poisoned) => any(&poisoned.into_inner()),
}
}
pub fn declares_interceptor(&self, agent: &str) -> bool {
match self.interceptors.lock() {
Ok(m) => m.contains_key(agent),
Err(poisoned) => poisoned.into_inner().contains_key(agent),
}
}
pub fn intercept_hosts(&self) -> BTreeMap<&'static str, BTreeSet<String>> {
match self.interceptors.lock() {
Ok(m) => m.clone(),
Err(poisoned) => poisoned.into_inner().clone(),
}
}
pub fn note_intercepted(&self, host: &str) {
let host = lowercase_host(host);
if self
.sole_interceptor_for(&host)
.is_some_and(|agent| self.intercept_proven(agent))
{
return;
}
let host = host.as_ref();
let declarers: Vec<&'static str> = match self.interceptors.lock() {
Ok(m) => m
.iter()
.filter(|(_, hosts)| hosts.contains(host))
.map(|(a, _)| *a)
.collect(),
Err(poisoned) => poisoned
.into_inner()
.iter()
.filter(|(_, hosts)| hosts.contains(host))
.map(|(a, _)| *a)
.collect(),
};
match self.intercepted.lock() {
Ok(mut m) => {
for agent in declarers {
m.insert(agent, true);
}
}
Err(poisoned) => {
let mut m = poisoned.into_inner();
for agent in declarers {
m.insert(agent, true);
}
}
}
}
pub fn intercept_proven(&self, agent: &str) -> bool {
match self.intercepted.lock() {
Ok(m) => m.get(agent).copied().unwrap_or(false),
Err(poisoned) => poisoned.into_inner().get(agent).copied().unwrap_or(false),
}
}
pub fn clear_intercept_proof(&self, agent: &'static str) {
match self.intercepted.lock() {
Ok(mut m) => {
m.insert(agent, false);
}
Err(poisoned) => {
poisoned.into_inner().insert(agent, false);
}
}
}
pub fn intercept_proofs(&self) -> BTreeMap<&'static str, bool> {
let declarers: Vec<&'static str> = match self.interceptors.lock() {
Ok(m) => m.keys().copied().collect(),
Err(poisoned) => poisoned.into_inner().keys().copied().collect(),
};
let proven = match self.intercepted.lock() {
Ok(m) => m.clone(),
Err(poisoned) => poisoned.into_inner().clone(),
};
declarers
.into_iter()
.map(|a| (a, proven.get(a).copied().unwrap_or(false)))
.collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ProbeShape {
AnthropicMessages,
OpenAiResponses,
OpenAiChatCompletions,
GoogleGenerateContent,
OllamaNative,
Reachability,
}
impl ProbeShape {
fn route(self) -> String {
match self {
Self::AnthropicMessages => "/v1/messages".to_string(),
Self::OpenAiResponses => "/v1/responses".to_string(),
Self::OpenAiChatCompletions => "/v1/chat/completions".to_string(),
Self::GoogleGenerateContent => {
format!("/v1beta/models/{PREFLIGHT_GOOGLE_MODEL}:generateContent")
}
Self::OllamaNative => "/api/chat".to_string(),
Self::Reachability => "/".to_string(),
}
}
fn method(self) -> reqwest::Method {
match self {
Self::Reachability => reqwest::Method::GET,
_ => reqwest::Method::POST,
}
}
fn body(self) -> &'static str {
match self {
Self::AnthropicMessages => PREFLIGHT_BODY,
Self::OpenAiResponses => PREFLIGHT_BODY_RESPONSES,
Self::OpenAiChatCompletions => PREFLIGHT_BODY_CHAT_COMPLETIONS,
Self::GoogleGenerateContent => PREFLIGHT_BODY_GENERATE_CONTENT,
Self::OllamaNative => PREFLIGHT_BODY_OLLAMA_NATIVE,
Self::Reachability => "",
}
}
fn auth_headers(self) -> &'static [(&'static str, &'static str)] {
match self {
Self::AnthropicMessages => &[
("anthropic-version", "2023-06-01"),
("x-api-key", PREFLIGHT_API_KEY),
],
Self::OpenAiResponses | Self::OpenAiChatCompletions => &[(
"authorization",
concat!("Bearer ", "ol-preflight-not-a-key"),
)],
Self::GoogleGenerateContent => &[("x-goog-api-key", PREFLIGHT_API_KEY)],
Self::OllamaNative | Self::Reachability => &[],
}
}
fn of(fmt: WireFormat) -> Self {
match fmt {
WireFormat::AnthropicMessages => Self::AnthropicMessages,
WireFormat::OpenAiResponses => Self::OpenAiResponses,
WireFormat::OpenAiChatCompletions => Self::OpenAiChatCompletions,
WireFormat::GoogleGenerateContent => Self::GoogleGenerateContent,
WireFormat::OllamaNative => Self::OllamaNative,
WireFormat::Unknown => Self::Reachability,
}
}
}
pub const CA_REASON_MARKER: &str = "ca-untrusted: ";
pub const SETTINGS_REASON_MARKER: &str = "settings: ";
pub const SETTINGS_FOREIGN: &str = "holds a proxy OpenLatch did not write";
pub const SETTINGS_NO_FILE: &str = "the agent's settings file is not on this host";
pub const SETTINGS_CONTENDED: &str = "the file kept changing under the write";
pub const SETTINGS_LAYOUT: &str = "the settings file's layout cannot take the key safely";
pub const CA_EXPIRING_PREFIX: &str = "certificate authority expires ";
fn find_subslice(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack
.windows(needle.len())
.position(|window| window == needle)
}
fn parse_status_line(head: &str) -> Result<u16, String> {
let first_line = head.lines().next().unwrap_or("");
first_line
.split_whitespace()
.nth(1)
.and_then(|code| code.parse::<u16>().ok())
.ok_or_else(|| format!("could not parse a status line from {first_line:?}"))
}
pub async fn probe_intercept(
cfg: &crate::config::Config,
port: u16,
fmt: WireFormat,
upstream: &str,
ca_pem: &std::path::Path,
host: &str,
timeout: Duration,
) -> Result<(), String> {
match tokio::time::timeout(
timeout,
probe_intercept_body(cfg, port, fmt, upstream, ca_pem, host),
)
.await
{
Ok(inner) => inner,
Err(_) => Err(format!(
"the intercept probe for {host} did not complete within {}s",
timeout.as_secs()
)),
}
}
async fn probe_intercept_body(
cfg: &crate::config::Config,
port: u16,
fmt: WireFormat,
upstream: &str,
ca_pem: &std::path::Path,
host: &str,
) -> Result<(), String> {
use tokio::io::AsyncWriteExt;
crate::core::egress::tunnel::route_supported(&cfg.egress)?;
let mut stream = tokio::net::TcpStream::connect(("127.0.0.1", port))
.await
.map_err(|e| format!("relay unreachable on 127.0.0.1:{port}: {e}"))?;
let connect_req =
format!("CONNECT {host}:443 HTTP/1.1\r\nHost: {host}:443\r\n{PREFLIGHT_HEADER}: 1\r\n\r\n");
stream
.write_all(connect_req.as_bytes())
.await
.map_err(|e| format!("could not send CONNECT to the relay: {e}"))?;
let head = read_head(&mut stream)
.await
.map_err(|e| format!("could not read the relay's CONNECT response: {e}"))?;
let status = parse_status_line(&head)?;
if status != 200 {
return Err(format!("relay refused CONNECT {host}:443 with {status}"));
}
use rustls::pki_types::pem::PemObject;
let mut roots = rustls::RootCertStore::empty();
let certs: Vec<rustls::pki_types::CertificateDer<'static>> =
rustls::pki_types::CertificateDer::pem_file_iter(ca_pem)
.map_err(|e| {
format!(
"{CA_REASON_MARKER}{} is missing or holds no certificate: {e}",
ca_pem.display()
)
})?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| {
format!(
"{CA_REASON_MARKER}{} is missing or holds no certificate: {e}",
ca_pem.display()
)
})?;
if certs.is_empty() {
return Err(format!(
"{CA_REASON_MARKER}{} is missing or holds no certificate",
ca_pem.display()
));
}
for cert in certs {
roots.add(cert).map_err(|e| {
format!(
"{CA_REASON_MARKER}{} could not be trusted as a root: {e}",
ca_pem.display()
)
})?;
}
let client_config = rustls::ClientConfig::builder_with_provider(std::sync::Arc::new(
rustls::crypto::ring::default_provider(),
))
.with_safe_default_protocol_versions()
.map_err(|e| format!("{CA_REASON_MARKER}TLS client config: {e}"))?
.with_root_certificates(roots)
.with_no_client_auth();
let connector = tokio_rustls::TlsConnector::from(std::sync::Arc::new(client_config));
let server_name = rustls::pki_types::ServerName::try_from(host.to_string())
.map_err(|e| format!("{host} is not a valid TLS server name: {e}"))?;
let mut tls = connector.connect(server_name, stream).await.map_err(|e| {
format!(
"{CA_REASON_MARKER}the certificate the relay presents for {host} is not signed by {}: {e}",
ca_pem.display()
)
})?;
let shape = ProbeShape::of(fmt);
let body = shape.body();
let mut req = format!(
"{} {} HTTP/1.1\r\nHost: {host}\r\n{PREFLIGHT_HEADER}: 1\r\n",
shape.method(),
shape.route(),
);
for (name, value) in shape.auth_headers() {
req.push_str(&format!("{name}: {value}\r\n"));
}
if !body.is_empty() {
req.push_str("content-type: application/json\r\n");
req.push_str(&format!("content-length: {}\r\n", body.len()));
}
req.push_str("\r\n");
req.push_str(body);
tls.write_all(req.as_bytes())
.await
.map_err(|e| format!("could not send the probe request through the tunnel: {e}"))?;
let head = read_head(&mut tls)
.await
.map_err(|e| format!("could not read the probe response through the tunnel: {e}"))?;
if head
.to_ascii_lowercase()
.lines()
.any(|line| line.starts_with(&format!("{UPSTREAM_UNREACHABLE_HEADER}:")))
{
return Err(format!(
"the model relay could not reach {upstream} — model calls would fail"
));
}
Ok(())
}
async fn read_head<R>(io: &mut R) -> Result<String, String>
where
R: tokio::io::AsyncRead + Unpin,
{
use tokio::io::AsyncReadExt;
const HEAD_LIMIT: usize = 8192;
let mut buf = Vec::with_capacity(512);
loop {
if buf.len() >= HEAD_LIMIT {
return Err("response head exceeded 8 KiB".to_string());
}
let mut chunk = [0u8; 512];
let n = io.read(&mut chunk).await.map_err(|e| e.to_string())?;
if n == 0 {
return Err("the connection closed before the head completed".to_string());
}
buf.extend_from_slice(&chunk[..n]);
if let Some(pos) = find_subslice(&buf, b"\r\n\r\n") {
return Ok(String::from_utf8_lossy(&buf[..pos]).into_owned());
}
}
}
fn untrusted_remedy(owns_machine: bool) -> String {
if owns_machine {
crate::model_relay::trust_store::install_command(&crate::model_relay::ca::ca_pem_path(
&crate::model_relay::ca::ca_dir(&crate::config::openlatch_dir()),
))
} else {
"only the machine's own install trusts its relay certificate".to_string()
}
}
pub fn prove_store(
store: &dyn crate::model_relay::trust_store::TrustStore,
ic: &crate::model_relay::ca::Interceptor,
host: &str,
) -> Result<(), String> {
let leaf = ic.mint_leaf_pem(host).map_err(|e| {
format!(
"{CA_REASON_MARKER}could not mint a leaf for {host}: {}",
e.message
)
})?;
match store.verify_leaf(&leaf, host) {
Ok(true) => Ok(()),
Ok(false) => Err(format!(
"{CA_REASON_MARKER}the user trust store does not trust the relay's CA for {host} — {}",
untrusted_remedy(crate::supervision::owns_machine_supervision())
)),
Err(e) => Err(format!(
"{CA_REASON_MARKER}the trust store could not be asked: {}",
e.message
)),
}
}
pub async fn probe(
port: u16,
fmt: WireFormat,
upstream: &str,
timeout: Duration,
) -> Result<(), String> {
let client = match crate::egress::client_builder().timeout(timeout).build() {
Ok(c) => c,
Err(e) => return Err(format!("could not build the preflight client: {e}")),
};
let shape = ProbeShape::of(fmt);
let url = format!("http://127.0.0.1:{port}{}", shape.route());
let mut req = client
.request(shape.method(), &url)
.header(PREFLIGHT_HEADER, "1");
if !shape.body().is_empty() {
req = req
.header("content-type", "application/json")
.body(shape.body());
}
for (name, value) in shape.auth_headers() {
req = req.header(*name, *value);
}
let sent = req.send().await;
let resp = match sent {
Ok(r) => r,
Err(e) if e.is_timeout() => {
return Err(format!(
"no response from the model relay on 127.0.0.1:{port} within {}s",
timeout.as_secs()
))
}
Err(e) => {
return Err(format!(
"could not reach the model relay on 127.0.0.1:{port}: {}",
e.without_url()
))
}
};
if resp.headers().contains_key(UPSTREAM_UNREACHABLE_HEADER) {
return Err(format!(
"the model relay is listening but could not reach {upstream} — model calls would fail"
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model_relay::{mock, serve_ephemeral, ModelRelayState};
use std::sync::Arc;
fn state_for(upstream_port: u16) -> Arc<ModelRelayState> {
let base = reqwest::Url::parse(&format!("http://127.0.0.1:{upstream_port}")).unwrap();
Arc::new(ModelRelayState::new(base, 0, 8, &[]))
}
#[test]
fn only_the_machine_install_is_told_to_change_os_trust() {
let isolated = untrusted_remedy(false);
assert!(!isolated.contains("add-trusted-cert") && !isolated.contains("certutil"));
#[cfg(target_os = "macos")]
assert!(untrusted_remedy(true).contains("add-trusted-cert"));
}
#[test]
fn sole_interceptor_for_names_one_declarer_and_nobody_for_two() {
let ws = WiringState::default();
ws.set_interceptors(BTreeMap::from([(
"cline",
BTreeSet::from(["a.test".to_string(), "qwen.gpu.test".to_string()]),
)]));
assert_eq!(ws.sole_interceptor_for("A.TEST"), Some("cline"));
assert_eq!(ws.sole_interceptor_for("qwen.gpu.test"), Some("cline"));
let mut two = ws.intercept_hosts();
two.insert("fake", BTreeSet::from(["a.test".to_string()]));
ws.set_interceptors(two);
assert_eq!(ws.sole_interceptor_for("a.test"), None);
assert_eq!(ws.sole_interceptor_for("qwen.gpu.test"), Some("cline"));
assert_eq!(ws.sole_interceptor_for("undeclared.test"), None);
assert!(ws.declares_interceptor("cline"));
assert!(!ws.declares_interceptor("codex"));
}
#[test]
fn a_decrypted_request_proves_every_declarer_until_rewired() {
let ws = WiringState::default();
ws.set_interceptors(BTreeMap::from([
("cline", BTreeSet::from(["a.test".to_string()])),
("fake", BTreeSet::from(["a.test".to_string()])),
]));
assert!(!ws.intercept_proven("cline"));
assert!(!ws.intercept_proven("fake"));
ws.note_intercepted("a.test");
assert!(ws.intercept_proven("cline"));
assert!(ws.intercept_proven("fake"));
ws.clear_intercept_proof("cline");
assert!(!ws.intercept_proven("cline"));
assert!(ws.intercept_proven("fake"));
let proofs = ws.intercept_proofs();
assert_eq!(
proofs.keys().copied().collect::<Vec<_>>(),
vec!["cline", "fake"]
);
}
#[test]
fn interceptors_never_enter_the_format_rung() {
let ws = WiringState::default();
ws.set_wired("claude-code", true);
ws.set_wired_format("claude-code", WireFormat::AnthropicMessages);
ws.set_wired("cline", true);
ws.set_interceptors(BTreeMap::from([(
"cline",
BTreeSet::from(["api.anthropic.com".to_string()]),
)]));
assert_eq!(
ws.sole_wired_agent_for(WireFormat::AnthropicMessages),
Some("claude-code")
);
ws.set_wired_format("cline", WireFormat::AnthropicMessages);
assert_eq!(ws.sole_wired_agent_for(WireFormat::AnthropicMessages), None);
}
#[test]
fn probe_paths_resolve_to_their_own_format() {
for fmt in [
WireFormat::AnthropicMessages,
WireFormat::OpenAiResponses,
WireFormat::OpenAiChatCompletions,
WireFormat::GoogleGenerateContent,
WireFormat::OllamaNative,
] {
let route = ProbeShape::of(fmt).route();
let (parts, _) = axum::http::Request::builder()
.method(axum::http::Method::POST)
.uri(format!("http://127.0.0.1:1{route}"))
.body(())
.expect("a request")
.into_parts();
assert_eq!(
WireFormat::resolve(&parts),
fmt,
"{fmt:?}'s probe route {route} must resolve to {fmt:?}, or the probe \
is forwarded to another format's upstream"
);
}
assert!(
ProbeShape::of(WireFormat::GoogleGenerateContent)
.route()
.ends_with(":generateContent"),
"the colon suffix is what the Google arm matches on — a bare \
/v1beta/models resolves Unknown and goes to Anthropic"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn preflight_probes_each_format_in_its_own_shape() {
let cases = [
(
WireFormat::OpenAiChatCompletions,
"/v1/chat/completions",
"\"messages\"",
"\"input\"",
),
(
WireFormat::GoogleGenerateContent,
":generateContent",
"\"contents\"",
"\"messages\"",
),
(
WireFormat::OllamaNative,
"POST /api/chat",
"\"stream\":false",
"max_tokens",
),
];
for (fmt, route_marker, expected, forbidden) in cases {
let upstream = mock::spawn_capture_200().await;
let port = serve_ephemeral(state_for(upstream.port)).await;
probe(port, fmt, "http://127.0.0.1", PREFLIGHT_TIMEOUT)
.await
.unwrap_or_else(|e| panic!("{fmt:?} probe: {e}"));
let line = upstream
.received_request_line
.lock()
.expect("the mock recorded a request line")
.clone()
.expect("a request arrived");
assert!(
line.contains(route_marker),
"{fmt:?} must probe its own route, got: {line}"
);
let body = String::from_utf8(
upstream
.received_body
.lock()
.expect("the mock recorded a body")
.clone()
.expect("a body arrived"),
)
.expect("utf8");
assert!(
body.contains(expected),
"{fmt:?} must probe in its own body shape, got: {body}"
);
assert!(
!body.contains(forbidden),
"{fmt:?} must not carry another format's body, got: {body}"
);
}
}
#[tokio::test(flavor = "multi_thread")]
async fn ollama_native_probe_posts_api_chat_and_unknown_probes_reachability() {
let upstream = mock::spawn_capture_200().await;
let port = serve_ephemeral(state_for(upstream.port)).await;
probe(
port,
WireFormat::Unknown,
"http://127.0.0.1",
PREFLIGHT_TIMEOUT,
)
.await
.expect("reachability probe");
let line = upstream
.received_request_line
.lock()
.expect("lock")
.clone()
.expect("a request arrived");
assert!(line.starts_with("GET / "), "{line}");
assert_eq!(upstream.header("x-api-key"), None);
assert_eq!(upstream.header("authorization"), None);
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_passes_when_upstream_answers() {
let upstream = mock::spawn_capture_200().await;
let port = serve_ephemeral(state_for(upstream.port)).await;
assert_eq!(
probe(
port,
WireFormat::AnthropicMessages,
crate::model_relay::ANTHROPIC_BASE,
PREFLIGHT_TIMEOUT
)
.await,
Ok(())
);
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_fails_when_upstream_is_unreachable() {
let dead = mock::closed_port().await;
let port = serve_ephemeral(state_for(dead)).await;
let err = probe(
port,
WireFormat::AnthropicMessages,
crate::model_relay::ANTHROPIC_BASE,
PREFLIGHT_TIMEOUT,
)
.await
.unwrap_err();
assert!(
err.contains("could not reach"),
"an unreachable upstream must be named as such, got: {err}"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_fails_fast_on_a_silent_upstream() {
let hung = mock::spawn_hang_after_accept().await;
let upstream = reqwest::Url::parse(&format!("http://127.0.0.1:{hung}")).unwrap();
let state = Arc::new(
ModelRelayState::new(upstream, 0, 8, &[]).with_header_timeout(Duration::from_secs(60)),
);
let port = serve_ephemeral(state).await;
let started = std::time::Instant::now();
let err = probe(
port,
WireFormat::AnthropicMessages,
crate::model_relay::ANTHROPIC_BASE,
Duration::from_millis(300),
)
.await
.unwrap_err();
assert!(
err.contains("no response"),
"a silent upstream must read as no response, got: {err}"
);
assert!(
started.elapsed() < Duration::from_secs(5),
"the probe must return on its own budget, not the forward path's"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_fails_when_nothing_is_listening() {
let port = mock::closed_port().await;
assert!(probe(
port,
WireFormat::AnthropicMessages,
crate::model_relay::ANTHROPIC_BASE,
Duration::from_millis(500)
)
.await
.is_err());
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_sends_the_format_it_was_given() {
let upstream = mock::spawn_capture_200().await;
let port = serve_ephemeral(state_for(upstream.port)).await;
assert_eq!(
probe(
port,
WireFormat::OpenAiResponses,
crate::model_relay::wire_format::OPENAI_BASE,
PREFLIGHT_TIMEOUT
)
.await,
Ok(())
);
let line = upstream
.received_request_line
.lock()
.unwrap()
.clone()
.expect("the mock recorded the request line");
assert!(
line.starts_with("POST /v1/responses"),
"a Responses probe must speak the Responses route, got: {line}"
);
assert_eq!(
upstream.header("anthropic-version"),
None,
"an Anthropic protocol header on an OpenAI request is the exact defect this gate exists to catch"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_carries_no_real_credential() {
let anthropic_up = mock::spawn_capture_200().await;
let port = serve_ephemeral(state_for(anthropic_up.port)).await;
assert_eq!(
probe(
port,
WireFormat::AnthropicMessages,
crate::model_relay::ANTHROPIC_BASE,
PREFLIGHT_TIMEOUT
)
.await,
Ok(())
);
assert_eq!(
anthropic_up.header("x-api-key").as_deref(),
Some(PREFLIGHT_API_KEY),
"the Anthropic probe must send the not-a-key literal"
);
let responses_up = mock::spawn_capture_200().await;
let port = serve_ephemeral(state_for(responses_up.port)).await;
assert_eq!(
probe(
port,
WireFormat::OpenAiResponses,
crate::model_relay::wire_format::OPENAI_BASE,
PREFLIGHT_TIMEOUT
)
.await,
Ok(())
);
assert_eq!(
responses_up.header("authorization").as_deref(),
Some(format!("Bearer {PREFLIGHT_API_KEY}").as_str()),
"the Responses probe must send the same not-a-key literal as a Bearer"
);
}
#[test]
fn verdict_labels_are_stable() {
assert_eq!(Verdict::default(), Verdict::Pending);
assert_eq!(Verdict::Pending.label(), "pending");
assert_eq!(Verdict::Ok.label(), "ok");
assert_eq!(Verdict::Failed("boom".into()).label(), "failed");
assert_eq!(Verdict::Failed("boom".into()).error(), Some("boom"));
assert_eq!(Verdict::Ok.error(), None);
assert!(Verdict::Ok.is_ok());
assert!(!Verdict::Pending.is_ok());
}
#[test]
fn wiring_state_round_trips() {
let st = WiringState::default();
assert!(!st.is_wired("claude-code"));
assert_eq!(st.verdict("claude-code"), Verdict::Pending);
st.set_wired("claude-code", true);
st.set_verdict("claude-code", Verdict::Ok);
assert!(st.is_wired("claude-code"));
assert_eq!(st.verdict("claude-code"), Verdict::Ok);
}
#[test]
fn wiring_state_is_keyed_per_agent() {
let st = WiringState::default();
st.set_wired("claude-code", true);
assert!(st.is_wired("claude-code"));
assert!(
!st.is_wired("codex-cli"),
"one agent's wiring says nothing about another's"
);
st.set_verdict("claude-code", Verdict::Ok);
assert_eq!(
st.verdict("codex-cli"),
Verdict::Pending,
"an agent with no entry is Pending — not the other agent's verdict"
);
st.set_verdict("codex-cli", Verdict::Failed("no round trip".into()));
let verdicts = st.verdicts();
assert_eq!(verdicts.get("claude-code"), Some(&Verdict::Ok));
assert_eq!(
verdicts.get("codex-cli"),
Some(&Verdict::Failed("no round trip".into())),
"the snapshot the admin surface renders carries every agent"
);
let wired = st.wired_agents();
assert_eq!(wired.get("claude-code"), Some(&true));
assert_eq!(
wired.get("codex-cli"),
None,
"a verdict is not a write: codex-cli was judged, never wired"
);
st.seed("cline");
st.seed("claude-code");
assert_eq!(st.verdict("cline"), Verdict::Pending);
assert_eq!(st.wired_agents().get("cline"), Some(&false));
assert_eq!(
st.verdict("claude-code"),
Verdict::Ok,
"re-seeding must never overwrite a verdict the supervisor recorded"
);
assert_eq!(st.wired_agents().get("claude-code"), Some(&true));
}
#[test]
fn a_lone_speaker_is_named() {
let st = WiringState::default();
st.set_wired("claude-code", true);
st.set_wired_format("claude-code", WireFormat::AnthropicMessages);
st.set_wired("codex-cli", true);
st.set_wired_format("codex-cli", WireFormat::OpenAiResponses);
assert_eq!(
st.sole_wired_agent_for(WireFormat::AnthropicMessages),
Some("claude-code")
);
assert_eq!(
st.sole_wired_agent_for(WireFormat::OpenAiResponses),
Some("codex-cli")
);
}
#[test]
fn two_speakers_of_one_format_name_nobody() {
let st = WiringState::default();
st.set_wired("claude-code", true);
st.set_wired_format("claude-code", WireFormat::AnthropicMessages);
st.set_wired("cline", true);
st.set_wired_format("cline", WireFormat::AnthropicMessages);
assert_eq!(st.sole_wired_agent_for(WireFormat::AnthropicMessages), None);
}
#[test]
fn an_unwired_agent_does_not_speak() {
let st = WiringState::default();
st.set_wired_format("claude-code", WireFormat::AnthropicMessages);
st.set_wired("claude-code", false);
assert_eq!(st.sole_wired_agent_for(WireFormat::AnthropicMessages), None);
st.set_wired("cline", true);
st.set_wired_format("cline", WireFormat::AnthropicMessages);
assert_eq!(
st.sole_wired_agent_for(WireFormat::AnthropicMessages),
Some("cline"),
"the unwired agent must not block the one that is actually wired"
);
}
#[test]
fn no_speaker_names_nobody() {
let st = WiringState::default();
st.set_wired("claude-code", true);
st.set_wired_format("claude-code", WireFormat::AnthropicMessages);
assert_eq!(st.sole_wired_agent_for(WireFormat::OpenAiResponses), None);
}
#[test]
fn the_uncaptured_format_is_never_attributable() {
let st = WiringState::default();
st.set_wired("claude-code", true);
st.set_wired_format("claude-code", WireFormat::Unknown);
assert_eq!(st.sole_wired_agent_for(WireFormat::Unknown), None);
}
#[test]
fn wiring_state_hands_out_one_slot_and_one_ledger() {
let wiring = WiringState::default();
assert!(
Arc::ptr_eq(&wiring.intercept(), &wiring.intercept()),
"the relay and the supervisor must share one intercept slot"
);
assert!(
Arc::ptr_eq(&wiring.refusals(), &wiring.refusals()),
"the relay and the supervisor must share one refusal ledger"
);
}
async fn intercepting_relay(
hosts: &[&'static str],
) -> (u16, std::path::PathBuf, tempfile::TempDir, u16) {
use crate::model_relay::ca::{ca_dir, ca_pem_path, Interceptor};
let dir = tempfile::tempdir().expect("tempdir");
let ca_root = ca_dir(dir.path());
let interceptor =
Arc::new(Interceptor::new(&ca_root, hosts.iter().copied()).expect("interceptor"));
let ca_pem = ca_pem_path(&ca_root);
let upstream = mock::spawn_always_200().await;
let map: std::collections::BTreeMap<String, String> = [(
WireFormat::AnthropicMessages.as_str().to_string(),
format!("http://127.0.0.1:{upstream}"),
)]
.into_iter()
.collect();
let intercept_slot: crate::model_relay::InterceptSlot = Default::default();
*intercept_slot.write().unwrap() = Some(interceptor);
let state = Arc::new(
ModelRelayState::new(
reqwest::Url::parse(&format!("http://127.0.0.1:{upstream}")).unwrap(),
0,
8,
&[],
)
.with_upstream_map(map)
.with_explicit_upstreams([WireFormat::AnthropicMessages.as_str()].into())
.with_intercept(intercept_slot),
);
let port = serve_ephemeral(state).await;
(port, ca_pem, dir, upstream)
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_intercept_passes_with_the_on_disk_ca() {
let (port, ca_pem, _dir, upstream) = intercepting_relay(&["a.test"]).await;
let cfg = crate::config::Config::defaults();
probe_intercept(
&cfg,
port,
WireFormat::AnthropicMessages,
&format!("http://127.0.0.1:{upstream}"),
&ca_pem,
"a.test",
PREFLIGHT_TIMEOUT,
)
.await
.expect("probe_intercept must pass with the on-disk CA");
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_intercept_fails_with_the_marker_when_the_ca_is_wrong() {
let (port, _ca_pem, _dir, upstream) = intercepting_relay(&["a.test"]).await;
let wrong_dir = tempfile::tempdir().expect("tempdir");
let wrong_root = crate::model_relay::ca::ca_dir(wrong_dir.path());
crate::model_relay::ca::LocalCa::load_or_generate(&wrong_root).expect("a different CA");
let wrong_ca_pem = crate::model_relay::ca::ca_pem_path(&wrong_root);
let cfg = crate::config::Config::defaults();
let err = probe_intercept(
&cfg,
port,
WireFormat::AnthropicMessages,
&format!("http://127.0.0.1:{upstream}"),
&wrong_ca_pem,
"a.test",
PREFLIGHT_TIMEOUT,
)
.await
.expect_err("a mismatched CA must fail the TLS handshake");
assert!(err.starts_with(CA_REASON_MARKER), "{err}");
}
#[tokio::test(flavor = "multi_thread")]
async fn probe_intercept_refuses_an_unsupported_route() {
use crate::core::egress::{EgressConfig, ProxyMode};
let (port, ca_pem, _dir, upstream) = intercepting_relay(&["a.test"]).await;
let mut manual_cfg = crate::config::Config::defaults();
manual_cfg.egress = EgressConfig {
mode: ProxyMode::Manual,
url: Some("https://proxy.test:3128".to_string()),
..EgressConfig::direct()
};
let err = probe_intercept(
&manual_cfg,
port,
WireFormat::AnthropicMessages,
&format!("http://127.0.0.1:{upstream}"),
&ca_pem,
"a.test",
PREFLIGHT_TIMEOUT,
)
.await
.expect_err("an https:// proxy route cannot carry an opaque tunnel yet");
assert!(
!err.starts_with(CA_REASON_MARKER),
"a route refusal is not a CA failure: {err}"
);
let direct_cfg = crate::config::Config::defaults();
probe_intercept(
&direct_cfg,
port,
WireFormat::AnthropicMessages,
&format!("http://127.0.0.1:{upstream}"),
&ca_pem,
"a.test",
PREFLIGHT_TIMEOUT,
)
.await
.expect("Direct egress must reach the scheme check and pass — it never reaches it");
}
#[test]
fn prove_store_passes_only_when_the_store_verifies_the_leaf() {
use crate::model_relay::ca::Interceptor;
use crate::model_relay::trust_store::test_support::FakeStore;
let dir = tempfile::tempdir().expect("tempdir");
let ca_root = crate::model_relay::ca::ca_dir(dir.path());
let interceptor = Interceptor::new(&ca_root, ["a.test"]).expect("interceptor");
let store = FakeStore::default();
store
.verify
.store(true, std::sync::atomic::Ordering::SeqCst);
prove_store(&store, &interceptor, "a.test").expect("verify: true must pass");
let store = FakeStore::default();
store
.verify
.store(false, std::sync::atomic::Ordering::SeqCst);
let err = prove_store(&store, &interceptor, "a.test").expect_err("verify: false must fail");
assert!(err.starts_with(CA_REASON_MARKER), "{err}");
}
}