[codex-core] Fix inline compaction event ordering and token accounting [ci changed_files]

Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
Cooper Gamble
2026-03-11 02:48:35 +00:00
co-authored by Codex
parent ab9a474c86
commit 29ddeb71ad
3 changed files with 208 additions and 11 deletions
+5 -6
View File
@@ -7368,14 +7368,12 @@ async fn try_run_sampling_request(
&mut assistant_message_stream_parsers,
)
.await;
let mut applied_server_side_compaction_checkpoint = false;
if let Some(PendingServerSideCompactionCheckpoint {
history_at_checkpoint,
item,
turn_item,
}) = pending_server_side_compaction_checkpoint.take()
{
let turn_item = TurnItem::ContextCompaction(turn_item);
sess.emit_turn_item_started(&turn_context, &turn_item).await;
sess.apply_server_side_compaction_checkpoint(
turn_context.as_ref(),
item,
@@ -7385,11 +7383,12 @@ async fn try_run_sampling_request(
history_at_checkpoint.as_slice(),
)
.await;
sess.emit_turn_item_completed(&turn_context, turn_item)
applied_server_side_compaction_checkpoint = true;
}
if !applied_server_side_compaction_checkpoint {
sess.update_token_usage_info(&turn_context, token_usage.as_ref())
.await;
}
sess.update_token_usage_info(&turn_context, token_usage.as_ref())
.await;
should_emit_turn_diff = true;
needs_follow_up |= sess.has_pending_input().await;
+9 -5
View File
@@ -152,7 +152,6 @@ pub(crate) struct OutputItemResult {
pub(crate) struct PendingServerSideCompactionCheckpoint {
pub history_at_checkpoint: Vec<ResponseItem>,
pub item: ResponseItem,
pub turn_item: ContextCompactionItem,
}
pub(crate) struct HandleOutputCtx {
@@ -172,18 +171,23 @@ pub(crate) async fn handle_output_item_done(
let plan_mode = ctx.turn_context.collaboration_mode.mode == ModeKind::Plan;
if matches!(item, ResponseItem::Compaction { .. }) {
let compaction_item = match previously_active_item {
let turn_item = TurnItem::ContextCompaction(match previously_active_item {
Some(TurnItem::ContextCompaction(item)) => item,
_ => ContextCompactionItem::new(),
};
});
debug!(
turn_id = %ctx.turn_context.sub_id,
"buffering streamed server-side compaction item until response.completed"
"emitting streamed server-side compaction item and buffering history rewrite until response.completed"
);
ctx.sess
.emit_turn_item_started(&ctx.turn_context, &turn_item)
.await;
ctx.sess
.emit_turn_item_completed(&ctx.turn_context, turn_item)
.await;
output.pending_server_side_compaction = Some(PendingServerSideCompactionCheckpoint {
history_at_checkpoint: ctx.sess.clone_history().await.raw_items().to_vec(),
item,
turn_item: compaction_item,
});
return Ok(output);
}