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

@@ -1,11 +1,15 @@
use super::*;
use crate::chatwidget::UserMessage;
use codex_app_server_protocol::CodexErrorInfo;
use codex_app_server_protocol::ErrorNotification;
use codex_app_server_protocol::ThreadGoalStatus;
use codex_protocol::models::ManagedFileSystemPermissions;
use codex_protocol::permissions::FileSystemAccessMode;
use codex_protocol::permissions::FileSystemPath;
use codex_protocol::permissions::FileSystemSandboxEntry;
use codex_protocol::permissions::FileSystemSpecialPath;
use codex_protocol::permissions::NetworkSandboxPolicy;
use pretty_assertions::assert_eq;
#[tokio::test]
async fn turn_start_failure_is_shown_without_exiting() -> Result<()> {
@@ -57,6 +61,142 @@ async fn turn_start_failure_is_shown_without_exiting() -> Result<()> {
Ok(())
}
#[tokio::test]
async fn misalignment_policy_blocks_queued_turns_and_goal_resumption() -> Result<()> {
let (mut app, _app_event_rx, mut op_rx) = make_test_app_with_channels().await;
let mut tui = crate::tui::test_support::make_test_tui()?;
let mut app_server = crate::start_embedded_app_server_for_picker(&app.config).await?;
let started = app_server.start_thread(&app.config).await?;
let thread_id = started.session.thread_id;
let session = started.session.clone();
app.enqueue_primary_thread_session(started.session, started.turns)
.await?;
app_server
.thread_goal_set(
thread_id,
Some("Do not restart this stopped goal".to_string()),
Some(ThreadGoalStatus::Blocked),
/*token_budget*/ None,
)
.await?;
app.chat_widget
.restore_user_message_to_composer("queued before terminal failure".into());
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
let queued_turn = next_user_turn_op(&mut op_rx);
let failed_turn = Turn {
error: Some(AppServerTurnError {
message: "misalignment policy violation".to_string(),
codex_error_info: Some(CodexErrorInfo::MisalignmentPolicyViolation),
additional_details: None,
}),
..test_turn("turn-1", TurnStatus::Failed, Vec::new())
};
app.enqueue_primary_thread_session(session, vec![failed_turn])
.await?;
let precaution = render_bottom_popup(&app.chat_widget, /*width*/ 80);
let AppServerRequestId::Integer(next_request_id) = app_server.next_request_id() else {
unreachable!("embedded app-server request IDs are integers");
};
app.maybe_prompt_resume_paused_goal_after_resume(&mut app_server, thread_id)
.await;
for event in [
AppEvent::CodexOp(queued_turn),
AppEvent::CodexOp(crate::app_command::AppCommand::ExecApproval {
id: "queued-approval".to_string(),
turn_id: None,
decision: codex_app_server_protocol::CommandExecutionApprovalDecision::Accept,
}),
AppEvent::SetThreadGoalStatus {
thread_id,
status: ThreadGoalStatus::Active,
},
AppEvent::SetThreadGoalDraft {
thread_id,
draft: crate::goal_files::GoalDraft {
objective: "Do not replace this stopped goal".to_string(),
..Default::default()
},
mode: crate::app_event::ThreadGoalSetMode::ReplaceExisting,
},
AppEvent::StartSide {
parent_thread_id: thread_id,
user_message: Some("Do not fork this stopped thread".into()),
},
AppEvent::ForkCurrentSession { name: None },
AppEvent::OpenAgentPicker,
AppEvent::SelectAgentThread(thread_id),
] {
app.handle_event(&mut tui, &mut app_server, event).await?;
}
assert_eq!(
app_server.next_request_id(),
AppServerRequestId::Integer(next_request_id + 1)
);
assert_eq!(
render_bottom_popup(&app.chat_widget, /*width*/ 80),
precaution
);
assert_eq!(
app_server
.thread_goal_get(thread_id)
.await?
.goal
.unwrap()
.status,
ThreadGoalStatus::Blocked
);
app_server.shutdown().await?;
Ok(())
}
#[tokio::test]
async fn misalignment_policy_in_parent_stops_active_side_conversation() -> Result<()> {
let (mut app, _app_event_rx, mut op_rx) = make_test_app_with_channels().await;
let parent_thread_id = ThreadId::new();
let side_thread_id = ThreadId::new();
app.primary_thread_id = Some(parent_thread_id);
app.active_thread_id = Some(side_thread_id);
app.side_threads
.insert(side_thread_id, SideThreadState::new(parent_thread_id));
app.chat_widget.handle_thread_session(test_thread_session(
side_thread_id,
app.config.cwd.to_path_buf(),
));
app.chat_widget
.restore_user_message_to_composer("pending side turn".into());
app.chat_widget
.handle_key_event(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
while op_rx.try_recv().is_ok() {}
app.enqueue_thread_notification(
parent_thread_id,
ServerNotification::Error(ErrorNotification {
error: AppServerTurnError {
message: "misalignment policy violation".to_string(),
codex_error_info: Some(CodexErrorInfo::MisalignmentPolicyViolation),
additional_details: None,
},
will_retry: false,
thread_id: parent_thread_id.to_string(),
turn_id: "parent-turn".to_string(),
}),
)
.await?;
assert!(app.chat_widget.has_misalignment_policy_violation());
assert!(
render_bottom_popup(&app.chat_widget, /*width*/ 80)
.contains("Chat stopped as a precaution")
);
assert_matches!(op_rx.try_recv(), Ok(Op::Interrupt));
Ok(())
}
#[tokio::test]
async fn unsupported_legacy_permissions_are_shown_without_exiting() -> Result<()> {
let (mut app, mut app_event_rx, mut op_rx) = make_test_app_with_channels().await;