Stop TUI chats on misalignment policy violations (#39261)

## Why

When the app server reports a misalignment policy violation, the TUI must stop the affected chat rather than allow more work to continue in it.

## What changed

- Finalize the active turn, clear queued and draft input, disable the composer, and reject further operations and interactive requests for the stopped chat.
- Interrupt an active side conversation when its parent reports the violation, and prevent queued turns, forks, approvals, or goal resumption from restarting work.
- Show a non-dismissible precaution view that directs users to start a new chat or resume another one, with agent command center navigation for remote sessions.

## Testing

Added coverage for stopped-chat behavior, parent/side-thread handling, blocked goal resumption, navigation actions, and narrow and remote-session rendering.

GitOrigin-RevId: 0268c46a19294a17765ea4099f389f15ffaaf030
This commit is contained in:
Eric Traut authored and copyberry committed 2026-08-18 18:50:58 +00:00
1 parent 3006151ad4
commit 785ecd7452
16 files changed
+452 -3

No files matched your search

@@ -203,6 +203,7 @@ impl ChatWidget {
thread_rename_block_message: None,
active_side_conversation: false,
blocks_direct_input: false,
misalignment_policy_violation: false,
normal_placeholder_text: placeholder,
side_placeholder_text: side_placeholder,
forked_from: None,
+5 -2
View File
@@ -110,6 +110,9 @@ impl ChatWidget {
action: QueuedInputAction,
pending_pastes: Vec<(String, String)>,
) {
if self.misalignment_policy_violation {
return;
}
let should_run_now = self.is_session_configured()
&& !self.is_user_turn_pending_or_running()
&& !self.input_queue.suppress_queue_autosend;
@@ -135,7 +138,7 @@ impl ChatWidget {
/// If idle and there are queued inputs, submit exactly one to start the next turn.
pub(crate) fn maybe_send_next_queued_input(&mut self) -> bool {
if self.input_queue.suppress_queue_autosend {
if self.misalignment_policy_violation || self.input_queue.suppress_queue_autosend {
return false;
}
if self.blocks_direct_input {
@@ -178,7 +181,7 @@ impl ChatWidget {
submitted_follow_up
}
pub(super) fn is_user_turn_pending_or_running(&self) -> bool {
pub(crate) fn is_user_turn_pending_or_running(&self) -> bool {
self.input_queue.user_turn_pending_start
|| self.turn_lifecycle.agent_turn_running
|| self.review.is_review_mode
@@ -99,6 +99,9 @@ impl ChatWidget {
history_record: UserMessageHistoryRecord,
shell_escape_policy: ShellEscapePolicy,
) -> (bool, Option<AppCommand>) {
if self.misalignment_policy_violation {
return (false, None);
}
if !self.is_session_configured() {
tracing::warn!("cannot submit user message before session is configured; queueing");
self.input_queue
@@ -0,0 +1,83 @@
//! Terminal precaution UI for misalignment policy violations.
use super::*;
const MISALIGNMENT_POLICY_TITLE: &str = "Chat stopped as a precaution";
const MISALIGNMENT_POLICY_DESCRIPTION: &str = "We couldn’t confirm the agent was acting safely and following your instructions. To continue working, start or resume another chat.";
impl ChatWidget {
pub(crate) fn has_misalignment_policy_violation(&self) -> bool {
self.misalignment_policy_violation
}
pub(crate) fn rejects_misalignment_policy_op(&self, op: &AppCommand) -> bool {
self.misalignment_policy_violation
&& !matches!(
op,
AppCommand::Interrupt
| AppCommand::CleanBackgroundTerminals
| AppCommand::OverrideTurnContext { .. }
| AppCommand::ReloadUserConfig
| AppCommand::ListSkills { .. }
| AppCommand::SetThreadName { .. }
)
}
pub(crate) fn on_misalignment_policy_violation(&mut self) {
if self.misalignment_policy_violation {
return;
}
self.misalignment_policy_violation = true;
self.input_queue.clear();
self.finalize_turn();
self.refresh_pending_input_preview();
self.bottom_pane.drain_pending_submission_state();
self.bottom_pane
.set_composer_text(String::new(), Vec::new(), Vec::new());
self.bottom_pane.set_composer_input_enabled(
/*enabled*/ false,
Some(MISALIGNMENT_POLICY_TITLE.to_string()),
);
self.show_misalignment_policy_precaution();
}
pub(crate) fn show_misalignment_policy_precaution(&mut self) {
let mut items = vec![
SelectionItem {
name: "New chat".to_string(),
actions: vec![Box::new(|tx| tx.send(AppEvent::NewSession { name: None }))],
dismiss_on_select: true,
..Default::default()
},
SelectionItem {
name: "Resume another chat".to_string(),
actions: vec![Box::new(|tx| tx.send(AppEvent::OpenResumePicker))],
..Default::default()
},
];
if self.remote_connection.is_some() {
items.insert(
1,
SelectionItem {
name: "Agent command center".to_string(),
actions: vec![Box::new(|tx| tx.send(AppEvent::OpenAgentsOverview))],
..Default::default()
},
);
}
self.bottom_pane.show_selection_view(SelectionViewParams {
header: Box::new(
Paragraph::new(vec![
Line::from(MISALIGNMENT_POLICY_TITLE).bold(),
Line::from(MISALIGNMENT_POLICY_DESCRIPTION).dim(),
])
.wrap(Wrap { trim: false }),
),
items,
allow_cancel: false,
..Default::default()
});
}
}
@@ -11,6 +11,10 @@ impl ChatWidget {
request: ServerRequest,
replay_kind: Option<ReplayKind>,
) {
if self.misalignment_policy_violation {
return;
}
let id = request.id().to_string();
match request {
ServerRequest::CommandExecutionRequestApproval { params, .. } => {
@@ -0,0 +1,10 @@
---
source: tui/src/chatwidget/tests/misalignment_policy_tests.rs
expression: popup
---
Chat stopped as a precaution
We couldn’t confirm the agent was acting safely and following your
instructions. To continue working, start or resume another chat.
› 1. New chat
2. Resume another chat
@@ -0,0 +1,12 @@
---
source: tui/src/chatwidget/tests/misalignment_policy_tests.rs
expression: "render_bottom_popup(&chat, 38)"
---
Chat stopped as a precaution
We couldn’t confirm the agent was
acting safely and following your
instructions. To continue working,
start or resume another chat.
› 1. New chat
2. Resume another chat
@@ -0,0 +1,11 @@
---
source: tui/src/chatwidget/tests/misalignment_policy_tests.rs
expression: "render_bottom_popup(&chat, 80)"
---
Chat stopped as a precaution
We couldn’t confirm the agent was acting safely and following your
instructions. To continue working, start or resume another chat.
› 1. New chat
2. Agent command center
3. Resume another chat
+2
View File
@@ -243,6 +243,8 @@ mod guardian;
pub(crate) mod helpers;
mod history_replay;
mod mcp_startup;
#[path = "tests/misalignment_policy_tests.rs"]
mod misalignment_policy;
mod permissions;
mod plan_mode;
#[path = "tests/plugin_catalog_tests.rs"]
@@ -0,0 +1,121 @@
use super::*;
use pretty_assertions::assert_eq;
const ERROR_MESSAGE: &str = "Responses API returned misalignment_policy_violation";
#[tokio::test]
async fn misalignment_policy_failure_stops_the_thread_and_renders_once() {
let (mut chat, mut rx, mut op_rx) = make_chatwidget_manual(/*model_override*/ None).await;
let thread_id = ThreadId::new();
chat.thread_id = Some(thread_id);
handle_turn_started(&mut chat, "turn-1");
chat.handle_server_notification(
ServerNotification::ModelSafetyBufferingUpdated(ModelSafetyBufferingUpdatedNotification {
thread_id: thread_id.to_string(),
turn_id: "turn-1".to_string(),
model: "current-model".to_string(),
use_cases: Vec::new(),
reasons: Vec::new(),
show_buffering_ui: true,
faster_model: None,
}),
/*replay_kind*/ None,
);
assert!(render_bottom_popup(&chat, /*width*/ 80).contains("thinking a bit more"));
chat.queue_user_message(UserMessage::from("queued follow-up"));
chat.bottom_pane
.set_composer_text("stale draft".to_string(), Vec::new(), Vec::new());
drain_insert_history(&mut rx);
handle_error(
&mut chat,
ERROR_MESSAGE,
Some(CodexErrorInfo::MisalignmentPolicyViolation),
);
assert!(!chat.bottom_pane.is_task_running());
assert!(!chat.bottom_pane.composer_input_enabled());
assert!(chat.queued_user_message_texts().is_empty());
assert_eq!(chat.bottom_pane.composer_text_with_pending(), "");
let popup = render_bottom_popup(&chat, /*width*/ 80);
assert_chatwidget_snapshot!("misalignment_policy_violation", popup);
assert_chatwidget_snapshot!(
"misalignment_policy_violation_narrow",
render_bottom_popup(&chat, /*width*/ 38)
);
chat.submit_user_message(UserMessage::from("direct follow-up"));
chat.queue_user_message(UserMessage::from("queued follow-up"));
assert!(!chat.submit_op(Op::Compact));
assert!(!chat.submit_op(Op::run_user_shell_command("echo unsafe".to_string())));
assert!(!chat.maybe_send_next_queued_input());
assert!(chat.queued_user_message_texts().is_empty());
assert_no_submit_op(&mut op_rx);
chat.handle_server_request(
ServerRequest::ToolRequestUserInput {
request_id: AppServerRequestId::Integer(1),
params: ToolRequestUserInputParams {
thread_id: thread_id.to_string(),
turn_id: "turn-1".to_string(),
item_id: "input-1".to_string(),
questions: Vec::new(),
is_blocking: true,
auto_resolution_ms: None,
},
},
/*replay_kind*/ None,
);
chat.handle_server_notification(
ServerNotification::TurnCompleted(TurnCompletedNotification {
thread_id: thread_id.to_string(),
turn: app_server_turn(
"turn-1",
AppServerTurnStatus::Failed,
/*duration_ms*/ None,
Some(AppServerTurnError {
message: ERROR_MESSAGE.to_string(),
codex_error_info: Some(CodexErrorInfo::MisalignmentPolicyViolation),
additional_details: None,
}),
),
}),
/*replay_kind*/ None,
);
assert_eq!(render_bottom_popup(&chat, /*width*/ 80), popup);
assert!(drain_insert_history(&mut rx).is_empty());
chat.handle_key_event(KeyEvent::from(KeyCode::Esc));
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
assert_matches!(rx.try_recv(), Ok(AppEvent::NewSession { name: None }));
assert!(rx.try_recv().is_err());
chat.show_misalignment_policy_precaution();
assert_eq!(render_bottom_popup(&chat, /*width*/ 80), popup);
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
assert_matches!(rx.try_recv(), Ok(AppEvent::NewSession { name: None }));
chat.show_misalignment_policy_precaution();
chat.handle_key_event(KeyEvent::from(KeyCode::Down));
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
assert_matches!(rx.try_recv(), Ok(AppEvent::OpenResumePicker));
assert!(chat.bottom_pane.has_active_view());
chat.remote_connection = Some(crate::status::remote_connection::RemoteConnectionStatus {
address: "wss://remote.example.com".to_string(),
version: "v1.0.0".to_string(),
});
chat.show_misalignment_policy_precaution();
assert_chatwidget_snapshot!(
"misalignment_policy_violation_remote",
render_bottom_popup(&chat, /*width*/ 80)
);
chat.handle_key_event(KeyEvent::from(KeyCode::Down));
chat.handle_key_event(KeyEvent::from(KeyCode::Enter));
assert_matches!(rx.try_recv(), Ok(AppEvent::OpenAgentsOverview));
assert!(chat.bottom_pane.has_active_view());
}
+3 -1
View File
@@ -443,7 +443,9 @@ impl ChatWidget {
message: String,
codex_error_info: Option<AppServerCodexErrorInfo>,
) {
if codex_error_info
if codex_error_info == Some(AppServerCodexErrorInfo::MisalignmentPolicyViolation) {
self.on_misalignment_policy_violation();
} else if codex_error_info
.as_ref()
.is_some_and(|info| self.handle_app_server_steer_rejected_error(info))
{