collet 0.1.1

Relentless agentic coding orchestrator with zero-drop agent loops
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
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
mod confirm;
mod info_select;
mod list;

use confirm::LspInstallCtx;
use ratatui::prelude::*;
use ratatui::widgets::{Block, BorderType, Borders, Clear, Paragraph};

use crate::tui::state::{PopupKind, UiState};
use crate::tui::theme::Theme;

/// Common context shared by popup list renderers.
pub(super) struct PopupListCtx<'a> {
    pub(super) popup_area: Rect,
    pub(super) selected: usize,
    pub(super) scroll: u16,
    pub(super) search: &'a str,
    pub(super) theme: &'a Theme,
}

/// Render the esc-hint corner badge and the bottom hint line for a popup.
fn render_popup_chrome(
    popup_area: Rect,
    hint_area: Rect,
    bottom_hint: &str,
    theme: &Theme,
    buf: &mut Buffer,
) {
    Paragraph::new(" esc ")
        .style(Style::default().fg(theme.text_muted))
        .render(hint_area, buf);
    render_bottom_hint(popup_area, buf, bottom_hint, theme.text_muted);
}

/// Render a centered popup overlay.
pub fn render(state: &UiState, area: Rect, buf: &mut Buffer) {
    let Some(popup) = &state.popup else { return };
    let theme = &state.theme;

    // Popup dimensions: 80% width, 75% height, centered
    let popup_width = (area.width as f32 * 0.80).max(50.0) as u16;
    let popup_height = (area.height as f32 * 0.75).max(12.0) as u16;
    let popup_x = area.x + (area.width.saturating_sub(popup_width)) / 2;
    let popup_y = area.y + (area.height.saturating_sub(popup_height)) / 2;
    let popup_area = Rect::new(popup_x, popup_y, popup_width, popup_height);

    // Clear background for popup area
    Clear.render(popup_area, buf);

    // Draw the outer border block
    let block = Block::default()
        .title(Span::styled(
            format!(" {} ", popup.title),
            Style::default()
                .fg(theme.accent)
                .add_modifier(Modifier::BOLD),
        ))
        .title_alignment(Alignment::Left)
        .borders(Borders::ALL)
        .border_type(BorderType::Rounded)
        .border_style(Style::default().fg(theme.accent))
        .style(Style::default().bg(theme.bg_surface));

    // "esc" hint in top-right corner of border
    let esc_hint = " esc ";
    let hint_x = popup_area.x + popup_area.width.saturating_sub(esc_hint.len() as u16 + 1);
    let hint_area = Rect::new(hint_x, popup_area.y, esc_hint.len() as u16, 1);

    match &popup.kind {
        PopupKind::Info => {
            info_select::render_info(popup, popup_area, block, theme, buf);
            render_popup_chrome(popup_area, hint_area, "↑↓ scroll", theme, buf);
        }
        PopupKind::Select { items, selected } => {
            info_select::render_select(popup, items, *selected, popup_area, block, theme, buf);
            render_popup_chrome(
                popup_area,
                hint_area,
                "type to search  ↑↓ navigate  Enter apply",
                theme,
                buf,
            );
        }
        PopupKind::TableSelect { items, selected } => {
            let ctx = PopupListCtx {
                popup_area,
                selected: *selected,
                scroll: popup.scroll,
                search: &popup.search,
                theme,
            };
            list::render_table_select(&ctx, buf, block, items);
            render_popup_chrome(
                popup_area,
                hint_area,
                "↑↓ navigate  Enter select",
                theme,
                buf,
            );
        }
        PopupKind::Config { items, selected } => {
            list::render_config(
                popup_area,
                buf,
                block,
                items,
                *selected,
                popup.scroll,
                theme,
            );
            render_popup_chrome(
                popup_area,
                hint_area,
                "↑↓ navigate  Space change  Enter save  Esc cancel",
                theme,
                buf,
            );
        }
        PopupKind::QueueConfirm { pending, selected } => {
            confirm::render_queue_confirm(
                popup_area,
                buf,
                block,
                pending,
                *selected,
                popup.scroll,
                theme,
            );
            render_bottom_hint(
                popup_area,
                buf,
                "↑↓ Select  Enter Confirm  Esc Close",
                theme.text_muted,
            );
        }
        PopupKind::ContinuationConfirm { selected } => {
            confirm::render_continuation_confirm(
                popup_area,
                buf,
                block,
                &popup.content,
                *selected,
                theme,
            );
            render_bottom_hint(
                popup_area,
                buf,
                "↑↓ Select  Enter Confirm  Esc Stop",
                theme.text_muted,
            );
        }
        PopupKind::InitConfirm { selected } => {
            confirm::render_init_confirm(popup_area, buf, block, *selected, theme);
            render_bottom_hint(
                popup_area,
                buf,
                "↑↓ Select  Enter Confirm  Esc Cancel",
                theme.text_muted,
            );
        }
        PopupKind::ToolApproval {
            tool_name,
            tool_args,
            selected,
        } => {
            confirm::render_tool_approval(
                popup_area, buf, block, tool_name, tool_args, *selected, theme,
            );
            render_bottom_hint(
                popup_area,
                buf,
                "↑↓ Select  Enter Confirm",
                theme.text_muted,
            );
        }
        PopupKind::ModeApproval {
            mode,
            description,
            selected,
        } => {
            confirm::render_mode_approval(
                popup_area,
                buf,
                block,
                mode,
                description,
                *selected,
                theme,
            );
            render_bottom_hint(
                popup_area,
                buf,
                "↑↓ Select  Enter Confirm  Esc Single agent",
                theme.text_muted,
            );
        }
        PopupKind::SessionResume { items, selected } => {
            let ctx = PopupListCtx {
                popup_area,
                selected: *selected,
                scroll: popup.scroll,
                search: &popup.search,
                theme,
            };
            list::render_session_resume(&ctx, buf, block, items);
            render_popup_chrome(
                popup_area,
                hint_area,
                "type to search  ↑↓ navigate  Enter resume  Esc cancel",
                theme,
                buf,
            );
        }
        PopupKind::ThemeSelect {
            selected,
            dark_mode,
        } => {
            list::render_theme_select(
                popup_area,
                buf,
                block,
                *selected,
                *dark_mode,
                popup.scroll,
                theme,
            );
            render_popup_chrome(
                popup_area,
                hint_area,
                "↑↓ family  ← dark  light →  Enter apply  Esc cancel",
                theme,
                buf,
            );
        }
        PopupKind::LspInstall {
            language,
            server,
            install_cmd,
            selected,
        } => {
            confirm::render_lsp_install(
                popup_area,
                buf,
                block,
                &LspInstallCtx {
                    language,
                    server,
                    install_cmd,
                    selected: *selected,
                    theme,
                },
            );
            render_bottom_hint(
                popup_area,
                buf,
                "↑↓ select  Enter confirm  Esc skip",
                theme.text_muted,
            );
        }
        PopupKind::PiiWarning { findings, selected } => {
            confirm::render_pii_warning(popup_area, buf, block, findings, *selected, theme);
            render_popup_chrome(
                popup_area,
                hint_area,
                "↑↓ select  Enter confirm",
                theme,
                buf,
            );
        }
        PopupKind::McpToggle {
            items, selected, ..
        } => {
            let ctx = PopupListCtx {
                popup_area,
                selected: *selected,
                scroll: popup.scroll,
                search: &popup.search,
                theme,
            };
            list::render_mcp_toggle(&ctx, buf, block, items);
            render_popup_chrome(
                popup_area,
                hint_area,
                "type to search  ↑↓  Space toggle  Esc close",
                theme,
                buf,
            );
        }
        PopupKind::OptimizeSuggestion {
            model,
            session_count,
            items,
            selected,
            action,
        } => {
            confirm::render_optimize_suggestion(
                popup_area,
                buf,
                block,
                &OptimizeSuggestionCtx {
                    model,
                    session_count: *session_count,
                    items,
                    selected: *selected,
                    action: *action,
                    scroll: popup.scroll,
                    theme,
                },
            );
            render_popup_chrome(
                popup_area,
                hint_area,
                "↑↓ navigate  Space toggle  Tab action  Enter apply",
                theme,
                buf,
            );
        }
    }
}

pub(super) fn truncate_str(s: &str, max_cols: usize) -> String {
    use unicode_width::UnicodeWidthChar;
    let mut width = 0usize;
    let mut end = s.len();
    let mut truncated = false;
    for (i, c) in s.char_indices() {
        let cw = c.width().unwrap_or(1);
        if width + cw > max_cols.saturating_sub(1) {
            end = i;
            truncated = true;
            break;
        }
        width += cw;
    }
    if truncated {
        format!("{}", &s[..end])
    } else {
        s.to_string()
    }
}

pub(super) fn render_bottom_hint(popup_area: Rect, buf: &mut Buffer, hint: &str, color: Color) {
    let hint_x = popup_area.x + 2;
    let hint_y = popup_area.y + popup_area.height - 1;
    let max_w = popup_area.width.saturating_sub(4);
    if max_w > 0 {
        let hint_area = Rect::new(hint_x, hint_y, max_w.min(hint.len() as u16), 1);
        Paragraph::new(hint)
            .style(Style::default().fg(color))
            .render(hint_area, buf);
    }
}

pub(super) fn render_info_line<'a>(line: &'a str, theme: &crate::tui::theme::Theme) -> Line<'a> {
    // Git diff output — check before generic handlers to avoid misclassification
    if line.starts_with("@@") {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default()
                .fg(theme.accent)
                .add_modifier(Modifier::DIM),
        ));
    }
    if line.starts_with("diff ") || line.starts_with("index ") {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.text_muted),
        ));
    }
    // +++ / --- diff headers (file paths) — must come before generic +/- checks
    if line.starts_with("+++ ") || line.starts_with("--- ") {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.text_muted),
        ));
    }
    // Added lines — foreground + subtle background tint
    if line.starts_with('+') {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.diff_add_fg).bold(),
        ))
        .style(Style::default().bg(theme.diff_add_bg));
    }
    // Removed lines — foreground + subtle background tint
    if line.starts_with('-') {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.diff_remove_fg),
        ))
        .style(Style::default().bg(theme.diff_remove_bg));
    }
    // Section headers
    if let Some(rest) = line.strip_prefix("## ") {
        return Line::from(Span::styled(
            rest.to_string(),
            Style::default()
                .fg(theme.accent)
                .add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
        ));
    }
    if let Some(rest) = line.strip_prefix("# ") {
        return Line::from(Span::styled(
            rest.to_string(),
            Style::default()
                .fg(theme.accent)
                .add_modifier(Modifier::BOLD),
        ));
    }
    // Bold key-value
    if line.starts_with("**") {
        return Line::from(Span::styled(
            line.replace("**", ""),
            Style::default().fg(theme.text).add_modifier(Modifier::BOLD),
        ));
    }
    // Dimmed separators
    if line.starts_with("---") || line.starts_with("═══") {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.text_muted),
        ));
    }
    // Command lines: `/command   description` — name + desc two-column
    if line.trim_start().starts_with('/') {
        let trimmed = line.trim_start();
        // Split at first run of 2+ spaces
        if let Some(idx) = trimmed
            .char_indices()
            .zip(trimmed.char_indices().skip(1))
            .find(|((_, a), (_, b))| *a == ' ' && *b == ' ')
            .map(|((i, _), _)| i)
        {
            let cmd = &trimmed[..idx];
            let desc = trimmed[idx..].trim_start();
            return Line::from(vec![
                Span::styled(
                    format!("  {cmd:<22}", cmd = cmd),
                    Style::default()
                        .fg(theme.accent)
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(desc.to_string(), Style::default().fg(theme.text_dim)),
            ]);
        }
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.accent),
        ));
    }
    // Bullet points
    if line.trim_start().starts_with("") {
        let content = line.trim_start().trim_start_matches("");
        if let Some(idx) = content.find("   ") {
            let key = &content[..idx];
            let val = content[idx..].trim_start();
            return Line::from(vec![
                Span::styled(
                    format!("  • {key:<20}", key = key),
                    Style::default().fg(theme.accent),
                ),
                Span::styled(val.to_string(), Style::default().fg(theme.text_dim)),
            ]);
        }
        return Line::from(vec![
            Span::styled("", Style::default().fg(theme.accent)),
            Span::styled(content.to_string(), Style::default().fg(theme.text)),
        ]);
    }
    if line.trim_start().starts_with("- ") {
        return Line::from(vec![
            Span::styled("", Style::default().fg(theme.accent)),
            Span::styled(
                line.trim_start().trim_start_matches("- ").to_string(),
                Style::default().fg(theme.text),
            ),
        ]);
    }
    // Diff lines: +added / -removed / @@ hunk header
    if line.starts_with('+') && !line.starts_with("+++") {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.success),
        ));
    }
    if line.starts_with('-') && !line.starts_with("---") {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.error),
        ));
    }
    if line.starts_with("@@") {
        return Line::from(Span::styled(
            line.to_string(),
            Style::default().fg(theme.info),
        ));
    }
    // Diff stat summary lines: " file | 3 ++-" pattern
    if line.contains(" | ") {
        let parts: Vec<&str> = line.splitn(2, " | ").collect();
        if parts.len() == 2 {
            let right = parts[1];
            let stat_chars = right
                .chars()
                .rev()
                .take_while(|c| *c == '+' || *c == '-')
                .count();
            if stat_chars > 0 {
                let stat_start = right.len() - stat_chars;
                let count_part = &right[..stat_start];
                let symbols = &right[stat_start..];
                let mut spans = vec![
                    Span::styled(parts[0].to_string(), Style::default().fg(theme.accent)),
                    Span::styled(" | ", Style::default().fg(theme.text_muted)),
                    Span::styled(count_part.to_string(), Style::default().fg(theme.text_dim)),
                ];
                for ch in symbols.chars() {
                    let color = if ch == '+' {
                        theme.success
                    } else {
                        theme.error
                    };
                    spans.push(Span::styled(ch.to_string(), Style::default().fg(color)));
                }
                return Line::from(spans);
            }
        }
    }
    // Default
    Line::from(Span::styled(
        line.to_string(),
        Style::default().fg(theme.text),
    ))
}

pub(super) struct OptimizeSuggestionCtx<'a> {
    pub(super) model: &'a str,
    pub(super) session_count: usize,
    pub(super) items: &'a [crate::tui::state::OptimizeSuggestionItem],
    pub(super) selected: usize,
    pub(super) action: usize,
    pub(super) scroll: u16,
    pub(super) theme: &'a crate::tui::theme::Theme,
}

/// Render the parameter optimization suggestion popup.
pub(super) fn render_search_bar(
    area: Rect,
    buf: &mut Buffer,
    query: &str,
    matched: usize,
    total: usize,
    theme: &Theme,
) {
    let row = Rect::new(area.x, area.y, area.width, 1);

    // Background separator line
    Paragraph::new(Span::styled(
        " ".repeat(area.width as usize),
        Style::default().bg(theme.bg),
    ))
    .render(row, buf);

    // "/ query▌" on the left
    let cursor = if query.is_empty() { "" } else { "" };
    let search_text = format!(" / {}{}", query, cursor);

    // Match count on the right
    let count_text = if query.is_empty() {
        format!("  {} items  ", total)
    } else {
        format!("  {}/{} matched  ", matched, total)
    };
    let count_color = if matched == 0 && !query.is_empty() {
        theme.error
    } else {
        theme.text_muted
    };

    let count_len = count_text.len() as u16;
    let search_w = area.width.saturating_sub(count_len);

    // Render search text
    Paragraph::new(Span::styled(
        truncate_str(&search_text, search_w as usize),
        Style::default().fg(theme.accent),
    ))
    .render(Rect::new(area.x, area.y, search_w, 1), buf);

    // Render count (right-aligned)
    let count_x = area.x + search_w;
    Paragraph::new(Span::styled(count_text, Style::default().fg(count_color)))
        .render(Rect::new(count_x, area.y, count_len, 1), buf);
}