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
use ratatui::prelude::*;
use ratatui::widgets::{Block, Paragraph, Wrap};

use crate::tui::theme::Theme;

use super::{OptimizeSuggestionCtx, truncate_str};

pub(super) fn render_queue_confirm(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    pending: &[String],
    selected: usize,
    _scroll: u16,
    theme: &crate::tui::theme::Theme,
) {
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    let mut lines: Vec<Line> = vec![
        Line::from(""),
        Line::from(Span::styled(
            format!("  {} message(s) pending in queue.", pending.len()),
            Style::default().fg(theme.text),
        )),
        Line::from(""),
    ];

    // Show pending messages preview (up to 5)
    let show = pending.len().min(5);
    for msg in &pending[..show] {
        let preview = if msg.len() > 60 {
            format!("{}", crate::util::truncate_bytes(msg, 57))
        } else {
            msg.clone()
        };
        lines.push(Line::from(Span::styled(
            format!("{preview}"),
            Style::default().fg(theme.text_muted),
        )));
    }
    if pending.len() > 5 {
        lines.push(Line::from(Span::styled(
            format!("  ... ({} more)", pending.len() - 5),
            Style::default().fg(theme.text_dim),
        )));
    }

    lines.push(Line::from(""));

    let choices = ["Continue", "Cancel all queued"];
    for (i, label) in choices.iter().enumerate() {
        let (prefix, style) = if i == selected {
            (
                "",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        } else {
            ("    ", Style::default().fg(theme.text))
        };
        lines.push(Line::from(Span::styled(format!("{prefix}{label}"), style)));
    }

    Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .render(inner, buf);
}

/// Render the continuation confirmation popup.
pub(super) fn render_continuation_confirm(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    content: &str,
    selected: usize,
    theme: &crate::tui::theme::Theme,
) {
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    let mut lines: Vec<Line> = vec![Line::from("")];
    for line in content.lines() {
        lines.push(Line::from(Span::styled(
            format!("  {line}"),
            Style::default().fg(theme.text),
        )));
    }
    lines.push(Line::from(""));

    let choices = ["Continue", "Stop"];
    for (i, label) in choices.iter().enumerate() {
        let (prefix, style) = if i == selected {
            (
                "",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        } else {
            ("    ", Style::default().fg(theme.text))
        };
        lines.push(Line::from(Span::styled(format!("{prefix}{label}"), style)));
    }

    Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .render(inner, buf);
}

/// Render the init confirmation popup (merge vs replace for existing AGENTS.md).
pub(super) fn render_init_confirm(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    selected: usize,
    theme: &crate::tui::theme::Theme,
) {
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    let mut lines: Vec<Line> = vec![
        Line::from(""),
        Line::from(Span::styled(
            "  AGENTS.md already exists. How should /init proceed?",
            Style::default().fg(theme.text),
        )),
        Line::from(""),
    ];

    let choices = [
        ("Merge", "Preserve custom content, update changed sections"),
        (
            "Replace",
            "Discard existing file and regenerate from scratch",
        ),
    ];
    for (i, (label, hint)) in choices.iter().enumerate() {
        let (prefix, style) = if i == selected {
            (
                "",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        } else {
            ("    ", Style::default().fg(theme.text))
        };
        lines.push(Line::from(Span::styled(format!("{prefix}{label}"), style)));
        lines.push(Line::from(Span::styled(
            format!("       {hint}"),
            Style::default().fg(theme.text_muted),
        )));
    }

    lines.push(Line::from(""));
    Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .render(inner, buf);
}

/// Render the mode approval popup (fork/hive selection for this session).
pub(super) fn render_mode_approval(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    _mode: &str,
    description: &str,
    selected: usize,
    theme: &crate::tui::theme::Theme,
) {
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    let mut lines: Vec<Line> = vec![
        Line::from(""),
        Line::from(Span::styled(
            format!("  {description}"),
            Style::default().fg(theme.text),
        )),
        Line::from(""),
    ];

    let choices = [
        (
            "Fork mode",
            "Coordinator splits → parallel execution → merge results",
        ),
        (
            "Hive mode",
            "Consensus communication between agents, coordinator supervises",
        ),
        (
            "Flock mode",
            "Real-time messaging between agents (experimental)",
        ),
        (
            "Single agent",
            "Sequential processing in the conventional way",
        ),
    ];
    for (i, (label, _hint)) in choices.iter().enumerate() {
        let (prefix, style) = if i == selected {
            (
                "",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        } else {
            ("    ", Style::default().fg(theme.text))
        };
        lines.push(Line::from(Span::styled(format!("{prefix}{label}"), style)));
    }

    lines.push(Line::from(""));
    let hint_text = choices
        .get(selected)
        .map(|(_, h)| *h)
        .unwrap_or("Sequential processing by a single agent in the conventional way");
    lines.push(Line::from(Span::styled(
        format!("  {hint_text}"),
        Style::default().fg(theme.text_muted),
    )));

    Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .render(inner, buf);
}

pub(super) struct LspInstallCtx<'a> {
    pub(super) language: &'a str,
    pub(super) server: &'a str,
    pub(super) install_cmd: &'a str,
    pub(super) selected: usize,
    pub(super) theme: &'a Theme,
}

/// Render the LSP install prompt popup.
pub(super) fn render_lsp_install(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    ctx: &LspInstallCtx<'_>,
) {
    let language = ctx.language;
    let server = ctx.server;
    let install_cmd = ctx.install_cmd;
    let selected = ctx.selected;
    let theme = ctx.theme;
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    let lines: Vec<Line> = vec![
        Line::from(""),
        Line::from(Span::styled(
            format!("  LSP server not found for {language}"),
            Style::default()
                .fg(theme.warning)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(""),
        Line::from(vec![
            Span::styled("  Server:  ", Style::default().fg(theme.text_dim)),
            Span::styled(
                server,
                Style::default().fg(theme.text).add_modifier(Modifier::BOLD),
            ),
        ]),
        Line::from(vec![
            Span::styled("  Install: ", Style::default().fg(theme.text_dim)),
            Span::styled(install_cmd, Style::default().fg(theme.accent)),
        ]),
        Line::from(""),
        Line::from(if selected == 0 {
            Span::styled(
                "  ▶ Install now",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        } else {
            Span::styled("    Install now", Style::default().fg(theme.text))
        }),
        Line::from(if selected == 1 {
            Span::styled(
                "  ▶ Skip",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        } else {
            Span::styled("    Skip", Style::default().fg(theme.text))
        }),
    ];

    Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .render(inner, buf);
}

pub(super) fn render_pii_warning(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    findings: &[crate::security::pii_filter::PiiMatch],
    selected: usize,
    theme: &Theme,
) {
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    let mut lines: Vec<Line> = vec![
        Line::from(""),
        Line::from(Span::styled(
            "  ⚠ Sensitive data detected in your input:",
            Style::default()
                .fg(theme.warning)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(""),
    ];

    let show = findings.len().min(8);
    for f in &findings[..show] {
        lines.push(Line::from(vec![
            Span::styled("", Style::default().fg(theme.warning)),
            Span::styled(
                format!("{}: ", f.category),
                Style::default().fg(theme.text).add_modifier(Modifier::BOLD),
            ),
            Span::styled(&f.masked, Style::default().fg(theme.text_muted)),
        ]));
    }
    if findings.len() > 8 {
        lines.push(Line::from(Span::styled(
            format!("  ... ({} more)", findings.len() - 8),
            Style::default().fg(theme.text_dim),
        )));
    }

    lines.push(Line::from(""));

    let choices = ["Proceed anyway", "Cancel"];
    for (i, label) in choices.iter().enumerate() {
        let (prefix, style) = if i == selected {
            (
                "",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        } else {
            ("    ", Style::default().fg(theme.text))
        };
        lines.push(Line::from(Span::styled(format!("{prefix}{label}"), style)));
    }

    Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .render(inner, buf);
}

pub(super) fn render_optimize_suggestion(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    ctx: &OptimizeSuggestionCtx<'_>,
) {
    let model = ctx.model;
    let session_count = ctx.session_count;
    let items = ctx.items;
    let selected = ctx.selected;
    let action = ctx.action;
    let scroll = ctx.scroll;
    let theme = ctx.theme;
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    // Clear inner area
    for row in inner.y..inner.y + inner.height {
        for col in inner.x..inner.x + inner.width {
            buf[(col, row)]
                .set_char(' ')
                .set_bg(theme.bg_surface)
                .set_fg(theme.text);
        }
    }

    let mut y = inner.y;
    let w = inner.width as usize;

    // Header: "Based on N sessions with <model>"
    if y < inner.y + inner.height {
        let header = format!("  Based on {} sessions with {}", session_count, model,);
        Paragraph::new(Span::styled(
            truncate_str(&header, w),
            Style::default().fg(theme.text_muted).italic(),
        ))
        .render(Rect::new(inner.x, y, inner.width, 1), buf);
        y += 2;
    }

    // Items
    for (i, item) in items.iter().enumerate() {
        if y.saturating_sub(scroll) >= inner.y + inner.height {
            break;
        }

        let is_selected = i == selected;
        let checkbox = if item.apply { "[x]" } else { "[ ]" };
        let confidence_pct = (item.confidence * 100.0) as u8;

        // Line 1: checkbox + label: current → suggested (confidence%)
        let line1 = format!(
            "  {} {}: {}{} ({confidence_pct}%)",
            checkbox, item.label, item.current, item.suggested,
        );
        let line1_style = if is_selected {
            Style::default().fg(theme.accent).bold()
        } else {
            Style::default().fg(theme.text)
        };

        if y >= inner.y && y < inner.y + inner.height {
            let display_y = y.saturating_sub(scroll);
            if display_y >= inner.y && display_y < inner.y + inner.height {
                Paragraph::new(Span::styled(truncate_str(&line1, w), line1_style))
                    .render(Rect::new(inner.x, display_y, inner.width, 1), buf);
            }
        }
        y += 1;

        // Line 2: reason (indented, muted)
        let line2 = format!("      {}", item.reason);
        if y >= inner.y && y < inner.y + inner.height {
            let display_y = y.saturating_sub(scroll);
            if display_y >= inner.y && display_y < inner.y + inner.height {
                // Wrap reason if too long
                let max_w = w.saturating_sub(6);
                let reason_display = if item.reason.len() > max_w {
                    format!("      {}", &item.reason[..max_w.saturating_sub(1)])
                } else {
                    line2
                };
                Paragraph::new(Span::styled(
                    reason_display,
                    Style::default().fg(theme.text_muted),
                ))
                .render(Rect::new(inner.x, display_y, inner.width, 1), buf);
            }
        }
        y += 2; // blank line between items
    }

    // Action buttons at the bottom
    let btn_y = inner.y + inner.height.saturating_sub(2);
    if btn_y > inner.y {
        let apply_style = if action == 0 {
            Style::default().fg(theme.bg).bg(theme.accent).bold()
        } else {
            Style::default().fg(theme.text)
        };
        let dismiss_style = if action == 1 {
            Style::default().fg(theme.bg).bg(theme.text_muted).bold()
        } else {
            Style::default().fg(theme.text_muted)
        };

        let apply_span = Span::styled("  Apply Selected  ", apply_style);
        let gap_span = Span::raw("  ");
        let dismiss_span = Span::styled("  Dismiss  ", dismiss_style);

        Paragraph::new(Line::from(vec![
            Span::raw("    "),
            apply_span,
            gap_span,
            dismiss_span,
        ]))
        .render(Rect::new(inner.x, btn_y, inner.width, 1), buf);
    }
}

/// Render the tool approval popup (Manual / Auto mode with unsafe tool).
pub(super) fn render_tool_approval(
    popup_area: Rect,
    buf: &mut Buffer,
    block: Block,
    tool_name: &str,
    tool_args: &str,
    selected: usize,
    theme: &crate::tui::theme::Theme,
) {
    let inner = block.inner(popup_area);
    block.render(popup_area, buf);

    // Truncate args preview to avoid overflowing the popup width.
    let max_args_len = inner.width.saturating_sub(4) as usize;
    let args_preview = if tool_args.len() > max_args_len {
        format!(
            "{}",
            crate::util::truncate_bytes(tool_args, max_args_len.saturating_sub(1))
        )
    } else {
        tool_args.to_string()
    };

    let mut lines: Vec<Line> = vec![
        Line::from(""),
        Line::from(Span::styled(
            format!("  Tool: {tool_name}"),
            Style::default()
                .fg(theme.accent)
                .add_modifier(Modifier::BOLD),
        )),
        Line::from(Span::styled(
            format!("  Args: {args_preview}"),
            Style::default().fg(theme.text_muted),
        )),
        Line::from(""),
        Line::from(Span::styled(
            "  Allow this tool to execute?",
            Style::default().fg(theme.text),
        )),
        Line::from(""),
    ];

    let choices = ["Approve", "Approve for session", "Deny"];
    for (i, label) in choices.iter().enumerate() {
        let (prefix, style) = if i == selected {
            (
                "",
                Style::default()
                    .fg(theme.accent)
                    .add_modifier(Modifier::BOLD),
            )
        } else {
            ("    ", Style::default().fg(theme.text))
        };
        lines.push(Line::from(Span::styled(format!("{prefix}{label}"), style)));
    }

    Paragraph::new(lines)
        .wrap(Wrap { trim: false })
        .render(inner, buf);
}