Skip to content

Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.

Rust

fix(libsy): redact upstream error content from judge warning logs (#497)

GDRSbd00f88ec418
diff --git a/crates/libsy/src/algorithms/util.rs b/crates/libsy/src/algorithms/util.rs
index d2e85c4c2..86a548049 100644
--- a/crates/libsy/src/algorithms/util.rs
+++ b/crates/libsy/src/algorithms/util.rs
@@ -6,6 +6,7 @@ pub(crate) mod classifier_contract;
pub mod escalation;
pub(crate) mod llm_judge;
pub(crate) mod prompts;
+pub(crate) mod robustness;
pub(crate) mod stage;
pub mod subagent;
pub(crate) mod target_selector;
diff --git a/crates/libsy/src/algorithms/util/llm_judge.rs b/crates/libsy/src/algorithms/util/llm_judge.rs
index fb2dca589..0210bda27 100644
--- a/crates/libsy/src/algorithms/util/llm_judge.rs
+++ b/crates/libsy/src/algorithms/util/llm_judge.rs
@@ -17,6 +17,8 @@ use switchyard_protocol::{
completion_text,
};
+use super::robustness::{safe_client_error, safe_error_summary};
+
use super::classifier_contract::ClassifierContract;
use crate::core::algorithm::Driver;
use crate::core::classifier::{Classification, Classifier};
@@ -243,23 +245,40 @@ where
vec![self.target.clone()],
)
.await
- .inspect_err(|error| report_fail_open(judge_model, error, libsy_error_reason(error)))
+ .inspect_err(|error| {
+ report_fail_open(
+ judge_model,
+ safe_error_summary(error),
+ libsy_error_reason(error),
+ )
+ })
.ok()?;
let aggregate = response
.llm_response
.into_agg()
.await
- .inspect_err(|error| report_fail_open(judge_model, error, client_error_reason(error)))
+ .inspect_err(|error| {
+ report_fail_open(
+ judge_model,
+ safe_client_error(error),
+ client_error_reason(error),
+ )
+ })
.ok()?;
self.judge
.parse(&aggregate)
- .inspect_err(|error| report_fail_open(judge_model, error, "parse_error"))
+ .inspect_err(|error| {
+ report_fail_open(judge_model, safe_error_summary(error), "parse_error")
+ })
.ok()
}
}
/// Logs and counts a judge failure with a bounded label that excludes message content.
-fn report_fail_open(judge_model: &str, error: &dyn std::fmt::Display, reason: &'static str) {
+/// `error` must already be redacted: `LlmClientError::UpstreamHttp`'s `Display` interpolates the
+/// raw upstream body, which can quote the conversation back. Callers pass a
+/// `robustness::safe_*` summary rather than the error itself.
+fn report_fail_open(judge_model: &str, error: String, reason: &'static str) {
tracing::warn!(
target: "libsy",
judge_model,
diff --git a/crates/libsy/src/algorithms/util/robustness.rs b/crates/libsy/src/algorithms/util/robustness.rs
new file mode 100644
index 000000000..ed51518a2
--- /dev/null
+++ b/crates/libsy/src/algorithms/util/robustness.rs
@@ -0,0 +1,177 @@
+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+// SPDX-License-Identifier: Apache-2.0
+
+//! Hardening helpers shared by classifier-style inner-LLM callers.
+//!
+//! Consulting a model to make a routing decision fails in ways the routed call does
+//! not, and those failures are logged on a path that runs for every turn. The
+//! natural `Display` of an error is not safe there: an upstream error body is echoed
+//! verbatim, and both bodies and decode failures can embed the conversation or the
+//! model's reply. [`safe_error_summary`] is the redaction those log sites use.
+
+use switchyard_protocol::LlmClientError;
+
+use crate::LibsyError;
+
+/// A loggable description of `error` that carries no conversation or response content.
+///
+/// Each case contributes only vetted operational detail: the failure's class, the
+/// target or model it concerns, and a status code where one exists. Anything sourced
+/// from outside the process is reduced to its class.
+///
+/// The match is deliberately exhaustive rather than defaulting to `to_string()`, so a
+/// new [`LibsyError`] variant cannot start leaking content by omission.
+pub(crate) fn safe_error_summary(error: &LibsyError) -> String {
+ match error {
+ LibsyError::ClientCall { target, source } => {
+ format!(
+ "client call to target {target:?} failed: {}",
+ safe_client_error(source)
+ )
+ }
+ // `message` is a `String` with no constraint on its contents. Callers build it
+ // from static text today, but nothing stops one embedding a response or an
+ // upstream error, so only the class is reported.
+ LibsyError::AlgorithmError { .. } => "algorithm error".to_string(),
+ LibsyError::Driver(_) => "algorithm driver failed".to_string(),
+ // The operation is a static label, but the boxed source comes from a user
+ // extension and is unvetted.
+ LibsyError::External { operation, .. } => format!("{operation} failed"),
+ // The remaining variants render only structural detail: configured target
+ // names and fixed strings, nothing request-derived.
+ LibsyError::TargetNotFound { .. }
+ | LibsyError::NoTargets
+ | LibsyError::MissingFinalResponse => error.to_string(),
+ }
+}
+
+/// The client half of [`safe_error_summary`].
+///
+/// `UpstreamHttp` is the sharp edge: its `Display` interpolates the raw upstream
+/// body, which routinely quotes the request back. Boxed transport, decode, and FFI
+/// sources are reduced for the same reason.
+pub(crate) fn safe_client_error(error: &LlmClientError) -> String {
+ match error {
+ LlmClientError::UpstreamHttp { status, .. } => format!("upstream HTTP {status}"),
+ LlmClientError::ContextWindowExceeded { model, .. } => {
+ format!("context window exceeded for model {model}")
+ }
+ LlmClientError::Timeout { .. } => "upstream request timed out".to_string(),
+ LlmClientError::Transport { .. } => "upstream transport error".to_string(),
+ LlmClientError::InvalidResponse { .. } => "invalid upstream response".to_string(),
+ LlmClientError::Ffi { .. } => "foreign function interface error".to_string(),
+ LlmClientError::InvalidRequest { .. } => "invalid request".to_string(),
+ LlmClientError::RequestTranslation(_) => "request translation failed".to_string(),
+ LlmClientError::RequestEncoding(_) => "outbound request encoding failed".to_string(),
+ LlmClientError::ResponseTranslation(_) => "response translation failed".to_string(),
+ // `message` is unconstrained here too, so the class is reported without it.
+ LlmClientError::Configuration { .. } => "client configuration error".to_string(),
+ // Free-form by definition.
+ LlmClientError::General(_) => "client call failed".to_string(),
+ // `LlmClientError` is `#[non_exhaustive]`, so a wildcard is required. A future
+ // variant is unvetted until it is given an arm above.
+ _ => "client call failed".to_string(),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use switchyard_protocol::ModelId;
+
+ use super::*;
+
+ /// The conversation text a leaking log line would expose.
+ const SECRET: &str = "patient name is Jane Doe";
+
+ #[test]
+ fn an_upstream_body_never_reaches_the_summary() {
+ let error = LibsyError::ClientCall {
+ target: ModelId::new("weak"),
+ source: LlmClientError::UpstreamHttp {
+ status: http::StatusCode::BAD_REQUEST,
+ body: format!(r#"{{"error":{{"message":"bad request: {SECRET}"}}}}"#),
+ },
+ };
+
+ let summary = safe_error_summary(&error);
+
+ // The status and target survive; the body does not.
+ assert!(summary.contains("upstream HTTP 400"), "{summary}");
+ assert!(summary.contains("weak"), "{summary}");
+ assert!(!summary.contains(SECRET), "{summary}");
+ // Guard against the redaction silently regressing to `Display`.
+ assert!(error.to_string().contains(SECRET));
+ }
+
+ #[test]
+ fn boxed_sources_are_reduced_to_their_class() {
+ for source in [
+ LlmClientError::Transport {
+ source: std::io::Error::other(SECRET).into(),
+ },
+ LlmClientError::InvalidResponse {
+ source: std::io::Error::other(SECRET).into(),
+ },
+ LlmClientError::Timeout {
+ source: std::io::Error::other(SECRET).into(),
+ },
+ ] {
+ let summary = safe_client_error(&source);
+ assert!(!summary.contains(SECRET), "{summary}");
+ assert!(!summary.is_empty());
+ }
+ }
+
+ #[test]
+ fn context_overflow_keeps_the_model_but_not_the_message() {
+ let summary = safe_client_error(&LlmClientError::ContextWindowExceeded {
+ model: ModelId::new("weak"),
+ message: SECRET.to_string(),
+ });
+ assert!(summary.contains("weak"), "{summary}");
+ assert!(!summary.contains(SECRET), "{summary}");
+ }
+
+ #[test]
+ fn an_algorithm_error_message_is_reduced_to_its_class() {
+ // `AlgorithmError::message` is an unconstrained `String`. Callers build it from
+ // static text today, but the summary must not depend on that holding.
+ let summary = safe_error_summary(&LibsyError::AlgorithmError {
+ message: format!("judge reply did not parse: {SECRET}"),
+ });
+ assert_eq!(summary, "algorithm error");
+ assert!(!summary.contains(SECRET), "{summary}");
+ }
+
+ #[test]
+ fn a_client_configuration_message_is_reduced_to_its_class() {
+ let summary = safe_client_error(&LlmClientError::Configuration {
+ message: SECRET.to_string(),
+ });
+ assert_eq!(summary, "client configuration error");
+ }
+
+ #[test]
+ fn a_general_error_is_reduced_to_its_class() {
+ let summary = safe_client_error(&LlmClientError::General(SECRET.to_string()));
+ assert!(!summary.contains(SECRET), "{summary}");
+ }
+
+ #[test]
+ fn an_extension_failure_keeps_its_label_but_not_its_source() {
+ let error = LibsyError::external("loading extension", std::io::Error::other(SECRET));
+
+ let summary = safe_error_summary(&error);
+
+ assert_eq!(summary, "loading extension failed");
+ assert!(error.to_string().contains(SECRET));
+ }
+
+ #[test]
+ fn structural_variants_keep_their_detail() {
+ let summary = safe_error_summary(&LibsyError::TargetNotFound {
+ target: ModelId::new("strong"),
+ });
+ assert!(summary.contains("strong"), "{summary}");
+ }
+}