-
Notifications
You must be signed in to change notification settings - Fork 14k
[codex] Add internal auto-compaction opt-out #28260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| #![allow(clippy::expect_used)] | ||
| use anyhow::Result; | ||
| use anyhow::anyhow; | ||
| use codex_core::CodexThread; | ||
| use codex_core::compact::SUMMARIZATION_PROMPT; | ||
| use codex_core::compact::SUMMARY_PREFIX; | ||
| use codex_core::config::Config; | ||
|
|
@@ -35,6 +36,7 @@ use core_test_support::responses; | |
| use core_test_support::responses::ev_reasoning_item; | ||
| use core_test_support::responses::mount_models_once; | ||
| use core_test_support::skip_if_no_network; | ||
| use core_test_support::test_codex::TestCodex; | ||
| use core_test_support::test_codex::local_selections; | ||
| use core_test_support::test_codex::test_codex; | ||
| use core_test_support::test_codex::turn_permission_fields; | ||
|
|
@@ -92,6 +94,48 @@ const REMOTE_V2_SUMMARY: &str = "global-instructions-remote-v2-summary"; | |
|
|
||
| pub(super) const COMPACT_WARNING_MESSAGE: &str = "Heads up: Long threads and multiple compactions can cause the model to be less accurate. Start a new thread when possible to keep threads small and targeted."; | ||
|
|
||
| async fn build_auto_compaction_disabled_codex(server: &MockServer) -> TestCodex { | ||
| let mut model_provider = non_openai_model_provider(server); | ||
| model_provider.stream_max_retries = Some(0); | ||
| test_codex() | ||
| .with_config(move |config| { | ||
| config.model_provider = model_provider; | ||
| set_test_compact_prompt(config); | ||
| config.model_context_window = Some(100); | ||
| config.model_auto_compact_token_limit = Some(90); | ||
| let _ = config.features.disable(Feature::AutoCompaction); | ||
| }) | ||
| .build(server) | ||
| .await | ||
| .expect("build codex") | ||
| } | ||
|
|
||
| async fn submit_context_window_exceeded_turn(codex: &Arc<CodexThread>, text: &str) { | ||
| codex | ||
| .submit(Op::UserInput { | ||
| items: vec![UserInput::Text { | ||
| text: text.to_string(), | ||
| text_elements: Vec::new(), | ||
| }], | ||
| final_output_json_schema: None, | ||
| responsesapi_client_metadata: None, | ||
| additional_context: Default::default(), | ||
| thread_settings: Default::default(), | ||
| }) | ||
| .await | ||
| .expect("submit context window exceeded turn"); | ||
| let error_message = wait_for_event_match(codex, |event| match event { | ||
| EventMsg::Error(err) => Some(err.message.clone()), | ||
| _ => None, | ||
| }) | ||
| .await; | ||
| wait_for_event(codex, |event| matches!(event, EventMsg::TurnComplete(_))).await; | ||
| assert!( | ||
| error_message.contains("ran out of room in the model's context window"), | ||
| "expected context window exceeded message, got {error_message}" | ||
| ); | ||
| } | ||
|
|
||
| fn ev_shell_command_call(call_id: &str, command: &str) -> serde_json::Value { | ||
| ev_function_call( | ||
| call_id, | ||
|
|
@@ -2283,6 +2327,93 @@ async fn pre_sampling_compact_runs_when_comp_hash_changes() { | |
| ); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
| async fn auto_compaction_feature_disabled_skips_comp_hash_model_switch_compaction() { | ||
| skip_if_no_network!(); | ||
|
|
||
| let server = MockServer::start().await; | ||
| let previous_model = "gpt-5.3-codex"; | ||
| let next_model = "gpt-5.2"; | ||
|
|
||
| let models_mock = mount_models_once( | ||
| &server, | ||
| ModelsResponse { | ||
| models: vec![ | ||
| model_info_with_optional_comp_hash(previous_model, Some("hash-a")), | ||
| model_info_with_optional_comp_hash(next_model, Some("hash-b")), | ||
| ], | ||
| }, | ||
| ) | ||
| .await; | ||
| let request_log = mount_sse_sequence( | ||
| &server, | ||
| vec![ | ||
| sse(vec![ | ||
| ev_assistant_message("m1", "before switch"), | ||
| ev_completed_with_tokens("r1", /*total_tokens*/ 100), | ||
| ]), | ||
| sse(vec![ | ||
| ev_assistant_message("m2", "after switch"), | ||
| ev_completed_with_tokens("r2", /*total_tokens*/ 100), | ||
| ]), | ||
| ], | ||
| ) | ||
| .await; | ||
| let model_provider = non_openai_model_provider(&server); | ||
| let mut builder = test_codex() | ||
| .with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing()) | ||
| .with_model(previous_model) | ||
| .with_config(move |config| { | ||
| config.model_provider = model_provider; | ||
| set_test_compact_prompt(config); | ||
| let _ = config.features.disable(Feature::AutoCompaction); | ||
| }); | ||
| let test = builder.build(&server).await.expect("build test codex"); | ||
|
|
||
| test.codex | ||
| .submit(disabled_permission_user_turn( | ||
| "before switch", | ||
| test.cwd.path().to_path_buf(), | ||
| previous_model.to_string(), | ||
| )) | ||
| .await | ||
| .expect("submit first user turn"); | ||
| wait_for_event(&test.codex, |event| { | ||
| matches!(event, EventMsg::TurnComplete(_)) | ||
| }) | ||
| .await; | ||
| test.codex | ||
| .submit(disabled_permission_user_turn( | ||
| "after switch", | ||
| test.cwd.path().to_path_buf(), | ||
| next_model.to_string(), | ||
| )) | ||
| .await | ||
| .expect("submit second user turn"); | ||
| wait_for_event(&test.codex, |event| { | ||
| matches!(event, EventMsg::TurnComplete(_)) | ||
| }) | ||
| .await; | ||
|
|
||
| let requests = request_log.requests(); | ||
| assert_eq!(models_mock.requests().len(), 1); | ||
| assert_eq!( | ||
| requests.len(), | ||
| 2, | ||
| "disabled auto-compaction should skip compaction on a comp-hash model switch" | ||
| ); | ||
| let first = requests[0].body_json(); | ||
| let second = requests[1].body_json(); | ||
| assert_eq!(first["model"].as_str(), Some(previous_model)); | ||
| assert_eq!(second["model"].as_str(), Some(next_model)); | ||
| assert!(second.to_string().contains("before switch")); | ||
| assert!(second.to_string().contains("after switch")); | ||
| assert!( | ||
| !body_contains_text(&second.to_string(), SUMMARIZATION_PROMPT), | ||
| "disabled auto-compaction should preserve history instead of requesting a summary" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
| async fn pre_sampling_compact_skips_when_either_comp_hash_is_missing() { | ||
| skip_if_no_network!(); | ||
|
|
@@ -3671,6 +3802,45 @@ async fn snapshot_request_shape_mid_turn_continuation_compaction() { | |
| ); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
| async fn auto_compaction_feature_disabled_skips_mid_turn_compaction() { | ||
| skip_if_no_network!(); | ||
|
|
||
| let server = start_mock_server().await; | ||
| let over_limit_tokens = 100 * 95 / 100 + 1; | ||
| let first_turn = sse(vec![ | ||
| ev_function_call(DUMMY_CALL_ID, DUMMY_FUNCTION_NAME, "{}"), | ||
| ev_completed_with_tokens("r1", over_limit_tokens), | ||
| ]); | ||
| let request_log = mount_sse_sequence( | ||
| &server, | ||
| vec![ | ||
| first_turn, | ||
| sse_failed( | ||
| "response-failed", | ||
| "context_length_exceeded", | ||
| CONTEXT_LIMIT_MESSAGE, | ||
| ), | ||
| ], | ||
| ) | ||
| .await; | ||
| let test = build_auto_compaction_disabled_codex(&server).await; | ||
|
|
||
| submit_context_window_exceeded_turn(&test.codex, FUNCTION_CALL_LIMIT_MSG).await; | ||
|
|
||
| let requests = request_log.requests(); | ||
| assert_eq!(requests.len(), 2); | ||
| let continuation_request = &requests[1]; | ||
| continuation_request.function_call_output(DUMMY_CALL_ID); | ||
| assert!( | ||
| !body_contains_text( | ||
| &continuation_request.body_json().to_string(), | ||
| SUMMARIZATION_PROMPT | ||
| ), | ||
| "disabled auto-compaction should continue without a compaction request" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
| async fn auto_compact_clamps_config_limit_to_context_window() { | ||
| skip_if_no_network!(); | ||
|
|
@@ -4498,6 +4668,44 @@ async fn snapshot_request_shape_pre_turn_compaction_context_window_exceeded() { | |
| ); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
| async fn auto_compaction_feature_disabled_skips_pre_turn_compaction() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a disabled model-switch/comp-hash case too? |
||
| skip_if_no_network!(); | ||
|
|
||
| let server = start_mock_server().await; | ||
| let first_turn = sse(vec![ | ||
| ev_assistant_message("m1", FIRST_REPLY), | ||
| ev_completed_with_tokens("r1", /*total_tokens*/ 500), | ||
| ]); | ||
| let request_log = mount_sse_sequence( | ||
| &server, | ||
| vec![ | ||
| first_turn, | ||
| sse_failed( | ||
| "response-failed", | ||
| "context_length_exceeded", | ||
| CONTEXT_LIMIT_MESSAGE, | ||
| ), | ||
| ], | ||
| ) | ||
| .await; | ||
| let test = build_auto_compaction_disabled_codex(&server).await; | ||
|
|
||
| test.submit_turn("USER_ONE") | ||
| .await | ||
| .expect("submit first turn"); | ||
| submit_context_window_exceeded_turn(&test.codex, "USER_TWO").await; | ||
|
|
||
| let requests = request_log.requests(); | ||
| assert_eq!(requests.len(), 2); | ||
| let second_request_body = requests[1].body_json().to_string(); | ||
| assert!(second_request_body.contains("USER_TWO")); | ||
| assert!( | ||
| !body_contains_text(&second_request_body, SUMMARIZATION_PROMPT), | ||
| "disabled auto-compaction should sample without a pre-turn compaction request" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test(flavor = "multi_thread", worker_threads = 2)] | ||
| async fn snapshot_request_shape_manual_compact_without_previous_user_messages() { | ||
| skip_if_no_network!(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still lets new_context drop the whole transcript when token_budget is enabled: that tool is exposed independently, and
maybe_start_new_context_windowruns above this gate.Should disabling auto_compaction also hide/reject new_context? Otherwise the SPO escape hatch can discard context instead of reaching the provider error