koda-cli 0.6.2

A high-performance AI coding agent for macOS and Linux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Foundation work for #1116/#1175 — see the equivalent header note in
// composer/key_hint.rs for the full rationale. The blanket allow goes away
// when PR 2 wires up the consumers.

//! Reduced port of codex's `codex-rs/tui/src/wrapping.rs`.
//!
//! ## Provenance
//!
//! Selected helpers ported from `codex-rs/tui/src/wrapping.rs` at upstream
//! commit `d55479488e125ef7a0a8584505d839a22eaf6204` (codex `main` as of
//! 2026-05-01).
//!
//! Verified byte-identical between this SHA and our previous reference
//! `7e8594fc198615068018b198ab86a9ae0a541dff` (`git diff` returns 0
//! lines), so the re-stamp is a pure attribution change.
//!
//! Original work: Copyright (c) OpenAI / codex contributors,
//! licensed under the Apache License, Version 2.0.
//! See `LICENSES/codex-APACHE-2.0` for the full license text.
//!
//! ## What was kept vs dropped
//!
//! - **Kept:** [`wrap_ranges`] and its helper [`map_owned_wrapped_line_to_range`].
//!   These are the only wrapping primitives [`super::textarea::TextArea`]
//!   needs to drive its cursor-position math.
//! - **Dropped:** all the URL-aware adaptive wrapping (~1,300 LoC of
//!   codex's `wrapping.rs`). koda's history-render path uses different
//!   wrapping logic (`koda-cli/src/history_render.rs`) and the URL
//!   helpers there are not needed inside the composer's textarea
//!   (which renders user-typed input, not agent output). If we later
//!   want URL-aware wrapping in the composer, we can port more.
//! - **Dropped:** `wrap_ranges_trim` (used by codex but not by the
//!   textarea sub-module; would be dead code here).

use std::ops::Range;
use textwrap::Options;
use textwrap::core::Word;
use textwrap::word_splitters::split_words;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

/// Halfwidth katakana voiced and semi-voiced sound marks.
const HALFWIDTH_SOUND_MARKS: [char; 2] = ['\u{FF9E}', '\u{FF9F}'];

/// Display width as Ratatui draws it (upstream openai/codex#35962).
///
/// Unicode gives the halfwidth sound marks width 0, but Ratatui draws each
/// in its own cell; counting them keeps wrapping, the cursor and the mask
/// in step with what is on screen.
pub(crate) fn display_width(text: &str) -> usize {
    text.width() + text.matches(HALFWIDTH_SOUND_MARKS).count()
}

/// Projected text keeps source-offset lookup separate from legal grapheme
/// split points.
struct ProjectedText {
    text: String,
    source_boundaries: Vec<(usize, usize)>,
    grapheme_boundaries: Vec<usize>,
}

/// Replaces halfwidth sound-mark graphemes with equally wide, textwrap-safe
/// placeholders, since textwrap measures them as width 0.
///
/// Source boundaries recover original byte offsets, while grapheme
/// boundaries keep placeholders indivisible and preserve leading whitespace
/// as a wrapping opportunity.
fn project_halfwidth_sound_marks(text: &str) -> Option<ProjectedText> {
    if !text.contains(HALFWIDTH_SOUND_MARKS) {
        return None;
    }

    let mut projected = String::with_capacity(text.len());
    let mut source_boundaries = vec![(0, 0)];
    let mut grapheme_boundaries = vec![0];
    for (source_start, grapheme) in text.grapheme_indices(/*is_extended*/ true) {
        if grapheme.contains(HALFWIDTH_SOUND_MARKS) {
            let source_end = source_start + grapheme.len();
            let content_start = grapheme
                .find(|ch: char| !ch.is_whitespace())
                .unwrap_or(grapheme.len());
            let (whitespace, content) = grapheme.split_at(content_start);
            for (offset, ch) in whitespace.char_indices() {
                projected.push(ch);
                source_boundaries.push((projected.len(), source_start + offset + ch.len_utf8()));
                grapheme_boundaries.push(projected.len());
            }

            // Two-column and one-column placeholders joined by WORD JOINER so
            // textwrap never breaks inside one source grapheme.
            let width = display_width(content);
            let projected_start = projected.len();
            for _ in 0..width / 2 {
                if projected.len() > projected_start {
                    projected.push('\u{2060}');
                }
                projected.push('界');
                source_boundaries.push((projected.len(), source_end));
            }
            if width % 2 == 1 {
                if projected.len() > projected_start {
                    projected.push('\u{2060}');
                }
                projected.push('a');
                source_boundaries.push((projected.len(), source_end));
            }
        } else {
            for (offset, ch) in grapheme.char_indices() {
                projected.push(ch);
                source_boundaries.push((projected.len(), source_start + offset + ch.len_utf8()));
            }
        }
        grapheme_boundaries.push(projected.len());
    }

    Some(ProjectedText {
        text: projected,
        source_boundaries,
        grapheme_boundaries,
    })
}

/// Maps a projected byte offset back to the corresponding original-text
/// boundary.
fn source_offset(boundaries: &[(usize, usize)], projected_offset: usize) -> usize {
    boundaries
        .binary_search_by_key(&projected_offset, |(offset, _)| *offset)
        .map(|index| boundaries[index].1)
        .unwrap_or(projected_offset)
}

/// Splits oversized projected words without separating placeholders for one
/// source grapheme.
fn break_projected_words<'a>(
    words: impl Iterator<Item = Word<'a>>,
    projected: &'a ProjectedText,
    line_width: usize,
) -> Vec<Word<'a>> {
    let projected_start = projected.text.as_ptr() as usize;
    let mut pieces = Vec::new();

    for word in words {
        if display_width(word.word) <= line_width {
            pieces.push(word);
            continue;
        }

        let word_start = word.word.as_ptr() as usize - projected_start;
        let word_end = word_start + word.word.len();
        let mut piece_start = word_start;
        let mut piece_width = 0;
        let mut atom_start = word_start;
        let boundary_start = projected
            .grapheme_boundaries
            .partition_point(|atom_end| *atom_end <= word_start);

        for atom_end in projected
            .grapheme_boundaries
            .iter()
            .copied()
            .skip(boundary_start)
        {
            if atom_end > word_end {
                break;
            }

            let atom_width = display_width(&projected.text[atom_start..atom_end]);
            if piece_width > 0 && piece_width + atom_width > line_width {
                pieces.push(Word::from(&projected.text[piece_start..atom_start]));
                piece_start = atom_start;
                piece_width = 0;
            }
            piece_width += atom_width;
            atom_start = atom_end;
        }

        let mut last = Word::from(&projected.text[piece_start..word_end]);
        last.whitespace = word.whitespace;
        last.penalty = word.penalty;
        pieces.push(last);
    }

    pieces
}

/// Wraps projected text and translates the resulting ranges back to source
/// byte offsets, in the same shape as [`wrap_ranges`].
fn wrap_projected_ranges(projected: &ProjectedText, opts: &Options<'_>) -> Vec<Range<usize>> {
    let line_widths = [
        opts.width
            .saturating_sub(display_width(opts.initial_indent)),
        opts.width
            .saturating_sub(display_width(opts.subsequent_indent)),
    ];
    let line_ending = opts.line_ending.as_str();
    let mut ranges = Vec::new();
    let mut line_start = 0;

    for line in projected.text.split(line_ending) {
        let words = opts.word_separator.find_words(line);
        let split_words = split_words(words, &opts.word_splitter);
        let mut broken_words = if opts.break_words {
            break_projected_words(split_words, projected, line_widths[1])
        } else {
            split_words.collect()
        };
        if opts.break_words && !opts.initial_indent.is_empty() {
            broken_words.insert(0, Word::from(""));
        }

        let wrapped_words = opts.wrap_algorithm.wrap(&broken_words, &line_widths);
        let mut cursor = line_start;
        for words in wrapped_words {
            let Some(last_word) = words.last() else {
                let source = source_offset(&projected.source_boundaries, cursor);
                ranges.push(source..source + 1);
                continue;
            };
            let len = words
                .iter()
                .map(|word| word.word.len() + word.whitespace.len())
                .sum::<usize>()
                - last_word.whitespace.len();
            let end = cursor + len;
            let trailing_spaces = projected.text[end..]
                .chars()
                .take_while(|ch| *ch == ' ')
                .count();
            let source_start = source_offset(&projected.source_boundaries, cursor);
            let source_end = source_offset(&projected.source_boundaries, end + trailing_spaces);
            ranges.push(source_start..source_end + 1);
            cursor = end + last_word.whitespace.len();
        }
        line_start += line.len() + line_ending.len();
    }

    ranges
}

/// Returns byte-ranges into `text` for each wrapped line, including
/// trailing whitespace and a +1 sentinel byte. Used by the textarea
/// cursor-position logic.
///
/// The sentinel byte is critical: without it, a cursor at the END of a
/// wrapped line wouldn't have a valid byte position to point at, and
/// the textarea couldn't distinguish "cursor at end of line N" from
/// "cursor at start of line N+1" in the soft-wrapped representation.
pub(crate) fn wrap_ranges<'a, O>(text: &str, width_or_options: O) -> Vec<Range<usize>>
where
    O: Into<Options<'a>>,
{
    let opts = width_or_options.into();
    if let Some(projected) = project_halfwidth_sound_marks(text) {
        return wrap_projected_ranges(&projected, &opts);
    }
    let mut lines: Vec<Range<usize>> = Vec::new();
    let mut cursor = 0usize;
    for (line_index, line) in textwrap::wrap(text, &opts).iter().enumerate() {
        match line {
            std::borrow::Cow::Borrowed(slice) => {
                // `Cow::Borrowed` does not guarantee a sub-slice of `text`:
                // at width 0 textwrap returns a static `""` for every empty
                // hard line. Such a line starts on the next hard line.
                let Some(start) = subslice_offset(text, slice).or_else(|| {
                    slice
                        .is_empty()
                        .then(|| next_hard_line_start(text, cursor, line_index))
                }) else {
                    let mapped = map_owned_wrapped_line_to_range(text, cursor, slice, "");
                    let trailing_spaces =
                        text[mapped.end..].chars().take_while(|c| *c == ' ').count();
                    lines.push(mapped.start..mapped.end + trailing_spaces + 1);
                    cursor = mapped.end + trailing_spaces;
                    continue;
                };
                let end = start + slice.len();
                let trailing_spaces = text[end..].chars().take_while(|c| *c == ' ').count();
                lines.push(start..end + trailing_spaces + 1);
                cursor = end + trailing_spaces;
            }
            std::borrow::Cow::Owned(slice) => {
                let synthetic_prefix = if line_index == 0 {
                    opts.initial_indent
                } else {
                    opts.subsequent_indent
                };
                let mapped = map_owned_wrapped_line_to_range(text, cursor, slice, synthetic_prefix);
                let trailing_spaces = text[mapped.end..].chars().take_while(|c| *c == ' ').count();
                lines.push(mapped.start..mapped.end + trailing_spaces + 1);
                cursor = mapped.end + trailing_spaces;
            }
        }
    }
    lines
}

/// Byte offset of `slice` within `text`, or `None` when `slice` does not
/// lie inside `text`. Compares addresses only, so it needs no `unsafe`.
fn subslice_offset(text: &str, slice: &str) -> Option<usize> {
    let start = (slice.as_ptr() as usize).checked_sub(text.as_ptr() as usize)?;
    (start + slice.len() <= text.len()).then_some(start)
}

/// Start of the hard line that follows `cursor`, for an empty wrapped line
/// textwrap did not borrow from `text`. The first wrapped line always
/// starts at 0.
fn next_hard_line_start(text: &str, cursor: usize, line_index: usize) -> usize {
    if line_index == 0 {
        return 0;
    }
    text[cursor..]
        .find('\n')
        .map_or(text.len(), |newline| cursor + newline + 1)
}

/// Maps an owned (materialized) wrapped line back to a byte range in `text`.
///
/// `textwrap` returns `Cow::Owned` when it inserts a hyphenation penalty
/// character (typically `-`) that does not exist in the source. This
/// function walks the owned string character-by-character against the
/// source, skipping trailing penalty chars, and returns the
/// corresponding source byte range starting from `cursor`.
fn map_owned_wrapped_line_to_range(
    text: &str,
    cursor: usize,
    wrapped: &str,
    synthetic_prefix: &str,
) -> Range<usize> {
    let wrapped = if synthetic_prefix.is_empty() {
        wrapped
    } else {
        wrapped.strip_prefix(synthetic_prefix).unwrap_or(wrapped)
    };

    let mut start = cursor;
    while start < text.len() && !wrapped.starts_with(' ') {
        let Some(ch) = text[start..].chars().next() else {
            break;
        };
        if ch != ' ' {
            break;
        }
        start += ch.len_utf8();
    }

    let mut end = start;
    let mut saw_source_char = false;
    let mut chars = wrapped.chars().peekable();
    while let Some(ch) = chars.next() {
        if end < text.len() {
            let Some(src) = text[end..].chars().next() else {
                unreachable!("checked end < text.len()");
            };
            if ch == src {
                end += src.len_utf8();
                saw_source_char = true;
                continue;
            }
        }

        // textwrap can materialize owned lines when penalties are inserted.
        // The default penalty is a trailing '-'; it does not correspond to
        // source bytes, so we skip it while keeping byte ranges in source text.
        if ch == '-' && chars.peek().is_none() {
            continue;
        }

        // Non-source chars can be synthesized by textwrap in owned output
        // (e.g. non-space indent prefixes). Keep going and map the source bytes
        // we can confidently match instead of crashing the app.
        if !saw_source_char {
            continue;
        }

        tracing::warn!(
            wrapped = %wrapped,
            cursor,
            end,
            "wrap_ranges: could not fully map owned line; returning partial source range"
        );
        break;
    }

    start..end
}

#[cfg(test)]
mod tests {
    use super::*;

    fn wrap_first_fit(text: &str, width: usize) -> Vec<Range<usize>> {
        wrap_ranges(
            text,
            Options::new(width).wrap_algorithm(textwrap::WrapAlgorithm::FirstFit),
        )
    }

    // Regression: a pty with no size gave the composer width 0, and typing a
    // newline panicked with an out-of-bounds start index computed from a
    // static `""` that textwrap returns for empty lines at width 0.
    #[test]
    fn width_zero_with_newline_maps_empty_lines() {
        assert_eq!(wrap_first_fit("\n", 0), vec![0..1, 1..2]);
        assert_eq!(wrap_first_fit("a\n", 0), vec![0..2, 2..3]);
        assert_eq!(wrap_first_fit("a\n\nb", 0), vec![0..2, 2..3, 3..5]);
        assert_eq!(wrap_first_fit("\n\n", 0), vec![0..1, 1..2, 2..3]);
    }

    #[test]
    fn width_zero_matches_normal_width_for_short_lines() {
        for text in ["\n", "a\n", "a\n\nb", "\n\n", "", "a"] {
            assert_eq!(
                wrap_first_fit(text, 0),
                wrap_first_fit(text, 80),
                "{text:?}"
            );
        }
    }

    // Upstream openai/codex#35962: halfwidth sound marks count as one column
    // and stay with their base character; byte offsets are unchanged.
    #[test]
    fn wrap_ranges_count_halfwidth_sound_marks_without_changing_byte_offsets() {
        for (text, width, expected) in [
            ("abガc", 4, &["abガ", "c"][..]),
            ("abガc", 3, &["ab", "ガc"][..]),
            ("゙ab", 2, &["゙a", "b"][..]),
            ("a ゙b", 2, &["a", "゙b"][..]),
            ("a ゚b", 2, &["a", "゚b"][..]),
            ("ガ゙x", 3, &["ガ゙", "x"][..]),
            ("界゙x", 3, &["界゙", "x"][..]),
            ("ガ゙ab", 2, &["ガ゙", "ab"][..]),
            ("界゙ab", 2, &["界゙", "ab"][..]),
            ("abガ゙cd", 2, &["ab", "ガ゙", "cd"][..]),
            ("ab界゙cd", 2, &["ab", "界゙", "cd"][..]),
        ] {
            let wrapped: Vec<&str> = wrap_first_fit(text, width)
                .into_iter()
                .map(|range| text[range.start..range.end - 1].trim_end_matches(' '))
                .collect();
            assert_eq!(wrapped, expected, "{text:?} at width {width}");
        }
    }

    #[test]
    fn subslice_offset_rejects_foreign_slices() {
        let text = String::from("hello");
        assert_eq!(subslice_offset(&text, &text[1..3]), Some(1));
        assert_eq!(subslice_offset(&text, &text[5..]), Some(5));
        assert_eq!(subslice_offset(&text, "elsewhere"), None);
    }
}