mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
Route turn and response checkpoints through the rollout budget policy (#62190)
## What changed Move rollout work admission into `RolloutWorkGate` and route compaction deferral through `ThreadBudgetPolicy`. Use `TurnWork` to check the budget when accepting a sampling response and to handle checkpoints after stop hooks, compaction, and sampling errors. Pass sampling charges and response completion through `ResponseWork`. The existing budget check still rejects responses after exhaustion. The new checkpoints leave retry, completion, compaction, and charge handling behavior unchanged. GitOrigin-RevId: 7526416b8ce24ca84035494aeb2a23abd8ca3ddc
This commit is contained in:
committed by
copyberry
parent
1b84166252
commit
8e1fc8e059
@@ -191,4 +191,6 @@ windows-sys = { workspace = true, features = ["Win32_Foundation", "Win32_System_
|
||||
|
||||
[package.metadata.cargo-shear]
|
||||
ignored = ["openssl-sys"]
|
||||
ignored-paths = ["tests/remote_env_windows/*.rs"]
|
||||
ignored-paths = [
|
||||
"tests/remote_env_windows/*.rs",
|
||||
]
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
mod work_gate;
|
||||
use crate::config::RolloutBudgetConfig;
|
||||
use crate::config::rollout_budget_threshold_reached;
|
||||
use codex_protocol::BudgetUnitsSum;
|
||||
@@ -10,26 +11,9 @@ use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::MutexGuard;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct RolloutWorkGate(OnceLock<()>);
|
||||
|
||||
impl RolloutWorkGate {
|
||||
fn stop_budget(&self) {
|
||||
let _ = self.0.set(());
|
||||
}
|
||||
|
||||
fn is_stopped(&self) -> bool {
|
||||
self.0.get().is_some()
|
||||
}
|
||||
|
||||
fn check_start(&self) -> CodexResult<()> {
|
||||
if self.is_stopped() {
|
||||
return Err(CodexErr::SessionBudgetExceeded);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
pub(crate) use work_gate::ResponseWork;
|
||||
pub use work_gate::RolloutWorkGate;
|
||||
pub(crate) use work_gate::TurnWork;
|
||||
|
||||
/// Budget reminder returned by a controller and acknowledged after history insertion.
|
||||
pub struct RolloutBudgetReminder {
|
||||
@@ -67,6 +51,14 @@ impl ThreadBudgetPolicy {
|
||||
pub(crate) fn check_start(&self) -> CodexResult<()> {
|
||||
self.rollout.gate.check_start()
|
||||
}
|
||||
|
||||
pub(crate) fn defer_compaction_start(
|
||||
&self,
|
||||
source: &codex_protocol::protocol::SessionSource,
|
||||
phase: codex_analytics::CompactionPhase,
|
||||
) -> bool {
|
||||
work_gate::defer_compaction_start(self, source, phase)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for RolloutBudget {
|
||||
@@ -223,12 +215,7 @@ impl RolloutBudget {
|
||||
}
|
||||
|
||||
pub(crate) fn can_receive_turn_input(&self, thread: ThreadId, turn: &str) -> bool {
|
||||
let _ = (thread, turn);
|
||||
!self.is_exhausted()
|
||||
}
|
||||
|
||||
pub(crate) fn can_force_root_final(&self) -> bool {
|
||||
false
|
||||
self.gate.can_receive_turn_input(thread, turn)
|
||||
}
|
||||
|
||||
pub(crate) fn is_exhausted(&self) -> bool {
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
//! Admission for new rollout work and checkpoints within a turn.
|
||||
|
||||
use super::ThreadBudgetPolicy;
|
||||
use crate::client_common::SamplingCharge;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use std::ops::ControlFlow;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct RolloutWorkGate(OnceLock<()>);
|
||||
|
||||
impl RolloutWorkGate {
|
||||
pub(super) fn stop_budget(&self) {
|
||||
let _ = self.0.set(());
|
||||
}
|
||||
|
||||
pub(super) fn is_stopped(&self) -> bool {
|
||||
self.0.get().is_some()
|
||||
}
|
||||
|
||||
pub(super) fn check_start(&self) -> Result<()> {
|
||||
if self.is_stopped() {
|
||||
return Err(CodexErr::SessionBudgetExceeded);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn can_receive_turn_input(&self, _thread: ThreadId, _turn: &str) -> bool {
|
||||
!self.is_stopped()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn defer_compaction_start(
|
||||
_policy: &ThreadBudgetPolicy,
|
||||
_source: &SessionSource,
|
||||
_phase: codex_analytics::CompactionPhase,
|
||||
) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
pub(crate) struct WorkCheckpoint;
|
||||
|
||||
impl WorkCheckpoint {
|
||||
pub(crate) fn should_retry(&self, _needs_follow_up: bool) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
pub(crate) fn is_finished(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
pub(crate) fn should_return(&self, _outcome: &codex_hooks::StopOutcome) -> bool {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct TurnWork {
|
||||
policy: ThreadBudgetPolicy,
|
||||
}
|
||||
|
||||
impl TurnWork {
|
||||
pub(crate) fn new(
|
||||
policy: &ThreadBudgetPolicy,
|
||||
_thread: ThreadId,
|
||||
_turn: &str,
|
||||
_source: &SessionSource,
|
||||
) -> Self {
|
||||
Self {
|
||||
policy: policy.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn accept_response(&mut self, _has_final_answer: bool) -> Result<WorkCheckpoint> {
|
||||
self.policy.check_start()?;
|
||||
Ok(WorkCheckpoint)
|
||||
}
|
||||
|
||||
pub(crate) fn assistant_message(
|
||||
&mut self,
|
||||
final_answer: Option<String>,
|
||||
commentary: Option<String>,
|
||||
) -> Option<String> {
|
||||
commentary.or(final_answer)
|
||||
}
|
||||
|
||||
pub(crate) fn after_stop_hook(&self) -> Result<WorkCheckpoint> {
|
||||
Ok(WorkCheckpoint)
|
||||
}
|
||||
|
||||
pub(crate) fn should_finish_after_compaction(&self) -> Result<bool> {
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
pub(crate) fn on_error(
|
||||
&self,
|
||||
error: CodexErr,
|
||||
) -> Result<ControlFlow<WorkCheckpoint, CodexErr>> {
|
||||
Ok(ControlFlow::Continue(error))
|
||||
}
|
||||
|
||||
pub(crate) fn take_answer(&mut self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct ResponseWork {}
|
||||
|
||||
impl ResponseWork {
|
||||
pub(crate) fn accept_charge(&mut self, _charge: &mut SamplingCharge) {}
|
||||
|
||||
pub(crate) fn finish(self, _policy: &ThreadBudgetPolicy) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -89,9 +89,9 @@ impl Session {
|
||||
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()
|
||||
self.services
|
||||
.budget_policy
|
||||
.defer_compaction_start(&turn.session_source, phase)
|
||||
}
|
||||
|
||||
pub(crate) fn finish_compaction(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::ops::ControlFlow;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
@@ -43,6 +44,8 @@ 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::rollout_budget::ResponseWork;
|
||||
use crate::rollout_budget::TurnWork;
|
||||
use crate::session::PreviousTurnSettings;
|
||||
use crate::session::TurnInput;
|
||||
use crate::session::daemon_recovery::RecordedTurnInput;
|
||||
@@ -522,6 +525,13 @@ pub(crate) async fn run_turn(
|
||||
track_turn_resolved_config_analytics(&sess, &first_step_context, &input).await;
|
||||
|
||||
let mut last_agent_message: Option<String> = None;
|
||||
let mut work = TurnWork::new(
|
||||
&sess.services.budget_policy,
|
||||
sess.thread_id(),
|
||||
&turn_context.sub_id,
|
||||
&turn_context.session_source,
|
||||
);
|
||||
let mut completion_hooks = TurnCompletionHooks::default();
|
||||
let mut stop_hook_active = false;
|
||||
// A steer can reopen sampling within the same turn. Keep its aggregate until the turn ends.
|
||||
let turn_diff_tracker = turn_context
|
||||
@@ -665,12 +675,13 @@ pub(crate) async fn run_turn(
|
||||
.await;
|
||||
let sampling_request_result =
|
||||
sampling_request_result.and_then(|(output, notifications)| {
|
||||
sess.check_rollout_budget()?;
|
||||
Ok((output, notifications))
|
||||
let has_final_answer =
|
||||
!output.mailbox_preempted && output.last_agent_message.final_answer.is_some();
|
||||
let next = work.accept_response(has_final_answer)?;
|
||||
Ok((output, notifications, next))
|
||||
});
|
||||
match sampling_request_result {
|
||||
Ok(result) => {
|
||||
let (sampling_request_output, notify_messages) = result;
|
||||
Ok((sampling_request_output, notify_messages, next)) => {
|
||||
guardian_budget_compacted = false;
|
||||
|
||||
let SamplingRequestResult {
|
||||
@@ -682,13 +693,13 @@ pub(crate) async fn run_turn(
|
||||
final_answer,
|
||||
trailing_commentary,
|
||||
} = sampling_request_last_agent_message;
|
||||
let sampling_request_last_agent_message = trailing_commentary.or(final_answer);
|
||||
let hooks = TurnCompletionHooks {
|
||||
let sampling_request_last_agent_message =
|
||||
work.assistant_message(final_answer, trailing_commentary);
|
||||
completion_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(
|
||||
@@ -700,6 +711,14 @@ pub(crate) async fn run_turn(
|
||||
can_drain_pending_input = true;
|
||||
// Process async hooks only after sampling and its tools have finished.
|
||||
drain_async_hook_results(&sess, &turn_context, /*before_user_prompt*/ false).await;
|
||||
if next.should_retry(model_needs_follow_up) {
|
||||
continue;
|
||||
}
|
||||
if next.is_finished() {
|
||||
return completion_hooks
|
||||
.finish(&sess, &step_context, stop_hook_active, work.take_answer())
|
||||
.await;
|
||||
}
|
||||
let (has_pending_input, token_status) = async {
|
||||
// Tool output can add audio without a queued user input. Resolve its
|
||||
// duration before checking the continuation's compaction threshold.
|
||||
@@ -835,6 +854,15 @@ pub(crate) async fn run_turn(
|
||||
let stop_outcome = completion_hooks
|
||||
.run_stop(&sess, &step_context, stop_hook_active)
|
||||
.await;
|
||||
let after_stop = work.after_stop_hook()?;
|
||||
if after_stop.is_finished() {
|
||||
return completion_hooks
|
||||
.finish(&sess, &step_context, stop_hook_active, work.take_answer())
|
||||
.await;
|
||||
}
|
||||
if after_stop.should_return(&stop_outcome) {
|
||||
return Ok(work.take_answer());
|
||||
}
|
||||
if matches!(
|
||||
turn_context.session_source,
|
||||
SessionSource::Internal(InternalSessionSource::MemoryConsolidation)
|
||||
@@ -931,6 +959,9 @@ pub(crate) async fn run_turn(
|
||||
warn!(error = %err, "Post-turn compaction failed; preserving the completed turn");
|
||||
}
|
||||
}
|
||||
if work.should_finish_after_compaction()? {
|
||||
return Ok(work.take_answer());
|
||||
}
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
@@ -994,6 +1025,17 @@ pub(crate) async fn run_turn(
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
let e = match work.on_error(e)? {
|
||||
ControlFlow::Break(next) => {
|
||||
if next.is_finished() {
|
||||
return completion_hooks
|
||||
.finish(&sess, &step_context, stop_hook_active, work.take_answer())
|
||||
.await;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
ControlFlow::Continue(e) => e,
|
||||
};
|
||||
info!("Turn error: {e:#}");
|
||||
if matches!(
|
||||
e.details(),
|
||||
@@ -1069,6 +1111,21 @@ impl TurnCompletionHooks {
|
||||
};
|
||||
run_legacy_after_agent_hook(sess, turn, messages, self.last_assistant_message.clone()).await
|
||||
}
|
||||
async fn finish(
|
||||
mut self,
|
||||
sess: &Arc<Session>,
|
||||
step_context: &Arc<StepContext>,
|
||||
stop_hook_active: bool,
|
||||
answer: Option<String>,
|
||||
) -> CodexResult<Option<String>> {
|
||||
if !self.stop_ran {
|
||||
self.run_stop(sess, step_context, stop_hook_active).await;
|
||||
}
|
||||
if self.run_after_agent(sess, &step_context.turn).await {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(answer)
|
||||
}
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
@@ -3074,6 +3131,7 @@ async fn try_run_sampling_request(
|
||||
== codex_config::config_toml::ResponseProcessMode::Streaming;
|
||||
let mut wait_for_bill_only = false;
|
||||
let mut in_flight = FuturesOrdered::new();
|
||||
let mut response_work = ResponseWork::default();
|
||||
let mut needs_follow_up = false;
|
||||
let mut last_agent_message = AssistantMessages::default();
|
||||
let mut active_item: Option<TurnItem> = None;
|
||||
@@ -3631,6 +3689,8 @@ async fn try_run_sampling_request(
|
||||
usage_metadata.as_ref(),
|
||||
)
|
||||
.await;
|
||||
let mut hook_output = hook_output;
|
||||
response_work.accept_charge(&mut hook_output);
|
||||
let budget_result = sess
|
||||
.record_token_usage_info(
|
||||
&turn_context,
|
||||
@@ -3876,6 +3936,7 @@ async fn try_run_sampling_request(
|
||||
return Err(CodexErr::TurnAborted);
|
||||
}
|
||||
|
||||
response_work.finish(&sess.services.budget_policy)?;
|
||||
outcome
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user