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(routing): send Codex spawned threads to the subagent route (#748)

Elyas Mehtabuddinb9219793cbe8
diff --git a/crates/protocol/src/metadata.rs b/crates/protocol/src/metadata.rs
index c4e91bcab..d2e73ee5c 100644
--- a/crates/protocol/src/metadata.rs
+++ b/crates/protocol/src/metadata.rs
@@ -75,7 +75,7 @@ const CLAUDE_CHILD_IDENTITY_VERSION: [u64; 3] = [2, 1, 139];
/// Harness-defined sub-agent kinds that carry delegated user work rather than
/// harness maintenance (`compact`, `memory_consolidation`, ...). Unknown kinds
/// are excluded deliberately; extend with captured request fixtures.
-const SUBAGENT_WORK_KINDS: &[&str] = &["collab_spawn", "review"];
+const SUBAGENT_WORK_KINDS: &[&str] = &["collab_spawn", "thread_spawn", "review"];
/// Ordered candidate lookup paths for each correlation field, keyed by the field's
/// canonical `x-switchyard-*` header name.
@@ -267,9 +267,9 @@ fn parse_sub_agent(headers: &http::HeaderMap) -> (Option<String>, bool, bool) {
let parent = sy_header(headers, SWITCHYARD_PARENT_AGENT_ID_HEADER)
.or_else(|| claude_parent.map(str::to_string));
- // Current Codex releases identify spawned children through lineage rather than
- // `subagent_kind`. Require both fields so a parent id used only for correlation
- // cannot accidentally route an ordinary turn as delegated work.
+ // Codex child lineage requires both a parent id and `thread_source = subagent`.
+ // A parent id used only for correlation must not route an ordinary turn as
+ // delegated work.
let codex_child = parent.is_some()
&& resolve_path(headers, CODEX_THREAD_SOURCE_PATH).as_deref() == Some("subagent");
diff --git a/crates/switchyard-server/tests/server.rs b/crates/switchyard-server/tests/server.rs
index 98ea8387a..fee47a1d3 100644
--- a/crates/switchyard-server/tests/server.rs
+++ b/crates/switchyard-server/tests/server.rs
@@ -3464,7 +3464,8 @@ selector = "/decision/target"
Ok(())
}
-// Compaction uses the parent classifier; review tasks use the subagent worker.
+// Codex's structured kind takes precedence over the flat `collab_spawn` header:
+// maintenance uses the parent classifier; delegated work uses the subagent route.
#[tokio::test]
async fn codex_maintenance_uses_the_parent_classifier_across_apis() -> TestResult {
let upstream = MockUpstream::start().await?;
@@ -3490,6 +3491,7 @@ subagents = {{ type = "passthrough", target = "strong" }}
let cases = [
(Some("compact"), None, "model/weak"),
(Some("review"), None, "model/strong"),
+ (Some("thread_spawn"), None, "model/strong"),
(Some("collab_spawn"), None, "model/strong"),
(Some("unknown"), None, "model/weak"),
(Some("memory_consolidation"), None, "model/weak"),
@@ -3506,15 +3508,16 @@ subagents = {{ type = "passthrough", target = "strong" }}
(
"/v1/messages",
json!({"model":"agent","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}),
- &cases[..2],
+ &cases[..3],
),
(
"/v1/responses",
json!({"model":"agent","input":"hi"}),
- &cases[..2],
+ &cases[..3],
),
] {
for (kind, explicit, expected) in cases {
+ let mut headers = Vec::new();
let mut metadata = json!({
"thread_id": "child",
"parent_thread_id": "root",
@@ -3522,9 +3525,10 @@ subagents = {{ type = "passthrough", target = "strong" }}
});
if let Some(kind) = kind {
metadata["subagent_kind"] = json!(kind);
+ headers.push(("x-openai-subagent", "collab_spawn"));
}
let metadata = metadata.to_string();
- let mut headers = vec![("x-codex-turn-metadata", metadata.as_str())];
+ headers.push(("x-codex-turn-metadata", metadata.as_str()));
if let Some(explicit) = explicit {
headers.push(("x-switchyard-is-subagent", explicit));
}
@@ -3538,7 +3542,7 @@ subagents = {{ type = "passthrough", target = "strong" }}
for (model, expected) in [
("model/classifier", 7),
("model/weak", 7),
- ("model/strong", 5),
+ ("model/strong", 8),
] {
assert_eq!(
models.iter().filter(|actual| *actual == model).count(),