Files
codex/codex-rs/tui/src/app/thread_goal_actions.rs
T
Eric Traut 0d344aca9b goal: pause continuation loops on usage limits and blockers (#23094)
Addresses #22833, #22245, #23067

## Why
`/goal` can keep synthesizing turns even when the next turn cannot make
meaningful progress. Hard usage exhaustion can replay failing turns, and
repeated permission or external-resource blockers can keep burning
tokens while waiting for user or system intervention.

## What changed
- Add resumable `blocked` and `usageLimited` goal states. As with
`paused`, goal continuation stops with these states.
- Move to `usageLimited` after usage-limit failures.
- Allow the built-in `update_goal` tool to set `blocked` only under
explicit repeated-impasse guidance. Updated goal continuation prompt to
specify that agent should use `blocked` only when it has made at least
three attempts to get past an impasse.

Most of the files touched by this PR are because of the small app server
protocol update.

## Validation

I manually reproduced a number of situations where an agent can run into
a true impasse and verified that it properly enters `blocked` state. I
then resumed and verified that it once again entered `blocked` state
several turns later if the impasse still exists.

I also manually reproduced the usage-limit condition by creating a
simulated responses API endpoint that returns 429 errors with the
appropriate error message. Verified that the goal runtime properly moves
the goal into `usageLimited` state and TUI UI updates appropriately.
Verified that `/goal resume` resumes (and immediately goes back into
`ussageLImited` state if appropriate).


## Follow-up PRs

Small changes will be needed to the GUI clients to properly handle the
two new states.
2026-05-18 11:28:53 -07:00

277 lines
9.1 KiB
Rust

use super::App;
use crate::app_event::AppEvent;
use crate::app_event::ThreadGoalSetMode;
use crate::app_server_session::AppServerSession;
use crate::bottom_pane::SelectionAction;
use crate::bottom_pane::SelectionItem;
use crate::bottom_pane::SelectionViewParams;
use crate::bottom_pane::popup_consts::standard_popup_hint_line;
use crate::goal_display::goal_status_label;
use crate::goal_display::goal_usage_summary;
use codex_app_server_protocol::ThreadGoalStatus;
use codex_protocol::ThreadId;
impl App {
pub(super) async fn open_thread_goal_menu(
&mut self,
app_server: &mut AppServerSession,
thread_id: ThreadId,
) {
let result = app_server.thread_goal_get(thread_id).await;
if self.current_displayed_thread_id() != Some(thread_id) {
return;
}
let response = match result {
Ok(response) => response,
Err(err) => {
self.chat_widget
.add_error_message(format!("Failed to read thread goal: {err}"));
return;
}
};
let Some(goal) = response.goal else {
self.chat_widget.add_info_message(
"Usage: /goal <objective>".to_string(),
Some("No goal is currently set.".to_string()),
);
return;
};
self.chat_widget.show_goal_summary(goal);
}
pub(super) async fn maybe_prompt_resume_paused_goal_after_resume(
&mut self,
app_server: &mut AppServerSession,
thread_id: ThreadId,
) {
let result = app_server.thread_goal_get(thread_id).await;
if self.current_displayed_thread_id() != Some(thread_id) {
return;
}
let response = match result {
Ok(response) => response,
Err(err) => {
tracing::warn!("failed to read thread goal after resume: {err}");
return;
}
};
let Some(goal) = response.goal else {
return;
};
if matches!(
goal.status,
ThreadGoalStatus::Paused | ThreadGoalStatus::Blocked | ThreadGoalStatus::UsageLimited
) {
self.chat_widget
.show_resume_paused_goal_prompt(thread_id, goal.objective);
}
}
pub(super) async fn open_thread_goal_editor(
&mut self,
app_server: &mut AppServerSession,
thread_id: Option<ThreadId>,
) {
let Some(thread_id) = thread_id else {
self.show_no_thread_goal_to_edit();
return;
};
let result = app_server.thread_goal_get(thread_id).await;
if self.current_displayed_thread_id() != Some(thread_id) {
return;
}
let response = match result {
Ok(response) => response,
Err(err) => {
self.chat_widget
.add_error_message(format!("Failed to read thread goal: {err}"));
return;
}
};
let Some(goal) = response.goal else {
self.show_no_thread_goal_to_edit();
return;
};
self.chat_widget.show_goal_edit_prompt(thread_id, goal);
}
pub(super) async fn set_thread_goal_objective(
&mut self,
app_server: &mut AppServerSession,
thread_id: ThreadId,
objective: String,
mode: ThreadGoalSetMode,
) {
if matches!(mode, ThreadGoalSetMode::ConfirmIfExists) {
let result = app_server.thread_goal_get(thread_id).await;
if self.current_displayed_thread_id() != Some(thread_id) {
return;
}
match result {
Ok(response) if response.goal.is_some() => {
self.show_replace_thread_goal_confirmation(thread_id, objective);
return;
}
Ok(_) => {}
Err(err) => {
self.chat_widget
.add_error_message(format!("Failed to read thread goal: {err}"));
return;
}
}
}
let replacing_goal = matches!(mode, ThreadGoalSetMode::ReplaceExisting);
if replacing_goal {
let result = app_server.thread_goal_clear(thread_id).await;
if let Err(err) = result {
if self.current_displayed_thread_id() != Some(thread_id) {
return;
}
self.chat_widget
.add_error_message(format!("Failed to replace thread goal: {err}"));
return;
}
}
let (status, token_budget) = match mode {
ThreadGoalSetMode::ConfirmIfExists | ThreadGoalSetMode::ReplaceExisting => {
(ThreadGoalStatus::Active, None)
}
ThreadGoalSetMode::UpdateExisting {
status,
token_budget,
} => (status, Some(token_budget)),
};
let result = app_server
.thread_goal_set(thread_id, Some(objective), Some(status), token_budget)
.await;
if self.current_displayed_thread_id() != Some(thread_id) {
return;
}
match result {
Ok(response) => self.chat_widget.add_info_message(
format!("Goal {}", goal_status_label(response.goal.status)),
Some(goal_usage_summary(&response.goal)),
),
Err(err) => {
let action = if replacing_goal { "replace" } else { "set" };
self.chat_widget
.add_error_message(format!("Failed to {action} thread goal: {err}"));
}
}
}
pub(super) async fn set_thread_goal_status(
&mut self,
app_server: &mut AppServerSession,
thread_id: ThreadId,
status: ThreadGoalStatus,
) {
let result = app_server
.thread_goal_set(
thread_id,
/*objective*/ None,
Some(status),
/*token_budget*/ None,
)
.await;
if self.current_displayed_thread_id() != Some(thread_id) {
return;
}
match result {
Ok(response) => self.chat_widget.add_info_message(
format!("Goal {}", goal_status_label(response.goal.status)),
Some(goal_usage_summary(&response.goal)),
),
Err(err) => self
.chat_widget
.add_error_message(format!("Failed to update thread goal: {err}")),
}
}
pub(super) async fn clear_thread_goal(
&mut self,
app_server: &mut AppServerSession,
thread_id: ThreadId,
) {
let result = app_server.thread_goal_clear(thread_id).await;
if self.current_displayed_thread_id() != Some(thread_id) {
return;
}
match result {
Ok(response) => {
if response.cleared {
self.chat_widget
.add_info_message("Goal cleared".to_string(), /*hint*/ None);
} else {
self.chat_widget.add_info_message(
"No goal to clear".to_string(),
Some("This thread does not currently have a goal.".to_string()),
);
}
}
Err(err) => self
.chat_widget
.add_error_message(format!("Failed to clear thread goal: {err}")),
}
}
fn show_replace_thread_goal_confirmation(&mut self, thread_id: ThreadId, objective: String) {
let replace_objective = objective.clone();
let replace_actions: Vec<SelectionAction> = vec![Box::new(move |tx| {
tx.send(AppEvent::SetThreadGoalObjective {
thread_id,
objective: replace_objective.clone(),
mode: ThreadGoalSetMode::ReplaceExisting,
});
})];
let items = vec![
SelectionItem {
name: "Replace current goal".to_string(),
description: Some("Set the new objective and start it now".to_string()),
actions: replace_actions,
dismiss_on_select: true,
..Default::default()
},
SelectionItem {
name: "Cancel".to_string(),
description: Some("Keep the current goal".to_string()),
dismiss_on_select: true,
..Default::default()
},
];
self.chat_widget.show_selection_view(SelectionViewParams {
title: Some("Replace goal?".to_string()),
subtitle: Some(format!("New objective: {objective}")),
footer_hint: Some(standard_popup_hint_line()),
items,
..Default::default()
});
}
fn show_no_thread_goal_to_edit(&mut self) {
self.chat_widget
.add_error_message("No goal is currently set.".to_string());
self.chat_widget.add_info_message(
"Usage: /goal <objective>".to_string(),
Some("Create a goal before editing it.".to_string()),
);
}
}