rho-coding-agent 1.32.0

A lightweight agent harness inspired by Pi
Documentation
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
use super::*;
use ratatui::style::Style;

fn line_text(line: &Line<'_>) -> String {
    line.spans
        .iter()
        .map(|span| span.content.as_ref())
        .collect()
}

fn line_styles(line: &Line<'_>) -> Vec<Style> {
    line.spans.iter().map(|span| span.style).collect()
}

#[test]
fn keeps_list_markers_with_a_long_path_in_narrow_output() {
    for marker in ["-", "1.", "2)"] {
        let markdown =
            format!("{marker} fixtures/downstream/no-default-features/Cargo.toml: package 0.0.0");
        let mut in_code_block = false;
        let lines = markdown_lines(&markdown, 39, &mut in_code_block);

        let first_line_suffix_len = 39 - marker.len() - 1;
        let path = "fixtures/downstream/no-default-features/Cargo.toml: package 0.0.0";
        assert_eq!(
            lines.iter().map(line_text).collect::<Vec<_>>(),
            vec![
                format!("{marker} {}", &path[..first_line_suffix_len]),
                path[first_line_suffix_len..].to_string(),
            ]
        );
    }
}

#[test]
fn streams_list_lines_at_the_same_wrap_boundary_as_final_rendering() {
    let markdown = "- fixtures/downstream/no-default-features/Cargo.toml: package 0.0.0";

    let bounds = markdown_stream_bounds(markdown, 39, false);

    assert_eq!(bounds.drain.byte_index, 39);
    assert!(bounds.drain.ends_with_wrap);
}

#[test]
fn preserves_underscores_inside_identifiers() {
    let mut in_code_block = false;
    let lines = markdown_lines(
        "keep foo_bar_baz literal but style _this_",
        120,
        &mut in_code_block,
    );

    assert_eq!(
        line_text(&lines[0]),
        "keep foo_bar_baz literal but style this"
    );
    assert!(line_styles(&lines[0]).contains(&Theme::markdown_italic()));
}

#[test]
fn wraps_long_unicode_styled_lines_without_losing_text_or_styles() {
    let plain_prefix = "éλ".repeat(256);
    let bold = "你🙂".repeat(256);
    let plain_suffix = "界ß".repeat(256);
    let markdown = format!("{plain_prefix} **{bold}** {plain_suffix}");
    let expected = format!("{plain_prefix} {bold} {plain_suffix}");
    let mut in_code_block = false;

    let lines = markdown_lines(&markdown, 17, &mut in_code_block);
    let rendered = lines.iter().map(line_text).collect::<String>();
    let rendered_bold = lines
        .iter()
        .flat_map(|line| &line.spans)
        .filter(|span| span.style == Theme::markdown_bold())
        .map(|span| span.content.as_ref())
        .collect::<String>();

    assert_eq!(rendered, expected);
    assert_eq!(rendered_bold, bold);
    assert!(lines
        .iter()
        .all(|line| display_width(&line_text(line)) <= 17));
}

#[test]
fn code_block_padding_uses_display_width() {
    let mut in_code_block = false;
    let lines = markdown_lines("```\nä½ \n```", 6, &mut in_code_block);

    assert_eq!(line_text(&lines[1]), "│ 你 │");
    assert_eq!(display_width(&line_text(&lines[1])), 6);
}

#[test]
fn code_blocks_preserve_markdown_markers_as_literal_text() {
    let mut in_code_block = false;
    let lines = markdown_lines(
        "```rust\nfn __init__() { println!(\"*ok*\"); }\n```",
        80,
        &mut in_code_block,
    );

    assert!(line_text(&lines[1]).contains("fn __init__() { println!(\"*ok*\"); }"));
    assert_eq!(line_styles(&lines[1]), vec![Theme::markdown_code_block()]);
}

#[test]
fn code_fence_closers_match_marker_length_and_allow_only_whitespace() {
    let opening = parse_opening_fence("   ````mermaid").expect("valid opening fence");
    assert_eq!(opening.marker, '`');
    assert_eq!(opening.length, 4);
    assert!(!is_closing_fence("```", opening));
    assert!(!is_closing_fence("~~~~", opening));
    assert!(!is_closing_fence("````not-a-close", opening));
    assert!(is_closing_fence("  `````   ", opening));
    assert!(parse_opening_fence("    ```rust").is_none());
    assert!(parse_opening_fence("```rust`edition").is_none());
}

#[test]
fn streamed_code_fence_state_preserves_marker_and_length_across_chunks() {
    let mut state = CodeFenceState::default();
    update_code_block_state("````mermaid\nflowchart TD", &mut state);
    assert!(state.is_open());
    update_code_block_state("```", &mut state);
    assert!(state.is_open());
    update_code_block_state("````", &mut state);
    assert!(!state.is_open());

    update_code_block_state("~~~~mermaid", &mut state);
    assert!(state.is_open());
    update_code_block_state("```", &mut state);
    assert!(state.is_open());
    update_code_block_state("~~~~", &mut state);
    assert!(!state.is_open());
}

#[test]
fn mermaid_scanner_keeps_an_invalid_closer_inside_the_raw_block() {
    let mut in_code_block = false;
    let rendered = render_markdown(
        "````mermaid\nflowchart TD\nA[one]\n```not-a-close\nA --> B[two]\n````",
        80,
        &mut in_code_block,
    );
    let text = rendered
        .lines
        .iter()
        .map(line_text)
        .collect::<Vec<_>>()
        .join("\n");
    assert!(text.contains("MERMAID"), "{text}");
    assert!(text.contains("one"), "{text}");
    assert!(text.contains("two"), "{text}");
}

#[test]
fn open_mermaid_fence_stays_raw_until_closed() {
    let mut in_code_block = false;
    let open = render_markdown("```mermaid\nflowchart LR\nA --> B", 60, &mut in_code_block);
    let open_text = open.lines.iter().map(line_text).collect::<Vec<_>>();

    assert!(in_code_block);
    assert!(open_text.iter().any(|line| line.contains("flowchart LR")));
    assert!(!open_text.iter().any(|line| line.contains("MERMAID")));

    let mut in_code_block = false;
    let closed = render_markdown(
        "```mermaid\nflowchart LR\nA --> B\n```",
        60,
        &mut in_code_block,
    );
    assert!(!in_code_block);
    assert!(line_text(&closed.lines[0]).contains("MERMAID"));
    assert!(!closed
        .lines
        .iter()
        .map(line_text)
        .any(|line| line.contains("flowchart LR")));
}

#[test]
fn mermaid_render_reflows_to_the_requested_transcript_width() {
    let markdown = "```mermaid\nflowchart LR\nA[Parse] --> B[Render]\n```";
    let mut wide_state = false;
    let wide = markdown_lines(markdown, 80, &mut wide_state);
    let mut narrow_state = false;
    let narrow = markdown_lines(markdown, 36, &mut narrow_state);

    assert!(wide
        .iter()
        .all(|line| display_width(&line_text(line)) <= 80));
    assert!(narrow
        .iter()
        .all(|line| display_width(&line_text(line)) <= 36));
    assert_ne!(
        wide.iter().map(line_text).collect::<Vec<_>>(),
        narrow.iter().map(line_text).collect::<Vec<_>>()
    );
}

#[test]
fn image_syntax_inside_code_fence_stays_literal() {
    let mut in_code_block = false;
    let lines = markdown_lines("```\n![diagram](arch.png)\n```", 120, &mut in_code_block);
    let text: Vec<String> = lines.iter().map(line_text).collect();

    assert!(text
        .iter()
        .any(|line| line.contains("![diagram](arch.png)")));
}

#[test]
fn stable_prefix_stops_at_earliest_open_marker() {
    assert_eq!(
        inline_markdown_stable_prefix_len("before **bold"),
        "before ".len()
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("before *it **also"),
        "before ".len(),
        "earlier italic must win over later bold"
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("before **bold `code"),
        "before ".len(),
        "earlier bold must win over later code"
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("before `code **bold"),
        "before ".len()
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("a _x **y"),
        "a ".len(),
        "earlier underscore italic must win over later bold"
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("done **x** tail"),
        "done **x** tail".len()
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("text *a* **b"),
        "text *a* ".len()
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("see [x](http"),
        "see ".len()
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("see [x]"),
        "see ".len(),
        "trailing ] may still become a link target"
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("see [x] plain"),
        "see [x] plain".len(),
        "] followed by non-( stays plain text"
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("go https://ex"),
        "go ".len()
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("before [link](https://x) **b"),
        "before [link](https://x) ".len()
    );
}

// Covers: open $ math must hold the preview while currency dollars stay plain
// Owner: pure unit (markdown inline math streaming bounds)
#[test]
fn stable_prefix_holds_open_inline_math_but_not_currency() {
    assert_eq!(
        inline_markdown_stable_prefix_len("before $x^2"),
        "before ".len()
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("pay $"),
        "pay ".len(),
        "a trailing $ may still open math once more input arrives"
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("done $x^2$ tail"),
        "done $x^2$ tail".len()
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("cost $5 and more"),
        "cost $5 and more".len(),
        "a $ before a digit is currency, not math"
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("a `$x` b"),
        "a `$x` b".len(),
        "$ inside a code span never opens math"
    );
    assert_eq!(
        inline_markdown_stable_prefix_len("closed $a_i$ then *em"),
        "closed $a_i$ then ".len(),
        "markers after closed math must still hold"
    );
}

// Covers: closed $...$ renders single-row art while currency and tall math stay literal
// Owner: pure unit (markdown inline math integration)
#[test]
fn renders_single_row_inline_math_in_prose() {
    let mut in_code_block = false;
    let lines = markdown_lines("energy $E = mc^2$ done", 80, &mut in_code_block);
    let text = lines.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(text, vec!["energy E = mc² done"]);

    let mut in_code_block = false;
    let currency = markdown_lines("that costs $5 and $10 total", 80, &mut in_code_block);
    let currency_text = currency.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(currency_text, vec!["that costs $5 and $10 total"]);

    let mut in_code_block = false;
    let tall = markdown_lines(r"half is $\frac{1}{2}$ here", 80, &mut in_code_block);
    let tall_text = tall.iter().map(line_text).collect::<Vec<_>>();
    assert_eq!(
        tall_text,
        vec![r"half is $\frac{1}{2}$ here"],
        "multi-row inline math must keep its literal source"
    );

    let mut in_code_block = false;
    let code = markdown_lines("run `echo $x^2$` now", 80, &mut in_code_block);
    let code_text = code.iter().map(line_text).collect::<Vec<_>>();
    assert!(
        code_text.iter().any(|line| line.contains("echo $x^2$")),
        "code spans keep dollars literal: {code_text:?}"
    );
}

// Covers: closed $$ blocks must render through the markdown panel path
// Owner: pure unit (markdown display math integration)
#[test]
fn renders_closed_display_math_blocks_in_markdown() {
    let mut in_code_block = false;
    let multi = markdown_lines(
        "before\n$$\n\\frac{a}{b}\n$$\nafter",
        40,
        &mut in_code_block,
    );
    let multi_text = multi.iter().map(line_text).collect::<Vec<_>>();
    assert!(
        multi_text.iter().any(|line| line.contains("MATH")),
        "{multi_text:?}"
    );
    assert!(
        multi_text
            .iter()
            .any(|line| line.contains('a') && !line.contains("\\frac")),
        "{multi_text:?}"
    );
    assert!(
        multi_text.iter().any(|line| line == "after"),
        "{multi_text:?}"
    );
    assert!(!in_code_block);

    let mut in_code_block = false;
    let single = markdown_lines("$$x^2 + y^2$$", 40, &mut in_code_block);
    let single_text = single.iter().map(line_text).collect::<Vec<_>>();
    assert!(
        single_text.iter().any(|line| line.contains("MATH")),
        "{single_text:?}"
    );
    assert!(
        !single_text.iter().any(|line| line.contains("$$")),
        "{single_text:?}"
    );
}

// Covers: $$ inside fences and open $$ tails must not false-commit
// Owner: pure unit (markdown display math streaming bounds)
#[test]
fn keeps_fenced_and_open_display_math_literal() {
    let mut in_code_block = false;
    let fenced = markdown_lines("```text\n$$x^2$$\n```", 40, &mut in_code_block);
    let fenced_text = fenced.iter().map(line_text).collect::<Vec<_>>();
    assert!(
        fenced_text.iter().any(|line| line.contains("$$x^2$$")),
        "{fenced_text:?}"
    );
    assert!(
        !fenced_text.iter().any(|line| line.contains("MATH")),
        "{fenced_text:?}"
    );

    let open = "intro\n$$\n\\frac{a}{b}";
    assert_eq!(
        incremental_markdown_tail_start(open),
        "intro\n".len(),
        "open multi-line math must stay in the mutable tail"
    );
    let closed_then_prose = "intro\n$$\n\\frac{a}{b}\n$$\nafter";
    assert_eq!(
        incremental_markdown_tail_start(closed_then_prose),
        "intro\n$$\n\\frac{a}{b}\n$$\n".len(),
        "prose after a closed math block becomes the trailing block"
    );
}