Skip to content

Commit 1ad261d

Browse files
Changed default wrap algorithm from OptimalFit to FirstFit (#7960)
Codex identified this as the cause of a reported hang: #7822. Apparently, the wrapping algorithm we're using has known issues and bad worst-case behaviors when OptimalFit is used on certain strings. It recommended switching to FirstFit instead.
1 parent 6ec2831 commit 1ad261d

File tree

6 files changed

+20
-30
lines changed

6 files changed

+20
-30
lines changed

codex-rs/tui/src/history_cell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,8 +1664,8 @@ mod tests {
16641664
assert_eq!(
16651665
rendered,
16661666
vec![
1667-
"✔ You approved codex".to_string(),
1668-
" to run echo something".to_string(),
1667+
"✔ You approved codex to".to_string(),
1668+
" run echo something".to_string(),
16691669
" really long to ensure".to_string(),
16701670
" wrapping happens this".to_string(),
16711671
" time".to_string(),

codex-rs/tui/src/markdown_render.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ mod tests {
595595
lines,
596596
vec![
597597
"- outer item with".to_string(),
598-
" several words".to_string(),
599-
" to wrap".to_string(),
598+
" several words to".to_string(),
599+
" wrap".to_string(),
600600
" - inner item".to_string(),
601601
" that also".to_string(),
602602
" needs wrapping".to_string(),

codex-rs/tui/src/wrapping.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use ratatui::text::Span;
33
use std::borrow::Cow;
44
use std::ops::Range;
55
use textwrap::Options;
6-
use textwrap::wrap_algorithms::Penalties;
76

87
use crate::render::line_utils::push_owned_lines;
98

@@ -92,11 +91,7 @@ impl<'a> RtOptions<'a> {
9291
subsequent_indent: Line::default(),
9392
break_words: true,
9493
word_separator: textwrap::WordSeparator::new(),
95-
wrap_algorithm: textwrap::WrapAlgorithm::OptimalFit(Penalties {
96-
// ~infinite overflow penalty, we never want to overflow a line.
97-
overflow_penalty: usize::MAX / 4,
98-
..Default::default()
99-
}),
94+
wrap_algorithm: textwrap::WrapAlgorithm::FirstFit,
10095
word_splitter: textwrap::WordSplitter::HyphenSplitter,
10196
}
10297
}
@@ -641,11 +636,11 @@ mod tests {
641636
let joined: String = wrapped.iter().map(ToString::to_string).join("\n");
642637
assert_eq!(
643638
joined,
644-
r#"Years passed, and Willowmere thrived
645-
in peace and friendship. Mira’s herb
646-
garden flourished with both ordinary and
647-
enchanted plants, and travelers spoke
648-
of the kindness of the woman who tended
639+
r#"Years passed, and Willowmere thrived in
640+
peace and friendship. Mira’s herb garden
641+
flourished with both ordinary and
642+
enchanted plants, and travelers spoke of
643+
the kindness of the woman who tended
649644
them."#
650645
);
651646
}

codex-rs/tui2/src/history_cell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,8 +1664,8 @@ mod tests {
16641664
assert_eq!(
16651665
rendered,
16661666
vec![
1667-
"✔ You approved codex".to_string(),
1668-
" to run echo something".to_string(),
1667+
"✔ You approved codex to".to_string(),
1668+
" run echo something".to_string(),
16691669
" really long to ensure".to_string(),
16701670
" wrapping happens this".to_string(),
16711671
" time".to_string(),

codex-rs/tui2/src/markdown_render.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ mod tests {
595595
lines,
596596
vec![
597597
"- outer item with".to_string(),
598-
" several words".to_string(),
599-
" to wrap".to_string(),
598+
" several words to".to_string(),
599+
" wrap".to_string(),
600600
" - inner item".to_string(),
601601
" that also".to_string(),
602602
" needs wrapping".to_string(),

codex-rs/tui2/src/wrapping.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use ratatui::text::Span;
33
use std::borrow::Cow;
44
use std::ops::Range;
55
use textwrap::Options;
6-
use textwrap::wrap_algorithms::Penalties;
76

87
use crate::render::line_utils::push_owned_lines;
98

@@ -92,11 +91,7 @@ impl<'a> RtOptions<'a> {
9291
subsequent_indent: Line::default(),
9392
break_words: true,
9493
word_separator: textwrap::WordSeparator::new(),
95-
wrap_algorithm: textwrap::WrapAlgorithm::OptimalFit(Penalties {
96-
// ~infinite overflow penalty, we never want to overflow a line.
97-
overflow_penalty: usize::MAX / 4,
98-
..Default::default()
99-
}),
94+
wrap_algorithm: textwrap::WrapAlgorithm::FirstFit,
10095
word_splitter: textwrap::WordSplitter::HyphenSplitter,
10196
}
10297
}
@@ -641,11 +636,11 @@ mod tests {
641636
let joined: String = wrapped.iter().map(ToString::to_string).join("\n");
642637
assert_eq!(
643638
joined,
644-
r#"Years passed, and Willowmere thrived
645-
in peace and friendship. Mira’s herb
646-
garden flourished with both ordinary and
647-
enchanted plants, and travelers spoke
648-
of the kindness of the woman who tended
639+
r#"Years passed, and Willowmere thrived in
640+
peace and friendship. Mira’s herb garden
641+
flourished with both ordinary and
642+
enchanted plants, and travelers spoke of
643+
the kindness of the woman who tended
649644
them."#
650645
);
651646
}

0 commit comments

Comments
 (0)