mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
Report pending startup dialogs through terminal program status (#64384)
## Why Terminal hosts need to know when Codex is waiting for a startup decision, including while a chat status record is active. ## What changed Emit a separate `OSC 7501` blocked record while waiting for directory trust, startup hook review, or an update decision. Report trust and hook review as permission prompts and the update prompt as a question. Clear the dialog record when the prompt closes or Codex suspends. Report it again if Codex resumes waiting for input. Dialog records can coexist with the chat status record. ## Testing Extend the connected directory trust PTY test to check blocked and clear records for consent and hook review. Exercise Ctrl-Z in a PTY without a controlling terminal and verify that the pending trust record stays active until the prompt is answered. GitOrigin-RevId: 9801e03b28a19b75f62a09e3e927de0dd59c5448
This commit is contained in:
@@ -31,6 +31,7 @@ fn encodes_iterm_session_status_fields() {
|
||||
] {
|
||||
let mut value = String::new();
|
||||
SetTerminalProgramStatus {
|
||||
id: None,
|
||||
status,
|
||||
iterm_detail: detail,
|
||||
emit_iterm_status: true,
|
||||
@@ -52,6 +53,7 @@ fn encodes_iterm_session_status_fields() {
|
||||
fn encodes_generic_program_status() {
|
||||
let mut encoded = String::new();
|
||||
SetTerminalProgramStatus {
|
||||
id: None,
|
||||
status: ProgramStatus::Working,
|
||||
iterm_detail: "ignored",
|
||||
emit_iterm_status: false,
|
||||
@@ -78,12 +80,14 @@ fn detail_is_safe_bounded_osc_text() {
|
||||
fn clear_empties_every_owned_field() {
|
||||
let mut combined = String::new();
|
||||
ClearTerminalProgramStatus {
|
||||
id: None,
|
||||
emit_iterm_status: true,
|
||||
}
|
||||
.write_ansi(&mut combined)
|
||||
.expect("encode combined terminal program status clear");
|
||||
let mut generic = String::new();
|
||||
ClearTerminalProgramStatus {
|
||||
id: None,
|
||||
emit_iterm_status: false,
|
||||
}
|
||||
.write_ansi(&mut generic)
|
||||
|
||||
@@ -52,6 +52,7 @@ use crate::onboarding::trust_directory::TrustDirectorySelection;
|
||||
use crate::onboarding::trust_directory::TrustDirectoryWidget;
|
||||
use crate::onboarding::welcome::WelcomeWidget;
|
||||
use crate::startup_draft::StartupCancelled;
|
||||
use crate::terminal_program_status::StartupDialog;
|
||||
use crate::tui::FrameRequester;
|
||||
use crate::tui::MouseCapture;
|
||||
use crate::tui::Tui;
|
||||
@@ -728,8 +729,7 @@ async fn run_onboarding_screen_inner(
|
||||
})?;
|
||||
|
||||
tui.discard_pending_input_before_interactive_screen()?;
|
||||
let tui_events = tui.event_stream();
|
||||
tokio::pin!(tui_events);
|
||||
let mut tui_events = tui.input_stream();
|
||||
let mut trust_step_was_active = onboarding_screen.is_trust_step_active();
|
||||
|
||||
let folder_connection = match (
|
||||
@@ -741,6 +741,9 @@ async fn run_onboarding_screen_inner(
|
||||
};
|
||||
|
||||
while !onboarding_screen.is_done() {
|
||||
tui_events
|
||||
.dialog_status
|
||||
.set(trust_step_was_active.then_some(StartupDialog::Trust));
|
||||
tokio::select! {
|
||||
() = async {
|
||||
match onboarding_screen.auth_widget_mut() {
|
||||
@@ -796,6 +799,9 @@ async fn run_onboarding_screen_inner(
|
||||
} else {
|
||||
onboarding_screen.handle_key_event(key_event);
|
||||
}
|
||||
if trust_step_was_active && !onboarding_screen.is_trust_step_active() {
|
||||
tui_events.dialog_status.clear();
|
||||
}
|
||||
if !directory_trust_persisted {
|
||||
directory_trust_persisted = persist_selected_trust(
|
||||
&mut onboarding_screen,
|
||||
|
||||
@@ -31,6 +31,7 @@ use crate::keymap::RuntimeKeymap;
|
||||
use crate::legacy_core::config::Config;
|
||||
use crate::render::renderable::ColumnRenderable;
|
||||
use crate::render::renderable::Renderable;
|
||||
use crate::terminal_program_status::StartupDialog;
|
||||
use crate::tui::Tui;
|
||||
use crate::tui::TuiEvent;
|
||||
use codex_app_server_client::AppServerRequestHandle;
|
||||
@@ -104,10 +105,10 @@ async fn run_startup_hooks_review_app(
|
||||
draw_view(tui, &view, &chord_matcher, &keymap)?;
|
||||
|
||||
tui.discard_pending_input_before_interactive_screen()?;
|
||||
let tui_events = tui.event_stream();
|
||||
tokio::pin!(tui_events);
|
||||
let mut tui_events = tui.input_stream();
|
||||
|
||||
loop {
|
||||
tui_events.dialog_status.set(Some(StartupDialog::Hooks));
|
||||
let Some(event) = tui_events.next().await else {
|
||||
return Ok(StartupHooksReviewOutcome::Continue);
|
||||
};
|
||||
@@ -138,6 +139,7 @@ async fn run_startup_hooks_review_app(
|
||||
draw_view(tui, &view, &chord_matcher, &keymap)?;
|
||||
continue;
|
||||
};
|
||||
tui_events.dialog_status.clear();
|
||||
match selection {
|
||||
StartupHooksReviewSelection::ReviewHooks => {
|
||||
return Ok(StartupHooksReviewOutcome::OpenHooksBrowser(entry));
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
//! OSC 7501 reports lifecycle state and blocking reasons to terminal sessions.
|
||||
//! Direct iTerm2 sessions also receive OSC 21337 status, including bounded
|
||||
//! activity text. The process-wide cache is used for exit, panic, and terminal
|
||||
//! handoff cleanup.
|
||||
//! handoff cleanup of the root record. Each dialog owns its child record through
|
||||
//! the input stream that waits for the decision.
|
||||
|
||||
use crate::terminal_capabilities::TerminalProgramStatusSupport;
|
||||
use crate::terminal_capabilities::detect_terminal_program_status_support;
|
||||
@@ -96,6 +97,78 @@ pub(crate) enum ProgramStatusKind {
|
||||
Auth,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub(crate) enum StartupDialog {
|
||||
Trust,
|
||||
Hooks,
|
||||
#[cfg(not(debug_assertions))]
|
||||
Update,
|
||||
}
|
||||
|
||||
impl StartupDialog {
|
||||
fn record(self) -> (&'static str, ProgramStatusKind) {
|
||||
match self {
|
||||
Self::Trust => ("startup/trust", ProgramStatusKind::Permission),
|
||||
Self::Hooks => ("startup/hooks", ProgramStatusKind::Permission),
|
||||
#[cfg(not(debug_assertions))]
|
||||
Self::Update => ("startup/update", ProgramStatusKind::Question),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A dialog can coexist with the chat root. Never clear the root or emit iTerm
|
||||
/// status here, since neither operation is scoped to the dialog.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct DialogStatus(Option<StartupDialog>);
|
||||
|
||||
impl DialogStatus {
|
||||
pub(crate) fn clear(&mut self) {
|
||||
self.set(/*dialog*/ None);
|
||||
}
|
||||
|
||||
pub(crate) fn set(&mut self, dialog: Option<StartupDialog>) {
|
||||
if self.0 == dialog || cfg!(test) || !current_terminal_program_status_support().osc_7501 {
|
||||
return;
|
||||
}
|
||||
if let Some(previous) = self.0 {
|
||||
let (id, _) = previous.record();
|
||||
if let Err(err) = execute!(
|
||||
stdout(),
|
||||
ClearTerminalProgramStatus {
|
||||
id: Some(id),
|
||||
emit_iterm_status: false,
|
||||
}
|
||||
) {
|
||||
tracing::debug!("failed to clear dialog status: {err}");
|
||||
return;
|
||||
}
|
||||
self.0 = None;
|
||||
}
|
||||
if let Some(dialog) = dialog {
|
||||
let (id, kind) = dialog.record();
|
||||
if let Err(err) = execute!(
|
||||
stdout(),
|
||||
SetTerminalProgramStatus {
|
||||
id: Some(id),
|
||||
status: ProgramStatus::Blocked(kind),
|
||||
iterm_detail: "",
|
||||
emit_iterm_status: false,
|
||||
}
|
||||
) {
|
||||
tracing::debug!("failed to report dialog status: {err}");
|
||||
return;
|
||||
}
|
||||
self.0 = Some(dialog);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for DialogStatus {
|
||||
fn drop(&mut self) {
|
||||
self.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ProgramStatus {
|
||||
fn iterm_label(self) -> &'static str {
|
||||
match self {
|
||||
@@ -146,6 +219,7 @@ pub(crate) fn set_terminal_program_status(
|
||||
execute!(
|
||||
stdout(),
|
||||
SetTerminalProgramStatus {
|
||||
id: None,
|
||||
status,
|
||||
iterm_detail: &detail,
|
||||
emit_iterm_status: support.osc_21337,
|
||||
@@ -168,6 +242,7 @@ pub(crate) fn clear_terminal_program_status() -> io::Result<()> {
|
||||
execute!(
|
||||
stdout(),
|
||||
ClearTerminalProgramStatus {
|
||||
id: None,
|
||||
emit_iterm_status: support.osc_21337,
|
||||
}
|
||||
)
|
||||
@@ -199,6 +274,7 @@ fn sanitize_iterm_session_detail(detail: &str) -> String {
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct SetTerminalProgramStatus<'a> {
|
||||
id: Option<&'static str>,
|
||||
status: ProgramStatus,
|
||||
iterm_detail: &'a str,
|
||||
emit_iterm_status: bool,
|
||||
@@ -228,6 +304,9 @@ impl Command for SetTerminalProgramStatus<'_> {
|
||||
};
|
||||
write!(f, ":kind={kind}")?;
|
||||
}
|
||||
if let Some(id) = self.id {
|
||||
write!(f, ":id={id}")?;
|
||||
}
|
||||
write!(f, "\x1b\\")
|
||||
}
|
||||
|
||||
@@ -246,6 +325,7 @@ impl Command for SetTerminalProgramStatus<'_> {
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct ClearTerminalProgramStatus {
|
||||
id: Option<&'static str>,
|
||||
emit_iterm_status: bool,
|
||||
}
|
||||
|
||||
@@ -254,7 +334,11 @@ impl Command for ClearTerminalProgramStatus {
|
||||
if self.emit_iterm_status {
|
||||
write!(f, "\x1b]21337;status=;indicator=;status-color=;detail=\x07")?;
|
||||
}
|
||||
write!(f, "\x1b]7501;state=clear\x1b\\")
|
||||
write!(f, "\x1b]7501;state=clear")?;
|
||||
if let Some(id) = self.id {
|
||||
write!(f, ":id={id}")?;
|
||||
}
|
||||
write!(f, "\x1b\\")
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
|
||||
@@ -219,6 +219,7 @@ pub struct TuiEventStream<S: EventSource + Default + Unpin = CrosstermEventSourc
|
||||
resume_stream: WatchStream<()>,
|
||||
terminal_focused: Arc<AtomicBool>,
|
||||
poll_draw_first: bool,
|
||||
pub(crate) dialog_status: crate::terminal_program_status::DialogStatus,
|
||||
pub(crate) coalesce_divider_drags: bool,
|
||||
#[cfg(unix)]
|
||||
suspend_context: crate::tui::job_control::SuspendContext,
|
||||
@@ -241,6 +242,7 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
|
||||
resume_stream,
|
||||
terminal_focused,
|
||||
poll_draw_first: false,
|
||||
dialog_status: Default::default(),
|
||||
coalesce_divider_drags: false,
|
||||
#[cfg(unix)]
|
||||
suspend_context,
|
||||
@@ -358,7 +360,9 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
|
||||
#[cfg(unix)]
|
||||
if crate::tui::job_control::SUSPEND_KEY.is_press(key_event) {
|
||||
self.broker.pause_events();
|
||||
let suspend_result = self.suspend_context.suspend(&self.alt_screen_active);
|
||||
let suspend_result = self
|
||||
.suspend_context
|
||||
.suspend(&self.alt_screen_active, &mut self.dialog_status);
|
||||
self.broker.resume_events();
|
||||
if let Err(err) = suspend_result {
|
||||
tracing::warn!(
|
||||
|
||||
@@ -17,6 +17,7 @@ use ratatui::layout::Rect;
|
||||
use ratatui::layout::Size;
|
||||
|
||||
use crate::key_hint;
|
||||
use crate::terminal_program_status::DialogStatus;
|
||||
|
||||
use super::Tui;
|
||||
|
||||
@@ -59,7 +60,11 @@ impl SuspendContext {
|
||||
/// otherwise record `RealignInline`.
|
||||
/// - Update the cached inline cursor row so suspend can place the cursor meaningfully.
|
||||
/// - Trigger SIGTSTP so the process can be resumed and continue drawing with the saved state.
|
||||
pub(crate) fn suspend(&self, alt_screen_active: &Arc<AtomicBool>) -> Result<()> {
|
||||
pub(crate) fn suspend(
|
||||
&self,
|
||||
alt_screen_active: &Arc<AtomicBool>,
|
||||
dialog_status: &mut DialogStatus,
|
||||
) -> Result<()> {
|
||||
// A launcher can supply a PTY without making it our controlling terminal.
|
||||
// Leave the TUI intact if we couldn't verify ownership on resume.
|
||||
if unsafe { libc::tcgetpgrp(libc::STDIN_FILENO) } == -1 {
|
||||
@@ -74,6 +79,8 @@ impl SuspendContext {
|
||||
}
|
||||
let y = self.suspend_cursor_y.load(Ordering::Relaxed);
|
||||
let _ = execute!(stdout(), MoveTo(0, y), Show);
|
||||
// The dialog loop reports again when it resumes waiting for input.
|
||||
dialog_status.clear();
|
||||
if let Err(err) = crate::terminal_program_status::clear_terminal_program_status() {
|
||||
tracing::debug!(error = %err, "failed to clear terminal program status before suspend");
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ use crate::render::renderable::FlexRenderable;
|
||||
use crate::render::renderable::Renderable;
|
||||
use crate::render::renderable::RenderableExt as _;
|
||||
use crate::render::renderable::RenderableItem;
|
||||
#[cfg(not(debug_assertions))]
|
||||
use crate::terminal_program_status::StartupDialog;
|
||||
use crate::tui::FrameRequester;
|
||||
#[cfg(not(debug_assertions))]
|
||||
use crate::tui::Tui;
|
||||
@@ -66,10 +68,10 @@ pub(crate) async fn run_update_prompt_if_needed(
|
||||
})?;
|
||||
|
||||
tui.discard_pending_input_before_interactive_screen()?;
|
||||
let events = tui.event_stream();
|
||||
tokio::pin!(events);
|
||||
let mut events = tui.input_stream();
|
||||
|
||||
while !screen.is_done() {
|
||||
events.dialog_status.set(Some(StartupDialog::Update));
|
||||
if let Some(event) = events.next().await {
|
||||
tui.screen_size_for_event(&event)?;
|
||||
match event {
|
||||
@@ -86,6 +88,7 @@ pub(crate) async fn run_update_prompt_if_needed(
|
||||
}
|
||||
}
|
||||
|
||||
drop(events);
|
||||
match screen.selection() {
|
||||
Some(UpdateSelection::UpdateNow) => {
|
||||
tui.terminal.clear()?;
|
||||
|
||||
@@ -14,6 +14,7 @@ use std::sync::Mutex;
|
||||
use std::sync::atomic::AtomicUsize;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
use tokio::net::UnixListener;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
|
||||
@@ -166,6 +167,11 @@ async fn connected_trust_cancellation_and_acceptance_control_task_creation() ->
|
||||
} else {
|
||||
"Trust and continue"
|
||||
};
|
||||
let trust_pair = [
|
||||
"state=blocked:app=codex:kind=permission:id=startup/trust",
|
||||
"state=clear:id=startup/trust",
|
||||
];
|
||||
let mut consent_count = 0;
|
||||
for (expected, input) in [
|
||||
(prompt, b"\x1b".as_slice()),
|
||||
("Launch-folder task", b"\x1b"),
|
||||
@@ -200,8 +206,19 @@ async fn connected_trust_cancellation_and_acceptance_control_task_creation() ->
|
||||
);
|
||||
}
|
||||
if is_consent {
|
||||
consent_count += 1;
|
||||
terminal.wait_for_screen("Back to Agent Command Center")?;
|
||||
}
|
||||
if is_consent && consent_count == 1 {
|
||||
// The host must see the first pending decision before we answer it.
|
||||
let deadline = Instant::now() + Duration::from_secs(/*secs*/ 5);
|
||||
while !String::from_utf8_lossy(&terminal.output).contains(trust_pair[0]) {
|
||||
anyhow::ensure!(Instant::now() < deadline, "missing trust status");
|
||||
terminal.read_output(Duration::from_millis(/*millis*/ 50))?;
|
||||
}
|
||||
// This PTY is not the controlling terminal. Ctrl-Z must leave the record intact.
|
||||
terminal.write_input(b"\x1a")?;
|
||||
}
|
||||
// Confirmations must be fresh input after the protected-screen transition.
|
||||
tokio::time::sleep(Duration::from_millis(/*millis*/ 200)).await;
|
||||
terminal.write_input(input)?;
|
||||
@@ -220,6 +237,27 @@ async fn connected_trust_cancellation_and_acceptance_control_task_creation() ->
|
||||
terminal.write_input(b"3")?;
|
||||
}
|
||||
terminal.wait_for_screen("Consent accepted")?;
|
||||
let output = String::from_utf8_lossy(&terminal.output);
|
||||
let records: Vec<_> = output
|
||||
.split("\x1b]7501;")
|
||||
.skip(1)
|
||||
.filter_map(|s| s.split_once("\x1b\\"))
|
||||
.map(|(record, _)| record)
|
||||
.filter(|record| record.contains(":id=startup/"))
|
||||
.collect();
|
||||
// Each consent step contributes one blocked/clear pair, followed by optional hook review.
|
||||
let (trust, hooks) = records
|
||||
.split_at_checked(2 * consent_count)
|
||||
.expect("missing folder consent reports");
|
||||
assert_eq!(trust, trust_pair.repeat(consent_count));
|
||||
if trust_level.is_none() {
|
||||
insta::assert_snapshot!(hooks.join("\n"), @r"
|
||||
state=blocked:app=codex:kind=permission:id=startup/hooks
|
||||
state=clear:id=startup/hooks
|
||||
");
|
||||
} else {
|
||||
assert!(hooks.is_empty());
|
||||
}
|
||||
drop(terminal);
|
||||
server.abort();
|
||||
let methods = methods.lock().unwrap();
|
||||
|
||||
@@ -340,7 +340,7 @@ pub(super) struct PtyCodex {
|
||||
slave: File,
|
||||
child: Child,
|
||||
parser: vt100::Parser,
|
||||
output: Vec<u8>,
|
||||
pub(super) output: Vec<u8>,
|
||||
cursor_answered: bool,
|
||||
palette_answered: bool,
|
||||
keyboard_answered: bool,
|
||||
@@ -424,6 +424,7 @@ impl PtyCodex {
|
||||
.env_remove("TMUX")
|
||||
.env_remove("TMUX_PANE")
|
||||
.env_remove("STY")
|
||||
.env_remove("VTE_VERSION")
|
||||
.env("TERM_PROGRAM", "kitty")
|
||||
.env_remove("TERM_PROGRAM_VERSION")
|
||||
// Exercise the fixture's login state even on hosts configured with workload identity.
|
||||
|
||||
Reference in New Issue
Block a user