From f30ec6ace761320eb04d3f7af5e3b6883b93ab42 Mon Sep 17 00:00:00 2001 From: Kevin Liu Date: Thu, 22 Jan 2026 14:01:59 -0800 Subject: [PATCH] up --- codex-rs/cli/src/main.rs | 3 ++ codex-rs/tui/src/app.rs | 6 +++ codex-rs/tui/src/chatwidget.rs | 60 ++++++++++++---------------- codex-rs/tui/src/chatwidget/tests.rs | 3 +- codex-rs/tui/src/cli.rs | 4 ++ codex-rs/tui/src/lib.rs | 2 + codex-rs/tui/src/slash_command.rs | 4 +- 7 files changed, 43 insertions(+), 39 deletions(-) diff --git a/codex-rs/cli/src/main.rs b/codex-rs/cli/src/main.rs index d620b8ab35..1f22f61ae7 100644 --- a/codex-rs/cli/src/main.rs +++ b/codex-rs/cli/src/main.rs @@ -833,6 +833,9 @@ fn merge_interactive_cli_flags(interactive: &mut TuiCli, subcommand_cli: TuiCli) if subcommand_cli.full_auto { interactive.full_auto = true; } + if subcommand_cli.immortal { + interactive.immortal = true; + } if subcommand_cli.dangerously_bypass_approvals_and_sandbox { interactive.dangerously_bypass_approvals_and_sandbox = true; } diff --git a/codex-rs/tui/src/app.rs b/codex-rs/tui/src/app.rs index c42b8f39d2..d0d6c528f5 100644 --- a/codex-rs/tui/src/app.rs +++ b/codex-rs/tui/src/app.rs @@ -402,6 +402,7 @@ impl App { is_first_run: false, model: Some(self.chat_widget.current_model().to_string()), otel_manager: self.otel_manager.clone(), + immortality_enable_after_first_turn: false, } } @@ -423,6 +424,7 @@ impl App { active_profile: Option, initial_prompt: Option, initial_images: Vec, + immortal: bool, session_selection: SessionSelection, feedback: codex_feedback::CodexFeedback, is_first_run: bool, @@ -499,6 +501,7 @@ impl App { is_first_run, model: Some(model.clone()), otel_manager: otel_manager.clone(), + immortality_enable_after_first_turn: immortal, }; ChatWidget::new(init, thread_manager.clone()) } @@ -527,6 +530,7 @@ impl App { is_first_run, model: config.model.clone(), otel_manager: otel_manager.clone(), + immortality_enable_after_first_turn: immortal, }; ChatWidget::new_from_existing(init, resumed.thread, resumed.session_configured) } @@ -555,6 +559,7 @@ impl App { is_first_run, model: config.model.clone(), otel_manager: otel_manager.clone(), + immortality_enable_after_first_turn: immortal, }; ChatWidget::new_from_existing(init, forked.thread, forked.session_configured) } @@ -757,6 +762,7 @@ impl App { is_first_run: false, model: Some(model), otel_manager: self.otel_manager.clone(), + immortality_enable_after_first_turn: false, }; self.chat_widget = ChatWidget::new(init, self.server.clone()); if let Some(summary) = summary { diff --git a/codex-rs/tui/src/chatwidget.rs b/codex-rs/tui/src/chatwidget.rs index 11ff78e612..94fcf72863 100644 --- a/codex-rs/tui/src/chatwidget.rs +++ b/codex-rs/tui/src/chatwidget.rs @@ -118,7 +118,7 @@ use tokio::sync::mpsc::UnboundedSender; use tokio::task::JoinHandle; use tracing::debug; -const IMMORTALITY_FOLLOWUP_PROMPT: &str = "Write the next user message to keep the conversation going. Speak as the user in first person. Be concise and specific. Output only the user message with no quotes or extra commentary."; +const IMMORTALITY_FOLLOWUP_PROMPT: &str = "Please keep going. Work as autonomously as possible. Ask the user for clarification, but don't block on their response. Worst case, if you have nothing useful to do, poll every 30s for a response from the user for 5 minutes and then end your current turn."; const DEFAULT_MODEL_DISPLAY_NAME: &str = "loading"; @@ -366,6 +366,7 @@ pub(crate) struct ChatWidgetInit { pub(crate) is_first_run: bool, pub(crate) model: Option, pub(crate) otel_manager: OtelManager, + pub(crate) immortality_enable_after_first_turn: bool, } #[derive(Default)] @@ -484,10 +485,10 @@ pub(crate) struct ChatWidget { is_review_mode: bool, // Snapshot of token usage to restore after review mode exits. pre_review_token_info: Option>, - /// Auto-continue by submitting "Keep going" after each completed turn. + /// Auto-continue by submitting a follow-up after each completed turn. immortality_mode: bool, - /// True when the auto-followup generation turn is in-flight. - immortality_generation_inflight: bool, + /// Enable immortality after the next completed turn (used by --immortal). + immortality_enable_after_first_turn: bool, // Whether the next streamed assistant content should be preceded by a final message separator. // // This is set whenever we insert a visible history cell that conceptually belongs to a turn. @@ -890,9 +891,20 @@ impl ChatWidget { self.clear_unified_exec_processes(); self.request_redraw(); + if self.immortality_enable_after_first_turn && !self.is_review_mode { + self.immortality_enable_after_first_turn = false; + if !self.immortality_mode { + self.immortality_mode = true; + self.add_info_message( + "Immortality mode enabled (from --immortal).".to_string(), + Some("Press Ctrl+C to interrupt.".to_string()), + ); + } + } + // If there is a queued user message, send exactly one now to begin the next turn. self.maybe_send_next_queued_input(); - self.handle_immortality_followup(last_agent_message.as_deref()); + self.maybe_send_immortality_prompt(); // Emit a notification when the turn completes (suppressed if focused). self.notify(Notification::AgentTurnComplete { response: last_agent_message.unwrap_or_default(), @@ -1122,7 +1134,7 @@ impl ChatWidget { if reason == TurnAbortReason::Interrupted && self.immortality_mode { self.immortality_mode = false; - self.immortality_generation_inflight = false; + self.immortality_enable_after_first_turn = false; self.add_info_message( "Immortality mode disabled after interrupt.".to_string(), None, @@ -1853,6 +1865,7 @@ impl ChatWidget { is_first_run, model, otel_manager, + immortality_enable_after_first_turn, } = common; let model = model.filter(|m| !m.trim().is_empty()); let mut config = config; @@ -1939,7 +1952,7 @@ impl ChatWidget { is_review_mode: false, pre_review_token_info: None, immortality_mode: false, - immortality_generation_inflight: false, + immortality_enable_after_first_turn, needs_final_message_separator: false, had_work_activity: false, last_separator_elapsed_secs: None, @@ -1978,6 +1991,7 @@ impl ChatWidget { feedback, model, otel_manager, + immortality_enable_after_first_turn, .. } = common; let model = model.filter(|m| !m.trim().is_empty()); @@ -2060,7 +2074,7 @@ impl ChatWidget { is_review_mode: false, pre_review_token_info: None, immortality_mode: false, - immortality_generation_inflight: false, + immortality_enable_after_first_turn, needs_final_message_separator: false, had_work_activity: false, last_separator_elapsed_secs: None, @@ -2415,9 +2429,6 @@ impl ChatWidget { } SlashCommand::Immortality => { self.immortality_mode = !self.immortality_mode; - if !self.immortality_mode { - self.immortality_generation_inflight = false; - } let status = if self.immortality_mode { "enabled" } else { @@ -2425,7 +2436,7 @@ impl ChatWidget { }; self.add_info_message( format!( - "Immortality mode {status}. I will ask the model to generate the next user followup after each turn until interrupted.", + "Immortality mode {status}. I will keep the agent running after each turn until interrupted.", ), Some("Press Ctrl+C to interrupt.".to_string()), ); @@ -2970,35 +2981,14 @@ impl ChatWidget { self.refresh_queued_user_messages(); } - fn handle_immortality_followup(&mut self, last_agent_message: Option<&str>) { + fn maybe_send_immortality_prompt(&mut self) { if !self.immortality_mode || self.is_review_mode { return; } if self.bottom_pane.is_task_running() || !self.queued_user_messages.is_empty() { return; } - if self.immortality_generation_inflight { - self.immortality_generation_inflight = false; - let followup = last_agent_message.unwrap_or_default().trim(); - if followup.is_empty() { - self.add_info_message( - "Immortality mode: model returned an empty followup; retrying.".to_string(), - None, - ); - self.maybe_send_immortality_prompt(); - return; - } - self.submit_user_message(UserMessage::from(followup)); - return; - } - self.maybe_send_immortality_prompt(); - } - - fn maybe_send_immortality_prompt(&mut self) { - if self.immortality_mode && !self.immortality_generation_inflight { - self.immortality_generation_inflight = true; - self.submit_user_message(UserMessage::from(IMMORTALITY_FOLLOWUP_PROMPT)); - } + self.submit_user_message(UserMessage::from(IMMORTALITY_FOLLOWUP_PROMPT)); } /// Rebuild and update the queued user messages from the current queue. diff --git a/codex-rs/tui/src/chatwidget/tests.rs b/codex-rs/tui/src/chatwidget/tests.rs index 9a683bdbf9..82b2b6aa3c 100644 --- a/codex-rs/tui/src/chatwidget/tests.rs +++ b/codex-rs/tui/src/chatwidget/tests.rs @@ -720,6 +720,7 @@ async fn helpers_are_available_and_do_not_panic() { is_first_run: true, model: Some(resolved_model), otel_manager, + immortality_enable_after_first_turn: false, }; let mut w = ChatWidget::new(init, thread_manager); // Basic construction sanity. @@ -839,7 +840,7 @@ async fn make_chatwidget_manual( is_review_mode: false, pre_review_token_info: None, immortality_mode: false, - immortality_generation_inflight: false, + immortality_enable_after_first_turn: false, needs_final_message_separator: false, had_work_activity: false, last_separator_elapsed_secs: None, diff --git a/codex-rs/tui/src/cli.rs b/codex-rs/tui/src/cli.rs index 8308f1c2a6..f2550dafef 100644 --- a/codex-rs/tui/src/cli.rs +++ b/codex-rs/tui/src/cli.rs @@ -110,6 +110,10 @@ pub struct Cli { #[arg(long = "no-alt-screen", default_value_t = false)] pub no_alt_screen: bool, + /// Enable immortality mode after the first user turn. + #[arg(long = "immortal", default_value_t = false)] + pub immortal: bool, + #[clap(skip)] pub config_overrides: CliConfigOverrides, } diff --git a/codex-rs/tui/src/lib.rs b/codex-rs/tui/src/lib.rs index cac1b12e06..3808fa6a2d 100644 --- a/codex-rs/tui/src/lib.rs +++ b/codex-rs/tui/src/lib.rs @@ -595,6 +595,7 @@ async fn run_ratatui_app( prompt, images, no_alt_screen, + immortal, .. } = cli; @@ -608,6 +609,7 @@ async fn run_ratatui_app( active_profile, prompt, images, + immortal, session_selection, feedback, should_show_trust_screen, // Proxy to: is it a first run in this directory? diff --git a/codex-rs/tui/src/slash_command.rs b/codex-rs/tui/src/slash_command.rs index 3cc7cc2d2b..442330d725 100644 --- a/codex-rs/tui/src/slash_command.rs +++ b/codex-rs/tui/src/slash_command.rs @@ -49,9 +49,7 @@ impl SlashCommand { SlashCommand::New => "start a new chat during a conversation", SlashCommand::Init => "create an AGENTS.md file with instructions for Codex", SlashCommand::Compact => "summarize conversation to prevent hitting the context limit", - SlashCommand::Immortality => { - "auto-continue by generating the next user message each turn" - } + SlashCommand::Immortality => "auto-continue after each turn", SlashCommand::Review => "review my current changes and find issues", SlashCommand::Resume => "resume a saved chat", SlashCommand::Fork => "fork the current chat",