mirror of
https://github.com/openai/codex.git
synced 2026-10-30 19:18:49 +00:00
[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:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user