rho-coding-agent 2.30.0

A fast Rust agent harness with a small footprint and opinionated defaults
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
use std::panic::AssertUnwindSafe;

use ratatui::text::{Line, Span};

use super::super::{
    render::{display_width, truncate_to_display_width},
    theme::Theme,
};
use super::panel::ClosedPanel;
use crate::tui::terminal_graph::{GraphStyles, Oversize};

mod flow;
mod gantt;
mod gitgraph;
mod mindmap;
mod model;
mod policy;
mod security;
mod sequence;
mod validity;

pub(super) struct MermaidArt {
    pub(super) styled_lines: Vec<Line<'static>>,
    pub(super) plain_lines: Vec<String>,
}

const MAX_SOURCE_BYTES: usize = 64 * 1024;
const MAX_SOURCE_LINES: usize = 2_048;
const MAX_PRIMARY_ENTITIES: usize = 128;
const MAX_RELATIONSHIPS: usize = 512;
const MAX_GROUPS: usize = 24;
const MAX_DETAILS: usize = 1_024;
const MAX_RENDERED_LINES: usize = 4_096;
const MAX_RENDERED_CELLS: usize = 2_000_000;
/// Narrowest pane where a horizontally clipped diagram still reads as one.
/// Below this width, a clipped canvas carries too little structure, so source wins.
const MIN_CLIP_WIDTH: usize = 24;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum MermaidFallback {
    Blank,
    SourceBytes,
    SourceLines,
    UnsafeContent,
    Unsupported,
    Malformed,
    StructuralLimit,
    Panic,
    OutputLines,
    OutputCells,
    TooWide,
    AnsiOutput,
}

impl MermaidFallback {
    pub(super) fn panel_title(self) -> &'static str {
        match self {
            Self::TooWide => "MERMAID · PANE TOO NARROW",
            Self::Unsupported => "MERMAID · UNSUPPORTED",
            Self::Malformed => "MERMAID · INVALID",
            Self::SourceBytes
            | Self::SourceLines
            | Self::StructuralLimit
            | Self::OutputLines
            | Self::OutputCells => "MERMAID · TOO LARGE",
            Self::Blank | Self::UnsafeContent | Self::Panic | Self::AnsiOutput => {
                "MERMAID · NOT RENDERED"
            }
        }
    }
}

/// How much of a fence body the renderer sees.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum SourceExtent {
    /// The fence closed: the source must be a whole diagram.
    Complete,
    /// The fence is still streaming: complete lines so far, where a `{`
    /// block may still be open.
    StreamingPrefix,
}

#[derive(Debug, PartialEq, Eq)]
pub(super) enum MermaidRender {
    Rendered(Vec<Line<'static>>),
    /// Art laid out wider than the pane and cut at the right edge. The marker
    /// row appended by [`render_closed_fence`] names the hidden columns; COPY
    /// still yields the full source.
    Clipped {
        lines: Vec<Line<'static>>,
        hidden_columns: usize,
    },
    Fallback(MermaidFallback),
}

pub(super) fn render_closed_fence(source: String, inner_width: usize) -> ClosedPanel {
    match mermaid_art(&source, inner_width, SourceExtent::Complete) {
        Ok((title, lines)) => ClosedPanel::Art {
            title,
            lines,
            source,
        },
        Err(reason) => ClosedPanel::SourceFallback {
            title: reason.panel_title(),
            source,
        },
    }
}

/// Live mermaid for an unclosed fence.
///
/// Tries the complete-line prefix, then walks back through earlier complete
/// lines on blank/malformed so a later bad line keeps last-good art. A `{`
/// block still open at the prefix's end is not malformed: its closing line
/// may not have streamed yet. Sticky failures (unsafe, unsupported, too large) return
/// `None` so the caller keeps an ordinary source fence until close.
pub(super) fn render_open_prefix(
    complete_body: &str,
    copy_source: &str,
    inner_width: usize,
) -> Option<ClosedPanel> {
    let mut body = complete_body;
    loop {
        match mermaid_art(body, inner_width, SourceExtent::StreamingPrefix) {
            Ok((title, lines)) => {
                return Some(ClosedPanel::Art {
                    title,
                    lines,
                    source: copy_source.to_string(),
                });
            }
            Err(MermaidFallback::Blank | MermaidFallback::Malformed) => {
                let (shorter, _) = body.rsplit_once('\n')?;
                body = shorter;
            }
            Err(_) => return None,
        }
    }
}

fn mermaid_art(
    source: &str,
    inner_width: usize,
    extent: SourceExtent,
) -> Result<(&'static str, Vec<Line<'static>>), MermaidFallback> {
    match render_mermaid(source, inner_width, extent) {
        MermaidRender::Rendered(lines) => Ok(("MERMAID", lines)),
        MermaidRender::Clipped {
            mut lines,
            hidden_columns,
        } => {
            lines.push(Line::from(Span::styled(
                format!("▶ {hidden_columns} cols clipped"),
                Theme::dim(),
            )));
            Ok(("MERMAID · CLIPPED", lines))
        }
        MermaidRender::Fallback(reason) => Err(reason),
    }
}

fn render_mermaid(source: &str, inner_width: usize, extent: SourceExtent) -> MermaidRender {
    match std::panic::catch_unwind(AssertUnwindSafe(|| {
        render_inner(source, inner_width, extent)
    })) {
        Ok(result) => result,
        Err(_) => MermaidRender::Fallback(MermaidFallback::Panic),
    }
}

fn render_inner(source: &str, inner_width: usize, extent: SourceExtent) -> MermaidRender {
    if source.trim().is_empty() {
        return MermaidRender::Fallback(MermaidFallback::Blank);
    }
    if source.len() > MAX_SOURCE_BYTES {
        return MermaidRender::Fallback(MermaidFallback::SourceBytes);
    }
    if source.lines().count() > MAX_SOURCE_LINES {
        return MermaidRender::Fallback(MermaidFallback::SourceLines);
    }
    if inner_width == 0 || security::contains_unsafe_content(source) {
        return MermaidRender::Fallback(MermaidFallback::UnsafeContent);
    }
    if !is_supported_header(source) {
        return MermaidRender::Fallback(MermaidFallback::Unsupported);
    }

    let parsed = match mermaid_rs_renderer::parse_mermaid_strict(source) {
        Ok(parsed) => parsed,
        Err(_) => return MermaidRender::Fallback(MermaidFallback::Malformed),
    };
    let Some(kind) = policy::painted(parsed.graph.kind) else {
        return MermaidRender::Fallback(MermaidFallback::Unsupported);
    };
    if !parsed.graph.node_links.is_empty() {
        return MermaidRender::Fallback(MermaidFallback::UnsafeContent);
    }
    // After the safety gates: unsafe content stays a sticky decline rather
    // than a malformed walk-back while streaming.
    match (validity::check(source, kind, &parsed.graph), extent) {
        (Ok(()), _) | (Err(validity::Defect::UnclosedBlock), SourceExtent::StreamingPrefix) => {}
        (Err(validity::Defect::UnclosedBlock), SourceExtent::Complete)
        | (Err(validity::Defect::Invalid), _) => {
            return MermaidRender::Fallback(MermaidFallback::Malformed);
        }
        (Err(validity::Defect::Misread), _) => {
            return MermaidRender::Fallback(MermaidFallback::Unsupported);
        }
    }
    let (primary, relationships, groups, details) = model::complexity(&parsed.graph);
    if primary > MAX_PRIMARY_ENTITIES
        || relationships > MAX_RELATIONSHIPS
        || groups > MAX_GROUPS
        || details > MAX_DETAILS
    {
        return MermaidRender::Fallback(MermaidFallback::StructuralLimit);
    }

    let Some(model) = model::from_ir(&parsed.graph) else {
        return MermaidRender::Fallback(MermaidFallback::Unsupported);
    };
    let style = Theme::code_text();
    let styles = GraphStyles {
        border: style,
        node_text: style,
        edge: style,
        edge_label: style,
        node_styles: Vec::new(),
    };
    let result = layout_model(&model, &styles, Some(inner_width));
    let art = match result {
        Ok(art) => art,
        Err(Oversize::Width) => {
            return render_clipped(&model, &styles, inner_width);
        }
        Err(Oversize::Cells) => {
            return MermaidRender::Fallback(MermaidFallback::OutputCells);
        }
    };
    if let Err(reason) = validate_output(&art.plain_lines, /*max_width*/ Some(inner_width)) {
        return MermaidRender::Fallback(reason);
    }
    MermaidRender::Rendered(art.styled_lines)
}

/// Failure-path floor: lay the diagram out without a width cap and cut it at
/// the pane's right edge. Runs only after every bounded compaction rung fails,
/// so fitting diagrams pay nothing. Cell/line caps still bound the unbounded
/// layout.
///
/// Known limit: layout centers parents over their children, so on very wide
/// single ranks a centered node can fall right of the window. The clip keeps
/// the left edge because TD flow anchors entry nodes there; a follow-the-root
/// window is future work if receipts show it matters.
fn render_clipped(
    model: &model::TerminalModel,
    styles: &GraphStyles,
    inner_width: usize,
) -> MermaidRender {
    if inner_width < MIN_CLIP_WIDTH {
        return MermaidRender::Fallback(MermaidFallback::TooWide);
    }
    let art = match layout_model(model, styles, /*max_width*/ None) {
        Ok(art) => art,
        // Unbounded layout cannot fail on width; anything else keeps the
        // pre-clip fallback taxonomy.
        Err(Oversize::Width) => return MermaidRender::Fallback(MermaidFallback::TooWide),
        Err(Oversize::Cells) => return MermaidRender::Fallback(MermaidFallback::OutputCells),
    };
    let full_width = art
        .plain_lines
        .iter()
        .map(|line| display_width(line))
        .max()
        .unwrap_or(0);
    let hidden_columns = full_width.saturating_sub(inner_width);
    if hidden_columns == 0 {
        // Bounded layout failed but the unbounded one fits: still art.
        return MermaidRender::Rendered(art.styled_lines);
    }
    let lines: Vec<Line<'static>> = art
        .styled_lines
        .into_iter()
        .map(|line| clip_line_to_width(line, inner_width))
        .collect();
    let plain_clipped: Vec<String> = lines
        .iter()
        .map(|line| {
            line.spans
                .iter()
                .map(|span| span.content.as_ref())
                .collect()
        })
        .collect();
    if let Err(reason) = validate_output(&plain_clipped, /*max_width*/ None) {
        return MermaidRender::Fallback(reason);
    }
    MermaidRender::Clipped {
        lines,
        hidden_columns,
    }
}

/// Cut a styled line at a display-column boundary, preserving span styles.
fn clip_line_to_width(line: Line<'static>, width: usize) -> Line<'static> {
    let mut used = 0usize;
    let mut spans = Vec::new();
    for span in line.spans {
        if used >= width {
            break;
        }
        let span_width = display_width(span.content.as_ref());
        if used + span_width <= width {
            used += span_width;
            spans.push(span);
        } else {
            let cut = truncate_to_display_width(span.content.as_ref(), width - used).into_owned();
            spans.push(Span::styled(cut, span.style));
            // A dropped trailing double-width grapheme can leave a leftover
            // column. Everything after this span is past the boundary.
            break;
        }
    }
    Line::from(spans)
}

fn layout_model(
    model: &model::TerminalModel,
    styles: &GraphStyles,
    max_width: Option<usize>,
) -> Result<MermaidArt, Oversize> {
    match model {
        model::TerminalModel::Sequence(sequence) => {
            sequence::layout_sequence(sequence, styles, max_width)
        }
        model::TerminalModel::Class { graph, info } => {
            flow::render_class(graph, info, styles, max_width)
        }
        model::TerminalModel::Flow(graph) => flow::layout_flow(graph, styles, max_width),
        model::TerminalModel::GitGraph(graph) => {
            gitgraph::layout_gitgraph(graph, styles, max_width)
        }
        model::TerminalModel::Gantt(chart) => gantt::layout_gantt(chart, styles, max_width),
        model::TerminalModel::Mindmap(map) => mindmap::layout_mindmap(map, styles, max_width),
    }
}

pub(super) fn art_from_plain(plain_lines: Vec<String>, styles: &GraphStyles) -> MermaidArt {
    let styled_lines = plain_lines
        .iter()
        .map(|line| Line::from(Span::styled(line.clone(), styles.node_text)))
        .collect();
    MermaidArt {
        styled_lines,
        plain_lines,
    }
}

fn validate_output(lines: &[String], max_width: Option<usize>) -> Result<(), MermaidFallback> {
    if lines.len() > MAX_RENDERED_LINES {
        return Err(MermaidFallback::OutputLines);
    }
    let mut cells = 0usize;
    for line in lines {
        if line.contains('\x1b') {
            return Err(MermaidFallback::AnsiOutput);
        }
        let width = display_width(line);
        if max_width.is_some_and(|max| width > max) {
            return Err(MermaidFallback::TooWide);
        }
        cells = cells.saturating_add(width);
        if cells > MAX_RENDERED_CELLS {
            return Err(MermaidFallback::OutputCells);
        }
    }
    Ok(())
}

fn is_supported_header(source: &str) -> bool {
    let Some(header) = source
        .lines()
        .map(str::trim)
        .find(|line| !line.is_empty() && !line.starts_with("%%"))
        .and_then(|line| line.split_whitespace().next())
    else {
        return false;
    };
    // `gitGraph:` / `gantt` / `flowchart TD` all appear as the first token.
    // Strip a trailing colon so the kind name matches the allowlist.
    let header = header.trim_end_matches(':').to_ascii_lowercase();
    matches!(
        header.as_str(),
        "flowchart"
            | "graph"
            | "statediagram"
            | "statediagram-v2"
            | "sequencediagram"
            | "classdiagram"
            | "erdiagram"
            | "gitgraph"
            | "gantt"
            | "mindmap"
    )
}

#[cfg(test)]
pub(crate) const PHASE_CHAIN_FLOWCHART: &str = concat!(
    "flowchart LR\n",
    "  P1[\"Phase 1: retention sweep\"] --> P2[\"Phase 2: parent link on disk\"]\n",
    "  P2 --> P3[\"Phase 3: session delete API + CLI\"]\n",
    "  P3 --> P4[\"Phase 4: TUI delete in resume picker\"]\n",
    "  P3 --> P5[\"Phase 5: nest runs under session\"]"
);

#[cfg(test)]
#[path = "mermaid_tests.rs"]
mod tests;