Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Delay automatic recaps and compact their TUI layout (#45089)

Felipe Couryf16c2237a561
diff --git a/codex-rs/tui/src/app/event_dispatch.rs b/codex-rs/tui/src/app/event_dispatch.rs
index 1d04512a2e..5ff2b60dc0 100644
--- a/codex-rs/tui/src/app/event_dispatch.rs
+++ b/codex-rs/tui/src/app/event_dispatch.rs
@@ -2950,7 +2950,9 @@ impl App {
AppEvent::CheckRecap { thread_id } => {
if self.current_displayed_thread_id() == Some(thread_id)
&& !self.chat_widget.is_user_turn_pending_or_running()
- && self.recap.should_generate(std::time::Instant::now())
+ && self
+ .recap
+ .should_generate(tokio::time::Instant::now().into_std())
{
self.request_recap(app_server, thread_id, RecapTrigger::Automatic);
}
diff --git a/codex-rs/tui/src/app/recap.rs b/codex-rs/tui/src/app/recap.rs
index d7d50b3e51..e862672be7 100644
--- a/codex-rs/tui/src/app/recap.rs
+++ b/codex-rs/tui/src/app/recap.rs
@@ -33,7 +33,7 @@ use uuid::Uuid;
const MIN_COMPLETED_TURNS: usize = 3;
const MIN_TURNS_BETWEEN_RECAPS: usize = 2;
-pub(super) const RECAP_DELAY: Duration = Duration::from_secs(/*secs*/ 3 * 60);
+pub(super) const RECAP_DELAY: Duration = Duration::from_secs(/*secs*/ 30 * 60);
const RECAP_HISTORY_MAX_TURNS: usize = 8;
const RECAP_MAX_CHARS: usize = 320;
const RECAP_RETRY_DELAY: Duration = Duration::from_secs(/*secs*/ 30);
@@ -196,10 +196,11 @@ impl App {
return;
};
let delay = deadline.saturating_duration_since(now);
+ let sleep = tokio::time::sleep(delay);
let app_event_tx = self.app_event_tx.clone();
self.recap.scheduled_check = Some(tokio::spawn(async move {
- tokio::time::sleep(delay).await;
+ sleep.await;
app_event_tx.send(AppEvent::CheckRecap { thread_id });
}));
}
@@ -357,7 +358,9 @@ impl App {
};
let trigger_is_eligible = match trigger {
- RecapTrigger::Automatic => self.recap.should_generate(Instant::now()),
+ RecapTrigger::Automatic => self
+ .recap
+ .should_generate(tokio::time::Instant::now().into_std()),
RecapTrigger::Manual => true,
};
diff --git a/codex-rs/tui/src/app/recap_tests.rs b/codex-rs/tui/src/app/recap_tests.rs
index 8f03cabe41..3bb9506f6a 100644
--- a/codex-rs/tui/src/app/recap_tests.rs
+++ b/codex-rs/tui/src/app/recap_tests.rs
@@ -21,10 +21,13 @@ use crate::history_cell::PlainHistoryCell;
use crate::history_cell::ThreadRecapHistoryCell;
use crate::history_cell::ThreadRecapLoadingCell;
use crate::history_cell::UserHistoryCell;
+use crate::line_truncation::line_width;
use codex_app_server_protocol::Turn;
use codex_app_server_protocol::TurnStatus;
use codex_protocol::ThreadId;
use pretty_assertions::assert_eq;
+use ratatui::style::Modifier;
+use ratatui::style::Stylize;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
@@ -316,7 +319,7 @@ fn regaining_focus_preserves_only_manual_in_flight_request() {
}
#[tokio::test(flavor = "current_thread", start_paused = true)]
-async fn scheduled_check_fires_at_recap_deadline() {
+async fn scheduled_check_fires_after_thirty_minutes() {
let thread_id = ThreadId::new();
let now = Instant::now();
let mut app = make_test_app().await;
@@ -330,7 +333,7 @@ async fn scheduled_check_fires_at_recap_deadline() {
app.schedule_recap_check(thread_id, now);
tokio::task::yield_now().await;
- tokio::time::advance(RECAP_DELAY - Duration::from_secs(/*secs*/ 1)).await;
+ tokio::time::advance(Duration::from_secs(/*secs*/ 30 * 60 - 1)).await;
tokio::task::yield_now().await;
assert!(matches!(event_rx.try_recv(), Err(TryRecvError::Empty)));
@@ -518,20 +521,28 @@ fn restored_history_never_reduces_observed_completed_turns() {
}
#[test]
-fn recap_history_cell_uses_labeled_checkpoint_layout() {
+fn recap_history_cell_uses_hanging_indent_and_right_padding() {
let cell =
ThreadRecapHistoryCell::new("Automatic recaps stay compact on wide terminals.".to_string());
- let rendered = cell
- .display_lines(/*width*/ 64)
+ let lines = cell.display_lines(/*width*/ 56);
+ assert!(
+ lines
+ .iter()
+ .all(|line| line.style.add_modifier.contains(Modifier::ITALIC))
+ );
+ assert_eq!(
+ &lines[0].spans[..3],
+ &[" ".into(), "↳ ".dim(), "Recap: ".bold()],
+ );
+ let rendered = lines
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join("\n");
insta::assert_snapshot!(rendered, @r"
- ─ Conversation recap ───────────────────────────────────────────
-
- Automatic recaps stay compact on wide terminals.
+ ↳ Recap: Automatic recaps stay compact on wide
+ terminals.
");
}
@@ -548,11 +559,117 @@ fn recap_history_cell_wraps_in_narrow_terminals() {
.join("\n");
insta::assert_snapshot!(rendered, @r"
- ─ Conversation recap ───────────
+ ↳ Recap: Keep conversation
+ recaps readable in
+ narrow terminals.
+ ");
+}
+
+#[test]
+fn recap_history_cell_preserves_unicode_and_url_tokens() {
+ let cell = ThreadRecapHistoryCell::new(
+ "Résumé ready. See https://example.com/review/42 日本語 details.".to_string(),
+ );
+ let rendered = cell
+ .display_lines(/*width*/ 48)
+ .iter()
+ .map(ToString::to_string)
+ .collect::<Vec<_>>()
+ .join("\n");
+
+ insta::assert_snapshot!(rendered, @r"
+ ↳ Recap: Résumé ready. See
+ https://example.com/review/42
+ 日本語 details.
+ ");
+}
+
+#[test]
+fn recap_history_cell_wraps_long_urls_in_narrow_terminals() {
+ let cell = ThreadRecapHistoryCell::new(
+ "The café review is paused with the draft ready at https://example.com/review/42. 日本語"
+ .to_string(),
+ );
+ let lines = cell.display_lines(/*width*/ 32);
+ assert!(lines.iter().all(|line| line_width(line) <= 30));
+ assert!(
+ lines
+ .iter()
+ .skip(/*n*/ 1)
+ .all(|line| line.to_string().starts_with(" "))
+ );
+ assert!(
+ lines
+ .iter()
+ .all(|line| line.style.add_modifier.contains(Modifier::ITALIC))
+ );
+ let rendered = lines
+ .iter()
+ .map(ToString::to_string)
+ .collect::<Vec<_>>()
+ .join("\n");
+
+ insta::assert_snapshot!(rendered, @"
+ ↳ Recap: The café review is
+ paused with the
+ draft ready at
+ https://example.com
+ /review/42. 日本語
+ ");
+}
+
+#[test]
+fn recap_history_cell_splits_only_urls_wider_than_the_text_column() {
+ for (url, expected) in [
+ (
+ "https://example.com",
+ vec![" ↳ Recap: https://example.com"],
+ ),
+ (
+ "https://example.com/",
+ vec![" ↳ Recap: https://example.com", " /"],
+ ),
+ (
+ "https://a.co/ガガガガガガ",
+ vec![" ↳ Recap: https://a.co/ガガガ", " ガガガ"],
+ ),
+ ] {
+ let cell = ThreadRecapHistoryCell::new(url.to_string());
+ let lines = cell.display_lines(/*width*/ 32);
+ assert!(lines.iter().all(|line| line_width(line) <= 30));
+ assert_eq!(
+ lines.iter().map(ToString::to_string).collect::<Vec<_>>(),
+ expected
+ );
+ let displayed_url = lines
+ .iter()
+ .map(|line| line.to_string().chars().skip(/*n*/ 11).collect::<String>())
+ .collect::<String>();
+ assert_eq!(displayed_url, url);
+ }
+}
+
+#[test]
+fn recap_history_cell_uses_available_space_below_indent_width() {
+ let cell = ThreadRecapHistoryCell::new(
+ "Resume this task.\nhttps://example.com/review/42.".to_string(),
+ );
+ let rendered = cell
+ .display_lines(/*width*/ 12)
+ .iter()
+ .map(ToString::to_string)
+ .collect::<Vec<_>>()
+ .join("\n");
- Keep conversation recaps
- readable in narrow terminals.
+ insta::assert_snapshot!(rendered, @r"
+ ↳ Recap:
+ Resume
+ this task.
+ https://ex
+ ample.com/
+ review/42.
");
+ assert_eq!(cell.display_lines(/*width*/ 0), Vec::new());
}
#[test]
@@ -590,10 +707,8 @@ fn recap_history_cell_preserves_explicit_line_breaks() {
.join("\n");
insta::assert_snapshot!(displayed, @r"
- ─ Conversation recap ───────────────────────────
-
- Finished the parser.
- Next: run the focused tests.
+ ↳ Recap: Finished the parser.
+ Next: run the focused tests.
");
insta::assert_snapshot!(raw, @r"
Conversation recap
diff --git a/codex-rs/tui/src/app/tests/active_reconnect_tests.rs b/codex-rs/tui/src/app/tests/active_reconnect_tests.rs
index ba6e507178..3a457de0cc 100644
--- a/codex-rs/tui/src/app/tests/active_reconnect_tests.rs
+++ b/codex-rs/tui/src/app/tests/active_reconnect_tests.rs
@@ -231,7 +231,7 @@ async fn reconnect_restores_history_permissions_and_keeps_old_input_paused() ->
&mut app.rate_limit_hard_stop_generation,
)
.unwrap();
- let before_disconnect = Instant::now() - Duration::from_secs(/*secs*/ 300);
+ let before_disconnect = Instant::now();
app.recap.note_focus_lost(before_disconnect);
for _ in 0..3 {
app.recap
@@ -273,6 +273,10 @@ async fn reconnect_restores_history_permissions_and_keeps_old_input_paused() ->
!app.agent_navigation
.finish_picker_refresh(id, stale_picker_refresh)
);
+ // Let the rebound timer become due without depending on machine uptime.
+ tokio::time::pause();
+ tokio::time::advance(recap::RECAP_DELAY).await;
+ tokio::time::resume();
let mut deferred = Vec::new();
tokio::time::timeout(Duration::from_secs(/*secs*/ 5), async {
loop {
diff --git a/codex-rs/tui/src/app/tests/recap_generation_tests.rs b/codex-rs/tui/src/app/tests/recap_generation_tests.rs
index b194702f1f..bf56a06e03 100644
--- a/codex-rs/tui/src/app/tests/recap_generation_tests.rs
+++ b/codex-rs/tui/src/app/tests/recap_generation_tests.rs
@@ -34,7 +34,7 @@ fn render_chat_widget(app: &App) -> String {
.join("\n")
}
-fn prepare_eligible_recap(app: &mut App, thread_id: ThreadId) {
+async fn prepare_eligible_recap(app: &mut App, thread_id: ThreadId) {
app.active_thread_id = Some(thread_id);
app.transcript_cells
.push(Arc::new(crate::history_cell::UserHistoryCell {
@@ -44,15 +44,19 @@ fn prepare_eligible_recap(app: &mut App, thread_id: ThreadId) {
local_image_paths: Vec::new(),
remote_image_urls: Vec::new(),
}));
- let ready_at = Instant::now() - recap::RECAP_DELAY;
+ let ready_at = Instant::now();
app.recap.note_focus_lost(ready_at);
for _ in 0..3 {
app.recap
.note_turn_finished(&TurnStatus::Completed, ready_at);
}
+ // Advance the scheduler's clock instead of subtracting from the host's uptime.
+ tokio::time::pause();
+ tokio::time::advance(recap::RECAP_DELAY).await;
+ tokio::time::resume();
}
-#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+#[tokio::test]
async fn recap_generation_uses_bounded_structured_request_and_inserts_result() -> Result<()> {
let chunks = [
ev_response_created("recap-response"),
@@ -113,7 +117,7 @@ stream_max_retries = 0
.await?;
while app_event_rx.try_recv().is_ok() {}
- prepare_eligible_recap(&mut app, thread_id);
+ prepare_eligible_recap(&mut app, thread_id).await;
app.handle_event(
&mut tui,
@@ -305,7 +309,7 @@ async fn manual_recap_works_when_auto_recap_disabled() -> Result<()> {
async fn auto_recap_opt_out_blocks_requests_and_cleans_up_pending_start() -> Result<()> {
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
let thread_id = ThreadId::new();
- prepare_eligible_recap(&mut app, thread_id);
+ prepare_eligible_recap(&mut app, thread_id).await;
let (mut app_server, requests, proxy) = start_recording_remote_app_server(&app.config).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
app.local_settings.tui.auto_recap = false;
@@ -453,7 +457,7 @@ async fn temporary_recap_threads_disable_memories_and_remote_mcp_servers() -> Re
async fn recap_check_rejects_a_non_displayed_thread() -> Result<()> {
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
let displayed_thread_id = ThreadId::new();
- prepare_eligible_recap(&mut app, displayed_thread_id);
+ prepare_eligible_recap(&mut app, displayed_thread_id).await;
while app_event_rx.try_recv().is_ok() {}
let (mut app_server, requests, proxy) = start_recording_remote_app_server(&app.config).await?;
let mut tui = crate::tui::test_support::make_test_tui()?;
@@ -483,7 +487,7 @@ async fn recap_check_rejects_a_non_displayed_thread() -> Result<()> {
async fn recap_check_rejects_a_running_turn() -> Result<()> {
let (mut app, mut app_event_rx, _op_rx) = make_test_app_with_channels().await;
let thread_id = ThreadId::new();
- prepare_eligible_recap(&mut app, thread_id);
+ prepare_eligible_recap(&mut app, thread_id).await;
app.chat_widget
.handle_thread_session(test_thread_session(thread_id, app.config.cwd.to_path_buf()));
app.chat_widget.handle_server_notification(
diff --git a/codex-rs/tui/src/history_cell/notices.rs b/codex-rs/tui/src/history_cell/notices.rs
index 82e62f07d5..b05c4e36ae 100644
--- a/codex-rs/tui/src/history_cell/notices.rs
+++ b/codex-rs/tui/src/history_cell/notices.rs
@@ -1,6 +1,9 @@
//! Informational, warning, update, and policy notice history cells.
use super::*;
+use crate::line_truncation::line_width;
+use crate::wrapping::url_preserving_wrap_options;
+use crate::wrapping::word_wrap_line;
#[cfg_attr(not(test), allow(dead_code))]
const RECAP_HEADING: &str = "Conversation recap";
@@ -325,39 +328,43 @@ impl ThreadRecapHistoryCell {
impl HistoryCell for ThreadRecapHistoryCell {
fn display_lines(&self, width: u16) -> Vec<Line<'static>> {
- let width = usize::from(width);
- let mut remaining_width = width;
- let mut heading = Vec::new();
-
- if remaining_width > 0 {
- heading.push("─".dim());
- remaining_width -= 1;
- }
- if remaining_width > 0 {
- heading.push(" ".dim());
- remaining_width -= 1;
+ if width == 0 {
+ return Vec::new();
}
- let (visible_heading, _suffix, heading_width) =
- take_prefix_by_width(RECAP_HEADING, remaining_width);
- if !visible_heading.is_empty() {
- heading.push(visible_heading.bold());
- remaining_width -= heading_width;
- }
- if remaining_width > 0 {
- heading.push(" ".dim());
- remaining_width -= 1;
- }
- if remaining_width > 0 {
- heading.push("─".repeat(remaining_width).dim());
+ let wrap_width = usize::from(width.saturating_sub(/*rhs*/ 2).max(/*other*/ 1));
+ let mut body = raw_lines_from_source(&self.recap)
+ .into_iter()
+ .map(Line::italic)
+ .collect::<Vec<_>>();
+ let prefix = Line::from(vec![" ".into(), "↳ ".dim(), "Recap: ".bold()]).italic();
+ let mut options = if wrap_width <= prefix.width() {
+ // Keep the text readable when the terminal cannot fit the hanging indent.
+ body.insert(
+ /*index*/ 0,
+ Line::from(vec!["↳ ".dim(), "Recap:".bold()]).italic(),
+ );
+ RtOptions::new(wrap_width)
+ } else {
+ RtOptions::new(wrap_width)
+ .subsequent_indent(" ".repeat(prefix.width()).into())
+ .initial_indent(prefix)
+ };
+ let mut lines = Vec::new();
+ for line in body {
+ let mut wrapped = adaptive_wrap_line(&line, options.clone());
+ if wrapped.iter().any(|line| line_width(line) > wrap_width) {
+ // Terminal autowrap loses the hanging indent. Rewrap the source line so only
+ // oversized tokens split, without applying the recap prefix a second time.
+ wrapped = word_wrap_line(
+ &line,
+ url_preserving_wrap_options(options.clone())
+ .break_words(/*break_words*/ true),
+ );
+ }
+ push_owned_lines(&wrapped, &mut lines);
+ options.initial_indent = options.subsequent_indent.clone();
}
-
- let wrap_width = width.saturating_sub(2).max(1);
- let body = raw_lines_from_source(&self.recap);
- let wrapped = adaptive_wrap_lines(body, RtOptions::new(wrap_width));
- let mut lines = vec![heading.into(), Line::default()];
- lines.extend(prefix_lines(wrapped, " ".into(), " ".into()));
-
lines
}
diff --git a/codex-rs/tui/src/temporary_structured_request.rs b/codex-rs/tui/src/temporary_structured_request.rs
index da1926a34b..f421426321 100644
--- a/codex-rs/tui/src/temporary_structured_request.rs
+++ b/codex-rs/tui/src/temporary_structured_request.rs
@@ -84,10 +84,6 @@ pub(crate) async fn start_temporary_thread(
("features.view_image".to_string(), false.into()),
("orchestrator.skills.enabled".to_string(), false.into()),
("skills.include_instructions".to_string(), false.into()),
- (
- "token_budget.use_history_notes_extension".to_string(),
- false.into(),
- ),
(
"tools.experimental_request_user_input.enabled".to_string(),
false.into(),