mirror of
https://github.com/openai/codex.git
synced 2026-11-26 07:26:11 +00:00
Block goals after three empty automatic continuation turns (#44320)
## Why Automatic goal continuations can repeatedly return empty final answers without making progress. Stop this loop by marking the goal as `blocked` after three consecutive empty turns with no other activity. ## What changed - Observe completed turn items through a new `on_item_completed` lifecycle hook. - Track empty final answers only for automatically admitted goal turns, resetting the streak on activity, user turns, or goal changes. - Preserve normal turn completion and streamed message deltas when blocking a goal. ## Testing Add accounting coverage for the three-turn threshold and streak resets, plus app-server tests for empty continuations and recovery through final-answer text, commentary, or tool activity. GitOrigin-RevId: 4b9d2cb2e306b0adc316972429cc35000115b88b
This commit is contained in:
1 parent
eb680c0558
commit
0735c51978
8 files changed
+331
-2
No files matched your search
@@ -1,6 +1,9 @@
|
||||
use codex_extension_api::ToolCallOutcome;
|
||||
use codex_extension_api::ToolName;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::items::AgentMessageContent;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::models::MessagePhase;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
use codex_state::ThreadGoalStatus;
|
||||
use std::collections::HashMap;
|
||||
@@ -28,6 +31,8 @@ struct GoalAccountingInner {
|
||||
budget_limit_reported_goal_id: Option<String>,
|
||||
execution_failure_goal_id: Option<String>,
|
||||
consecutive_execution_failure_turns: u8,
|
||||
automatic_goal_turn_id: Option<String>,
|
||||
consecutive_empty_turns: u8,
|
||||
last_accounted_descendant_token_usage: i64,
|
||||
}
|
||||
|
||||
@@ -39,6 +44,8 @@ struct GoalTurnAccounting {
|
||||
account_tokens: bool,
|
||||
failed_execution: bool,
|
||||
successful_tool: bool,
|
||||
empty_final: bool,
|
||||
has_activity: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -106,9 +113,11 @@ impl GoalAccountingState {
|
||||
outcome: ToolCallOutcome,
|
||||
) {
|
||||
let mut inner = self.inner();
|
||||
inner.consecutive_empty_turns = 0;
|
||||
let Some(turn) = inner.turns.get_mut(turn_id) else {
|
||||
return;
|
||||
};
|
||||
turn.has_activity = true;
|
||||
if turn.active_goal_id.is_none() {
|
||||
return;
|
||||
}
|
||||
@@ -151,6 +160,75 @@ impl GoalAccountingState {
|
||||
(inner.consecutive_execution_failure_turns >= 3).then_some(goal_id)
|
||||
}
|
||||
|
||||
pub(crate) fn record_item(&self, turn_id: &str, item: &TurnItem) {
|
||||
let mut inner = self.inner();
|
||||
let Some(turn) = inner.turns.get_mut(turn_id) else {
|
||||
return;
|
||||
};
|
||||
match item {
|
||||
TurnItem::AgentMessage(message) => {
|
||||
let has_text = message.content.iter().any(|content| match content {
|
||||
AgentMessageContent::Text { text } => !text.trim().is_empty(),
|
||||
});
|
||||
turn.has_activity |= has_text || message.questions.is_some();
|
||||
turn.empty_final |=
|
||||
!has_text && !matches!(message.phase, Some(MessagePhase::Commentary));
|
||||
}
|
||||
TurnItem::Reasoning(reasoning) => {
|
||||
turn.has_activity |= reasoning
|
||||
.summary_text
|
||||
.iter()
|
||||
.any(|text| !text.trim().is_empty());
|
||||
}
|
||||
TurnItem::UserMessage(_)
|
||||
| TurnItem::FunctionCallOutput(_)
|
||||
| TurnItem::HookPrompt(_)
|
||||
| TurnItem::Plan(_)
|
||||
| TurnItem::CommandExecution(_)
|
||||
| TurnItem::DynamicToolCall(_)
|
||||
| TurnItem::CollabAgentToolCall(_)
|
||||
| TurnItem::SubAgentActivity(_)
|
||||
| TurnItem::WebSearch(_)
|
||||
| TurnItem::ImageView(_)
|
||||
| TurnItem::Extension(_)
|
||||
| TurnItem::ImageGeneration(_)
|
||||
| TurnItem::EnteredReviewMode(_)
|
||||
| TurnItem::ExitedReviewMode(_)
|
||||
| TurnItem::FileChange(_)
|
||||
| TurnItem::McpToolCall(_)
|
||||
| TurnItem::ContextCompaction(_) => turn.has_activity = true,
|
||||
}
|
||||
if turn.has_activity {
|
||||
inner.consecutive_empty_turns = 0;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn mark_goal_continuation(&self, turn_id: String) {
|
||||
self.inner().automatic_goal_turn_id = Some(turn_id);
|
||||
}
|
||||
|
||||
pub(crate) fn reset_empty_responses(&self) {
|
||||
let mut inner = self.inner();
|
||||
inner.automatic_goal_turn_id = None;
|
||||
inner.consecutive_empty_turns = 0;
|
||||
}
|
||||
|
||||
/// Evaluated under the goal-state permit after automatic admission records its turn ID.
|
||||
pub(crate) fn empty_response_goal(&self, turn_id: &str) -> Option<String> {
|
||||
let mut inner = self.inner();
|
||||
let automatic = inner.automatic_goal_turn_id.as_deref() == Some(turn_id);
|
||||
let turn = inner.turns.get_mut(turn_id)?;
|
||||
let goal_id = turn.active_goal_id.clone()?;
|
||||
let empty = automatic && turn.empty_final && !turn.has_activity;
|
||||
turn.empty_final = false;
|
||||
if !empty {
|
||||
inner.consecutive_empty_turns = 0;
|
||||
return None;
|
||||
}
|
||||
inner.consecutive_empty_turns = inner.consecutive_empty_turns.saturating_add(1);
|
||||
(inner.consecutive_empty_turns >= 3).then_some(goal_id)
|
||||
}
|
||||
|
||||
/// Acquires the per-thread progress-accounting permit.
|
||||
///
|
||||
/// Hold the returned permit from before taking a progress snapshot until after the persistent
|
||||
@@ -215,6 +293,7 @@ impl GoalAccountingState {
|
||||
turn.active_goal_id = Some(goal_id.clone());
|
||||
if inner.current_turn_id.as_deref() == Some(turn_id) {
|
||||
if inner.wall_clock.active_goal_id.as_deref() != Some(goal_id.as_str()) {
|
||||
inner.consecutive_empty_turns = 0;
|
||||
inner.last_accounted_descendant_token_usage =
|
||||
self.descendant_token_usage.load(Ordering::Relaxed);
|
||||
}
|
||||
@@ -242,6 +321,8 @@ impl GoalAccountingState {
|
||||
turn.active_goal_id = Some(goal_id.clone());
|
||||
if goal_changed {
|
||||
turn.reset_baseline_to_current();
|
||||
inner.automatic_goal_turn_id = None;
|
||||
inner.consecutive_empty_turns = 0;
|
||||
inner.last_accounted_descendant_token_usage =
|
||||
self.descendant_token_usage.load(Ordering::Relaxed);
|
||||
}
|
||||
@@ -256,6 +337,7 @@ impl GoalAccountingState {
|
||||
inner.budget_limit_reported_goal_id = None;
|
||||
}
|
||||
if inner.wall_clock.active_goal_id.as_deref() != Some(goal_id.as_str()) {
|
||||
inner.consecutive_empty_turns = 0;
|
||||
inner.last_accounted_descendant_token_usage =
|
||||
self.descendant_token_usage.load(Ordering::Relaxed);
|
||||
}
|
||||
@@ -272,6 +354,8 @@ impl GoalAccountingState {
|
||||
inner.budget_limit_reported_goal_id = None;
|
||||
inner.execution_failure_goal_id = None;
|
||||
inner.consecutive_execution_failure_turns = 0;
|
||||
inner.automatic_goal_turn_id = None;
|
||||
inner.consecutive_empty_turns = 0;
|
||||
Some(turn_id)
|
||||
}
|
||||
|
||||
@@ -286,6 +370,8 @@ impl GoalAccountingState {
|
||||
inner.budget_limit_reported_goal_id = None;
|
||||
inner.execution_failure_goal_id = None;
|
||||
inner.consecutive_execution_failure_turns = 0;
|
||||
inner.automatic_goal_turn_id = None;
|
||||
inner.consecutive_empty_turns = 0;
|
||||
}
|
||||
|
||||
pub(crate) fn progress_snapshot(&self, turn_id: &str) -> Option<GoalProgressSnapshot> {
|
||||
@@ -454,6 +540,8 @@ impl Default for GoalAccountingInner {
|
||||
budget_limit_reported_goal_id: None,
|
||||
execution_failure_goal_id: None,
|
||||
consecutive_execution_failure_turns: 0,
|
||||
automatic_goal_turn_id: None,
|
||||
consecutive_empty_turns: 0,
|
||||
last_accounted_descendant_token_usage: 0,
|
||||
}
|
||||
}
|
||||
@@ -479,6 +567,8 @@ impl GoalTurnAccounting {
|
||||
account_tokens,
|
||||
failed_execution: false,
|
||||
successful_tool: false,
|
||||
empty_final: false,
|
||||
has_activity: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ use codex_extension_api::TurnStartInput;
|
||||
use codex_extension_api::TurnStopInput;
|
||||
use codex_otel::MetricsClient;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::items::TurnItem;
|
||||
use codex_protocol::protocol::CodexErrorInfo;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::SubAgentSource;
|
||||
@@ -271,6 +272,23 @@ where
|
||||
})
|
||||
}
|
||||
|
||||
fn on_item_completed<'a>(
|
||||
&'a self,
|
||||
thread_store: &'a ExtensionData,
|
||||
turn_store: &'a ExtensionData,
|
||||
item: &'a TurnItem,
|
||||
) -> ExtensionFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
if let Some(runtime) = goal_runtime_handle(thread_store)
|
||||
&& runtime.is_enabled()
|
||||
{
|
||||
runtime
|
||||
.accounting_state()
|
||||
.record_item(turn_store.level_id(), item);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn on_turn_stop<'a>(&'a self, input: TurnStopInput<'a>) -> ExtensionFuture<'a, ()> {
|
||||
Box::pin(async move {
|
||||
let Some(runtime) = goal_runtime_handle(input.thread_store) else {
|
||||
@@ -296,6 +314,14 @@ where
|
||||
);
|
||||
return;
|
||||
}
|
||||
if let Err(err) = runtime
|
||||
.stop_active_goal_for_turn(turn_id, ActiveGoalStopReason::EmptyResponse)
|
||||
.await
|
||||
{
|
||||
input.thread_store.remove::<TurnStartOptions>();
|
||||
tracing::warn!("failed to stop goal after empty responses for {turn_id}: {err}");
|
||||
return;
|
||||
}
|
||||
if let Err(err) = runtime
|
||||
.account_active_goal_progress(
|
||||
turn_id,
|
||||
@@ -334,6 +360,7 @@ where
|
||||
let Some(runtime) = goal_runtime_handle(input.thread_store) else {
|
||||
return;
|
||||
};
|
||||
runtime.accounting_state().reset_empty_responses();
|
||||
if !runtime.is_enabled() {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ pub(crate) enum ActiveGoalStopReason {
|
||||
TurnError,
|
||||
UsageLimit,
|
||||
ExecutionUnavailable { expected_goal_id: String },
|
||||
EmptyResponse,
|
||||
}
|
||||
|
||||
struct GoalRuntimeInner {
|
||||
@@ -157,6 +158,8 @@ impl GoalRuntimeHandle {
|
||||
if !self.is_enabled() {
|
||||
return Ok(());
|
||||
}
|
||||
// Invalidate the old turn before the persisted objective/status changes.
|
||||
self.inner.accounting_state.reset_empty_responses();
|
||||
|
||||
if let Some(turn_id) = self.inner.accounting_state.current_turn_id() {
|
||||
self.account_active_goal_progress(
|
||||
@@ -187,6 +190,7 @@ impl GoalRuntimeHandle {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
self.inner.accounting_state.reset_empty_responses();
|
||||
let replaced_existing_goal = previous_goal
|
||||
.as_ref()
|
||||
.is_some_and(|previous_goal| previous_goal.goal_id != goal.goal_id);
|
||||
@@ -262,7 +266,7 @@ impl GoalRuntimeHandle {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Accounts the ending turn and stops its active goal after a terminal error.
|
||||
/// Accounts the ending turn and stops its active goal after an error or repeated empty output.
|
||||
pub(crate) async fn stop_active_goal_for_turn(
|
||||
&self,
|
||||
turn_id: &str,
|
||||
@@ -297,6 +301,21 @@ impl GoalRuntimeHandle {
|
||||
codex_state::ThreadGoalStatus::UsageLimited,
|
||||
None,
|
||||
),
|
||||
ActiveGoalStopReason::EmptyResponse => {
|
||||
let Some(expected_goal_id) =
|
||||
self.inner.accounting_state.empty_response_goal(turn_id)
|
||||
else {
|
||||
return Ok(());
|
||||
};
|
||||
if accounting_goal_id != expected_goal_id {
|
||||
return Ok(());
|
||||
}
|
||||
(
|
||||
"empty-response",
|
||||
codex_state::ThreadGoalStatus::Blocked,
|
||||
Some(expected_goal_id),
|
||||
)
|
||||
}
|
||||
ActiveGoalStopReason::ExecutionUnavailable { expected_goal_id } => (
|
||||
"execution-unavailable",
|
||||
codex_state::ThreadGoalStatus::Blocked,
|
||||
@@ -459,7 +478,11 @@ impl GoalRuntimeHandle {
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(StartIfIdleSubmission::Started { .. }) => {}
|
||||
Ok(StartIfIdleSubmission::Started { turn_id }) => {
|
||||
// Turn-stop evaluation takes the same permit, so even a fast response
|
||||
// cannot finish before this host-admitted continuation is identified.
|
||||
self.inner.accounting_state.mark_goal_continuation(turn_id);
|
||||
}
|
||||
Ok(StartIfIdleSubmission::NotSubmitted { reason }) => {
|
||||
tracing::debug!(
|
||||
?reason,
|
||||
|
||||
Reference in new issue
Block a user