mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
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:
@@ -77,6 +77,42 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn inline_compaction_allowed(&self, _turn: &TurnContext) -> CodexResult<bool> {
|
||||
self.check_rollout_budget().map(|()| true)
|
||||
}
|
||||
|
||||
/// A final-only root can decline an inline compaction before it sends anything.
|
||||
/// Do not show the client a live compaction item unless a checkpoint will install.
|
||||
pub(crate) fn defer_compaction_start(
|
||||
&self,
|
||||
turn: &TurnContext,
|
||||
phase: codex_analytics::CompactionPhase,
|
||||
) -> bool {
|
||||
!matches!(phase, codex_analytics::CompactionPhase::StandaloneTurn)
|
||||
&& !turn.session_source.is_non_root_agent()
|
||||
&& self.work_budget().can_force_root_final()
|
||||
}
|
||||
|
||||
pub(crate) fn finish_compaction(
|
||||
&self,
|
||||
turn: &TurnContext,
|
||||
phase: codex_analytics::CompactionPhase,
|
||||
result: CodexResult<bool>,
|
||||
) -> CodexResult<bool> {
|
||||
let standalone = matches!(phase, codex_analytics::CompactionPhase::StandaloneTurn);
|
||||
match result {
|
||||
Ok(installed) => {
|
||||
if standalone {
|
||||
self.check_rollout_budget()?;
|
||||
} else {
|
||||
self.inline_compaction_allowed(turn)?;
|
||||
}
|
||||
Ok(installed)
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn record_rollout_budget_usage(
|
||||
&self,
|
||||
turn_id: &str,
|
||||
|
||||
@@ -42,6 +42,7 @@ use crate::responses_metadata::CodexResponsesRequestKind;
|
||||
use crate::responses_retry::ResponsesStreamRequest;
|
||||
use crate::responses_retry::ResponsesStreamRetryState;
|
||||
use crate::responses_retry::handle_response_stream_error;
|
||||
use crate::responses_retry::is_terminal_sampling_outcome;
|
||||
use crate::session::PreviousTurnSettings;
|
||||
use crate::session::TurnInput;
|
||||
use crate::session::daemon_recovery::RecordedTurnInput;
|
||||
@@ -49,6 +50,7 @@ use crate::session::session::Session;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::skills::emit_explicit_skill_invocations;
|
||||
use crate::stream_events_utils::AssistantText;
|
||||
use crate::stream_events_utils::HandleOutputCtx;
|
||||
use crate::stream_events_utils::InFlightFuture;
|
||||
use crate::stream_events_utils::TurnItemContributorPolicy;
|
||||
@@ -541,7 +543,11 @@ pub(crate) async fn run_turn(
|
||||
// Note that pending_input would be something like a message the user
|
||||
// submitted through the UI while the model was running. Though the UI
|
||||
// may support this, the model might not.
|
||||
let pending_input = if can_drain_pending_input && !sess.work_budget().is_exhausted() {
|
||||
let pending_input = if can_drain_pending_input
|
||||
&& sess
|
||||
.work_budget()
|
||||
.can_receive_turn_input(sess.thread_id(), &turn_context.sub_id)
|
||||
{
|
||||
sess.input_queue
|
||||
.get_pending_input(&sess.active_turn)
|
||||
.await
|
||||
@@ -654,20 +660,32 @@ pub(crate) async fn run_turn(
|
||||
.await
|
||||
}
|
||||
.await;
|
||||
// Completed can precede Guardian's accepted-text replay. Preserve the
|
||||
// entire consumed response before deciding whether the turn may continue.
|
||||
let sampling_request_result = sampling_request_result.and_then(|output| {
|
||||
sess.check_rollout_budget()?;
|
||||
Ok(output)
|
||||
});
|
||||
let sampling_request_result =
|
||||
sampling_request_result.and_then(|(output, notifications)| {
|
||||
sess.check_rollout_budget()?;
|
||||
Ok((output, notifications))
|
||||
});
|
||||
match sampling_request_result {
|
||||
Ok((sampling_request_output, notify_messages)) => {
|
||||
Ok(result) => {
|
||||
let (sampling_request_output, notify_messages) = result;
|
||||
guardian_budget_compacted = false;
|
||||
|
||||
let SamplingRequestResult {
|
||||
needs_follow_up: model_needs_follow_up,
|
||||
mailbox_preempted,
|
||||
last_agent_message: sampling_request_last_agent_message,
|
||||
} = sampling_request_output;
|
||||
let AssistantMessages {
|
||||
final_answer,
|
||||
trailing_commentary,
|
||||
} = sampling_request_last_agent_message;
|
||||
let sampling_request_last_agent_message = trailing_commentary.or(final_answer);
|
||||
let hooks = TurnCompletionHooks {
|
||||
last_assistant_message: sampling_request_last_agent_message,
|
||||
notify_messages: Some(notify_messages),
|
||||
stop_ran: false,
|
||||
};
|
||||
let mut completion_hooks = hooks;
|
||||
if model_needs_follow_up {
|
||||
sess.input_queue
|
||||
.accept_mailbox_delivery_for_current_turn(
|
||||
@@ -810,14 +828,10 @@ pub(crate) async fn run_turn(
|
||||
}
|
||||
|
||||
if !needs_follow_up {
|
||||
last_agent_message = sampling_request_last_agent_message;
|
||||
let stop_outcome = run_turn_stop_hooks(
|
||||
&sess,
|
||||
&step_context,
|
||||
stop_hook_active,
|
||||
last_agent_message.clone(),
|
||||
)
|
||||
.await;
|
||||
last_agent_message.clone_from(&completion_hooks.last_assistant_message);
|
||||
let stop_outcome = completion_hooks
|
||||
.run_stop(&sess, &step_context, stop_hook_active)
|
||||
.await;
|
||||
if matches!(
|
||||
turn_context.session_source,
|
||||
SessionSource::Internal(InternalSessionSource::MemoryConsolidation)
|
||||
@@ -860,14 +874,7 @@ pub(crate) async fn run_turn(
|
||||
if stop_outcome.should_stop {
|
||||
break;
|
||||
}
|
||||
if run_legacy_after_agent_hook(
|
||||
&sess,
|
||||
&turn_context,
|
||||
notify_messages,
|
||||
last_agent_message.clone(),
|
||||
)
|
||||
.await
|
||||
{
|
||||
if completion_hooks.run_after_agent(&sess, &turn_context).await {
|
||||
return Ok(None);
|
||||
}
|
||||
// Token-budget resets do not summarize, so preserve their existing rollover
|
||||
@@ -1017,6 +1024,45 @@ pub(crate) async fn run_turn(
|
||||
Ok(last_agent_message)
|
||||
}
|
||||
|
||||
/// Both completion hooks observe the same accepted response, even if the root
|
||||
/// budget stops between them. The turn's returned final answer is chosen separately.
|
||||
#[derive(Default)]
|
||||
struct TurnCompletionHooks {
|
||||
last_assistant_message: Option<String>,
|
||||
notify_messages: Option<Vec<String>>,
|
||||
stop_ran: bool,
|
||||
}
|
||||
|
||||
impl TurnCompletionHooks {
|
||||
async fn run_stop(
|
||||
&mut self,
|
||||
sess: &Arc<Session>,
|
||||
step_context: &Arc<StepContext>,
|
||||
stop_hook_active: bool,
|
||||
) -> codex_hooks::StopOutcome {
|
||||
debug_assert!(!self.stop_ran);
|
||||
let outcome = run_turn_stop_hooks(
|
||||
sess,
|
||||
step_context,
|
||||
stop_hook_active,
|
||||
self.last_assistant_message.clone(),
|
||||
)
|
||||
.await;
|
||||
self.stop_ran = true;
|
||||
if outcome.should_stop {
|
||||
self.notify_messages = None;
|
||||
}
|
||||
outcome
|
||||
}
|
||||
|
||||
async fn run_after_agent(&mut self, sess: &Arc<Session>, turn: &Arc<TurnContext>) -> bool {
|
||||
let Some(messages) = self.notify_messages.take() else {
|
||||
return false;
|
||||
};
|
||||
run_legacy_after_agent_hook(sess, turn, messages, self.last_assistant_message.clone()).await
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
async fn turn_diff_display_roots(step_context: &StepContext) -> Vec<(String, PathUri)> {
|
||||
let mut display_roots = Vec::new();
|
||||
@@ -1695,7 +1741,9 @@ async fn run_auto_compact(
|
||||
candidate: Option<Box<BackgroundCompactionCandidate>>,
|
||||
) -> CodexResult<bool> {
|
||||
let turn_context = &step_context.turn;
|
||||
sess.check_rollout_budget()?;
|
||||
if !sess.inline_compaction_allowed(turn_context)? {
|
||||
return Ok(false);
|
||||
}
|
||||
if matches!(phase, CompactionPhase::PreTurn)
|
||||
&& crate::guardian::is_basic_session_source(&turn_context.session_source)
|
||||
&& !crate::guardian::should_compact_guardian_input(sess)?
|
||||
@@ -1714,9 +1762,10 @@ async fn run_auto_compact(
|
||||
turn.id = %turn_context.sub_id,
|
||||
);
|
||||
if turn_context.config.features.enabled(Feature::TokenBudget) {
|
||||
// Unlike sampling compaction, this reset has no transport admission check.
|
||||
// World-state preparation above may have waited while a peer posted a bill.
|
||||
sess.check_rollout_budget()?;
|
||||
// This reset has no provider send. Recheck after preparing its world state.
|
||||
if !sess.inline_compaction_allowed(turn_context)? {
|
||||
return Ok(false);
|
||||
}
|
||||
// Compaction is the reset request, so force a new context window
|
||||
// instead of consuming a pending `new_context` tool request.
|
||||
crate::compact_token_budget::run_inline_auto_compact_task(
|
||||
@@ -1790,8 +1839,7 @@ async fn run_auto_compact(
|
||||
reason,
|
||||
phase,
|
||||
)
|
||||
.await?;
|
||||
Ok(true)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2050,7 +2098,7 @@ async fn run_sampling_request(
|
||||
return Err(err);
|
||||
}
|
||||
CodexErrorDetails::Fatal(_) => return Err(err),
|
||||
_ if crate::responses_retry::is_terminal_sampling_outcome(&err) => return Err(err),
|
||||
_ if is_terminal_sampling_outcome(&err) => return Err(err),
|
||||
_ => err,
|
||||
},
|
||||
};
|
||||
@@ -2090,7 +2138,7 @@ async fn run_sampling_request(
|
||||
SamplingRequestResult {
|
||||
needs_follow_up: true,
|
||||
mailbox_preempted: draining_for_mailbox,
|
||||
last_agent_message: None,
|
||||
last_agent_message: AssistantMessages::default(),
|
||||
},
|
||||
notify_messages,
|
||||
));
|
||||
@@ -2208,12 +2256,31 @@ pub(super) async fn built_tools(
|
||||
Ok(Arc::new(router))
|
||||
}
|
||||
|
||||
/// Keep the ordinary last message and the last accepted final distinguishable:
|
||||
/// commentary after a final must not replace the answer to a stopped root turn.
|
||||
#[derive(Debug, Default)]
|
||||
struct AssistantMessages {
|
||||
final_answer: Option<String>,
|
||||
trailing_commentary: Option<String>,
|
||||
}
|
||||
|
||||
impl AssistantMessages {
|
||||
fn record(&mut self, message: AssistantText) {
|
||||
if message.is_final_answer {
|
||||
self.final_answer = Some(message.text);
|
||||
self.trailing_commentary = None;
|
||||
} else {
|
||||
self.trailing_commentary = Some(message.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct SamplingRequestResult {
|
||||
needs_follow_up: bool,
|
||||
/// Queued mail interrupted sampling and must be drained before the next request.
|
||||
mailbox_preempted: bool,
|
||||
last_agent_message: Option<String>,
|
||||
last_agent_message: AssistantMessages,
|
||||
}
|
||||
|
||||
/// Ephemeral per-response state for streaming a single proposed plan.
|
||||
@@ -2779,7 +2846,7 @@ async fn handle_assistant_item_done_in_plan_mode(
|
||||
item: &ResponseItem,
|
||||
state: &mut PlanModeStreamState,
|
||||
previously_active_item: Option<&TurnItem>,
|
||||
last_agent_message: &mut Option<String>,
|
||||
last_agent_message: &mut AssistantMessages,
|
||||
moderation: Option<&codex_protocol::protocol::TargetedAnswerModeration>,
|
||||
) -> bool {
|
||||
let turn_context = &step_context.turn;
|
||||
@@ -2824,7 +2891,7 @@ async fn handle_assistant_item_done_in_plan_mode(
|
||||
)
|
||||
.await;
|
||||
if let Some(agent_message) = final_last_agent_message {
|
||||
*last_agent_message = Some(agent_message);
|
||||
last_agent_message.record(agent_message);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -2990,7 +3057,7 @@ async fn try_run_sampling_request(
|
||||
let mut wait_for_bill_only = false;
|
||||
let mut in_flight = FuturesOrdered::new();
|
||||
let mut needs_follow_up = false;
|
||||
let mut last_agent_message: Option<String> = None;
|
||||
let mut last_agent_message = AssistantMessages::default();
|
||||
let mut active_item: Option<TurnItem> = None;
|
||||
let mut active_tool_argument_diff_consumer: Option<(
|
||||
String,
|
||||
@@ -3296,7 +3363,7 @@ async fn try_run_sampling_request(
|
||||
in_flight.push_back(tool);
|
||||
}
|
||||
if let Some(agent_message) = output_result.last_agent_message {
|
||||
last_agent_message = Some(agent_message);
|
||||
last_agent_message.record(agent_message);
|
||||
}
|
||||
needs_follow_up |= output_result.needs_follow_up;
|
||||
// Hosts can keep the current response intact and deliver mail before the next
|
||||
|
||||
@@ -20,6 +20,7 @@ impl TurnItemContributor for RewriteAgentMessageContributor {
|
||||
) -> codex_extension_api::ExtensionFuture<'a, Result<(), String>> {
|
||||
Box::pin(async move {
|
||||
if let TurnItem::AgentMessage(agent_message) = item {
|
||||
agent_message.phase = Some(codex_protocol::models::MessagePhase::Commentary);
|
||||
agent_message.content = vec![AgentMessageContent::Text {
|
||||
text: "plan contributed assistant text".to_string(),
|
||||
}];
|
||||
@@ -74,7 +75,7 @@ async fn plan_mode_uses_contributed_turn_item_for_last_agent_message() {
|
||||
session.services.extensions = Arc::new(builder.build());
|
||||
let turn_store = ExtensionData::new(turn_context.sub_id.clone());
|
||||
let mut state = PlanModeStreamState::new(/*unfinished_plan_id*/ None);
|
||||
let mut last_agent_message = None;
|
||||
let mut last_agent_message = AssistantMessages::default();
|
||||
let item = assistant_output_text("original assistant text");
|
||||
|
||||
let step_context = StepContext::for_test(Arc::new(turn_context));
|
||||
@@ -92,9 +93,10 @@ async fn plan_mode_uses_contributed_turn_item_for_last_agent_message() {
|
||||
|
||||
assert!(handled);
|
||||
assert_eq!(
|
||||
last_agent_message.as_deref(),
|
||||
last_agent_message.trailing_commentary.as_deref(),
|
||||
Some("plan contributed assistant text")
|
||||
);
|
||||
assert_eq!(last_agent_message.final_answer, None);
|
||||
}
|
||||
|
||||
/// A model retry can reuse an unfinished plan. A later completion must use its own ID.
|
||||
|
||||
Reference in New Issue
Block a user