fix(codex): strip cache_control before responses upstream

This commit is contained in:
stabey
2026-06-04 12:01:36 +08:00
parent ada690624b
commit 2de2a792f6
@@ -398,6 +398,23 @@ fn collect_codex_prompt_cache_control_anchors(value: &Value, anchors: &mut Vec<V
}
}
fn strip_codex_cache_control_fields(value: &mut Value) {
match value {
Value::Object(object) => {
object.remove("cache_control");
for child in object.values_mut() {
strip_codex_cache_control_fields(child);
}
}
Value::Array(items) => {
for child in items {
strip_codex_cache_control_fields(child);
}
}
_ => {}
}
}
fn extract_codex_prompt_cache_control_seed(provider_request_body: &Value) -> Option<String> {
let mut anchors = Vec::new();
collect_codex_prompt_cache_control_anchors(provider_request_body, &mut anchors);
@@ -780,6 +797,7 @@ pub fn apply_codex_openai_responses_special_body_edits(
inject_codex_default_variation_prompt(body_object);
}
strip_codex_cache_control_fields(provider_request_body);
insert_codex_prompt_cache_key(provider_request_body, prompt_cache_key);
}
@@ -1206,6 +1224,49 @@ mod tests {
assert_eq!(body_a["prompt_cache_key"], body_b["prompt_cache_key"]);
assert_ne!(body_a["prompt_cache_key"], body_c["prompt_cache_key"]);
assert!(!body_a.to_string().contains("\"cache_control\""));
assert!(!body_b.to_string().contains("\"cache_control\""));
assert!(!body_c.to_string().contains("\"cache_control\""));
}
#[test]
fn codex_responses_body_edits_strip_developer_cache_control_before_upstream() {
let mut provider_request_body = json!({
"input": [{
"type": "message",
"role": "developer",
"content": [{
"type": "input_text",
"text": "stable system brief",
"cache_control": {"type": "ephemeral"}
}]
}, {
"type": "message",
"role": "user",
"content": [{"type": "input_text", "text": "new turn"}]
}],
"model": "gpt-5.4"
});
apply_codex_openai_responses_special_body_edits(
&mut provider_request_body,
"codex",
"openai:responses",
None,
Some("key-a"),
);
assert!(provider_request_body
.get("prompt_cache_key")
.and_then(|value| value.as_str())
.is_some_and(|value| !value.trim().is_empty()));
assert!(!provider_request_body
.to_string()
.contains("\"cache_control\""));
assert_eq!(
provider_request_body["input"][0]["content"][0]["text"],
json!("stable system brief")
);
}
#[test]