Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(rust): 7 proxy bugs found in review (phase-1)
chopratejas56f679f2479c
diff --git a/crates/headroom-proxy/Cargo.toml b/crates/headroom-proxy/Cargo.tomlindex 87e7af660..8b8ba7c5e 100644--- a/crates/headroom-proxy/Cargo.toml+++ b/crates/headroom-proxy/Cargo.toml@@ -39,6 +39,7 @@ hyper = "1"url = "2"humantime = "2"bytesize = "1"+tokio-util = { version = "0.7" }headroom-core = { path = "../headroom-core" }[dev-dependencies]diff --git a/crates/headroom-proxy/src/health.rs b/crates/headroom-proxy/src/health.rsindex 8c0524bcf..683b917a6 100644--- a/crates/headroom-proxy/src/health.rs+++ b/crates/headroom-proxy/src/health.rs@@ -17,16 +17,12 @@ pub async fn healthz() -> impl IntoResponse {/// 2xx, 503 otherwise. The endpoint name is reserved by the proxy and is/// not forwarded; operators must not name a real upstream route this.pub async fn healthz_upstream(State(state): State<AppState>) -> Response {- let url = match state.config.upstream.join("healthz") {- Ok(u) => u,- Err(e) => {- return (- StatusCode::SERVICE_UNAVAILABLE,- Json(json!({"ok": false, "error": format!("bad upstream url: {e}")})),- )- .into_response();- }- };+ // Use an absolute path so upstream URLs with non-trailing-slash paths+ // (e.g. http://localhost:8788/api) resolve to /healthz, not replace the+ // last segment. Url::join("healthz") would strip "api" per RFC 3986.+ let mut url = state.config.upstream.clone();+ url.set_path("/healthz");+ url.set_query(None);match state.client.get(url).send().await {Ok(resp) if resp.status().is_success() => {(StatusCode::OK, Json(json!({"ok": true}))).into_response()diff --git a/crates/headroom-proxy/src/main.rs b/crates/headroom-proxy/src/main.rsindex dc5c2d47a..b9d8b57b1 100644--- a/crates/headroom-proxy/src/main.rs+++ b/crates/headroom-proxy/src/main.rs@@ -37,8 +37,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {let listener = tokio::net::TcpListener::bind(config.listen).await?;tracing::info!(addr = %listener.local_addr()?, "listening");+ let grace = config.graceful_shutdown_timeout;axum::serve(listener, app)- .with_graceful_shutdown(shutdown_signal())+ .with_graceful_shutdown(async move {+ shutdown_signal().await;+ tracing::info!(+ timeout_s = grace.as_secs(),+ "draining in-flight requests before exit"+ );+ tokio::time::sleep(grace).await;+ }).await?;Ok(())diff --git a/crates/headroom-proxy/src/proxy.rs b/crates/headroom-proxy/src/proxy.rsindex 42ff2c560..16d937db0 100644--- a/crates/headroom-proxy/src/proxy.rs+++ b/crates/headroom-proxy/src/proxy.rs@@ -10,8 +10,9 @@ use axum::http::{HeaderMap, HeaderName, Request, Response, StatusCode, Uri};use axum::response::IntoResponse;use axum::routing::{any, get};use axum::Router;+use futures_util::{StreamExt as _, TryStreamExt};+#[cfg(test)]use bytes::Bytes;-use futures_util::TryStreamExt;#[cfg(test)]use http_body_util::BodyExt;@@ -100,30 +101,30 @@ fn is_websocket_upgrade(headers: &HeaderMap) -> bool {/// Build the upstream URL by joining the configured base with the incoming/// path-and-query. Preserves '?' and the query string verbatim.pub(crate) fn build_upstream_url(base: &url::Url, uri: &Uri) -> Result<url::Url, ProxyError> {- let path = uri.path();- let query = uri.query();+ Ok(join_upstream_path(base, uri.path(), uri.query()))+}- // We treat the configured upstream as a base. Strip its trailing slash- // when joining a path that starts with '/' so that "http://x:1/api" + "/v1/foo"- // yields "http://x:1/api/v1/foo" rather than "http://x:1/v1/foo".+/// Shared path-join helper used by HTTP and WebSocket handlers.+/// Appends `path` to `base`, preserving any base path prefix, then sets `query`.+pub(crate) fn join_upstream_path(base: &url::Url, path: &str, query: Option<&str>) -> url::Url {let mut joined = base.clone();- {- let base_path = joined.path().trim_end_matches('/').to_string();- let combined = if path.is_empty() || path == "/" {- if base_path.is_empty() {- "/".to_string()- } else {- base_path- }- } else if base_path.is_empty() {- path.to_string()+ // Strip trailing slash from base path so "http://x:1/api" + "/v1/foo"+ // yields "http://x:1/api/v1/foo" rather than "http://x:1/v1/foo".+ let base_path = joined.path().trim_end_matches('/').to_string();+ let combined = if path.is_empty() || path == "/" {+ if base_path.is_empty() {+ "/".to_string()} else {- format!("{base_path}{path}")- };- joined.set_path(&combined);- }+ base_path+ }+ } else if base_path.is_empty() {+ path.to_string()+ } else {+ format!("{base_path}{path}")+ };+ joined.set_path(&combined);joined.set_query(query);- Ok(joined)+ joined}/// Forward an HTTP request to the upstream and stream the response back.@@ -184,9 +185,17 @@ async fn forward_http(let status = StatusCode::from_u16(upstream_status.as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);let resp_headers = filter_response_headers(upstream_resp.headers());- // Stream response body back without buffering.- let resp_stream = upstream_resp.bytes_stream();- let body = Body::from_stream(resp_stream.map_ok(Bytes::from));+ // Stream response body back without buffering. Wrap errors so mid-stream+ // upstream failures are logged rather than silently truncating the client.+ let rid = request_id.clone();+ let resp_stream = upstream_resp.bytes_stream().map(move |r| match r {+ Ok(b) => Ok(b),+ Err(e) => {+ tracing::warn!(request_id = %rid, error = %e, "upstream stream error mid-response");+ Err(e)+ }+ });+ let body = Body::from_stream(resp_stream);let mut response = Response::builder().status(status);{diff --git a/crates/headroom-proxy/src/websocket.rs b/crates/headroom-proxy/src/websocket.rsindex 668f1eb65..b976e5675 100644--- a/crates/headroom-proxy/src/websocket.rs+++ b/crates/headroom-proxy/src/websocket.rs@@ -15,7 +15,7 @@ use tokio_tungstenite::tungstenite::protocol::CloseFrame as TgCloseFrame;use tokio_tungstenite::tungstenite::Message as TgMsg;use crate::headers::build_forward_request_headers;-use crate::proxy::AppState;+use crate::proxy::{join_upstream_path, AppState};/// Entry point invoked from the catch-all when an upgrade is detected.pub async fn ws_handler(@@ -63,6 +63,7 @@ pub async fn ws_handler(.and_then(|v| v.to_str().ok()).map(|s| s.to_string());+ let path = req.uri().path().to_string();ws.on_upgrade(move |client_ws| async move {if let Err(e) = run_ws_pump(client_ws,@@ -70,6 +71,7 @@ pub async fn ws_handler(forward_headers,subprotocols,request_id,+ path,).await{@@ -93,31 +95,15 @@ impl IntoResponseBody for (StatusCode, String) {fn build_upstream_ws_url(base: &url::Url, req_uri: &http::Uri) -> Result<url::Url, String> {let mut joined = base.clone();let new_scheme = match joined.scheme() {- "http" => "ws".to_string(),- "https" => "wss".to_string(),- s @ ("ws" | "wss") => s.to_string(),+ "http" => "ws",+ "https" => "wss",+ "ws" | "wss" => "ws", // already WS; set_scheme is a no-op but keeps it uniformother => return Err(format!("unsupported upstream scheme: {other}")),};joined- .set_scheme(&new_scheme)+ .set_scheme(new_scheme).map_err(|()| "failed to set ws scheme".to_string())?;- let path = req_uri.path();- let query = req_uri.query();- let base_path = joined.path().trim_end_matches('/').to_string();- let combined = if path.is_empty() || path == "/" {- if base_path.is_empty() {- "/".to_string()- } else {- base_path- }- } else if base_path.is_empty() {- path.to_string()- } else {- format!("{base_path}{path}")- };- joined.set_path(&combined);- joined.set_query(query);- Ok(joined)+ Ok(join_upstream_path(&joined, req_uri.path(), req_uri.query()))}async fn run_ws_pump(@@ -126,6 +112,7 @@ async fn run_ws_pump(forward_headers: http::HeaderMap,subprotocols: Option<String>,request_id: String,+ path: String,) -> Result<(), String> {// Build the upstream handshake request manually so we can inject headers.let mut req = upstream_url@@ -167,58 +154,61 @@ async fn run_ws_pump(let (mut upstream_sink, mut upstream_stream) = upstream_ws.split();let (mut client_sink, mut client_stream) = client_ws.split();+ tracing::info!(+ request_id = %request_id,+ path = %path,+ protocol = "ws",+ upstream = %upstream_url,+ "ws session opened"+ );++ // Each direction runs in its own task. We use a cancel token so that when+ // either side closes/errors, the other is aborted immediately rather than+ // blocking forever on next().await (the half-close hang bug).+ let cancel = tokio_util::sync::CancellationToken::new();+ let cancel_c2u = cancel.clone();+ let cancel_u2c = cancel.clone();+// Pump client -> upstream.- let c2u = async {- while let Some(msg) = client_stream.next().await {- let m = match msg {- Ok(m) => m,- Err(_) => break,- };- let tg = match ax_to_tg(m) {- Some(tg) => tg,- None => continue,- };- let close = matches!(tg, TgMsg::Close(_));- if upstream_sink.send(tg).await.is_err() {- break;- }- if close {- break;+ let c2u = tokio::spawn(async move {+ loop {+ tokio::select! {+ _ = cancel_c2u.cancelled() => break,+ msg = client_stream.next() => {+ let Some(msg) = msg else { break };+ let m = match msg { Ok(m) => m, Err(_) => break };+ let tg = match ax_to_tg(m) { Some(tg) => tg, None => continue };+ let close = matches!(tg, TgMsg::Close(_));+ if upstream_sink.send(tg).await.is_err() { break; }+ if close { break; }+ }}}let _ = upstream_sink.close().await;- };+ cancel_c2u.cancel();+ });// Pump upstream -> client.- let u2c = async {- while let Some(msg) = upstream_stream.next().await {- let m = match msg {- Ok(m) => m,- Err(_) => break,- };- let ax = match tg_to_ax(m) {- Some(ax) => ax,- None => continue,- };- let close = matches!(ax, AxMsg::Close(_));- if client_sink.send(ax).await.is_err() {- break;- }- if close {- break;+ let u2c = tokio::spawn(async move {+ loop {+ tokio::select! {+ _ = cancel_u2c.cancelled() => break,+ msg = upstream_stream.next() => {+ let Some(msg) = msg else { break };+ let m = match msg { Ok(m) => m, Err(_) => break };+ let ax = match tg_to_ax(m) { Some(ax) => ax, None => continue };+ let close = matches!(ax, AxMsg::Close(_));+ if client_sink.send(ax).await.is_err() { break; }+ if close { break; }+ }}}let _ = client_sink.close().await;- };+ cancel_u2c.cancel();+ });- tracing::info!(- request_id = %request_id,- protocol = "ws",- upstream = %upstream_url,- "ws session opened"- );- tokio::join!(c2u, u2c);- tracing::info!(request_id = %request_id, protocol = "ws", "ws session closed");+ let _ = tokio::join!(c2u, u2c);+ tracing::info!(request_id = %request_id, path = %path, protocol = "ws", "ws session closed");Ok(())}