From ab6095dde0040bb8cdf8592acb89c344e5f27a56 Mon Sep 17 00:00:00 2001 From: Rohan Mehta Date: Tue, 20 Oct 2026 14:06:46 +0000 Subject: [PATCH] Allow callers to control prompt cache keys independently of session IDs (#55587) ## What changed Expose `PromptCacheKey` through `StartThreadOptions` with three policies: retain the default session-derived key, send a custom key, or omit `prompt_cache_key` from model requests. Keep session identity headers independent of this policy. Propagate the policy to child agents, delegated sessions, and guardian review sessions. Hosts must reapply the policy when resuming a thread from storage. ## Testing Add coverage for all three policies across turns, including unchanged session and thread headers. Extend subagent coverage to verify inheritance of the default and omitted-key policies. GitOrigin-RevId: f9e8ab31fcf568636f9712726760e4e9a4672428 --- codex-rs/core-api/src/lib.rs | 1 + codex-rs/core/src/agent/control/spawn.rs | 22 +++-- codex-rs/core/src/client.rs | 41 +++++++--- codex-rs/core/src/client_tests.rs | 12 +-- codex-rs/core/src/codex_delegate.rs | 4 + .../core/src/guardian/review_session_setup.rs | 1 + codex-rs/core/src/lib.rs | 1 + codex-rs/core/src/session/mod.rs | 4 + codex-rs/core/src/session/session.rs | 2 + codex-rs/core/src/session/tests.rs | 2 + .../core/src/session/tests/guardian_tests.rs | 1 + codex-rs/core/src/thread_manager.rs | 49 +++++++++--- codex-rs/core/src/thread_manager_tests.rs | 9 ++- codex-rs/core/tests/suite/prompt_cache_key.rs | 80 +++++++++++++++++-- 14 files changed, 185 insertions(+), 44 deletions(-) diff --git a/codex-rs/core-api/src/lib.rs b/codex-rs/core-api/src/lib.rs index 97f2e8afbc..5c9fcdd84b 100644 --- a/codex-rs/core-api/src/lib.rs +++ b/codex-rs/core-api/src/lib.rs @@ -48,6 +48,7 @@ pub use codex_core::NetworkUnixSocketPermission; pub use codex_core::NetworkUnixSocketPermissions; pub use codex_core::NewThread; pub use codex_core::NotSubmittedReason; +pub use codex_core::PromptCacheKey; pub use codex_core::RecoverTurnRequest; pub use codex_core::StartIfIdleSubmission; pub use codex_core::StartThreadOptions; diff --git a/codex-rs/core/src/agent/control/spawn.rs b/codex-rs/core/src/agent/control/spawn.rs index c13d3b20f2..22cd725605 100644 --- a/codex-rs/core/src/agent/control/spawn.rs +++ b/codex-rs/core/src/agent/control/spawn.rs @@ -607,11 +607,21 @@ impl LocalAgentControl { None, ) }; - let inherited_instructions = if let Some((parent, _)) = parent.as_ref() { - Some(parent.session.inherited_instructions().await) - } else if let Some(parent_thread_id) = parent_thread_id - && let Ok(parent) = state.get_thread(parent_thread_id).await - { + let inherited_parent = if let Some((parent, _)) = parent.as_ref() { + Some(Arc::clone(parent)) + } else if let Some(parent_thread_id) = parent_thread_id { + state.get_thread(parent_thread_id).await.ok() + } else { + None + }; + let prompt_cache_key = inherited_parent.as_ref().map(|parent| { + parent + .session + .services + .model_client + .prompt_cache_key_policy() + }); + let inherited_instructions = if let Some(parent) = inherited_parent { Some(parent.session.inherited_instructions().await) } else { self.runtime @@ -644,6 +654,7 @@ impl LocalAgentControl { match state .resume_thread_with_history_with_source(ResumeThreadWithHistoryOptions { config, + prompt_cache_key, initial_history, agent_control: self.clone(), session_source, @@ -1524,6 +1535,7 @@ impl LocalAgentControl { let resumed_thread = state .resume_thread_with_history_with_source(ResumeThreadWithHistoryOptions { config: config.clone(), + prompt_cache_key: None, initial_history, agent_control: self.clone(), session_source, diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 172a7416ed..a63641a4d7 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -36,6 +36,7 @@ use std::sync::atomic::Ordering; mod tool_metadata; use crate::CodexResponsesHeaders; +use crate::PromptCacheKey; use crate::tools::ExecutedToolCalls; use async_channel::Sender; use codex_api::AgentIdentityTelemetry; @@ -268,7 +269,8 @@ impl RequestRouteTelemetry { pub struct ModelClient { state: Arc, agent_identity_policy: AgentIdentityAuthPolicy, - prompt_cache_key_override: Option, + prompt_cache_key_policy: PromptCacheKey, + session_id_override: Option, codex_responses_headers: Option>, event_sender: Option>, http_client_factory: HttpClientFactory, @@ -601,7 +603,8 @@ impl ModelClient { last_inference_tools: StdMutex::new(None), }), agent_identity_policy, - prompt_cache_key_override: None, + prompt_cache_key_policy: PromptCacheKey::default(), + session_id_override: None, codex_responses_headers: None, event_sender: None, http_client_factory, @@ -631,30 +634,45 @@ impl ModelClient { pub(crate) fn with_session_context( mut self, - prompt_cache_key_override: Option, + prompt_cache_key: PromptCacheKey, + session_id_override: Option, event_sender: Sender, codex_responses_headers: Option>, api_key_cyber_access_programs: cyber_access_program::ApiKeyCyberAccessPrograms, ) -> Self { - self.prompt_cache_key_override = prompt_cache_key_override; + self.prompt_cache_key_policy = prompt_cache_key; + self.session_id_override = session_id_override; self.event_sender = Some(event_sender); self.codex_responses_headers = codex_responses_headers; self.api_key_cyber_access_programs = api_key_cyber_access_programs; self } - fn prompt_cache_key(&self, responses_metadata: &CodexResponsesMetadata) -> String { - if let Some(prompt_cache_key) = &self.prompt_cache_key_override { - return prompt_cache_key.clone(); + pub(crate) fn prompt_cache_key_policy(&self) -> PromptCacheKey { + self.prompt_cache_key_policy.clone() + } + + /// The caller's policy controls the request body independently of the session-id header. + fn prompt_cache_key(&self, metadata: &CodexResponsesMetadata) -> Option { + match &self.prompt_cache_key_policy { + PromptCacheKey::SessionDerived => Some(self.session_derived_cache_key(metadata)), + PromptCacheKey::Custom(key) => Some(key.clone()), + PromptCacheKey::Omit => None, + } + } + + fn session_derived_cache_key(&self, metadata: &CodexResponsesMetadata) -> String { + if let Some(session_id) = &self.session_id_override { + return session_id.clone(); } if let SessionSource::Internal(source) = &self.state.session_source - && let Some(parent_thread_id) = responses_metadata.parent_thread_id + && let Some(parent_thread_id) = metadata.parent_thread_id { return format!("{source}:{parent_thread_id}"); } - responses_metadata.session_id.clone() + metadata.session_id.clone() } // ChatGPT derives cache affinity from the Responses session-id header. Keep the @@ -663,7 +681,7 @@ impl ModelClient { if self.state.session_source.is_non_root_agent() { metadata.session_id.clone() } else { - self.prompt_cache_key(metadata) + self.session_derived_cache_key(metadata) } } @@ -1069,7 +1087,6 @@ impl ModelClient { &prompt.output_schema, prompt.output_schema_strict, ); - let prompt_cache_key = Some(self.prompt_cache_key(responses_metadata)); let service_tier = model_info .service_tier_for_request(service_tier) .filter(|tier| { @@ -1099,7 +1116,7 @@ impl ModelClient { stream_options, include, service_tier, - prompt_cache_key, + prompt_cache_key: self.prompt_cache_key(responses_metadata), text, client_metadata: Some(client_metadata), access_programs: None, diff --git a/codex-rs/core/src/client_tests.rs b/codex-rs/core/src/client_tests.rs index 46ac895307..b9a23192a7 100644 --- a/codex-rs/core/src/client_tests.rs +++ b/codex-rs/core/src/client_tests.rs @@ -1514,7 +1514,7 @@ fn internal_session_prompt_cache_key_is_scoped_to_parent_thread() { assert_eq!( client.prompt_cache_key(&metadata), - format!("guardian:{parent_thread_id}") + Some(format!("guardian:{parent_thread_id}")) ); } @@ -2077,11 +2077,11 @@ fn thread_responses_headers_are_scoped_to_model_and_backend_auth() { ); } -#[test_case::test_case(/*cache_key*/ None; "own_cache")] -#[test_case::test_case(Some("parent-session"); "inherited_cache")] +#[test_case::test_case(/*session_id_override*/ None; "own_session_id")] +#[test_case::test_case(Some("parent-session"); "inherited_session_id")] #[tokio::test] async fn websocket_handshake_includes_attestation_for_chatgpt_codex_responses( - cache_key: Option<&str>, + session_id_override: Option<&str>, ) { let (mut model_client, attestation_calls) = model_client_with_counting_attestation(/*include_attestation*/ true); @@ -2093,7 +2093,7 @@ async fn websocket_handshake_includes_attestation_for_chatgpt_codex_responses( TestCodexResponsesRequestKind::WebsocketConnection, ); - model_client.prompt_cache_key_override = cache_key.map(str::to_string); + model_client.session_id_override = session_id_override.map(str::to_string); let headers = model_client .build_websocket_headers(&responses_metadata) .await; @@ -2107,7 +2107,7 @@ async fn websocket_handshake_includes_attestation_for_chatgpt_codex_responses( assert_eq!(attestation_calls.load(Ordering::Relaxed), 1); assert_eq!( headers["session-id"], - cache_key.unwrap_or(&responses_metadata.session_id) + session_id_override.unwrap_or(&responses_metadata.session_id) ); assert_eq!(headers["thread-id"], responses_metadata.thread_id); } diff --git a/codex-rs/core/src/codex_delegate.rs b/codex-rs/core/src/codex_delegate.rs index 4a77f764bf..54d0d201d9 100644 --- a/codex-rs/core/src/codex_delegate.rs +++ b/codex-rs/core/src/codex_delegate.rs @@ -105,6 +105,10 @@ pub(crate) async fn run_codex_thread_interactive( let spawn_result = Session::spawn(SessionSpawnArgs { startup: Some(Arc::clone(&startup)), config, + prompt_cache_key: parent_session + .services + .model_client + .prompt_cache_key_policy(), allow_provider_model_fallback: false, instructions, installation_id: parent_session.installation_id.clone(), diff --git a/codex-rs/core/src/guardian/review_session_setup.rs b/codex-rs/core/src/guardian/review_session_setup.rs index 8cae33a364..7131d38025 100644 --- a/codex-rs/core/src/guardian/review_session_setup.rs +++ b/codex-rs/core/src/guardian/review_session_setup.rs @@ -141,6 +141,7 @@ impl PreparedGuardianContext { .model_client .responses_websocket_enabled(); let options = crate::StartThreadOptions { + prompt_cache_key: self.parent.services.model_client.prompt_cache_key_policy(), history_mode: Some(codex_protocol::protocol::ThreadHistoryMode::Paginated), internal_parent: Some(crate::thread_manager::InternalSessionParent { thread_id: self.parent.thread_id(), diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index 477b780f12..97600eb9e6 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -160,6 +160,7 @@ pub use thread_manager::AgentTreeShutdownReport; pub use thread_manager::ForkSnapshot; pub use thread_manager::InternalSessionParent; pub use thread_manager::NewThread; +pub use thread_manager::PromptCacheKey; pub use thread_manager::StartThreadOptions; pub use thread_manager::ThreadManager; pub use thread_manager::ThreadShutdownReport; diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index ee4a5468f9..a35f324712 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -1,5 +1,6 @@ pub(crate) mod startup; +use crate::PromptCacheKey; use crate::WithTurnExtensionData; use std::borrow::Cow; use std::collections::HashMap; @@ -483,6 +484,7 @@ impl From for CompactedHistoryInstallation<'_> { pub(crate) struct SessionSpawnArgs { pub(crate) startup: Option>, pub(crate) config: Config, + pub(crate) prompt_cache_key: PromptCacheKey, pub(crate) allow_provider_model_fallback: bool, pub(crate) instructions: SessionInstructions, pub(crate) installation_id: String, @@ -599,6 +601,7 @@ impl Session { let SessionSpawnArgs { startup, config, + prompt_cache_key, allow_provider_model_fallback, instructions, installation_id, @@ -965,6 +968,7 @@ impl Session { let session = Box::pin(Session::new( startup.clone(), session_configuration, + prompt_cache_key, &environment_selections, config.clone(), instructions, diff --git a/codex-rs/core/src/session/session.rs b/codex-rs/core/src/session/session.rs index 05bc138da7..0e5c4080d5 100644 --- a/codex-rs/core/src/session/session.rs +++ b/codex-rs/core/src/session/session.rs @@ -782,6 +782,7 @@ impl Session { pub(crate) async fn new( startup: Option>, mut session_configuration: SessionConfiguration, + prompt_cache_key: PromptCacheKey, environment_selections: &[TurnEnvironmentSelection], config: Arc, mut instructions: SessionInstructions, @@ -1810,6 +1811,7 @@ impl Session { InitialHistory::Resumed(_) | InitialHistory::Forked(_) )) .with_session_context( + prompt_cache_key, crate::guardian::prompt_cache_key_override_for_review_session( &session_configuration.session_source, session_configuration.parent_thread_id, diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 972349b273..c978a7dc11 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -7175,6 +7175,7 @@ async fn make_session_with_config_and_rx( let session = Session::new( /*startup*/ None, session_configuration, + PromptCacheKey::default(), &default_environments, Arc::clone(&config), SessionInstructions::default(), @@ -7311,6 +7312,7 @@ async fn make_session_with_history_source_and_agent_control_and_rx( let session = Session::new( /*startup*/ None, session_configuration, + PromptCacheKey::default(), &default_environments, Arc::clone(&config), SessionInstructions::default(), diff --git a/codex-rs/core/src/session/tests/guardian_tests.rs b/codex-rs/core/src/session/tests/guardian_tests.rs index e70fa5fd8f..adf47840f0 100644 --- a/codex-rs/core/src/session/tests/guardian_tests.rs +++ b/codex-rs/core/src/session/tests/guardian_tests.rs @@ -1290,6 +1290,7 @@ async fn guardian_subagent_does_not_inherit_parent_exec_policy_rules() { let (session, io) = Session::spawn(SessionSpawnArgs { startup: None, config, + prompt_cache_key: PromptCacheKey::default(), allow_provider_model_fallback: false, instructions: Default::default(), installation_id: "11111111-1111-4111-8111-111111111111".to_string(), diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index 4910246f07..7c27b9d1e3 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -267,8 +267,22 @@ pub struct InternalSessionParent { pub(crate) inherited_instructions: Option, } +/// Controls the prompt cache key sent on model requests, independently of session identity. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub enum PromptCacheKey { + /// Preserve the existing session-derived key, including internal-session and fork routing. + #[default] + SessionDerived, + /// Send the caller's key on every model request in the session. + Custom(String), + /// Leave `prompt_cache_key` out of model requests. + Omit, +} + pub struct StartThreadOptions { pub config: Config, + /// Request-body cache key policy for this thread and its children. Hosts must reapply it on cold resume. + pub prompt_cache_key: PromptCacheKey, /// Host-owned provider for this root thread's additional instructions. /// /// Core composes these after the global [`UserInstructionsProvider`] @@ -313,6 +327,7 @@ impl StartThreadOptions { pub fn new(config: Config) -> Self { Self { config, + prompt_cache_key: PromptCacheKey::default(), thread_instructions_provider: None, allow_provider_model_fallback: false, initial_history: InitialHistory::New, @@ -406,6 +421,7 @@ fn effective_originator_value( pub(crate) struct ResumeThreadWithHistoryOptions { pub(crate) config: Config, + pub(crate) prompt_cache_key: Option, pub(crate) initial_history: InitialHistory, pub(crate) agent_control: LocalAgentControl, pub(crate) session_source: SessionSource, @@ -1993,8 +2009,8 @@ impl ThreadManagerState { parent_thread_id: Option, inherited_exec_policy: Option>, ) -> CodexResult { - options.client_mcp_extensions = - self.client_mcp_extensions_for_child(parent_thread_id).await; + (options.client_mcp_extensions, options.prompt_cache_key) = + self.inherited_child_settings(parent_thread_id).await; let forked = matches!(options.initial_history, InitialHistory::Forked(_)); let mut request = ThreadSpawnRequest::new(options, Arc::clone(&self.auth_manager), agent_control); @@ -2013,6 +2029,7 @@ impl ThreadManagerState { ) -> CodexResult { let ResumeThreadWithHistoryOptions { config, + prompt_cache_key, initial_history, agent_control, session_source, @@ -2023,12 +2040,13 @@ impl ThreadManagerState { inherited_exec_policy, client_mcp_extensions, } = options; - let client_mcp_extensions = match client_mcp_extensions { - Some(client_mcp_extensions) => client_mcp_extensions, - None => self.client_mcp_extensions_for_child(parent_thread_id).await, - }; + let (parent_client_mcp_extensions, inherited_prompt_cache_key) = + self.inherited_child_settings(parent_thread_id).await; + let client_mcp_extensions = client_mcp_extensions.unwrap_or(parent_client_mcp_extensions); + let prompt_cache_key = prompt_cache_key.unwrap_or(inherited_prompt_cache_key); let thread_source = initial_history.get_resumed_thread_source(); let options = StartThreadOptions { + prompt_cache_key, initial_history, session_source: Some(session_source), thread_source, @@ -2050,16 +2068,25 @@ impl ThreadManagerState { Box::pin(self.spawn_thread(request)).await } - async fn client_mcp_extensions_for_child( + async fn inherited_child_settings( &self, parent_thread_id: Option, - ) -> ClientMcpExtensions { + ) -> (ClientMcpExtensions, PromptCacheKey) { let Some(parent_thread_id) = parent_thread_id else { - return ClientMcpExtensions::default(); + return Default::default(); }; self.get_thread(parent_thread_id) .await - .map(|parent| parent.session.services.client_mcp_extensions.clone()) + .map(|parent| { + ( + parent.session.services.client_mcp_extensions.clone(), + parent + .session + .services + .model_client + .prompt_cache_key_policy(), + ) + }) .unwrap_or_default() } @@ -2107,6 +2134,7 @@ impl ThreadManagerState { }; let StartThreadOptions { mut config, + prompt_cache_key, thread_instructions_provider, allow_provider_model_fallback, initial_history, @@ -2338,6 +2366,7 @@ impl ThreadManagerState { let spawn_result = Session::spawn(SessionSpawnArgs { startup, config, + prompt_cache_key, allow_provider_model_fallback, instructions, installation_id: self.installation_id.clone(), diff --git a/codex-rs/core/src/thread_manager_tests.rs b/codex-rs/core/src/thread_manager_tests.rs index 5091ba987c..e740707349 100644 --- a/codex-rs/core/src/thread_manager_tests.rs +++ b/codex-rs/core/src/thread_manager_tests.rs @@ -521,11 +521,12 @@ async fn child_session_inherits_client_mcp_extensions() { .await .expect("start parent thread"); + let (client_mcp_extensions, _) = manager + .state + .inherited_child_settings(Some(parent.thread_id)) + .await; assert_eq!( - manager - .state - .client_mcp_extensions_for_child(Some(parent.thread_id)) - .await, + client_mcp_extensions, ClientMcpExtensions::new(HashMap::from([ (OPENAI_FORM_EXTENSION_ID.to_string(), serde_json::json!({})), ( diff --git a/codex-rs/core/tests/suite/prompt_cache_key.rs b/codex-rs/core/tests/suite/prompt_cache_key.rs index 3e391c51a8..9a5037d390 100644 --- a/codex-rs/core/tests/suite/prompt_cache_key.rs +++ b/codex-rs/core/tests/suite/prompt_cache_key.rs @@ -2,6 +2,8 @@ use std::time::Duration; use anyhow::Result; use anyhow::anyhow; +use codex_core::PromptCacheKey; +use codex_core::StartThreadOptions; use codex_features::Feature; use codex_login::CodexAuth; use codex_protocol::protocol::TurnEnvironmentSelection; @@ -9,9 +11,11 @@ use core_test_support::responses::ev_assistant_message; use core_test_support::responses::ev_completed; use core_test_support::responses::ev_function_call_with_namespace; use core_test_support::responses::ev_response_created; +use core_test_support::responses::mount_sse_once; use core_test_support::responses::mount_sse_once_match; use core_test_support::responses::sse; use core_test_support::responses::start_mock_server; +use core_test_support::test_codex::TestCodex; use core_test_support::test_codex::test_codex; use pretty_assertions::assert_eq; use serde_json::Value; @@ -22,6 +26,62 @@ const CHILD_TASK: &str = "inspect the repository"; const SPAWN_CALL_ID: &str = "spawn-worker"; const COLLABORATION_NAMESPACE: &str = "collaboration"; +async fn start_with_prompt_cache_key(test: &mut TestCodex, policy: PromptCacheKey) -> Result<()> { + let thread = test + .thread_manager + .start_thread(StartThreadOptions { + prompt_cache_key: policy, + ..test.start_thread_options().await + }) + .await?; + test.codex = thread.thread; + test.session_configured = thread.session_configured; + Ok(()) +} + +fn expected_prompt_cache_key(policy: &PromptCacheKey, session_id: &str) -> Option { + match policy { + PromptCacheKey::SessionDerived => Some(json!(session_id)), + PromptCacheKey::Custom(key) => Some(json!(key)), + PromptCacheKey::Omit => None, + } +} + +#[test_case::test_case(PromptCacheKey::SessionDerived; "session_derived")] +#[test_case::test_case(PromptCacheKey::Custom("shared-prefix".to_string()); "custom")] +#[test_case::test_case(PromptCacheKey::Omit; "omitted")] +#[tokio::test] +async fn prompt_cache_key_policy_applies_across_turns(policy: PromptCacheKey) -> Result<()> { + let server = start_mock_server().await; + let mut test = test_codex() + .with_auth(CodexAuth::from_api_key("dummy")) + .build_with_auto_env(&server) + .await?; + start_with_prompt_cache_key(&mut test, policy.clone()).await?; + let session_id = test.session_configured.session_id.to_string(); + let expected_key = expected_prompt_cache_key(&policy, &session_id); + + for turn in ["first", "second"] { + let response = mount_sse_once(&server, sse(vec![ev_completed(turn)])).await; + test.submit_text_turn(turn).await?; + let request = response.single_request(); + let body = request.body_json(); + assert_eq!( + ( + body.get("prompt_cache_key"), + request.header("session-id"), + request.header("thread-id"), + ), + ( + expected_key.as_ref(), + Some(session_id.clone()), + Some(test.session_configured.thread_id.to_string()), + ), + ); + } + Ok(()) +} + fn body_contains(request: &wiremock::Request, text: &str) -> bool { serde_json::from_slice::(&request.body).is_ok_and(|body| body.to_string().contains(text)) } @@ -37,8 +97,10 @@ fn request_has_input_type(request: &wiremock::Request, input_type: &str) -> bool }) } +#[test_case::test_case(PromptCacheKey::SessionDerived; "session_derived")] +#[test_case::test_case(PromptCacheKey::Omit; "omitted")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn api_key_subagent_uses_session_id_as_prompt_cache_key() -> Result<()> { +async fn api_key_subagent_inherits_prompt_cache_key_policy(policy: PromptCacheKey) -> Result<()> { let server = start_mock_server().await; let spawn_args = serde_json::to_string(&json!({ "message": CHILD_TASK, @@ -98,8 +160,10 @@ async fn api_key_subagent_uses_session_id_as_prompt_cache_key() -> Result<()> { .enable(Feature::MultiAgentV2) .expect("test config should allow feature update"); }); - let test = builder.build(&server).await?; + let mut test = builder.build(&server).await?; + start_with_prompt_cache_key(&mut test, policy.clone()).await?; let expected_session_id = test.session_configured.session_id.to_string(); + let expected_cache_key = expected_prompt_cache_key(&policy, &expected_session_id); test.submit_turn(ROOT_PROMPT).await?; let root_request = root_request @@ -124,6 +188,13 @@ async fn api_key_subagent_uses_session_id_as_prompt_cache_key() -> Result<()> { .map_err(|_| anyhow!("timed out waiting for the child request"))?; let child_thread_id = child_request.header("thread-id").expect("child thread ID"); + assert_eq!( + ( + root_request.body_json().get("prompt_cache_key").cloned(), + child_request.body_json().get("prompt_cache_key").cloned(), + ), + (expected_cache_key.clone(), expected_cache_key), + ); assert_eq!( json!({ "differentThreadIds": root_thread_id != child_thread_id, @@ -131,13 +202,11 @@ async fn api_key_subagent_uses_session_id_as_prompt_cache_key() -> Result<()> { "sessionId": root_request.header("session-id"), "threadId": &root_thread_id, "clientRequestId": root_request.header("x-client-request-id"), - "promptCacheKey": root_request.body_json()["prompt_cache_key"].clone(), }, "child": { "sessionId": child_request.header("session-id"), "threadId": &child_thread_id, "clientRequestId": child_request.header("x-client-request-id"), - "promptCacheKey": child_request.body_json()["prompt_cache_key"].clone(), }, }), json!({ @@ -146,13 +215,11 @@ async fn api_key_subagent_uses_session_id_as_prompt_cache_key() -> Result<()> { "sessionId": &expected_session_id, "threadId": &root_thread_id, "clientRequestId": &root_thread_id, - "promptCacheKey": &expected_session_id, }, "child": { "sessionId": &expected_session_id, "threadId": &child_thread_id, "clientRequestId": &child_thread_id, - "promptCacheKey": &expected_session_id, }, }) ); @@ -163,7 +230,6 @@ async fn api_key_subagent_uses_session_id_as_prompt_cache_key() -> Result<()> { #[tokio::test] async fn ephemeral_fork_shares_cache_routing_but_keeps_session_identity() -> Result<()> { use codex_core::ForkSnapshot; - use codex_core::StartThreadOptions; use core_test_support::responses::mount_sse_sequence; let server = start_mock_server().await;