git-cognitive 0.2.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
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
mod cognitive_debt;
mod db;
mod index;
mod picker;
mod session;
mod treesitter;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "git-cognitive")]
#[command(
    version,
    about = "Cognitive debt detection and management for Git repositories"
)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    #[command(about = "Index commits for cognitive debt")]
    Index {
        #[arg(long, help = "Index a specific commit SHA or HEAD")]
        commit: Option<String>,

        #[arg(long, help = "Index all commits since this SHA")]
        since: Option<String>,

        #[arg(long, help = "Backfill all history (last 500 commits)")]
        all: bool,

        #[arg(long, help = "Scan for zombie AI commits (>30 days unendorsed)")]
        check_zombies: bool,
    },

    #[command(about = "Endorse a commit as understood and vouched for")]
    Endorse {
        #[arg(help = "Commit SHA or HEAD (omit for interactive picker)")]
        sha: Option<String>,

        #[arg(long, help = "One sentence explaining what this commit does")]
        reason: Option<String>,
    },

    #[command(about = "Show cognitive debt — flat list of commits with friction and status")]
    Debt {
        #[arg(long, help = "Open interactive picker to endorse items")]
        interactive: bool,
    },

    #[command(about = "Show activity item details and endorsement history for a commit")]
    Show {
        #[arg(help = "Commit SHA or HEAD")]
        sha: String,
    },

    #[command(about = "Show the session slice captured for a commit")]
    Session {
        #[arg(help = "Commit SHA or HEAD")]
        sha: String,
    },

    #[command(about = "Push cognitive debt data to origin")]
    Push,

    #[command(about = "Pull cognitive debt data from origin")]
    Pull,

    #[command(about = "Enable a coding agent for this project (e.g. claude)")]
    Enable {
        #[arg(help = "Agent to enable: claude")]
        agent: String,
    },

    #[command(about = "Start the MCP server (JSON-RPC over stdio)")]
    Mcp,
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    match cli.command {
        Commands::Index {
            commit,
            since,
            all,
            check_zombies,
        } => {
            let repo_path = PathBuf::from(".");
            let since = if all {
                Some("HEAD~500".to_string())
            } else {
                since
            };
            index::run_index(
                &repo_path,
                since.as_deref(),
                commit.as_deref(),
                check_zombies,
            )?;
        }
        Commands::Endorse { sha, reason } => {
            endorse_command(sha.as_deref(), reason.as_deref())?;
        }
        Commands::Debt { interactive } => {
            if interactive {
                debt_interactive()?;
            } else {
                debt_command()?;
            }
        }
        Commands::Show { sha } => {
            let resolved = resolve_sha(&sha)?;
            show_command(&resolved)?;
        }
        Commands::Session { sha } => {
            let repo_path = PathBuf::from(".");
            let resolved = resolve_sha(&sha)?;
            session::run_show_session(&repo_path, &resolved)?;
        }
        Commands::Push => sync_push()?,
        Commands::Pull => sync_pull()?,
        Commands::Enable { agent } => match agent.as_str() {
            "claude" => enable_claude()?,
            other => anyhow::bail!("Unknown agent '{}'. Supported: claude", other),
        },
        Commands::Mcp => mcp_serve()?,
    }

    Ok(())
}

fn resolve_sha(sha: &str) -> Result<String> {
    if sha == "HEAD" {
        let out = std::process::Command::new("git")
            .args(["rev-parse", "HEAD"])
            .output()
            .context("Failed to resolve HEAD")?;
        Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
    } else {
        Ok(sha.to_string())
    }
}

fn do_endorse(sha: &str, reason: Option<&str>) -> Result<()> {
    use cognitive_debt::{DebtStore, EndorsementRecord, EndorsementStatus};

    let author = std::process::Command::new("git")
        .args(["config", "user.email"])
        .output()
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .unwrap_or_else(|_| "unknown".to_string());

    let repo_path = PathBuf::from(".");
    let store = DebtStore::open(&repo_path)?;
    let record = EndorsementRecord {
        sha: sha.to_string(),
        status: EndorsementStatus::Endorsed,
        author,
        timestamp: cognitive_debt::now_rfc3339(),
        reason: reason.map(|s| s.to_string()),
    };

    store.write_endorsement(&record)?;
    let db = db::Database::init()?;
    db.insert_endorsement(&record)?;
    store.commit()?;
    sync_push().ok();

    println!("Endorsed {}.", &sha[..8.min(sha.len())]);
    Ok(())
}

fn endorse_command(sha: Option<&str>, reason: Option<&str>) -> Result<()> {
    match sha {
        Some(s) => {
            let resolved = resolve_sha(s)?;
            let reason = match reason {
                Some(r) => Some(r.to_string()),
                None => prompt_reason()?,
            };
            do_endorse(&resolved, reason.as_deref())
        }
        None => {
            loop {
                let db = db::Database::init()?;
                let items = db.all_activity_items()?;
                let picker_items = picker::build_picker_items(&items, true);

                if picker_items.is_empty() {
                    println!("No unendorsed items remaining.");
                    break;
                }

                match picker::run_picker(picker_items)? {
                    None => break,
                    Some(sha) => {
                        let reason = prompt_reason()?;
                        do_endorse(&sha, reason.as_deref())?;
                    }
                }
            }
            Ok(())
        }
    }
}

fn prompt_reason() -> Result<Option<String>> {
    use std::io::Write;
    print!("  reason (one sentence, or Enter to skip): ");
    std::io::stdout().flush()?;
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf)?;
    let trimmed = buf.trim().to_string();
    if trimmed.is_empty() {
        Ok(None)
    } else {
        Ok(Some(trimmed))
    }
}

fn show_command(sha: &str) -> Result<()> {
    let repo_path = PathBuf::from(".");

    let item = cognitive_debt::read_activity_from_branch(&repo_path, sha)?;
    let endorsements = cognitive_debt::read_endorsements_from_branch(&repo_path, sha)?;

    match item {
        None => {
            println!("No activity item found for {}.", &sha[..8.min(sha.len())]);
            println!(
                "Run `git-cognitive index --commit {}` first.",
                &sha[..8.min(sha.len())]
            );
        }
        Some(item) => {
            println!();
            println!("  commit   {}", item.id);
            println!("  branch   {}", item.branch);
            println!("  title    {}", item.title);
            if !item.summary.is_empty() {
                println!("  summary  {}", item.summary);
            }
            println!();
            println!("  class    {}", item.classification);
            println!("  friction {:.2}", item.cognitive_friction_score);
            if let Some(pct) = item.attribution_pct {
                println!("  agent    {:.0}%", pct * 100.0);
            }
            println!("  lines    {}", item.lines_changed);
            if item.large_diff {
                println!("  large_diff  yes (>100 lines, AI-attributed)");
            }
            if let Some(dur) = item.session_duration_secs {
                println!("  session  {}h {}m", dur / 3600, (dur % 3600) / 60);
            }
            if item.fatigue {
                println!("  fatigue  yes (commit after 3h+ session)");
            }
            println!("  zombie   {}", if item.zombie { "yes" } else { "no" });
            println!("  status   {}", item.endorsement_status);
            println!("  audited  {}", item.audited_at);
            println!();

            if endorsements.is_empty() {
                println!("  No endorsements yet.");
            } else {
                println!("  Endorsements ({}):", endorsements.len());
                for e in &endorsements {
                    println!("    {}  {}  {}", e.timestamp, e.status, e.author);
                    if let Some(r) = &e.reason {
                        println!("      \"{}\"", r);
                    }
                }
            }
            println!();
        }
    }

    Ok(())
}

fn debt_interactive() -> Result<()> {
    loop {
        let db = db::Database::init().context("Failed to initialize database")?;
        let items = db.all_activity_items()?;

        if items.is_empty() {
            println!("No activity items. Run `git-cognitive index` first.");
            break;
        }

        let picker_items = picker::build_picker_items(&items, false);

        match picker::run_picker(picker_items)? {
            None => break,
            Some(sha) => {
                let reason = prompt_reason()?;
                do_endorse(&sha, reason.as_deref())?;
            }
        }
    }

    Ok(())
}

fn debt_command() -> Result<()> {
    use cognitive_debt::{Classification, EndorsementStatus};

    let db = db::Database::init().context("Failed to initialize database")?;
    let items = db
        .all_activity_items()
        .context("Failed to load activity items")?;

    if items.is_empty() {
        println!("No activity items found. Run `git-cognitive index` first.");
        return Ok(());
    }

    let visible: Vec<_> = items
        .iter()
        .filter(|i| !matches!(i.endorsement_status, EndorsementStatus::Excluded))
        .collect();

    println!(
        "\n{:<10} {:<12} {:<10} {:<8} {:<59} STATUS",
        "COMMIT", "CLASS", "FRICTION", "AI", "TITLE"
    );
    println!("{}", "-".repeat(105));

    for item in &visible {
        let status = if item.zombie {
            "\x1B[31mZOMBIE\x1B[0m"
        } else {
            match item.endorsement_status {
                EndorsementStatus::Endorsed => "\x1B[32mendorsed\x1B[0m",
                _ => "\x1B[31munendorsed\x1B[0m",
            }
        };

        let ai = item
            .attribution_pct
            .map(|p| format!("{:3.0}%", p * 100.0))
            .unwrap_or_else(|| {
                if item.ai_attributed {
                    " ai ".to_string()
                } else {
                    "    ".to_string()
                }
            });

        println!(
            "{:<10} {:<12} {:<10} {:<8} {:<59} {}",
            &item.id[..8.min(item.id.len())],
            &item.classification.to_string()[..12.min(item.classification.to_string().len())],
            format!("{:.2}", item.cognitive_friction_score),
            ai,
            &item.title[..59.min(item.title.len())],
            status,
        );
    }

    println!();

    let risk_items: Vec<_> = visible
        .iter()
        .filter(|i| {
            matches!(i.classification, Classification::Risk)
                && !matches!(i.endorsement_status, EndorsementStatus::Endorsed)
        })
        .collect();

    if !risk_items.is_empty() {
        println!("  {} unendorsed RISK item(s):", risk_items.len());
        for item in risk_items.iter().take(5) {
            println!("   {} {}", &item.id[..8.min(item.id.len())], item.title);
        }
        println!();
    }

    let zombie_items: Vec<_> = visible.iter().filter(|i| i.zombie).collect();
    if !zombie_items.is_empty() {
        println!("  {} zombie(s) detected:", zombie_items.len());
        for item in zombie_items.iter().take(5) {
            println!("   {} {}", &item.id[..8.min(item.id.len())], item.title);
        }
        println!();
    }

    Ok(())
}

fn sync_push() -> Result<()> {
    let out = std::process::Command::new("git")
        .args(["push", "origin", "cognitive/v1"])
        .output()
        .context("Failed to run git push")?;
    if out.status.success() {
        println!("Pushed cognitive/v1 to origin.");
    } else {
        let stderr = String::from_utf8_lossy(&out.stderr);
        anyhow::bail!("Push failed: {}", stderr.trim());
    }
    Ok(())
}

fn sync_pull() -> Result<()> {
    let out = std::process::Command::new("git")
        .args(["fetch", "origin", "cognitive/v1:cognitive/v1"])
        .output()
        .context("Failed to run git fetch")?;
    if out.status.success() {
        println!("Pulled cognitive/v1 from origin.");
        println!("Run `git-cognitive debt` to see the updated state.");
    } else {
        let stderr = String::from_utf8_lossy(&out.stderr);
        anyhow::bail!("Pull failed: {}", stderr.trim());
    }
    Ok(())
}

fn mcp_serve() -> Result<()> {
    use std::io::{BufRead, Write};

    let stdin = std::io::stdin();
    let stdout = std::io::stdout();
    let mut out = std::io::BufWriter::new(stdout.lock());

    for line in stdin.lock().lines() {
        let line = match line {
            Ok(l) if l.trim().is_empty() => continue,
            Ok(l) => l,
            Err(_) => break,
        };

        let req: serde_json::Value = match serde_json::from_str(&line) {
            Ok(v) => v,
            Err(_) => continue,
        };

        let id = req.get("id").cloned().unwrap_or(serde_json::Value::Null);
        let method = req["method"].as_str().unwrap_or("");

        let response = match method {
            "initialize" => mcp_ok(
                id,
                serde_json::json!({
                    "protocolVersion": "2024-11-05",
                    "capabilities": { "tools": {} },
                    "serverInfo": { "name": "git-cognitive", "version": env!("CARGO_PKG_VERSION") }
                }),
            ),
            "notifications/initialized" => continue,
            "tools/list" => mcp_ok(
                id,
                serde_json::json!({
                    "tools": [
                        {
                            "name": "debt",
                            "description": "List all indexed commits with their cognitive friction score, AI attribution, classification, and endorsement status. Use this to get an overview of the repo's cognitive debt.",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "filter": {
                                        "type": "string",
                                        "enum": ["all", "unendorsed", "risk", "zombie"],
                                        "description": "Filter results. Default: all."
                                    }
                                }
                            }
                        },
                        {
                            "name": "show",
                            "description": "Get full details for a commit: classification, friction score, AI attribution percentage, summary, and endorsement history.",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sha": { "type": "string", "description": "Commit SHA or HEAD" }
                                },
                                "required": ["sha"]
                            }
                        },
                        {
                            "name": "endorse",
                            "description": "Endorse a commit as understood and vouched for. Records the endorsement with the git user identity. Provide a reason — one sentence explaining what the commit does. If you cannot write it, do not endorse.",
                            "inputSchema": {
                                "type": "object",
                                "properties": {
                                    "sha": { "type": "string", "description": "Commit SHA or HEAD" },
                                    "reason": { "type": "string", "description": "One sentence explaining what this commit does" }
                                },
                                "required": ["sha"]
                            }
                        }
                    ]
                }),
            ),
            "tools/call" => {
                let name = req["params"]["name"].as_str().unwrap_or("");
                let args = &req["params"]["arguments"];
                match mcp_dispatch(name, args) {
                    Ok(data) => mcp_ok(
                        id,
                        serde_json::json!({
                            "content": [{ "type": "text", "text": serde_json::to_string_pretty(&data).unwrap_or_default() }],
                            "structuredContent": data
                        }),
                    ),
                    Err(e) => mcp_err(id, -32000, &e.to_string()),
                }
            }
            _ => mcp_err(id, -32601, "method not found"),
        };

        writeln!(out, "{}", serde_json::to_string(&response)?)?;
        out.flush()?;
    }

    Ok(())
}

fn mcp_ok(id: serde_json::Value, result: serde_json::Value) -> serde_json::Value {
    serde_json::json!({ "jsonrpc": "2.0", "id": id, "result": result })
}

fn mcp_err(id: serde_json::Value, code: i32, msg: &str) -> serde_json::Value {
    serde_json::json!({ "jsonrpc": "2.0", "id": id, "error": { "code": code, "message": msg } })
}

fn mcp_dispatch(name: &str, args: &serde_json::Value) -> Result<serde_json::Value> {
    match name {
        "debt" => {
            let filter = args["filter"].as_str().unwrap_or("all");
            mcp_debt(filter)
        }
        "show" => {
            let sha = args["sha"]
                .as_str()
                .ok_or_else(|| anyhow::anyhow!("missing required argument: sha"))?;
            let resolved = resolve_sha(sha)?;
            mcp_show(&resolved)
        }
        "endorse" => {
            let sha = args["sha"]
                .as_str()
                .ok_or_else(|| anyhow::anyhow!("missing required argument: sha"))?;
            let reason = args["reason"].as_str();
            let resolved = resolve_sha(sha)?;
            do_endorse(&resolved, reason)?;
            Ok(serde_json::json!({ "sha": resolved, "status": "endorsed", "reason": reason }))
        }
        _ => anyhow::bail!("unknown tool: {}", name),
    }
}

fn mcp_debt(filter: &str) -> Result<serde_json::Value> {
    use cognitive_debt::{Classification, EndorsementStatus};

    let db = db::Database::init()?;
    let items = db.all_activity_items()?;

    let visible: Vec<_> = items
        .iter()
        .filter(|i| !matches!(i.endorsement_status, EndorsementStatus::Excluded))
        .filter(|i| match filter {
            "unendorsed" => !matches!(i.endorsement_status, EndorsementStatus::Endorsed),
            "risk" => matches!(i.classification, Classification::Risk),
            "zombie" => i.zombie,
            _ => true,
        })
        .map(|i| {
            serde_json::json!({
                "sha": i.id,
                "branch": i.branch,
                "title": i.title,
                "classification": i.classification.to_string(),
                "friction": i.cognitive_friction_score,
                "ai_attributed": i.ai_attributed,
                "attribution_pct": i.attribution_pct,
                "lines_changed": i.lines_changed,
                "large_diff": i.large_diff,
                "session_duration_secs": i.session_duration_secs,
                "fatigue": i.fatigue,
                "zombie": i.zombie,
                "status": i.endorsement_status.to_string(),
                "audited_at": i.audited_at,
            })
        })
        .collect();

    Ok(serde_json::json!({
        "items": visible,
        "total": visible.len(),
    }))
}

fn mcp_show(sha: &str) -> Result<serde_json::Value> {
    let repo_path = PathBuf::from(".");

    let item = cognitive_debt::read_activity_from_branch(&repo_path, sha)?;
    let endorsements = cognitive_debt::read_endorsements_from_branch(&repo_path, sha)?;

    match item {
        None => Ok(serde_json::json!({
            "error": "not_found",
            "sha": sha,
            "hint": format!("Run `git-cognitive index --commit {}` first.", &sha[..8.min(sha.len())])
        })),
        Some(item) => {
            let endorsements_json: Vec<_> = endorsements
                .iter()
                .map(|e| {
                    serde_json::json!({
                        "author": e.author,
                        "status": e.status.to_string(),
                        "timestamp": e.timestamp,
                    })
                })
                .collect();

            Ok(serde_json::json!({
                "sha": item.id,
                "branch": item.branch,
                "title": item.title,
                "summary": item.summary,
                "classification": item.classification.to_string(),
                "friction": item.cognitive_friction_score,
                "ai_attributed": item.ai_attributed,
                "attribution_pct": item.attribution_pct,
                "lines_changed": item.lines_changed,
                "large_diff": item.large_diff,
                "session_duration_secs": item.session_duration_secs,
                "fatigue": item.fatigue,
                "zombie": item.zombie,
                "status": item.endorsement_status.to_string(),
                "audited_at": item.audited_at,
                "endorsements": endorsements_json,
            }))
        }
    }
}

fn enable_claude() -> Result<()> {
    let git_hooks_dir = PathBuf::from(".git/hooks");
    if !git_hooks_dir.exists() {
        anyhow::bail!("No .git/hooks directory found — are you in a git repository?");
    }

    // --- post-commit hook ---
    let post_commit = git_hooks_dir.join("post-commit");
    let post_commit_script = "#!/bin/sh\nsleep 2\ngit-cognitive index --commit HEAD 2>/dev/null || true\ngit-cognitive push 2>/dev/null || true\n";

    let should_write = if post_commit.exists() {
        let existing = std::fs::read_to_string(&post_commit).unwrap_or_default();
        !existing.contains("git-cognitive index")
    } else {
        true
    };

    if should_write {
        if post_commit.exists() {
            let existing = std::fs::read_to_string(&post_commit).unwrap_or_default();
            let appended = format!(
                "{}\n# git-cognitive cognitive debt index\nsleep 2\ngit-cognitive index --commit HEAD 2>/dev/null || true\ngit-cognitive push 2>/dev/null || true\n",
                existing.trim()
            );
            std::fs::write(&post_commit, appended)?;
        } else {
            std::fs::write(&post_commit, post_commit_script)?;
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&post_commit, std::fs::Permissions::from_mode(0o755))
                .context("Failed to set post-commit hook permissions")?;
        }
        println!("  wrote .git/hooks/post-commit");
    } else {
        println!("  .git/hooks/post-commit already configured — nothing to do.");
    }

    println!("\nDone. Every commit will now:");
    println!("  • snapshot the active Claude session");
    println!("  • attribute AI lines to that commit");
    println!("  • score friction and classify");
    Ok(())
}