mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-08 10:27:46 +08:00
fix(codex): 统一通用缓存键与原生会话身份
This commit is contained in:
@@ -4,7 +4,9 @@ use super::{
|
||||
apply_codex_openai_responses_special_body_edits, apply_codex_openai_special_headers,
|
||||
codex_model_capabilities,
|
||||
};
|
||||
use crate::ai_serving::planner::standard::build_local_openai_responses_request_body;
|
||||
use crate::ai_serving::planner::standard::{
|
||||
build_cross_format_openai_responses_request_body, build_local_openai_responses_request_body,
|
||||
};
|
||||
use http::{HeaderMap, HeaderValue};
|
||||
use serde_json::json;
|
||||
|
||||
@@ -180,11 +182,11 @@ fn does_not_synthesize_prompt_cache_key_from_api_key_identity() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_existing_prompt_cache_key_for_codex_requests() {
|
||||
fn adapts_generic_prompt_cache_key_to_codex_native_identity() {
|
||||
let mut body = json!({
|
||||
"model": "gpt-5",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": "existing-key",
|
||||
"prompt_cache_key": "ltm-pc-v2-5557e02f5c9b447a97673ba330dbe77a",
|
||||
});
|
||||
|
||||
apply_codex_openai_responses_special_body_edits(
|
||||
@@ -195,7 +197,222 @@ fn keeps_existing_prompt_cache_key_for_codex_requests() {
|
||||
Some("key-123"),
|
||||
);
|
||||
|
||||
assert_eq!(body["prompt_cache_key"], "existing-key");
|
||||
let expected_identity = "d9c5d122-7c1c-5fb1-ba9d-656062eda44e";
|
||||
assert_eq!(body["prompt_cache_key"], expected_identity);
|
||||
assert_eq!(body["client_metadata"]["session_id"], expected_identity);
|
||||
assert_eq!(body["client_metadata"]["thread_id"], expected_identity);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_native_codex_cache_identity_and_metadata() {
|
||||
let mut body = json!({
|
||||
"model": "gpt-5",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": "guardian:parent-thread",
|
||||
"client_metadata": {
|
||||
"session_id": "native-session",
|
||||
"thread_id": "native-thread",
|
||||
"turn_id": "native-turn"
|
||||
}
|
||||
});
|
||||
let expected = body.clone();
|
||||
|
||||
apply_codex_openai_responses_special_body_edits(
|
||||
&mut body,
|
||||
"codex",
|
||||
"openai:responses",
|
||||
None,
|
||||
Some("key-123"),
|
||||
);
|
||||
|
||||
assert_eq!(body["prompt_cache_key"], expected["prompt_cache_key"]);
|
||||
assert_eq!(body["client_metadata"], expected["client_metadata"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preserves_uuid_prompt_cache_key_while_completing_codex_identity() {
|
||||
let identity = "172c39e6-c0a0-5a70-8b63-e0f8e0d185a3";
|
||||
let mut body = json!({
|
||||
"model": "gpt-5.6-luna",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": identity
|
||||
});
|
||||
|
||||
apply_codex_openai_responses_special_body_edits(
|
||||
&mut body,
|
||||
"codex",
|
||||
"openai:responses",
|
||||
None,
|
||||
None,
|
||||
);
|
||||
|
||||
assert_eq!(body["prompt_cache_key"], identity);
|
||||
assert_eq!(body["client_metadata"]["session_id"], identity);
|
||||
assert_eq!(body["client_metadata"]["thread_id"], identity);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_codex_prompt_cache_domains_distinct() {
|
||||
let mut first = json!({
|
||||
"model": "gpt-5.6-luna",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": "tenant-a"
|
||||
});
|
||||
let mut second = json!({
|
||||
"model": "gpt-5.6-luna",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": "tenant-b"
|
||||
});
|
||||
|
||||
for body in [&mut first, &mut second] {
|
||||
apply_codex_openai_responses_special_body_edits(
|
||||
body,
|
||||
"codex",
|
||||
"openai:responses",
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
assert_ne!(first["prompt_cache_key"], second["prompt_cache_key"]);
|
||||
assert_eq!(
|
||||
first["prompt_cache_key"],
|
||||
first["client_metadata"]["session_id"]
|
||||
);
|
||||
assert_eq!(
|
||||
second["prompt_cache_key"],
|
||||
second["client_metadata"]["session_id"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_partial_and_null_codex_client_metadata() {
|
||||
let mut partial = json!({
|
||||
"model": "gpt-5.6-luna",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": "generic-affinity",
|
||||
"client_metadata": {
|
||||
"thread_id": "native-thread",
|
||||
"caller": "sdk"
|
||||
}
|
||||
});
|
||||
let mut null_metadata = json!({
|
||||
"model": "gpt-5.6-luna",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": "generic-affinity",
|
||||
"client_metadata": null
|
||||
});
|
||||
|
||||
for body in [&mut partial, &mut null_metadata] {
|
||||
apply_codex_openai_responses_special_body_edits(
|
||||
body,
|
||||
"codex",
|
||||
"openai:responses",
|
||||
None,
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(partial["client_metadata"]["thread_id"], "native-thread");
|
||||
assert_eq!(partial["client_metadata"]["caller"], "sdk");
|
||||
assert_eq!(
|
||||
partial["client_metadata"]["session_id"],
|
||||
partial["prompt_cache_key"]
|
||||
);
|
||||
assert_eq!(
|
||||
null_metadata["client_metadata"]["session_id"],
|
||||
null_metadata["prompt_cache_key"]
|
||||
);
|
||||
assert_eq!(
|
||||
null_metadata["client_metadata"]["thread_id"],
|
||||
null_metadata["prompt_cache_key"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leaves_malformed_codex_client_metadata_unchanged() {
|
||||
let mut body = json!({
|
||||
"model": "gpt-5.6-luna",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": "generic-affinity",
|
||||
"client_metadata": "invalid"
|
||||
});
|
||||
|
||||
apply_codex_openai_responses_special_body_edits(
|
||||
&mut body,
|
||||
"codex",
|
||||
"openai:responses",
|
||||
None,
|
||||
None,
|
||||
);
|
||||
|
||||
assert_eq!(body["prompt_cache_key"], "generic-affinity");
|
||||
assert_eq!(body["client_metadata"], "invalid");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn limits_prompt_cache_identity_adaptation_to_codex_responses_family() {
|
||||
let original = json!({
|
||||
"model": "gpt-5.6-luna",
|
||||
"input": "hello",
|
||||
"prompt_cache_key": "generic-affinity"
|
||||
});
|
||||
let mut standard_openai = original.clone();
|
||||
let mut codex_compact = original.clone();
|
||||
|
||||
apply_codex_openai_responses_special_body_edits(
|
||||
&mut standard_openai,
|
||||
"openai",
|
||||
"openai:responses",
|
||||
None,
|
||||
None,
|
||||
);
|
||||
apply_codex_openai_responses_special_body_edits(
|
||||
&mut codex_compact,
|
||||
"codex",
|
||||
"openai:responses:compact",
|
||||
None,
|
||||
None,
|
||||
);
|
||||
|
||||
assert_eq!(standard_openai, original);
|
||||
assert_ne!(codex_compact["prompt_cache_key"], "generic-affinity");
|
||||
assert!(codex_compact.get("client_metadata").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chat_to_codex_responses_adapts_prompt_cache_identity_end_to_end() {
|
||||
let body = json!({
|
||||
"model": "gpt-5.6-luna",
|
||||
"messages": [{"role": "user", "content": "hello"}],
|
||||
"prompt_cache_key": "ltm-pc-v2-5557e02f5c9b447a97673ba330dbe77a"
|
||||
});
|
||||
|
||||
let provider_request_body = build_cross_format_openai_responses_request_body(
|
||||
&body,
|
||||
"gpt-5.6-luna",
|
||||
"openai:chat",
|
||||
"openai:responses",
|
||||
true,
|
||||
false,
|
||||
"codex",
|
||||
None,
|
||||
None,
|
||||
&HeaderMap::new(),
|
||||
false,
|
||||
)
|
||||
.expect("chat to Codex Responses request should build");
|
||||
|
||||
let expected_identity = "d9c5d122-7c1c-5fb1-ba9d-656062eda44e";
|
||||
assert_eq!(provider_request_body["prompt_cache_key"], expected_identity);
|
||||
assert_eq!(
|
||||
provider_request_body["client_metadata"]["session_id"],
|
||||
expected_identity
|
||||
);
|
||||
assert_eq!(
|
||||
provider_request_body["client_metadata"]["thread_id"],
|
||||
expected_identity
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user