mirror of
https://github.com/openai/codex.git
synced 2026-11-28 23:34:06 +00:00
Add body_after_prefix auto-compact token limit scope (#22870)
## Why `model_auto_compact_token_limit` has only been able to budget the full active context. That makes it hard to set a small "growth since compaction" budget for sessions that preserve a large carried window prefix: the preserved prefix can consume the whole budget and force immediate repeated compaction. This PR adds an opt-in `body_after_prefix` scope so callers can apply `model_auto_compact_token_limit` to sampled output and later growth after the current carried prefix, while still forcing compaction before the full model context window is exhausted. ## What changed - Adds `AutoCompactTokenLimitScope` with the existing `total` behavior as the default and a new `body_after_prefix` mode: [`config_types.rs`](https://github.com/openai/codex/blob/973806b1cb35792555bead994cb3ed94656eb171/codex-rs/protocol/src/config_types.rs#L24-L37). - Threads `model_auto_compact_token_limit_scope` through config loading, `Config`, `core-api`, and app-server v2 schema/TypeScript generation. - Records the first observed input-token count for a `body_after_prefix` compaction window and uses it as the baseline when deciding whether the scoped auto-compaction budget is exhausted: [`turn.rs`](https://github.com/openai/codex/blob/973806b1cb35792555bead994cb3ed94656eb171/codex-rs/core/src/session/turn.rs#L743-L781). - Keeps a hard context-window cap in `body_after_prefix`, so scoped budgeting cannot let the active context overrun the usable window. ## Verification Added compact-suite coverage for the two key behaviors: `body_after_prefix` does not re-compact just because the carried prefix is larger than the scoped budget, and it still compacts when the total active context reaches the configured context window: [`compact.rs`](https://github.com/openai/codex/blob/973806b1cb35792555bead994cb3ed94656eb171/codex-rs/core/tests/suite/compact.rs#L3003-L3128).
This commit is contained in:
1 parent
05e171094d
commit
80fdd4688f
24 files changed
+933
-58
No files matched your search
@@ -7,6 +7,7 @@ use codex_login::CodexAuth;
|
||||
use codex_model_provider_info::ModelProviderInfo;
|
||||
use codex_model_provider_info::built_in_model_providers;
|
||||
use codex_models_manager::bundled_models_response;
|
||||
use codex_protocol::config_types::AutoCompactTokenLimitScope;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
@@ -126,6 +127,22 @@ fn set_test_compact_prompt(config: &mut Config) {
|
||||
config.compact_prompt = Some(SUMMARIZATION_PROMPT.to_string());
|
||||
}
|
||||
|
||||
fn ev_completed_with_usage(id: &str, input_tokens: i64, output_tokens: i64) -> Value {
|
||||
json!({
|
||||
"type": "response.completed",
|
||||
"response": {
|
||||
"id": id,
|
||||
"usage": {
|
||||
"input_tokens": input_tokens,
|
||||
"input_tokens_details": null,
|
||||
"output_tokens": output_tokens,
|
||||
"output_tokens_details": null,
|
||||
"total_tokens": input_tokens + output_tokens
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn body_contains_text(body: &str, text: &str) -> bool {
|
||||
body.contains(&json_fragment(text))
|
||||
}
|
||||
@@ -2074,6 +2091,100 @@ async fn pre_sampling_compact_runs_on_switch_to_smaller_context_model() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn body_after_prefix_model_switch_budget_compacts_with_next_model() {
|
||||
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_context_window(previous_model, /*context_window*/ 273_000),
|
||||
model_info_with_context_window(next_model, /*context_window*/ 125_000),
|
||||
],
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
let request_log = mount_sse_sequence(
|
||||
&server,
|
||||
vec![
|
||||
sse(vec![
|
||||
ev_assistant_message("m1", "before switch"),
|
||||
ev_completed_with_usage("r1", /*input_tokens*/ 100, /*output_tokens*/ 50),
|
||||
]),
|
||||
sse(vec![
|
||||
ev_assistant_message("m2", "BODY_BUDGET_SUMMARY"),
|
||||
ev_completed_with_tokens("r2", /*total_tokens*/ 10),
|
||||
]),
|
||||
sse(vec![
|
||||
ev_assistant_message("m3", "after switch"),
|
||||
ev_completed_with_tokens("r3", /*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.enable(Feature::RemoteModels);
|
||||
config.model_auto_compact_token_limit = Some(20);
|
||||
config.model_auto_compact_token_limit_scope =
|
||||
AutoCompactTokenLimitScope::BodyAfterPrefix;
|
||||
});
|
||||
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");
|
||||
assert_compaction_uses_turn_lifecycle_id(&test.codex).await;
|
||||
|
||||
let requests = request_log.requests();
|
||||
assert_eq!(models_mock.requests().len(), 1);
|
||||
assert_eq!(
|
||||
requests.len(),
|
||||
3,
|
||||
"expected user, compact, and follow-up requests"
|
||||
);
|
||||
assert_eq!(
|
||||
requests[0].body_json()["model"].as_str(),
|
||||
Some(previous_model)
|
||||
);
|
||||
assert_eq!(requests[1].body_json()["model"].as_str(), Some(next_model));
|
||||
assert_eq!(requests[2].body_json()["model"].as_str(), Some(next_model));
|
||||
assert!(
|
||||
body_contains_text(&requests[1].body_json().to_string(), SUMMARIZATION_PROMPT),
|
||||
"body-budget compaction request should include summarization prompt"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn pre_sampling_compact_runs_after_resume_and_switch_to_smaller_model() {
|
||||
skip_if_no_network!();
|
||||
@@ -3011,6 +3122,237 @@ async fn auto_compact_clamps_config_limit_to_context_window() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn auto_compact_body_after_prefix_ignores_starting_window_prefix() {
|
||||
skip_if_no_network!();
|
||||
|
||||
let server = start_mock_server().await;
|
||||
|
||||
let first_turn = sse(vec![
|
||||
ev_assistant_message("m1", FIRST_REPLY),
|
||||
ev_completed_with_usage("r1", /*input_tokens*/ 600, /*output_tokens*/ 50),
|
||||
]);
|
||||
let second_turn = sse(vec![
|
||||
ev_assistant_message("m2", SECOND_LARGE_REPLY),
|
||||
ev_completed_with_usage("r2", /*input_tokens*/ 700, /*output_tokens*/ 50),
|
||||
]);
|
||||
let auto_compact_turn = sse(vec![
|
||||
ev_assistant_message("m3", AUTO_SUMMARY_TEXT),
|
||||
ev_completed_with_tokens("r3", /*total_tokens*/ 20),
|
||||
]);
|
||||
let third_turn = sse(vec![
|
||||
ev_assistant_message("m4", FINAL_REPLY),
|
||||
ev_completed_with_usage("r4", /*input_tokens*/ 750, /*output_tokens*/ 20),
|
||||
]);
|
||||
let request_log = mount_sse_sequence(
|
||||
&server,
|
||||
vec![first_turn, second_turn, auto_compact_turn, third_turn],
|
||||
)
|
||||
.await;
|
||||
|
||||
let model_provider = non_openai_model_provider(&server);
|
||||
let test = test_codex()
|
||||
.with_config(move |config| {
|
||||
config.model_provider = model_provider;
|
||||
set_test_compact_prompt(config);
|
||||
config.model_context_window = Some(1_000);
|
||||
config.model_auto_compact_token_limit = Some(100);
|
||||
config.model_auto_compact_token_limit_scope =
|
||||
AutoCompactTokenLimitScope::BodyAfterPrefix;
|
||||
})
|
||||
.build(&server)
|
||||
.await
|
||||
.expect("build codex");
|
||||
|
||||
for user in ["PREFIX_FREE_ONE", "PREFIX_FREE_TWO"] {
|
||||
test.submit_turn(user).await.expect("submit turn");
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
request_log.requests().len(),
|
||||
2,
|
||||
"the first two turns should not compact just because the prefix exceeds the body budget"
|
||||
);
|
||||
|
||||
test.submit_turn("PREFIX_FREE_THREE")
|
||||
.await
|
||||
.expect("submit third turn");
|
||||
|
||||
let requests = request_log.requests();
|
||||
assert_eq!(
|
||||
requests.len(),
|
||||
4,
|
||||
"third turn should include pre-turn compaction plus the post-compaction request"
|
||||
);
|
||||
let compact_body = requests[2].body_json().to_string();
|
||||
assert!(
|
||||
body_contains_text(&compact_body, SUMMARIZATION_PROMPT),
|
||||
"body-after-prefix mode should compact once tokens after the first assistant sample exceed the configured budget"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn auto_compact_body_after_prefix_counts_growth_after_compaction() {
|
||||
skip_if_no_network!();
|
||||
|
||||
let server = start_mock_server().await;
|
||||
|
||||
let first_turn = sse(vec![
|
||||
ev_assistant_message("m1", FIRST_REPLY),
|
||||
ev_completed_with_usage("r1", /*input_tokens*/ 100, /*output_tokens*/ 50),
|
||||
]);
|
||||
let first_auto_compact_turn = sse(vec![
|
||||
ev_assistant_message("m2", AUTO_SUMMARY_TEXT),
|
||||
ev_completed_with_tokens("r2", /*total_tokens*/ 20),
|
||||
]);
|
||||
let second_turn = sse(vec![
|
||||
ev_assistant_message("m3", SECOND_LARGE_REPLY),
|
||||
ev_completed_with_usage(
|
||||
"r3", /*input_tokens*/ 100_000, /*output_tokens*/ 10,
|
||||
),
|
||||
]);
|
||||
let third_turn = sse(vec![
|
||||
ev_assistant_message("m4", FINAL_REPLY),
|
||||
ev_completed_with_usage(
|
||||
"r4", /*input_tokens*/ 100_100, /*output_tokens*/ 5,
|
||||
),
|
||||
]);
|
||||
let second_auto_compact_turn = sse(vec![
|
||||
ev_assistant_message("m5", AUTO_SUMMARY_TEXT),
|
||||
ev_completed_with_tokens("r5", /*total_tokens*/ 20),
|
||||
]);
|
||||
let fourth_turn = sse(vec![
|
||||
ev_assistant_message("m6", FINAL_REPLY),
|
||||
ev_completed_with_usage("r6", /*input_tokens*/ 80, /*output_tokens*/ 5),
|
||||
]);
|
||||
let request_log = mount_sse_sequence(
|
||||
&server,
|
||||
vec![
|
||||
first_turn,
|
||||
first_auto_compact_turn,
|
||||
second_turn,
|
||||
third_turn,
|
||||
second_auto_compact_turn,
|
||||
fourth_turn,
|
||||
],
|
||||
)
|
||||
.await;
|
||||
|
||||
let model_provider = non_openai_model_provider(&server);
|
||||
let test = test_codex()
|
||||
.with_config(move |config| {
|
||||
config.model_provider = model_provider;
|
||||
set_test_compact_prompt(config);
|
||||
config.model_context_window = Some(200_000);
|
||||
config.model_auto_compact_token_limit = Some(40);
|
||||
config.model_auto_compact_token_limit_scope =
|
||||
AutoCompactTokenLimitScope::BodyAfterPrefix;
|
||||
})
|
||||
.build(&server)
|
||||
.await
|
||||
.expect("build codex");
|
||||
|
||||
test.submit_turn("WINDOW_PREFIX")
|
||||
.await
|
||||
.expect("submit first turn");
|
||||
test.submit_turn("GROWTH_AFTER_COMPACT")
|
||||
.await
|
||||
.expect("submit second turn");
|
||||
|
||||
let requests = request_log.requests();
|
||||
assert_eq!(
|
||||
requests.len(),
|
||||
3,
|
||||
"second turn should compact first and then sample the new growth"
|
||||
);
|
||||
|
||||
test.submit_turn("AFTER_GROWTH")
|
||||
.await
|
||||
.expect("submit third turn");
|
||||
|
||||
let requests = request_log.requests();
|
||||
assert_eq!(
|
||||
requests.len(),
|
||||
4,
|
||||
"the first server-observed input in the new window should become the prefill baseline"
|
||||
);
|
||||
|
||||
test.submit_turn("AFTER_GROWTH_TRIGGER")
|
||||
.await
|
||||
.expect("submit fourth turn");
|
||||
|
||||
let requests = request_log.requests();
|
||||
assert_eq!(
|
||||
requests.len(),
|
||||
6,
|
||||
"fourth turn should compact because later post-compaction growth counted against the body budget"
|
||||
);
|
||||
let compact_body = requests[4].body_json().to_string();
|
||||
assert!(
|
||||
body_contains_text(&compact_body, SUMMARIZATION_PROMPT),
|
||||
"post-compaction growth should trigger a second body-after-prefix compaction"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn auto_compact_body_after_prefix_still_caps_at_context_window() {
|
||||
skip_if_no_network!();
|
||||
|
||||
let server = start_mock_server().await;
|
||||
|
||||
let first_turn = sse(vec![
|
||||
ev_assistant_message("m1", FIRST_REPLY),
|
||||
ev_completed_with_usage("r1", /*input_tokens*/ 80, /*output_tokens*/ 5),
|
||||
]);
|
||||
let second_turn = sse(vec![
|
||||
ev_assistant_message("m2", SECOND_LARGE_REPLY),
|
||||
ev_completed_with_usage("r2", /*input_tokens*/ 98, /*output_tokens*/ 1),
|
||||
]);
|
||||
let auto_compact_turn = sse(vec![
|
||||
ev_assistant_message("m3", AUTO_SUMMARY_TEXT),
|
||||
ev_completed_with_tokens("r3", /*total_tokens*/ 20),
|
||||
]);
|
||||
let third_turn = sse(vec![
|
||||
ev_assistant_message("m4", FINAL_REPLY),
|
||||
ev_completed_with_usage("r4", /*input_tokens*/ 80, /*output_tokens*/ 5),
|
||||
]);
|
||||
let request_log = mount_sse_sequence(
|
||||
&server,
|
||||
vec![first_turn, second_turn, auto_compact_turn, third_turn],
|
||||
)
|
||||
.await;
|
||||
|
||||
let model_provider = non_openai_model_provider(&server);
|
||||
let test = 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(200);
|
||||
config.model_auto_compact_token_limit_scope =
|
||||
AutoCompactTokenLimitScope::BodyAfterPrefix;
|
||||
})
|
||||
.build(&server)
|
||||
.await
|
||||
.expect("build codex");
|
||||
|
||||
for user in ["CONTEXT_CAP_ONE", "CONTEXT_CAP_TWO", "CONTEXT_CAP_THREE"] {
|
||||
test.submit_turn(user).await.expect("submit turn");
|
||||
}
|
||||
|
||||
let requests = request_log.requests();
|
||||
assert_eq!(
|
||||
requests.len(),
|
||||
4,
|
||||
"third turn should compact before sampling because total context hit the usable window"
|
||||
);
|
||||
let compact_body = requests[2].body_json().to_string();
|
||||
assert!(
|
||||
body_contains_text(&compact_body, SUMMARIZATION_PROMPT),
|
||||
"body-after-prefix mode should still clamp the total threshold to the usable context window"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn auto_compact_counts_encrypted_reasoning_before_last_user() {
|
||||
skip_if_no_network!();
|
||||
|
||||
Reference in new issue
Block a user