dugix 1.0.0

A local-first terminal chat app for deep research and multi-agent work
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
//! Mermaid previews rendered off the draw path into fixed-height transcript slots.

use std::collections::HashMap;
use std::sync::{Arc, OnceLock};

use anyhow::{Context as _, Result, bail, ensure};
use image::DynamicImage;
use mermaid_rs_renderer::ir::DiagramKind;
use mermaid_rs_renderer::{RenderOptions, compute_layout, parse_mermaid_strict, render_svg};
use ratatui::Frame;
use ratatui::layout::{Rect, Size};
use ratatui::style::{Color, Style};
use ratatui::widgets::{Paragraph, Wrap};
use ratatui_image::picker::{Picker, ProtocolType};
use ratatui_image::sliced::{SlicedImage, SlicedProtocol};
use tokio::sync::mpsc;

/// Keeps transcript geometry stable while previews load.
pub const PREVIEW_ROWS: u16 = 12;
const MAX_SOURCE_BYTES: usize = 16 * 1024;
const MAX_SOURCE_LINES: usize = 128;
const MAX_JOBS: usize = 2;
const MAX_ENTRIES: usize = 8;
const MAX_PIXEL_WIDTH: u32 = 4096;
const MAX_PIXEL_HEIGHT: u32 = 1024;
static FONTS: OnceLock<Arc<resvg::usvg::fontdb::Database>> = OnceLock::new();
fn render_safely<T>(render: impl FnOnce() -> Result<T>) -> Result<T, String> {
    dugix_core::panic_guard::catch(render).map_or_else(
        |_| Err("diagram renderer failed; source is available below".into()),
        |result| result.map_err(|error| format!("{error:#}")),
    )
}

#[derive(Clone, Debug)]
pub struct Placement {
    pub row: usize,
    pub width: u16,
    pub source: String,
    pub complete: bool,
}

#[derive(Clone, PartialEq, Eq, Hash)]
struct Key {
    source: String,
    width: u16,
    dark: bool,
    generation: u64,
}

enum State {
    Pending,
    Ready(SlicedProtocol),
    Failed(String),
}

struct Entry {
    state: State,
    touched: u64,
}

pub struct Completion {
    key: Key,
    result: Result<SlicedProtocol, String>,
}

pub struct DiagramCache {
    picker: Picker,
    entries: HashMap<Key, Entry>,
    sender: mpsc::UnboundedSender<Completion>,
    receiver: mpsc::UnboundedReceiver<Completion>,
    in_flight: usize,
    generation: u64,
    clock: u64,
}

impl Default for DiagramCache {
    fn default() -> Self {
        let (sender, receiver) = mpsc::unbounded_channel();
        Self {
            picker: Picker::halfblocks(),
            entries: HashMap::new(),
            sender,
            receiver,
            in_flight: 0,
            generation: 0,
            clock: 0,
        }
    }
}

impl DiagramCache {
    pub fn set_picker(&mut self, picker: Picker) {
        self.clear();
        self.picker = picker;
    }

    pub fn clear(&mut self) {
        self.generation = self.generation.wrapping_add(1);
        self.entries.clear();
    }

    pub async fn next_completion(&mut self) -> Option<Completion> {
        self.receiver.recv().await
    }

    pub fn complete(&mut self, completion: Completion) {
        self.in_flight = self.in_flight.saturating_sub(1);
        if let Some(entry) = self.entries.get_mut(&completion.key) {
            entry.state = match completion.result {
                Ok(image) => State::Ready(image),
                Err(error) => State::Failed(error),
            };
        }
    }

    /// Paints cached previews without running layout, rasterization or encoding on the draw path.
    pub fn paint(
        &mut self,
        frame: &mut Frame,
        viewport: Rect,
        top: usize,
        placements: &[Placement],
        background: Color,
    ) {
        if viewport.width < 8 || viewport.height == 0 {
            return;
        }
        let dark = match background {
            Color::Rgb(r, g, b) => u32::from(r) + u32::from(g) + u32::from(b) < 384,
            Color::White | Color::Gray => false,
            _ => true,
        };
        for placement in placements {
            if !placement.complete
                || placement.row >= top + usize::from(viewport.height)
                || placement.row + usize::from(PREVIEW_ROWS) <= top
            {
                continue;
            }
            let area = Rect::new(
                viewport.x + 4, // Match the transcript's assistant rail and code-box padding.
                viewport.y,
                placement.width.min(viewport.width.saturating_sub(4)),
                viewport.height.min(32_767),
            );
            let y = if placement.row >= top {
                i16::try_from(placement.row - top).unwrap_or(i16::MAX)
            } else {
                -i16::try_from(top - placement.row).unwrap_or(i16::MAX)
            };
            if let Err(error) = validate_source(&placement.source) {
                paint_error(frame, area, y, &error.to_string());
                continue;
            }
            let key = Key {
                source: placement.source.clone(),
                width: area.width,
                dark,
                generation: self.generation,
            };
            self.request(&key);
            if let Some(entry) = self.entries.get_mut(&key) {
                self.clock = self.clock.wrapping_add(1);
                entry.touched = self.clock;
                match &entry.state {
                    State::Ready(image) => {
                        // The iTerm2 sliced widget cannot safely clip both ends at once.
                        if self.picker.protocol_type() == ProtocolType::Iterm2
                            && y < 0
                            && i32::from(y) + i32::from(image.size().height)
                                > i32::from(area.height)
                        {
                            continue;
                        }
                        frame.render_widget(SlicedImage::new(image, (0, y).into()), area);
                    }
                    State::Failed(error) => paint_error(frame, area, y, error),
                    State::Pending => {}
                }
            }
        }
    }

    fn request(&mut self, key: &Key) {
        if self.entries.contains_key(key) || self.in_flight >= MAX_JOBS || key.width == 0 {
            return;
        }
        let Ok(runtime) = tokio::runtime::Handle::try_current() else {
            return;
        };
        if self.entries.len() >= MAX_ENTRIES {
            let oldest = self
                .entries
                .iter()
                .filter(|(_, entry)| !matches!(entry.state, State::Pending))
                .min_by_key(|(_, entry)| entry.touched)
                .map(|(key, _)| key.clone());
            if let Some(oldest) = oldest {
                self.entries.remove(&oldest);
            } else {
                return;
            }
        }
        self.entries.insert(
            key.clone(),
            Entry {
                state: State::Pending,
                touched: self.clock,
            },
        );
        self.in_flight += 1;
        let key = key.clone();
        let picker = self.picker.clone();
        let sender = self.sender.clone();
        runtime.spawn_blocking(move || {
            let result = render_safely(|| prepare(&key.source, key.width, key.dark, &picker));
            let _ = sender.send(Completion { key, result });
        });
    }
}

fn validate_source(source: &str) -> Result<()> {
    ensure!(
        source.len() <= MAX_SOURCE_BYTES,
        "diagram exceeds 16 KiB; showing source"
    );
    ensure!(
        source.lines().count() <= MAX_SOURCE_LINES,
        "diagram exceeds 128 lines; showing source"
    );
    ensure!(
        !source
            .chars()
            .any(|ch| ch.is_control() && !matches!(ch, '\n' | '\r' | '\t')),
        "diagram contains control characters"
    );
    Ok(())
}

fn prepare(source: &str, width: u16, dark: bool, picker: &Picker) -> Result<SlicedProtocol> {
    let image = rasterize(source, width, dark, picker)?;
    SlicedProtocol::new(picker, image, Some(Size::new(width, PREVIEW_ROWS)))
        .context("encoding terminal diagram")
}

fn rasterize(source: &str, width: u16, dark: bool, picker: &Picker) -> Result<DynamicImage> {
    validate_source(source)?;
    let parsed = parse_mermaid_strict(source).context("invalid Mermaid")?;
    ensure!(
        matches!(
            parsed.graph.kind,
            DiagramKind::Flowchart | DiagramKind::Sequence | DiagramKind::State
        ),
        "preview supports flowcharts, sequence and state diagrams; showing source"
    );
    ensure!(
        parsed.graph.nodes.len() <= 64
            && parsed.graph.edges.len() <= 128
            && parsed.graph.subgraphs.len() <= 16,
        "diagram is too complex; showing source"
    );
    let mut options = RenderOptions::modern();
    if dark {
        options.theme = mermaid_rs_renderer::Theme::dark();
    }
    // Supported renderers honor this setting throughout layout, avoiding persistent font-cache I/O.
    options.layout.fast_text_metrics = true;
    let layout = compute_layout(&parsed.graph, &options.theme, &options.layout);
    let svg = render_svg(&layout, &options.theme, &options.layout);
    ensure!(svg.len() <= 1024 * 1024, "diagram SVG exceeds 1 MiB");

    let svg_options = resvg::usvg::Options {
        // Never resolve model-authored local paths, data or remote images.
        image_href_resolver: resvg::usvg::ImageHrefResolver {
            resolve_data: Box::new(|_, _, _| None),
            resolve_string: Box::new(|_, _| None),
        },
        fontdb: FONTS
            .get_or_init(|| {
                let mut fonts = resvg::usvg::fontdb::Database::new();
                fonts.load_system_fonts();
                Arc::new(fonts)
            })
            .clone(),
        ..Default::default()
    };
    let tree = resvg::usvg::Tree::from_str(&svg, &svg_options).context("reading diagram SVG")?;
    let font = picker.font_size();
    let pixel_width = u32::from(width) * u32::from(font.width);
    let pixel_height = u32::from(PREVIEW_ROWS) * u32::from(font.height);
    ensure!(
        (1..=MAX_PIXEL_WIDTH).contains(&pixel_width)
            && (1..=MAX_PIXEL_HEIGHT).contains(&pixel_height),
        "terminal diagram dimensions exceed the pixel budget"
    );
    let mut pixmap = resvg::tiny_skia::Pixmap::new(pixel_width, pixel_height)
        .context("allocating diagram image")?;
    let shade = if dark { 51 } else { 255 };
    pixmap.fill(resvg::tiny_skia::Color::from_rgba8(
        shade, shade, shade, 255,
    ));
    let size = tree.size();
    #[allow(clippy::cast_precision_loss)] // bounded raster dimensions
    let scale = (pixel_width as f32 / size.width()).min(pixel_height as f32 / size.height());
    if !scale.is_finite() || scale <= 0.0 {
        bail!("invalid diagram dimensions");
    }
    resvg::render(
        &tree,
        resvg::tiny_skia::Transform::from_scale(scale, scale),
        &mut pixmap.as_mut(),
    );
    // tiny-skia uses premultiplied RGBA; the image crate expects straight RGBA.
    let bytes = pixmap
        .pixels()
        .iter()
        .flat_map(|pixel| {
            let color = pixel.demultiply();
            [color.red(), color.green(), color.blue(), color.alpha()]
        })
        .collect();
    let image = image::RgbaImage::from_raw(pixel_width, pixel_height, bytes)
        .context("reading rasterized diagram")?;
    Ok(DynamicImage::ImageRgba8(image))
}

fn paint_error(frame: &mut Frame, area: Rect, y: i16, message: &str) {
    let skip = u16::try_from(-i32::from(y).min(0)).unwrap_or(PREVIEW_ROWS);
    let offset = u16::try_from(y.max(0)).unwrap_or(0);
    let height = PREVIEW_ROWS
        .saturating_sub(skip)
        .min(area.height.saturating_sub(offset));
    let visible = Rect::new(area.x, area.y + offset, area.width, height);
    frame.render_widget(
        Paragraph::new(format!("Mermaid preview unavailable: {message}"))
            .style(Style::default().fg(Color::Yellow))
            .wrap(Wrap { trim: false })
            .scroll((skip, 0)),
        visible,
    );
}

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

    #[test]
    fn tested_diagram_kinds_rasterize_without_browser_or_output_files() {
        let picker = Picker::halfblocks();
        for source in [
            "flowchart LR\nA[Start] -->|next| B[End]",
            "flowchart TD\nsubgraph Client\nA[Input] --> B[Parse]\nend\nB --> C[Render]",
            "sequenceDiagram\nAlice->>Bob: Hello",
            "stateDiagram-v2\n[*] --> Idle\nIdle --> Running",
            "flowchart LR\nA[研究] --> B[結果]",
        ] {
            let image = rasterize(source, 60, true, &picker).expect(source);
            assert_eq!(image.width(), 600);
            assert_eq!(image.height(), 240);
            assert!(
                image
                    .to_rgba8()
                    .pixels()
                    .any(|pixel| pixel.0 != [51, 51, 51, 255])
            );
        }
    }

    #[test]
    fn prompt_advertised_mermaid_examples_render() {
        let picker = Picker::halfblocks();
        for source in dugix_core::app::MERMAID_PREVIEW_EXAMPLES {
            let image = rasterize(source, 60, true, &picker).expect(source);
            assert_eq!(image.width(), 600);
            assert_eq!(image.height(), 240);
        }
    }

    #[test]
    fn renderer_panics_are_marked_for_the_terminal_hook_and_become_errors() {
        use dugix_core::panic_guard::catching;
        assert!(!catching());
        let result: Result<(), String> = render_safely(|| {
            assert!(catching());
            panic!("simulated renderer panic");
        });
        assert!(result.unwrap_err().contains("source is available"));
        assert!(!catching());
        assert!(render_safely(|| Ok(())).is_ok());
        assert!(!catching());
    }

    #[test]
    fn budgets_and_unsupported_kinds_fail_with_source_fallback() {
        let picker = Picker::halfblocks();
        assert!(rasterize("not a diagram", 60, false, &picker).is_err());
        assert!(rasterize("flowchart LR\nA-->B", u16::MAX, false, &picker).is_err());
        assert!(rasterize("pie\n\"one\" : 1", 60, false, &picker).is_err());
        assert!(rasterize(&"x".repeat(MAX_SOURCE_BYTES + 1), 60, false, &picker).is_err());
        assert!(rasterize(&"x\n".repeat(MAX_SOURCE_LINES + 1), 60, false, &picker).is_err());
        assert!(rasterize("flowchart LR\nA[\x1b]", 60, false, &picker).is_err());
        let large = format!(
            "flowchart LR\n{}",
            (0..65).map(|i| format!("N{i}\n")).collect::<String>()
        );
        assert!(rasterize(&large, 60, false, &picker).is_err());
    }

    #[test]
    fn clipped_halfblock_previews_stay_inside_the_history_viewport() {
        let mut cache = DiagramCache::default();
        let source = "flowchart LR\nA-->B";
        let key = Key {
            source: source.into(),
            width: 20,
            dark: true,
            generation: 0,
        };
        let image = prepare(source, 20, true, &cache.picker).expect("preview");
        cache.entries.insert(
            key,
            Entry {
                state: State::Ready(image),
                touched: 0,
            },
        );
        let placement = Placement {
            source: source.into(),
            width: 20,
            row: 3,
            complete: true,
        };
        let viewport = Rect::new(2, 3, 30, 5);
        let mut terminal =
            ratatui::Terminal::new(ratatui::backend::TestBackend::new(40, 15)).unwrap();
        terminal
            .draw(|frame| {
                frame.render_widget(
                    ratatui::widgets::Block::default().style(Style::default().bg(Color::Blue)),
                    frame.area(),
                );
                cache.paint(frame, viewport, 5, &[placement.clone()], Color::Black);
            })
            .unwrap();
        let buffer = terminal.backend().buffer();
        let mut pixels = 0;
        for y in 0..15 {
            for x in 0..40 {
                if buffer[(x, y)].symbol() == "▀" {
                    pixels += 1;
                    assert!(viewport.contains(ratatui::layout::Position::new(x, y)));
                } else if !viewport.contains(ratatui::layout::Position::new(x, y)) {
                    assert_eq!(buffer[(x, y)].bg, Color::Blue);
                }
            }
        }
        assert!(pixels > 0);
    }

    #[tokio::test]
    async fn cache_eviction_preserves_the_most_recent_previews() {
        let mut cache = DiagramCache::default();
        let keys: Vec<_> = (0..MAX_ENTRIES)
            .map(|i| Key {
                source: format!("flowchart LR\nA-->N{i}"),
                width: 60,
                dark: true,
                generation: 0,
            })
            .collect();
        for (i, key) in keys.iter().enumerate() {
            cache.entries.insert(
                key.clone(),
                Entry {
                    state: State::Failed("old".into()),
                    touched: u64::try_from(i).unwrap(),
                },
            );
        }
        let key = Key {
            source: "flowchart LR\nA-->New".into(),
            width: 60,
            dark: true,
            generation: 0,
        };
        cache.request(&key);
        assert_eq!(cache.entries.len(), MAX_ENTRIES);
        assert!(!cache.entries.contains_key(&keys[0]));
        assert!(cache.entries.contains_key(&keys[MAX_ENTRIES - 1]));
        let completion = cache.next_completion().await.expect("completion");
        cache.complete(completion);
    }

    #[tokio::test]
    async fn requests_deduplicate_and_stale_completions_do_not_return() {
        let mut cache = DiagramCache::default();
        let key = Key {
            source: "flowchart LR\nA-->B".into(),
            width: 60,
            dark: true,
            generation: 0,
        };
        cache.request(&key);
        cache.request(&key);
        assert_eq!(cache.in_flight, 1);
        cache.clear();
        let completion =
            tokio::time::timeout(std::time::Duration::from_secs(10), cache.next_completion())
                .await
                .expect("renderer completed")
                .expect("completion");
        cache.complete(completion);
        assert_eq!(cache.in_flight, 0);
        assert!(cache.entries.is_empty());
    }

    #[tokio::test]
    async fn worker_concurrency_is_bounded() {
        let mut cache = DiagramCache::default();
        for i in 0..MAX_JOBS + 2 {
            cache.request(&Key {
                source: format!("flowchart LR\nA-->N{i}"),
                width: 60,
                dark: true,
                generation: 0,
            });
        }
        assert_eq!(cache.in_flight, MAX_JOBS);
        assert_eq!(cache.entries.len(), MAX_JOBS);
        for _ in 0..MAX_JOBS {
            let completion = cache.next_completion().await.expect("completion");
            cache.complete(completion);
        }
        assert_eq!(cache.in_flight, 0);
    }
}