[codex-core] Trim inline compaction coverage [ci changed_files]

- reduce the server-side compaction test matrix to the highest-signal cases
- add comments around the deferred checkpoint rewrite and inline/preflight split

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Cooper Gamble
2026-03-08 21:08:09 -07:00
parent d298fbd6bb
commit 46615cc195
4 changed files with 18 additions and 957 deletions

View File

@@ -3245,6 +3245,9 @@ impl Session {
history_before_turn: &[ResponseItem],
history_at_checkpoint: &[ResponseItem],
) {
// The server emits compaction as a streamed item before the response is fully complete.
// Wait until `response.completed` to rewrite local history so later streamed items from the
// same turn can still be appended in wire order before we collapse the checkpoint.
let current_history = self.clone_history().await;
let replacement_history = build_server_side_compaction_replacement_history(
item.clone(),
@@ -5963,6 +5966,11 @@ fn build_server_side_compaction_replacement_history(
history_at_checkpoint: &[ResponseItem],
current_history: &[ResponseItem],
) -> Vec<ResponseItem> {
// Rebuild the active turn around the compaction checkpoint:
// 1. keep the turn-local items that existed when compaction fired
// 2. replace any prior same-turn compaction summary with the newest one
// 3. re-append items that arrived later in the same streamed response
// 4. reattach ghost snapshots at the end so undo state survives the rewrite
let checkpoint_turn_items = history_at_checkpoint
.strip_prefix(history_before_turn)
.unwrap_or(history_at_checkpoint);
@@ -6130,6 +6138,13 @@ async fn maybe_run_previous_model_inline_compact(
turn_context: &Arc<TurnContext>,
total_usage_tokens: i64,
) -> CodexResult<bool> {
// Keep OpenAI auto-compaction on one path. If inline server-side compaction is eligible for
// the current turn, let the normal pre-turn inline request handle it instead of running the
// older previous-model client-side preflight flow first.
if inline_server_side_compaction_threshold(sess, turn_context).is_some() {
return Ok(false);
}
let Some(previous_turn_settings) = sess.previous_turn_settings().await else {
return Ok(false);
};

View File

@@ -286,56 +286,6 @@ fn assistant_message_stream_parsers_seed_plan_parser_across_added_and_delta_boun
#[test]
fn build_server_side_compaction_replacement_history_keeps_current_turn_inputs() {
let prior_snapshot = ghost_snapshot("ghost-before");
let same_turn_snapshot = ghost_snapshot("ghost-during");
let history_before_turn = vec![user_message("earlier"), prior_snapshot.clone()];
let turn_start_context_items = vec![
developer_message("fresh permissions"),
environment_context_message("/fresh"),
];
let current_turn_user = user_message("current turn");
let current_turn_tool_output = ResponseItem::FunctionCallOutput {
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload::from_text("tool result".to_string()),
};
let current_history = vec![
user_message("earlier"),
prior_snapshot.clone(),
turn_start_context_items[0].clone(),
turn_start_context_items[1].clone(),
current_turn_user.clone(),
current_turn_tool_output.clone(),
same_turn_snapshot.clone(),
];
let compaction_item = ResponseItem::Compaction {
encrypted_content: "INLINE_SUMMARY".to_string(),
};
let replacement_history = build_server_side_compaction_replacement_history(
compaction_item.clone(),
&turn_start_context_items,
&turn_start_context_items,
&history_before_turn,
&current_history,
&current_history,
);
assert_eq!(
replacement_history,
vec![
turn_start_context_items[0].clone(),
turn_start_context_items[1].clone(),
current_turn_user,
current_turn_tool_output,
compaction_item,
prior_snapshot,
same_turn_snapshot,
]
);
}
#[test]
fn build_server_side_compaction_replacement_history_preserves_turn_scoped_injections() {
let prior_snapshot = ghost_snapshot("ghost-before");
let same_turn_snapshot = ghost_snapshot("ghost-during");
let history_before_turn = vec![user_message("earlier"), prior_snapshot.clone()];
@@ -392,163 +342,6 @@ fn build_server_side_compaction_replacement_history_preserves_turn_scoped_inject
);
}
#[test]
fn build_server_side_compaction_replacement_history_replaces_prior_same_turn_summary() {
let prior_snapshot = ghost_snapshot("ghost-before");
let same_turn_snapshot = ghost_snapshot("ghost-during");
let history_before_turn = vec![user_message("earlier"), prior_snapshot.clone()];
let turn_start_context_items = vec![
developer_message("fresh permissions"),
environment_context_message("/fresh"),
];
let current_turn_user = user_message("current turn");
let current_turn_tool_output = ResponseItem::FunctionCallOutput {
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload::from_text("tool result".to_string()),
};
let prior_compaction = ResponseItem::Compaction {
encrypted_content: "INLINE_SUMMARY_1".to_string(),
};
let new_compaction = ResponseItem::Compaction {
encrypted_content: "INLINE_SUMMARY_2".to_string(),
};
let current_history = vec![
turn_start_context_items[0].clone(),
turn_start_context_items[1].clone(),
current_turn_user.clone(),
current_turn_tool_output.clone(),
prior_compaction,
prior_snapshot.clone(),
same_turn_snapshot.clone(),
];
let replacement_history = build_server_side_compaction_replacement_history(
new_compaction.clone(),
&turn_start_context_items,
&turn_start_context_items,
&history_before_turn,
&current_history,
&current_history,
);
assert_eq!(
replacement_history,
vec![
turn_start_context_items[0].clone(),
turn_start_context_items[1].clone(),
current_turn_user,
current_turn_tool_output,
new_compaction,
prior_snapshot,
same_turn_snapshot,
]
);
}
#[test]
fn build_server_side_compaction_replacement_history_replaces_prior_summary_with_empty_history() {
let same_turn_snapshot = ghost_snapshot("ghost-during");
let history_before_turn = Vec::new();
let turn_start_context_items = vec![
developer_message("fresh permissions"),
environment_context_message("/fresh"),
];
let current_turn_user = user_message("current turn");
let current_turn_tool_output = ResponseItem::FunctionCallOutput {
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload::from_text("tool result".to_string()),
};
let prior_compaction = ResponseItem::Compaction {
encrypted_content: "INLINE_SUMMARY_1".to_string(),
};
let new_compaction = ResponseItem::Compaction {
encrypted_content: "INLINE_SUMMARY_2".to_string(),
};
let current_history = vec![
turn_start_context_items[0].clone(),
turn_start_context_items[1].clone(),
prior_compaction,
current_turn_user.clone(),
current_turn_tool_output.clone(),
same_turn_snapshot.clone(),
];
let replacement_history = build_server_side_compaction_replacement_history(
new_compaction.clone(),
&turn_start_context_items,
&turn_start_context_items,
&history_before_turn,
&current_history,
&current_history,
);
assert_eq!(
replacement_history,
vec![
turn_start_context_items[0].clone(),
turn_start_context_items[1].clone(),
current_turn_user,
current_turn_tool_output,
new_compaction,
same_turn_snapshot,
]
);
}
#[test]
fn build_server_side_compaction_replacement_history_reuses_existing_initial_context_once() {
let history_before_turn = vec![user_message("earlier")];
let compaction_initial_context = vec![
developer_message("fresh permissions"),
environment_context_message("/fresh"),
];
let turn_start_context_items = Vec::new();
let current_turn_user = user_message("current turn");
let prior_compaction = ResponseItem::Compaction {
encrypted_content: "INLINE_SUMMARY_1".to_string(),
};
let new_compaction = ResponseItem::Compaction {
encrypted_content: "INLINE_SUMMARY_2".to_string(),
};
let current_turn_tool_output = ResponseItem::FunctionCallOutput {
call_id: "call-1".to_string(),
output: FunctionCallOutputPayload::from_text("tool result".to_string()),
};
let history_at_checkpoint = vec![
compaction_initial_context[0].clone(),
compaction_initial_context[1].clone(),
current_turn_user.clone(),
prior_compaction,
];
let current_history = vec![
history_at_checkpoint[0].clone(),
history_at_checkpoint[1].clone(),
history_at_checkpoint[2].clone(),
history_at_checkpoint[3].clone(),
current_turn_tool_output.clone(),
];
let replacement_history = build_server_side_compaction_replacement_history(
new_compaction.clone(),
&compaction_initial_context,
&turn_start_context_items,
&history_before_turn,
&history_at_checkpoint,
&current_history,
);
assert_eq!(
replacement_history,
vec![
compaction_initial_context[0].clone(),
compaction_initial_context[1].clone(),
current_turn_user,
new_compaction,
current_turn_tool_output,
]
);
}
#[test]
fn build_server_side_compaction_replacement_history_prefers_longer_initial_context_prefix() {
let history_before_turn = vec![user_message("earlier")];
@@ -603,66 +396,6 @@ fn build_server_side_compaction_replacement_history_prefers_longer_initial_conte
);
}
#[test]
fn build_server_side_compaction_replacement_history_keeps_checkpoint_before_post_compaction_items()
{
let prior_snapshot = ghost_snapshot("ghost-before");
let same_turn_snapshot = ghost_snapshot("ghost-during");
let history_before_turn = vec![user_message("earlier"), prior_snapshot.clone()];
let turn_start_context_items = vec![
developer_message("fresh permissions"),
environment_context_message("/fresh"),
];
let current_turn_user = user_message("current turn");
let post_checkpoint_tool_call = ResponseItem::FunctionCall {
id: None,
call_id: "call-1".to_string(),
name: "test_tool".to_string(),
arguments: "{}".to_string(),
};
let history_at_checkpoint = vec![
user_message("earlier"),
prior_snapshot.clone(),
turn_start_context_items[0].clone(),
turn_start_context_items[1].clone(),
current_turn_user.clone(),
];
let current_history = vec![
history_at_checkpoint[0].clone(),
history_at_checkpoint[1].clone(),
history_at_checkpoint[2].clone(),
history_at_checkpoint[3].clone(),
history_at_checkpoint[4].clone(),
post_checkpoint_tool_call.clone(),
same_turn_snapshot.clone(),
];
let compaction_item = ResponseItem::Compaction {
encrypted_content: "INLINE_SUMMARY".to_string(),
};
let replacement_history = build_server_side_compaction_replacement_history(
compaction_item.clone(),
&turn_start_context_items,
&turn_start_context_items,
&history_before_turn,
&history_at_checkpoint,
&current_history,
);
assert_eq!(
replacement_history,
vec![
turn_start_context_items[0].clone(),
turn_start_context_items[1].clone(),
current_turn_user,
compaction_item,
post_checkpoint_tool_call,
prior_snapshot,
same_turn_snapshot,
]
);
}
fn make_mcp_tool(
server_name: &str,
tool_name: &str,

View File

@@ -178,6 +178,9 @@ pub(crate) async fn handle_output_item_done(
Some(TurnItem::ContextCompaction(item)) => item,
_ => ContextCompactionItem::new(),
});
// Preserve the raw wire event immediately, but defer the committed turn-item lifecycle
// until `response.completed` so later streamed output from the same response is not
// reordered around the local checkpoint rewrite.
debug!(
turn_id = %ctx.turn_context.sub_id,
"emitting streamed server-side raw compaction item and buffering committed checkpoint until response.completed"