Clamp auto-compact limit to context window (#11516)

- Clamp auto-compaction to the minimum of configured limit and 90% of
context window
- Add an e2e compact test for clamped behavior
- Update remote compact tests to account for earlier auto-compaction in
setup turns
This commit is contained in:
Ahmed Ibrahim
2026-02-11 17:41:08 -08:00
committed by GitHub
parent 6938150c5e
commit 40de788c4d
3 changed files with 70 additions and 10 deletions

View File

@@ -2553,6 +2553,64 @@ async fn auto_compact_triggers_after_function_call_over_95_percent_usage() {
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn auto_compact_clamps_config_limit_to_context_window() {
skip_if_no_network!();
let server = start_mock_server().await;
let context_window = 100;
let config_limit = 200;
let over_limit_tokens = context_window * 90 / 100 + 1;
let first_turn = sse(vec![
ev_assistant_message("m1", FIRST_REPLY),
ev_completed_with_tokens("r1", over_limit_tokens),
]);
let auto_summary_payload = auto_summary(AUTO_SUMMARY_TEXT);
let auto_compact_turn = sse(vec![
ev_assistant_message("m2", &auto_summary_payload),
ev_completed_with_tokens("r2", 10),
]);
let post_auto_compact_turn = sse(vec![ev_completed_with_tokens("r3", 10)]);
let first_turn_mock = mount_sse_once(&server, first_turn).await;
let auto_compact_mock = mount_sse_once(&server, auto_compact_turn).await;
mount_sse_once(&server, post_auto_compact_turn).await;
let model_provider = non_openai_model_provider(&server);
let mut builder = test_codex().with_config(move |config| {
config.model_provider = model_provider;
set_test_compact_prompt(config);
config.model_context_window = Some(context_window);
config.model_auto_compact_token_limit = Some(config_limit);
});
let codex = builder.build(&server).await.unwrap();
codex.submit_turn("OVER_LIMIT_TURN").await.unwrap();
codex.submit_turn("FOLLOW_UP_AFTER_CLAMP").await.unwrap();
assert!(
first_turn_mock.single_request().input().iter().any(|item| {
item.get("type").and_then(|value| value.as_str()) == Some("message")
&& item
.get("content")
.and_then(|content| content.as_array())
.and_then(|entries| entries.first())
.and_then(|entry| entry.get("text"))
.and_then(|value| value.as_str())
== Some("OVER_LIMIT_TURN")
}),
"first request should contain the over-limit user input"
);
let auto_compact_body = auto_compact_mock.single_request().body_json().to_string();
assert!(
body_contains_text(&auto_compact_body, SUMMARIZATION_PROMPT),
"auto compact should run with the summarization prompt when config limit exceeds context"
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn auto_compact_counts_encrypted_reasoning_before_last_user() {
skip_if_no_network!();

View File

@@ -266,7 +266,6 @@ async fn remote_compact_trims_function_call_history_to_fit_context_window() -> R
responses::ev_shell_command_call(trimmed_call_id, trimmed_command),
responses::ev_completed("trimmed-call-response"),
]),
sse(vec![responses::ev_completed("trimmed-final-response")]),
],
)
.await;
@@ -385,10 +384,6 @@ async fn auto_remote_compact_trims_function_call_history_to_fit_context_window()
"trimmed-final-response",
500_000,
)]),
sse(vec![
responses::ev_assistant_message("post-compact-assistant", "post compact complete"),
responses::ev_completed("post-compact-final-response"),
]),
],
)
.await;