From e4f4e1e9a95d37f07537b1badaaddca4cb8c5b4a Mon Sep 17 00:00:00 2001 From: Pavlo Ratushnyi Date: Sun, 18 Oct 2026 02:35:27 +0200 Subject: [PATCH] fix(control-plane): chat completions keeps Retry-After on a memory_pressure 503; gate test covers chat and messages Co-Authored-By: Claude Sonnet 5.5 --- control-plane/src/chat.rs | 8 +++++++- control-plane/tests/memory_gate_http.rs | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/control-plane/src/chat.rs b/control-plane/src/chat.rs index 260cb409..eb57c190 100644 --- a/control-plane/src/chat.rs +++ b/control-plane/src/chat.rs @@ -490,6 +490,8 @@ pub async fn create( return stream_response(response.into_body(), include_usage); } let status = response.status(); + // a refusal that tells the client when to come back (503 memory_pressure) keeps its Retry-After through the re-wrapping below + let retry_after = response.headers().get(axum::http::header::RETRY_AFTER).cloned(); let bytes = match to_bytes(response.into_body(), MAX_EVENT).await { Ok(b) => b, Err(_) => { @@ -522,7 +524,11 @@ pub async fn create( }; value["error"]["param"] = json!(mapped); } - return (status, Json(value)).into_response(); + let mut refusal = (status, Json(value)).into_response(); + if let Some(retry_after) = retry_after { + refusal.headers_mut().insert(axum::http::header::RETRY_AFTER, retry_after); + } + return refusal; } Json(completion(&value)).into_response() } diff --git a/control-plane/tests/memory_gate_http.rs b/control-plane/tests/memory_gate_http.rs index 4e22b38f..fea40607 100644 --- a/control-plane/tests/memory_gate_http.rs +++ b/control-plane/tests/memory_gate_http.rs @@ -147,6 +147,19 @@ async fn a_reject_report_closes_the_gate_with_503_and_retry_after_and_shed_still let body: Value = serde_json::from_str(&refused.text().await.unwrap()).unwrap(); assert_eq!(body["error"]["code"], "memory_pressure", "{body}"); assert!(body["error"]["message"].as_str().unwrap().contains("hard floor")); + // the Chat Completions and Messages dialects get the same 503 with the same Retry-After + for (path, body) in [ + ("/v1/chat/completions", json!({"model": "fixture", "messages": [{"role": "user", "content": "hi"}]})), + ("/v1/chat/completions", json!({"model": "fixture", "stream": true, "messages": [{"role": "user", "content": "hi"}]})), + ("/v1/messages", json!({"model": "fixture", "max_tokens": 16, "messages": [{"role": "user", "content": "hi"}]})), + ] { + let r = stack.client.post(format!("{}{path}", stack.base)).header("content-type", "application/json").header("anthropic-version", "2023-06-01").body(body.to_string()).send().await.unwrap(); + let status = r.status(); + let retry = r.headers().get("retry-after").map(|v| v.to_str().unwrap().to_owned()); + let text = r.text().await.unwrap_or_default(); + assert_eq!(status, 503, "{path}: {text}"); + assert_eq!(retry.as_deref(), Some("7"), "{path} keeps Retry-After: {text}"); + } // streaming requests are refused up front too: a proper 503, not a 200 SSE that fails inside let refused = stack.post(&request(true)).await; assert_eq!(refused.status(), 503); @@ -161,9 +174,9 @@ async fn a_reject_report_closes_the_gate_with_503_and_retry_after_and_shed_still let status: Value = serde_json::from_str(&stack.client.get(format!("{}/v1/status", stack.base)).send().await.unwrap().text().await.unwrap()).unwrap(); assert_eq!(status["memory"]["gate"]["level"], "normal"); - assert_eq!(status["memory"]["gate_rejections"], 2); + assert_eq!(status["memory"]["gate_rejections"], 5); let metrics: Value = serde_json::from_str(&stack.client.get(format!("{}/metrics", stack.base)).send().await.unwrap().text().await.unwrap()).unwrap(); - assert_eq!(metrics["memory"]["gate_rejections"], 2); + assert_eq!(metrics["memory"]["gate_rejections"], 5); stack.finish().await; }