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
421
422
423
424
425
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>,
    },

    #[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,
    },
}

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 } => {
            endorse_command(sha.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),
        },
    }

    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) -> 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(),
    };

    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>) -> Result<()> {
    match sha {
        Some(s) => {
            let resolved = resolve_sha(s)?;
            do_endorse(&resolved)
        }
        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) => do_endorse(&sha)?,
                }
            }
            Ok(())
        }
    }
}

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!("  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);
                }
            }
            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) => do_endorse(&sha)?,
        }
    }

    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 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(())
}