mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +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:
@@ -1342,6 +1342,14 @@ impl ModelClient {
|
||||
Ok(request)
|
||||
}
|
||||
|
||||
fn check_generation_start(&self, metadata: &CodexResponsesMetadata) -> Result<()> {
|
||||
if let Some(policy) = &self.budget_policy {
|
||||
let _ = metadata;
|
||||
policy.check_start()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prepare_response_items_for_request(&self, input: &mut [ResponseItem]) {
|
||||
for item in input {
|
||||
// A provider can echo a bare reserved prefix or return an opaque ID.
|
||||
@@ -2109,6 +2117,7 @@ impl ResponsesHttpExecutor<'_> {
|
||||
{
|
||||
continue;
|
||||
}
|
||||
self.client.check_generation_start(responses_metadata)?;
|
||||
let interceptors = crate::model_request::prepare(
|
||||
&self.client.request_contributors,
|
||||
&thread_id,
|
||||
@@ -2130,9 +2139,6 @@ impl ResponsesHttpExecutor<'_> {
|
||||
client_setup.api_auth,
|
||||
)
|
||||
.with_telemetry(Some(request_telemetry), Some(sse_telemetry));
|
||||
if let Some(policy) = &self.client.budget_policy {
|
||||
policy.check_start()?;
|
||||
}
|
||||
let stream_result = client
|
||||
.stream_request_with_observer(request, options, |wire| {
|
||||
inference_trace_attempt.record_started(
|
||||
@@ -2441,6 +2447,9 @@ impl ModelClientSession {
|
||||
client_setup.auth.as_ref(),
|
||||
&responses_headers,
|
||||
);
|
||||
if !warmup {
|
||||
self.client.check_generation_start(responses_metadata)?;
|
||||
}
|
||||
let interceptors = crate::model_request::prepare(
|
||||
&self.client.request_contributors,
|
||||
&thread_id,
|
||||
@@ -2487,9 +2496,6 @@ impl ModelClientSession {
|
||||
),
|
||||
],
|
||||
);
|
||||
if !warmup && let Some(policy) = &self.client.budget_policy {
|
||||
policy.check_start()?;
|
||||
}
|
||||
let stream_result = websocket_connection
|
||||
.stream_request_with_observer(
|
||||
ws_request,
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -218,7 +218,7 @@ async fn run_remote_compact_task_inner(
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
let started_item = if candidate.is_none() {
|
||||
let started_item = if candidate.is_none() && !sess.defer_compaction_start(turn_context, phase) {
|
||||
let item = TurnItem::ContextCompaction(ContextCompactionItem::new());
|
||||
sess.emit_turn_item_started(turn_context, &item).await;
|
||||
Some(item)
|
||||
@@ -264,7 +264,7 @@ async fn run_remote_compact_task_inner(
|
||||
attempt
|
||||
.track(sess.as_ref(), status, codex_error, analytics_details)
|
||||
.await;
|
||||
let result = result.and_then(|installed| sess.check_rollout_budget().map(|()| installed));
|
||||
let result = sess.finish_compaction(turn_context, phase, result);
|
||||
match result {
|
||||
Ok(installed) => Ok(installed),
|
||||
Err(err)
|
||||
|
||||
@@ -27,6 +27,7 @@ pub(super) async fn run_compaction(
|
||||
candidate: Option<BackgroundCompactionCandidate>,
|
||||
fallback: CompactionFallback,
|
||||
) -> CodexResult<bool> {
|
||||
let mut installed_prefix = false;
|
||||
if let Some(candidate) = candidate {
|
||||
let installed = build_and_install_compaction(
|
||||
sess,
|
||||
@@ -41,6 +42,7 @@ pub(super) async fn run_compaction(
|
||||
)
|
||||
.await?;
|
||||
if installed {
|
||||
installed_prefix = true;
|
||||
let status = crate::session::context_window::context_window_token_status(
|
||||
sess,
|
||||
&replacement_step_context.turn,
|
||||
@@ -56,7 +58,7 @@ pub(super) async fn run_compaction(
|
||||
return Ok(false);
|
||||
}
|
||||
}
|
||||
build_and_install_compaction(
|
||||
let foreground = build_and_install_compaction(
|
||||
sess,
|
||||
step_context,
|
||||
replacement_step_context,
|
||||
@@ -67,7 +69,11 @@ pub(super) async fn run_compaction(
|
||||
started_item,
|
||||
/*candidate*/ None,
|
||||
)
|
||||
.await
|
||||
.await;
|
||||
match foreground {
|
||||
Ok(installed) => Ok(installed_prefix || installed),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
@@ -104,7 +110,8 @@ async fn build_and_install_compaction(
|
||||
}),
|
||||
turn_context.provider.info().name.as_str(),
|
||||
);
|
||||
if needs_start && candidate.is_none() {
|
||||
let defer_start = sess.defer_compaction_start(turn_context, compaction_metadata.phase());
|
||||
if needs_start && candidate.is_none() && !defer_start {
|
||||
sess.emit_turn_item_started(turn_context, &compaction_item)
|
||||
.await;
|
||||
}
|
||||
@@ -263,6 +270,10 @@ async fn build_and_install_compaction(
|
||||
compaction_model_hash: compaction_turn_context.model_info().comp_hash.clone(),
|
||||
reviewer_compaction_hash,
|
||||
};
|
||||
if needs_start && 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(),
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
mod gate;
|
||||
|
||||
use crate::config::RolloutBudgetConfig;
|
||||
use crate::config::rollout_budget_threshold_reached;
|
||||
use codex_protocol::BudgetUnitsSum;
|
||||
@@ -7,13 +5,32 @@ use codex_protocol::ThreadId;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
pub use gate::RolloutWorkGate;
|
||||
use std::collections::HashMap;
|
||||
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(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Budget reminder returned by a controller and acknowledged after history insertion.
|
||||
pub struct RolloutBudgetReminder {
|
||||
pub remaining_units: f64,
|
||||
@@ -205,6 +222,15 @@ impl RolloutBudget {
|
||||
self.gate.stop_budget();
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
pub(crate) fn is_exhausted(&self) -> bool {
|
||||
self.gate.is_stopped()
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
//! The first stop closes every work-start boundary in this rollout tree.
|
||||
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
/// Controls new work across the rollout tree.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct RolloutWorkGate(OnceLock<Stop>);
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Stop {
|
||||
Budget,
|
||||
}
|
||||
|
||||
impl RolloutWorkGate {
|
||||
pub(super) fn stop_budget(&self) {
|
||||
let _ = self.0.set(Stop::Budget);
|
||||
}
|
||||
|
||||
pub(super) fn is_stopped(&self) -> bool {
|
||||
self.0.get().is_some()
|
||||
}
|
||||
|
||||
pub(super) fn check_start(&self) -> Result<()> {
|
||||
match self.0.get() {
|
||||
None => Ok(()),
|
||||
Some(Stop::Budget) => Err(CodexErr::SessionBudgetExceeded),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -224,9 +224,16 @@ async fn record_stage1_output_usage_for_memory_citation(
|
||||
pub(crate) type InFlightFuture<'f> =
|
||||
Pin<Box<dyn Future<Output = Result<ResponseItemEnvelope>> + Send + 'f>>;
|
||||
|
||||
/// Text the harness actually accepted, together with the phase of that message.
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct AssistantText {
|
||||
pub(crate) text: String,
|
||||
pub(crate) is_final_answer: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct OutputItemResult {
|
||||
pub last_agent_message: Option<String>,
|
||||
pub last_agent_message: Option<AssistantText>,
|
||||
pub needs_follow_up: bool,
|
||||
pub tool_future: Option<InFlightFuture<'static>>,
|
||||
}
|
||||
@@ -269,7 +276,7 @@ pub(crate) struct FinalizedTurnItem {
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct FinalizedTurnItemFacts {
|
||||
pub(crate) memory_citation: Option<MemoryCitation>,
|
||||
pub(crate) last_agent_message: Option<String>,
|
||||
pub(crate) last_agent_message: Option<AssistantText>,
|
||||
pub(crate) defers_mailbox_delivery_to_next_turn: bool,
|
||||
}
|
||||
|
||||
@@ -291,19 +298,16 @@ pub(crate) async fn finalize_non_tool_response_item(
|
||||
codex_protocol::items::AgentMessageContent::Text { text } => text.as_str(),
|
||||
})
|
||||
.collect::<String>();
|
||||
let last_agent_message = if combined.trim().is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(combined)
|
||||
};
|
||||
let defers_mailbox_delivery_to_next_turn =
|
||||
assistant_message_defers_mailbox_delivery(agent_message.phase.as_ref(), || {
|
||||
last_agent_message.is_some()
|
||||
});
|
||||
let has_text = !combined.trim().is_empty();
|
||||
let is_final_answer =
|
||||
assistant_message_is_final(agent_message.phase.as_ref(), || has_text);
|
||||
(
|
||||
agent_message.memory_citation.clone(),
|
||||
last_agent_message,
|
||||
defers_mailbox_delivery_to_next_turn,
|
||||
has_text.then_some(AssistantText {
|
||||
text: combined,
|
||||
is_final_answer,
|
||||
}),
|
||||
is_final_answer,
|
||||
)
|
||||
}
|
||||
_ => (None, None, false),
|
||||
@@ -578,7 +582,7 @@ fn completed_item_defers_mailbox_delivery_to_next_turn(
|
||||
) -> bool {
|
||||
match item {
|
||||
ResponseItem::Message { role, phase, .. } if role == "assistant" => {
|
||||
assistant_message_defers_mailbox_delivery(phase.as_ref(), || {
|
||||
assistant_message_is_final(phase.as_ref(), || {
|
||||
last_assistant_message_from_item(item, plan_mode).is_some()
|
||||
})
|
||||
}
|
||||
@@ -586,7 +590,7 @@ fn completed_item_defers_mailbox_delivery_to_next_turn(
|
||||
}
|
||||
}
|
||||
|
||||
fn assistant_message_defers_mailbox_delivery(
|
||||
fn assistant_message_is_final(
|
||||
phase: Option<&MessagePhase>,
|
||||
has_visible_text: impl FnOnce() -> bool,
|
||||
) -> bool {
|
||||
|
||||
@@ -335,7 +335,7 @@ async fn handle_output_item_done_returns_contributed_last_agent_message() {
|
||||
.expect("assistant message should complete");
|
||||
|
||||
assert_eq!(
|
||||
output.last_agent_message.as_deref(),
|
||||
output.last_agent_message.as_ref().map(|m| m.text.as_str()),
|
||||
Some("contributed assistant text")
|
||||
);
|
||||
}
|
||||
@@ -502,7 +502,11 @@ async fn finalized_turn_item_defers_mailbox_for_contributed_visible_text(plan_mo
|
||||
.expect("assistant message should parse");
|
||||
|
||||
assert_eq!(
|
||||
finalized.facts.last_agent_message.as_deref(),
|
||||
finalized
|
||||
.facts
|
||||
.last_agent_message
|
||||
.as_ref()
|
||||
.map(|m| m.text.as_str()),
|
||||
Some("contributed assistant text")
|
||||
);
|
||||
assert!(finalized.facts.defers_mailbox_delivery_to_next_turn);
|
||||
@@ -530,7 +534,11 @@ async fn finalized_turn_item_keeps_mailbox_open_for_nonterminal_text(phase: Mess
|
||||
.expect("assistant message should parse");
|
||||
|
||||
assert_eq!(
|
||||
finalized.facts.last_agent_message.as_deref(),
|
||||
finalized
|
||||
.facts
|
||||
.last_agent_message
|
||||
.as_ref()
|
||||
.map(|m| m.text.as_str()),
|
||||
Some("contributed assistant text")
|
||||
);
|
||||
assert!(!finalized.facts.defers_mailbox_delivery_to_next_turn);
|
||||
|
||||
Reference in New Issue
Block a user