|
1 | 1 | use codex_core::ConversationManager; |
2 | 2 | use codex_core::ModelProviderInfo; |
3 | 3 | use codex_core::built_in_model_providers; |
| 4 | +use codex_core::protocol::AskForApproval; |
4 | 5 | use codex_core::protocol::EventMsg; |
5 | 6 | use codex_core::protocol::InputItem; |
6 | 7 | use codex_core::protocol::Op; |
| 8 | +use codex_core::protocol::SandboxPolicy; |
| 9 | +use codex_core::protocol_config_types::ReasoningEffort as ReasoningEffortConfig; |
| 10 | +use codex_core::protocol_config_types::ReasoningSummary as ReasoningSummaryConfig; |
7 | 11 | use codex_login::CodexAuth; |
8 | 12 | use core_test_support::load_default_config_for_test; |
9 | 13 | use core_test_support::load_sse_fixture_with_id; |
@@ -129,3 +133,126 @@ async fn prefixes_context_and_instructions_once_and_consistently_across_requests |
129 | 133 | ); |
130 | 134 | assert_eq!(body2["input"], expected_body2); |
131 | 135 | } |
| 136 | + |
| 137 | +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 138 | +async fn overrides_turn_context_but_keeps_cached_prefix_and_key_constant() { |
| 139 | + use pretty_assertions::assert_eq; |
| 140 | + |
| 141 | + let server = MockServer::start().await; |
| 142 | + |
| 143 | + let sse = sse_completed("resp"); |
| 144 | + let template = ResponseTemplate::new(200) |
| 145 | + .insert_header("content-type", "text/event-stream") |
| 146 | + .set_body_raw(sse, "text/event-stream"); |
| 147 | + |
| 148 | + // Expect two POSTs to /v1/responses |
| 149 | + Mock::given(method("POST")) |
| 150 | + .and(path("/v1/responses")) |
| 151 | + .respond_with(template) |
| 152 | + .expect(2) |
| 153 | + .mount(&server) |
| 154 | + .await; |
| 155 | + |
| 156 | + let model_provider = ModelProviderInfo { |
| 157 | + base_url: Some(format!("{}/v1", server.uri())), |
| 158 | + ..built_in_model_providers()["openai"].clone() |
| 159 | + }; |
| 160 | + |
| 161 | + let cwd = TempDir::new().unwrap(); |
| 162 | + let codex_home = TempDir::new().unwrap(); |
| 163 | + let mut config = load_default_config_for_test(&codex_home); |
| 164 | + config.cwd = cwd.path().to_path_buf(); |
| 165 | + config.model_provider = model_provider; |
| 166 | + config.user_instructions = Some("be consistent and helpful".to_string()); |
| 167 | + |
| 168 | + let conversation_manager = ConversationManager::default(); |
| 169 | + let codex = conversation_manager |
| 170 | + .new_conversation_with_auth(config, Some(CodexAuth::from_api_key("Test API Key"))) |
| 171 | + .await |
| 172 | + .expect("create new conversation") |
| 173 | + .conversation; |
| 174 | + |
| 175 | + // First turn |
| 176 | + codex |
| 177 | + .submit(Op::UserInput { |
| 178 | + items: vec![InputItem::Text { |
| 179 | + text: "hello 1".into(), |
| 180 | + }], |
| 181 | + }) |
| 182 | + .await |
| 183 | + .unwrap(); |
| 184 | + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await; |
| 185 | + |
| 186 | + // Change everything about the turn context. |
| 187 | + let new_cwd = TempDir::new().unwrap(); |
| 188 | + let writable = TempDir::new().unwrap(); |
| 189 | + codex |
| 190 | + .submit(Op::OverrideTurnContext { |
| 191 | + cwd: Some(new_cwd.path().to_path_buf()), |
| 192 | + approval_policy: Some(AskForApproval::Never), |
| 193 | + sandbox_policy: Some(SandboxPolicy::WorkspaceWrite { |
| 194 | + writable_roots: vec![writable.path().to_path_buf()], |
| 195 | + network_access: true, |
| 196 | + exclude_tmpdir_env_var: true, |
| 197 | + exclude_slash_tmp: true, |
| 198 | + }), |
| 199 | + model: Some("o3".to_string()), |
| 200 | + effort: Some(ReasoningEffortConfig::High), |
| 201 | + summary: Some(ReasoningSummaryConfig::Detailed), |
| 202 | + }) |
| 203 | + .await |
| 204 | + .unwrap(); |
| 205 | + |
| 206 | + // Second turn after overrides |
| 207 | + codex |
| 208 | + .submit(Op::UserInput { |
| 209 | + items: vec![InputItem::Text { |
| 210 | + text: "hello 2".into(), |
| 211 | + }], |
| 212 | + }) |
| 213 | + .await |
| 214 | + .unwrap(); |
| 215 | + wait_for_event(&codex, |ev| matches!(ev, EventMsg::TaskComplete(_))).await; |
| 216 | + |
| 217 | + // Verify we issued exactly two requests, and the cached prefix stayed identical. |
| 218 | + let requests = server.received_requests().await.unwrap(); |
| 219 | + assert_eq!(requests.len(), 2, "expected two POST requests"); |
| 220 | + |
| 221 | + let body1 = requests[0].body_json::<serde_json::Value>().unwrap(); |
| 222 | + let body2 = requests[1].body_json::<serde_json::Value>().unwrap(); |
| 223 | + |
| 224 | + // prompt_cache_key should remain constant across overrides |
| 225 | + assert_eq!( |
| 226 | + body1["prompt_cache_key"], body2["prompt_cache_key"], |
| 227 | + "prompt_cache_key should not change across overrides" |
| 228 | + ); |
| 229 | + |
| 230 | + // The entire prefix from the first request should be identical and reused |
| 231 | + // as the prefix of the second request, ensuring cache hit potential. |
| 232 | + let expected_user_message_2 = serde_json::json!({ |
| 233 | + "type": "message", |
| 234 | + "id": serde_json::Value::Null, |
| 235 | + "role": "user", |
| 236 | + "content": [ { "type": "input_text", "text": "hello 2" } ] |
| 237 | + }); |
| 238 | + // After overriding the turn context, the environment context should be emitted again |
| 239 | + // reflecting the new cwd, approval policy and sandbox settings. |
| 240 | + let expected_env_text_2 = format!( |
| 241 | + "<environment_context>\nCurrent working directory: {}\nApproval policy: never\nSandbox mode: workspace-write\nNetwork access: enabled\n</environment_context>", |
| 242 | + new_cwd.path().to_string_lossy() |
| 243 | + ); |
| 244 | + let expected_env_msg_2 = serde_json::json!({ |
| 245 | + "type": "message", |
| 246 | + "id": serde_json::Value::Null, |
| 247 | + "role": "user", |
| 248 | + "content": [ { "type": "input_text", "text": expected_env_text_2 } ] |
| 249 | + }); |
| 250 | + let expected_body2 = serde_json::json!( |
| 251 | + [ |
| 252 | + body1["input"].as_array().unwrap().as_slice(), |
| 253 | + [expected_env_msg_2, expected_user_message_2].as_slice(), |
| 254 | + ] |
| 255 | + .concat() |
| 256 | + ); |
| 257 | + assert_eq!(body2["input"], expected_body2); |
| 258 | +} |
0 commit comments