git-cognitive 0.1.1

Cognitive debt detection and management for Git repositories — audit, endorse, and surface AI-attributed risk
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
use anyhow::Result;
use crossterm::{
    cursor,
    event::{self, Event, KeyCode, KeyModifiers},
    execute, queue,
    style::{self, Color, Stylize},
    terminal::{self, ClearType},
};
use std::io::{self, Write};

use crate::cognitive_debt::{ActivityItem, EndorsementStatus};

#[derive(Clone)]
pub struct PickerItem {
    pub sha: String,
    pub short_sha: String,
    pub classification: String,
    pub title: String,
    pub friction: f32,
    pub attribution_pct: Option<f32>,
    pub endorsement_status: String,
    pub zombie: bool,
    pub days_old: Option<u64>,
}

impl PickerItem {
    pub fn from_activity(item: &ActivityItem) -> Self {
        let days_old = parse_days_old(&item.audited_at);
        Self {
            sha: item.id.clone(),
            short_sha: item.id[..8.min(item.id.len())].to_string(),
            classification: item.classification.to_string(),
            title: item.title.chars().take(63).collect(),
            friction: item.cognitive_friction_score,
            attribution_pct: item.attribution_pct,
            endorsement_status: item.endorsement_status.to_string(),
            zombie: item.zombie,
            days_old,
        }
    }
}

fn parse_days_old(audited_at: &str) -> Option<u64> {
    if audited_at.is_empty() {
        return None;
    }
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .ok()?
        .as_secs();

    let out = std::process::Command::new("date")
        .args(["-j", "-f", "%Y-%m-%dT%H:%M:%S", &audited_at[..19], "+%s"])
        .output()
        .ok()?;

    let ts: u64 = if out.status.success() {
        String::from_utf8_lossy(&out.stdout).trim().parse().ok()?
    } else {
        let out2 = std::process::Command::new("date")
            .args(["-d", audited_at, "+%s"])
            .output()
            .ok()?;
        String::from_utf8_lossy(&out2.stdout).trim().parse().ok()?
    };

    Some((now.saturating_sub(ts)) / 86400)
}

struct Col {
    x: u16,
    width: u16,
}

const COL_CURSOR: Col = Col { x: 0, width: 2 };
const COL_BADGE: Col = Col { x: 2, width: 7 };
const COL_ZOMBIE: Col = Col { x: 9, width: 2 };
const COL_TITLE: Col = Col { x: 11, width: 59 };
const COL_FRICTION: Col = Col { x: 70, width: 11 };
const COL_ATTRIB: Col = Col { x: 81, width: 7 };
const COL_DAYS: Col = Col { x: 88, width: 5 };
const COL_STATUS: Col = Col { x: 93, width: 12 };
const ROW_WIDTH: u16 = 105;

fn write_cell(
    stdout: &mut io::Stdout,
    row: u16,
    col: &Col,
    text: &str,
    fg: Option<Color>,
    bg: Option<Color>,
    bold: bool,
) -> Result<()> {
    let chars: Vec<char> = text.chars().collect();
    let visible_len = chars.len().min(col.width as usize);
    let cell: String = chars[..visible_len].iter().collect();
    let padding = " ".repeat((col.width as usize).saturating_sub(visible_len));
    let full = format!("{}{}", cell, padding);

    queue!(stdout, cursor::MoveTo(col.x, row))?;

    let mut styled = style::style(full);
    if let Some(f) = fg {
        styled = styled.with(f);
    }
    if let Some(b) = bg {
        styled = styled.on(b);
    }
    if bold {
        styled = styled.bold();
    }

    queue!(stdout, style::PrintStyledContent(styled))?;
    Ok(())
}

fn badge_colors(c: &str) -> (&'static str, Color, Color) {
    match c {
        "risk" => ("RISK", Color::White, Color::Red),
        "tech_debt" => ("DEBT", Color::Black, Color::Yellow),
        "new_feature" => ("FEAT", Color::White, Color::Blue),
        "bug_fix" => ("FIX ", Color::Black, Color::Green),
        "refactor" => ("RFCT", Color::White, Color::DarkGrey),
        "minor" => ("MIN ", Color::White, Color::DarkGrey),
        "dependency_update" => ("DEP ", Color::White, Color::DarkGrey),
        _ => ("OTH ", Color::White, Color::DarkGrey),
    }
}

fn status_color(s: &str) -> (&'static str, Color) {
    match s {
        "endorsed" => ("endorsed", Color::Green),
        "excluded" => ("excluded", Color::DarkGrey),
        _ => ("unendorsed", Color::Red),
    }
}

fn friction_bar(score: f32) -> (String, Color) {
    let filled = (score * 10.0).round() as usize;
    let bar: String = (0..10)
        .map(|i| if i < filled { '' } else { '' })
        .collect();
    let color = if score >= 0.7 {
        Color::Red
    } else if score >= 0.4 {
        Color::Yellow
    } else {
        Color::Green
    };
    (bar, color)
}

fn render_row(
    stdout: &mut io::Stdout,
    row: u16,
    item: &PickerItem,
    is_selected: bool,
) -> Result<()> {
    let bg = if is_selected {
        Some(Color::DarkBlue)
    } else {
        None
    };

    queue!(stdout, cursor::MoveTo(0, row))?;
    let blank = " ".repeat(ROW_WIDTH as usize);
    if let Some(b) = bg {
        queue!(stdout, style::PrintStyledContent(style::style(blank).on(b)))?;
    } else {
        queue!(stdout, style::Print(blank))?;
    }

    let cursor_char = if is_selected { "" } else { " " };
    write_cell(
        stdout,
        row,
        &COL_CURSOR,
        cursor_char,
        Some(Color::White),
        bg,
        false,
    )?;

    let (badge_text, badge_fg, badge_bg) = badge_colors(&item.classification);
    write_cell(
        stdout,
        row,
        &COL_BADGE,
        badge_text,
        Some(badge_fg),
        Some(badge_bg),
        true,
    )?;

    if item.zombie {
        write_cell(stdout, row, &COL_ZOMBIE, "", Some(Color::Red), bg, true)?;
    }

    write_cell(
        stdout,
        row,
        &COL_TITLE,
        &item.title,
        Some(Color::White),
        bg,
        false,
    )?;

    let (bar, bar_color) = friction_bar(item.friction);
    write_cell(stdout, row, &COL_FRICTION, &bar, Some(bar_color), bg, false)?;

    let attrib = item
        .attribution_pct
        .map(|p| format!("{:3.0}%ai", p * 100.0))
        .unwrap_or_else(|| "      ".to_string());
    write_cell(
        stdout,
        row,
        &COL_ATTRIB,
        &attrib,
        Some(Color::DarkGrey),
        bg,
        false,
    )?;

    let days = item
        .days_old
        .map(|d| format!("{}d", d))
        .unwrap_or_else(|| "-".to_string());
    write_cell(
        stdout,
        row,
        &COL_DAYS,
        &days,
        Some(Color::DarkGrey),
        bg,
        false,
    )?;

    let (status_text, status_color) = status_color(&item.endorsement_status);
    write_cell(
        stdout,
        row,
        &COL_STATUS,
        status_text,
        Some(status_color),
        bg,
        false,
    )?;

    Ok(())
}

pub fn run_picker(items: Vec<PickerItem>) -> Result<Option<String>> {
    if items.is_empty() {
        println!("No items to review.");
        return Ok(None);
    }

    let mut stdout = io::stdout();
    terminal::enable_raw_mode()?;
    execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;

    let result = picker_loop(&mut stdout, &items);

    execute!(stdout, terminal::LeaveAlternateScreen, cursor::Show)?;
    terminal::disable_raw_mode()?;

    result
}

fn picker_loop(stdout: &mut io::Stdout, items: &[PickerItem]) -> Result<Option<String>> {
    let mut selected = 0usize;
    let mut status_msg = String::new();

    loop {
        let (_, rows) = terminal::size().unwrap_or((120, 40));
        let header_rows = 3u16;
        let footer_rows = 4u16;
        let visible_rows = (rows.saturating_sub(header_rows + footer_rows)) as usize;

        execute!(stdout, terminal::Clear(ClearType::All))?;

        let header = format!(
            " git-semantic endorse  ({} items, {} unendorsed) ",
            items.len(),
            items
                .iter()
                .filter(|i| i.endorsement_status == "unendorsed")
                .count()
        );
        queue!(
            stdout,
            cursor::MoveTo(0, 0),
            style::PrintStyledContent(style::style(header).on(Color::DarkBlue).white().bold())
        )?;

        let col_header = format!(
            "  {:<6} {:<2} {:<59}{:<11}{:<7}{:<5}{}",
            "TYPE", "", "TITLE", "FRICTION", "AI", "AGE", "STATUS"
        );
        queue!(
            stdout,
            cursor::MoveTo(0, 1),
            style::PrintStyledContent(style::style(col_header).dark_grey())
        )?;

        let scroll_offset = if selected >= visible_rows {
            selected - visible_rows + 1
        } else {
            0
        };

        for (i, item) in items
            .iter()
            .enumerate()
            .skip(scroll_offset)
            .take(visible_rows)
        {
            let row = header_rows + (i - scroll_offset) as u16;
            render_row(stdout, row, item, i == selected)?;
        }

        let detail_row = rows - footer_rows;
        let item = &items[selected];
        let detail = format!(
            " {}  friction {:.2}  {}",
            item.short_sha, item.friction, item.endorsement_status,
        );
        queue!(
            stdout,
            cursor::MoveTo(0, detail_row),
            style::PrintStyledContent(style::style(&*detail).white())
        )?;

        let help = "  ↑↓/jk navigate   e/Enter endorse   s git show   q quit";
        queue!(
            stdout,
            cursor::MoveTo(0, detail_row + 1),
            style::PrintStyledContent(style::style(help).dark_grey())
        )?;

        if !status_msg.is_empty() {
            queue!(
                stdout,
                cursor::MoveTo(0, detail_row + 2),
                style::PrintStyledContent(style::style(&*status_msg).green().bold())
            )?;
            status_msg.clear();
        }

        stdout.flush()?;

        if let Event::Key(key) = event::read()? {
            match (key.code, key.modifiers) {
                (KeyCode::Char('q'), _) | (KeyCode::Esc, _) => return Ok(None),
                (KeyCode::Char('c'), KeyModifiers::CONTROL) => return Ok(None),
                (KeyCode::Up, _) | (KeyCode::Char('k'), _) => {
                    selected = selected.saturating_sub(1);
                }
                (KeyCode::Down, _) | (KeyCode::Char('j'), _) => {
                    if selected < items.len() - 1 {
                        selected += 1;
                    }
                }
                (KeyCode::Char('e'), _) | (KeyCode::Enter, _) => {
                    return Ok(Some(items[selected].sha.clone()));
                }
                (KeyCode::Char('s'), _) => {
                    let sha = items[selected].sha.clone();
                    execute!(stdout, terminal::LeaveAlternateScreen, cursor::Show)?;
                    terminal::disable_raw_mode()?;

                    std::process::Command::new("git")
                        .args(["show", "--stat", &sha])
                        .status()
                        .ok();

                    println!("\nPress Enter to return...");
                    let mut buf = String::new();
                    io::stdin().read_line(&mut buf).ok();

                    terminal::enable_raw_mode()?;
                    execute!(stdout, terminal::EnterAlternateScreen, cursor::Hide)?;
                    status_msg = format!("git show {}", &sha[..8]);
                }
                _ => {}
            }
        }
    }
}

pub fn build_picker_items(items: &[ActivityItem], filter_unendorsed: bool) -> Vec<PickerItem> {
    let mut picker: Vec<PickerItem> = items
        .iter()
        .filter(|i| {
            if filter_unendorsed {
                !matches!(
                    i.endorsement_status,
                    EndorsementStatus::Endorsed | EndorsementStatus::Excluded
                )
            } else {
                !matches!(i.endorsement_status, EndorsementStatus::Excluded)
            }
        })
        .map(PickerItem::from_activity)
        .collect();

    picker.sort_by(|a, b| {
        let a_risk = a.classification == "risk";
        let b_risk = b.classification == "risk";
        b_risk.cmp(&a_risk).then(b.zombie.cmp(&a.zombie)).then(
            b.friction
                .partial_cmp(&a.friction)
                .unwrap_or(std::cmp::Ordering::Equal),
        )
    });

    picker
}