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(protocol): keep Codex compaction requests on the parent route (#694)
Elyas Mehtabuddindb9c9632e336
diff --git a/crates/protocol/src/metadata.rs b/crates/protocol/src/metadata.rsindex 9c62add80..a45987b57 100644--- a/crates/protocol/src/metadata.rs+++ b/crates/protocol/src/metadata.rs@@ -263,19 +263,14 @@ fn parse_sub_agent(headers: &http::HeaderMap) -> (Option<String>, bool, bool) {let is_subagent = explicit.unwrap_or(claude_subagent || codex_child || harness_kind.is_some());- let is_delegated_work = match explicit {- Some(false) => false,- Some(true) => harness_kind- .as_deref()- .map(|k| SUBAGENT_WORK_KINDS.contains(&k))- .unwrap_or(true),- None => {- claude_subagent- || codex_child- || harness_kind- .as_deref()- .is_some_and(|k| SUBAGENT_WORK_KINDS.contains(&k))- }+ // An explicit false disables subagent routing. Otherwise, a supplied task kind must+ // be in SUBAGENT_WORK_KINDS; child-thread headers cannot override it. Without a kind,+ // an explicit true or recognized child-thread headers indicate subagent work.+ let is_delegated_work = match (explicit, harness_kind.as_deref()) {+ (Some(false), _) => false,+ (_, Some(kind)) => SUBAGENT_WORK_KINDS.contains(&kind),+ (Some(true), None) => true,+ (None, None) => claude_subagent || codex_child,};(parent, is_subagent, is_delegated_work)diff --git a/crates/switchyard-server/tests/server.rs b/crates/switchyard-server/tests/server.rsindex 847baf408..c8da27d8f 100644--- a/crates/switchyard-server/tests/server.rs+++ b/crates/switchyard-server/tests/server.rs@@ -2889,6 +2889,92 @@ selector = "/decision/target"Ok(())}+// Compaction uses the parent classifier; review tasks use the subagent worker.+#[tokio::test]+async fn codex_maintenance_uses_the_parent_classifier_across_apis() -> TestResult {+ let upstream = MockUpstream::start().await?;+ let app = build_switchyard_router(load_test_config(&format!(+ r#"+schema_version = 1+[llm_clients.upstream]+format = "openai_chat"+base_url = "{base_url}"+[targets]+classifier = {{ id = "model/classifier", llm_client = "upstream" }}+strong = {{ id = "model/strong", llm_client = "upstream" }}+weak = {{ id = "model/weak", llm_client = "upstream" }}+[routes.agent]+id = "agent"+type = "composite"+classifier = {{ target = "classifier", base_threshold = 0.5, classify_trigger = "user_turn" }}+stage = {{ capable_target = "strong", efficient_target = "weak", confidence_threshold = 0.3 }}+subagents = {{ type = "passthrough", target = "strong" }}+"#,+ base_url = upstream.base_url+ ))?);+ let cases = [+ (Some("compact"), None, "model/weak"),+ (Some("review"), None, "model/strong"),+ (Some("collab_spawn"), None, "model/strong"),+ (Some("unknown"), None, "model/weak"),+ (Some("memory_consolidation"), None, "model/weak"),+ (None, None, "model/strong"),+ (Some("review"), Some("false"), "model/weak"),+ (Some("compact"), Some("true"), "model/weak"),+ ];+ for (path, body, cases) in [+ (+ "/v1/chat/completions",+ json!({"model":"agent","messages":[{"role":"user","content":"hi"}]}),+ &cases[..],+ ),+ (+ "/v1/messages",+ json!({"model":"agent","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}),+ &cases[..2],+ ),+ (+ "/v1/responses",+ json!({"model":"agent","input":"hi"}),+ &cases[..2],+ ),+ ] {+ for (kind, explicit, expected) in cases {+ let mut metadata = json!({+ "thread_id": "child",+ "parent_thread_id": "root",+ "thread_source": "subagent",+ });+ if let Some(kind) = kind {+ metadata["subagent_kind"] = json!(kind);+ }+ let metadata = metadata.to_string();+ let mut headers = vec![("x-codex-turn-metadata", metadata.as_str())];+ if let Some(explicit) = explicit {+ headers.push(("x-switchyard-is-subagent", explicit));+ }+ let response =+ send_with_headers(&app, "POST", path, Some(body.clone()), &headers).await?;+ assert_eq!(response.status, StatusCode::OK);+ assert_eq!(response.headers["x-model-router-selected-model"], *expected);+ }+ }+ let models = upstream.models().await;+ for (model, expected) in [+ ("model/classifier", 7),+ ("model/weak", 7),+ ("model/strong", 5),+ ] {+ assert_eq!(+ models.iter().filter(|actual| *actual == model).count(),+ expected+ );+ }+ let stats = send(&app, "GET", "/v1/stats", None).await?.json()?;+ assert_eq!(stats["classifier"]["total_requests"], 7);+ Ok(())+}+#[tokio::test]async fn all_inbound_formats_run_libsy_and_return_the_caller_format() -> TestResult {let (upstream, app) = test_app(&[(ROUTE_MODEL, &["model/a"])]).await?;