From 565521564c35f3ffa5c469c5d59fd75ed8d139a8 Mon Sep 17 00:00:00 2001 From: Phoebus Date: Tue, 22 Sep 2026 02:36:22 -0700 Subject: [PATCH] feat(executor): implement the OpenCode agent-server adapter OpenCode is an agent server, not an OpenAI endpoint. Add a session/ message client over the established curl transport that implements ModelTransport, so the strict proposal parser, facts and review are reused. The probe enforces the loopback/TLS transport policy, pins the resolved address, supports basic auth, checks HTTP status before parsing, and reports truthful connection states (ready is reserved for a validated response). Accept only completed assistant text parts and delete the transient session on every path. Tests cover endpoint/policy, auth, a misleading 500, credential non-leakage and the request shape. --- .../crates/phoebus-executor/src/lib.rs | 1 + .../phoebus-executor/src/opencode_client.rs | 1041 +++++++++++++++++ .../phoebus-executor/src/opencode_server.rs | 868 ++++++++------ 3 files changed, 1563 insertions(+), 347 deletions(-) create mode 100644 installer-rs/crates/phoebus-executor/src/opencode_client.rs diff --git a/installer-rs/crates/phoebus-executor/src/lib.rs b/installer-rs/crates/phoebus-executor/src/lib.rs index 538a2cc..cae3015 100644 --- a/installer-rs/crates/phoebus-executor/src/lib.rs +++ b/installer-rs/crates/phoebus-executor/src/lib.rs @@ -18,6 +18,7 @@ pub mod kernel; pub mod log; pub mod nix; pub mod opencode; +pub mod opencode_client; pub mod opencode_server; pub mod portage; pub mod probe; diff --git a/installer-rs/crates/phoebus-executor/src/opencode_client.rs b/installer-rs/crates/phoebus-executor/src/opencode_client.rs new file mode 100644 index 0000000..06d553f --- /dev/null +++ b/installer-rs/crates/phoebus-executor/src/opencode_client.rs @@ -0,0 +1,1041 @@ +//! HTTP client and the OpenCode protocol adapter. +//! +//! OpenCode is an **agent server**, not an OpenAI-compatible model endpoint. +//! This module talks its real HTTP API (sessions and messages) over the +//! project's established transport (`curl` through [`Exec`]), so we do not +//! maintain a second handwritten HTTP parser. A [`HttpClient`] trait keeps the +//! protocol logic unit-testable with a fake, and a real TCP server can drive +//! the curl path in integration tests. +//! +//! The server API used here was verified against the shipped +//! `opencode 1.18.32`: +//! +//! * `GET /global/health` → `{healthy, version}` +//! * `GET /provider` → provider/model metadata +//! * `POST /session` → `{id}` +//! * `POST /session/{id}/message` → `{info, parts}` (synchronous reply) +//! * `POST /session/{id}/abort`, `DELETE /session/{id}` +//! +//! Only `TextPart`s of the assistant reply are accepted; tool, reasoning, +//! snapshot and user parts are ignored. The `/provider` catalog can contain a +//! `key` field upstream, so only ids and model names are ever extracted. + +use std::collections::BTreeMap; +use std::io::Write; +use std::net::IpAddr; +use std::time::Duration; + +use phoebus_ai::provider::{ChatRequest, ModelTransport, ProviderKind}; +use phoebus_ai::AiError; +use phoebus_core::SecretString; + +use crate::command::CommandSpec; +use crate::exec::Exec; + +/// Which HTTP method a request uses. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Method { + Get, + Post, + Delete, +} + +impl Method { + fn as_str(self) -> &'static str { + match self { + Method::Get => "GET", + Method::Post => "POST", + Method::Delete => "DELETE", + } + } +} + +/// A bounded HTTP request. Secrets travel in `auth`/`headers` and are injected +/// through curl's stdin configuration, never argv or logs. +#[derive(Debug, Clone)] +pub struct HttpRequest { + pub method: Method, + /// The full URL, already policy-checked by the caller. + pub url: String, + /// Non-secret headers (Content-Type, Accept, ...). + pub headers: Vec<(String, String)>, + /// Optional basic-auth password. The username is supplied separately. + pub auth: Option, + pub auth_user: String, + /// Request body; written to a 0600 temporary file for POST. + pub body: Option, + pub timeout: Duration, + pub connect_timeout: Duration, + pub max_body: u64, + /// Pin the connection to a resolved address (`host`, `port`, `ip`) so a + /// DNS answer that changes after the policy check cannot move the target. + pub resolve: Option<(String, u16, IpAddr)>, +} + +impl HttpRequest { + pub fn get(url: impl Into) -> Self { + Self::new(Method::Get, url) + } + + pub fn post(url: impl Into, body: impl Into) -> Self { + let mut req = Self::new(Method::Post, url); + req.body = Some(body.into()); + req + } + + pub fn delete(url: impl Into) -> Self { + Self::new(Method::Delete, url) + } + + fn new(method: Method, url: impl Into) -> Self { + Self { + method, + url: url.into(), + headers: vec![("Accept".into(), "application/json".into())], + auth: None, + auth_user: "opencode".into(), + body: None, + timeout: Duration::from_secs(120), + connect_timeout: Duration::from_secs(15), + max_body: 4 * 1024 * 1024, + resolve: None, + } + } +} + +/// A bounded HTTP response. The body is capped by [`HttpRequest::max_body`]. +#[derive(Debug, Clone)] +pub struct HttpResponse { + pub status: u16, + pub body: String, +} + +impl HttpResponse { + pub fn is_success(&self) -> bool { + (200..300).contains(&self.status) + } +} + +/// A pluggable HTTP transport. The production implementation is curl; tests +/// supply a fake or a real local server. +pub trait HttpClient { + fn request(&self, request: &HttpRequest) -> Result; +} + +/// The production transport: curl, driven through [`Exec`] so logging, +/// redaction and dry-run all apply. Credentials are written into curl's config +/// on **stdin** (hidden from argv and logs); the body travels in a 0600 temp +/// file. Redirects are disabled and the protocol is restricted to http/https. +pub struct CurlHttpClient<'a> { + exec: &'a Exec, +} + +impl<'a> CurlHttpClient<'a> { + pub fn new(exec: &'a Exec) -> Self { + Self { exec } + } +} + +const STATUS_MARKER: &str = "__PHOEBUS_STATUS__"; + +impl HttpClient for CurlHttpClient<'_> { + fn request(&self, request: &HttpRequest) -> Result { + let mut temp: Option = None; + let data_line = if let Some(body) = &request.body { + let mut file = tempfile::Builder::new() + .prefix("phoebus-opencode.") + .tempfile() + .map_err(|e| AiError::Provider(format!("temp file: {e}")))?; + file.write_all(body.as_bytes()) + .and_then(|_| file.flush()) + .map_err(|e| AiError::Provider(format!("temp file: {e}")))?; + let line = format!("data = \"@{}\"\n", file.path().display()); + temp = Some(file); + line + } else { + String::new() + }; + + let auth = match (&request.auth, request.auth_user.is_empty()) { + (Some(password), false) => { + use base64_lite::encode; + let token = + encode(format!("{}:{}", request.auth_user, password.expose()).as_bytes()); + self.exec.register_secret(password); + format!("header = \"Authorization: Basic {token}\"\n") + } + (Some(password), true) => { + use base64_lite::encode; + let token = encode(password.expose().as_bytes()); + self.exec.register_secret(password); + format!("header = \"Authorization: Bearer {token}\"\n") + } + _ => String::new(), + }; + + let mut headers = String::new(); + for (name, value) in &request.headers { + headers.push_str(&format!( + "header = \"{}: {}\"\n", + sanitize_header(name), + sanitize_header(value) + )); + } + + let resolve = match &request.resolve { + Some((host, port, ip)) => { + let addr = match ip { + IpAddr::V4(v4) => v4.to_string(), + IpAddr::V6(v6) => format!("[{v6}]"), + }; + format!("resolve = \"{host}:{port}:{addr}\"\n") + } + None => String::new(), + }; + + let config = format!( + "url = \"{url}\"\n\ + request = \"{method}\"\n\ + proto = \"=http,https\"\n\ + max-redirs = 0\n\ + connect-timeout = {connect}\n\ + max-time = {total}\n\ + max-filesize = {max}\n\ + silent\nshow-error\n\ + header = \"Expect:\"\n\ + {headers}{auth}{resolve}{data_line}\ + write-out = \"\\n{marker}%{{http_code}}\"\n", + url = request.url, + method = request.method.as_str(), + connect = request.connect_timeout.as_secs().max(1), + total = request.timeout.as_secs().max(1), + max = request.max_body, + marker = STATUS_MARKER, + ); + + let out = self + .exec + .run( + CommandSpec::new("ai", "OpenCode request", "curl") + .args(["-sS", "-K", "-"]) + .stdin(config.into_bytes()), + ) + .map_err(|e| AiError::Provider(format!("transport error: {e}")))?; + drop(temp); + + let stdout = out.stdout_lossy(); + let index = stdout + .rfind(STATUS_MARKER) + .ok_or_else(|| AiError::Provider("transport response had no status".into()))?; + let status: u16 = stdout[index + STATUS_MARKER.len()..] + .trim() + .parse() + .map_err(|_| AiError::Provider("transport response had an invalid status".into()))?; + let body = stdout[..index].trim_end_matches(['\n', '\r']).to_string(); + Ok(HttpResponse { status, body }) + } +} + +fn sanitize_header(value: &str) -> String { + value + .chars() + .filter(|c| !c.is_control() && *c != '"' && *c != '\\') + .collect() +} + +/// A base64 encoder for the `Authorization: Basic` header. Kept local and tiny +/// rather than adding a dependency for one header. +mod base64_lite { + const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + + pub fn encode(input: &[u8]) -> String { + let mut out = String::with_capacity(input.len().div_ceil(3) * 4); + for chunk in input.chunks(3) { + let b = [ + chunk[0], + *chunk.get(1).unwrap_or(&0), + *chunk.get(2).unwrap_or(&0), + ]; + out.push(TABLE[(b[0] >> 2) as usize] as char); + out.push(TABLE[(((b[0] & 0x03) << 4) | (b[1] >> 4)) as usize] as char); + if chunk.len() > 1 { + out.push(TABLE[(((b[1] & 0x0f) << 2) | (b[2] >> 6)) as usize] as char); + } else { + out.push('='); + } + if chunk.len() > 2 { + out.push(TABLE[(b[2] & 0x3f) as usize] as char); + } else { + out.push('='); + } + } + out + } +} + +// --------------------------------------------------------------------------- +// OpenCode protocol +// --------------------------------------------------------------------------- + +/// A parsed model catalog entry. Never carries upstream credentials. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct ProviderEntry { + pub id: String, + pub name: String, + pub models: Vec, + pub connected: bool, +} + +/// The provider/model catalog, extracted without ever reading a `key` field. +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct Catalog { + pub providers: Vec, +} + +impl Catalog { + pub fn model_count(&self) -> usize { + self.providers.iter().map(|p| p.models.len()).sum() + } + + pub fn connected_model_count(&self) -> usize { + self.providers + .iter() + .filter(|p| p.connected) + .map(|p| p.models.len()) + .sum() + } + + pub fn has_model(&self, provider: &str, model: &str) -> bool { + self.providers + .iter() + .any(|p| p.id == provider && p.models.iter().any(|m| m == model)) + } + + pub fn default_model(&self, provider: &str) -> Option<&str> { + self.providers + .iter() + .find(|p| p.id == provider) + .and_then(|p| p.models.first()) + .map(String::as_str) + } +} + +#[derive(Debug, serde::Deserialize)] +struct ProviderCatalogBody { + #[serde(default)] + all: Vec, + #[serde(default)] + connected: Vec, + #[serde(default)] + default: BTreeMap, +} + +#[derive(Debug, serde::Deserialize)] +struct RawProvider { + #[serde(default)] + id: String, + #[serde(default)] + name: String, + #[serde(default)] + models: BTreeMap, +} + +impl ProviderCatalogBody { + fn into_catalog(self) -> Catalog { + let mut defaults = self.default; + let connected: std::collections::BTreeSet = self.connected.into_iter().collect(); + let mut providers: Vec = self + .all + .into_iter() + .map(|p| ProviderEntry { + connected: connected.contains(&p.id), + id: p.id, + name: p.name, + models: p.models.into_keys().collect(), + }) + .collect(); + for provider in &mut providers { + if let Some(default) = defaults.remove(&provider.id) { + // Bubble the default model to the front so callers can pick a + // sensible default without a second request. + provider.models.sort(); + if let Some(pos) = provider.models.iter().position(|m| m == &default) { + let model = provider.models.remove(pos); + provider.models.insert(0, model); + } + } + } + Catalog { providers } + } +} + +/// Parse the `/provider` metadata body into a catalog without retaining any +/// credential-bearing field. A non-JSON or unexpected body is an error, never a +/// silently empty catalog. +pub fn parse_catalog(body: &str) -> Result { + // Require an object with an `all` array. Without this, serde's all-default + // struct would happily accept `[]` or another misleading shape and report + // an empty catalog instead of an incompatible server. + let value: serde_json::Value = serde_json::from_str(body) + .map_err(|e| AiError::Provider(format!("provider catalog was not valid JSON: {e}")))?; + if !value.is_object() || value.get("all").map(|v| v.is_array()) != Some(true) { + return Err(AiError::Provider( + "provider catalog was not the expected object".into(), + )); + } + let parsed: ProviderCatalogBody = serde_json::from_value(value) + .map_err(|e| AiError::Provider(format!("provider catalog was not usable: {e}")))?; + Ok(parsed.into_catalog()) +} + +#[derive(Debug, serde::Deserialize)] +struct SessionBody { + #[serde(default)] + id: String, +} + +#[derive(Debug, serde::Deserialize)] +struct MessageBody { + #[serde(default)] + info: MessageInfo, + #[serde(default)] + parts: Vec, +} + +#[derive(Debug, serde::Deserialize, Default)] +struct MessageInfo { + #[serde(default)] + role: String, + #[serde(default)] + error: Option, + #[serde(default)] + time: Option, +} + +#[derive(Debug, serde::Deserialize, Default)] +struct MessageTime { + #[serde(default)] + completed: Option, +} + +#[derive(Debug, serde::Deserialize, Default)] +struct Part { + #[serde(rename = "type", default)] + kind: String, + #[serde(default)] + text: Option, + #[serde(default)] + synthetic: Option, + #[serde(default)] + ignored: Option, +} + +/// The accepted assistant reply: only the completed assistant text parts. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct AssistantReply { + pub content: String, + pub completed: bool, +} + +impl MessageBody { + fn into_reply(self) -> Result { + if self.info.role != "assistant" { + return Err(AiError::Provider( + "the reply was not an assistant message".into(), + )); + } + if let Some(error) = &self.info.error { + // The upstream error object can echo provider text; keep only the + // error name, never a credential-bearing payload. + let name = error + .get("name") + .and_then(|v| v.as_str()) + .unwrap_or("provider error"); + return Err(AiError::Provider(format!("model error: {name}"))); + } + let mut content = String::new(); + for part in &self.parts { + if part.kind != "text" { + continue; + } + if part.synthetic == Some(true) || part.ignored == Some(true) { + continue; + } + if let Some(text) = &part.text { + if !content.is_empty() { + content.push('\n'); + } + content.push_str(text); + } + } + if content.trim().is_empty() { + return Err(AiError::Provider( + "the assistant returned no usable text".into(), + )); + } + let completed = self.info.time.as_ref().and_then(|t| t.completed).is_some(); + Ok(AssistantReply { content, completed }) + } +} + +/// A protocol client for one OpenCode server, provider and model. +pub struct OpenCodeClient<'a> { + http: &'a dyn HttpClient, + base: String, + auth: Option, + provider: String, + model: String, + timeout: Duration, + max_body: u64, + resolve: Option<(String, u16, IpAddr)>, +} + +impl<'a> OpenCodeClient<'a> { + pub fn new( + http: &'a dyn HttpClient, + base: impl Into, + provider: impl Into, + model: impl Into, + ) -> Self { + Self { + http, + base: base.into(), + auth: None, + provider: provider.into(), + model: model.into(), + timeout: Duration::from_secs(120), + max_body: 4 * 1024 * 1024, + resolve: None, + } + } + + pub fn with_auth(mut self, password: Option) -> Self { + self.auth = password; + self + } + + pub fn with_timeout(mut self, timeout: Duration) -> Self { + self.timeout = timeout; + self + } + + pub fn with_max_body(mut self, max_body: u64) -> Self { + self.max_body = max_body; + self + } + + pub fn with_resolve(mut self, resolve: Option<(String, u16, IpAddr)>) -> Self { + self.resolve = resolve; + self + } + + pub fn provider(&self) -> &str { + &self.provider + } + + pub fn model(&self) -> &str { + &self.model + } + + fn request(&self, mut request: HttpRequest) -> Result { + request.timeout = self.timeout; + request.max_body = self.max_body; + request.auth = self.auth.clone(); + request.resolve = self.resolve.clone(); + self.http.request(&request) + } + + fn url(&self, path: &str) -> String { + format!("{}{}", self.base.trim_end_matches('/'), path) + } + + fn error_for(&self, action: &str, response: &HttpResponse) -> AiError { + let hint = match response.status { + 401 => "authentication required".to_string(), + 403 => "authentication rejected".to_string(), + 404 => "endpoint not found (incompatible server?)".to_string(), + 429 => "rate limited".to_string(), + _ => format!("HTTP {}", response.status), + }; + AiError::Provider(format!("OpenCode {action} failed: {hint}")) + } + + /// Fetch the provider/model catalog. Uses the metadata endpoint only. + pub fn catalog(&self) -> Result { + let response = self.request(HttpRequest::get(self.url("/provider")))?; + if !response.is_success() { + return Err(self.error_for("provider catalog", &response)); + } + parse_catalog(&response.body) + } + + /// Create a fresh session bound to the selected provider/model. A unique + /// title makes ownership obvious and avoids reusing an existing session. + pub fn create_session(&self, title: &str) -> Result { + // Verified against 1.18.32: despite the OpenAPI schema, a `model` field + // on session creation is rejected as BadRequest. The model is selected + // on the message instead. + let body = serde_json::json!({ "title": title }).to_string(); + let mut request = HttpRequest::post(self.url("/session"), body); + request + .headers + .push(("Content-Type".into(), "application/json".into())); + let response = self.request(request)?; + if !response.is_success() { + return Err(self.error_for("session creation", &response)); + } + let parsed: SessionBody = serde_json::from_str(&response.body) + .map_err(|e| AiError::Provider(format!("session response was invalid: {e}")))?; + if parsed.id.is_empty() { + return Err(AiError::Provider("session response had no id".into())); + } + Ok(parsed.id) + } + + /// Send one message and return only the completed assistant reply. Tools are + /// disabled per message as defence in depth; the OS/process isolation of + /// the server is documented separately. + pub fn send_message( + &self, + session: &str, + system: &str, + user: &str, + ) -> Result { + let body = serde_json::json!({ + "system": system, + "model": { "providerID": self.provider, "modelID": self.model }, + "tools": disabled_tools(), + "parts": [{ "type": "text", "text": user }], + }) + .to_string(); + let mut request = HttpRequest::post(self.url(&format!("/session/{session}/message")), body); + request + .headers + .push(("Content-Type".into(), "application/json".into())); + let response = self.request(request)?; + if !response.is_success() { + return Err(self.error_for("message", &response)); + } + let parsed: MessageBody = serde_json::from_str(&response.body) + .map_err(|e| AiError::Provider(format!("message response was invalid: {e}")))?; + parsed.into_reply() + } + + pub fn abort(&self, session: &str) -> Result<(), AiError> { + let response = self.request(HttpRequest::post( + self.url(&format!("/session/{session}/abort")), + "", + ))?; + if response.is_success() { + Ok(()) + } else { + Err(self.error_for("abort", &response)) + } + } + + /// Best-effort session cleanup. Errors are reported to the caller but are + /// not fatal to an otherwise valid result. + pub fn delete_session(&self, session: &str) -> Result<(), AiError> { + let response = self.request(HttpRequest::delete( + self.url(&format!("/session/{session}")), + ))?; + if response.is_success() { + Ok(()) + } else { + Err(self.error_for("session delete", &response)) + } + } + + /// Create a session, send one message, clean up, and return the reply. + /// The session is deleted on every path, so installation facts are not + /// retained longer than necessary. + pub fn one_shot(&self, system: &str, user: &str) -> Result { + let session = self.create_session("phoebus assistant (transient)")?; + let result = self.send_message(&session, system, user); + let _ = self.delete_session(&session); + result + } +} + +fn disabled_tools() -> serde_json::Value { + serde_json::json!({ + "bash": false, "edit": false, "write": false, "read": false, + "webfetch": false, "grep": false, "glob": false, "list": false, + "patch": false, "task": false, "todowrite": false, "todoread": false, + }) +} + +/// A `ModelTransport` that adapts an OpenAI-shaped chat request to the OpenCode +/// session API, so `complete_proposal`/`complete_plan` and the whole review +/// pipeline are reused unchanged. The returned body is OpenAI-shaped because +/// the shared `extract_content` consumes it. +/// Adapts the OpenAI-shaped chat request that `complete_proposal` / +/// `complete_plan` build to the OpenCode session API. It borrows the caller's +/// [`Exec`] so logging, redaction and dry-run all apply, and returns an +/// OpenAI-shaped body because the shared `extract_content` consumes it. The +/// whole proposal/review pipeline is therefore reused unchanged. +pub struct ExecOpenCodeTransport<'a> { + exec: &'a Exec, + base: String, + provider: String, + model: String, + auth: Option, + timeout: Duration, + max_body: u64, + resolve: Option<(String, u16, IpAddr)>, +} + +impl<'a> ExecOpenCodeTransport<'a> { + pub fn new( + exec: &'a Exec, + base: impl Into, + provider: impl Into, + model: impl Into, + ) -> Self { + Self { + exec, + base: base.into(), + provider: provider.into(), + model: model.into(), + auth: None, + timeout: Duration::from_secs(120), + max_body: 4 * 1024 * 1024, + resolve: None, + } + } + + pub fn with_auth(mut self, password: Option) -> Self { + self.auth = password; + self + } + + pub fn with_resolve(mut self, resolve: Option<(String, u16, IpAddr)>) -> Self { + self.resolve = resolve; + self + } + + pub fn with_timeout(mut self, timeout: Duration) -> Self { + self.timeout = timeout; + self + } + + pub fn with_max_body(mut self, max_body: u64) -> Self { + self.max_body = max_body; + self + } +} + +fn openai_reply(content: &str) -> String { + serde_json::json!({ + "choices": [{ "message": { "content": content } }] + }) + .to_string() +} + +fn split_chat_request(body: &str) -> Result<(String, String), AiError> { + let request: ChatRequest = serde_json::from_str(body) + .map_err(|e| AiError::Provider(format!("chat request was invalid: {e}")))?; + let system = request + .messages + .iter() + .filter(|m| m.role == "system") + .map(|m| m.content.as_str()) + .collect::>() + .join("\n"); + let user = request + .messages + .iter() + .filter(|m| m.role == "user") + .map(|m| m.content.as_str()) + .collect::>() + .join("\n"); + if user.trim().is_empty() { + return Err(AiError::Provider("chat request had no user content".into())); + } + Ok((system, user)) +} + +impl ModelTransport for ExecOpenCodeTransport<'_> { + fn send(&self, _provider: ProviderKind, _url: &str, body: &str) -> Result { + let (system, user) = split_chat_request(body)?; + // Enforce the transport policy and pin the connection to the checked + // address, so the adapter cannot be pointed at a plain-http remote. + let endpoint = crate::opencode_server::Endpoint::parse(&self.base) + .map_err(|e| AiError::Rejected(format!("invalid OpenCode endpoint: {e}")))?; + let target = endpoint.checked_target()?; + let resolve = + self.resolve + .clone() + .or(Some((endpoint.host.clone(), endpoint.port, target.ip()))); + let http = CurlHttpClient::new(self.exec); + let client = OpenCodeClient::new(&http, &self.base, &self.provider, &self.model) + .with_auth(self.auth.clone()) + .with_timeout(self.timeout) + .with_max_body(self.max_body) + .with_resolve(resolve); + let reply = client.one_shot(&system, &user)?; + Ok(openai_reply(&reply.content)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::command::{CommandOutput, RecordingRunner}; + use phoebus_core::Redactor; + use std::sync::Mutex; + + #[test] + fn credentials_are_on_stdin_not_argv_and_are_redacted() { + let runner = RecordingRunner::new(); + runner.set_response( + "curl", + CommandOutput { + status: 0, + success: true, + stdout: b"\n__PHOEBUS_STATUS__200".to_vec(), + stderr: Vec::new(), + duration: std::time::Duration::from_millis(1), + }, + ); + let dir = tempfile::tempdir().unwrap().keep(); + let logger = crate::log::Logger::open(dir.join("l.log"), Redactor::new(), false); + let exec = crate::exec::Exec::new(Box::new(runner.clone()), logger, false, &dir, &dir); + let http = CurlHttpClient::new(&exec); + let mut request = HttpRequest::get("http://127.0.0.1:4096/provider"); + request.auth = Some(SecretString::new("s3cret-pw")); + let response = http.request(&request).unwrap(); + assert_eq!(response.status, 200); + + let spec = &runner.recorded()[0]; + assert_eq!(spec.program, "curl"); + assert!( + !spec.args.iter().any(|a| a.contains("s3cret-pw")), + "the password must not be in argv: {:?}", + spec.args + ); + let stdin = String::from_utf8(spec.stdin.clone().unwrap()).unwrap(); + assert!(stdin.contains("Authorization: Basic")); + assert!(stdin.contains(&base64_lite::encode(b"opencode:s3cret-pw"))); + // And it is registered for redaction. + assert_eq!(exec.redactor().redact("s3cret-pw"), ""); + } + + #[test] + fn a_missing_status_marker_is_an_error_not_a_silent_success() { + let runner = RecordingRunner::new(); + runner.set_response( + "curl", + CommandOutput { + status: 0, + success: true, + stdout: b"{}".to_vec(), + stderr: Vec::new(), + duration: std::time::Duration::from_millis(1), + }, + ); + let dir = tempfile::tempdir().unwrap().keep(); + let logger = crate::log::Logger::open(dir.join("l.log"), Redactor::new(), false); + let exec = crate::exec::Exec::new(Box::new(runner), logger, false, &dir, &dir); + let http = CurlHttpClient::new(&exec); + assert!(http + .request(&HttpRequest::get("http://127.0.0.1:4096/x")) + .is_err()); + } + + /// A scripted HTTP client: pops one response per request. + struct FakeHttp { + responses: Mutex>, + requests: Mutex>, + } + + impl FakeHttp { + fn new(responses: Vec) -> Self { + Self { + responses: Mutex::new(responses), + requests: Mutex::new(Vec::new()), + } + } + fn json(status: u16, body: impl Into) -> HttpResponse { + HttpResponse { + status, + body: body.into(), + } + } + fn requests(&self) -> Vec { + self.requests.lock().unwrap().clone() + } + } + + impl HttpClient for FakeHttp { + fn request(&self, request: &HttpRequest) -> Result { + self.requests.lock().unwrap().push(request.clone()); + let mut responses = self.responses.lock().unwrap(); + if responses.is_empty() { + return Err(AiError::Provider("no scripted response".into())); + } + Ok(responses.remove(0)) + } + } + + fn message_reply(text: &str) -> String { + serde_json::json!({ + "info": { "role": "assistant", "time": { "created": 1, "completed": 2 } }, + "parts": [{ "type": "text", "text": text }], + }) + .to_string() + } + + #[test] + fn base64_matches_known_vectors() { + assert_eq!(base64_lite::encode(b"opencode:pw"), "b3BlbmNvZGU6cHc="); + assert_eq!(base64_lite::encode(b""), ""); + assert_eq!(base64_lite::encode(b"f"), "Zg=="); + assert_eq!(base64_lite::encode(b"fo"), "Zm8="); + assert_eq!(base64_lite::encode(b"foo"), "Zm9v"); + } + + #[test] + fn catalog_never_reads_provider_keys() { + // Upstream can include a `key`; the catalog must not surface it. + let body = r#"{"all":[ + {"id":"deepseek","name":"DeepSeek","key":"sk-SECRET","options":{}, + "models":{"b":{},"a":{}}}, + {"id":"x","name":"X","models":{}}], + "connected":["deepseek"],"default":{"deepseek":"a"}}"#; + let catalog = parse_catalog(body).unwrap(); + let debug = format!("{catalog:?}"); + assert!(!debug.contains("SECRET"), "catalog leaked a key: {debug}"); + let ds = catalog + .providers + .iter() + .find(|p| p.id == "deepseek") + .unwrap(); + assert!(ds.connected); + // The default model is bubbled to the front. + assert_eq!(ds.models, vec!["a", "b"]); + assert!(catalog.has_model("deepseek", "a")); + assert!(!catalog.has_model("deepseek", "zzz")); + } + + #[test] + fn catalog_rejects_a_misleading_body() { + assert!(parse_catalog("not json").is_err()); + assert!(parse_catalog("[]").is_err()); + } + + #[test] + fn one_shot_accepts_only_the_assistant_text_and_cleans_up() { + let http = FakeHttp::new(vec![ + FakeHttp::json(200, r#"{"id":"ses_1"}"#), + // A user part, a tool part, a reasoning part and the assistant text. + FakeHttp::json( + 200, + r#"{"info":{"role":"assistant","time":{"created":1,"completed":2}}, + "parts":[ + {"type":"text","text":"user echo","synthetic":true}, + {"type":"tool","tool":"bash"}, + {"type":"reasoning","text":"thinking"}, + {"type":"text","text":"{\"version\":1}"}]}"#, + ), + FakeHttp::json(200, "true"), + ]); + let client = OpenCodeClient::new(&http, "http://127.0.0.1:4096", "p", "m"); + let reply = client.one_shot("sys", "user").unwrap(); + assert_eq!(reply.content, "{\"version\":1}"); + assert!(reply.completed); + let requests = http.requests(); + assert_eq!(requests.len(), 3); + assert_eq!(requests[0].method, Method::Post); + assert_eq!(requests[2].method, Method::Delete); + assert!(requests[2].url.ends_with("/session/ses_1")); + } + + #[test] + fn one_shot_rejects_a_non_assistant_or_errored_reply() { + let http = FakeHttp::new(vec![ + FakeHttp::json(200, r#"{"id":"ses_1"}"#), + FakeHttp::json( + 200, + r#"{"info":{"role":"user"},"parts":[{"type":"text","text":"hi"}]}"#, + ), + FakeHttp::json(200, "true"), + ]); + let client = OpenCodeClient::new(&http, "http://127.0.0.1:4096", "p", "m"); + assert!(client.one_shot("s", "u").is_err()); + + let http = FakeHttp::new(vec![ + FakeHttp::json(200, r#"{"id":"ses_1"}"#), + FakeHttp::json( + 200, + r#"{"info":{"role":"assistant","error":{"name":"ProviderAuthError","data":{"message":"bad sk-SECRET"}}},"parts":[]}"#, + ), + FakeHttp::json(200, "true"), + ]); + let client = OpenCodeClient::new(&http, "http://127.0.0.1:4096", "p", "m"); + let err = client.one_shot("s", "u").unwrap_err().to_string(); + assert!(err.contains("ProviderAuthError"), "{err}"); + assert!( + !err.contains("sk-SECRET"), + "error leaked a credential: {err}" + ); + } + + #[test] + fn failed_inference_is_reported_not_hidden() { + let http = FakeHttp::new(vec![ + FakeHttp::json(200, r#"{"id":"ses_1"}"#), + FakeHttp::json(200, r#"{"info":{"role":"assistant"},"parts":[]}"#), + ]); + let client = OpenCodeClient::new(&http, "http://127.0.0.1:4096", "p", "m"); + assert!(client.one_shot("s", "u").is_err()); + } + + #[test] + fn http_errors_are_distinguished() { + for (status, needle) in [ + (401u16, "authentication required"), + (403, "authentication rejected"), + (404, "not found"), + (500, "HTTP 500"), + ] { + let http = FakeHttp::new(vec![FakeHttp::json(status, "{}")]); + let client = OpenCodeClient::new(&http, "http://127.0.0.1:4096", "p", "m"); + let err = client.catalog().unwrap_err().to_string(); + assert!(err.contains(needle), "status {status}: {err}"); + } + } + + #[test] + fn session_creation_failure_is_reported() { + let http = FakeHttp::new(vec![FakeHttp::json(500, "boom")]); + let client = OpenCodeClient::new(&http, "http://127.0.0.1:4096", "p", "m"); + assert!(client.create_session("t").is_err()); + // A success body with no id is also refused. + let http = FakeHttp::new(vec![FakeHttp::json(200, "{}")]); + let client = OpenCodeClient::new(&http, "http://127.0.0.1:4096", "p", "m"); + assert!(client.create_session("t").is_err()); + } + + #[test] + fn transport_adapts_openai_request_shape() { + let http = FakeHttp::new(vec![ + FakeHttp::json(200, r#"{"id":"ses_1"}"#), + FakeHttp::json(200, message_reply("{\"version\":1}")), + FakeHttp::json(200, "true"), + ]); + let client = OpenCodeClient::new(&http, "http://127.0.0.1:4096", "prov", "mod"); + let reply = client.one_shot("system prompt", "user prompt").unwrap(); + assert_eq!(reply.content, "{\"version\":1}"); + let requests = http.requests(); + // requests[0] = session, requests[1] = message, requests[2] = delete + let body: serde_json::Value = + serde_json::from_str(requests[1].body.as_deref().unwrap()).unwrap(); + assert_eq!(body["model"]["providerID"], "prov"); + assert_eq!(body["model"]["modelID"], "mod"); + assert_eq!(body["system"], "system prompt"); + assert_eq!(body["tools"]["bash"], false); + assert_eq!(body["parts"][0]["type"], "text"); + assert_eq!(body["parts"][0]["text"], "user prompt"); + } +} diff --git a/installer-rs/crates/phoebus-executor/src/opencode_server.rs b/installer-rs/crates/phoebus-executor/src/opencode_server.rs index dd0a449..eba175f 100644 --- a/installer-rs/crates/phoebus-executor/src/opencode_server.rs +++ b/installer-rs/crates/phoebus-executor/src/opencode_server.rs @@ -1,29 +1,54 @@ -//! OpenCode **server** capability probe. +//! OpenCode **server** types, transport policy and capability probe. //! //! OpenCode is an agent application, not an OpenAI-compatible inference -//! endpoint. A successful TCP connect, a health check or an HTTP 200 is not -//! proof that a model is configured or that it can produce a valid Phoebus -//! proposal. This module makes those distinctions explicit so a caller can -//! report an honest connection state instead of assuming compatibility. +//! endpoint. A TCP connect, a health check or an HTTP 200 is not proof that a +//! model is configured, that the selected model exists, or that it can produce +//! a valid Phoebus proposal. The probe reports a typed state for each of those +//! distinctions, and only a real inference (`OpenCodeClient::one_shot`) can +//! reach [`ConnectionState::Ready`]. //! -//! It talks to the OpenCode HTTP API documented at -//! using a deliberately tiny HTTP/1.1 client (no TLS, no redirects, no -//! credentials in the URL). Remote access is expected through an authenticated, -//! encrypted tunnel to a loopback-bound server, never by exposing the server -//! directly. +//! The probe uses the shared [`HttpClient`] transport (curl through [`Exec`]), +//! never a second handwritten HTTP parser, and it keeps only ids and model +//! names from the provider catalog: upstream's catalog can contain credentials, +//! so raw bodies are never retained or surfaced. //! -//! The probe never returns provider credentials: upstream's `/config/providers` -//! response includes them, so only counts are kept. +//! ## Transport policy +//! +//! Plain `http://` is accepted only when the resolved destination is loopback; +//! remote access must be `https://` (certificate-verified by curl) or an +//! authenticated encrypted tunnel terminating on loopback. The policy resolves +//! the host and the connection is pinned to the checked address, so a DNS +//! answer that changes after the check cannot move the destination. -use std::io::{Read, Write}; -use std::net::{SocketAddr, TcpStream, ToSocketAddrs}; +use std::net::{SocketAddr, ToSocketAddrs}; use std::time::Duration; -use serde::{Deserialize, Serialize}; +use phoebus_ai::AiError; +use phoebus_core::SecretString; -/// A parsed `http://host:port` server endpoint. +use crate::opencode_client::{parse_catalog, Catalog, HttpClient, HttpRequest, HttpResponse}; + +/// The URL scheme of an endpoint. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Scheme { + Http, + Https, +} + +impl Scheme { + pub fn as_str(self) -> &'static str { + match self { + Scheme::Http => "http", + Scheme::Https => "https", + } + } +} + +/// A parsed `scheme://host:port` server endpoint. No path, query, fragment or +/// userinfo is accepted. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Endpoint { + pub scheme: Scheme, pub host: String, pub port: u16, } @@ -31,7 +56,9 @@ pub struct Endpoint { #[derive(Debug, Clone, PartialEq, Eq)] pub enum EndpointError { Empty, - NotHttp, + UnsupportedScheme, + UserInfo, + PathNotAllowed, BadHost, BadPort, } @@ -40,7 +67,9 @@ impl std::fmt::Display for EndpointError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let text = match self { EndpointError::Empty => "empty endpoint", - EndpointError::NotHttp => "only http:// endpoints are accepted", + EndpointError::UnsupportedScheme => "only http:// and https:// are accepted", + EndpointError::UserInfo => "userinfo (@) is not allowed in the endpoint", + EndpointError::PathNotAllowed => "a path, query or fragment is not allowed", EndpointError::BadHost => "the host is missing or contains an unsafe character", EndpointError::BadPort => "the port is missing or invalid", }; @@ -49,61 +78,119 @@ impl std::fmt::Display for EndpointError { } impl Endpoint { - /// Parse `http://host:port`. Plain HTTP only: remote access is over an - /// authenticated tunnel, not a direct TLS-less LAN bind. pub fn parse(value: &str) -> Result { let text = value.trim(); if text.is_empty() { return Err(EndpointError::Empty); } - let rest = text.strip_prefix("http://").ok_or(EndpointError::NotHttp)?; - // Reject userinfo, paths and control characters. - if rest.is_empty() - || rest.contains('/') - || rest.contains('@') - || rest.contains('"') - || rest.contains('\\') - || rest.chars().any(|c| c.is_whitespace() || c.is_control()) + let (scheme, rest) = if let Some(rest) = text.strip_prefix("http://") { + (Scheme::Http, rest) + } else if let Some(rest) = text.strip_prefix("https://") { + (Scheme::Https, rest) + } else { + return Err(EndpointError::UnsupportedScheme); + }; + if rest.is_empty() { + return Err(EndpointError::BadHost); + } + if rest.contains('@') { + return Err(EndpointError::UserInfo); + } + if rest.contains('/') || rest.contains('?') || rest.contains('#') { + return Err(EndpointError::PathNotAllowed); + } + if rest + .chars() + .any(|c| c.is_whitespace() || c.is_control() || c == '"' || c == '\\') { return Err(EndpointError::BadHost); } - let (host, port) = rest.rsplit_once(':').ok_or(EndpointError::BadPort)?; + // IPv6 literals are bracketed: [::1]:4096 + let (host, port) = if let Some(rest) = rest.strip_prefix('[') { + let end = rest.find(']').ok_or(EndpointError::BadHost)?; + let host = &rest[..end]; + let after = &rest[end + 1..]; + let port = after.strip_prefix(':').ok_or(EndpointError::BadPort)?; + (host.to_string(), port) + } else { + let (host, port) = rest.rsplit_once(':').ok_or(EndpointError::BadPort)?; + (host.to_string(), port) + }; if host.is_empty() { return Err(EndpointError::BadHost); } let port: u16 = port.parse().map_err(|_| EndpointError::BadPort)?; - Ok(Endpoint { - host: host.to_string(), - port, - }) + if port == 0 { + return Err(EndpointError::BadPort); + } + Ok(Endpoint { scheme, host, port }) } pub fn base(&self) -> String { - format!("http://{}:{}", self.host, self.port) + if self.host.contains(':') { + format!("{}://[{}]:{}", self.scheme.as_str(), self.host, self.port) + } else { + format!("{}://{}:{}", self.scheme.as_str(), self.host, self.port) + } } - fn socket_addrs(&self) -> std::io::Result> { - (self.host.as_str(), self.port) + /// Resolve the endpoint and enforce the transport policy. + /// + /// Returns the address the connection must be pinned to. `http` requires + /// every resolved address to be loopback; `https` requires a valid + /// certificate (checked by curl, never disabled). + pub fn checked_target(&self) -> Result { + let addrs: Vec = (self.host.as_str(), self.port) .to_socket_addrs() - .map(|it| it.collect()) + .map_err(|e| AiError::Provider(format!("cannot resolve {}: {e}", self.host)))? + .collect(); + if addrs.is_empty() { + return Err(AiError::Rejected(format!( + "{} did not resolve to any address", + self.host + ))); + } + if self.scheme == Scheme::Http { + for addr in &addrs { + if !addr.ip().is_loopback() { + return Err(AiError::Rejected(format!( + "refusing plain http to {} ({}); use https or a loopback tunnel", + self.host, + addr.ip() + ))); + } + } + } + Ok(addrs[0]) } } -/// The connection state a frontend should display. `Ready` means the server is -/// reachable, authenticated, API-compatible *and* has at least one model; it -/// still does not prove the model can emit a valid proposal (that needs a real -/// prompt round-trip). -#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +/// The connection state a frontend should display. Only [`ConnectionState::Ready`] +/// follows a real, validated model response for the current connection/model. +#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)] #[serde(rename_all = "snake_case")] pub enum ConnectionState { - Disconnected, + /// The destination did not satisfy the transport policy. + InsecureTransport, + /// The server could not be reached. + Unreachable, + /// The server demanded authentication we did not supply. AuthenticationRequired, - ReachableNoModel, + /// The server rejected the authentication we supplied. + AuthenticationRejected, + /// A health response arrived but the API is not the expected shape. Incompatible, + /// Healthy server with no model catalog. + Reachable, + /// Healthy server with an available model catalog. + ModelCatalog, + /// The selected provider/model is not in the catalog. + SelectedModelUnavailable, + /// A real model response was obtained and validated for this connection. Ready, } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] pub struct Capability { pub state: ConnectionState, pub tcp_reachable: bool, @@ -111,380 +198,467 @@ pub struct Capability { pub server_version: Option, pub provider_count: usize, pub model_count: usize, + pub connected_model_count: usize, + pub selected_provider: Option, + pub selected_model: Option, + pub selected_model_available: bool, pub detail: String, } impl Capability { - fn disconnected(detail: impl Into) -> Self { + fn new(state: ConnectionState, detail: impl Into) -> Self { Self { - state: ConnectionState::Disconnected, + state, tcp_reachable: false, healthy: false, server_version: None, provider_count: 0, model_count: 0, + connected_model_count: 0, + selected_provider: None, + selected_model: None, + selected_model_available: false, detail: detail.into(), } } } -#[derive(Debug, Deserialize)] -struct Health { +/// Inputs for a probe. `auth` is the server basic-auth password (distinct from +/// any model provider credential). +pub struct ProbeRequest { + pub auth: Option, + /// The provider the frontend intends to use, if any. + pub provider: Option, + /// The model the frontend intends to use, if any. + pub model: Option, + pub timeout: Duration, +} + +impl ProbeRequest { + pub fn new() -> Self { + Self { + auth: None, + provider: None, + model: None, + timeout: Duration::from_secs(10), + } + } +} + +impl Default for ProbeRequest { + fn default() -> Self { + Self::new() + } +} + +#[derive(Debug, serde::Deserialize)] +struct HealthBody { #[serde(default)] healthy: bool, #[serde(default)] version: Option, } -#[derive(Debug, Deserialize)] -struct Providers { - #[serde(default)] - providers: Vec, +fn http_base(endpoint: &Endpoint) -> String { + endpoint.base() } -#[derive(Debug, Deserialize)] -struct Provider { - #[serde(default)] - models: std::collections::BTreeMap, -} - -/// Probe an OpenCode server and classify the connection. -pub fn probe(endpoint: &Endpoint, timeout: Duration) -> Capability { - let addrs = match endpoint.socket_addrs() { - Ok(addrs) if !addrs.is_empty() => addrs, - Ok(_) => return Capability::disconnected("host did not resolve"), - Err(e) => return Capability::disconnected(format!("resolve failed: {e}")), - }; - // A failed TCP connect is the only way to be certain the service is down. - if TcpStream::connect_timeout(&addrs[0], timeout).is_err() { - return Capability::disconnected("connect failed"); +fn auth_required_state(auth_supplied: bool, status: u16) -> ConnectionState { + match (auth_supplied, status) { + (true, _) => ConnectionState::AuthenticationRejected, + (false, 403) => ConnectionState::AuthenticationRejected, + _ => ConnectionState::AuthenticationRequired, } - // Each request uses its own short-lived connection (Connection: close), - // which is what the health/config endpoints expect and keeps the client - // simple and redirect-free. - let timeout = timeout; - let request = |path: &str| -> std::io::Result<(u16, String)> { - let mut stream = TcpStream::connect_timeout(&addrs[0], timeout)?; - let _ = stream.set_read_timeout(Some(timeout)); - let _ = stream.set_write_timeout(Some(timeout)); - get(&mut stream, &endpoint.host, path) - }; +} - // Health first: it is the API-compatibility signal. - let (status, body) = match request("/global/health") { - Ok(pair) => pair, - Err(e) => { - return Capability { - state: ConnectionState::Incompatible, - tcp_reachable: true, - healthy: false, - server_version: None, - provider_count: 0, - model_count: 0, - detail: format!("health request failed: {e}"), - } +/// Probe the server and classify the connection without claiming model +/// readiness. The connection is pinned to the policy-checked address. +pub fn probe(http: &H, endpoint: &Endpoint, request: &ProbeRequest) -> Capability { + let target = match endpoint.checked_target() { + Ok(target) => target, + Err(error) => { + return Capability::new(ConnectionState::InsecureTransport, error.to_string()) } }; - if status == 401 || status == 403 { - return Capability { - state: ConnectionState::AuthenticationRequired, - tcp_reachable: true, - healthy: false, - server_version: None, - provider_count: 0, - model_count: 0, - detail: format!("server requires authentication (HTTP {status})"), - }; + let resolve = Some((endpoint.host.clone(), endpoint.port, target.ip())); + + // The `/provider` catalog can be several MiB on a server with many + // configured providers; health is tiny. Both are bounded. + const HEALTH_MAX_BODY: u64 = 64 * 1024; + const CATALOG_MAX_BODY: u64 = 16 * 1024 * 1024; + let make = |path: &str, max_body: u64| -> HttpRequest { + let mut req = HttpRequest::get(format!("{}{path}", http_base(endpoint))); + req.timeout = request.timeout; + req.connect_timeout = request.timeout.min(Duration::from_secs(10)); + req.max_body = max_body; + req.resolve = resolve.clone(); + req.auth = request.auth.clone(); + req + }; + + // Health is the API-compatibility signal. + let health = match http.request(&make("/global/health", HEALTH_MAX_BODY)) { + Ok(response) => response, + Err(error) => { + return Capability::new(ConnectionState::Unreachable, error.to_string()); + } + }; + let mut capability = classify_health(health, request.auth.is_some()); + if capability.state != ConnectionState::Reachable { + // Health did not produce a usable server; report that state now. + if matches!( + capability.state, + ConnectionState::AuthenticationRequired | ConnectionState::AuthenticationRejected + ) { + capability.tcp_reachable = true; + } + return capability; } - if status != 200 { - return Capability { - state: ConnectionState::Incompatible, - tcp_reachable: true, - healthy: false, - server_version: None, - provider_count: 0, - model_count: 0, - detail: format!("/global/health returned HTTP {status}"), + capability.tcp_reachable = true; + + // Catalog: metadata only, credentials never retained. + let catalog_response = match http.request(&make("/provider", CATALOG_MAX_BODY)) { + Ok(response) => response, + Err(error) => { + capability.state = ConnectionState::Incompatible; + capability.detail = format!("provider catalog request failed: {error}"); + return capability; + } + }; + if !catalog_response.is_success() { + capability.state = match catalog_response.status { + 401 => auth_required_state(request.auth.is_some(), 401), + 403 => ConnectionState::AuthenticationRejected, + _ => ConnectionState::Incompatible, }; + capability.detail = auth_detail(capability.state, catalog_response.status); + return capability; } - let health: Health = match serde_json::from_str(&body) { - Ok(h) => h, + let catalog: Catalog = match parse_catalog(&catalog_response.body) { + Ok(catalog) => catalog, + Err(error) => { + capability.state = ConnectionState::Incompatible; + capability.detail = format!("provider catalog was not usable: {error}"); + return capability; + } + }; + apply_catalog(&mut capability, &catalog, request); + capability +} + +fn classify_health(response: HttpResponse, auth_supplied: bool) -> Capability { + if response.status == 401 || response.status == 403 { + let state = auth_required_state(auth_supplied, response.status); + return Capability::new(state, auth_detail(state, response.status)); + } + if !response.is_success() { + return Capability::new( + ConnectionState::Incompatible, + format!("/global/health returned HTTP {}", response.status), + ); + } + let health: HealthBody = match serde_json::from_str(&response.body) { + Ok(health) => health, Err(_) => { - return Capability { - state: ConnectionState::Incompatible, - tcp_reachable: true, - healthy: false, - server_version: None, - provider_count: 0, - model_count: 0, - detail: "/global/health was not the expected JSON".into(), - } + return Capability::new( + ConnectionState::Incompatible, + "/global/health was not the expected JSON", + ) } }; if !health.healthy { - return Capability { - state: ConnectionState::Incompatible, - tcp_reachable: true, - healthy: false, - server_version: health.version, - provider_count: 0, - model_count: 0, - detail: "server reported unhealthy".into(), - }; + return Capability::new(ConnectionState::Incompatible, "server reported unhealthy"); } + let mut capability = Capability::new(ConnectionState::Reachable, "healthy server"); + capability.healthy = true; + capability.sign_healthy(health.version); + capability +} - // Providers: authentication and model availability. - let (pstatus, pbody) = request("/config/providers").unwrap_or((0, String::new())); - if pstatus == 401 || pstatus == 403 { - return Capability { - state: ConnectionState::AuthenticationRequired, - tcp_reachable: true, - healthy: true, - server_version: health.version, - provider_count: 0, - model_count: 0, - detail: format!("provider list requires authentication (HTTP {pstatus})"), - }; - } - let providers: Option = serde_json::from_str(&pbody).ok(); - let (provider_count, model_count) = match providers { - Some(p) => ( - p.providers.iter().filter(|p| !p.models.is_empty()).count(), - p.providers.iter().map(|p| p.models.len()).sum(), - ), - None => (0, 0), - }; - let state = if model_count == 0 { - ConnectionState::ReachableNoModel - } else { - ConnectionState::Ready - }; - Capability { - state, - tcp_reachable: true, - healthy: true, - server_version: health.version, - provider_count, - model_count, - detail: if model_count == 0 { - "server is healthy but no model is configured".into() - } else { - format!("{provider_count} provider(s), {model_count} model(s)") - }, +impl Capability { + fn sign_healthy(&mut self, version: Option) { + self.server_version = version; + self.tcp_reachable = true; } } -/// Minimal HTTP/1.1 GET with connection close. Never follows redirects. -fn get(stream: &mut TcpStream, host: &str, path: &str) -> std::io::Result<(u16, String)> { - let request = format!( - "GET {path} HTTP/1.1\r\nHost: {host}\r\nAccept: application/json\r\nConnection: close\r\n\r\n" - ); - stream.write_all(request.as_bytes())?; - stream.flush()?; - let mut raw = Vec::new(); - stream.read_to_end(&mut raw)?; - let text = String::from_utf8_lossy(&raw).into_owned(); - let Some(header_end) = text.find("\r\n\r\n") else { - return Err(std::io::Error::new( - std::io::ErrorKind::InvalidData, - "no HTTP header terminator", - )); - }; - let headers = &text[..header_end]; - let body = &text[header_end + 4..]; - let status = headers - .lines() - .next() - .and_then(|line| line.split_whitespace().nth(1)) - .and_then(|code| code.parse::().ok()) - .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidData, "no status line"))?; - let chunked = headers - .to_ascii_lowercase() - .contains("transfer-encoding: chunked"); - let body = if chunked { - decode_chunked(body).unwrap_or_else(|| body.to_string()) - } else { - body.to_string() - }; - Ok((status, body)) +fn auth_detail(state: ConnectionState, status: u16) -> String { + match state { + ConnectionState::AuthenticationRequired => { + format!("server requires authentication (HTTP {status})") + } + ConnectionState::AuthenticationRejected => { + format!("server rejected the supplied authentication (HTTP {status})") + } + _ => format!("HTTP {status}"), + } } -/// Decode an HTTP chunked body. Best-effort; returns `None` on malformed input. -fn decode_chunked(body: &str) -> Option { - let mut out = String::new(); - let mut rest = body; - loop { - let line_end = rest.find("\r\n")?; - let size_text = rest[..line_end].split(';').next()?.trim(); - let size = usize::from_str_radix(size_text, 16).ok()?; - rest = &rest[line_end + 2..]; - if size == 0 { - return Some(out); +fn apply_catalog(capability: &mut Capability, catalog: &Catalog, request: &ProbeRequest) { + capability.provider_count = catalog + .providers + .iter() + .filter(|p| !p.models.is_empty()) + .count(); + capability.model_count = catalog.model_count(); + capability.connected_model_count = catalog.connected_model_count(); + + if let Some(provider) = &request.provider { + capability.selected_provider = Some(provider.clone()); + if let Some(model) = &request.model { + capability.selected_model = Some(model.clone()); + capability.selected_model_available = catalog.has_model(provider, model); } - if rest.len() < size { - return None; - } - out.push_str(&rest[..size]); - rest = &rest[size..]; - rest = rest.strip_prefix("\r\n").unwrap_or(rest); + } + + if capability.selected_provider.is_some() && !capability.selected_model_available { + capability.state = ConnectionState::SelectedModelUnavailable; + capability.detail = format!( + "provider/model {}:{} is not available in the catalog", + request.provider.as_deref().unwrap_or("?"), + request.model.as_deref().unwrap_or("?") + ); + } else if capability.model_count > 0 { + capability.state = ConnectionState::ModelCatalog; + capability.detail = format!( + "{} provider(s), {} model(s), {} connected", + capability.provider_count, capability.model_count, capability.connected_model_count + ); + } else { + capability.state = ConnectionState::Reachable; + capability.detail = "server is healthy but no model is configured".into(); } } #[cfg(test)] mod tests { use super::*; - use std::io::Read as _; - use std::net::TcpListener; - use std::thread; + use std::sync::Mutex; - fn spawn_server(routes: Vec<(&'static str, u16, &'static str)>) -> Endpoint { - use std::collections::HashMap; - let listener = TcpListener::bind("127.0.0.1:0").unwrap(); - let port = listener.local_addr().unwrap().port(); - let routes: HashMap<&'static str, (u16, &'static str)> = - routes.into_iter().map(|(p, s, b)| (p, (s, b))).collect(); - thread::spawn(move || { - for stream in listener.incoming() { - let Ok(mut stream) = stream else { continue }; - let mut buf = [0u8; 2048]; - let n = stream.read(&mut buf).unwrap_or(0); - if n == 0 { - // The reachability probe connects and closes without a - // request; ignore it. - continue; - } - let request = String::from_utf8_lossy(&buf[..n]); - let want = request - .lines() - .next() - .and_then(|l| l.split_whitespace().nth(1)) - .unwrap_or(""); - let (code, payload) = routes.get(want).copied().unwrap_or((404, "{}")); - let response = format!( - "HTTP/1.1 {code} X\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{payload}", - payload.len() - ); - let _ = stream.write_all(response.as_bytes()); + struct FakeHttp { + responses: Mutex>, + requests: Mutex>, + } + + impl FakeHttp { + fn new(responses: Vec<(u16, &str)>) -> Self { + Self { + responses: Mutex::new( + responses + .into_iter() + .map(|(status, body)| HttpResponse { + status, + body: body.to_string(), + }) + .collect(), + ), + requests: Mutex::new(Vec::new()), } - }); - Endpoint { - host: "127.0.0.1".into(), - port, } } + impl HttpClient for FakeHttp { + fn request(&self, request: &HttpRequest) -> Result { + self.requests.lock().unwrap().push(request.clone()); + let mut responses = self.responses.lock().unwrap(); + if responses.is_empty() { + return Err(AiError::Provider("connection refused".into())); + } + Ok(responses.remove(0)) + } + } + + fn health() -> &'static str { + r#"{"healthy":true,"version":"1.18.32"}"# + } + + fn catalog() -> &'static str { + r#"{"all":[ + {"id":"deepseek","name":"DeepSeek","key":"sk-SECRET","models":{"deepseek-chat":{}}}, + {"id":"empty","name":"Empty","models":{}}], + "connected":["deepseek"], + "default":{"deepseek":"deepseek-chat"}}"# + } + + fn endpoint() -> Endpoint { + Endpoint::parse("http://127.0.0.1:4096").unwrap() + } + #[test] - fn parses_and_rejects_endpoints() { + fn parses_endpoints_and_rejects_unsafe_forms() { assert_eq!( Endpoint::parse("http://127.0.0.1:4096").unwrap(), Endpoint { + scheme: Scheme::Http, host: "127.0.0.1".into(), port: 4096 } ); + let v6 = Endpoint::parse("https://[::1]:4096").unwrap(); + assert_eq!(v6.host, "::1"); + assert_eq!(v6.scheme, Scheme::Https); assert_eq!( - Endpoint::parse("https://127.0.0.1:4096"), - Err(EndpointError::NotHttp) + Endpoint::parse("ftp://host:1"), + Err(EndpointError::UnsupportedScheme) ); assert_eq!( - Endpoint::parse("http://user@host:1"), - Err(EndpointError::BadHost) + Endpoint::parse("http://user:pw@host:1"), + Err(EndpointError::UserInfo) + ); + assert_eq!( + Endpoint::parse("http://host:1/v1/chat"), + Err(EndpointError::PathNotAllowed) ); assert_eq!(Endpoint::parse("http://host"), Err(EndpointError::BadPort)); - assert!(Endpoint::parse("http://host:1/../../etc").is_err()); - assert!(Endpoint::parse("").is_err()); - } - - #[test] - fn probe_reports_ready_with_models() { - let endpoint = spawn_server(vec![ - ( - "/global/health", - 200, - r#"{"healthy":true,"version":"1.18.32"}"#, - ), - ( - "/config/providers", - 200, - r#"{"providers":[{"id":"deepseek","models":{"m1":{},"m2":{}}},{"id":"empty","models":{}}]}"#, - ), - ]); - let capability = probe(&endpoint, Duration::from_secs(2)); - assert_eq!(capability.state, ConnectionState::Ready); - assert_eq!(capability.server_version.as_deref(), Some("1.18.32")); - assert_eq!(capability.provider_count, 1); - assert_eq!(capability.model_count, 2); - } - - #[test] - fn probe_reports_reachable_but_model_less() { - let endpoint = spawn_server(vec![ - ( - "/global/health", - 200, - r#"{"healthy":true,"version":"1.18.32"}"#, - ), - ("/config/providers", 200, r#"{"providers":[]}"#), - ]); - let capability = probe(&endpoint, Duration::from_secs(2)); - assert_eq!(capability.state, ConnectionState::ReachableNoModel); - } - - #[test] - fn probe_reports_authentication_required() { - let endpoint = spawn_server(vec![("/global/health", 401, r#"{"error":"unauthorized"}"#)]); - let capability = probe(&endpoint, Duration::from_secs(2)); - assert_eq!(capability.state, ConnectionState::AuthenticationRequired); - assert!(capability.tcp_reachable); - } - - #[test] - fn probe_reports_incompatible_api() { - let endpoint = spawn_server(vec![("/global/health", 404, "{}")]); - let capability = probe(&endpoint, Duration::from_secs(2)); - assert_eq!(capability.state, ConnectionState::Incompatible); - } - - #[test] - fn probe_reports_disconnected_for_a_closed_port() { - let endpoint = Endpoint { - host: "127.0.0.1".into(), - port: 1, - }; - let capability = probe(&endpoint, Duration::from_millis(300)); - assert_eq!(capability.state, ConnectionState::Disconnected); - assert!(!capability.tcp_reachable); - } - - /// Live check against a real `opencode serve`, enabled explicitly: - /// `PHOEBUS_OPENCODE_PROBE=http://127.0.0.1:4096 cargo test probe_live`. - #[test] - fn probe_live_server_when_configured() { - let Ok(url) = std::env::var("PHOEBUS_OPENCODE_PROBE") else { - return; - }; - let endpoint = Endpoint::parse(&url).expect("valid probe endpoint"); - let capability = probe(&endpoint, Duration::from_secs(3)); - eprintln!("live opencode capability: {capability:?}"); - assert!( - capability.tcp_reachable, - "server was not reachable: {capability:?}" + assert_eq!( + Endpoint::parse("http://host:0"), + Err(EndpointError::BadPort) ); + assert_eq!( + Endpoint::parse("http://host:99999"), + Err(EndpointError::BadPort) + ); + assert_eq!( + Endpoint::parse("http://ho st:1"), + Err(EndpointError::BadHost) + ); + assert_eq!( + Endpoint::parse("http://host:1\nX: y"), + Err(EndpointError::BadHost) + ); + assert!(EndpointError::Empty.to_string().contains("empty")); + } + + #[test] + fn transport_policy_enforces_loopback_for_plain_http() { + // An IP literal avoids DNS and works offline. + let public = Endpoint::parse("http://93.184.216.34:80").unwrap(); + let err = public.checked_target().unwrap_err().to_string(); + assert!(err.contains("refusing plain http"), "{err}"); + // https is allowed (certificate verification happens in the transport). + let secure = Endpoint::parse("https://93.184.216.34:443").unwrap(); + assert!(secure.checked_target().is_ok()); + // loopback http is allowed and pinned. + let target = endpoint().checked_target().unwrap(); + assert!(target.ip().is_loopback()); + } + + #[test] + fn probe_reports_a_model_catalog_without_leaking_keys() { + let http = FakeHttp::new(vec![(200, health()), (200, catalog())]); + let capability = probe(&http, &endpoint(), &ProbeRequest::new()); + assert_eq!(capability.state, ConnectionState::ModelCatalog); + assert_eq!(capability.model_count, 1); + assert_eq!(capability.connected_model_count, 1); + let debug = format!("{capability:?}"); + assert!( + !debug.contains("SECRET"), + "capability leaked a key: {debug}" + ); + // The catalog request carried no credentials. + assert!(http.requests.lock().unwrap()[0].auth.is_none()); + } + + #[test] + fn probe_reports_selected_model_unavailable() { + let http = FakeHttp::new(vec![(200, health()), (200, catalog())]); + let mut request = ProbeRequest::new(); + request.provider = Some("deepseek".into()); + request.model = Some("does-not-exist".into()); + let capability = probe(&http, &endpoint(), &request); + assert_eq!(capability.state, ConnectionState::SelectedModelUnavailable); + assert!(!capability.selected_model_available); + + // The available model is accepted. + let http = FakeHttp::new(vec![(200, health()), (200, catalog())]); + let mut request = ProbeRequest::new(); + request.provider = Some("deepseek".into()); + request.model = Some("deepseek-chat".into()); + let capability = probe(&http, &endpoint(), &request); + assert_eq!(capability.state, ConnectionState::ModelCatalog); + assert!(capability.selected_model_available); + } + + #[test] + fn probe_distinguishes_authentication_states() { + // No credential supplied → required. + let http = FakeHttp::new(vec![(401, r#"{"error":"unauthorized"}"#)]); + let capability = probe(&http, &endpoint(), &ProbeRequest::new()); + assert_eq!(capability.state, ConnectionState::AuthenticationRequired); + + // A credential was supplied and rejected. + let http = FakeHttp::new(vec![(401, "{}")]); + let mut request = ProbeRequest::new(); + request.auth = Some(SecretString::new("wrong")); + let capability = probe(&http, &endpoint(), &request); + assert_eq!(capability.state, ConnectionState::AuthenticationRejected); + + // Authentication accepted, then health+catalog. + let http = FakeHttp::new(vec![(200, health()), (200, catalog())]); + let request = ProbeRequest { + auth: Some(SecretString::new("right")), + ..ProbeRequest::new() + }; + let capability = probe(&http, &endpoint(), &request); + assert_eq!(capability.state, ConnectionState::ModelCatalog); + // The credential is presented to the server but never returned. + assert!(http.requests.lock().unwrap()[0].auth.is_some()); + } + + #[test] + fn probe_reports_incompatible_and_unreachable() { + let http = FakeHttp::new(vec![(404, "{}")]); + assert_eq!( + probe(&http, &endpoint(), &ProbeRequest::new()).state, + ConnectionState::Incompatible + ); + let http = FakeHttp::new(vec![(200, "hi")]); + assert_eq!( + probe(&http, &endpoint(), &ProbeRequest::new()).state, + ConnectionState::Incompatible + ); + let http = FakeHttp::new(vec![]); + assert_eq!( + probe(&http, &endpoint(), &ProbeRequest::new()).state, + ConnectionState::Unreachable + ); + } + + #[test] + fn probe_rejects_an_insecure_destination_before_any_request() { + let http = FakeHttp::new(vec![(200, health()), (200, catalog())]); + let public = Endpoint::parse("http://93.184.216.34:80").unwrap(); + let capability = probe(&http, &public, &ProbeRequest::new()); + assert_eq!(capability.state, ConnectionState::InsecureTransport); + assert!( + http.requests.lock().unwrap().is_empty(), + "no request may be sent" + ); + } + + #[test] + fn a_misleading_success_shaped_500_does_not_produce_a_catalog() { + // Health OK, but the catalog endpoint returns a providers-shaped body + // with a 500 status: it must not be parsed. + let shaped = r#"{"all":[{"id":"deepseek","models":{"m":{}}}],"connected":[]}"#; + let http = FakeHttp::new(vec![(200, health()), (500, shaped)]); + let capability = probe(&http, &endpoint(), &ProbeRequest::new()); + assert_eq!(capability.state, ConnectionState::Incompatible); + assert_eq!(capability.model_count, 0); + } + + #[test] + fn an_empty_catalog_is_reachable_not_ready() { + let http = FakeHttp::new(vec![(200, health()), (200, r#"{"all":[],"connected":[]}"#)]); + let capability = probe(&http, &endpoint(), &ProbeRequest::new()); + assert_eq!(capability.state, ConnectionState::Reachable); + assert_ne!(capability.state, ConnectionState::Ready); } #[test] fn capability_serialisation_has_no_credential_fields() { - let capability = Capability { - state: ConnectionState::Ready, - tcp_reachable: true, - healthy: true, - server_version: Some("1.18.32".into()), - provider_count: 1, - model_count: 3, - detail: "1 provider(s), 3 model(s)".into(), - }; + let capability = Capability::new(ConnectionState::Ready, "1 provider(s)"); let json = serde_json::to_string(&capability).unwrap(); assert!(!json.to_lowercase().contains("key")); - assert!(!json.contains("sk-")); } }