Refactor rollout budget checks and turn completion state (#61583)

## What changed

- Centralize generation admission checks before request contributors run for HTTP and non-warmup WebSocket requests.
- Route pending input and inline compaction through dedicated budget checks, and centralize post-compaction budget handling.
- Preserve whether any checkpoint was installed when remote compaction combines a background candidate with foreground compaction.
- Track accepted final answers separately from trailing commentary and encapsulate the response state shared by stop and after-agent hooks.

## Testing

Update the plan-mode contributor test to verify that contributed commentary is tracked separately from final answers, and adapt stream-event assertions to the phase-aware assistant text representation.

GitOrigin-RevId: 43b663e92bfd24ec7e5a0778b711938f16a7fea4
This commit is contained in:
tongzhou wang
2026-11-15 00:03:59 +00:00
committed by copyberry
parent ce82e38b16
commit eaa25e1f43
11 changed files with 246 additions and 110 deletions
+15 -8
View File
@@ -127,7 +127,7 @@ pub(crate) async fn run_inline_auto_compact_task(
world_state: Arc<WorldState>,
reason: CompactionReason,
phase: CompactionPhase,
) -> CodexResult<()> {
) -> CodexResult<bool> {
let prompt = step_context
.turn
.config
@@ -154,8 +154,7 @@ pub(crate) async fn run_inline_auto_compact_task(
phase,
),
)
.await?;
Ok(())
.await
}
pub(crate) async fn run_compact_task(
@@ -188,7 +187,7 @@ async fn run_compact_task_inner(
input: Vec<UserInput>,
world_state: Arc<WorldState>,
compaction_metadata: CompactionTurnMetadata,
) -> CodexResult<()> {
) -> CodexResult<bool> {
let turn_context = Arc::clone(&step_context.turn);
let trigger = compaction_metadata.trigger();
let reason = compaction_metadata.reason();
@@ -251,8 +250,9 @@ async fn run_compact_task_inner(
CompactionAnalyticsDetails::default(),
)
.await;
// Keep an installed checkpoint and its analytics; stop before continuing the turn.
let result = result.and_then(|_| sess.check_rollout_budget());
// Keep an installed checkpoint and its analytics. An inline stop hands control
// back to the root if the deadline permits its final answer.
let result = sess.finish_compaction(&turn_context, phase, result.map(|_| true));
if let Err(err) = &result
&& !matches!(phase, CompactionPhase::PostTurn)
&& !matches!(err.details(), CodexErrorDetails::Interrupted)
@@ -281,8 +281,11 @@ async fn run_compact_task_inner_impl(
codex_hooks::SamplingKind::ForegroundCompaction,
);
let compaction_item = TurnItem::ContextCompaction(ContextCompactionItem::new());
sess.emit_turn_item_started(&turn_context, &compaction_item)
.await;
let defer_start = sess.defer_compaction_start(&turn_context, compaction_metadata.phase());
if !defer_start {
sess.emit_turn_item_started(&turn_context, &compaction_item)
.await;
}
let initial_input_for_turn: ResponseInputItem = ResponseInputItem::from(input);
let mut history = sess.clone_history().await;
@@ -449,6 +452,10 @@ async fn run_compact_task_inner_impl(
let (prefix, context) = split_prefix_updates(updates);
let new_history =
assemble_compaction_history(new_history, prefix, merge_world_state_updates(context));
if defer_start {
sess.emit_turn_item_started(&turn_context, &compaction_item)
.await;
}
sess.replace_compacted_history(
new_history,
replacement_step_context.to_turn_context_item(),