zeph-tui 0.22.4

Ratatui-based TUI dashboard with real-time metrics for Zeph
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span};

use crate::metrics::{MetricsSnapshot, SecurityEventCategory};
use crate::theme::Theme;
use crate::widgets::panel;

/// Build the security panel's content lines: a header, then either "No security events." or
/// the full metric list plus recent-event entries.
///
/// Pure function of `metrics` and `theme` — never of the allocated `Rect` — so
/// [`desired_height`] and [`render`] can never disagree about how many rows this panel needs.
pub(crate) fn lines(metrics: &MetricsSnapshot, theme: &Theme) -> Vec<Line<'static>> {
    let event_count = metrics.security_events.len();
    let header_text = format!(
        "security · {event_count} event{}",
        if event_count == 1 { "" } else { "s" }
    );
    let mut out = vec![Line::from(Span::styled(
        header_text,
        theme.system_message.add_modifier(Modifier::BOLD),
    ))];

    let all_zero = metrics.sanitizer_runs == 0
        && metrics.sanitizer_injection_flags == 0
        && metrics.sanitizer_truncations == 0
        && metrics.quarantine_invocations == 0
        && metrics.quarantine_failures == 0
        && metrics.exfiltration_images_blocked == 0
        && metrics.exfiltration_tool_urls_flagged == 0
        && metrics.exfiltration_memory_guards == 0
        && metrics.pre_execution_blocks == 0
        && metrics.pre_execution_warnings == 0
        && metrics.egress_requests_total == 0
        && metrics.egress_blocked_total == 0
        && metrics.security_events.is_empty();

    if all_zero {
        out.push(Line::from(Span::styled(
            "No security events.",
            theme.system_message,
        )));
        return out;
    }

    let base = theme.system_message;
    let flag_style = Style::default()
        .fg(Color::Yellow)
        .add_modifier(Modifier::BOLD);
    let block_style = Style::default().fg(Color::Red).add_modifier(Modifier::BOLD);

    out.extend(build_metric_items(metrics, base, flag_style, block_style));
    append_event_items(metrics, &mut out, base, flag_style, block_style);
    out
}

/// Number of rows the security panel needs to show all of `metrics` without truncation.
#[must_use]
pub fn desired_height(metrics: &MetricsSnapshot, theme: &Theme) -> u16 {
    u16::try_from(lines(metrics, theme).len()).unwrap_or(u16::MAX)
}

pub fn render(metrics: &MetricsSnapshot, frame: &mut Frame, area: Rect, theme: &Theme) {
    panel::render_lines(frame, area, lines(metrics, theme), theme);
}

/// Build a plain styled label+value line, using `base` style for both.
fn plain_metric_item(
    label: &'static str,
    value: impl std::fmt::Display,
    base: Style,
) -> Line<'static> {
    Line::from(Span::styled(format!("{label}{value}"), base))
}

/// Build a label+value line whose value span switches to `alert_style` when the value is
/// non-zero.
fn styled_counter_item(
    label: &'static str,
    value: u64,
    base: Style,
    alert_style: Style,
) -> Line<'static> {
    Line::from(vec![
        Span::styled(label, base),
        Span::styled(
            value.to_string(),
            if value > 0 { alert_style } else { base },
        ),
    ])
}

fn build_sanitizer_items(
    metrics: &MetricsSnapshot,
    base: Style,
    flag_style: Style,
) -> Vec<Line<'static>> {
    vec![
        plain_metric_item("Sanitizer runs:    ", metrics.sanitizer_runs, base),
        styled_counter_item(
            "Inj flags:         ",
            metrics.sanitizer_injection_flags,
            base,
            flag_style,
        ),
        plain_metric_item("Truncations:       ", metrics.sanitizer_truncations, base),
        plain_metric_item("Quarantine calls:  ", metrics.quarantine_invocations, base),
        plain_metric_item("Quarantine fails:  ", metrics.quarantine_failures, base),
    ]
}

fn build_exfiltration_items(
    metrics: &MetricsSnapshot,
    base: Style,
    block_style: Style,
) -> Vec<Line<'static>> {
    vec![
        styled_counter_item(
            "Exfil images:      ",
            metrics.exfiltration_images_blocked,
            base,
            block_style,
        ),
        styled_counter_item(
            "Exfil URLs:        ",
            metrics.exfiltration_tool_urls_flagged,
            base,
            block_style,
        ),
        plain_metric_item(
            "Memory guards:     ",
            metrics.exfiltration_memory_guards,
            base,
        ),
    ]
}

fn build_pre_execution_items(
    metrics: &MetricsSnapshot,
    base: Style,
    flag_style: Style,
    block_style: Style,
) -> Vec<Line<'static>> {
    vec![
        styled_counter_item(
            "Verify blocks:     ",
            metrics.pre_execution_blocks,
            base,
            block_style,
        ),
        styled_counter_item(
            "Verify warnings:   ",
            metrics.pre_execution_warnings,
            base,
            flag_style,
        ),
    ]
}

fn build_egress_items(
    metrics: &MetricsSnapshot,
    base: Style,
    flag_style: Style,
    block_style: Style,
) -> Vec<Line<'static>> {
    vec![
        plain_metric_item("Egress requests:   ", metrics.egress_requests_total, base),
        styled_counter_item(
            "Egress blocked:    ",
            metrics.egress_blocked_total,
            base,
            block_style,
        ),
        styled_counter_item(
            "Egress dropped:    ",
            metrics.egress_dropped_total,
            base,
            flag_style,
        ),
    ]
}

fn build_metric_items(
    metrics: &MetricsSnapshot,
    base: Style,
    flag_style: Style,
    block_style: Style,
) -> Vec<Line<'static>> {
    let mut items = build_sanitizer_items(metrics, base, flag_style);
    items.extend(build_exfiltration_items(metrics, base, block_style));
    items.extend(build_pre_execution_items(
        metrics,
        base,
        flag_style,
        block_style,
    ));
    items.extend(build_egress_items(metrics, base, flag_style, block_style));
    items
}

fn append_event_items(
    metrics: &MetricsSnapshot,
    items: &mut Vec<Line<'static>>,
    base: Style,
    flag_style: Style,
    block_style: Style,
) {
    if metrics.security_events.is_empty() {
        return;
    }
    items.push(Line::from(Span::styled(
        "Recent events:",
        Style::default()
            .fg(Color::DarkGray)
            .add_modifier(Modifier::UNDERLINED),
    )));

    // Show last 5 events (most recent last).
    let start = metrics.security_events.len().saturating_sub(5);
    for ev in metrics.security_events.range(start..) {
        let (cat_str, cat_style) = match ev.category {
            SecurityEventCategory::InjectionFlag => ("[inj]  ", flag_style),
            SecurityEventCategory::InjectionBlocked => ("[injb] ", block_style),
            SecurityEventCategory::ExfiltrationBlock => ("[exfil]", block_style),
            SecurityEventCategory::Quarantine => ("[quar] ", Style::default().fg(Color::Cyan)),
            SecurityEventCategory::Truncation => ("[trunc]", Style::default().fg(Color::DarkGray)),
            SecurityEventCategory::RateLimit => ("[rlim] ", Style::default().fg(Color::Yellow)),
            SecurityEventCategory::MemoryValidation => {
                ("[mval] ", Style::default().fg(Color::Magenta))
            }
            SecurityEventCategory::PreExecutionBlock => ("[pexb] ", block_style),
            SecurityEventCategory::PreExecutionWarn => ("[pexw] ", flag_style),
            SecurityEventCategory::ResponseVerification => ("[rver] ", flag_style),
            SecurityEventCategory::CausalIpiFlag => ("[cipi] ", flag_style),
            SecurityEventCategory::CrossBoundaryMcpToAcp => {
                ("[xbnd] ", Style::default().fg(Color::Red))
            }
            SecurityEventCategory::VigilFlag => ("[vigi] ", block_style),
            SecurityEventCategory::GoalDrift => ("[gdft] ", flag_style),
            _ => ("[unkn] ", Style::default().fg(Color::DarkGray)),
        };
        let hm = format_hm(ev.timestamp);
        items.push(Line::from(vec![
            Span::styled(format!("{hm} "), Style::default().fg(Color::DarkGray)),
            Span::styled(cat_str, cat_style),
            Span::styled(format!(" {}", ev.source), base),
        ]));
        items.push(Line::from(Span::styled(
            format!("  {}", ev.detail),
            Style::default().fg(Color::DarkGray),
        )));
    }
}

fn format_hm(ts: u64) -> String {
    #[allow(clippy::cast_possible_wrap)]
    chrono::DateTime::from_timestamp(ts as i64, 0).map_or_else(
        || "??:??".to_owned(),
        |dt| dt.with_timezone(&chrono::Local).format("%H:%M").to_string(),
    )
}

#[cfg(test)]
mod tests {
    use std::collections::VecDeque;

    use zeph_common::SecurityEventCategory;
    use zeph_core::metrics::SecurityEvent;

    use super::*;
    use crate::test_utils::render_to_string;

    #[test]
    fn renders_no_events_message_when_all_zero() {
        let metrics = MetricsSnapshot::default();
        let output = render_to_string(40, 10, |frame, area| {
            let theme = crate::theme::Theme::default();
            render(&metrics, frame, area, &theme);
        });
        assert!(output.contains("No security events."));
    }

    #[test]
    fn renders_injection_flag_count() {
        let metrics = MetricsSnapshot {
            sanitizer_injection_flags: 3,
            ..MetricsSnapshot::default()
        };
        let output = render_to_string(40, 12, |frame, area| {
            let theme = crate::theme::Theme::default();
            render(&metrics, frame, area, &theme);
        });
        assert!(output.contains('3'));
    }

    #[test]
    fn renders_recent_events() {
        let mut events = VecDeque::new();
        events.push_back(SecurityEvent::new(
            SecurityEventCategory::InjectionFlag,
            "web_scrape",
            "Detected pattern: ignore previous",
        ));
        let metrics = MetricsSnapshot {
            sanitizer_injection_flags: 1,
            security_events: events,
            ..MetricsSnapshot::default()
        };
        let output = render_to_string(50, 25, |frame, area| {
            let theme = crate::theme::Theme::default();
            render(&metrics, frame, area, &theme);
        });
        assert!(output.contains("web_scrape") || output.contains("inj"));
    }

    #[test]
    fn renders_exfiltration_block_category() {
        let mut events = VecDeque::new();
        events.push_back(SecurityEvent::new(
            SecurityEventCategory::ExfiltrationBlock,
            "llm_output",
            "1 markdown image(s) blocked",
        ));
        let metrics = MetricsSnapshot {
            exfiltration_images_blocked: 1,
            security_events: events,
            ..MetricsSnapshot::default()
        };
        let output = render_to_string(50, 25, |frame, area| {
            let theme = crate::theme::Theme::default();
            render(&metrics, frame, area, &theme);
        });
        assert!(
            output.contains("Exfil") || output.contains("exfil") || output.contains("llm_output")
        );
    }

    #[test]
    fn renders_quarantine_category() {
        let mut events = VecDeque::new();
        events.push_back(SecurityEvent::new(
            SecurityEventCategory::Quarantine,
            "web_scrape",
            "Content quarantined, facts extracted",
        ));
        let metrics = MetricsSnapshot {
            quarantine_invocations: 1,
            security_events: events,
            ..MetricsSnapshot::default()
        };
        let output = render_to_string(50, 25, |frame, area| {
            let theme = crate::theme::Theme::default();
            render(&metrics, frame, area, &theme);
        });
        assert!(output.contains("quar") || output.contains("web_scrape"));
    }

    #[test]
    fn renders_only_last_5_events_when_more_exist() {
        let mut metrics = MetricsSnapshot {
            sanitizer_injection_flags: 8,
            ..MetricsSnapshot::default()
        };
        for i in 0..8u64 {
            metrics.security_events.push_back(SecurityEvent::new(
                SecurityEventCategory::InjectionFlag,
                format!("source_{i}"),
                format!("detail_{i}"),
            ));
        }
        let output = render_to_string(60, 30, |frame, area| {
            let theme = crate::theme::Theme::default();
            render(&metrics, frame, area, &theme);
        });
        // Last 5: sources 3..7 should appear, first 3 should not.
        assert!(output.contains("source_7"), "last event must be rendered");
        assert!(
            output.contains("source_3"),
            "5th-from-last must be rendered"
        );
        assert!(
            !output.contains("source_2"),
            "6th-from-last must NOT be rendered"
        );
    }

    #[test]
    fn renders_without_panic_on_zero_height() {
        let metrics = MetricsSnapshot::default();
        // height=0 means inner area is zero — must not panic
        render_to_string(40, 0, |frame, area| {
            let theme = crate::theme::Theme::default();
            render(&metrics, frame, area, &theme);
        });
    }

    // ── desired_height / render parity (#6675 tester gap 2) ─────────────────────

    #[test]
    fn desired_height_matches_actual_rendered_row_count() {
        use crate::test_utils::count_non_blank_rows;

        let mut events = VecDeque::new();
        events.push_back(SecurityEvent::new(
            SecurityEventCategory::InjectionFlag,
            "web_scrape",
            "Detected pattern: ignore previous",
        ));
        let metrics = MetricsSnapshot {
            sanitizer_runs: 10,
            sanitizer_injection_flags: 1,
            security_events: events,
            ..MetricsSnapshot::default()
        };
        let theme = crate::theme::Theme::default();
        let expected = desired_height(&metrics, &theme);

        let output = render_to_string(80, 40, |frame, area| {
            render(&metrics, frame, area, &theme);
        });
        assert_eq!(
            u16::try_from(count_non_blank_rows(&output)).unwrap(),
            expected,
            "desired_height must match the actual non-blank rendered row count, got:\n{output}"
        );
    }

    // ── overflow indicator via render() (#6675 tester gap 3) ────────────────────

    #[test]
    fn render_shows_overflow_indicator_when_area_too_small() {
        let metrics = MetricsSnapshot {
            sanitizer_runs: 10,
            sanitizer_injection_flags: 1,
            ..MetricsSnapshot::default()
        };
        let theme = crate::theme::Theme::default();
        // The always-present 13 metric rows + header comfortably exceed 2 rows.
        let output = render_to_string(50, 2, |frame, area| {
            render(&metrics, frame, area, &theme);
        });
        assert!(
            output.contains("more"),
            "must show overflow indicator when granted area is smaller than content, got:\n{output}"
        );
    }
}